2 * ============LICENSE_START====================================================
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
11 * http://www.apache.org/licenses/LICENSE-2.0
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====================================================
22 package org.onap.aaf.cadi.persist.test;
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;
31 import java.io.ByteArrayOutputStream;
33 import java.io.IOException;
34 import java.io.PrintStream;
35 import java.nio.file.Path;
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;
58 public class JU_Persist {
60 private static final String resourceDirString = "src/test/resources";
61 private static final String tokenDirString = "tokenDir";
62 private static final String key = "key";
64 private static final int data = 5;
66 private static final byte[] cred = "password".getBytes();
68 private PropAccess access;
69 private Result<Persistable<Integer>> result;
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;
80 public void setup() throws APIException, CadiException, LocatorException {
81 MockitoAnnotations.initMocks(this);
83 doReturn(dfMock).when(envMock).newDataFactory((Class<?>[]) any());
84 when(dfMock.newData()).thenReturn(dataMock);
85 when(dataMock.load(data)).thenReturn(dataMock);
88 result = Result.ok(200, ctMock1);
89 when(loaderMock.load(key)).thenReturn(result);
91 access = new PropAccess(new PrintStream(new ByteArrayOutputStream()), new String[0]);
92 access.setProperty(Config.CADI_TOKEN_DIR, resourceDirString);
96 public void tearDown() {
97 File dir = new File(resourceDirString + '/' + tokenDirString);
98 for (File f : dir.listFiles()) {
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));
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));
123 assertThat(persist.get(null, null, null), is(nullValue()));
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);
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); }
137 protected Persistable<Integer> newCacheable(Integer t, long expires_secsFrom1970, byte[] hash, Path path)
138 throws APIException, IOException { return null; }
140 public<T> Path writeDisk(final RosettaDF<T> df, final T t, final byte[] cred, final Path target, final long expires) throws CadiException {
143 @SuppressWarnings("unchecked")
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);