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 com.att.nsa.apiClient.credentials.ApiCredential;
24 import fj.data.Either;
25 import mockit.Deencapsulation;
26 import org.apache.http.HttpStatus;
27 import org.junit.Before;
28 import org.junit.Test;
29 import org.mockito.InjectMocks;
30 import org.mockito.Mock;
31 import org.mockito.Mockito;
32 import org.mockito.MockitoAnnotations;
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;
47 import java.util.concurrent.atomic.AtomicBoolean;
49 import static org.junit.Assert.assertEquals;
50 import static org.junit.Assert.assertTrue;
51 import static org.mockito.Mockito.when;
53 public class EnvironmentsEngineTest {
56 private EnvironmentsEngine envEngine;
58 private OperationalEnvironmentDao operationalEnvironmentDao;
60 private ConfigurationManager configurationManager;
62 private DistributionEngineConfiguration distributionEngineConfiguration;
64 private AaiRequestHandler aaiRequestHandler;
67 public void preStart() {
68 MockitoAnnotations.initMocks(this);
72 public void testInit() {
73 envEngine.setConfigurationManager(configurationManager);
74 Configuration config = Mockito.mock(Configuration.class);
75 DmaapConsumerConfiguration dmaapConf = Mockito.mock(DmaapConsumerConfiguration.class);
76 List<OperationalEnvironmentEntry> entryList = Arrays.asList(createOpEnvEntry("Env1"), createOpEnvEntry("Env2"));
77 Either<List<OperationalEnvironmentEntry>, CassandraOperationStatus> successEither = Either.left(entryList);
78 when(operationalEnvironmentDao.getByEnvironmentsStatus(EnvironmentStatusEnum.COMPLETED)).thenReturn(successEither);
79 when(configurationManager.getDistributionEngineConfiguration()).thenReturn(distributionEngineConfiguration);
80 when(distributionEngineConfiguration.getEnvironments()).thenReturn(Arrays.asList("Env Loaded From Configuration"));
81 when(distributionEngineConfiguration.getUebPublicKey()).thenReturn("Dummy Public Key");
82 when(distributionEngineConfiguration.getUebSecretKey()).thenReturn("Dummy Private Key");
83 when(distributionEngineConfiguration.getUebServers()).thenReturn(
84 Arrays.asList("uebsb91kcdc.it.com:3904", "uebsb92kcdc.it.com:3904", "uebsb91kcdc.it.com:3904"));
85 when(configurationManager.getConfiguration()).thenReturn(config);
86 when(config.getDmaapConsumerConfiguration()).thenReturn(dmaapConf);
87 when(dmaapConf.isActive()).thenReturn(false);
90 Map<String, OperationalEnvironmentEntry> mapEnvs = envEngine.getEnvironments();
91 assertEquals("unexpected size of map",3, mapEnvs.size());
96 public void testGetFullOperationalEnvByIdSuccess() {
97 String json = getFullOperationalEnvJson();
99 HttpResponse restResponse = new HttpResponse(json, HttpStatus.SC_OK, "Successfully completed");
100 when(aaiRequestHandler.getOperationalEnvById(Mockito.anyString())).thenReturn(restResponse);
102 Either<OperationalEnvInfo, Integer> response = envEngine.getOperationalEnvById("DummyId");
103 assertTrue("The operational environment request ran as not expected", response.isLeft());
105 OperationalEnvInfo operationalEnvInfo = response.left().value();
107 assertEquals("The operational environment json is not as expected", operationalEnvInfo.toString(), json);
111 public void testGetPartialOperationalEnvByIdSuccess() {
112 String json = getPartialOperationalEnvJson();
114 HttpResponse<String> restResponse = new HttpResponse<String>(json, HttpStatus.SC_OK, "Successfully completed");
115 when(aaiRequestHandler.getOperationalEnvById(Mockito.anyString())).thenReturn(restResponse);
117 Either<OperationalEnvInfo, Integer> response = envEngine.getOperationalEnvById("DummyId");
118 assertTrue("The operational environment request ran as not expected", response.isLeft());
120 OperationalEnvInfo operationalEnvInfo = response.left().value();
122 assertEquals("The operational environment json is not as expected", operationalEnvInfo.toString(), json);
127 public void testGetOperationalEnvByIdFailedByJsonConvert() {
128 String jsonCorrupted = getCorruptedOperationalEnvJson();
130 HttpResponse<String> restResponse = new HttpResponse<String>(jsonCorrupted, HttpStatus.SC_OK, "Successfully Completed");
131 when(aaiRequestHandler.getOperationalEnvById(Mockito.anyString())).thenReturn(restResponse);
133 Either<OperationalEnvInfo, Integer> response = envEngine.getOperationalEnvById("DummyId");
134 assertTrue("The operational environment request ran as not expected", response.isRight());
135 assertEquals("The operational environment request status code is not as expected", (Integer)HttpStatus.SC_INTERNAL_SERVER_ERROR, response.right().value());
139 public void testGetOperationalEnvByIdFailed404() {
140 String json = getFullOperationalEnvJson();
141 HttpResponse<String> restResponse = new HttpResponse<String>(json, HttpStatus.SC_NOT_FOUND, "Not Found");
142 when(aaiRequestHandler.getOperationalEnvById(Mockito.anyString())).thenReturn(restResponse);
144 Either<OperationalEnvInfo, Integer> response = envEngine.getOperationalEnvById("DummyId");
145 assertTrue("The operational environment request ran as not expected", response.isRight());
146 assertEquals("The operational environment request status code is not as expected", (Integer)HttpStatus.SC_NOT_FOUND, response.right().value());
150 @Test(expected = IOException.class)
151 public void testCorruptedOperationalEnvJson() throws IOException {
152 String jsonCorrupted = getCorruptedOperationalEnvJson();
153 OperationalEnvInfo.createFromJson(jsonCorrupted);
157 public void getEnvironmentById() {
158 OperationalEnvironmentEntry oe = new OperationalEnvironmentEntry();
159 oe.setEnvironmentId("mock");
160 envEngine.addToMap(oe);
161 assertTrue(envEngine.isInMap("mock"));
162 assertTrue(envEngine.isInMap(oe));
163 OperationalEnvironmentEntry returnedOe = envEngine.getEnvironmentById("mock");
164 assertTrue(oe == returnedOe);
167 private String getCorruptedOperationalEnvJson() {
168 return "{\"OPERATIONAL-environment-name\":\"Op Env Name\","
169 + "\"OPERATIONAL-environment-type\":\"VNF\","
170 + "\"OPERATIONAL-environment-status\":\"Activate\","
171 + "\"tenant-context\":\"Test\"}";
174 private String getPartialOperationalEnvJson() {
176 "\"operational-environment-id\":\"UUID of Operational Environment\"," +
177 "\"operational-environment-name\":\"Op Env Name\"," +
178 "\"operational-environment-type\":\"VNF\"," +
179 "\"operational-environment-status\":\"Activate\"," +
180 "\"tenant-context\":\"Test\"," +
181 "\"workload-context\":\"VNF_Development\"," +
182 "\"resource-version\":\"1505228226913\"," +
183 "\"relationship-list\":{" +
184 "\"relationship\":[]" +
189 private String getFullOperationalEnvJson() {
191 "\"operational-environment-id\":\"OEid1\"," +
192 "\"operational-environment-name\":\"OEname1\"," +
193 "\"operational-environment-type\":\"OEtype1\"," +
194 "\"operational-environment-status\":\"OEstatus1\"," +
195 "\"tenant-context\":\"OEtenantcontext1\"," +
196 "\"workload-context\":\"OEworkloadcontext1\"," +
197 "\"resource-version\":\"1511363173278\"," +
198 "\"relationship-list\":{" +
199 "\"relationship\":[" +
201 "\"related-to\":\"operational-environment\"," +
202 "\"relationship-label\":\"managedBy\"," +
203 "\"related-link\":\"/aai/v12/cloud-infrastructure/operational-environments/operational-environment/OEid3\"," +
204 "\"relationship-data\":[" +
206 "\"relationship-key\":\"operational-environment.operational-environment-id\"," +
207 "\"relationship-value\":\"OEid3\"" +
210 "\"related-to-property\":[" +
212 "\"property-key\":\"operational-environment.operational-environment-name\"," +
213 "\"property-value\":\"OEname3\"" +
217 private OperationalEnvironmentEntry createOpEnvEntry(String name) {
218 OperationalEnvironmentEntry entry = new OperationalEnvironmentEntry();
219 entry.setEnvironmentId(name);
223 public void testHandleMessageLogic() throws Exception {
224 String notification = "";
228 result = envEngine.handleMessageLogic(notification);
232 public void testValidateNotification() throws Exception {
233 Wrapper<Boolean> errorWrapper = new Wrapper<>();
234 errorWrapper.setInnerElement(true);
235 IDmaapNotificationData notificationData = Mockito.mock(IDmaapNotificationData.class);
236 IDmaapAuditNotificationData auditNotificationData = Mockito.mock(IDmaapAuditNotificationData.class);
239 Deencapsulation.invoke(envEngine, "validateNotification", errorWrapper, notificationData,
240 auditNotificationData);
244 public void testSaveEntryWithFailedStatus() throws Exception {
245 Wrapper<Boolean> errorWrapper = new Wrapper<>();
246 OperationalEnvironmentEntry opEnvEntry = new OperationalEnvironmentEntry();
249 Deencapsulation.invoke(envEngine, "saveEntryWithFailedStatus", errorWrapper, opEnvEntry);
253 public void testRetrieveUebAddressesFromAftDme() throws Exception {
254 Wrapper<Boolean> errorWrapper = new Wrapper<>();
255 OperationalEnvironmentEntry opEnvEntry = new OperationalEnvironmentEntry();
258 Deencapsulation.invoke(envEngine, "retrieveUebAddressesFromAftDme", errorWrapper, opEnvEntry);
262 public void testRetrieveOpEnvInfoFromAAI() throws Exception {
263 Wrapper<Boolean> errorWrapper = new Wrapper<>();
264 OperationalEnvironmentEntry opEnvEntry = new OperationalEnvironmentEntry();
265 opEnvEntry.setEnvironmentId("mock");
266 Mockito.when(aaiRequestHandler.getOperationalEnvById(Mockito.nullable(String.class))).thenReturn(new HttpResponse<String>("{}", 200));
268 Deencapsulation.invoke(envEngine, "retrieveOpEnvInfoFromAAI", new Wrapper<>(), opEnvEntry);
272 public void testRetrieveOpEnvInfoFromAAIError() throws Exception {
273 Wrapper<Boolean> errorWrapper = new Wrapper<>();
274 OperationalEnvironmentEntry opEnvEntry = new OperationalEnvironmentEntry();
275 opEnvEntry.setEnvironmentId("mock");
276 Mockito.when(aaiRequestHandler.getOperationalEnvById(Mockito.nullable(String.class))).thenReturn(new HttpResponse<String>("{}", 500));
278 Deencapsulation.invoke(envEngine, "retrieveOpEnvInfoFromAAI", new Wrapper<>(), opEnvEntry);
282 public void testSaveEntryWithInProgressStatus() throws Exception {
283 Wrapper<Boolean> errorWrapper = new Wrapper<>();
284 Wrapper<OperationalEnvironmentEntry> opEnvEntryWrapper = new Wrapper<>();
285 IDmaapNotificationData notificationData = Mockito.mock(IDmaapNotificationData.class);
287 Deencapsulation.invoke(envEngine, "saveEntryWithInProgressStatus", errorWrapper, opEnvEntryWrapper,
292 public void testValidateStateGeneralError() throws Exception {
293 Wrapper<Boolean> errorWrapper = null;
294 IDmaapNotificationData notificationData = Mockito.mock(IDmaapNotificationData.class);
296 Mockito.when(operationalEnvironmentDao.get(Mockito.nullable(String.class)))
297 .thenReturn(Either.right(CassandraOperationStatus.GENERAL_ERROR));
299 Deencapsulation.invoke(envEngine, "validateState", new Wrapper<>(), notificationData);
303 public void testValidateState() throws Exception {
304 Wrapper<Boolean> errorWrapper = null;
305 IDmaapNotificationData notificationData = Mockito.mock(IDmaapNotificationData.class);
307 OperationalEnvironmentEntry a = new OperationalEnvironmentEntry();
308 a.setStatus(EnvironmentStatusEnum.IN_PROGRESS.getName());
309 Mockito.when(operationalEnvironmentDao.get(Mockito.nullable(String.class))).thenReturn(Either.left(a));
311 Deencapsulation.invoke(envEngine, "validateState", new Wrapper<>(), notificationData);
315 public void testValidateActionType() throws Exception {
316 Wrapper<Boolean> errorWrapper = new Wrapper<>();
317 IDmaapNotificationData notificationData = Mockito.mock(IDmaapNotificationData.class);
318 Mockito.when(notificationData.getAction()).thenReturn(IDmaapNotificationData.DmaapActionEnum.DELETE);
320 Deencapsulation.invoke(envEngine, "validateActionType", errorWrapper, notificationData);
324 public void testValidateActionType2() throws Exception {
325 Wrapper<Boolean> errorWrapper = new Wrapper<>();
326 IDmaapNotificationData notificationData = Mockito.mock(IDmaapNotificationData.class);
327 Mockito.when(notificationData.getAction()).thenReturn(IDmaapNotificationData.DmaapActionEnum.CREATE);
329 Deencapsulation.invoke(envEngine, "validateActionType", errorWrapper, notificationData);
333 public void testValidateEnvironmentType() throws Exception {
334 Wrapper<Boolean> errorWrapper = new Wrapper<>();
335 IDmaapNotificationData notificationData = Mockito.mock(IDmaapNotificationData.class);
336 IDmaapAuditNotificationData auditNotificationData = Mockito.mock(IDmaapAuditNotificationData.class);
337 Mockito.when(auditNotificationData.getOperationalEnvironmentName()).thenReturn("mock");
338 Mockito.when(notificationData.getOperationalEnvironmentType()).thenReturn(IDmaapNotificationData.OperationaEnvironmentTypeEnum.ECOMP);
341 Deencapsulation.invoke(envEngine, "validateEnvironmentType", errorWrapper, notificationData,
342 auditNotificationData);
346 public void testMap2OpEnvKey() throws Exception {
347 OperationalEnvironmentEntry entry = new OperationalEnvironmentEntry();
351 result = Deencapsulation.invoke(envEngine, "map2OpEnvKey", entry);