1 package org.openecomp.sdc.be.components.impl;
3 import org.junit.Before;
4 import org.junit.BeforeClass;
6 import org.junit.runner.RunWith;
7 import org.mockito.ArgumentCaptor;
8 import org.mockito.Captor;
9 import org.mockito.InjectMocks;
10 import org.mockito.Mock;
11 import org.mockito.junit.MockitoJUnitRunner;
12 import org.openecomp.sdc.be.auditing.api.AuditEventFactory;
13 import org.openecomp.sdc.be.auditing.impl.AuditBaseEventFactory;
14 import org.openecomp.sdc.be.auditing.impl.AuditingManager;
15 import org.openecomp.sdc.be.datatypes.enums.ComponentTypeEnum;
16 import org.openecomp.sdc.be.datatypes.enums.ResourceTypeEnum;
17 import org.openecomp.sdc.be.impl.ComponentsUtils;
18 import org.openecomp.sdc.be.model.Component;
19 import org.openecomp.sdc.be.model.LifecycleStateEnum;
20 import org.openecomp.sdc.be.model.Resource;
21 import org.openecomp.sdc.be.model.Service;
22 import org.openecomp.sdc.be.model.User;
23 import org.openecomp.sdc.be.resources.data.auditing.AuditingActionEnum;
24 import org.openecomp.sdc.be.resources.data.auditing.DistributionDownloadEvent;
25 import org.openecomp.sdc.be.resources.data.auditing.EcompOperationalEnvironmentEvent;
26 import org.openecomp.sdc.be.resources.data.auditing.ResourceAdminEvent;
27 import org.openecomp.sdc.be.resources.data.auditing.UserAdminEvent;
28 import org.openecomp.sdc.be.resources.data.auditing.model.DistributionData;
29 import org.openecomp.sdc.be.resources.data.auditing.model.ResourceAuditData;
30 import org.openecomp.sdc.common.util.ThreadLocalsHolder;
31 import org.openecomp.sdc.exception.ResponseFormat;
33 import static org.assertj.core.api.Assertions.assertThat;
34 import static org.mockito.Mockito.verify;
35 import static org.mockito.Mockito.when;
36 import static org.openecomp.sdc.be.auditing.impl.AuditTestUtils.ARTIFACT_DATA;
37 import static org.openecomp.sdc.be.auditing.impl.AuditTestUtils.ARTIFACT_UUID;
38 import static org.openecomp.sdc.be.auditing.impl.AuditTestUtils.COMMENT;
39 import static org.openecomp.sdc.be.auditing.impl.AuditTestUtils.CURRENT_STATE;
40 import static org.openecomp.sdc.be.auditing.impl.AuditTestUtils.CURRENT_VERSION;
41 import static org.openecomp.sdc.be.auditing.impl.AuditTestUtils.DCURR_STATUS;
42 import static org.openecomp.sdc.be.auditing.impl.AuditTestUtils.DESCRIPTION;
43 import static org.openecomp.sdc.be.auditing.impl.AuditTestUtils.DESC_ERROR;
44 import static org.openecomp.sdc.be.auditing.impl.AuditTestUtils.DIST_CONSUMER_ID;
45 import static org.openecomp.sdc.be.auditing.impl.AuditTestUtils.DIST_ID;
46 import static org.openecomp.sdc.be.auditing.impl.AuditTestUtils.DIST_RESOURCE_URL;
47 import static org.openecomp.sdc.be.auditing.impl.AuditTestUtils.DPREV_STATUS;
48 import static org.openecomp.sdc.be.auditing.impl.AuditTestUtils.INVARIANT_UUID;
49 import static org.openecomp.sdc.be.auditing.impl.AuditTestUtils.MODIFIER_FIRST_NAME;
50 import static org.openecomp.sdc.be.auditing.impl.AuditTestUtils.MODIFIER_ID;
51 import static org.openecomp.sdc.be.auditing.impl.AuditTestUtils.MODIFIER_LAST_NAME;
52 import static org.openecomp.sdc.be.auditing.impl.AuditTestUtils.MODIFIER_UID;
53 import static org.openecomp.sdc.be.auditing.impl.AuditTestUtils.OP_ENV_ACTION;
54 import static org.openecomp.sdc.be.auditing.impl.AuditTestUtils.OP_ENV_ID;
55 import static org.openecomp.sdc.be.auditing.impl.AuditTestUtils.OP_ENV_NAME;
56 import static org.openecomp.sdc.be.auditing.impl.AuditTestUtils.OP_ENV_TYPE;
57 import static org.openecomp.sdc.be.auditing.impl.AuditTestUtils.PREV_RESOURCE_STATE;
58 import static org.openecomp.sdc.be.auditing.impl.AuditTestUtils.PREV_RESOURCE_VERSION;
59 import static org.openecomp.sdc.be.auditing.impl.AuditTestUtils.REQUEST_ID;
60 import static org.openecomp.sdc.be.auditing.impl.AuditTestUtils.RESOURCE_NAME;
61 import static org.openecomp.sdc.be.auditing.impl.AuditTestUtils.SERVICE_INSTANCE_ID;
62 import static org.openecomp.sdc.be.auditing.impl.AuditTestUtils.STATUS_500;
63 import static org.openecomp.sdc.be.auditing.impl.AuditTestUtils.STATUS_OK;
64 import static org.openecomp.sdc.be.auditing.impl.AuditTestUtils.TENANT_CONTEXT;
65 import static org.openecomp.sdc.be.auditing.impl.AuditTestUtils.TESTER_USER_ROLE;
66 import static org.openecomp.sdc.be.auditing.impl.AuditTestUtils.TOSCA_NODE_TYPE;
67 import static org.openecomp.sdc.be.auditing.impl.AuditTestUtils.UPDATED_USER_EXTENDED_NAME;
68 import static org.openecomp.sdc.be.auditing.impl.AuditTestUtils.USER_EMAIL;
69 import static org.openecomp.sdc.be.auditing.impl.AuditTestUtils.USER_FIRST_NAME;
70 import static org.openecomp.sdc.be.auditing.impl.AuditTestUtils.USER_ID;
71 import static org.openecomp.sdc.be.auditing.impl.AuditTestUtils.USER_LAST_NAME;
73 @RunWith(MockitoJUnitRunner.class)
74 public class ComponentsUtilsTest {
76 private static User modifier = new User();
77 private Component service = new Service();
78 private Resource resource = new Resource();
81 private AuditingManager manager;
84 private ResponseFormat responseFormat;
87 private ArgumentCaptor<AuditBaseEventFactory> factoryCaptor;
90 private static ComponentsUtils utils;
93 public static void setUpClass() {
94 modifier.setFirstName(MODIFIER_FIRST_NAME);
95 modifier.setLastName(MODIFIER_LAST_NAME);
96 modifier.setUserId(MODIFIER_ID);
101 public void setUp() {
102 ThreadLocalsHolder.setUuid(REQUEST_ID);
103 utils = new ComponentsUtils(manager);
107 public void auditComponentWhenAllParamsPassed() {
108 when(responseFormat.getStatus()).thenReturn(Integer.valueOf(STATUS_OK));
109 when(responseFormat.getFormattedMessage()).thenReturn(DESCRIPTION);
110 service.setUUID(SERVICE_INSTANCE_ID);
111 service.setInvariantUUID(INVARIANT_UUID);
113 utils.auditComponent(responseFormat, modifier, service, AuditingActionEnum.ARTIFACT_DELETE, service.getComponentType(),
114 ResourceAuditData.newBuilder()
115 .state(PREV_RESOURCE_STATE)
116 .version(PREV_RESOURCE_VERSION)
117 .artifactUuid(ARTIFACT_UUID)
118 .distributionStatus(DPREV_STATUS).build(),
119 ResourceAuditData.newBuilder()
120 .state(CURRENT_STATE)
121 .version(CURRENT_VERSION)
122 .artifactUuid(ARTIFACT_UUID)
123 .distributionStatus(DCURR_STATUS).build(),
124 RESOURCE_NAME, COMMENT, ARTIFACT_DATA, DIST_ID);
125 verifyResourceAdminEvent(AuditingActionEnum.ARTIFACT_DELETE.getName(), STATUS_OK, DESCRIPTION, service.getComponentType().getValue(),
126 false, true, true, true, true, true);
130 public void auditComponentWhenAllParamsPassedAndMostFromComponent() {
131 when(responseFormat.getStatus()).thenReturn(Integer.valueOf(STATUS_OK));
132 when(responseFormat.getFormattedMessage()).thenReturn(DESCRIPTION);
134 service.setUUID(SERVICE_INSTANCE_ID);
135 service.setInvariantUUID(INVARIANT_UUID);
136 service.setState(LifecycleStateEnum.CERTIFIED);
137 service.setVersion(CURRENT_VERSION);
138 utils.auditComponent(responseFormat, modifier, service, AuditingActionEnum.CREATE_RESOURCE, service.getComponentType(),
139 ResourceAuditData.newBuilder()
140 .state(PREV_RESOURCE_STATE)
141 .version(PREV_RESOURCE_VERSION)
142 .artifactUuid(ARTIFACT_UUID)
143 .distributionStatus(DPREV_STATUS).build(),
144 ResourceAuditData.newBuilder()
145 .artifactUuid(ARTIFACT_UUID)
146 .distributionStatus(DCURR_STATUS).build(),
147 RESOURCE_NAME, COMMENT, ARTIFACT_DATA, DIST_ID);
148 verifyResourceAdminEvent(AuditingActionEnum.CREATE_RESOURCE.getName(), STATUS_OK, DESCRIPTION, service.getComponentType().getValue(),
149 false, true, true, true, true, true);
154 public void auditComponentDistStateWithoutArtDataCommentAndDid() {
155 when(responseFormat.getStatus()).thenReturn(Integer.valueOf(STATUS_OK));
156 when(responseFormat.getFormattedMessage()).thenReturn(DESCRIPTION);
158 service.setUUID(SERVICE_INSTANCE_ID);
159 service.setInvariantUUID(INVARIANT_UUID);
160 service.setState(LifecycleStateEnum.CERTIFIED);
161 service.setName(RESOURCE_NAME);
162 service.setVersion(CURRENT_VERSION);
163 utils.auditComponent(responseFormat, modifier, service, AuditingActionEnum.DISTRIBUTION_STATE_CHANGE_REQUEST, service.getComponentType(),
164 ResourceAuditData.newBuilder()
165 .state(PREV_RESOURCE_STATE)
166 .version(PREV_RESOURCE_VERSION)
167 .artifactUuid(ARTIFACT_UUID)
168 .distributionStatus(DPREV_STATUS).build(),
169 ResourceAuditData.newBuilder()
170 .artifactUuid(ARTIFACT_UUID)
171 .distributionStatus(DCURR_STATUS).build());
172 verifyResourceAdminEvent(AuditingActionEnum.DISTRIBUTION_STATE_CHANGE_REQUEST.getName(),
173 STATUS_OK, DESCRIPTION, service.getComponentType().getValue(),
174 false, true, true, false, false, true);
178 public void auditComponentWhenError() {
179 when(responseFormat.getStatus()).thenReturn(Integer.valueOf(STATUS_500));
180 when(responseFormat.getFormattedMessage()).thenReturn(DESC_ERROR);
182 service.setUUID(SERVICE_INSTANCE_ID);
183 service.setInvariantUUID(INVARIANT_UUID);
184 service.setState(LifecycleStateEnum.CERTIFIED);
185 service.setName(RESOURCE_NAME);
186 service.setVersion(CURRENT_VERSION);
188 utils.auditComponent(responseFormat, modifier, service, AuditingActionEnum.CERTIFICATION_REQUEST_RESOURCE,
189 ComponentTypeEnum.SERVICE,
190 ResourceAuditData.newBuilder().state(PREV_RESOURCE_STATE).version(PREV_RESOURCE_VERSION).artifactUuid(ARTIFACT_UUID).build());
191 verifyResourceAdminEvent(AuditingActionEnum.CERTIFICATION_REQUEST_RESOURCE.getName(), STATUS_500, DESC_ERROR,
192 service.getComponentType().getValue(), false, true, false, false, false, false);
196 public void auditComponentWhenCompIsNull() {
197 when(responseFormat.getStatus()).thenReturn(Integer.valueOf(STATUS_500));
198 when(responseFormat.getFormattedMessage()).thenReturn(DESC_ERROR);
200 utils.auditComponent(responseFormat, modifier, AuditingActionEnum.START_CERTIFICATION_RESOURCE,
201 RESOURCE_NAME, ComponentTypeEnum.SERVICE, COMMENT);
202 verifyResourceAdminEvent(AuditingActionEnum.START_CERTIFICATION_RESOURCE.getName(), STATUS_500, DESC_ERROR, service.getComponentType().getValue(),
203 true, false, false, false, true, false);
207 public void auditComponentAdmin() {
208 when(responseFormat.getStatus()).thenReturn(Integer.valueOf(STATUS_500));
209 when(responseFormat.getFormattedMessage()).thenReturn(DESC_ERROR);
211 service.setUUID(SERVICE_INSTANCE_ID);
212 service.setInvariantUUID(INVARIANT_UUID);
213 service.setName(RESOURCE_NAME);
214 service.setState(LifecycleStateEnum.CERTIFIED);
215 service.setVersion(CURRENT_VERSION);
216 utils.auditComponentAdmin(responseFormat, modifier, service, AuditingActionEnum.CREATE_RESOURCE, service.getComponentType());
217 verifyResourceAdminEvent(AuditingActionEnum.CREATE_RESOURCE.getName(), STATUS_500, DESC_ERROR, service.getComponentType().getValue(),
218 false, false, false, false, false, false);
222 public void auditResourceWhenAllParamsPassed() {
223 when(responseFormat.getStatus()).thenReturn(Integer.valueOf(STATUS_OK));
224 when(responseFormat.getFormattedMessage()).thenReturn(DESCRIPTION);
226 resource.setUUID(SERVICE_INSTANCE_ID);
227 resource.setInvariantUUID(INVARIANT_UUID);
228 resource.setName(RESOURCE_NAME);
229 resource.setLifecycleState(LifecycleStateEnum.CERTIFIED);
230 resource.setVersion(CURRENT_VERSION);
231 resource.setToscaResourceName(TOSCA_NODE_TYPE);
232 utils.auditResource(responseFormat, modifier, resource, null, AuditingActionEnum.IMPORT_RESOURCE,
233 ResourceAuditData.newBuilder()
234 .state(PREV_RESOURCE_STATE)
235 .version(PREV_RESOURCE_VERSION)
236 .artifactUuid(ARTIFACT_UUID)
238 ARTIFACT_UUID, ARTIFACT_DATA);
239 verifyResourceAdminEvent(AuditingActionEnum.IMPORT_RESOURCE.getName(), STATUS_OK, DESCRIPTION, resource.getResourceType().name(),
240 false, true, true, true, false, false);
244 public void auditResourceWithoutPrevFields() {
245 when(responseFormat.getStatus()).thenReturn(Integer.valueOf(STATUS_500));
246 when(responseFormat.getFormattedMessage()).thenReturn(DESC_ERROR);
248 resource.setUUID(SERVICE_INSTANCE_ID);
249 resource.setInvariantUUID(INVARIANT_UUID);
250 resource.setName(RESOURCE_NAME);
251 resource.setLifecycleState(LifecycleStateEnum.CERTIFIED);
252 resource.setVersion(CURRENT_VERSION);
254 utils.auditResource(responseFormat, modifier, resource, AuditingActionEnum.UPDATE_RESOURCE_METADATA);
255 verifyResourceAdminEvent(AuditingActionEnum.UPDATE_RESOURCE_METADATA.getName(), STATUS_500, DESC_ERROR, resource.getResourceType().name(),
256 false, false, false, false, false, false);
260 public void auditResourceWhenResourceIsNull() {
261 when(responseFormat.getStatus()).thenReturn(Integer.valueOf(STATUS_500));
262 when(responseFormat.getFormattedMessage()).thenReturn(DESC_ERROR);
264 utils.auditResource(responseFormat, modifier, RESOURCE_NAME, AuditingActionEnum.CHECKOUT_RESOURCE);
265 verifyResourceAdminEvent(AuditingActionEnum.CHECKOUT_RESOURCE.getName(), STATUS_500, DESC_ERROR, ComponentTypeEnum.RESOURCE.getValue(),
266 true, false, false, false, false, false);
270 public void auditUserAdminEvent() {
271 when(responseFormat.getStatus()).thenReturn(Integer.valueOf(STATUS_OK));
272 when(responseFormat.getFormattedMessage()).thenReturn(DESCRIPTION);
274 User userAfter = new User();
275 userAfter.setFirstName(USER_FIRST_NAME);
276 userAfter.setUserId(USER_ID);
277 userAfter.setLastName(USER_LAST_NAME);
278 userAfter.setRole(TESTER_USER_ROLE);
279 userAfter.setEmail(USER_EMAIL);
281 utils.auditAdminUserAction(AuditingActionEnum.ADD_USER, modifier, null, userAfter, responseFormat);
282 verify(manager).auditEvent(factoryCaptor.capture());
283 AuditEventFactory factory = factoryCaptor.getValue();
284 UserAdminEvent event = (UserAdminEvent)factory.getDbEvent();
285 assertThat(event.getModifier()).isEqualTo(MODIFIER_UID);
286 assertThat(event.getStatus()).isEqualTo(STATUS_OK);
287 assertThat(event.getDesc()).isEqualTo(DESCRIPTION);
288 assertThat(event.getRequestId()).isEqualTo(REQUEST_ID);
289 assertThat(event.getAction()).isEqualTo(AuditingActionEnum.ADD_USER.getName());
290 assertThat(event.getUserBefore()).isNull();
291 assertThat(event.getUserAfter()).isEqualTo(UPDATED_USER_EXTENDED_NAME);
295 public void auditEcompOpEnvEvent() {
296 utils.auditEnvironmentEngine(AuditingActionEnum.CREATE_ENVIRONMENT, OP_ENV_ID, OP_ENV_TYPE, OP_ENV_ACTION, OP_ENV_NAME, TENANT_CONTEXT);
297 verify(manager).auditEvent(factoryCaptor.capture());
298 AuditEventFactory factory = factoryCaptor.getValue();
299 EcompOperationalEnvironmentEvent event = (EcompOperationalEnvironmentEvent)factory.getDbEvent();
300 assertThat(event.getAction()).isEqualTo(AuditingActionEnum.CREATE_ENVIRONMENT.getName());
301 assertThat(event.getOperationalEnvironmentId()).isEqualTo(OP_ENV_ID);
302 assertThat(event.getOperationalEnvironmentType()).isEqualTo(OP_ENV_TYPE);
303 assertThat(event.getOperationalEnvironmentName()).isEqualTo(OP_ENV_NAME);
304 assertThat(event.getOperationalEnvironmentAction()).isEqualTo(OP_ENV_ACTION);
305 assertThat(event.getTenantContext()).isEqualTo(TENANT_CONTEXT);
309 public void auditDistrDownloadEvent(){
310 when(responseFormat.getStatus()).thenReturn(Integer.valueOf(STATUS_OK));
311 when(responseFormat.getFormattedMessage()).thenReturn(DESCRIPTION);
313 utils.auditDistributionDownload(responseFormat, new DistributionData(DIST_CONSUMER_ID, DIST_RESOURCE_URL));
315 verify(manager).auditEvent(factoryCaptor.capture());
316 AuditEventFactory factory = factoryCaptor.getValue();
317 DistributionDownloadEvent event = (DistributionDownloadEvent)factory.getDbEvent();
318 assertThat(event.getResourceUrl()).isEqualTo(DIST_RESOURCE_URL);
319 assertThat(event.getConsumerId()).isEqualTo(DIST_CONSUMER_ID);
320 assertThat(event.getStatus()).isEqualTo(STATUS_OK);
321 assertThat(event.getDesc()).isEqualTo(DESCRIPTION);
322 assertThat(event.getRequestId()).isEqualTo(REQUEST_ID);
323 assertThat(event.getAction()).isEqualTo(AuditingActionEnum.DISTRIBUTION_ARTIFACT_DOWNLOAD.getName());
326 private void verifyResourceAdminEvent(String action, String status, String desc, String resourceType, boolean isComponentNull,
327 boolean isPrevStateAndVersionSet, boolean isCurrFieldsProvided, boolean isArtDataProvided, boolean isCommentProvided,
328 boolean isDistStatusProvided) {
329 verify(manager).auditEvent(factoryCaptor.capture());
330 AuditEventFactory factory = factoryCaptor.getValue();
331 ResourceAdminEvent event = (ResourceAdminEvent)factory.getDbEvent();
332 assertThat(event.getModifier()).isEqualTo(MODIFIER_UID);
333 assertThat(event.getStatus()).isEqualTo(status);
334 assertThat(event.getDesc()).isEqualTo(desc);
335 assertThat(event.getRequestId()).isEqualTo(REQUEST_ID);
336 assertThat(event.getAction()).isEqualTo(action);
337 assertThat(event.getResourceName()).isEqualTo(RESOURCE_NAME);
339 if (isComponentNull) {
340 assertThat(event.getServiceInstanceId()).isNull();
341 assertThat(event.getCurrState()).isNull();
342 assertThat(event.getCurrVersion()).isNull();
343 assertThat(event.getInvariantUUID()).isNull();
344 assertThat(event.getResourceType()).isEqualTo(resourceType);
347 assertThat(event.getServiceInstanceId()).isEqualTo(SERVICE_INSTANCE_ID);
348 assertThat(event.getCurrState()).isEqualTo(CURRENT_STATE);
349 assertThat(event.getCurrVersion()).isEqualTo(CURRENT_VERSION);
350 assertThat(event.getInvariantUUID()).isEqualTo(INVARIANT_UUID);
352 if (action.equals(AuditingActionEnum.IMPORT_RESOURCE.getName())) {
353 assertThat(event.getToscaNodeType()).isEqualTo(TOSCA_NODE_TYPE);
356 assertThat(event.getToscaNodeType()).isEmpty();
358 if (isPrevStateAndVersionSet) {
359 assertThat(event.getPrevState()).isEqualTo(PREV_RESOURCE_STATE);
360 assertThat(event.getPrevVersion()).isEqualTo(PREV_RESOURCE_VERSION);
361 assertThat(event.getPrevArtifactUUID()).isEqualTo(ARTIFACT_UUID);
364 assertThat(event.getPrevState()).isNull();
365 assertThat(event.getPrevVersion()).isNull();
366 assertThat(event.getPrevArtifactUUID()).isNull();
368 if (isCurrFieldsProvided) {
369 assertThat(event.getCurrArtifactUUID()).isEqualTo(ARTIFACT_UUID);
372 assertThat(event.getCurrArtifactUUID()).isNull();
374 if (isArtDataProvided) {
375 assertThat(event.getArtifactData()).isEqualTo(ARTIFACT_DATA);
376 if (resourceType.equals(ResourceTypeEnum.VFC.name())) {
377 assertThat(event.getDid()).isNull();
380 assertThat(event.getDid()).isEqualTo(DIST_ID);
384 assertThat(event.getArtifactData()).isNull();
385 assertThat(event.getDid()).isNull();
387 if (isCommentProvided) {
388 assertThat(event.getComment()).isEqualTo(COMMENT);
391 assertThat(event.getComment()).isEmpty();
393 if (isDistStatusProvided) {
394 assertThat(event.getDcurrStatus()).isEqualTo(DCURR_STATUS);
395 assertThat(event.getDprevStatus()).isEqualTo(DPREV_STATUS);
398 assertThat(event.getDcurrStatus()).isNull();
399 assertThat(event.getDprevStatus()).isNull();