d66546bf97eafc57796bcdeceb7ab3e2c73120ea
[aaf/authz.git] / cadi / aaf / src / test / java / org / onap / aaf / cadi / cm / test / JU_PlaceArtifactOnStream.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.ByteArrayOutputStream;
29 import java.io.PrintStream;
30 import java.util.ArrayList;
31 import java.util.List;
32
33 import org.junit.*;
34 import org.mockito.*;
35 import org.onap.aaf.cadi.configure.PlaceArtifactOnStream;
36 import org.onap.aaf.misc.env.LogTarget;
37 import org.onap.aaf.misc.env.Trans;
38
39 import certman.v1_0.Artifacts.Artifact;
40 import certman.v1_0.CertInfo;
41
42 public class JU_PlaceArtifactOnStream {
43
44     @Mock private Trans transMock;
45     @Mock private CertInfo certInfoMock;
46     @Mock private Artifact artiMock;
47
48     private static final String luggagePassword = "12345";  // That's the stupidest combination I've ever heard in my life
49     private static final String privateKeyString = "I'm a private key!";
50     
51     private ByteArrayOutputStream outStream;
52
53     private List<String> certs;
54
55     @Before
56     public void setup() {
57         MockitoAnnotations.initMocks(this);
58
59         certs = new ArrayList<>();
60         certs.add("cert1");
61         certs.add("cert2");
62
63         when(certInfoMock.getChallenge()).thenReturn(luggagePassword);
64         when(certInfoMock.getCerts()).thenReturn(certs);
65         when(certInfoMock.getPrivatekey()).thenReturn(privateKeyString);
66         
67         outStream = new ByteArrayOutputStream();
68     }
69
70     @Test
71     public void test() {
72         PlaceArtifactOnStream placer = new PlaceArtifactOnStream(new PrintStream(outStream));
73         placer.place(transMock, certInfoMock, artiMock, "machine");
74         
75         String[] output = outStream.toString().split(System.lineSeparator(), 0);
76         
77         String[] expected = {
78                 "Challenge:  " + luggagePassword,
79                 "PrivateKey:",
80                 privateKeyString,
81                 "Certificate Chain:",
82                 "cert1",
83                 "cert2"
84         };
85         
86         assertThat(output.length, is(expected.length));
87         for (int i = 0; i < output.length; i++) {
88             assertThat(output[i], is(expected[i]));
89         }
90
91         // coverage
92         when(certInfoMock.getNotes()).thenReturn("");
93         placer.place(transMock, certInfoMock, artiMock, "machine");
94
95         when(certInfoMock.getNotes()).thenReturn("Some Notes");
96         when(transMock.info()).thenReturn(mock(LogTarget.class));
97         placer.place(transMock, certInfoMock, artiMock, "machine");
98     }
99
100 }