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