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