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