Add a MassMail Batch Program
[aaf/authz.git] / cadi / aaf / src / test / java / org / onap / aaf / cadi / persist / test / JU_PersistFile.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.junit.Assert.fail;
26 import static org.hamcrest.CoreMatchers.is;
27 import static org.mockito.Mockito.when;
28 import static org.mockito.Matchers.any;
29
30 import java.io.ByteArrayOutputStream;
31 import java.io.File;
32 import java.io.IOException;
33 import java.io.PrintStream;
34 import java.nio.file.Path;
35 import java.nio.file.attribute.FileTime;
36
37 import javax.crypto.CipherInputStream;
38
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;
52
53 public class JU_PersistFile {
54
55     private static final String resourceDirString = "src/test/resources";
56     private static final String tokenDirString = "tokenDir";
57     private static final String tokenFileName = "token";
58
59     private static final int data = 5;
60     private static final long expires = 10000;
61
62     private static final byte[] cred = "password".getBytes();
63
64     private PropAccess access;
65     private Holder<Path> hp = new Holder<Path>(null);
66     private Holder<Long> hl = new Holder<Long>(null);
67
68     @Mock private RosettaDF<Integer> dfMock;
69     @Mock private RosettaData<Integer> dataMock;
70     @Mock private Holder<Path> hpMock;
71
72     @Before
73     public void setup() throws APIException {
74         MockitoAnnotations.initMocks(this);
75
76         when(dfMock.newData()).thenReturn(dataMock);
77         when(dataMock.load(data)).thenReturn(dataMock);
78         when(dataMock.load((CipherInputStream)any())).thenReturn(dataMock);
79
80         access = new PropAccess(new PrintStream(new ByteArrayOutputStream()), new String[0]);
81         access.setProperty(Config.CADI_TOKEN_DIR, resourceDirString);
82     }
83
84     @After
85     public void tearDown() {
86         File dir = new File(resourceDirString + '/' + tokenDirString);
87         for (File f : dir.listFiles()) {
88             f.delete();
89         }
90         dir.delete();
91     }
92
93     @Test
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));
101
102         FileTime ft1 = persistFile.getFileTime(tokenFileName, hp);
103         FileTime ft2 = persistFile.getFileTime(tokenFileName, hpMock);
104         assertThat(ft1.toMillis(), is(ft2.toMillis()));
105
106         persistFile.deleteFromDisk(filepath);
107         persistFile.deleteFromDisk(resourceDirString + '/' + tokenDirString + '/' + tokenFileName);
108         assertThat(persistFile.readExpiration(filepath), is(0L));
109
110         persistFile.getPath(resourceDirString + '/' + tokenDirString + '/' + tokenFileName);
111
112         persistFile.writeDisk(dfMock, data, null, tokenFileName, expires);
113         try {
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"));
118         }
119     }
120
121 }