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;
38 import org.onap.aai.sparky.security.SecurityContextFactory;
39 import org.onap.aai.sparky.security.SecurityContextFactoryImpl;
40 import org.onap.aai.sparky.util.LogValidator;
42 import ch.qos.logback.classic.Level;
45 * The Class SecurityContextFactoryImplTest.
47 public class SecurityContextFactoryImplTest {
49 private LogValidator logValidator;
54 * @throws Exception the exception
57 public void init() throws Exception {
58 logValidator = new LogValidator();
59 logValidator.initializeLogger(Level.WARN);
63 * Basic construction test.
65 * @throws Exception the exception
68 public void basicConstructionTest() throws Exception {
70 SecurityContextFactory sslContextFactory = new SecurityContextFactoryImpl();
72 assertEquals("TLS", sslContextFactory.getSslAlgorithm());
73 assertEquals("SunX509", sslContextFactory.getKeyManagerAlgortihm());
74 assertEquals("PKCS12", sslContextFactory.getKeyStoreType());
75 assertEquals(false, sslContextFactory.isServerCertificationChainValidationEnabled());
76 assertEquals(null, sslContextFactory.getClientCertFileInputStream());
80 * Validate secure context.
82 * @throws Exception the exception
85 public void validateSecureContext() throws Exception {
87 SecurityContextFactory sslContextFactory = new SecurityContextFactoryImpl();
89 SSLContext sslContext = sslContextFactory.getSecureContext();
91 assertNotNull(sslContext);
95 * Validate secure context with server cert chain validation.
97 * @throws Exception the exception
100 public void validateSecureContext_withServerCertChainValidation() throws Exception {
102 SecurityContextFactory sslContextFactory = new SecurityContextFactoryImpl();
103 sslContextFactory.setServerCertificationChainValidationEnabled(true);
104 sslContextFactory.setTrustStoreFileName("filename");
106 sslContextFactory.setClientCertFileName(null);
108 SSLContext sslContext = sslContextFactory.getSecureContext();
110 assertNotNull(sslContext);
114 * Validate accessors.
116 * @throws Exception the exception
119 public void validateAccessors() throws Exception {
121 SecurityContextFactory sslContextFactory = new SecurityContextFactoryImpl();
123 FileInputStream mockInputStream = Mockito.mock(FileInputStream.class);
125 sslContextFactory.setSslAlgorithm("sslAlgorithm");
126 sslContextFactory.setKeyManagerAlgortihm("keyManagerAlgorithm");
127 sslContextFactory.setKeyStoreType("keyStoreType");
128 sslContextFactory.setClientCertFileInputStream(mockInputStream);
129 sslContextFactory.setServerCertificationChainValidationEnabled(true);
130 sslContextFactory.setTrustStoreFileName("truststoreFileName");
131 sslContextFactory.setClientCertPassword("password");
133 assertEquals("sslAlgorithm", sslContextFactory.getSslAlgorithm());
134 assertEquals("keyManagerAlgorithm", sslContextFactory.getKeyManagerAlgortihm());
135 assertEquals("keyStoreType", sslContextFactory.getKeyStoreType());
136 assertEquals(mockInputStream, sslContextFactory.getClientCertFileInputStream());
137 assertEquals(true, sslContextFactory.isServerCertificationChainValidationEnabled());
138 assertEquals("truststoreFileName", sslContextFactory.getTrustStoreFileName());
139 assertEquals("password", sslContextFactory.getClientCertPassword());