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() {
75 public void test() throws CadiException, IOException, KeyStoreException, NoSuchAlgorithmException, CertificateException {
76 ArtifactDirStud artiDir = new ArtifactDirStud();
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"));
85 when(artiMock.getDir()).thenReturn(dirName);
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"));
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");
98 File writableFile = new File(dirName + '/' + nsName + "writable.txt");
99 artiDir.write(writableFile, Chmod.to755, "first data point", "second data point");
101 artiDir.write(writableFile, Chmod.to755, (String[])null);
102 fail("Should've thrown an exception");
103 } catch(NullPointerException e) {
106 KeyStore ks = KeyStore.getInstance("pkcs12");
108 artiDir.write(writableFile, Chmod.to755, ks, luggagePassword.toCharArray());
109 fail("Should've thrown an exception");
110 } catch(CadiException e) {
114 artiDir.write(writableFile, Chmod.to755, ks, luggagePassword.toCharArray());
116 ArtifactDirStud artiDir2 = new ArtifactDirStud();
117 artiDir2.place(transMock, certInfoMock, artiMock, "machine");
120 artiDir.place(transMock, certInfoMock, artiMock, "machine");
123 artiDir.place(transMock, certInfoMock, artiMock, "machine");
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");
135 private class ArtifactDirStud extends ArtifactDir {
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
142 // Expose the protected methods
144 public void write(File f, Chmod c, String ... data) throws IOException {
145 super.write(f, c, data);
147 public void write(File f, Chmod c, KeyStore ks, char[] pass ) throws IOException, CadiException {
148 super.write(f, c, ks, pass);
152 private static void cleanup() {
153 File dir = new File(dirName);
155 for (File f : dir.listFiles()) {