2 * ============LICENSE_START=======================================================
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
11 * http://www.apache.org/licenses/LICENSE-2.0
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=========================================================
21 package org.openecomp.sdc.be.components.distribution.engine;
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;
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;
52 import org.openecomp.sdc.common.impl.ExternalConfiguration;
53 import org.openecomp.sdc.common.impl.FSConfigurationSource;
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;
61 public class EnvironmentsEngineTest {
64 private EnvironmentsEngine envEngine;
66 private OperationalEnvironmentDao operationalEnvironmentDao;
68 private ConfigurationManager configurationManager;
70 private DistributionEngineConfiguration distributionEngineConfiguration;
72 private AaiRequestHandler aaiRequestHandler;
75 public void preStart() {
76 MockitoAnnotations.initMocks(this);
77 new ConfigurationManager(new FSConfigurationSource(ExternalConfiguration.getChangeListener(), "src/test/resources/config/catalog-be"));
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);
99 Map<String, OperationalEnvironmentEntry> mapEnvs = envEngine.getEnvironments();
100 assertEquals("unexpected size of map",3, mapEnvs.size());
105 public void testGetFullOperationalEnvByIdSuccess() {
106 String json = getFullOperationalEnvJson();
108 HttpResponse restResponse = new HttpResponse(json, HttpStatus.SC_OK, "Successfully completed");
109 when(aaiRequestHandler.getOperationalEnvById(Mockito.anyString())).thenReturn(restResponse);
111 Either<OperationalEnvInfo, Integer> response = envEngine.getOperationalEnvById("DummyId");
112 assertTrue("The operational environment request ran as not expected", response.isLeft());
114 OperationalEnvInfo operationalEnvInfo = response.left().value();
116 assertEquals("The operational environment json is not as expected", operationalEnvInfo.toString(), json);
120 public void testGetPartialOperationalEnvByIdSuccess() {
121 String json = getPartialOperationalEnvJson();
123 HttpResponse<String> restResponse = new HttpResponse<String>(json, HttpStatus.SC_OK, "Successfully completed");
124 when(aaiRequestHandler.getOperationalEnvById(Mockito.anyString())).thenReturn(restResponse);
126 Either<OperationalEnvInfo, Integer> response = envEngine.getOperationalEnvById("DummyId");
127 assertTrue("The operational environment request ran as not expected", response.isLeft());
129 OperationalEnvInfo operationalEnvInfo = response.left().value();
131 assertEquals("The operational environment json is not as expected", operationalEnvInfo.toString(), json);
136 public void testGetOperationalEnvByIdFailedByJsonConvert() {
137 String jsonCorrupted = getCorruptedOperationalEnvJson();
139 HttpResponse<String> restResponse = new HttpResponse<String>(jsonCorrupted, HttpStatus.SC_OK, "Successfully Completed");
140 when(aaiRequestHandler.getOperationalEnvById(Mockito.anyString())).thenReturn(restResponse);
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());
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);
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());
159 @Test(expected = IOException.class)
160 public void testCorruptedOperationalEnvJson() throws IOException {
161 String jsonCorrupted = getCorruptedOperationalEnvJson();
162 OperationalEnvInfo.createFromJson(jsonCorrupted);
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);
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);
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);
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");
210 assertThat(envEngine.getEnvironmentByDmaapUebAddress(Arrays.asList("77","55"))
211 .getEnvironmentId()).isEqualTo("2");
213 assertThat(envEngine.getEnvironmentByDmaapUebAddress(Arrays.asList("11","44"))
214 .getEnvironmentId()).isEqualTo("1");
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\"}";
227 private String getPartialOperationalEnvJson() {
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\":[]" +
242 private String getFullOperationalEnvJson() {
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\":[" +
254 "\"related-to\":\"operational-environment\"," +
255 "\"relationship-label\":\"managedBy\"," +
256 "\"related-link\":\"/aai/v12/cloud-infrastructure/operational-environments/operational-environment/OEid3\"," +
257 "\"relationship-data\":[" +
259 "\"relationship-key\":\"operational-environment.operational-environment-id\"," +
260 "\"relationship-value\":\"OEid3\"" +
263 "\"related-to-property\":[" +
265 "\"property-key\":\"operational-environment.operational-environment-name\"," +
266 "\"property-value\":\"OEname3\"" +
270 private OperationalEnvironmentEntry createOpEnvEntry(String name) {
271 OperationalEnvironmentEntry entry = new OperationalEnvironmentEntry();
272 entry.setEnvironmentId(name);
276 public void testHandleMessageLogic() throws Exception {
277 String notification = "";
281 result = envEngine.handleMessageLogic(notification);
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);
292 Deencapsulation.invoke(envEngine, "validateNotification", errorWrapper, notificationData,
293 auditNotificationData);
297 public void testSaveEntryWithFailedStatus() throws Exception {
298 Wrapper<Boolean> errorWrapper = new Wrapper<>();
299 OperationalEnvironmentEntry opEnvEntry = new OperationalEnvironmentEntry();
302 Deencapsulation.invoke(envEngine, "saveEntryWithFailedStatus", errorWrapper, opEnvEntry);
306 public void testRetrieveUebAddressesFromAftDme() throws Exception {
307 Wrapper<Boolean> errorWrapper = new Wrapper<>();
308 OperationalEnvironmentEntry opEnvEntry = new OperationalEnvironmentEntry();
311 Deencapsulation.invoke(envEngine, "retrieveUebAddressesFromAftDme", errorWrapper, opEnvEntry);
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));
321 Deencapsulation.invoke(envEngine, "retrieveOpEnvInfoFromAAI", new Wrapper<>(), opEnvEntry);
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));
331 Deencapsulation.invoke(envEngine, "retrieveOpEnvInfoFromAAI", new Wrapper<>(), opEnvEntry);
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);
340 Deencapsulation.invoke(envEngine, "saveEntryWithInProgressStatus", errorWrapper, opEnvEntryWrapper,
345 public void testValidateStateGeneralError() throws Exception {
346 Wrapper<Boolean> errorWrapper = null;
347 IDmaapNotificationData notificationData = Mockito.mock(IDmaapNotificationData.class);
349 Mockito.when(operationalEnvironmentDao.get(Mockito.nullable(String.class)))
350 .thenReturn(Either.right(CassandraOperationStatus.GENERAL_ERROR));
352 Deencapsulation.invoke(envEngine, "validateState", new Wrapper<>(), notificationData);
356 public void testValidateState() throws Exception {
357 Wrapper<Boolean> errorWrapper = null;
358 IDmaapNotificationData notificationData = Mockito.mock(IDmaapNotificationData.class);
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));
364 Deencapsulation.invoke(envEngine, "validateState", new Wrapper<>(), notificationData);
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);
373 Deencapsulation.invoke(envEngine, "validateActionType", errorWrapper, notificationData);
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);
382 Deencapsulation.invoke(envEngine, "validateActionType", errorWrapper, notificationData);
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);
394 Deencapsulation.invoke(envEngine, "validateEnvironmentType", errorWrapper, notificationData,
395 auditNotificationData);
399 public void testMap2OpEnvKey() throws Exception {
400 OperationalEnvironmentEntry entry = new OperationalEnvironmentEntry();
404 result = Deencapsulation.invoke(envEngine, "map2OpEnvKey", entry);