Enabling profile handling for subsciption
[aai/sparky-be.git] / sparkybe-onap-service / 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
39 /**
40  * The Class SecurityContextFactoryImplTest.
41  */
42 public class SecurityContextFactoryImplTest {
43   
44
45   /**
46    * Inits the.
47    *
48    * @throws Exception the exception
49    */
50   @Before
51   public void init() throws Exception {
52   }
53
54   /**
55    * Basic construction test.
56    *
57    * @throws Exception the exception
58    */
59   @Test
60   public void basicConstructionTest() throws Exception {
61
62     SecurityContextFactory sslContextFactory = new SecurityContextFactoryImpl();
63
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());
69   }
70
71   /**
72    * Validate secure context.
73    *
74    * @throws Exception the exception
75    */
76   @Test
77   public void validateSecureContext() throws Exception {
78
79     SecurityContextFactory sslContextFactory = new SecurityContextFactoryImpl();
80
81     SSLContext sslContext = sslContextFactory.getSecureContext();
82
83     assertNotNull(sslContext);
84   }
85
86   /**
87    * Validate secure context with server cert chain validation.
88    *
89    * @throws Exception the exception
90    */
91   @Test
92   public void validateSecureContext_withServerCertChainValidation() throws Exception {
93
94     SecurityContextFactory sslContextFactory = new SecurityContextFactoryImpl();
95     sslContextFactory.setServerCertificationChainValidationEnabled(true);
96     sslContextFactory.setTrustStoreFileName("filename");
97
98     sslContextFactory.setClientCertFileName(null);
99
100     SSLContext sslContext = sslContextFactory.getSecureContext();
101
102     assertNotNull(sslContext);
103   }
104
105   /**
106    * Validate accessors.
107    *
108    * @throws Exception the exception
109    */
110   @Test
111   public void validateAccessors() throws Exception {
112
113     SecurityContextFactory sslContextFactory = new SecurityContextFactoryImpl();
114
115     FileInputStream mockInputStream = Mockito.mock(FileInputStream.class);
116
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");
124
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());
132
133   }
134
135 }