Add a MassMail Batch Program
[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
37 import org.junit.After;
38 import org.junit.Before;
39 import org.junit.Test;
40 import org.mockito.Mock;
41 import org.mockito.MockitoAnnotations;
42 import org.onap.aaf.cadi.Access;
43 import org.onap.aaf.cadi.CadiException;
44 import org.onap.aaf.cadi.LocatorException;
45 import org.onap.aaf.cadi.PropAccess;
46 import org.onap.aaf.cadi.util.Holder;
47 import org.onap.aaf.cadi.client.Result;
48 import org.onap.aaf.cadi.config.Config;
49 import org.onap.aaf.cadi.persist.Persist;
50 import org.onap.aaf.cadi.persist.Persist.Loader;
51 import org.onap.aaf.cadi.persist.Persistable;
52 import org.onap.aaf.cadi.persist.Persisting;
53 import org.onap.aaf.misc.env.APIException;
54 import org.onap.aaf.misc.rosetta.env.RosettaDF;
55 import org.onap.aaf.misc.rosetta.env.RosettaData;
56 import org.onap.aaf.misc.rosetta.env.RosettaEnv;
57
58 public class JU_Persist {
59
60     private static final String resourceDirString = "src/test/resources";
61     private static final String tokenDirString = "tokenDir";
62     private static final String key = "key";
63
64     private static final int data = 5;
65
66     private static final byte[] cred = "password".getBytes();
67
68     private PropAccess access;
69     private Result<Persistable<Integer>> result;
70
71     @Mock private RosettaEnv envMock;
72     @Mock private Persist<Integer, ?> persistMock;
73     @Mock private RosettaDF<Integer> dfMock;
74     @Mock private RosettaData<Integer> dataMock;
75     @Mock private Persistable<Integer> ctMock1;
76     @Mock private Persisting<Integer> ctMock2;
77     @Mock private Loader<Persistable<Integer>> loaderMock;
78
79     @Before
80     public void setup() throws APIException, CadiException, LocatorException {
81         MockitoAnnotations.initMocks(this);
82
83         doReturn(dfMock).when(envMock).newDataFactory((Class<?>[]) any());
84         when(dfMock.newData()).thenReturn(dataMock);
85         when(dataMock.load(data)).thenReturn(dataMock);
86
87
88         result = Result.ok(200, ctMock1);
89         when(loaderMock.load(key)).thenReturn(result);
90
91         access = new PropAccess(new PrintStream(new ByteArrayOutputStream()), new String[0]);
92         access.setProperty(Config.CADI_TOKEN_DIR, resourceDirString);
93     }
94
95     @After
96     public void tearDown() {
97         File dir = new File(resourceDirString + '/' + tokenDirString);
98         for (File f : dir.listFiles()) {
99             f.delete();
100         }
101         dir.delete();
102     }
103
104     @Test
105     public void test() throws CadiException, APIException, LocatorException, InterruptedException {
106         Persist<Integer, Persistable<Integer>> persist = new PersistStub(access, envMock, null, tokenDirString);
107         // Second call for coverage
108         persist = new PersistStub(access, envMock, null, tokenDirString);
109         assertThat(persist.getDF(), is(dfMock));
110         persist.put(key, ctMock2);
111         Result<Persistable<Integer>> output = persist.get(key, cred, loaderMock);
112         assertThat(output.code, is(200));
113         assertThat(output.isOK(), is(true));
114
115         when(ctMock2.checkSyncTime()).thenReturn(true);
116         when(ctMock2.hasBeenTouched()).thenReturn(true);
117         output = persist.get(key, cred, loaderMock);
118         assertThat(output.code, is(200));
119         assertThat(output.isOK(), is(true));
120
121         persist.delete(key);
122
123         assertThat(persist.get(null, null, null), is(nullValue()));
124
125         // Uncommenting this lets us begin to test the nested Clean class, but
126         // will dramatically slow down every build that runs tests - We need to
127         // either refactor or find a more creative way to test Clean
128 //        Thread.sleep(25000);
129
130         persist.close();
131     }
132
133     private class PersistStub extends Persist<Integer, Persistable<Integer>> {
134         public PersistStub(Access access, RosettaEnv env, Class<Integer> cls, String sub_dir)
135                 throws CadiException, APIException { super(access, env, cls, sub_dir); }
136         @Override
137         protected Persistable<Integer> newCacheable(Integer t, long expires_secsFrom1970, byte[] hash, Path path)
138                 throws APIException, IOException { return null; }
139         @Override
140         public<T> Path writeDisk(final RosettaDF<T> df, final T t, final byte[] cred, final Path target, final long expires) throws CadiException {
141             return null;
142         }
143         @SuppressWarnings("unchecked")
144         @Override
145         public <T> T readDisk(final RosettaDF<T> df, final byte[] cred, final String filename,final Holder<Path> hp, final Holder<Long> hl) throws CadiException {
146             return (T)new Integer(data);
147         }
148
149     }
150
151 }