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.openecomp.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.openecomp.sparky.util.Encryptor;
42 //import com.att.aai.util.EncryptedConfiguration;
44 public class ActiveInventorySslConfigTest {
46 private Encryptor encryptorMock = Mockito.mock(Encryptor.class);
49 * Test case initialization
51 * @throws Exception the exception
54 public void init() throws Exception {
55 System.setProperty("javax.net.debug", "invalid");
58 private Properties buildExpectedPropertyDefinition() throws Exception {
59 Encryptor encryptor = new Encryptor();
60 Properties props = new Properties();
62 props.put("aai.ssl.enableDebug", "false");
63 props.put("aai.ssl.validateServerHostName", "false");
64 props.put("aai.ssl.validateServiceCertificateChain", "false");
65 props.put("aai.ssl.keystore.type", "pkcs12");
66 props.put("aai.ssl.keystore.filename", "/opt/app/applocal/etc/cert.crt");
67 /*props.put("aai.ssl.keystore.pass", encryptor.decryptValue(value)EncryptedConfiguration.encryptToTriple("AES",
68 Long.toString(123456789 % 10000), "aa1admin", "password"));*/
69 props.put("aai.ssl.truststore.type", "jks");
70 props.put("aai.ssl.truststore.filename", "/opt/app/applocal/etc/cert.crt");
71 props.put("aai.ssl.basicAuth.username", "username");
72 props.put("aai.ssl.basicAuth.password", Password.obfuscate("password"));
77 private Properties buildInvalidPropertyDefinition() {
78 Properties props = new Properties();
80 props.put("aai.ssl.enableDebug", "true");
81 props.put("aai.ssl.validateServerHostName", "invalid");
82 props.put("aai.ssl.validateServiceCertificateChain", "invalid");
83 props.put("aai.ssl.keystore.type", "invalid");
84 // props.put("aai.ssl.keystore.filename", );
85 props.put("aai.ssl.keystore.pass", "invalid");
86 props.put("aai.ssl.truststore.type", "invalid");
87 // props.put("aai.ssl.truststore.filename", "/opt/app/applocal/etc/cert.crt");
88 props.put("aai.ssl.basicAuth.username", "invalid");
89 props.put("aai.ssl.basicAuth.password", "invalid");
94 private String generateAuthorizationHeaderValue(String username, String password) {
95 String usernameAndPassword = username + ":" + password;
96 return "Basic " + java.util.Base64.getEncoder().encodeToString(usernameAndPassword.getBytes());
100 * Success path initialization and validation of accessors
105 public void successfulInitialization() throws Exception {
108 * Setup encryptor expectations
110 Mockito.when(encryptorMock.decryptValue(Mockito.anyString())).thenReturn("password");
112 ActiveInventorySslConfig config =
113 new ActiveInventorySslConfig(buildExpectedPropertyDefinition(), encryptorMock);
116 * Now verify that all the internal members have been set to default values
119 assertEquals(System.getProperty("javax.net.debug"), "");
120 assertFalse(config.isValidateServerHostName());
121 assertFalse(config.isValidateServerCertificateChain());
123 assertEquals(config.getKeystoreType(), "pkcs12");
124 assertTrue(config.getKeystoreFilename().contains("/opt/app/applocal/etc/cert.crt"));
125 assertEquals(config.getKeystorePassword(), "password");
127 assertEquals(config.getTruststoreType(), "jks");
128 assertTrue(config.getTruststoreFilename().contains("/opt/app/applocal/etc/cert.crt"));
130 assertEquals(config.getBasicAuthUsername(), "username");
131 assertEquals(config.getBasicAuthPassword(), "password");
132 assertEquals(config.getBasicAuthenticationCredentials(),
133 generateAuthorizationHeaderValue("username", "password"));
138 * Failed path initialization
143 public void validateInitializationWithNullProperties() throws Exception {
146 * Setup encryptor expectations
148 Mockito.when(encryptorMock.decryptValue(Mockito.anyString())).thenReturn("");
150 ActiveInventorySslConfig config = new ActiveInventorySslConfig(null, encryptorMock);
153 * Now verify that all the internal members have been set to default values
156 assertEquals(System.getProperty("javax.net.debug"), "invalid");
157 assertFalse(config.isValidateServerHostName());
158 assertFalse(config.isValidateServerCertificateChain());
160 assertNull(config.getKeystoreType());
161 assertNull(config.getKeystoreFilename());
162 assertNull(config.getKeystorePassword());
164 assertNull(config.getTruststoreType());
165 assertNull(config.getTruststoreFilename());
167 assertNull(config.getBasicAuthUsername());
168 assertNull(config.getBasicAuthPassword());
169 assertEquals(config.getBasicAuthenticationCredentials(),
170 generateAuthorizationHeaderValue("null", "null"));
175 * Failed path initialization
180 public void validateInitializationWithInvalidProperties() throws Exception {
183 * Setup encryptor expectations
185 Mockito.when(encryptorMock.decryptValue(Mockito.anyString())).thenReturn("");
187 ActiveInventorySslConfig config =
188 new ActiveInventorySslConfig(buildInvalidPropertyDefinition(), encryptorMock);
191 * Now verify that all the internal members have been set to default values
194 assertEquals(System.getProperty("javax.net.debug"), "ssl");
195 assertFalse(config.isValidateServerHostName());
196 assertFalse(config.isValidateServerCertificateChain());
198 assertEquals(config.getKeystoreType(),"invalid");
199 assertTrue(config.getKeystoreFilename().contains("null"));
200 assertEquals(config.getKeystorePassword(),"");
202 assertEquals(config.getTruststoreType(),"invalid");
203 assertTrue(config.getTruststoreFilename().contains("null"));
205 assertEquals(config.getBasicAuthUsername(),"invalid");
206 assertEquals(config.getBasicAuthPassword(),"invalid");
207 assertEquals(config.getBasicAuthenticationCredentials(),
208 generateAuthorizationHeaderValue("invalid", "invalid"));
213 * Class accessor validator
218 public void validateClassAccessors() throws Exception {
221 * Setup encryptor expectations
223 Mockito.when(encryptorMock.decryptValue(Mockito.anyString())).thenReturn("password");
225 ActiveInventorySslConfig config =
226 new ActiveInventorySslConfig(buildInvalidPropertyDefinition(), encryptorMock);
229 * Now verify that all the internal members have been set to default values
232 config.setBasicAuthPassword("test");
233 config.setBasicAuthUsername("test");
234 config.setKeystoreFilename("test");
235 config.setKeystorePassword("test");
236 config.setKeystoreType("test");
237 config.setTruststoreFilename("test");
238 config.setTruststoreType("test");
239 config.setEncryptor(encryptorMock);
240 config.setValidateServerCertificateChain(true);
241 config.setValidateServerHostName(true);
243 assertEquals(System.getProperty("javax.net.debug"), "ssl");
244 assertTrue(config.isValidateServerHostName());
245 assertTrue(config.isValidateServerCertificateChain());
247 assertEquals(config.getKeystoreType(),"test");
248 assertTrue(config.getKeystoreFilename().contains("test"));
249 assertEquals(config.getKeystorePassword(),"test");
251 assertEquals(config.getTruststoreType(),"test");
252 assertTrue(config.getTruststoreFilename().contains("test"));
254 assertEquals(config.getBasicAuthUsername(),"test");
255 assertEquals(config.getBasicAuthPassword(),"test");
256 assertEquals(config.getBasicAuthenticationCredentials(),
257 generateAuthorizationHeaderValue("test", "test"));
259 assertNotNull(config.getEncryptor());
261 assertTrue(config.toString().contains("ActiveInventorySslConfig"));