9ac04885b3f0b1cc384c3cb42752213708d1d840
[sdc.git] /
1 /*-
2  * ============LICENSE_START=======================================================
3  * SDC
4  * ================================================================================
5  * Copyright (C) 2019 AT&T Intellectual Property. All rights reserved.
6  * ================================================================================
7  * Licensed under the Apache License, Version 2.0 (the "License");
8  * you may not use this file except in compliance with the License.
9  * You may obtain a copy of the License at
10  * 
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  * 
13  * Unless required by applicable law or agreed to in writing, software
14  * distributed under the License is distributed on an "AS IS" BASIS,
15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  * See the License for the specific language governing permissions and
17  * limitations under the License.
18  * ============LICENSE_END=========================================================
19  */
20
21 package org.openecomp.sdc.be.components.distribution.engine;
22
23 import fj.data.Either;
24 import mockit.Deencapsulation;
25 import org.apache.http.HttpStatus;
26 import org.junit.Before;
27 import org.junit.Test;
28 import org.mockito.InjectMocks;
29 import org.mockito.Mock;
30 import org.mockito.Mockito;
31 import org.mockito.MockitoAnnotations;
32 import org.openecomp.sdc.be.components.impl.exceptions.ComponentException;
33 import org.openecomp.sdc.be.config.Configuration;
34 import org.openecomp.sdc.be.config.ConfigurationManager;
35 import org.openecomp.sdc.be.config.DistributionEngineConfiguration;
36 import org.openecomp.sdc.be.config.DmaapConsumerConfiguration;
37 import org.openecomp.sdc.be.dao.cassandra.CassandraOperationStatus;
38 import org.openecomp.sdc.be.dao.cassandra.OperationalEnvironmentDao;
39 import org.openecomp.sdc.be.datatypes.enums.EnvironmentStatusEnum;
40 import org.openecomp.sdc.be.info.OperationalEnvInfo;
41 import org.openecomp.sdc.be.resources.data.OperationalEnvironmentEntry;
42 import org.openecomp.sdc.common.datastructure.Wrapper;
43 import org.openecomp.sdc.common.http.client.api.HttpResponse;
44
45 import java.io.IOException;
46 import java.util.ArrayList;
47 import java.util.Arrays;
48 import java.util.Collections;
49 import java.util.HashSet;
50 import java.util.List;
51 import java.util.Map;
52 import org.openecomp.sdc.common.impl.ExternalConfiguration;
53 import org.openecomp.sdc.common.impl.FSConfigurationSource;
54
55 import static org.assertj.core.api.Assertions.assertThat;
56 import static org.assertj.core.api.Assertions.assertThatThrownBy;
57 import static org.junit.Assert.assertEquals;
58 import static org.junit.Assert.assertTrue;
59 import static org.mockito.Mockito.when;
60
61 public class EnvironmentsEngineTest {
62
63     @InjectMocks
64     private EnvironmentsEngine envEngine;
65     @Mock
66     private OperationalEnvironmentDao operationalEnvironmentDao;
67     @Mock
68     private ConfigurationManager configurationManager;
69     @Mock
70     private DistributionEngineConfiguration distributionEngineConfiguration;
71     @Mock
72     private AaiRequestHandler aaiRequestHandler;
73
74     @Before
75     public void preStart() {
76         MockitoAnnotations.initMocks(this);
77         new ConfigurationManager(new FSConfigurationSource(ExternalConfiguration.getChangeListener(), "src/test/resources/config/catalog-be"));
78     }
79
80     @Test
81     public void testInit() {
82         envEngine.setConfigurationManager(configurationManager);
83         Configuration config = Mockito.mock(Configuration.class);
84         DmaapConsumerConfiguration dmaapConf = Mockito.mock(DmaapConsumerConfiguration.class);
85         List<OperationalEnvironmentEntry> entryList = Arrays.asList(createOpEnvEntry("Env1"), createOpEnvEntry("Env2"));
86         Either<List<OperationalEnvironmentEntry>, CassandraOperationStatus> successEither = Either.left(entryList);
87         when(operationalEnvironmentDao.getByEnvironmentsStatus(EnvironmentStatusEnum.COMPLETED)).thenReturn(successEither);
88         when(configurationManager.getDistributionEngineConfiguration()).thenReturn(distributionEngineConfiguration);
89         when(distributionEngineConfiguration.getEnvironments()).thenReturn(Collections.singletonList("Env Loaded From Configuration"));
90         when(distributionEngineConfiguration.getUebPublicKey()).thenReturn("Dummy Public Key");
91         when(distributionEngineConfiguration.getUebSecretKey()).thenReturn("Dummy Private Key");
92         when(distributionEngineConfiguration.getUebServers()).thenReturn(
93                 Arrays.asList("uebsb91kcdc.it.com:3904", "uebsb92kcdc.it.com:3904", "uebsb91kcdc.it.com:3904"));
94         when(configurationManager.getConfiguration()).thenReturn(config);
95         when(config.getDmaapConsumerConfiguration()).thenReturn(dmaapConf);
96         when(dmaapConf.isActive()).thenReturn(false);
97         envEngine.init();
98
99         Map<String, OperationalEnvironmentEntry> mapEnvs = envEngine.getEnvironments();
100         assertEquals("unexpected size of map",3, mapEnvs.size());
101     }
102
103
104     @Test
105     public void testGetFullOperationalEnvByIdSuccess() {
106         String json = getFullOperationalEnvJson();
107
108         HttpResponse restResponse = new HttpResponse(json, HttpStatus.SC_OK, "Successfully completed");
109         when(aaiRequestHandler.getOperationalEnvById(Mockito.anyString())).thenReturn(restResponse);
110
111         Either<OperationalEnvInfo, Integer> response = envEngine.getOperationalEnvById("DummyId");
112         assertTrue("The operational environment request ran as not expected", response.isLeft());
113
114         OperationalEnvInfo operationalEnvInfo = response.left().value();
115
116         assertEquals("The operational environment json is not as expected", operationalEnvInfo.toString(), json);
117     }
118
119     @Test
120     public void testGetPartialOperationalEnvByIdSuccess() {
121         String json = getPartialOperationalEnvJson();
122
123         HttpResponse<String> restResponse = new HttpResponse<String>(json, HttpStatus.SC_OK, "Successfully completed");
124         when(aaiRequestHandler.getOperationalEnvById(Mockito.anyString())).thenReturn(restResponse);
125
126         Either<OperationalEnvInfo, Integer> response = envEngine.getOperationalEnvById("DummyId");
127         assertTrue("The operational environment request ran as not expected", response.isLeft());
128
129         OperationalEnvInfo operationalEnvInfo = response.left().value();
130
131         assertEquals("The operational environment json is not as expected", operationalEnvInfo.toString(), json);
132     }
133
134
135     @Test
136     public void testGetOperationalEnvByIdFailedByJsonConvert() {
137         String jsonCorrupted = getCorruptedOperationalEnvJson();
138
139         HttpResponse<String> restResponse = new HttpResponse<String>(jsonCorrupted, HttpStatus.SC_OK, "Successfully Completed");
140         when(aaiRequestHandler.getOperationalEnvById(Mockito.anyString())).thenReturn(restResponse);
141
142         Either<OperationalEnvInfo, Integer> response = envEngine.getOperationalEnvById("DummyId");
143         assertTrue("The operational environment request ran as not expected", response.isRight());
144         assertEquals("The operational environment request status code is not as expected", (Integer)HttpStatus.SC_INTERNAL_SERVER_ERROR, response.right().value());
145     }
146
147     @Test
148     public void testGetOperationalEnvByIdFailed404() {
149         String json = getFullOperationalEnvJson();
150         HttpResponse<String> restResponse = new HttpResponse<String>(json, HttpStatus.SC_NOT_FOUND, "Not Found");
151         when(aaiRequestHandler.getOperationalEnvById(Mockito.anyString())).thenReturn(restResponse);
152
153         Either<OperationalEnvInfo, Integer> response = envEngine.getOperationalEnvById("DummyId");
154         assertTrue("The operational environment request ran as not expected", response.isRight());
155         assertEquals("The operational environment request status code is not as expected", (Integer)HttpStatus.SC_NOT_FOUND, response.right().value());
156     }
157
158
159     @Test(expected = IOException.class)
160     public void testCorruptedOperationalEnvJson() throws IOException {
161         String jsonCorrupted = getCorruptedOperationalEnvJson();
162         OperationalEnvInfo.createFromJson(jsonCorrupted);
163     }
164
165     @Test
166     public void getEnvironmentById() {
167         OperationalEnvironmentEntry oe = new OperationalEnvironmentEntry();
168         oe.setEnvironmentId("mock");
169         envEngine.addToMap(oe);
170         assertTrue(envEngine.isInMap("mock"));
171         assertTrue(envEngine.isInMap(oe));
172         OperationalEnvironmentEntry returnedOe = envEngine.getEnvironmentById("mock");
173         assertTrue(oe == returnedOe);
174     }
175
176     @Test
177     public void getEnvironmentByDmaapUebAddressNoProperEnvironment() {
178         OperationalEnvironmentEntry opEnvEntry = createOpEnvEntry("1");
179         opEnvEntry.setDmaapUebAddress(new HashSet<>());
180         envEngine.addToMap(opEnvEntry);
181         assertThatThrownBy(() -> {
182             envEngine.getEnvironmentByDmaapUebAddress(Arrays.asList("11", "22"));})
183                 .isInstanceOf(ComponentException.class);
184     }
185
186     @Test
187     public void getEnvironmentByDmaapUebAddressListWithEmptyList() {
188         OperationalEnvironmentEntry opEnvEntry = createOpEnvEntry("1");
189         opEnvEntry.setDmaapUebAddress(new HashSet<>(Arrays.asList("11","22")));
190         OperationalEnvironmentEntry opEnvEntry2 = createOpEnvEntry("2");
191         opEnvEntry2.setDmaapUebAddress(new HashSet<>(Arrays.asList("33","44","55")));
192         envEngine.addToMap(opEnvEntry);
193         envEngine.addToMap(opEnvEntry2);
194         assertThatThrownBy(() -> {
195             envEngine.getEnvironmentByDmaapUebAddress(new ArrayList<>());})
196                 .isInstanceOf(ComponentException.class);
197     }
198
199     @Test
200     public void getEnvironmentByDmaapUebAddressList() {
201         OperationalEnvironmentEntry opEnvEntry = createOpEnvEntry("1");
202         opEnvEntry.setDmaapUebAddress(new HashSet<>(Arrays.asList("11","22")));
203         OperationalEnvironmentEntry opEnvEntry2 = createOpEnvEntry("2");
204         opEnvEntry2.setDmaapUebAddress(new HashSet<>(Arrays.asList("33","44","55")));
205         envEngine.addToMap(opEnvEntry);
206         envEngine.addToMap(opEnvEntry2);
207         assertThat(envEngine.getEnvironmentByDmaapUebAddress(Arrays.asList("77","22"))
208                 .getEnvironmentId()).isEqualTo("1");
209
210         assertThat(envEngine.getEnvironmentByDmaapUebAddress(Arrays.asList("77","55"))
211                 .getEnvironmentId()).isEqualTo("2");
212
213         assertThat(envEngine.getEnvironmentByDmaapUebAddress(Arrays.asList("11","44"))
214                 .getEnvironmentId()).isEqualTo("1");
215     }
216
217
218
219
220     private String getCorruptedOperationalEnvJson() {
221         return "{\"OPERATIONAL-environment-name\":\"Op Env Name\","
222                 + "\"OPERATIONAL-environment-type\":\"VNF\","
223                 + "\"OPERATIONAL-environment-status\":\"Activate\","
224                 + "\"tenant-context\":\"Test\"}";
225     }
226
227     private String getPartialOperationalEnvJson() {
228         return "{" +
229                 "\"operational-environment-id\":\"UUID of Operational Environment\"," +
230                 "\"operational-environment-name\":\"Op Env Name\"," +
231                 "\"operational-environment-type\":\"VNF\"," +
232                 "\"operational-environment-status\":\"Activate\"," +
233                 "\"tenant-context\":\"Test\"," +
234                 "\"workload-context\":\"VNF_Development\"," +
235                 "\"resource-version\":\"1505228226913\"," +
236                 "\"relationship-list\":{" +
237                 "\"relationship\":[]" +
238                 "}" +
239                 "}";
240     }
241
242     private String getFullOperationalEnvJson() {
243         return  "{" +
244                 "\"operational-environment-id\":\"OEid1\"," +
245                 "\"operational-environment-name\":\"OEname1\"," +
246                 "\"operational-environment-type\":\"OEtype1\"," +
247                 "\"operational-environment-status\":\"OEstatus1\"," +
248                 "\"tenant-context\":\"OEtenantcontext1\"," +
249                 "\"workload-context\":\"OEworkloadcontext1\"," +
250                 "\"resource-version\":\"1511363173278\"," +
251                 "\"relationship-list\":{" +
252                 "\"relationship\":[" +
253                 "{" +
254                 "\"related-to\":\"operational-environment\"," +
255                 "\"relationship-label\":\"managedBy\"," +
256                 "\"related-link\":\"/aai/v12/cloud-infrastructure/operational-environments/operational-environment/OEid3\"," +
257                 "\"relationship-data\":[" +
258                 "{" +
259                 "\"relationship-key\":\"operational-environment.operational-environment-id\"," +
260                 "\"relationship-value\":\"OEid3\"" +
261                 "}" +
262                 "]," +
263                 "\"related-to-property\":[" +
264                 "{" +
265                 "\"property-key\":\"operational-environment.operational-environment-name\"," +
266                 "\"property-value\":\"OEname3\"" +
267                 "}]}]}}";
268     }
269
270     private OperationalEnvironmentEntry createOpEnvEntry(String name) {
271         OperationalEnvironmentEntry entry = new OperationalEnvironmentEntry();
272         entry.setEnvironmentId(name);
273         return entry;
274     }
275
276     public void testHandleMessageLogic() throws Exception {
277         String notification = "";
278         boolean result;
279
280         // default test
281         result = envEngine.handleMessageLogic(notification);
282     }
283
284     @Test
285     public void testValidateNotification() throws Exception {
286         Wrapper<Boolean> errorWrapper = new Wrapper<>();
287         errorWrapper.setInnerElement(true);
288         IDmaapNotificationData notificationData = Mockito.mock(IDmaapNotificationData.class);
289         IDmaapAuditNotificationData auditNotificationData = Mockito.mock(IDmaapAuditNotificationData.class);
290
291         // default test
292         Deencapsulation.invoke(envEngine, "validateNotification", errorWrapper, notificationData,
293                 auditNotificationData);
294     }
295
296     @Test
297     public void testSaveEntryWithFailedStatus() throws Exception {
298         Wrapper<Boolean> errorWrapper = new Wrapper<>();
299         OperationalEnvironmentEntry opEnvEntry = new OperationalEnvironmentEntry();
300
301         // default test
302         Deencapsulation.invoke(envEngine, "saveEntryWithFailedStatus", errorWrapper, opEnvEntry);
303     }
304
305     @Test
306     public void testRetrieveUebAddressesFromAftDme() throws Exception {
307         Wrapper<Boolean> errorWrapper = new Wrapper<>();
308         OperationalEnvironmentEntry opEnvEntry = new OperationalEnvironmentEntry();
309
310         // default test
311         Deencapsulation.invoke(envEngine, "retrieveUebAddressesFromAftDme", errorWrapper, opEnvEntry);
312     }
313
314     @Test
315     public void testRetrieveOpEnvInfoFromAAI() throws Exception {
316         Wrapper<Boolean> errorWrapper = new Wrapper<>();
317         OperationalEnvironmentEntry opEnvEntry = new OperationalEnvironmentEntry();
318         opEnvEntry.setEnvironmentId("mock");
319         Mockito.when(aaiRequestHandler.getOperationalEnvById(Mockito.nullable(String.class))).thenReturn(new HttpResponse<String>("{}", 200));
320         // default test
321         Deencapsulation.invoke(envEngine, "retrieveOpEnvInfoFromAAI", new Wrapper<>(), opEnvEntry);
322     }
323
324     @Test
325     public void testRetrieveOpEnvInfoFromAAIError() throws Exception {
326         Wrapper<Boolean> errorWrapper = new Wrapper<>();
327         OperationalEnvironmentEntry opEnvEntry = new OperationalEnvironmentEntry();
328         opEnvEntry.setEnvironmentId("mock");
329         Mockito.when(aaiRequestHandler.getOperationalEnvById(Mockito.nullable(String.class))).thenReturn(new HttpResponse<String>("{}", 500));
330         // default test
331         Deencapsulation.invoke(envEngine, "retrieveOpEnvInfoFromAAI", new Wrapper<>(), opEnvEntry);
332     }
333
334     @Test
335     public void testSaveEntryWithInProgressStatus() throws Exception {
336         Wrapper<Boolean> errorWrapper = new Wrapper<>();
337         Wrapper<OperationalEnvironmentEntry> opEnvEntryWrapper = new Wrapper<>();
338         IDmaapNotificationData notificationData = Mockito.mock(IDmaapNotificationData.class);
339
340         Deencapsulation.invoke(envEngine, "saveEntryWithInProgressStatus", errorWrapper, opEnvEntryWrapper,
341                 notificationData);
342     }
343
344     @Test
345     public void testValidateStateGeneralError() throws Exception {
346         Wrapper<Boolean> errorWrapper = null;
347         IDmaapNotificationData notificationData = Mockito.mock(IDmaapNotificationData.class);
348
349         Mockito.when(operationalEnvironmentDao.get(Mockito.nullable(String.class)))
350                 .thenReturn(Either.right(CassandraOperationStatus.GENERAL_ERROR));
351
352         Deencapsulation.invoke(envEngine, "validateState", new Wrapper<>(), notificationData);
353     }
354
355     @Test
356     public void testValidateState() throws Exception {
357         Wrapper<Boolean> errorWrapper = null;
358         IDmaapNotificationData notificationData = Mockito.mock(IDmaapNotificationData.class);
359
360         OperationalEnvironmentEntry a = new OperationalEnvironmentEntry();
361         a.setStatus(EnvironmentStatusEnum.IN_PROGRESS.getName());
362         Mockito.when(operationalEnvironmentDao.get(Mockito.nullable(String.class))).thenReturn(Either.left(a));
363
364         Deencapsulation.invoke(envEngine, "validateState", new Wrapper<>(), notificationData);
365     }
366
367     @Test
368     public void testValidateActionType() throws Exception {
369         Wrapper<Boolean> errorWrapper = new Wrapper<>();
370         IDmaapNotificationData notificationData = Mockito.mock(IDmaapNotificationData.class);
371         Mockito.when(notificationData.getAction()).thenReturn(IDmaapNotificationData.DmaapActionEnum.DELETE);
372
373         Deencapsulation.invoke(envEngine, "validateActionType", errorWrapper, notificationData);
374     }
375
376     @Test
377     public void testValidateActionType2() throws Exception {
378         Wrapper<Boolean> errorWrapper = new Wrapper<>();
379         IDmaapNotificationData notificationData = Mockito.mock(IDmaapNotificationData.class);
380         Mockito.when(notificationData.getAction()).thenReturn(IDmaapNotificationData.DmaapActionEnum.CREATE);
381
382         Deencapsulation.invoke(envEngine, "validateActionType", errorWrapper, notificationData);
383     }
384
385     @Test
386     public void testValidateEnvironmentType() throws Exception {
387         Wrapper<Boolean> errorWrapper = new Wrapper<>();
388         IDmaapNotificationData notificationData = Mockito.mock(IDmaapNotificationData.class);
389         IDmaapAuditNotificationData auditNotificationData = Mockito.mock(IDmaapAuditNotificationData.class);
390         Mockito.when(auditNotificationData.getOperationalEnvironmentName()).thenReturn("mock");
391         Mockito.when(notificationData.getOperationalEnvironmentType()).thenReturn(IDmaapNotificationData.OperationaEnvironmentTypeEnum.ECOMP);
392
393         // default test
394         Deencapsulation.invoke(envEngine, "validateEnvironmentType", errorWrapper, notificationData,
395                 auditNotificationData);
396     }
397
398     @Test
399     public void testMap2OpEnvKey() throws Exception {
400         OperationalEnvironmentEntry entry = new OperationalEnvironmentEntry();
401         String result;
402
403         // default test
404         result = Deencapsulation.invoke(envEngine, "map2OpEnvKey", entry);
405     }
406 }