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.mockito.Mockito.when;
27 import static org.mockito.Matchers.any;
29 import java.io.ByteArrayOutputStream;
31 import java.io.PrintStream;
32 import java.nio.file.Path;
33 import java.nio.file.Paths;
35 import javax.crypto.CipherInputStream;
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.CadiException;
43 import org.onap.aaf.cadi.PropAccess;
44 import org.onap.aaf.cadi.config.Config;
45 import org.onap.aaf.cadi.persist.Persist;
46 import org.onap.aaf.cadi.persist.PersistFile;
47 import org.onap.aaf.cadi.persist.Persisting;
48 import org.onap.aaf.misc.env.APIException;
49 import org.onap.aaf.misc.rosetta.env.RosettaDF;
50 import org.onap.aaf.misc.rosetta.env.RosettaData;
52 public class JU_Persisting {
54 private static final String resourceDirString = "src/test/resources";
55 private static final String tokenDirString = "tokenDir";
56 private static final String tokenFileName = "token";
58 private static final int data = 5;
59 private static final long expires = 10000;
61 private static final byte[] cred = "password".getBytes();
63 private PropAccess access;
65 @Mock private Persist<Integer, ?> persistMock;
66 @Mock private RosettaDF<Integer> dfMock;
67 @Mock private RosettaData<Integer> dataMock;
70 public void setup() throws APIException {
71 MockitoAnnotations.initMocks(this);
73 when(dfMock.newData()).thenReturn(dataMock);
74 when(dataMock.load(data)).thenReturn(dataMock);
75 when(dataMock.load((CipherInputStream)any())).thenReturn(dataMock);
77 access = new PropAccess(new PrintStream(new ByteArrayOutputStream()), new String[0]);
78 access.setProperty(Config.CADI_TOKEN_DIR, resourceDirString);
80 persistMock.access = access;
84 public void tearDown() {
85 File dir = new File(resourceDirString + '/' + tokenDirString);
86 for (File f : dir.listFiles()) {
93 public void test() throws CadiException, APIException {
94 Path tokenPath = Paths.get(resourceDirString, tokenDirString);
96 Persisting<Integer> persisting = new Persisting<>(persistMock, data, expires, cred, tokenPath);
97 assertThat(persisting.get(), is(data));
98 assertThat(persisting.expires(), is(expires));
99 assertThat(persisting.expired(), is(true));
100 assertThat(persisting.hasBeenTouched(), is(true));
102 PersistFile persistFile = new PersistFile(access, tokenDirString);
103 tokenPath = persistFile.writeDisk(dfMock, data, cred, tokenFileName, expires);
104 persisting = new Persisting<>(persistMock, data, expires, cred, tokenPath);
105 assertThat(persisting.hasBeenTouched(), is(false));
107 persisting = new Persisting<>(persistMock, data, expires * (int)10e9, cred, tokenPath);
108 assertThat(persisting.expired(), is(false));
110 assertThat(persisting.checkSyncTime(), is(true));
111 assertThat(persisting.checkSyncTime(), is(false));
113 assertThat(persisting.checkReloadable(), is(false));
115 assertThat(persisting.getHash(), is(cred));
117 assertThat(persisting.match(null), is(false));
118 assertThat(persisting.match("random!".getBytes()), is(false));
119 assertThat(persisting.match("passwrod".getBytes()), is(false));
120 assertThat(persisting.match(cred), is(true));
122 persisting.clearCount();
123 assertThat(persisting.count(), is(0));
125 assertThat(persisting.count(), is(1));
127 assertThat(persisting.path(), is(tokenPath));