Improve coverage of cadi-client
[aaf/authz.git] / cadi / client / src / test / java / org / onap / aaf / cadi / http / test / JU_HX509SS.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.http.test;
23
24 import java.io.ByteArrayOutputStream;
25 import java.io.IOException;
26 import java.io.PrintStream;
27 import java.net.HttpURLConnection;
28 import java.security.PrivateKey;
29 import java.security.cert.CertificateEncodingException;
30 import java.security.cert.X509Certificate;
31
32 import javax.net.ssl.HttpsURLConnection;
33 import javax.net.ssl.X509KeyManager;
34
35 import static org.junit.Assert.*;
36 import static org.mockito.Mockito.*;
37 import static org.hamcrest.CoreMatchers.*;
38
39 import org.junit.*;
40 import org.mockito.*;
41 import org.onap.aaf.cadi.CadiException;
42 import org.onap.aaf.cadi.PropAccess;
43 import org.onap.aaf.cadi.config.Config;
44 import org.onap.aaf.cadi.config.SecurityInfoC;
45 import org.onap.aaf.cadi.http.HX509SS;
46 import org.onap.aaf.misc.env.APIException;
47
48 public class JU_HX509SS {
49         
50         @Mock X509Certificate x509Mock;
51         @Mock X509KeyManager keyManagerMock;
52         @Mock PrivateKey privateKeyMock;
53         @Mock SecurityInfoC<HttpURLConnection> siMock;
54         @Mock HttpURLConnection hucMock;
55         @Mock HttpsURLConnection hucsMock;
56         
57         private final static String alias = "Some alias";
58         private final static String algName = "Some algName";
59         private final static byte[] publicKeyBytes = "a public key".getBytes();
60         
61         private PropAccess access;
62         private SecurityInfoC<HttpURLConnection> si;
63         
64         @Before
65         public void setup() throws IOException, CadiException, CertificateEncodingException {
66                 MockitoAnnotations.initMocks(this);
67                 
68                 when(x509Mock.getSigAlgName()).thenReturn(algName);
69                 when(x509Mock.getEncoded()).thenReturn(publicKeyBytes);
70                 
71                 when(keyManagerMock.getCertificateChain(alias)).thenReturn(new X509Certificate[] {x509Mock});
72                 when(keyManagerMock.getPrivateKey(alias)).thenReturn(privateKeyMock);
73
74                 when(siMock.getKeyManagers()).thenReturn(new X509KeyManager[] {keyManagerMock});
75                 
76                 access = new PropAccess(new PrintStream(new ByteArrayOutputStream()), new String[0]);
77                 access.setProperty(Config.CADI_ALIAS, alias);
78                 si = SecurityInfoC.instance(access, HttpURLConnection.class);
79         }
80
81         @Test
82         public void test() throws APIException, CadiException {
83                 HX509SS x509 = new HX509SS(alias, siMock);
84                 assertThat(x509.getID(), is(alias));
85                 assertThat(x509.setLastResponse(0), is(0));
86                 assertThat(x509.setLastResponse(1), is(0));
87                 assertThat(x509.setLastResponse(2), is(0));
88                 
89                 // coverage...
90                 x509.setSecurity(hucMock);
91                 x509.setSecurity(hucsMock);
92         }
93         
94         // TODO: Test the setSecurity method - Ian
95         // @Test
96         // public void test2() throws APIException, CadiException {
97                 // HX509SS x509 = new HX509SS(si, false);
98                 // x509.setSecurity(hucMock);
99                 // x509.setSecurity(hucsMock);
100         // }
101         
102         @Test(expected = APIException.class)
103         public void throws1Test() throws APIException, CadiException {
104                 @SuppressWarnings("unused")
105                 HX509SS x509 = new HX509SS(siMock);
106         }
107
108         @Test(expected = APIException.class)
109         public void throws2Test() throws APIException, CadiException {
110                 @SuppressWarnings("unused")
111                 HX509SS x509 = new HX509SS(si, false);
112         }
113
114         @Test(expected = APIException.class)
115         public void throws3Test() throws APIException, CadiException {
116                 when(keyManagerMock.getCertificateChain(alias)).thenReturn(new X509Certificate[0]);
117                 @SuppressWarnings("unused")
118                 HX509SS x509 = new HX509SS(alias, siMock);
119         }
120         
121 }