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