Merge "Fix Blocker/Critical sonar issues"
[aai/sparky-be.git] / src / test / java / org / onap / aai / sparky / dal / aai / config / ActiveInventorySslConfigTest.java
1 /* 
2 * ============LICENSE_START=======================================================
3 * SPARKY (inventory 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.dal.aai.config;
27
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;
33
34 import java.util.Properties;
35
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;
42
43 //import com.att.aai.util.EncryptedConfiguration;
44
45 public class ActiveInventorySslConfigTest {
46
47   private Encryptor encryptorMock = Mockito.mock(Encryptor.class);
48
49   /**
50    * Test case initialization
51    * 
52    * @throws Exception the exception
53    */
54   @Before
55   public void init() throws Exception {
56     System.setProperty("javax.net.debug", "invalid");
57   }
58
59   private Properties buildExpectedPropertyDefinition() throws Exception {
60     Encryptor encryptor = new Encryptor();
61     Properties props = new Properties();
62
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"));
74
75     return props;
76   }
77
78   private Properties buildInvalidPropertyDefinition() {
79     Properties props = new Properties();
80
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");
91
92     return props;
93   }
94
95   private String generateAuthorizationHeaderValue(String username, String password) {
96     String usernameAndPassword = username + ":" + password;
97     return "Basic " + java.util.Base64.getEncoder().encodeToString(usernameAndPassword.getBytes());
98   }
99
100   /**
101    * Success path initialization and validation of accessors
102    * 
103    * @throws Exception
104    */
105   @Test
106   public void successfulInitialization() throws Exception {
107
108     /*
109      * Setup encryptor expectations
110      */
111     Mockito.when(encryptorMock.decryptValue(Mockito.anyString())).thenReturn("password");
112
113     ActiveInventorySslConfig config =
114         new ActiveInventorySslConfig(buildExpectedPropertyDefinition(), encryptorMock);
115
116     /*
117      * Now verify that all the internal members have been set to default values
118      */
119
120     assertEquals(System.getProperty("javax.net.debug"), "");
121     assertFalse(config.isValidateServerHostName());
122     assertFalse(config.isValidateServerCertificateChain());
123
124     assertEquals(config.getKeystoreType(), "pkcs12");
125     assertTrue(config.getKeystoreFilename().contains("/opt/app/applocal/etc/cert.crt"));
126     assertEquals(config.getKeystorePassword(), "password");
127
128     assertEquals(config.getTruststoreType(), "jks");
129     assertTrue(config.getTruststoreFilename().contains("/opt/app/applocal/etc/cert.crt"));
130
131     assertEquals(config.getBasicAuthUsername(), "username");
132     assertEquals(config.getBasicAuthPassword(), "password");
133     assertEquals(config.getBasicAuthenticationCredentials(),
134         generateAuthorizationHeaderValue("username", "password"));
135
136   }
137
138   /**
139    * Failed path initialization
140    * 
141    * @throws Exception
142    */
143   @Test
144   public void validateInitializationWithNullProperties() throws Exception {
145
146     /*
147      * Setup encryptor expectations
148      */
149     Mockito.when(encryptorMock.decryptValue(Mockito.anyString())).thenReturn("");
150
151     ActiveInventorySslConfig config = new ActiveInventorySslConfig(null, encryptorMock);
152
153     /*
154      * Now verify that all the internal members have been set to default values
155      */
156
157     assertEquals(System.getProperty("javax.net.debug"), "invalid");
158     assertFalse(config.isValidateServerHostName());
159     assertFalse(config.isValidateServerCertificateChain());
160
161     assertNull(config.getKeystoreType());
162     assertNull(config.getKeystoreFilename());
163     assertNull(config.getKeystorePassword());
164
165     assertNull(config.getTruststoreType());
166     assertNull(config.getTruststoreFilename());
167
168     assertNull(config.getBasicAuthUsername());
169     assertNull(config.getBasicAuthPassword());
170     assertEquals(config.getBasicAuthenticationCredentials(),
171         generateAuthorizationHeaderValue("null", "null"));
172
173   }
174
175   /**
176    * Failed path initialization
177    * 
178    * @throws Exception
179    */
180   @Test
181   public void validateInitializationWithInvalidProperties() throws Exception {
182
183     /*
184      * Setup encryptor expectations
185      */
186     Mockito.when(encryptorMock.decryptValue(Mockito.anyString())).thenReturn("");
187
188     ActiveInventorySslConfig config =
189         new ActiveInventorySslConfig(buildInvalidPropertyDefinition(), encryptorMock);
190
191     /*
192      * Now verify that all the internal members have been set to default values
193      */
194
195     assertEquals(System.getProperty("javax.net.debug"), "ssl");
196     assertFalse(config.isValidateServerHostName());
197     assertFalse(config.isValidateServerCertificateChain());
198
199     assertEquals(config.getKeystoreType(),"invalid");
200     assertTrue(config.getKeystoreFilename().contains("null"));
201     assertEquals(config.getKeystorePassword(),"");
202
203     assertEquals(config.getTruststoreType(),"invalid");
204     assertTrue(config.getTruststoreFilename().contains("null"));
205
206     assertEquals(config.getBasicAuthUsername(),"invalid");
207     assertEquals(config.getBasicAuthPassword(),"invalid");
208     assertEquals(config.getBasicAuthenticationCredentials(),
209         generateAuthorizationHeaderValue("invalid", "invalid"));
210
211   }
212
213   /**
214    * Class accessor validator
215    * 
216    * @throws Exception
217    */
218   @Test
219   public void validateClassAccessors() throws Exception {
220
221     /*
222      * Setup encryptor expectations
223      */
224     Mockito.when(encryptorMock.decryptValue(Mockito.anyString())).thenReturn("password");
225
226     ActiveInventorySslConfig config =
227         new ActiveInventorySslConfig(buildInvalidPropertyDefinition(), encryptorMock);
228
229     /*
230      * Now verify that all the internal members have been set to default values
231      */
232
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);
243     
244     assertEquals(System.getProperty("javax.net.debug"), "ssl");
245     assertTrue(config.isValidateServerHostName());
246     assertTrue(config.isValidateServerCertificateChain());
247
248     assertEquals(config.getKeystoreType(),"test");
249     assertTrue(config.getKeystoreFilename().contains("test"));
250     assertEquals(config.getKeystorePassword(),"test");
251
252     assertEquals(config.getTruststoreType(),"test");
253     assertTrue(config.getTruststoreFilename().contains("test"));
254
255     assertEquals(config.getBasicAuthUsername(),"test");
256     assertEquals(config.getBasicAuthPassword(),"test");
257     assertEquals(config.getBasicAuthenticationCredentials(),
258         generateAuthorizationHeaderValue("test", "test"));
259     
260     assertNotNull(config.getEncryptor());
261     
262     assertTrue(config.toString().contains("ActiveInventorySslConfig"));
263     
264     
265   }
266   
267   
268   
269 }