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;
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;
59 public class EnvironmentsEngineTest {
62 private EnvironmentsEngine envEngine;
64 private OperationalEnvironmentDao operationalEnvironmentDao;
66 private ConfigurationManager configurationManager;
68 private DistributionEngineConfiguration distributionEngineConfiguration;
70 private AaiRequestHandler aaiRequestHandler;
73 public void preStart() {
74 MockitoAnnotations.initMocks(this);
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);
96 Map<String, OperationalEnvironmentEntry> mapEnvs = envEngine.getEnvironments();
97 assertEquals("unexpected size of map",3, mapEnvs.size());
102 public void testGetFullOperationalEnvByIdSuccess() {
103 String json = getFullOperationalEnvJson();
105 HttpResponse restResponse = new HttpResponse(json, HttpStatus.SC_OK, "Successfully completed");
106 when(aaiRequestHandler.getOperationalEnvById(Mockito.anyString())).thenReturn(restResponse);
108 Either<OperationalEnvInfo, Integer> response = envEngine.getOperationalEnvById("DummyId");
109 assertTrue("The operational environment request ran as not expected", response.isLeft());
111 OperationalEnvInfo operationalEnvInfo = response.left().value();
113 assertEquals("The operational environment json is not as expected", operationalEnvInfo.toString(), json);
117 public void testGetPartialOperationalEnvByIdSuccess() {
118 String json = getPartialOperationalEnvJson();
120 HttpResponse<String> restResponse = new HttpResponse<String>(json, HttpStatus.SC_OK, "Successfully completed");
121 when(aaiRequestHandler.getOperationalEnvById(Mockito.anyString())).thenReturn(restResponse);
123 Either<OperationalEnvInfo, Integer> response = envEngine.getOperationalEnvById("DummyId");
124 assertTrue("The operational environment request ran as not expected", response.isLeft());
126 OperationalEnvInfo operationalEnvInfo = response.left().value();
128 assertEquals("The operational environment json is not as expected", operationalEnvInfo.toString(), json);
133 public void testGetOperationalEnvByIdFailedByJsonConvert() {
134 String jsonCorrupted = getCorruptedOperationalEnvJson();
136 HttpResponse<String> restResponse = new HttpResponse<String>(jsonCorrupted, HttpStatus.SC_OK, "Successfully Completed");
137 when(aaiRequestHandler.getOperationalEnvById(Mockito.anyString())).thenReturn(restResponse);
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());
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);
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());
156 @Test(expected = IOException.class)
157 public void testCorruptedOperationalEnvJson() throws IOException {
158 String jsonCorrupted = getCorruptedOperationalEnvJson();
159 OperationalEnvInfo.createFromJson(jsonCorrupted);
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);
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);
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);
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");
207 assertThat(envEngine.getEnvironmentByDmaapUebAddress(Arrays.asList("77","55"))
208 .getEnvironmentId()).isEqualTo("2");
210 assertThat(envEngine.getEnvironmentByDmaapUebAddress(Arrays.asList("11","44"))
211 .getEnvironmentId()).isEqualTo("1");
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\"}";
224 private String getPartialOperationalEnvJson() {
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\":[]" +
239 private String getFullOperationalEnvJson() {
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\":[" +
251 "\"related-to\":\"operational-environment\"," +
252 "\"relationship-label\":\"managedBy\"," +
253 "\"related-link\":\"/aai/v12/cloud-infrastructure/operational-environments/operational-environment/OEid3\"," +
254 "\"relationship-data\":[" +
256 "\"relationship-key\":\"operational-environment.operational-environment-id\"," +
257 "\"relationship-value\":\"OEid3\"" +
260 "\"related-to-property\":[" +
262 "\"property-key\":\"operational-environment.operational-environment-name\"," +
263 "\"property-value\":\"OEname3\"" +
267 private OperationalEnvironmentEntry createOpEnvEntry(String name) {
268 OperationalEnvironmentEntry entry = new OperationalEnvironmentEntry();
269 entry.setEnvironmentId(name);
273 public void testHandleMessageLogic() throws Exception {
274 String notification = "";
278 result = envEngine.handleMessageLogic(notification);
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);
289 Deencapsulation.invoke(envEngine, "validateNotification", errorWrapper, notificationData,
290 auditNotificationData);
294 public void testSaveEntryWithFailedStatus() throws Exception {
295 Wrapper<Boolean> errorWrapper = new Wrapper<>();
296 OperationalEnvironmentEntry opEnvEntry = new OperationalEnvironmentEntry();
299 Deencapsulation.invoke(envEngine, "saveEntryWithFailedStatus", errorWrapper, opEnvEntry);
303 public void testRetrieveUebAddressesFromAftDme() throws Exception {
304 Wrapper<Boolean> errorWrapper = new Wrapper<>();
305 OperationalEnvironmentEntry opEnvEntry = new OperationalEnvironmentEntry();
308 Deencapsulation.invoke(envEngine, "retrieveUebAddressesFromAftDme", errorWrapper, opEnvEntry);
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));
318 Deencapsulation.invoke(envEngine, "retrieveOpEnvInfoFromAAI", new Wrapper<>(), opEnvEntry);
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));
328 Deencapsulation.invoke(envEngine, "retrieveOpEnvInfoFromAAI", new Wrapper<>(), opEnvEntry);
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);
337 Deencapsulation.invoke(envEngine, "saveEntryWithInProgressStatus", errorWrapper, opEnvEntryWrapper,
342 public void testValidateStateGeneralError() throws Exception {
343 Wrapper<Boolean> errorWrapper = null;
344 IDmaapNotificationData notificationData = Mockito.mock(IDmaapNotificationData.class);
346 Mockito.when(operationalEnvironmentDao.get(Mockito.nullable(String.class)))
347 .thenReturn(Either.right(CassandraOperationStatus.GENERAL_ERROR));
349 Deencapsulation.invoke(envEngine, "validateState", new Wrapper<>(), notificationData);
353 public void testValidateState() throws Exception {
354 Wrapper<Boolean> errorWrapper = null;
355 IDmaapNotificationData notificationData = Mockito.mock(IDmaapNotificationData.class);
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));
361 Deencapsulation.invoke(envEngine, "validateState", new Wrapper<>(), notificationData);
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);
370 Deencapsulation.invoke(envEngine, "validateActionType", errorWrapper, notificationData);
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);
379 Deencapsulation.invoke(envEngine, "validateActionType", errorWrapper, notificationData);
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);
391 Deencapsulation.invoke(envEngine, "validateEnvironmentType", errorWrapper, notificationData,
392 auditNotificationData);
396 public void testMap2OpEnvKey() throws Exception {
397 OperationalEnvironmentEntry entry = new OperationalEnvironmentEntry();
401 result = Deencapsulation.invoke(envEngine, "map2OpEnvKey", entry);