Improve coverage of cadi-aaf
[aaf/authz.git] / cadi / aaf / src / test / java / org / onap / aaf / cadi / persist / test / JU_Persist.java
1 /**
2  * ============LICENSE_START====================================================
3  * org.onap.aaf
4  * ===========================================================================
5  * Copyright (c) 2018 AT&T Intellectual Property. All rights reserved.
6  * ===========================================================================
7  * Licensed under the Apache License, Version 2.0 (the "License");
8  * you may not use this file except in compliance with the License.
9  * You may obtain a copy of the License at
10  *
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing, software
14  * distributed under the License is distributed on an "AS IS" BASIS,
15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  * See the License for the specific language governing permissions and
17  * limitations under the License.
18  * ============LICENSE_END====================================================
19  *
20  */
21
22 package org.onap.aaf.cadi.persist.test;
23
24 import static org.junit.Assert.assertThat;
25 import static org.hamcrest.CoreMatchers.is;
26 import static org.hamcrest.CoreMatchers.nullValue;
27 import static org.mockito.Mockito.when;
28 import static org.mockito.Mockito.doReturn;
29 import static org.mockito.Matchers.any;
30
31 import java.io.ByteArrayOutputStream;
32 import java.io.File;
33 import java.io.IOException;
34 import java.io.PrintStream;
35 import java.nio.file.Path;
36 import java.nio.file.Paths;
37
38 import org.junit.After;
39 import org.junit.Before;
40 import org.junit.Test;
41 import org.mockito.Mock;
42 import org.mockito.MockitoAnnotations;
43 import org.onap.aaf.cadi.Access;
44 import org.onap.aaf.cadi.CadiException;
45 import org.onap.aaf.cadi.LocatorException;
46 import org.onap.aaf.cadi.PropAccess;
47 import org.onap.aaf.cadi.client.Holder;
48 import org.onap.aaf.cadi.client.Result;
49 import org.onap.aaf.cadi.config.Config;
50 import org.onap.aaf.cadi.persist.Persist;
51 import org.onap.aaf.cadi.persist.Persist.Loader;
52 import org.onap.aaf.cadi.persist.Persistable;
53 import org.onap.aaf.cadi.persist.Persisting;
54 import org.onap.aaf.misc.env.APIException;
55 import org.onap.aaf.misc.rosetta.env.RosettaDF;
56 import org.onap.aaf.misc.rosetta.env.RosettaData;
57 import org.onap.aaf.misc.rosetta.env.RosettaEnv;
58
59 public class JU_Persist {
60
61         private static final String resourceDirString = "src/test/resources";
62         private static final String tokenDirString = "tokenDir";
63         private static final String key = "key";
64
65         private static final int data = 5;
66
67         private static final byte[] cred = "password".getBytes();
68
69         private PropAccess access;
70         private Result<Persistable<Integer>> result;
71
72         @Mock private RosettaEnv envMock;
73         @Mock private Persist<Integer, ?> persistMock;
74         @Mock private RosettaDF<Integer> dfMock;
75         @Mock private RosettaData<Integer> dataMock;
76         @Mock private Persistable<Integer> ctMock1;
77         @Mock private Persisting<Integer> ctMock2;
78         @Mock private Loader<Persistable<Integer>> loaderMock;
79
80         @Before
81         public void setup() throws APIException, CadiException, LocatorException {
82                 MockitoAnnotations.initMocks(this);
83
84                 doReturn(dfMock).when(envMock).newDataFactory((Class<?>[]) any());
85                 when(dfMock.newData()).thenReturn(dataMock);
86                 when(dataMock.load(data)).thenReturn(dataMock);
87
88
89                 result = Result.ok(200, ctMock1);
90                 when(loaderMock.load(key)).thenReturn(result);
91
92                 access = new PropAccess(new PrintStream(new ByteArrayOutputStream()), new String[0]);
93                 access.setProperty(Config.CADI_TOKEN_DIR, resourceDirString);
94         }
95
96         @After
97         public void tearDown() {
98                 File dir = new File(resourceDirString + '/' + tokenDirString);
99                 for (File f : dir.listFiles()) {
100                         f.delete();
101                 }
102                 dir.delete();
103         }
104
105         @Test
106         public void test() throws CadiException, APIException, LocatorException, InterruptedException {
107                 Persist<Integer, Persistable<Integer>> persist = new PersistStub(access, envMock, null, tokenDirString);
108                 // Second call for coverage
109                 persist = new PersistStub(access, envMock, null, tokenDirString);
110                 assertThat(persist.getDF(), is(dfMock));
111                 persist.put(key, ctMock2);
112                 Result<Persistable<Integer>> output = persist.get(key, cred, loaderMock);
113                 assertThat(output.code, is(200));
114                 assertThat(output.isOK(), is(true));
115
116                 when(ctMock2.checkSyncTime()).thenReturn(true);
117                 when(ctMock2.hasBeenTouched()).thenReturn(true);
118                 output = persist.get(key, cred, loaderMock);
119                 assertThat(output.code, is(200));
120                 assertThat(output.isOK(), is(true));
121
122                 persist.delete(key);
123
124                 assertThat(persist.get(null, null, null), is(nullValue()));
125
126                 // Uncommenting this lets us begin to test the nested Clean class, but
127                 // will dramatically slow down every build that runs tests - We need to
128                 // either refactor or find a more creative way to test Clean
129 //              Thread.sleep(25000);
130
131                 persist.close();
132         }
133
134         private class PersistStub extends Persist<Integer, Persistable<Integer>> {
135                 public PersistStub(Access access, RosettaEnv env, Class<Integer> cls, String sub_dir)
136                                 throws CadiException, APIException { super(access, env, cls, sub_dir); }
137                 @Override
138                 protected Persistable<Integer> newCacheable(Integer t, long expires_secsFrom1970, byte[] hash, Path path)
139                                 throws APIException, IOException { return null; }
140                 @Override
141                 public<T> Path writeDisk(final RosettaDF<T> df, final T t, final byte[] cred, final Path target, final long expires) throws CadiException {
142                         return null;
143                 }
144                 @SuppressWarnings("unchecked")
145                 @Override
146                 public <T> T readDisk(final RosettaDF<T> df, final byte[] cred, final String filename,final Holder<Path> hp, final Holder<Long> hl) throws CadiException {
147                         return (T)new Integer(data);
148                 }
149
150         }
151
152 }