1 package org.openecomp.sdc.be.components.distribution.engine;
3 import com.att.aft.dme2.api.DME2Exception;
4 import com.att.aft.dme2.iterator.DME2EndpointIterator;
5 import com.att.nsa.apiClient.credentials.ApiCredential;
6 import com.google.gson.Gson;
7 import com.google.gson.GsonBuilder;
8 import cucumber.api.java.Before;
9 import cucumber.api.java.en.Given;
10 import cucumber.api.java.en.Then;
11 import cucumber.api.java.en.When;
12 import fj.data.Either;
13 import org.apache.commons.lang.NotImplementedException;
14 import org.apache.commons.lang.math.NumberUtils;
15 import org.apache.http.HttpStatus;
16 import org.junit.Assert;
18 import org.openecomp.sdc.be.dao.cassandra.CassandraOperationStatus;
19 import org.openecomp.sdc.be.dao.cassandra.OperationalEnvironmentDao;
20 import org.openecomp.sdc.be.datatypes.enums.EnvironmentStatusEnum;
21 import org.openecomp.sdc.be.impl.ComponentsUtils;
22 import org.openecomp.sdc.be.resources.data.OperationalEnvironmentEntry;
23 import org.openecomp.sdc.be.resources.data.auditing.AuditingActionEnum;
24 import org.openecomp.sdc.common.datastructure.Wrapper;
25 import org.openecomp.sdc.common.http.client.api.HttpResponse;
27 import static java.util.Objects.isNull;
28 import static org.apache.commons.lang3.StringUtils.isEmpty;
29 import static org.mockito.Mockito.*;
31 public class StepsTenantIsolation {
33 // Notification Fields
34 private String operationalEnvironmentId = "28122015552391";
35 private String operationalEnvironmentName = "Operational Environment Name";
36 private String operationalEnvironmentType;
37 private String tenantContext ;
38 private String workloadContext;
39 private String action;
42 private DmaapConsumer dmaapConsumer;
44 private OperationalEnvironmentDao operationalEnvironmentDao;
46 private DME2EndpointIteratorCreator epIterCreator;
48 private ComponentsUtils componentsUtils;
50 private AaiRequestHandler aaiRequestHandler;
52 private CambriaHandler cambriaHandler;
55 private EnvironmentsEngine envEngine;
57 private boolean isSuccessful;
58 private boolean cassandraUp;
61 public void beforeScenario() {
62 MockitoAnnotations.initMocks(this);
63 when(operationalEnvironmentDao.getByEnvironmentsStatus(EnvironmentStatusEnum.COMPLETED))
64 .thenReturn(Either.right(CassandraOperationStatus.NOT_FOUND));
65 doNothing().when(envEngine).createUebTopicsForEnvironments();
69 // ############################# Given - Start #############################
70 @Given("^Dmaap consumer recieved notification with fields (.*)$")
71 public void dmaap_consumer_recieved_notification_with_fields(String notificationFields) throws Throwable {
72 Gson gson = new GsonBuilder().create();
73 IDmaapNotificationData notification = gson.fromJson(notificationFields, DmaapNotificationDataImpl.class);
74 if (!isNull(notification.getOperationalEnvironmentType())) {
75 this.operationalEnvironmentType = notification.getOperationalEnvironmentType().getEventTypenName();
77 if( !isEmpty(notification.getOperationalEnvironmentId()) ){
78 this.operationalEnvironmentId = notification.getOperationalEnvironmentId();
80 if( !isNull(notification.getAction()) ){
81 this.action = notification.getAction().getActionName();
86 @Given("^Cassandra service status is (.*)$")
87 public void cassandra_service_status_is(String status) throws Throwable {
90 this.cassandraUp = true;
93 when(operationalEnvironmentDao.get(operationalEnvironmentId))
94 .thenReturn(Either.right(CassandraOperationStatus.GENERAL_ERROR));
95 when(operationalEnvironmentDao.save(Mockito.any(OperationalEnvironmentEntry.class)))
96 .thenReturn(CassandraOperationStatus.GENERAL_ERROR);
99 throw new NotImplementedException();
103 @Given("^Record status is (.*)$")
104 public void record_status_is(String status) throws Throwable {
108 Either<OperationalEnvironmentEntry, CassandraOperationStatus> eitherResult;
109 final OperationalEnvironmentEntry entryMock = Mockito.mock(OperationalEnvironmentEntry.class);
111 case "FOUND_IN_PROGRESS":
112 when(entryMock.getStatus()).thenReturn(EnvironmentStatusEnum.IN_PROGRESS.getName());
113 eitherResult = Either.left(entryMock);
115 case "FOUND_COMPLETED":
116 when(entryMock.getStatus()).thenReturn(EnvironmentStatusEnum.COMPLETED.getName());
117 eitherResult = Either.left(entryMock);
120 when(entryMock.getStatus()).thenReturn(EnvironmentStatusEnum.FAILED.getName());
121 eitherResult = Either.left(entryMock);
124 eitherResult = Either.right(CassandraOperationStatus.NOT_FOUND);
127 throw new NotImplementedException();
130 when(operationalEnvironmentDao.get(operationalEnvironmentId)).thenReturn(eitherResult);
131 when(operationalEnvironmentDao.save(Mockito.any(OperationalEnvironmentEntry.class)))
132 .thenReturn(CassandraOperationStatus.OK);
135 @Given("^AAI service status is (.*) and Tenant returned is (.*) and worload returned is (.*)$")
136 public void aai_service_status_is(String aaiServiceStatus, String tenant, String workload) throws Throwable {
137 this.tenantContext = tenant;
138 this.workloadContext = workload;
139 HttpResponse<String> resp = Mockito.mock(HttpResponse.class);
140 when(aaiRequestHandler.getOperationalEnvById(operationalEnvironmentId)).thenReturn(resp);
141 switch (aaiServiceStatus) {
143 when(resp.getStatusCode()).thenReturn(HttpStatus.SC_OK);
144 String aaiResponseTemplate =
147 + " \"operational-environment-id\": \"%s\",\r\n"
148 + " \"operational-environment-name\": \"%s\",\r\n"
149 + " \"operational-environment-type\": \"%s\",\r\n"
150 + " \"operational-environment-status\": \"IN-PROGRESS\",\r\n"
151 + " \"tenant-context\": \"%s\",\r\n"
152 + " \"workload-context\": \"%s\"\r\n"
155 when(resp.getResponse()).thenReturn(String.format(aaiResponseTemplate, operationalEnvironmentId,
156 operationalEnvironmentName, operationalEnvironmentType, tenantContext, workloadContext));
160 when(resp.getStatusCode()).thenReturn(HttpStatus.SC_REQUEST_TIMEOUT);
163 throw new NotImplementedException();
167 @Given("^AFT_DME service status is (.*)$")
168 public void aft_dme_service_status_is(String aftDmeStatus) throws Throwable {
169 switch (aftDmeStatus) {
171 DME2EndpointIterator mockItr = Mockito.mock(DME2EndpointIterator.class);
172 when(mockItr.hasNext()).thenReturn(false);
173 when(epIterCreator.create(Mockito.anyString())).thenReturn(mockItr);
176 when(epIterCreator.create(Mockito.anyString()))
177 .thenThrow(new DME2Exception("dummyCode", new NotImplementedException()));
180 throw new NotImplementedException();
184 @SuppressWarnings("unchecked")
185 @Given("^UEB service status is (.*)$")
186 public void ueb_service_status_is(String status) throws Throwable {
188 Either<ApiCredential, CambriaErrorResponse> response;
191 ApiCredential apiCredential = Mockito.mock(ApiCredential.class);
192 when(apiCredential.getApiKey()).thenReturn("MockAPIKey");
193 when(apiCredential.getApiSecret()).thenReturn("MockSecretKey");
194 response = Either.left(apiCredential);
197 CambriaErrorResponse cambriaError = Mockito.mock(CambriaErrorResponse.class);
198 response = Either.right(cambriaError);
201 throw new NotImplementedException();
203 when(cambriaHandler.createUebKeys(Mockito.anyList())).thenReturn(response);
205 // ############################# Given - End #############################
207 // ############################# When - Start #############################
209 @When("^handle message is activated$")
210 public void handle_message_is_activated() throws Throwable {
211 this.isSuccessful = envEngine.handleMessage(buildNotification());
213 // ############################# When - End #############################
215 // ############################# Then - Start #############################
216 @SuppressWarnings("unchecked")
217 @Then("^handle message activates validation of eventType (.*)$")
218 public void handle_message_activates_validation_of_eventType(boolean isValidated) throws Throwable {
219 verify(envEngine, Mockito.times(getNumberOfCallsToValidate(isValidated)))
220 .validateEnvironmentType(Mockito.any(Wrapper.class), Mockito.any(IDmaapNotificationData.class),
221 Mockito.any(IDmaapAuditNotificationData.class));
224 @SuppressWarnings("unchecked")
225 @Then("^trying to write message to audit log and table (.*)$")
226 public void trying_to_write_message_to_audit_log_and_table(boolean isUnsupportedTypeEventRecorded) throws Throwable {
227 int count = isUnsupportedTypeEventRecorded ? 2 : 1;
228 verify(componentsUtils, Mockito.atLeast(count))
229 .auditEnvironmentEngine(Mockito.any(AuditingActionEnum.class), Mockito.eq(operationalEnvironmentId),
230 Mockito.any(String.class), Mockito.any(String.class), Mockito.eq(operationalEnvironmentName), Mockito.eq(tenantContext));
233 @SuppressWarnings("unchecked")
234 @Then("^handle message activates validation of action (.*)$")
235 public void handle_message_activates_validation_of_action(boolean isValidated) throws Throwable {
236 verify(envEngine, Mockito.times(getNumberOfCallsToValidate(isValidated)))
237 .validateActionType(Mockito.any(Wrapper.class), Mockito.any(IDmaapNotificationData.class));
240 @SuppressWarnings("unchecked")
241 @Then("^handle message activates validation of state (.*)$")
242 public void handle_message_activates_validation_of_state(boolean isValidated) throws Throwable {
243 verify(envEngine, Mockito.times(getNumberOfCallsToValidate(isValidated)))
244 .validateState(Mockito.any(Wrapper.class), Mockito.any(IDmaapNotificationData.class));
247 @SuppressWarnings("unchecked")
248 @Then("^trying to save in-progress record (.*)$")
249 public void trying_to_save_in_progress_record(boolean isActivated) throws Throwable {
250 verify(envEngine, Mockito.times(getNumberOfCallsToValidate(isActivated)))
251 .saveEntryWithInProgressStatus(Mockito.any(Wrapper.class), Mockito.any(Wrapper.class), Mockito.any(IDmaapNotificationData.class));
254 @SuppressWarnings("unchecked")
255 @Then("^trying to get environment info from A&AI API (.*)$")
256 public void trying_to_get_environment_info_from_AAI_AP(boolean isActivated) throws Throwable {
257 verify(envEngine, Mockito.times(getNumberOfCallsToValidate(isActivated)))
258 .retrieveOpEnvInfoFromAAI(Mockito.any(Wrapper.class), Mockito.any(OperationalEnvironmentEntry.class));
261 @SuppressWarnings("unchecked")
262 @Then("^trying to retrieve Ueb Addresses From AftDme (.*)$")
263 public void trying_to_retrieve_ueb_addresses_from_AftDme(boolean isActivated) throws Throwable {
264 verify(envEngine, Mockito.times(getNumberOfCallsToValidate(isActivated))).discoverUebHosts(
265 Mockito.anyString(), Mockito.anyString());
269 @SuppressWarnings("unchecked")
270 @Then("^trying to create Ueb keys (.*)$")
271 public void trying_to_create_ueb_keys(boolean isActivated) throws Throwable {
272 verify(envEngine, Mockito.times(getNumberOfCallsToValidate(isActivated)))
273 .createUebKeys(Mockito.any(Wrapper.class), Mockito.any(OperationalEnvironmentEntry.class));
276 @Then("^trying to create Ueb Topics (.*)$")
277 public void trying_to_create_ueb_topics(boolean isActivated) throws Throwable {
278 verify(envEngine, Mockito.times(getNumberOfCallsToValidate(isActivated)))
279 .createUebTopicsForEnvironment(Mockito.any(OperationalEnvironmentEntry.class));
282 @Then("^handle message finished successfully (.*)$")
283 public void handle_message_finished_successfully(boolean isSuccessfull) throws Throwable {
284 Assert.assertEquals(this.isSuccessful, isSuccessfull);
287 // ############################# Then - End #############################
289 private String buildNotification() {
290 String notificationTemplate = "{ \"operationalEnvironmentId\": \"%s\",\r\n"
291 + " \"operationalEnvironmentName\": \"%s\",\r\n"
292 + " \"operationalEnvironmentType\": \"%s\",\r\n" + " \"tenantContext\": \"%s\",\r\n"
293 + " \"workloadContext\": \"%s\",\r\n" + " \"action\": \"%s\"}";
295 return String.format(notificationTemplate, operationalEnvironmentId, operationalEnvironmentName,
296 operationalEnvironmentType, tenantContext, workloadContext, action);
299 private int getNumberOfCallsToValidate(boolean isValidated) {
300 return isValidated ? NumberUtils.INTEGER_ONE : NumberUtils.INTEGER_ZERO;