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