7afb4cf4c0ee54de1474bdc269e5a202f917eac2
[aaf/authz.git] / cadi / aaf / src / test / java / org / onap / aaf / cadi / cm / test / JU_PlaceArtifactInFiles.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.junit.Assert.*;
25 import static org.hamcrest.CoreMatchers.*;
26 import static org.mockito.Mockito.*;
27
28 import java.io.File;
29 import java.util.ArrayList;
30 import java.util.List;
31
32 import org.junit.*;
33 import org.mockito.*;
34 import org.onap.aaf.cadi.CadiException;
35 import org.onap.aaf.cadi.configure.PlaceArtifactInFiles;
36 import org.onap.aaf.misc.env.Trans;
37
38 import certman.v1_0.Artifacts.Artifact;
39 import certman.v1_0.CertInfo;
40
41 public class JU_PlaceArtifactInFiles {
42
43         @Mock private Trans transMock;
44         @Mock private CertInfo certInfoMock;
45         @Mock private Artifact artiMock;
46
47         private static final String dirName = "src/test/resources/artifacts";
48         private static final String nsName = "org.onap.test";
49         private static final String luggagePassword = "12345";  // That's the stupidest combination I've ever heard in my life
50
51         private List<String> certs;
52
53         @Before
54         public void setup() {
55                 MockitoAnnotations.initMocks(this);
56
57                 certs = new ArrayList<>();
58                 certs.add("cert1");
59                 certs.add("cert2");
60
61                 when(certInfoMock.getChallenge()).thenReturn(luggagePassword);
62                 when(certInfoMock.getCerts()).thenReturn(certs);
63
64                 when(artiMock.getDir()).thenReturn(dirName);
65                 when(artiMock.getNs()).thenReturn(nsName);
66         }
67
68         @AfterClass
69         public static void tearDownOnce() {
70                 cleanup();
71                 PlaceArtifactInFiles.clear();
72         }
73
74         @Test
75         public void test() throws CadiException {
76                 PlaceArtifactInFiles placer = new PlaceArtifactInFiles();
77                 placer.place(transMock, certInfoMock, artiMock, "machine");
78                 assertThat(placer._place(transMock, certInfoMock, artiMock), is(true));
79                 assertThat(new File(dirName + '/' + nsName + ".crt").exists(), is(true));
80                 assertThat(new File(dirName + '/' + nsName + ".key").exists(), is(true));
81                 
82                 when(certInfoMock.getCerts()).thenReturn(null);
83                 try {
84                         placer._place(transMock, certInfoMock, artiMock);
85                         fail("Should've thrown an exception");
86                 } catch (Exception e) {
87                 }
88         }
89
90         private static void cleanup() {
91                 File dir = new File(dirName);
92                 if (dir.exists()) {
93                         for (File f : dir.listFiles()) {
94                                 f.delete();
95                         }
96                         dir.delete();
97                 }
98         }
99
100 }