Initial commit for AAI-UI(sparky-backend)
[aai/sparky-be.git] / src / test / java / org / openecomp / sparky / dal / aai / config / ActiveInventoryRestConfigTest.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.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.ArrayList;
35 import java.util.List;
36 import java.util.Properties;
37
38 import org.junit.Before;
39 import org.junit.Test;
40 import org.openecomp.sparky.dal.aai.enums.RestAuthenticationMode;
41
42
43 public class ActiveInventoryRestConfigTest {
44
45   /**
46    * Test case initialization
47    * 
48    * @throws Exception the exception
49    */
50   @Before
51   public void init() throws Exception {}
52
53   private Properties buildExpectedPropertyDefinition() throws Exception {
54
55     Properties props = new Properties();
56
57     props.put("aai.rest.resourceBasePath", "/aai/v9");
58     props.put("aai.rest.host", "1.2.3.4");
59     props.put("aai.rest.port", "4321");
60     props.put("aai.rest.numRequestRetries", "100");
61     props.put("aai.rest.numResolverWorkers", "50");
62     props.put("aai.rest.maxConcurrentWorkers", "50");
63     props.put("aai.rest.connectTimeoutInMs", "1000");
64     props.put("aai.rest.readTimeoutInMs", "1500");
65     props.put("aai.rest.shallowEntities", "a,b,c,d");
66     props.put("aai.rest.authenticationMode", "HTTP_NOAUTH");
67
68     props.put("aai.rest.cache.enabled", "true");
69     props.put("aai.rest.cache.storageFolderOverride", "folderOverride");
70     props.put("aai.rest.cache.cacheFailures", "true");
71     props.put("aai.rest.cache.useCacheOnly", "true");
72     props.put("aai.rest.cache.numWorkers", "50");
73     props.put("aai.rest.cache.maxTimeToLiveInMs", "500");
74
75
76     return props;
77   }
78
79   /**
80    * Success path initialization and validation of accessors
81    * 
82    * @throws Exception
83    */
84   @Test
85   public void successfulInitialization() throws Exception {
86
87     ActiveInventoryRestConfig config =
88         new ActiveInventoryRestConfig(buildExpectedPropertyDefinition());
89
90     /*
91      * Now verify that all the internal members have been set to default values
92      */
93
94     assertEquals(config.getResourceBasePath(), "/aai/v9");
95     assertEquals(config.getHost(), "1.2.3.4");
96     assertEquals(config.getPort(), "4321");
97     assertEquals(config.getNumRequestRetries(), 100);
98     assertEquals(config.getNumResolverWorkers(), 50);
99     assertEquals(config.getConnectTimeoutInMs(), 1000);
100     assertEquals(config.getReadTimeoutInMs(), 1500);
101
102     List<String> expectedEntities = new ArrayList<String>();
103     expectedEntities.add("a");
104     expectedEntities.add("b");
105     expectedEntities.add("c");
106     expectedEntities.add("d");
107
108     assertEquals(config.getShallowEntities().size(), 4);
109     assertTrue(config.getShallowEntities().containsAll(expectedEntities));
110     assertEquals(config.getAuthenticationMode(), RestAuthenticationMode.HTTP_NOAUTH);
111
112     assertTrue(config.isCacheEnabled());
113     assertEquals(config.getStorageFolderOverride(), "folderOverride");
114     assertTrue(config.shouldCacheFailures());
115     assertTrue(config.isUseCacheOnly());
116     assertEquals(config.getNumCacheWorkers(), 50);
117     assertEquals(config.getMaxTimeToLiveInMs(), 500);
118
119
120   }
121
122   /**
123    * Failed path initialization
124    * 
125    * @throws Exception
126    */
127   @Test
128   public void validateInitializationWithNullProperties() throws Exception {
129
130     /*
131      * Setup encryptor expectations
132      */
133
134     ActiveInventoryRestConfig config = new ActiveInventoryRestConfig(null);
135
136     /*
137      * Now verify that all the internal members have been set to default values
138      */
139
140     assertNull(config.getResourceBasePath());
141     assertNull(config.getHost());
142     assertNull(config.getPort());
143     assertEquals(config.getNumRequestRetries(), 0);
144     assertEquals(config.getNumResolverWorkers(), 0);
145     assertEquals(config.getConnectTimeoutInMs(), 0);
146     assertEquals(config.getReadTimeoutInMs(), 0);
147
148     assertNull(config.getShallowEntities());
149     assertNull(config.getAuthenticationMode());
150
151     assertFalse(config.isCacheEnabled());
152     assertNull(config.getStorageFolderOverride());
153     assertFalse(config.shouldCacheFailures());
154     assertFalse(config.isUseCacheOnly());
155     assertEquals(config.getNumCacheWorkers(), 0);
156     assertEquals(config.getMaxTimeToLiveInMs(), 0);
157
158   }
159
160   /**
161    * Failed path initialization
162    * 
163    * @throws Exception
164    */
165   @Test
166   public void validateInitializationWithInvalidProperties() throws Exception {
167
168     /*
169      * Setup encryptor expectations
170      */
171
172     ActiveInventoryRestConfig config = new ActiveInventoryRestConfig(new Properties());
173
174     /*
175      * Now verify that all the internal members have been set to default values
176      */
177
178     assertEquals(config.getResourceBasePath(), "/aai/v7");
179     assertEquals(config.getHost(), "localhost");
180     assertEquals(config.getPort(), "8443");
181     assertEquals(config.getNumRequestRetries(), 5);
182     assertEquals(config.getNumResolverWorkers(), 15);
183     assertEquals(config.getConnectTimeoutInMs(), 5000);
184     assertEquals(config.getReadTimeoutInMs(), 10000);
185
186     assertEquals(config.getShallowEntities().size(), 1);
187     assertEquals(config.getAuthenticationMode(), RestAuthenticationMode.SSL_CERT);
188
189     assertFalse(config.isCacheEnabled());
190     assertNull(config.getStorageFolderOverride());
191     assertFalse(config.shouldCacheFailures());
192     assertFalse(config.isUseCacheOnly());
193     assertEquals(config.getNumCacheWorkers(), 5);
194     assertEquals(config.getMaxTimeToLiveInMs(), -1);
195
196   }
197
198   /**
199    * Class accessor validator
200    * 
201    * @throws Exception
202    */
203   @Test
204   public void validateClassAccessors() throws Exception {
205
206     /*
207      * Setup encryptor expectations
208      */
209
210     ActiveInventoryRestConfig config =
211         new ActiveInventoryRestConfig(buildExpectedPropertyDefinition());
212
213     /*
214      * Now verify that all the internal members have been set to default values
215      */
216
217     config.setAuthenticationMode(RestAuthenticationMode.SSL_BASIC);
218     config.setCacheEnabled(true);
219     config.setConnectTimeoutInMs(1000);
220     config.setHost("myhost");
221     config.setMaxTimeToLiveInMs(1234);
222     config.setNumCacheWorkers(1000);
223     config.setNumRequestRetries(1500);
224     config.setNumResolverWorkers(150);
225     config.setPort("11223344");
226     config.setReadTimeoutInMs(54321);
227     config.setResourceBasePath("/aai/v21");
228     config.setStorageFolderOverride("override");
229     config.setUseCacheOnly(true);
230     config.setShouldCacheFailures(true);
231
232     assertEquals(config.getResourceBasePath(), "/aai/v21");
233     assertEquals(config.getHost(), "myhost");
234     assertEquals(config.getPort(), "11223344");
235     assertEquals(config.getNumRequestRetries(), 1500);
236     assertEquals(config.getNumResolverWorkers(), 150);
237     assertEquals(config.getConnectTimeoutInMs(), 1000);
238     assertEquals(config.getReadTimeoutInMs(), 54321);
239     assertTrue(config.shouldCacheFailures());
240
241     List<String> expectedEntities = new ArrayList<String>();
242     expectedEntities.add("a");
243     expectedEntities.add("b");
244     expectedEntities.add("c");
245     expectedEntities.add("d");
246
247     assertEquals(config.getShallowEntities().size(), 4);
248     assertTrue(config.getShallowEntities().containsAll(expectedEntities));
249     assertTrue(config.isShallowEntity("b"));
250     assertFalse(config.isShallowEntity("f"));
251     assertFalse(config.isShallowEntity(null));
252     assertEquals(config.getAuthenticationMode(), RestAuthenticationMode.SSL_BASIC);
253
254     assertTrue(config.isCacheEnabled());
255     assertEquals(config.getStorageFolderOverride(), "override");
256     assertTrue(config.shouldCacheFailures());
257     assertTrue(config.isUseCacheOnly());
258     assertEquals(config.getNumCacheWorkers(), 1000);
259     assertEquals(config.getMaxTimeToLiveInMs(), 1234);
260
261     assertTrue(config.toString().contains("ActiveInventoryRestConfig"));
262
263   }
264
265
266   /**
267    * Validate auth mode edge cases
268    * 
269    * @throws Exception
270    */
271   @Test
272   public void validateUnknownAuthModeDefaultsToSslCert() throws Exception {
273
274     /*
275      * Setup encryptor expectations
276      */
277
278     Properties props = buildExpectedPropertyDefinition();
279     props.setProperty("aai.rest.authenticationMode", "invalid mode");
280     props.setProperty("aai.rest.storageFolderOverride", "");
281
282     ActiveInventoryRestConfig config = new ActiveInventoryRestConfig(props);
283
284     /*
285      * Now verify that all the internal members have been set to default values
286      */
287
288     assertNotNull(config.getShallowEntities());
289     assertEquals(RestAuthenticationMode.SSL_CERT, config.getAuthenticationMode());
290
291   }
292
293 }