1 package org.openecomp.sdc.be.components.distribution.engine;
3 import com.att.nsa.apiClient.credentials.ApiCredential;
5 import mockit.Deencapsulation;
6 import org.apache.http.HttpStatus;
7 import org.junit.Before;
9 import org.mockito.InjectMocks;
10 import org.mockito.Mock;
11 import org.mockito.Mockito;
12 import org.mockito.MockitoAnnotations;
13 import org.openecomp.sdc.be.config.Configuration;
14 import org.openecomp.sdc.be.config.ConfigurationManager;
15 import org.openecomp.sdc.be.config.DistributionEngineConfiguration;
16 import org.openecomp.sdc.be.config.DmaapConsumerConfiguration;
17 import org.openecomp.sdc.be.dao.cassandra.CassandraOperationStatus;
18 import org.openecomp.sdc.be.dao.cassandra.OperationalEnvironmentDao;
19 import org.openecomp.sdc.be.datatypes.enums.EnvironmentStatusEnum;
20 import org.openecomp.sdc.be.info.OperationalEnvInfo;
21 import org.openecomp.sdc.be.resources.data.OperationalEnvironmentEntry;
22 import org.openecomp.sdc.common.datastructure.Wrapper;
23 import org.openecomp.sdc.common.http.client.api.HttpResponse;
25 import java.io.IOException;
27 import java.util.concurrent.atomic.AtomicBoolean;
29 import static org.junit.Assert.assertEquals;
30 import static org.junit.Assert.assertTrue;
31 import static org.mockito.Mockito.when;
33 public class EnvironmentsEngineTest {
36 private EnvironmentsEngine envEngine;
38 private OperationalEnvironmentDao operationalEnvironmentDao;
40 private ConfigurationManager configurationManager;
42 private DistributionEngineConfiguration distributionEngineConfiguration;
44 private AaiRequestHandler aaiRequestHandler;
47 public void preStart() {
48 MockitoAnnotations.initMocks(this);
52 public void testInit() {
53 envEngine.setConfigurationManager(configurationManager);
54 Configuration config = Mockito.mock(Configuration.class);
55 DmaapConsumerConfiguration dmaapConf = Mockito.mock(DmaapConsumerConfiguration.class);
56 List<OperationalEnvironmentEntry> entryList = Arrays.asList(createOpEnvEntry("Env1"), createOpEnvEntry("Env2"));
57 Either<List<OperationalEnvironmentEntry>, CassandraOperationStatus> successEither = Either.left(entryList);
58 when(operationalEnvironmentDao.getByEnvironmentsStatus(EnvironmentStatusEnum.COMPLETED)).thenReturn(successEither);
59 when(configurationManager.getDistributionEngineConfiguration()).thenReturn(distributionEngineConfiguration);
60 when(distributionEngineConfiguration.getEnvironments()).thenReturn(Arrays.asList("Env Loaded From Configuration"));
61 when(distributionEngineConfiguration.getUebPublicKey()).thenReturn("Dummy Public Key");
62 when(distributionEngineConfiguration.getUebSecretKey()).thenReturn("Dummy Private Key");
63 when(distributionEngineConfiguration.getUebServers()).thenReturn(
64 Arrays.asList("uebsb91kcdc.it.com:3904", "uebsb92kcdc.it.com:3904", "uebsb91kcdc.it.com:3904"));
65 when(configurationManager.getConfiguration()).thenReturn(config);
66 when(config.getDmaapConsumerConfiguration()).thenReturn(dmaapConf);
67 when(dmaapConf.isActive()).thenReturn(false);
70 Map<String, OperationalEnvironmentEntry> mapEnvs = envEngine.getEnvironments();
71 assertEquals("unexpected size of map",3, mapEnvs.size());
76 public void testGetFullOperationalEnvByIdSuccess() {
77 String json = getFullOperationalEnvJson();
79 HttpResponse restResponse = new HttpResponse(json, HttpStatus.SC_OK, "Successfully completed");
80 when(aaiRequestHandler.getOperationalEnvById(Mockito.anyString())).thenReturn(restResponse);
82 Either<OperationalEnvInfo, Integer> response = envEngine.getOperationalEnvById("DummyId");
83 assertTrue("The operational environment request ran as not expected", response.isLeft());
85 OperationalEnvInfo operationalEnvInfo = response.left().value();
87 assertEquals("The operational environment json is not as expected", operationalEnvInfo.toString(), json);
91 public void testGetPartialOperationalEnvByIdSuccess() {
92 String json = getPartialOperationalEnvJson();
94 HttpResponse<String> restResponse = new HttpResponse<String>(json, HttpStatus.SC_OK, "Successfully completed");
95 when(aaiRequestHandler.getOperationalEnvById(Mockito.anyString())).thenReturn(restResponse);
97 Either<OperationalEnvInfo, Integer> response = envEngine.getOperationalEnvById("DummyId");
98 assertTrue("The operational environment request ran as not expected", response.isLeft());
100 OperationalEnvInfo operationalEnvInfo = response.left().value();
102 assertEquals("The operational environment json is not as expected", operationalEnvInfo.toString(), json);
107 public void testGetOperationalEnvByIdFailedByJsonConvert() {
108 String jsonCorrupted = getCorruptedOperationalEnvJson();
110 HttpResponse<String> restResponse = new HttpResponse<String>(jsonCorrupted, HttpStatus.SC_OK, "Successfully Completed");
111 when(aaiRequestHandler.getOperationalEnvById(Mockito.anyString())).thenReturn(restResponse);
113 Either<OperationalEnvInfo, Integer> response = envEngine.getOperationalEnvById("DummyId");
114 assertTrue("The operational environment request ran as not expected", response.isRight());
115 assertEquals("The operational environment request status code is not as expected", (Integer)HttpStatus.SC_INTERNAL_SERVER_ERROR, response.right().value());
119 public void testGetOperationalEnvByIdFailed404() {
120 String json = getFullOperationalEnvJson();
121 HttpResponse<String> restResponse = new HttpResponse<String>(json, HttpStatus.SC_NOT_FOUND, "Not Found");
122 when(aaiRequestHandler.getOperationalEnvById(Mockito.anyString())).thenReturn(restResponse);
124 Either<OperationalEnvInfo, Integer> response = envEngine.getOperationalEnvById("DummyId");
125 assertTrue("The operational environment request ran as not expected", response.isRight());
126 assertEquals("The operational environment request status code is not as expected", (Integer)HttpStatus.SC_NOT_FOUND, response.right().value());
130 @Test(expected = IOException.class)
131 public void testCorruptedOperationalEnvJson() throws IOException {
132 String jsonCorrupted = getCorruptedOperationalEnvJson();
133 OperationalEnvInfo.createFromJson(jsonCorrupted);
137 public void getEnvironmentById() {
138 OperationalEnvironmentEntry oe = new OperationalEnvironmentEntry();
139 oe.setEnvironmentId("mock");
140 envEngine.addToMap(oe);
141 assertTrue(envEngine.isInMap("mock"));
142 assertTrue(envEngine.isInMap(oe));
143 OperationalEnvironmentEntry returnedOe = envEngine.getEnvironmentById("mock");
144 assertTrue(oe == returnedOe);
147 private String getCorruptedOperationalEnvJson() {
148 return "{\"OPERATIONAL-environment-name\":\"Op Env Name\","
149 + "\"OPERATIONAL-environment-type\":\"VNF\","
150 + "\"OPERATIONAL-environment-status\":\"Activate\","
151 + "\"tenant-context\":\"Test\"}";
154 private String getPartialOperationalEnvJson() {
156 "\"operational-environment-id\":\"UUID of Operational Environment\"," +
157 "\"operational-environment-name\":\"Op Env Name\"," +
158 "\"operational-environment-type\":\"VNF\"," +
159 "\"operational-environment-status\":\"Activate\"," +
160 "\"tenant-context\":\"Test\"," +
161 "\"workload-context\":\"VNF_Development\"," +
162 "\"resource-version\":\"1505228226913\"," +
163 "\"relationship-list\":{" +
164 "\"relationship\":[]" +
169 private String getFullOperationalEnvJson() {
171 "\"operational-environment-id\":\"OEid1\"," +
172 "\"operational-environment-name\":\"OEname1\"," +
173 "\"operational-environment-type\":\"OEtype1\"," +
174 "\"operational-environment-status\":\"OEstatus1\"," +
175 "\"tenant-context\":\"OEtenantcontext1\"," +
176 "\"workload-context\":\"OEworkloadcontext1\"," +
177 "\"resource-version\":\"1511363173278\"," +
178 "\"relationship-list\":{" +
179 "\"relationship\":[" +
181 "\"related-to\":\"operational-environment\"," +
182 "\"relationship-label\":\"managedBy\"," +
183 "\"related-link\":\"/aai/v12/cloud-infrastructure/operational-environments/operational-environment/OEid3\"," +
184 "\"relationship-data\":[" +
186 "\"relationship-key\":\"operational-environment.operational-environment-id\"," +
187 "\"relationship-value\":\"OEid3\"" +
190 "\"related-to-property\":[" +
192 "\"property-key\":\"operational-environment.operational-environment-name\"," +
193 "\"property-value\":\"OEname3\"" +
197 private OperationalEnvironmentEntry createOpEnvEntry(String name) {
198 OperationalEnvironmentEntry entry = new OperationalEnvironmentEntry();
199 entry.setEnvironmentId(name);
203 public void testHandleMessageLogic() throws Exception {
204 String notification = "";
208 result = envEngine.handleMessageLogic(notification);
212 public void testValidateNotification() throws Exception {
213 Wrapper<Boolean> errorWrapper = new Wrapper<>();
214 errorWrapper.setInnerElement(true);
215 IDmaapNotificationData notificationData = Mockito.mock(IDmaapNotificationData.class);
216 IDmaapAuditNotificationData auditNotificationData = Mockito.mock(IDmaapAuditNotificationData.class);
219 Deencapsulation.invoke(envEngine, "validateNotification", errorWrapper, notificationData,
220 auditNotificationData);
224 public void testSaveEntryWithFailedStatus() throws Exception {
225 Wrapper<Boolean> errorWrapper = new Wrapper<>();
226 OperationalEnvironmentEntry opEnvEntry = new OperationalEnvironmentEntry();
229 Deencapsulation.invoke(envEngine, "saveEntryWithFailedStatus", errorWrapper, opEnvEntry);
233 public void testRetrieveUebAddressesFromAftDme() throws Exception {
234 Wrapper<Boolean> errorWrapper = new Wrapper<>();
235 OperationalEnvironmentEntry opEnvEntry = new OperationalEnvironmentEntry();
238 Deencapsulation.invoke(envEngine, "retrieveUebAddressesFromAftDme", errorWrapper, opEnvEntry);
242 public void testRetrieveOpEnvInfoFromAAI() throws Exception {
243 Wrapper<Boolean> errorWrapper = new Wrapper<>();
244 OperationalEnvironmentEntry opEnvEntry = new OperationalEnvironmentEntry();
245 opEnvEntry.setEnvironmentId("mock");
246 Mockito.when(aaiRequestHandler.getOperationalEnvById(Mockito.nullable(String.class))).thenReturn(new HttpResponse<String>("{}", 200));
248 Deencapsulation.invoke(envEngine, "retrieveOpEnvInfoFromAAI", new Wrapper<>(), opEnvEntry);
252 public void testRetrieveOpEnvInfoFromAAIError() throws Exception {
253 Wrapper<Boolean> errorWrapper = new Wrapper<>();
254 OperationalEnvironmentEntry opEnvEntry = new OperationalEnvironmentEntry();
255 opEnvEntry.setEnvironmentId("mock");
256 Mockito.when(aaiRequestHandler.getOperationalEnvById(Mockito.nullable(String.class))).thenReturn(new HttpResponse<String>("{}", 500));
258 Deencapsulation.invoke(envEngine, "retrieveOpEnvInfoFromAAI", new Wrapper<>(), opEnvEntry);
262 public void testSaveEntryWithInProgressStatus() throws Exception {
263 Wrapper<Boolean> errorWrapper = new Wrapper<>();
264 Wrapper<OperationalEnvironmentEntry> opEnvEntryWrapper = new Wrapper<>();
265 IDmaapNotificationData notificationData = Mockito.mock(IDmaapNotificationData.class);
267 Deencapsulation.invoke(envEngine, "saveEntryWithInProgressStatus", errorWrapper, opEnvEntryWrapper,
272 public void testValidateStateGeneralError() throws Exception {
273 Wrapper<Boolean> errorWrapper = null;
274 IDmaapNotificationData notificationData = Mockito.mock(IDmaapNotificationData.class);
276 Mockito.when(operationalEnvironmentDao.get(Mockito.nullable(String.class)))
277 .thenReturn(Either.right(CassandraOperationStatus.GENERAL_ERROR));
279 Deencapsulation.invoke(envEngine, "validateState", new Wrapper<>(), notificationData);
283 public void testValidateState() throws Exception {
284 Wrapper<Boolean> errorWrapper = null;
285 IDmaapNotificationData notificationData = Mockito.mock(IDmaapNotificationData.class);
287 OperationalEnvironmentEntry a = new OperationalEnvironmentEntry();
288 a.setStatus(EnvironmentStatusEnum.IN_PROGRESS.getName());
289 Mockito.when(operationalEnvironmentDao.get(Mockito.nullable(String.class))).thenReturn(Either.left(a));
291 Deencapsulation.invoke(envEngine, "validateState", new Wrapper<>(), notificationData);
295 public void testValidateActionType() throws Exception {
296 Wrapper<Boolean> errorWrapper = new Wrapper<>();
297 IDmaapNotificationData notificationData = Mockito.mock(IDmaapNotificationData.class);
298 Mockito.when(notificationData.getAction()).thenReturn(IDmaapNotificationData.DmaapActionEnum.DELETE);
300 Deencapsulation.invoke(envEngine, "validateActionType", errorWrapper, notificationData);
304 public void testValidateActionType2() throws Exception {
305 Wrapper<Boolean> errorWrapper = new Wrapper<>();
306 IDmaapNotificationData notificationData = Mockito.mock(IDmaapNotificationData.class);
307 Mockito.when(notificationData.getAction()).thenReturn(IDmaapNotificationData.DmaapActionEnum.CREATE);
309 Deencapsulation.invoke(envEngine, "validateActionType", errorWrapper, notificationData);
313 public void testValidateEnvironmentType() throws Exception {
314 Wrapper<Boolean> errorWrapper = new Wrapper<>();
315 IDmaapNotificationData notificationData = Mockito.mock(IDmaapNotificationData.class);
316 IDmaapAuditNotificationData auditNotificationData = Mockito.mock(IDmaapAuditNotificationData.class);
317 Mockito.when(auditNotificationData.getOperationalEnvironmentName()).thenReturn("mock");
318 Mockito.when(notificationData.getOperationalEnvironmentType()).thenReturn(IDmaapNotificationData.OperationaEnvironmentTypeEnum.ECOMP);
321 Deencapsulation.invoke(envEngine, "validateEnvironmentType", errorWrapper, notificationData,
322 auditNotificationData);
326 public void testMap2OpEnvKey() throws Exception {
327 OperationalEnvironmentEntry entry = new OperationalEnvironmentEntry();
331 result = Deencapsulation.invoke(envEngine, "map2OpEnvKey", entry);