b57f29e0b18569179cc3046dacc65701f585db2f
[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 static org.hamcrest.CoreMatchers.is;
25 import static org.junit.Assert.assertThat;
26 import static org.mockito.Mockito.when;
27
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;
35
36 import javax.net.ssl.HttpsURLConnection;
37 import javax.net.ssl.X509KeyManager;
38
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;
49
50 public class JU_HX509SS {
51         
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;
58         
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();
62         
63         private PropAccess access;
64         private SecurityInfoC<HttpURLConnection> si;
65         
66         @Before
67         public void setup() throws IOException, CadiException, CertificateEncodingException {
68                 MockitoAnnotations.initMocks(this);
69                 
70                 when(x509Mock.getSigAlgName()).thenReturn(algName);
71                 when(x509Mock.getEncoded()).thenReturn(publicKeyBytes);
72                 
73                 when(keyManagerMock.getCertificateChain(alias)).thenReturn(new X509Certificate[] {x509Mock});
74                 when(keyManagerMock.getPrivateKey(alias)).thenReturn(privateKeyMock);
75
76                 when(siMock.getKeyManagers()).thenReturn(new X509KeyManager[] {keyManagerMock});
77                 
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);
81         }
82
83         @Test
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));
90                 
91                 // coverage...
92                 x509.setSecurity(hucMock);
93                 x509.setSecurity(hucsMock);
94         }
95         
96         // TODO: Test the setSecurity method - Ian
97         // @Test
98         // public void test2() throws APIException, CadiException {
99                 // HX509SS x509 = new HX509SS(si, false);
100                 // x509.setSecurity(hucMock);
101                 // x509.setSecurity(hucsMock);
102         // }
103         
104         @Test(expected = APIException.class)
105         public void throws1Test() throws APIException, CadiException {
106                 @SuppressWarnings("unused")
107                 HX509SS x509 = new HX509SS(siMock);
108         }
109
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);
115         }
116         
117 }