Merge "Fix Blocker/Critical sonar issues"
[aai/sparky-be.git] / src / test / java / org / onap / aai / sparky / security / SecurityContextFactoryImplTest.java
1 /* 
2 * ============LICENSE_START=======================================================
3 * SPARKY (AAI UI service)
4 * ================================================================================
5 * Copyright © 2017 AT&T Intellectual Property.
6 * Copyright © 2017 Amdocs
7 * All rights reserved.
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
12
13 *      http://www.apache.org/licenses/LICENSE-2.0
14
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=========================================================
21
22 * ECOMP and OpenECOMP are trademarks
23 * and service marks of AT&T Intellectual Property.
24 */
25
26 package org.onap.aai.sparky.security;
27
28 import static org.junit.Assert.assertEquals;
29 import static org.junit.Assert.assertNotNull;
30
31 import java.io.FileInputStream;
32
33 import javax.net.ssl.SSLContext;
34
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;
41
42 import ch.qos.logback.classic.Level;
43
44 /**
45  * The Class SecurityContextFactoryImplTest.
46  */
47 public class SecurityContextFactoryImplTest {
48
49   private LogValidator logValidator;
50
51   /**
52    * Inits the.
53    *
54    * @throws Exception the exception
55    */
56   @Before
57   public void init() throws Exception {
58     logValidator = new LogValidator();
59     logValidator.initializeLogger(Level.WARN);
60   }
61
62   /**
63    * Basic construction test.
64    *
65    * @throws Exception the exception
66    */
67   @Test
68   public void basicConstructionTest() throws Exception {
69
70     SecurityContextFactory sslContextFactory = new SecurityContextFactoryImpl();
71
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());
77   }
78
79   /**
80    * Validate secure context.
81    *
82    * @throws Exception the exception
83    */
84   @Test
85   public void validateSecureContext() throws Exception {
86
87     SecurityContextFactory sslContextFactory = new SecurityContextFactoryImpl();
88
89     SSLContext sslContext = sslContextFactory.getSecureContext();
90
91     assertNotNull(sslContext);
92   }
93
94   /**
95    * Validate secure context with server cert chain validation.
96    *
97    * @throws Exception the exception
98    */
99   @Test
100   public void validateSecureContext_withServerCertChainValidation() throws Exception {
101
102     SecurityContextFactory sslContextFactory = new SecurityContextFactoryImpl();
103     sslContextFactory.setServerCertificationChainValidationEnabled(true);
104     sslContextFactory.setTrustStoreFileName("filename");
105
106     sslContextFactory.setClientCertFileName(null);
107
108     SSLContext sslContext = sslContextFactory.getSecureContext();
109
110     assertNotNull(sslContext);
111   }
112
113   /**
114    * Validate accessors.
115    *
116    * @throws Exception the exception
117    */
118   @Test
119   public void validateAccessors() throws Exception {
120
121     SecurityContextFactory sslContextFactory = new SecurityContextFactoryImpl();
122
123     FileInputStream mockInputStream = Mockito.mock(FileInputStream.class);
124
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");
132
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());
140
141   }
142
143 }