Sonar Fixes, Formatting
[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.configure.Agent;
46 import org.onap.aaf.cadi.configure.ArtifactDir;
47 import org.onap.aaf.cadi.util.Chmod;
48 import org.onap.aaf.misc.env.Trans;
49
50 import certman.v1_0.Artifacts.Artifact;
51 import certman.v1_0.CertInfo;
52
53 public class JU_ArtifactDir {
54
55     @Mock private Trans transMock;
56     @Mock private CertInfo certInfoMock;
57     @Mock private Artifact artiMock;
58
59     private static final String dirName = "src/test/resources/artifacts";
60     private static final String nsName = "org.onap.test";
61     private static final String luggagePassword = "12345";  // That's the stupidest combination I've ever heard in my life
62
63     private List<String> issuers;
64
65     @Before
66     public void setup() {
67         MockitoAnnotations.initMocks(this);
68
69         issuers = new ArrayList<>();
70         issuers.add("issuer1");
71         issuers.add("issuer2");
72     }
73
74     @After
75     public void tearDown() {
76         ArtifactDir.clear();
77     }
78
79     @AfterClass
80     public static void tearDownOnce() {
81         cleanup();
82     }
83
84     @Test
85     public void test() throws CadiException, IOException, KeyStoreException, NoSuchAlgorithmException, CertificateException {
86         ArtifactDirStud artiDir = new ArtifactDirStud();
87
88         try {
89             artiDir.place(transMock, certInfoMock, artiMock, "machine");
90             fail("Should've thrown an exception");
91         } catch (CadiException e) {
92             assertThat(e.getMessage(), is("File Artifacts require a path\nFile Artifacts require an AAF Namespace"));
93         }
94
95         when(artiMock.getDir()).thenReturn(dirName);
96         try {
97             artiDir.place(transMock, certInfoMock, artiMock, "machine");
98             fail("Should've thrown an exception");
99         } catch (CadiException e) {
100             assertThat(e.getMessage(), is("File Artifacts require an AAF Namespace"));
101         }
102
103         when(artiMock.getNs()).thenReturn(nsName);
104         when(certInfoMock.getCaIssuerDNs()).thenReturn(issuers);
105         when(certInfoMock.getChallenge()).thenReturn(luggagePassword);
106         artiDir.place(transMock, certInfoMock, artiMock, "machine");
107
108         File writableFile = new File(dirName + '/' + nsName + "writable.txt");
109         ArtifactDir.write(writableFile, Chmod.to755, "first data point", "second data point");
110         try {
111             ArtifactDir.write(writableFile, Chmod.to755, (String[])null);
112             fail("Should've thrown an exception");
113         } catch (NullPointerException e) {
114         }
115
116         KeyStore ks = KeyStore.getInstance(Agent.PKCS12);
117         try {
118             ArtifactDir.write(writableFile, Chmod.to755, ks, luggagePassword.toCharArray());
119             fail("Should've thrown an exception");
120         } catch (CadiException e) {
121         }
122
123         ks.load(null, null);
124         ArtifactDir.write(writableFile, Chmod.to755, ks, luggagePassword.toCharArray());
125
126         ArtifactDirStud artiDir2 = new ArtifactDirStud();
127         artiDir2.place(transMock, certInfoMock, artiMock, "machine");
128
129         // coverage
130         artiDir.place(transMock, certInfoMock, artiMock, "machine");
131
132         ArtifactDir.clear();
133         artiDir.place(transMock, certInfoMock, artiMock, "machine");
134
135     }
136
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
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 }