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