2 * ============LICENSE_START=======================================================
3 * SPARKY (inventory 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 //import com.att.aai.util.EncryptedConfiguration;
45 public class ActiveInventorySslConfigTest {
47 private Encryptor encryptorMock = Mockito.mock(Encryptor.class);
50 * Test case initialization
52 * @throws Exception the exception
55 public void init() throws Exception {
56 System.setProperty("javax.net.debug", "invalid");
59 private Properties buildExpectedPropertyDefinition() throws Exception {
60 Encryptor encryptor = new Encryptor();
61 Properties props = new Properties();
63 props.put("aai.ssl.enableDebug", "false");
64 props.put("aai.ssl.validateServerHostName", "false");
65 props.put("aai.ssl.validateServiceCertificateChain", "false");
66 props.put("aai.ssl.keystore.type", "pkcs12");
67 props.put("aai.ssl.keystore.filename", "/opt/app/applocal/etc/cert.crt");
68 /*props.put("aai.ssl.keystore.pass", encryptor.decryptValue(value)EncryptedConfiguration.encryptToTriple("AES",
69 Long.toString(123456789 % 10000), "aa1admin", "password"));*/
70 props.put("aai.ssl.truststore.type", "jks");
71 props.put("aai.ssl.truststore.filename", "/opt/app/applocal/etc/cert.crt");
72 props.put("aai.ssl.basicAuth.username", "username");
73 props.put("aai.ssl.basicAuth.password", Password.obfuscate("password"));
78 private Properties buildInvalidPropertyDefinition() {
79 Properties props = new Properties();
81 props.put("aai.ssl.enableDebug", "true");
82 props.put("aai.ssl.validateServerHostName", "invalid");
83 props.put("aai.ssl.validateServiceCertificateChain", "invalid");
84 props.put("aai.ssl.keystore.type", "invalid");
85 // props.put("aai.ssl.keystore.filename", );
86 props.put("aai.ssl.keystore.pass", "invalid");
87 props.put("aai.ssl.truststore.type", "invalid");
88 // props.put("aai.ssl.truststore.filename", "/opt/app/applocal/etc/cert.crt");
89 props.put("aai.ssl.basicAuth.username", "invalid");
90 props.put("aai.ssl.basicAuth.password", "invalid");
95 private String generateAuthorizationHeaderValue(String username, String password) {
96 String usernameAndPassword = username + ":" + password;
97 return "Basic " + java.util.Base64.getEncoder().encodeToString(usernameAndPassword.getBytes());
101 * Success path initialization and validation of accessors
106 public void successfulInitialization() throws Exception {
109 * Setup encryptor expectations
111 Mockito.when(encryptorMock.decryptValue(Mockito.anyString())).thenReturn("password");
113 ActiveInventorySslConfig config =
114 new ActiveInventorySslConfig(buildExpectedPropertyDefinition(), encryptorMock);
117 * Now verify that all the internal members have been set to default values
120 assertEquals(System.getProperty("javax.net.debug"), "");
121 assertFalse(config.isValidateServerHostName());
122 assertFalse(config.isValidateServerCertificateChain());
124 assertEquals(config.getKeystoreType(), "pkcs12");
125 assertTrue(config.getKeystoreFilename().contains("/opt/app/applocal/etc/cert.crt"));
126 assertEquals(config.getKeystorePassword(), "password");
128 assertEquals(config.getTruststoreType(), "jks");
129 assertTrue(config.getTruststoreFilename().contains("/opt/app/applocal/etc/cert.crt"));
131 assertEquals(config.getBasicAuthUsername(), "username");
132 assertEquals(config.getBasicAuthPassword(), "password");
133 assertEquals(config.getBasicAuthenticationCredentials(),
134 generateAuthorizationHeaderValue("username", "password"));
139 * Failed path initialization
144 public void validateInitializationWithNullProperties() throws Exception {
147 * Setup encryptor expectations
149 Mockito.when(encryptorMock.decryptValue(Mockito.anyString())).thenReturn("");
151 ActiveInventorySslConfig config = new ActiveInventorySslConfig(null, encryptorMock);
154 * Now verify that all the internal members have been set to default values
157 assertEquals(System.getProperty("javax.net.debug"), "invalid");
158 assertFalse(config.isValidateServerHostName());
159 assertFalse(config.isValidateServerCertificateChain());
161 assertNull(config.getKeystoreType());
162 assertNull(config.getKeystoreFilename());
163 assertNull(config.getKeystorePassword());
165 assertNull(config.getTruststoreType());
166 assertNull(config.getTruststoreFilename());
168 assertNull(config.getBasicAuthUsername());
169 assertNull(config.getBasicAuthPassword());
170 assertEquals(config.getBasicAuthenticationCredentials(),
171 generateAuthorizationHeaderValue("null", "null"));
176 * Failed path initialization
181 public void validateInitializationWithInvalidProperties() throws Exception {
184 * Setup encryptor expectations
186 Mockito.when(encryptorMock.decryptValue(Mockito.anyString())).thenReturn("");
188 ActiveInventorySslConfig config =
189 new ActiveInventorySslConfig(buildInvalidPropertyDefinition(), encryptorMock);
192 * Now verify that all the internal members have been set to default values
195 assertEquals(System.getProperty("javax.net.debug"), "ssl");
196 assertFalse(config.isValidateServerHostName());
197 assertFalse(config.isValidateServerCertificateChain());
199 assertEquals(config.getKeystoreType(),"invalid");
200 assertTrue(config.getKeystoreFilename().contains("null"));
201 assertEquals(config.getKeystorePassword(),"");
203 assertEquals(config.getTruststoreType(),"invalid");
204 assertTrue(config.getTruststoreFilename().contains("null"));
206 assertEquals(config.getBasicAuthUsername(),"invalid");
207 assertEquals(config.getBasicAuthPassword(),"invalid");
208 assertEquals(config.getBasicAuthenticationCredentials(),
209 generateAuthorizationHeaderValue("invalid", "invalid"));
214 * Class accessor validator
219 public void validateClassAccessors() throws Exception {
222 * Setup encryptor expectations
224 Mockito.when(encryptorMock.decryptValue(Mockito.anyString())).thenReturn("password");
226 ActiveInventorySslConfig config =
227 new ActiveInventorySslConfig(buildInvalidPropertyDefinition(), encryptorMock);
230 * Now verify that all the internal members have been set to default values
233 config.setBasicAuthPassword("test");
234 config.setBasicAuthUsername("test");
235 config.setKeystoreFilename("test");
236 config.setKeystorePassword("test");
237 config.setKeystoreType("test");
238 config.setTruststoreFilename("test");
239 config.setTruststoreType("test");
240 config.setEncryptor(encryptorMock);
241 config.setValidateServerCertificateChain(true);
242 config.setValidateServerHostName(true);
244 assertEquals(System.getProperty("javax.net.debug"), "ssl");
245 assertTrue(config.isValidateServerHostName());
246 assertTrue(config.isValidateServerCertificateChain());
248 assertEquals(config.getKeystoreType(),"test");
249 assertTrue(config.getKeystoreFilename().contains("test"));
250 assertEquals(config.getKeystorePassword(),"test");
252 assertEquals(config.getTruststoreType(),"test");
253 assertTrue(config.getTruststoreFilename().contains("test"));
255 assertEquals(config.getBasicAuthUsername(),"test");
256 assertEquals(config.getBasicAuthPassword(),"test");
257 assertEquals(config.getBasicAuthenticationCredentials(),
258 generateAuthorizationHeaderValue("test", "test"));
260 assertNotNull(config.getEncryptor());
262 assertTrue(config.toString().contains("ActiveInventorySslConfig"));