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.dal.aai.config;
28 import static org.junit.Assert.assertEquals;
29 import static org.junit.Assert.assertFalse;
30 import static org.junit.Assert.assertNotNull;
31 import static org.junit.Assert.assertNull;
32 import static org.junit.Assert.assertTrue;
34 import java.util.Properties;
36 import org.eclipse.jetty.util.security.Password;
37 import org.junit.Before;
38 import org.junit.Test;
39 import org.mockito.Mockito;
40 import org.onap.aai.sparky.dal.aai.config.ActiveInventorySslConfig;
41 import org.onap.aai.sparky.util.Encryptor;
43 public class ActiveInventorySslConfigTest {
45 private Encryptor encryptorMock = Mockito.mock(Encryptor.class);
48 * Test case initialization
50 * @throws Exception the exception
53 public void init() throws Exception {
54 System.setProperty("javax.net.debug", "invalid");
57 private Properties buildExpectedPropertyDefinition() throws Exception {
59 Properties props = new Properties();
61 props.put("aai.ssl.enableDebug", "false");
62 props.put("aai.ssl.validateServerHostName", "false");
63 props.put("aai.ssl.validateServiceCertificateChain", "false");
64 props.put("aai.ssl.keystore.type", "pkcs12");
65 props.put("aai.ssl.keystore.filename", "/opt/app/applocal/etc/cert.crt");
66 props.put("aai.ssl.keystore.pass", "OBF:1i9a1u2a1unz1lr61wn51wn11lss1unz1u301i6o");
67 props.put("aai.ssl.truststore.type", "jks");
68 props.put("aai.ssl.truststore.filename", "/opt/app/applocal/etc/cert.crt");
69 props.put("aai.ssl.basicAuth.username", "username");
70 props.put("aai.ssl.basicAuth.password", Password.obfuscate("password"));
75 private Properties buildInvalidPropertyDefinition() {
76 Properties props = new Properties();
78 props.put("aai.ssl.enableDebug", "true");
79 props.put("aai.ssl.validateServerHostName", "invalid");
80 props.put("aai.ssl.validateServiceCertificateChain", "invalid");
81 props.put("aai.ssl.keystore.type", "invalid");
82 // props.put("aai.ssl.keystore.filename", );
83 props.put("aai.ssl.keystore.pass", "invalid");
84 props.put("aai.ssl.truststore.type", "invalid");
85 // props.put("aai.ssl.truststore.filename", "/opt/app/applocal/etc/cert.crt");
86 props.put("aai.ssl.basicAuth.username", "invalid");
87 props.put("aai.ssl.basicAuth.password", "invalid");
92 private String generateAuthorizationHeaderValue(String username, String password) {
93 String usernameAndPassword = username + ":" + password;
94 return "Basic " + java.util.Base64.getEncoder().encodeToString(usernameAndPassword.getBytes());
98 * Success path initialization and validation of accessors
103 public void successfulInitialization() throws Exception {
106 * Setup encryptor expectations
108 Mockito.when(encryptorMock.decryptValue(Mockito.anyString())).thenReturn("password");
110 ActiveInventorySslConfig config =
111 new ActiveInventorySslConfig(buildExpectedPropertyDefinition(), encryptorMock);
114 * Now verify that all the internal members have been set to default values
117 assertEquals(System.getProperty("javax.net.debug"), "");
118 assertFalse(config.isValidateServerHostName());
119 assertFalse(config.isValidateServerCertificateChain());
121 assertEquals(config.getKeystoreType(), "pkcs12");
122 assertTrue(config.getKeystoreFilename().contains("/opt/app/applocal/etc/cert.crt"));
123 assertEquals(config.getKeystorePassword(), "password");
125 assertEquals(config.getTruststoreType(), "jks");
126 assertTrue(config.getTruststoreFilename().contains("/opt/app/applocal/etc/cert.crt"));
128 assertEquals(config.getBasicAuthUsername(), "username");
129 assertEquals(config.getBasicAuthPassword(), "password");
130 assertEquals(config.getBasicAuthenticationCredentials(),
131 generateAuthorizationHeaderValue("username", "password"));
136 * Failed path initialization
141 public void validateInitializationWithNullProperties() throws Exception {
144 * Setup encryptor expectations
146 Mockito.when(encryptorMock.decryptValue(Mockito.anyString())).thenReturn("");
148 ActiveInventorySslConfig config = new ActiveInventorySslConfig(null, encryptorMock);
151 * Now verify that all the internal members have been set to default values
154 assertEquals(System.getProperty("javax.net.debug"), "invalid");
155 assertFalse(config.isValidateServerHostName());
156 assertFalse(config.isValidateServerCertificateChain());
158 assertNull(config.getKeystoreType());
159 assertNull(config.getKeystoreFilename());
160 assertNull(config.getKeystorePassword());
162 assertNull(config.getTruststoreType());
163 assertNull(config.getTruststoreFilename());
165 assertNull(config.getBasicAuthUsername());
166 assertNull(config.getBasicAuthPassword());
167 assertEquals(config.getBasicAuthenticationCredentials(),
168 generateAuthorizationHeaderValue("null", "null"));
173 * Failed path initialization
178 public void validateInitializationWithInvalidProperties() throws Exception {
181 * Setup encryptor expectations
183 Mockito.when(encryptorMock.decryptValue(Mockito.anyString())).thenReturn("");
185 ActiveInventorySslConfig config =
186 new ActiveInventorySslConfig(buildInvalidPropertyDefinition(), encryptorMock);
189 * Now verify that all the internal members have been set to default values
192 assertEquals(System.getProperty("javax.net.debug"), "ssl");
193 assertFalse(config.isValidateServerHostName());
194 assertFalse(config.isValidateServerCertificateChain());
196 assertEquals(config.getKeystoreType(), "invalid");
197 assertTrue(config.getKeystoreFilename().contains("null"));
198 assertEquals(config.getKeystorePassword(), "");
200 assertEquals(config.getTruststoreType(), "invalid");
201 assertTrue(config.getTruststoreFilename().contains("null"));
203 assertEquals(config.getBasicAuthUsername(), "invalid");
204 assertEquals(config.getBasicAuthPassword(), "invalid");
205 assertEquals(config.getBasicAuthenticationCredentials(),
206 generateAuthorizationHeaderValue("invalid", "invalid"));
211 * Class accessor validator
216 public void validateClassAccessors() throws Exception {
219 * Setup encryptor expectations
221 Mockito.when(encryptorMock.decryptValue(Mockito.anyString())).thenReturn("password");
223 ActiveInventorySslConfig config =
224 new ActiveInventorySslConfig(buildInvalidPropertyDefinition(), encryptorMock);
227 * Now verify that all the internal members have been set to default values
230 config.setBasicAuthPassword("test");
231 config.setBasicAuthUsername("test");
232 config.setKeystoreFilename("test");
233 config.setKeystorePassword("test");
234 config.setKeystoreType("test");
235 config.setTruststoreFilename("test");
236 config.setTruststoreType("test");
237 config.setEncryptor(encryptorMock);
238 config.setValidateServerCertificateChain(true);
239 config.setValidateServerHostName(true);
241 assertEquals(System.getProperty("javax.net.debug"), "ssl");
242 assertTrue(config.isValidateServerHostName());
243 assertTrue(config.isValidateServerCertificateChain());
245 assertEquals(config.getKeystoreType(), "test");
246 assertTrue(config.getKeystoreFilename().contains("test"));
247 assertEquals(config.getKeystorePassword(), "test");
249 assertEquals(config.getTruststoreType(), "test");
250 assertTrue(config.getTruststoreFilename().contains("test"));
252 assertEquals(config.getBasicAuthUsername(), "test");
253 assertEquals(config.getBasicAuthPassword(), "test");
254 assertEquals(config.getBasicAuthenticationCredentials(),
255 generateAuthorizationHeaderValue("test", "test"));
257 assertNotNull(config.getEncryptor());
259 assertTrue(config.toString().contains("ActiveInventorySslConfig"));