Mass removal of all Tabs (Style Warnings)
[aaf/authz.git] / cadi / aaf / src / test / java / org / onap / aaf / cadi / persist / test / JU_Persisting.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.mockito.Mockito.when;
27 import static org.mockito.Matchers.any;
28
29 import java.io.ByteArrayOutputStream;
30 import java.io.File;
31 import java.io.PrintStream;
32 import java.nio.file.Path;
33 import java.nio.file.Paths;
34
35 import javax.crypto.CipherInputStream;
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.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;
51
52 public class JU_Persisting {
53
54     private static final String resourceDirString = "src/test/resources";
55     private static final String tokenDirString = "tokenDir";
56     private static final String tokenFileName = "token";
57
58     private static final int data = 5;
59     private static final long expires = 10000;
60
61     private static final byte[] cred = "password".getBytes();
62
63     private PropAccess access;
64
65     @Mock private Persist<Integer, ?> persistMock;
66     @Mock private RosettaDF<Integer> dfMock;
67     @Mock private RosettaData<Integer> dataMock;
68
69     @Before
70     public void setup() throws APIException {
71         MockitoAnnotations.initMocks(this);
72
73         when(dfMock.newData()).thenReturn(dataMock);
74         when(dataMock.load(data)).thenReturn(dataMock);
75         when(dataMock.load((CipherInputStream)any())).thenReturn(dataMock);
76
77         access = new PropAccess(new PrintStream(new ByteArrayOutputStream()), new String[0]);
78         access.setProperty(Config.CADI_TOKEN_DIR, resourceDirString);
79
80         persistMock.access = access;
81     }
82
83     @After
84     public void tearDown() {
85         File dir = new File(resourceDirString + '/' + tokenDirString);
86         for (File f : dir.listFiles()) {
87             f.delete();
88         }
89         dir.delete();
90     }
91
92     @Test
93     public void test() throws CadiException, APIException {
94         Path tokenPath = Paths.get(resourceDirString, tokenDirString);
95
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));
101
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));
106
107         persisting = new Persisting<>(persistMock, data, expires * (int)10e9, cred, tokenPath);
108         assertThat(persisting.expired(), is(false));
109
110         assertThat(persisting.checkSyncTime(), is(true));
111         assertThat(persisting.checkSyncTime(), is(false));
112
113         assertThat(persisting.checkReloadable(), is(false));
114
115         assertThat(persisting.getHash(), is(cred));
116
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));
121
122         persisting.clearCount();
123         assertThat(persisting.count(), is(0));
124         persisting.inc();
125         assertThat(persisting.count(), is(1));
126
127         assertThat(persisting.path(), is(tokenPath));
128     }
129
130 }