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