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.cm.test;
24 import static org.hamcrest.CoreMatchers.is;
25 import static org.junit.Assert.assertThat;
26 import static org.junit.Assert.assertTrue;
27 import static org.junit.Assert.fail;
28 import static org.mockito.Mockito.when;
31 import java.io.IOException;
32 import java.security.KeyStore;
33 import java.security.KeyStoreException;
34 import java.security.NoSuchAlgorithmException;
35 import java.security.cert.CertificateException;
36 import java.util.ArrayList;
37 import java.util.List;
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;
49 import certman.v1_0.Artifacts.Artifact;
50 import certman.v1_0.CertInfo;
52 public class JU_ArtifactDir {
54 @Mock private Trans transMock;
55 @Mock private CertInfo certInfoMock;
56 @Mock private Artifact artiMock;
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
62 private List<String> issuers;
66 MockitoAnnotations.initMocks(this);
68 issuers = new ArrayList<>();
69 issuers.add("issuer1");
70 issuers.add("issuer2");
74 public static void tearDownOnce() {
80 public void test() throws CadiException, IOException, KeyStoreException, NoSuchAlgorithmException, CertificateException {
81 ArtifactDirStud artiDir = new ArtifactDirStud();
84 artiDir.place(transMock, certInfoMock, artiMock, "machine");
85 fail("Should've thrown an exception");
86 } catch (CadiException e) {
87 assertThat(e.getMessage(), is("File Artifacts require a path\nFile Artifacts require an AAF Namespace"));
90 when(artiMock.getDir()).thenReturn(dirName);
92 artiDir.place(transMock, certInfoMock, artiMock, "machine");
93 fail("Should've thrown an exception");
94 } catch (CadiException e) {
95 assertThat(e.getMessage(), is("File Artifacts require an AAF Namespace"));
98 when(artiMock.getNs()).thenReturn(nsName);
99 when(certInfoMock.getCaIssuerDNs()).thenReturn(issuers);
100 when(certInfoMock.getChallenge()).thenReturn(luggagePassword);
101 artiDir.place(transMock, certInfoMock, artiMock, "machine");
103 File writableFile = new File(dirName + '/' + nsName + "writable.txt");
104 artiDir.write(writableFile, Chmod.to755, "first data point", "second data point");
106 artiDir.write(writableFile, Chmod.to755, (String[])null);
107 fail("Should've thrown an exception");
108 } catch(NullPointerException e) {
111 KeyStore ks = KeyStore.getInstance("pkcs12");
113 artiDir.write(writableFile, Chmod.to755, ks, luggagePassword.toCharArray());
114 fail("Should've thrown an exception");
115 } catch(CadiException e) {
119 artiDir.write(writableFile, Chmod.to755, ks, luggagePassword.toCharArray());
121 ArtifactDirStud artiDir2 = new ArtifactDirStud();
122 artiDir2.place(transMock, certInfoMock, artiMock, "machine");
125 artiDir.place(transMock, certInfoMock, artiMock, "machine");
128 artiDir.place(transMock, certInfoMock, artiMock, "machine");
133 public void throwsTest() throws CadiException {
134 ArtifactDirStud artiDir = new ArtifactDirStud();
135 when(artiMock.getDir()).thenReturn(dirName);
136 when(artiMock.getNs()).thenReturn(nsName);
137 assertTrue(artiDir.place(transMock, certInfoMock, artiMock, "machine"));
140 private class ArtifactDirStud extends ArtifactDir {
142 protected boolean _place(Trans trans, CertInfo certInfo, Artifact arti) throws CadiException {
143 // This is only here so that we have a concrete class to test
147 // Expose the protected methods
149 public void write(File f, Chmod c, String ... data) throws IOException {
150 super.write(f, c, data);
152 public void write(File f, Chmod c, KeyStore ks, char[] pass ) throws IOException, CadiException {
153 super.write(f, c, ks, pass);
157 private static void cleanup() {
158 File dir = new File(dirName);
160 for (File f : dir.listFiles()) {