Improve coverage of cadi-aaf
[aaf/authz.git] / cadi / aaf / src / test / java / org / onap / aaf / cadi / cm / test / JU_ArtifactDir.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.cm.test;
23
24 import static org.junit.Assert.*;
25 import static org.hamcrest.CoreMatchers.*;
26 import static org.mockito.Mockito.*;
27
28 import java.io.File;
29 import java.io.IOException;
30 import java.security.KeyStore;
31 import java.security.KeyStoreException;
32 import java.security.NoSuchAlgorithmException;
33 import java.security.cert.CertificateException;
34 import java.util.ArrayList;
35 import java.util.List;
36
37 import org.junit.*;
38 import org.mockito.*;
39
40 import org.onap.aaf.cadi.CadiException;
41 import org.onap.aaf.cadi.cm.ArtifactDir;
42 import org.onap.aaf.cadi.util.Chmod;
43 import org.onap.aaf.misc.env.Trans;
44
45 import certman.v1_0.Artifacts.Artifact;
46 import certman.v1_0.CertInfo;
47
48 public class JU_ArtifactDir {
49
50         @Mock private Trans transMock;
51         @Mock private CertInfo certInfoMock;
52         @Mock private Artifact artiMock;
53         
54         private static final String dirName = "src/test/resources/artifacts";
55         private static final String nsName = "org.onap.test";
56         private static final String luggagePassword = "12345";  // That's the stupidest combination I've ever heard in my life
57
58         private List<String> issuers;
59
60         @Before
61         public void setup() {
62                 MockitoAnnotations.initMocks(this);
63                 
64                 issuers = new ArrayList<>();
65                 issuers.add("issuer1");
66                 issuers.add("issuer2");
67         }
68         
69         @AfterClass
70         public static void tearDownOnce() {
71                 cleanup();
72         }
73
74         @Test
75         public void test() throws CadiException, IOException, KeyStoreException, NoSuchAlgorithmException, CertificateException {
76                 ArtifactDirStud artiDir = new ArtifactDirStud();
77
78                 try {
79                         artiDir.place(transMock, certInfoMock, artiMock, "machine");
80                         fail("Should've thrown an exception");
81                 } catch (CadiException e) {
82                         assertThat(e.getMessage(), is("File Artifacts require a path\nFile Artifacts require an AAF Namespace"));
83                 }
84                 
85                 when(artiMock.getDir()).thenReturn(dirName);
86                 try {
87                         artiDir.place(transMock, certInfoMock, artiMock, "machine");
88                         fail("Should've thrown an exception");
89                 } catch (CadiException e) {
90                         assertThat(e.getMessage(), is("File Artifacts require an AAF Namespace"));
91                 }
92                 
93                 when(artiMock.getNs()).thenReturn(nsName);
94                 when(certInfoMock.getCaIssuerDNs()).thenReturn(issuers);
95                 when(certInfoMock.getChallenge()).thenReturn(luggagePassword);
96                 artiDir.place(transMock, certInfoMock, artiMock, "machine");
97                 
98                 File writableFile = new File(dirName + '/' + nsName + "writable.txt");
99                 artiDir.write(writableFile, Chmod.to755, "first data point", "second data point");
100                 try {
101                         artiDir.write(writableFile, Chmod.to755, (String[])null);
102                         fail("Should've thrown an exception");
103                 } catch(NullPointerException e) {
104                 }
105                 
106                 KeyStore ks = KeyStore.getInstance("pkcs12");
107                 try {
108                         artiDir.write(writableFile, Chmod.to755, ks, luggagePassword.toCharArray());
109                         fail("Should've thrown an exception");
110                 } catch(CadiException e) {
111                 }
112                 
113                 ks.load(null, null);
114                 artiDir.write(writableFile, Chmod.to755, ks, luggagePassword.toCharArray());
115                 
116                 ArtifactDirStud artiDir2 = new ArtifactDirStud();
117                 artiDir2.place(transMock, certInfoMock, artiMock, "machine");
118
119                 // coverage
120                 artiDir.place(transMock, certInfoMock, artiMock, "machine");
121
122                 ArtifactDir.clear();
123                 artiDir.place(transMock, certInfoMock, artiMock, "machine");
124         
125         }
126
127         @Test(expected = CadiException.class)
128         public void throwsTest() throws CadiException {
129                 ArtifactDirStud artiDir = new ArtifactDirStud();
130                 when(artiMock.getDir()).thenReturn(dirName);
131                 when(artiMock.getNs()).thenReturn(nsName);
132                 artiDir.place(transMock, certInfoMock, artiMock, "machine");
133         }
134
135         private class ArtifactDirStud extends ArtifactDir {
136                 @Override
137                 protected boolean _place(Trans trans, CertInfo certInfo, Artifact arti) throws CadiException {
138                         // This is only here so that we have a concrete class to test
139                         return false;
140                 }
141                 
142                 // Expose the protected methods
143
144                 public  void write(File f, Chmod c, String ... data) throws IOException {
145                         super.write(f, c, data);
146                 }
147                 public void write(File f, Chmod c, KeyStore ks, char[] pass ) throws IOException, CadiException {
148                         super.write(f, c, ks, pass);
149                 }
150         }
151
152         private static void cleanup() {
153                 File dir = new File(dirName);
154                 if (dir.exists()) {
155                         for (File f : dir.listFiles()) {
156                                 f.delete();
157                         }
158                         dir.delete();
159                 }
160         }
161
162 }