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.junit.Assert.fail;
26 import static org.hamcrest.CoreMatchers.is;
27 import static org.mockito.Mockito.when;
28 import static org.mockito.Matchers.any;
30 import java.io.ByteArrayOutputStream;
32 import java.io.IOException;
33 import java.io.PrintStream;
34 import java.nio.file.Path;
35 import java.nio.file.attribute.FileTime;
37 import javax.crypto.CipherInputStream;
39 import org.junit.After;
40 import org.junit.Before;
41 import org.junit.Test;
42 import org.mockito.Mock;
43 import org.mockito.MockitoAnnotations;
44 import org.onap.aaf.cadi.CadiException;
45 import org.onap.aaf.cadi.PropAccess;
46 import org.onap.aaf.cadi.util.Holder;
47 import org.onap.aaf.cadi.config.Config;
48 import org.onap.aaf.cadi.persist.PersistFile;
49 import org.onap.aaf.misc.env.APIException;
50 import org.onap.aaf.misc.rosetta.env.RosettaDF;
51 import org.onap.aaf.misc.rosetta.env.RosettaData;
53 public class JU_PersistFile {
55 private static final String resourceDirString = "src/test/resources";
56 private static final String tokenDirString = "tokenDir";
57 private static final String tokenFileName = "token";
59 private static final int data = 5;
60 private static final long expires = 10000;
62 private static final byte[] cred = "password".getBytes();
64 private PropAccess access;
65 private Holder<Path> hp = new Holder<Path>(null);
66 private Holder<Long> hl = new Holder<Long>(null);
68 @Mock private RosettaDF<Integer> dfMock;
69 @Mock private RosettaData<Integer> dataMock;
70 @Mock private Holder<Path> hpMock;
73 public void setup() throws APIException {
74 MockitoAnnotations.initMocks(this);
76 when(dfMock.newData()).thenReturn(dataMock);
77 when(dataMock.load(data)).thenReturn(dataMock);
78 when(dataMock.load((CipherInputStream)any())).thenReturn(dataMock);
80 access = new PropAccess(new PrintStream(new ByteArrayOutputStream()), new String[0]);
81 access.setProperty(Config.CADI_TOKEN_DIR, resourceDirString);
85 public void tearDown() {
86 File dir = new File(resourceDirString + '/' + tokenDirString);
87 for (File f : dir.listFiles()) {
94 public void test() throws CadiException, APIException, IOException {
95 PersistFile persistFile = new PersistFile(access, tokenDirString);
96 // Second call is for coverage
97 persistFile = new PersistFile(access, tokenDirString);
98 Path filepath = persistFile.writeDisk(dfMock, data, cred, tokenFileName, expires);
99 persistFile.readDisk(dfMock, cred, tokenFileName, hp, hl);
100 assertThat(persistFile.readExpiration(filepath), is(expires));
102 FileTime ft1 = persistFile.getFileTime(tokenFileName, hp);
103 FileTime ft2 = persistFile.getFileTime(tokenFileName, hpMock);
104 assertThat(ft1.toMillis(), is(ft2.toMillis()));
106 persistFile.deleteFromDisk(filepath);
107 persistFile.deleteFromDisk(resourceDirString + '/' + tokenDirString + '/' + tokenFileName);
108 assertThat(persistFile.readExpiration(filepath), is(0L));
110 persistFile.getPath(resourceDirString + '/' + tokenDirString + '/' + tokenFileName);
112 persistFile.writeDisk(dfMock, data, null, tokenFileName, expires);
114 persistFile.readDisk(dfMock, cred, tokenFileName, hp, hl);
115 fail("Should've thrown an exception");
116 } catch (CadiException e) {
117 assertThat(e.getMessage(), is(CadiException.class.getName() + ": Hash does not match in Persistence"));