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.junit.Assert.*;
25 import static org.hamcrest.CoreMatchers.*;
26 import static org.mockito.Mockito.*;
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;
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;
45 import certman.v1_0.Artifacts.Artifact;
46 import certman.v1_0.CertInfo;
48 public class JU_ArtifactDir {
50 @Mock private Trans transMock;
51 @Mock private CertInfo certInfoMock;
52 @Mock private Artifact artiMock;
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
58 private List<String> issuers;
62 MockitoAnnotations.initMocks(this);
64 issuers = new ArrayList<>();
65 issuers.add("issuer1");
66 issuers.add("issuer2");
70 public static void tearDownOnce() {
76 public void test() throws CadiException, IOException, KeyStoreException, NoSuchAlgorithmException, CertificateException {
77 ArtifactDirStud artiDir = new ArtifactDirStud();
80 artiDir.place(transMock, certInfoMock, artiMock, "machine");
81 fail("Should've thrown an exception");
82 } catch (CadiException e) {
83 assertThat(e.getMessage(), is("File Artifacts require a path\nFile Artifacts require an AAF Namespace"));
86 when(artiMock.getDir()).thenReturn(dirName);
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 an AAF Namespace"));
94 when(artiMock.getNs()).thenReturn(nsName);
95 when(certInfoMock.getCaIssuerDNs()).thenReturn(issuers);
96 when(certInfoMock.getChallenge()).thenReturn(luggagePassword);
97 artiDir.place(transMock, certInfoMock, artiMock, "machine");
99 File writableFile = new File(dirName + '/' + nsName + "writable.txt");
100 artiDir.write(writableFile, Chmod.to755, "first data point", "second data point");
102 artiDir.write(writableFile, Chmod.to755, (String[])null);
103 fail("Should've thrown an exception");
104 } catch(NullPointerException e) {
107 KeyStore ks = KeyStore.getInstance("pkcs12");
109 artiDir.write(writableFile, Chmod.to755, ks, luggagePassword.toCharArray());
110 fail("Should've thrown an exception");
111 } catch(CadiException e) {
115 artiDir.write(writableFile, Chmod.to755, ks, luggagePassword.toCharArray());
117 ArtifactDirStud artiDir2 = new ArtifactDirStud();
118 artiDir2.place(transMock, certInfoMock, artiMock, "machine");
121 artiDir.place(transMock, certInfoMock, artiMock, "machine");
124 artiDir.place(transMock, certInfoMock, artiMock, "machine");
128 @Test(expected = CadiException.class)
129 public void throwsTest() throws CadiException {
130 ArtifactDirStud artiDir = new ArtifactDirStud();
131 when(artiMock.getDir()).thenReturn(dirName);
132 when(artiMock.getNs()).thenReturn(nsName);
133 artiDir.place(transMock, certInfoMock, artiMock, "machine");
136 private class ArtifactDirStud extends ArtifactDir {
138 protected boolean _place(Trans trans, CertInfo certInfo, Artifact arti) throws CadiException {
139 // This is only here so that we have a concrete class to test
143 // Expose the protected methods
145 public void write(File f, Chmod c, String ... data) throws IOException {
146 super.write(f, c, data);
148 public void write(File f, Chmod c, KeyStore ks, char[] pass ) throws IOException, CadiException {
149 super.write(f, c, ks, pass);
153 private static void cleanup() {
154 File dir = new File(dirName);
156 for (File f : dir.listFiles()) {