2 * ============LICENSE_START===================================================
3 * SPARKY (AAI UI service)
4 * ============================================================================
5 * Copyright © 2017 AT&T Intellectual Property.
6 * Copyright © 2017 Amdocs
8 * ============================================================================
9 * Licensed under the Apache License, Version 2.0 (the "License");
10 * you may not use this file except in compliance with the License.
11 * You may obtain a copy of the License at
13 * http://www.apache.org/licenses/LICENSE-2.0
15 * Unless required by applicable law or agreed to in writing, software
16 * distributed under the License is distributed on an "AS IS" BASIS,
17 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18 * See the License for the specific language governing permissions and
19 * limitations under the License.
20 * ============LICENSE_END=====================================================
22 * ECOMP and OpenECOMP are trademarks
23 * and service marks of AT&T Intellectual Property.
26 package org.onap.aai.sparky.security;
28 import static org.junit.Assert.assertEquals;
29 import static org.junit.Assert.assertNotNull;
31 import java.io.FileInputStream;
33 import javax.net.ssl.SSLContext;
35 import org.junit.Before;
36 import org.junit.Test;
37 import org.mockito.Mockito;
40 * The Class SecurityContextFactoryImplTest.
42 public class SecurityContextFactoryImplTest {
48 * @throws Exception the exception
51 public void init() throws Exception {
55 * Basic construction test.
57 * @throws Exception the exception
60 public void basicConstructionTest() throws Exception {
62 SecurityContextFactory sslContextFactory = new SecurityContextFactoryImpl();
64 assertEquals("TLS", sslContextFactory.getSslAlgorithm());
65 assertEquals("SunX509", sslContextFactory.getKeyManagerAlgortihm());
66 assertEquals("PKCS12", sslContextFactory.getKeyStoreType());
67 assertEquals(false, sslContextFactory.isServerCertificationChainValidationEnabled());
68 assertEquals(null, sslContextFactory.getClientCertFileInputStream());
72 * Validate secure context.
74 * @throws Exception the exception
77 public void validateSecureContext() throws Exception {
79 SecurityContextFactory sslContextFactory = new SecurityContextFactoryImpl();
81 SSLContext sslContext = sslContextFactory.getSecureContext();
83 assertNotNull(sslContext);
87 * Validate secure context with server cert chain validation.
89 * @throws Exception the exception
92 public void validateSecureContext_withServerCertChainValidation() throws Exception {
94 SecurityContextFactory sslContextFactory = new SecurityContextFactoryImpl();
95 sslContextFactory.setServerCertificationChainValidationEnabled(true);
96 sslContextFactory.setTrustStoreFileName("filename");
98 sslContextFactory.setClientCertFileName(null);
100 SSLContext sslContext = sslContextFactory.getSecureContext();
102 assertNotNull(sslContext);
106 * Validate accessors.
108 * @throws Exception the exception
111 public void validateAccessors() throws Exception {
113 SecurityContextFactory sslContextFactory = new SecurityContextFactoryImpl();
115 FileInputStream mockInputStream = Mockito.mock(FileInputStream.class);
117 sslContextFactory.setSslAlgorithm("sslAlgorithm");
118 sslContextFactory.setKeyManagerAlgortihm("keyManagerAlgorithm");
119 sslContextFactory.setKeyStoreType("keyStoreType");
120 sslContextFactory.setClientCertFileInputStream(mockInputStream);
121 sslContextFactory.setServerCertificationChainValidationEnabled(true);
122 sslContextFactory.setTrustStoreFileName("truststoreFileName");
123 sslContextFactory.setClientCertPassword("password");
125 assertEquals("sslAlgorithm", sslContextFactory.getSslAlgorithm());
126 assertEquals("keyManagerAlgorithm", sslContextFactory.getKeyManagerAlgortihm());
127 assertEquals("keyStoreType", sslContextFactory.getKeyStoreType());
128 assertEquals(mockInputStream, sslContextFactory.getClientCertFileInputStream());
129 assertEquals(true, sslContextFactory.isServerCertificationChainValidationEnabled());
130 assertEquals("truststoreFileName", sslContextFactory.getTrustStoreFileName());
131 assertEquals("password", sslContextFactory.getClientCertPassword());