Initial commit for AAI-UI(sparky-backend)
[aai/sparky-be.git] / src / test / java / org / openecomp / 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.openecomp.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.openecomp.sparky.util.LogValidator;
39
40 import ch.qos.logback.classic.Level;
41
42 /**
43  * The Class SecurityContextFactoryImplTest.
44  */
45 public class SecurityContextFactoryImplTest {
46
47   private LogValidator logValidator;
48
49   /**
50    * Inits the.
51    *
52    * @throws Exception the exception
53    */
54   @Before
55   public void init() throws Exception {
56     logValidator = new LogValidator();
57     logValidator.initializeLogger(Level.WARN);
58   }
59
60   /**
61    * Basic construction test.
62    *
63    * @throws Exception the exception
64    */
65   @Test
66   public void basicConstructionTest() throws Exception {
67
68     SecurityContextFactory sslContextFactory = new SecurityContextFactoryImpl();
69
70     assertEquals("TLS", sslContextFactory.getSslAlgorithm());
71     assertEquals("SunX509", sslContextFactory.getKeyManagerAlgortihm());
72     assertEquals("PKCS12", sslContextFactory.getKeyStoreType());
73     assertEquals(false, sslContextFactory.isServerCertificationChainValidationEnabled());
74     assertEquals(null, sslContextFactory.getClientCertFileInputStream());
75   }
76
77   /**
78    * Validate secure context.
79    *
80    * @throws Exception the exception
81    */
82   @Test
83   public void validateSecureContext() throws Exception {
84
85     SecurityContextFactory sslContextFactory = new SecurityContextFactoryImpl();
86
87     SSLContext sslContext = sslContextFactory.getSecureContext();
88
89     assertNotNull(sslContext);
90   }
91
92   /**
93    * Validate secure context with server cert chain validation.
94    *
95    * @throws Exception the exception
96    */
97   @Test
98   public void validateSecureContext_withServerCertChainValidation() throws Exception {
99
100     SecurityContextFactory sslContextFactory = new SecurityContextFactoryImpl();
101     sslContextFactory.setServerCertificationChainValidationEnabled(true);
102     sslContextFactory.setTrustStoreFileName("filename");
103
104     sslContextFactory.setClientCertFileName(null);
105
106     SSLContext sslContext = sslContextFactory.getSecureContext();
107
108     assertNotNull(sslContext);
109   }
110
111   /**
112    * Validate accessors.
113    *
114    * @throws Exception the exception
115    */
116   @Test
117   public void validateAccessors() throws Exception {
118
119     SecurityContextFactory sslContextFactory = new SecurityContextFactoryImpl();
120
121     FileInputStream mockInputStream = Mockito.mock(FileInputStream.class);
122
123     sslContextFactory.setSslAlgorithm("sslAlgorithm");
124     sslContextFactory.setKeyManagerAlgortihm("keyManagerAlgorithm");
125     sslContextFactory.setKeyStoreType("keyStoreType");
126     sslContextFactory.setClientCertFileInputStream(mockInputStream);
127     sslContextFactory.setServerCertificationChainValidationEnabled(true);
128     sslContextFactory.setTrustStoreFileName("truststoreFileName");
129     sslContextFactory.setClientCertPassword("password");
130
131     assertEquals("sslAlgorithm", sslContextFactory.getSslAlgorithm());
132     assertEquals("keyManagerAlgorithm", sslContextFactory.getKeyManagerAlgortihm());
133     assertEquals("keyStoreType", sslContextFactory.getKeyStoreType());
134     assertEquals(mockInputStream, sslContextFactory.getClientCertFileInputStream());
135     assertEquals(true, sslContextFactory.isServerCertificationChainValidationEnabled());
136     assertEquals("truststoreFileName", sslContextFactory.getTrustStoreFileName());
137     assertEquals("password", sslContextFactory.getClientCertPassword());
138
139   }
140
141 }