Catalog alignment
[sdc.git] / catalog-be / src / test / java / org / openecomp / sdc / be / components / impl / ComponentsUtilsTest.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * SDC
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
10  * 
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  * 
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=========================================================
19  */
20
21 package org.openecomp.sdc.be.components.impl;
22
23 import org.junit.Before;
24 import org.junit.BeforeClass;
25 import org.junit.Test;
26 import org.junit.runner.RunWith;
27 import org.mockito.ArgumentCaptor;
28 import org.mockito.Captor;
29 import org.mockito.InjectMocks;
30 import org.mockito.Mock;
31 import org.mockito.junit.MockitoJUnitRunner;
32 import org.openecomp.sdc.be.auditing.api.AuditEventFactory;
33 import org.openecomp.sdc.be.auditing.impl.AuditingManager;
34 import org.openecomp.sdc.be.auditing.impl.externalapi.AuditChangeLifecycleExternalApiEventFactory;
35 import org.openecomp.sdc.be.auditing.impl.externalapi.AuditCreateResourceExternalApiEventFactory;
36 import org.openecomp.sdc.be.auditing.impl.externalapi.AuditCreateServiceExternalApiEventFactory;
37 import org.openecomp.sdc.be.datatypes.enums.ComponentTypeEnum;
38 import org.openecomp.sdc.be.impl.ComponentsUtils;
39 import org.openecomp.sdc.be.model.ArtifactDefinition;
40 import org.openecomp.sdc.be.model.Component;
41 import org.openecomp.sdc.be.model.LifecycleStateEnum;
42 import org.openecomp.sdc.be.model.Resource;
43 import org.openecomp.sdc.be.model.Service;
44 import org.openecomp.sdc.be.model.User;
45 import org.openecomp.sdc.be.resources.data.auditing.AuditingActionEnum;
46 import org.openecomp.sdc.be.resources.data.auditing.DistributionDownloadEvent;
47 import org.openecomp.sdc.be.resources.data.auditing.EcompOperationalEnvironmentEvent;
48 import org.openecomp.sdc.be.resources.data.auditing.ExternalApiEvent;
49 import org.openecomp.sdc.be.resources.data.auditing.ResourceAdminEvent;
50 import org.openecomp.sdc.be.resources.data.auditing.UserAdminEvent;
51 import org.openecomp.sdc.be.resources.data.auditing.model.DistributionData;
52 import org.openecomp.sdc.be.resources.data.auditing.model.ResourceCommonInfo;
53 import org.openecomp.sdc.be.resources.data.auditing.model.ResourceVersionInfo;
54 import org.openecomp.sdc.common.api.Constants;
55 import org.openecomp.sdc.common.log.wrappers.LoggerSdcAudit;
56 import org.openecomp.sdc.common.util.ThreadLocalsHolder;
57 import org.openecomp.sdc.exception.ResponseFormat;
58
59 import javax.servlet.http.HttpServletRequest;
60
61 import static org.assertj.core.api.Assertions.assertThat;
62 import static org.mockito.Mockito.verify;
63 import static org.mockito.Mockito.when;
64 import static org.openecomp.sdc.be.auditing.impl.AuditTestUtils.ARTIFACT_DATA;
65 import static org.openecomp.sdc.be.auditing.impl.AuditTestUtils.ARTIFACT_UUID;
66 import static org.openecomp.sdc.be.auditing.impl.AuditTestUtils.COMMENT;
67 import static org.openecomp.sdc.be.auditing.impl.AuditTestUtils.CURRENT_STATE;
68 import static org.openecomp.sdc.be.auditing.impl.AuditTestUtils.CURRENT_VERSION;
69 import static org.openecomp.sdc.be.auditing.impl.AuditTestUtils.DCURR_STATUS;
70 import static org.openecomp.sdc.be.auditing.impl.AuditTestUtils.DESCRIPTION;
71 import static org.openecomp.sdc.be.auditing.impl.AuditTestUtils.DESC_ERROR;
72 import static org.openecomp.sdc.be.auditing.impl.AuditTestUtils.DIST_CONSUMER_ID;
73 import static org.openecomp.sdc.be.auditing.impl.AuditTestUtils.DIST_ID;
74 import static org.openecomp.sdc.be.auditing.impl.AuditTestUtils.DIST_RESOURCE_URL;
75 import static org.openecomp.sdc.be.auditing.impl.AuditTestUtils.DPREV_STATUS;
76 import static org.openecomp.sdc.be.auditing.impl.AuditTestUtils.INVARIANT_UUID;
77 import static org.openecomp.sdc.be.auditing.impl.AuditTestUtils.MODIFIER_FIRST_NAME;
78 import static org.openecomp.sdc.be.auditing.impl.AuditTestUtils.MODIFIER_ID;
79 import static org.openecomp.sdc.be.auditing.impl.AuditTestUtils.MODIFIER_LAST_NAME;
80 import static org.openecomp.sdc.be.auditing.impl.AuditTestUtils.MODIFIER_UID;
81 import static org.openecomp.sdc.be.auditing.impl.AuditTestUtils.OP_ENV_ACTION;
82 import static org.openecomp.sdc.be.auditing.impl.AuditTestUtils.OP_ENV_ID;
83 import static org.openecomp.sdc.be.auditing.impl.AuditTestUtils.OP_ENV_NAME;
84 import static org.openecomp.sdc.be.auditing.impl.AuditTestUtils.OP_ENV_TYPE;
85 import static org.openecomp.sdc.be.auditing.impl.AuditTestUtils.PREV_RESOURCE_STATE;
86 import static org.openecomp.sdc.be.auditing.impl.AuditTestUtils.PREV_RESOURCE_VERSION;
87 import static org.openecomp.sdc.be.auditing.impl.AuditTestUtils.REQUEST_ID;
88 import static org.openecomp.sdc.be.auditing.impl.AuditTestUtils.RESOURCE_NAME;
89 import static org.openecomp.sdc.be.auditing.impl.AuditTestUtils.SERVICE_INSTANCE_ID;
90 import static org.openecomp.sdc.be.auditing.impl.AuditTestUtils.STATUS_500;
91 import static org.openecomp.sdc.be.auditing.impl.AuditTestUtils.STATUS_OK;
92 import static org.openecomp.sdc.be.auditing.impl.AuditTestUtils.TENANT_CONTEXT;
93 import static org.openecomp.sdc.be.auditing.impl.AuditTestUtils.TESTER_USER_ROLE;
94 import static org.openecomp.sdc.be.auditing.impl.AuditTestUtils.TOSCA_NODE_TYPE;
95 import static org.openecomp.sdc.be.auditing.impl.AuditTestUtils.UPDATED_USER_EXTENDED_NAME;
96 import static org.openecomp.sdc.be.auditing.impl.AuditTestUtils.USER_EMAIL;
97 import static org.openecomp.sdc.be.auditing.impl.AuditTestUtils.USER_FIRST_NAME;
98 import static org.openecomp.sdc.be.auditing.impl.AuditTestUtils.USER_ID;
99 import static org.openecomp.sdc.be.auditing.impl.AuditTestUtils.USER_LAST_NAME;
100
101 @RunWith(MockitoJUnitRunner.class)
102 public class ComponentsUtilsTest {
103
104     private static User modifier = new User();
105     private Component service = new Service();
106     private Resource resource = new Resource();
107
108     @Mock
109     private AuditingManager manager;
110     @Mock
111     private ResponseFormat responseFormat;
112     @Mock
113     private HttpServletRequest request;
114
115     private ArtifactDefinition artifactDefinition = null;
116
117     @Captor
118     private ArgumentCaptor<AuditEventFactory> factoryCaptor;
119
120     @Captor
121     private ArgumentCaptor<LoggerSdcAudit> audit;
122
123     @InjectMocks
124     private static ComponentsUtils utils;
125
126     @BeforeClass
127     public static void setUpClass() {
128         modifier.setFirstName(MODIFIER_FIRST_NAME);
129         modifier.setLastName(MODIFIER_LAST_NAME);
130         modifier.setUserId(MODIFIER_ID);
131     }
132
133
134     @Before
135     public void setUp() {
136         ThreadLocalsHolder.setUuid(REQUEST_ID);
137         utils = new ComponentsUtils(manager);
138     }
139
140     @Test
141     public void auditComponentWhenAllParamsPassed() {
142         when(responseFormat.getStatus()).thenReturn(Integer.valueOf(STATUS_OK));
143         when(responseFormat.getFormattedMessage()).thenReturn(DESCRIPTION);
144         service.setUUID(SERVICE_INSTANCE_ID);
145         service.setInvariantUUID(INVARIANT_UUID);
146
147         utils.auditComponent(responseFormat, modifier, service, AuditingActionEnum.ARTIFACT_DELETE,
148                 new ResourceCommonInfo(RESOURCE_NAME, service.getComponentType().getValue()),
149                 ResourceVersionInfo.newBuilder()
150                         .state(PREV_RESOURCE_STATE)
151                         .version(PREV_RESOURCE_VERSION)
152                         .artifactUuid(ARTIFACT_UUID)
153                         .distributionStatus(DPREV_STATUS).build(),
154                 ResourceVersionInfo.newBuilder()
155                         .state(CURRENT_STATE)
156                         .version(CURRENT_VERSION)
157                         .artifactUuid(ARTIFACT_UUID)
158                         .distributionStatus(DCURR_STATUS).build(),
159                 COMMENT, artifactDefinition, DIST_ID);
160         verifyResourceAdminEvent(AuditingActionEnum.ARTIFACT_DELETE.getName(), STATUS_OK, DESCRIPTION, service.getComponentType().getValue(),
161                 false, true, true, false, true, true, true);
162     }
163
164     @Test
165     public void auditComponentWhenAllParamsPassedAndMostFromComponent() {
166         when(responseFormat.getStatus()).thenReturn(Integer.valueOf(STATUS_OK));
167         when(responseFormat.getFormattedMessage()).thenReturn(DESCRIPTION);
168
169         service.setUUID(SERVICE_INSTANCE_ID);
170         service.setInvariantUUID(INVARIANT_UUID);
171         service.setState(LifecycleStateEnum.CERTIFIED);
172         service.setVersion(CURRENT_VERSION);
173         utils.auditComponent(responseFormat, modifier, service, AuditingActionEnum.CREATE_RESOURCE,
174                 new ResourceCommonInfo(RESOURCE_NAME, service.getComponentType().getValue()),
175                 ResourceVersionInfo.newBuilder()
176                         .state(PREV_RESOURCE_STATE)
177                         .version(PREV_RESOURCE_VERSION)
178                         .artifactUuid(ARTIFACT_UUID)
179                         .distributionStatus(DPREV_STATUS).build(),
180                 ResourceVersionInfo.newBuilder()
181                         .artifactUuid(ARTIFACT_UUID)
182                         .distributionStatus(DCURR_STATUS).build(),
183                 COMMENT, artifactDefinition, DIST_ID);
184         verifyResourceAdminEvent(AuditingActionEnum.CREATE_RESOURCE.getName(), STATUS_OK, DESCRIPTION, service.getComponentType().getValue(),
185                 false, true, true, false, true, true, true);
186
187     }
188
189     @Test
190     public void auditComponentDistStateWithoutArtDataCommentAndDid() {
191         when(responseFormat.getStatus()).thenReturn(Integer.valueOf(STATUS_OK));
192         when(responseFormat.getFormattedMessage()).thenReturn(DESCRIPTION);
193
194         service.setUUID(SERVICE_INSTANCE_ID);
195         service.setInvariantUUID(INVARIANT_UUID);
196         service.setState(LifecycleStateEnum.CERTIFIED);
197         service.setName(RESOURCE_NAME);
198         service.setVersion(CURRENT_VERSION);
199         utils.auditComponent(responseFormat, modifier, service, AuditingActionEnum.DISTRIBUTION_STATE_CHANGE_REQUEST,
200                 new ResourceCommonInfo(service.getComponentType().getValue()),
201                 ResourceVersionInfo.newBuilder()
202                         .state(PREV_RESOURCE_STATE)
203                         .version(PREV_RESOURCE_VERSION)
204                         .artifactUuid(ARTIFACT_UUID)
205                         .distributionStatus(DPREV_STATUS).build(),
206                 ResourceVersionInfo.newBuilder()
207                         .artifactUuid(ARTIFACT_UUID)
208                         .distributionStatus(DCURR_STATUS).build());
209         verifyResourceAdminEvent(AuditingActionEnum.DISTRIBUTION_STATE_CHANGE_REQUEST.getName(),
210                 STATUS_OK, DESCRIPTION, service.getComponentType().getValue(),
211                 false, true, true, false, false, true, false);
212     }
213
214     @Test
215     public void auditComponentWhenError() {
216         when(responseFormat.getStatus()).thenReturn(Integer.valueOf(STATUS_500));
217         when(responseFormat.getFormattedMessage()).thenReturn(DESC_ERROR);
218
219         service.setUUID(SERVICE_INSTANCE_ID);
220         service.setInvariantUUID(INVARIANT_UUID);
221         service.setState(LifecycleStateEnum.CERTIFIED);
222         service.setName(RESOURCE_NAME);
223         service.setVersion(CURRENT_VERSION);
224
225         utils.auditComponent(responseFormat, modifier, service, AuditingActionEnum.CERTIFICATION_REQUEST_RESOURCE,
226                 new ResourceCommonInfo(ComponentTypeEnum.SERVICE.getValue()),
227                 ResourceVersionInfo.newBuilder().state(PREV_RESOURCE_STATE).version(PREV_RESOURCE_VERSION).artifactUuid(ARTIFACT_UUID).build());
228         verifyResourceAdminEvent(AuditingActionEnum.CERTIFICATION_REQUEST_RESOURCE.getName(), STATUS_500, DESC_ERROR,
229                 service.getComponentType().getValue(), false, true, false, false, false, false, false);
230     }
231
232     @Test
233     public void auditComponentWhenCompIsNull() {
234         when(responseFormat.getStatus()).thenReturn(Integer.valueOf(STATUS_500));
235         when(responseFormat.getFormattedMessage()).thenReturn(DESC_ERROR);
236
237         utils.auditComponent(responseFormat, modifier, AuditingActionEnum.START_CERTIFICATION_RESOURCE,
238                 new ResourceCommonInfo(RESOURCE_NAME, ComponentTypeEnum.SERVICE.getValue()), COMMENT);
239         verifyResourceAdminEvent(AuditingActionEnum.START_CERTIFICATION_RESOURCE.getName(), STATUS_500, DESC_ERROR, service.getComponentType().getValue(),
240                 true, false, false, false, true, false, false);
241     }
242
243     @Test
244     public void auditComponentAdmin() {
245         when(responseFormat.getStatus()).thenReturn(Integer.valueOf(STATUS_500));
246         when(responseFormat.getFormattedMessage()).thenReturn(DESC_ERROR);
247
248         service.setUUID(SERVICE_INSTANCE_ID);
249         service.setInvariantUUID(INVARIANT_UUID);
250         service.setName(RESOURCE_NAME);
251         service.setState(LifecycleStateEnum.CERTIFIED);
252         service.setVersion(CURRENT_VERSION);
253         utils.auditComponentAdmin(responseFormat, modifier, service, AuditingActionEnum.CREATE_RESOURCE, service.getComponentType());
254         verifyResourceAdminEvent(AuditingActionEnum.CREATE_RESOURCE.getName(), STATUS_500, DESC_ERROR, service.getComponentType().getValue(),
255                 false, false, false, false, false, false, false);
256     }
257
258     @Test
259     public void auditResourceWhenAllParamsPassed() {
260         when(responseFormat.getStatus()).thenReturn(Integer.valueOf(STATUS_OK));
261         when(responseFormat.getFormattedMessage()).thenReturn(DESCRIPTION);
262
263         resource.setUUID(SERVICE_INSTANCE_ID);
264         resource.setInvariantUUID(INVARIANT_UUID);
265         resource.setName(RESOURCE_NAME);
266         resource.setLifecycleState(LifecycleStateEnum.CERTIFIED);
267         resource.setVersion(CURRENT_VERSION);
268         resource.setToscaResourceName(TOSCA_NODE_TYPE);
269         utils.auditResource(responseFormat, modifier, resource, null, AuditingActionEnum.IMPORT_RESOURCE,
270                 ResourceVersionInfo.newBuilder()
271                         .state(PREV_RESOURCE_STATE)
272                         .version(PREV_RESOURCE_VERSION)
273                         .artifactUuid(ARTIFACT_UUID)
274                         .build(),
275                 ARTIFACT_UUID, artifactDefinition);
276         verifyResourceAdminEvent(AuditingActionEnum.IMPORT_RESOURCE.getName(), STATUS_OK, DESCRIPTION, resource.getResourceType().name(),
277                 false, true, true, false, false, false, false);
278     }
279
280     @Test
281     public void auditResourceWithoutPrevFields() {
282         when(responseFormat.getStatus()).thenReturn(Integer.valueOf(STATUS_500));
283         when(responseFormat.getFormattedMessage()).thenReturn(DESC_ERROR);
284
285         resource.setUUID(SERVICE_INSTANCE_ID);
286         resource.setInvariantUUID(INVARIANT_UUID);
287         resource.setName(RESOURCE_NAME);
288         resource.setLifecycleState(LifecycleStateEnum.CERTIFIED);
289         resource.setVersion(CURRENT_VERSION);
290
291         utils.auditResource(responseFormat, modifier, resource, AuditingActionEnum.UPDATE_RESOURCE_METADATA);
292         verifyResourceAdminEvent(AuditingActionEnum.UPDATE_RESOURCE_METADATA.getName(), STATUS_500, DESC_ERROR, resource.getResourceType().name(),
293                 false, false, false, false, false, false, false);
294     }
295
296     @Test
297     public void auditResourceWhenResourceIsNull() {
298         when(responseFormat.getStatus()).thenReturn(Integer.valueOf(STATUS_500));
299         when(responseFormat.getFormattedMessage()).thenReturn(DESC_ERROR);
300
301         utils.auditResource(responseFormat, modifier, RESOURCE_NAME, AuditingActionEnum.CHECKOUT_RESOURCE);
302         verifyResourceAdminEvent(AuditingActionEnum.CHECKOUT_RESOURCE.getName(), STATUS_500, DESC_ERROR, ComponentTypeEnum.RESOURCE.getValue(),
303                 true, false, false, false, false, false, false);
304     }
305
306     @Test
307     public void auditUserAdminEvent() {
308         when(responseFormat.getStatus()).thenReturn(Integer.valueOf(STATUS_OK));
309         when(responseFormat.getFormattedMessage()).thenReturn(DESCRIPTION);
310
311         User userAfter = new User();
312         userAfter.setFirstName(USER_FIRST_NAME);
313         userAfter.setUserId(USER_ID);
314         userAfter.setLastName(USER_LAST_NAME);
315         userAfter.setRole(TESTER_USER_ROLE);
316         userAfter.setEmail(USER_EMAIL);
317
318         utils.auditAdminUserAction(AuditingActionEnum.ADD_USER, modifier, null, userAfter, responseFormat);
319         verify(manager).auditEvent(factoryCaptor.capture());
320         AuditEventFactory factory = factoryCaptor.getValue();
321         UserAdminEvent event = (UserAdminEvent)factory.getDbEvent();
322         assertThat(event.getModifier()).isEqualTo(MODIFIER_UID);
323         assertThat(event.getStatus()).isEqualTo(STATUS_OK);
324         assertThat(event.getDesc()).isEqualTo(DESCRIPTION);
325         assertThat(event.getRequestId()).isEqualTo(REQUEST_ID);
326         assertThat(event.getAction()).isEqualTo(AuditingActionEnum.ADD_USER.getName());
327         assertThat(event.getUserBefore()).isNull();
328         assertThat(event.getUserAfter()).isEqualTo(UPDATED_USER_EXTENDED_NAME);
329     }
330
331     @Test
332     public void auditEcompOpEnvEvent() {
333
334         utils.auditEnvironmentEngine(AuditingActionEnum.CREATE_ENVIRONMENT, OP_ENV_ID, OP_ENV_TYPE, OP_ENV_ACTION, OP_ENV_NAME, TENANT_CONTEXT);
335         verify(manager).auditEvent(factoryCaptor.capture(), audit.capture());
336         AuditEventFactory factory = factoryCaptor.getValue();
337         EcompOperationalEnvironmentEvent event = (EcompOperationalEnvironmentEvent)factory.getDbEvent();
338         assertThat(event.getAction()).isEqualTo(AuditingActionEnum.CREATE_ENVIRONMENT.getName());
339         assertThat(event.getOperationalEnvironmentId()).isEqualTo(OP_ENV_ID);
340         assertThat(event.getOperationalEnvironmentType()).isEqualTo(OP_ENV_TYPE);
341         assertThat(event.getOperationalEnvironmentName()).isEqualTo(OP_ENV_NAME);
342         assertThat(event.getOperationalEnvironmentAction()).isEqualTo(OP_ENV_ACTION);
343         assertThat(event.getTenantContext()).isEqualTo(TENANT_CONTEXT);
344     }
345
346     @Test
347     public void auditDistrDownloadEvent(){
348         when(responseFormat.getStatus()).thenReturn(Integer.valueOf(STATUS_OK));
349         when(responseFormat.getFormattedMessage()).thenReturn(DESCRIPTION);
350
351         utils.auditDistributionDownload(responseFormat, new DistributionData(DIST_CONSUMER_ID, DIST_RESOURCE_URL));
352
353         verify(manager).auditEvent(factoryCaptor.capture());
354         AuditEventFactory factory = factoryCaptor.getValue();
355         DistributionDownloadEvent event = (DistributionDownloadEvent)factory.getDbEvent();
356         assertThat(event.getResourceUrl()).isEqualTo(DIST_RESOURCE_URL);
357         assertThat(event.getConsumerId()).isEqualTo(DIST_CONSUMER_ID);
358         assertThat(event.getStatus()).isEqualTo(STATUS_OK);
359         assertThat(event.getDesc()).isEqualTo(DESCRIPTION);
360         assertThat(event.getRequestId()).isEqualTo(REQUEST_ID);
361         assertThat(event.getAction()).isEqualTo(AuditingActionEnum.DISTRIBUTION_ARTIFACT_DOWNLOAD.getName());
362     }
363
364     private void verifyResourceAdminEvent(String action, String status, String desc, String resourceType, boolean isComponentNull,
365             boolean isPrevStateAndVersionSet, boolean isCurrFieldsProvided, boolean isArtDataProvided, boolean isCommentProvided,
366             boolean isDistStatusProvided, boolean isDidProvided) {
367         verify(manager).auditEvent(factoryCaptor.capture());
368         AuditEventFactory factory = factoryCaptor.getValue();
369         ResourceAdminEvent event = (ResourceAdminEvent)factory.getDbEvent();
370         assertThat(event.getModifier()).isEqualTo(MODIFIER_UID);
371         assertThat(event.getStatus()).isEqualTo(status);
372         assertThat(event.getDesc()).isEqualTo(desc);
373         assertThat(event.getRequestId()).isEqualTo(REQUEST_ID);
374         assertThat(event.getAction()).isEqualTo(action);
375         assertThat(event.getResourceName()).isEqualTo(RESOURCE_NAME);
376
377         if (isComponentNull) {
378             assertThat(event.getServiceInstanceId()).isNull();
379             assertThat(event.getCurrState()).isNull();
380             assertThat(event.getCurrVersion()).isNull();
381             assertThat(event.getInvariantUUID()).isNull();
382             assertThat(event.getResourceType()).isEqualTo(resourceType);
383         }
384         else {
385             assertThat(event.getServiceInstanceId()).isEqualTo(SERVICE_INSTANCE_ID);
386             assertThat(event.getCurrState()).isEqualTo(CURRENT_STATE);
387             assertThat(event.getCurrVersion()).isEqualTo(CURRENT_VERSION);
388             assertThat(event.getInvariantUUID()).isEqualTo(INVARIANT_UUID);
389         }
390         if (action.equals(AuditingActionEnum.IMPORT_RESOURCE.getName())) {
391             assertThat(event.getToscaNodeType()).isEqualTo(TOSCA_NODE_TYPE);
392         }
393         else {
394             assertThat(event.getToscaNodeType()).isEmpty();
395         }
396         if (isPrevStateAndVersionSet) {
397             assertThat(event.getPrevState()).isEqualTo(PREV_RESOURCE_STATE);
398             assertThat(event.getPrevVersion()).isEqualTo(PREV_RESOURCE_VERSION);
399             assertThat(event.getPrevArtifactUUID()).isEqualTo(ARTIFACT_UUID);
400         }
401         else {
402             assertThat(event.getPrevState()).isNull();
403             assertThat(event.getPrevVersion()).isNull();
404             assertThat(event.getPrevArtifactUUID()).isNull();
405         }
406         if (isCurrFieldsProvided) {
407             assertThat(event.getCurrArtifactUUID()).isEqualTo(ARTIFACT_UUID);
408         }
409         else {
410             assertThat(event.getCurrArtifactUUID()).isNull();
411         }
412         if (isArtDataProvided) {
413             assertThat(event.getArtifactData()).isEqualTo(ARTIFACT_DATA);
414         }
415         else {
416             assertThat(event.getArtifactData()).isEmpty();
417         }
418         if (isDidProvided) {
419             assertThat(event.getDid()).isEqualTo(DIST_ID);
420         }
421         else {
422             assertThat(event.getDid()).isNull();
423         }
424         if (isCommentProvided) {
425             assertThat(event.getComment()).isEqualTo(COMMENT);
426         }
427         else {
428             assertThat(event.getComment()).isEmpty();
429         }
430         if (isDistStatusProvided) {
431             assertThat(event.getDcurrStatus()).isEqualTo(DCURR_STATUS);
432             assertThat(event.getDprevStatus()).isEqualTo(DPREV_STATUS);
433         }
434         else {
435             assertThat(event.getDcurrStatus()).isNull();
436             assertThat(event.getDprevStatus()).isNull();
437         }
438     }
439
440     @Test
441     public void auditChangeLifeCycleExternalApiEventWhenComponentAndResponseObjectAreNull() {
442         when(responseFormat.getStatus()).thenReturn(Integer.valueOf(STATUS_500));
443         when(responseFormat.getFormattedMessage()).thenReturn(DESC_ERROR);
444
445         utils.auditChangeLifecycleAction(responseFormat, ComponentTypeEnum.RESOURCE, REQUEST_ID,
446                 null, null, new DistributionData(DIST_CONSUMER_ID, DIST_RESOURCE_URL), modifier);
447
448         verify(manager).auditEvent(factoryCaptor.capture());
449         AuditChangeLifecycleExternalApiEventFactory factory = (AuditChangeLifecycleExternalApiEventFactory)factoryCaptor.getValue();
450
451         ExternalApiEvent event = (ExternalApiEvent)factory.getDbEvent();
452         assertThat(event.getAction()).isEqualTo(AuditingActionEnum.CHANGE_LIFECYCLE_BY_API.getName());
453         verifyCommonDataForExternalApiEvent(event, false);
454         verifyPreviousResourceVersionInfoForExternalApiEvent(event, true);
455         verifyCurrentResourceVersionInfoForExternalApiEvent(event, true);
456         verifyDistributionDataForExternalApiEvent(event);
457         assertThat(event.getModifier()).isEqualTo(MODIFIER_UID);
458         assertThat(event.getInvariantUuid()).isEmpty();
459         assertThat(event.getResourceName()).isNull();
460         assertThat(event.getResourceType()).isEqualTo(ComponentTypeEnum.RESOURCE.getValue());
461     }
462
463     @Test
464     public void auditChangeLifeCycleExternalApiEventWhenComponentIsNullAndResponseObjectIsNotNull() {
465         when(responseFormat.getStatus()).thenReturn(Integer.valueOf(STATUS_OK));
466         when(responseFormat.getFormattedMessage()).thenReturn(DESCRIPTION);
467         Component responseObject = new Resource();
468         responseObject.setVersion(CURRENT_VERSION);
469         responseObject.setState(LifecycleStateEnum.CERTIFIED);
470         responseObject.setInvariantUUID(INVARIANT_UUID);
471         responseObject.setUUID(SERVICE_INSTANCE_ID);
472
473         utils.auditChangeLifecycleAction(responseFormat, ComponentTypeEnum.RESOURCE, REQUEST_ID,
474                 null, responseObject, new DistributionData(DIST_CONSUMER_ID, DIST_RESOURCE_URL), modifier);
475
476         verify(manager).auditEvent(factoryCaptor.capture());
477         AuditChangeLifecycleExternalApiEventFactory factory = (AuditChangeLifecycleExternalApiEventFactory)factoryCaptor.getValue();
478
479         ExternalApiEvent event = (ExternalApiEvent)factory.getDbEvent();
480         assertThat(event.getAction()).isEqualTo(AuditingActionEnum.CHANGE_LIFECYCLE_BY_API.getName());
481         verifyCommonDataForExternalApiEvent(event, true);
482         verifyPreviousResourceVersionInfoForExternalApiEvent(event, true);
483         verifyCurrentResourceVersionInfoForExternalApiEvent(event, false);
484         verifyDistributionDataForExternalApiEvent(event);
485         assertThat(event.getModifier()).isEqualTo(MODIFIER_UID);
486         assertThat(event.getInvariantUuid()).isEqualTo(INVARIANT_UUID);
487         assertThat(event.getResourceName()).isNull();
488         assertThat(event.getResourceType()).isEqualTo(ComponentTypeEnum.RESOURCE.getValue());
489     }
490
491
492     private void verifyDistributionDataForExternalApiEvent(ExternalApiEvent event) {
493         assertThat(event.getConsumerId()).isEqualTo(DIST_CONSUMER_ID);
494         assertThat(event.getResourceURL()).isEqualTo(DIST_RESOURCE_URL);
495     }
496
497     private void verifyDistributionDataNotSetForExternalApiEvent(ExternalApiEvent event) {
498         assertThat(event.getConsumerId()).isNull();
499         assertThat(event.getResourceURL()).isNull();
500     }
501
502     private void verifyCommonDataForExternalApiEvent(ExternalApiEvent event, boolean isSucceeded) {
503         if (isSucceeded) {
504             assertThat(event.getDesc()).isEqualTo(DESCRIPTION);
505             assertThat(event.getStatus()).isEqualTo(STATUS_OK);
506             assertThat(event.getServiceInstanceId()).isEqualTo(SERVICE_INSTANCE_ID);
507         }
508         else {
509             assertThat(event.getDesc()).isEqualTo(DESC_ERROR);
510             assertThat(event.getStatus()).isEqualTo(STATUS_500);
511             assertThat(event.getServiceInstanceId()).isNullOrEmpty();
512         }
513         assertThat(event.getRequestId()).isEqualTo(REQUEST_ID);
514     }
515
516     private void verifyCurrentResourceVersionInfoForExternalApiEvent(ExternalApiEvent event, boolean isNull) {
517         assertThat(event.getCurrArtifactUuid()).isNull();
518         if (isNull) {
519             assertThat(event.getCurrState()).isNull();
520             assertThat(event.getCurrVersion()).isNull();
521         }
522         else {
523             assertThat(event.getCurrState()).isEqualTo(LifecycleStateEnum.CERTIFIED.name());
524             assertThat(event.getCurrVersion()).isEqualTo(CURRENT_VERSION);
525         }
526     }
527
528     private void verifyPreviousResourceVersionInfoForExternalApiEvent(ExternalApiEvent event, boolean isNull) {
529         assertThat(event.getPrevArtifactUuid()).isNull();
530         if (isNull) {
531             assertThat(event.getPrevState()).isNull();
532             assertThat(event.getPrevVersion()).isNull();
533         }
534         else {
535             assertThat(event.getPrevVersion()).isEqualTo(PREV_RESOURCE_VERSION);
536         }
537     }
538
539     @Test
540     public void auditExternalCreateResourceEventWhenResourceObjectIsNull() {
541         when(responseFormat.getStatus()).thenReturn(Integer.valueOf(STATUS_500));
542         when(responseFormat.getFormattedMessage()).thenReturn(DESC_ERROR);
543
544         when(request.getHeader(Constants.USER_ID_HEADER)).thenReturn(USER_ID);
545         when(request.getHeader(Constants.X_ECOMP_INSTANCE_ID_HEADER)).thenReturn(DIST_CONSUMER_ID);
546         when(request.getHeader(Constants.X_ECOMP_REQUEST_ID_HEADER)).thenReturn(REQUEST_ID);
547         when(request.getRequestURI()).thenReturn(DIST_RESOURCE_URL);
548
549         utils.auditCreateResourceExternalApi(responseFormat, new ResourceCommonInfo(RESOURCE_NAME, ComponentTypeEnum.RESOURCE.getValue()),
550                 request, null);
551
552         verify(manager).auditEvent(factoryCaptor.capture());
553         AuditCreateResourceExternalApiEventFactory factory = (AuditCreateResourceExternalApiEventFactory)factoryCaptor.getValue();
554
555         ExternalApiEvent event = (ExternalApiEvent)factory.getDbEvent();
556         verifyCommonDataForExternalApiEvent(event, false);
557         verifyPreviousResourceVersionInfoForExternalApiEvent(event, true);
558         verifyCurrentResourceVersionInfoForExternalApiEvent(event, true);
559         verifyDistributionDataForExternalApiEvent(event);
560         assertThat(event.getModifier()).isEqualTo("(" + USER_ID + ")");
561         assertThat(event.getInvariantUuid()).isNull();
562         assertThat(event.getResourceName()).isEqualTo(RESOURCE_NAME);
563         assertThat(event.getResourceType()).isEqualTo(ComponentTypeEnum.RESOURCE.getValue());
564     }
565
566     @Test
567     public void auditExternalCreateResourceEventWhenResourceObjectIsNullAndRequestDataIsNotProvided() {
568         when(responseFormat.getStatus()).thenReturn(Integer.valueOf(STATUS_500));
569         when(responseFormat.getFormattedMessage()).thenReturn(DESC_ERROR);
570
571         when(request.getHeader(Constants.USER_ID_HEADER)).thenReturn(null);
572         when(request.getHeader(Constants.X_ECOMP_INSTANCE_ID_HEADER)).thenReturn(null);
573         when(request.getHeader(Constants.X_ECOMP_REQUEST_ID_HEADER)).thenReturn(REQUEST_ID);
574         when(request.getRequestURI()).thenReturn(null);
575
576         utils.auditCreateResourceExternalApi(responseFormat, new ResourceCommonInfo(ComponentTypeEnum.RESOURCE.getValue()),
577                 request, null);
578
579         verify(manager).auditEvent(factoryCaptor.capture());
580         AuditCreateResourceExternalApiEventFactory factory = (AuditCreateResourceExternalApiEventFactory)factoryCaptor.getValue();
581
582         ExternalApiEvent event = (ExternalApiEvent)factory.getDbEvent();
583         verifyCommonDataForExternalApiEvent(event, false);
584         verifyPreviousResourceVersionInfoForExternalApiEvent(event, true);
585         verifyCurrentResourceVersionInfoForExternalApiEvent(event, true);
586         verifyDistributionDataNotSetForExternalApiEvent(event);
587         assertThat(event.getModifier()).isEmpty();
588         assertThat(event.getInvariantUuid()).isNull();
589         assertThat(event.getResourceName()).isNull();
590         assertThat(event.getResourceType()).isEqualTo(ComponentTypeEnum.RESOURCE.getValue());
591     }
592
593     @Test
594     public void auditExternalCreateResourceEventWhenResourceObjectAndRequestDataProvided() {
595         when(responseFormat.getStatus()).thenReturn(Integer.valueOf(STATUS_OK));
596         when(responseFormat.getFormattedMessage()).thenReturn(DESCRIPTION);
597
598         when(request.getHeader(Constants.USER_ID_HEADER)).thenReturn(USER_ID);
599         when(request.getHeader(Constants.X_ECOMP_INSTANCE_ID_HEADER)).thenReturn(DIST_CONSUMER_ID);
600         when(request.getHeader(Constants.X_ECOMP_REQUEST_ID_HEADER)).thenReturn(REQUEST_ID);
601         when(request.getRequestURI()).thenReturn(DIST_RESOURCE_URL);
602
603         Resource resource = new Resource();
604         resource.setName(RESOURCE_NAME);
605         resource.setInvariantUUID(INVARIANT_UUID);
606         resource.setUUID(SERVICE_INSTANCE_ID);
607
608         utils.auditCreateResourceExternalApi(responseFormat, new ResourceCommonInfo(ComponentTypeEnum.RESOURCE.getValue()),
609                 request, resource);
610
611         verify(manager).auditEvent(factoryCaptor.capture());
612         AuditCreateResourceExternalApiEventFactory factory = (AuditCreateResourceExternalApiEventFactory)factoryCaptor.getValue();
613
614         ExternalApiEvent event = (ExternalApiEvent)factory.getDbEvent();
615         verifyCommonDataForExternalApiEvent(event, true);
616         verifyPreviousResourceVersionInfoForExternalApiEvent(event, true);
617         verifyDistributionDataForExternalApiEvent(event);
618         assertThat(event.getCurrArtifactUuid()).isNull();
619         assertThat(event.getCurrState()).isEqualTo(LifecycleStateEnum.NOT_CERTIFIED_CHECKOUT.name());
620         assertThat(event.getCurrVersion()).isEqualTo(ImportUtils.Constants.FIRST_NON_CERTIFIED_VERSION);
621         assertThat(event.getModifier()).isEqualTo("(" + USER_ID + ")");
622         assertThat(event.getInvariantUuid()).isEqualTo(INVARIANT_UUID);
623         assertThat(event.getResourceName()).isEqualTo(RESOURCE_NAME);
624         assertThat(event.getResourceType()).isEqualTo(ComponentTypeEnum.RESOURCE.getValue());
625     }
626
627     @Test
628     public void auditExternalCreateServiceEventWhenResourceObjectIsNull() {
629         when(responseFormat.getStatus()).thenReturn(Integer.valueOf(STATUS_500));
630         when(responseFormat.getFormattedMessage()).thenReturn(DESC_ERROR);
631
632         when(request.getHeader(Constants.USER_ID_HEADER)).thenReturn(USER_ID);
633         when(request.getHeader(Constants.X_ECOMP_INSTANCE_ID_HEADER)).thenReturn(DIST_CONSUMER_ID);
634         when(request.getHeader(Constants.X_ECOMP_REQUEST_ID_HEADER)).thenReturn(REQUEST_ID);
635         when(request.getRequestURI()).thenReturn(DIST_RESOURCE_URL);
636
637         utils.auditCreateServiceExternalApi(responseFormat, request, null);
638
639         verify(manager).auditEvent(factoryCaptor.capture());
640         AuditCreateServiceExternalApiEventFactory factory = (AuditCreateServiceExternalApiEventFactory)factoryCaptor.getValue();
641
642         ExternalApiEvent event = (ExternalApiEvent)factory.getDbEvent();
643         verifyCommonDataForExternalApiEvent(event, false);
644         verifyPreviousResourceVersionInfoForExternalApiEvent(event, true);
645         verifyCurrentResourceVersionInfoForExternalApiEvent(event, true);
646         verifyDistributionDataForExternalApiEvent(event);
647         assertThat(event.getModifier()).isEqualTo("(" + USER_ID + ")");
648         assertThat(event.getInvariantUuid()).isNull();
649     }
650
651     @Test
652     public void auditExternalCreateServiceEventWhenResourceObjectIsNullAndRequestDataIsNotProvided() {
653         when(responseFormat.getStatus()).thenReturn(Integer.valueOf(STATUS_500));
654         when(responseFormat.getFormattedMessage()).thenReturn(DESC_ERROR);
655
656         when(request.getHeader(Constants.USER_ID_HEADER)).thenReturn(null);
657         when(request.getHeader(Constants.X_ECOMP_INSTANCE_ID_HEADER)).thenReturn(null);
658         when(request.getHeader(Constants.X_ECOMP_REQUEST_ID_HEADER)).thenReturn(REQUEST_ID);
659         when(request.getRequestURI()).thenReturn(null);
660
661         utils.auditCreateServiceExternalApi(responseFormat, request, null);
662
663         verify(manager).auditEvent(factoryCaptor.capture());
664         AuditCreateServiceExternalApiEventFactory factory = (AuditCreateServiceExternalApiEventFactory)factoryCaptor.getValue();
665
666         ExternalApiEvent event = (ExternalApiEvent)factory.getDbEvent();
667         verifyCommonDataForExternalApiEvent(event, false);
668         verifyPreviousResourceVersionInfoForExternalApiEvent(event, true);
669         verifyCurrentResourceVersionInfoForExternalApiEvent(event, true);
670         verifyDistributionDataNotSetForExternalApiEvent(event);
671         assertThat(event.getModifier()).isEmpty();
672         assertThat(event.getInvariantUuid()).isNull();
673     }
674
675     @Test
676     public void auditExternalCreateServiceEventWhenResourceObjectAndRequestDataProvided() {
677         when(responseFormat.getStatus()).thenReturn(Integer.valueOf(STATUS_OK));
678         when(responseFormat.getFormattedMessage()).thenReturn(DESCRIPTION);
679
680         when(request.getHeader(Constants.USER_ID_HEADER)).thenReturn(USER_ID);
681         when(request.getHeader(Constants.X_ECOMP_INSTANCE_ID_HEADER)).thenReturn(DIST_CONSUMER_ID);
682         when(request.getHeader(Constants.X_ECOMP_REQUEST_ID_HEADER)).thenReturn(REQUEST_ID);
683         when(request.getRequestURI()).thenReturn(DIST_RESOURCE_URL);
684
685         Service service = new Service();
686         service.setInvariantUUID(INVARIANT_UUID);
687         service.setUUID(SERVICE_INSTANCE_ID);
688
689         utils.auditCreateServiceExternalApi(responseFormat, request, service);
690
691         verify(manager).auditEvent(factoryCaptor.capture());
692         AuditCreateServiceExternalApiEventFactory factory = (AuditCreateServiceExternalApiEventFactory)factoryCaptor.getValue();
693
694         ExternalApiEvent event = (ExternalApiEvent)factory.getDbEvent();
695         verifyCommonDataForExternalApiEvent(event, true);
696         verifyPreviousResourceVersionInfoForExternalApiEvent(event, true);
697         verifyDistributionDataForExternalApiEvent(event);
698         assertThat(event.getCurrArtifactUuid()).isNull();
699         assertThat(event.getModifier()).isEqualTo("(" + USER_ID + ")");
700         assertThat(event.getInvariantUuid()).isEqualTo(INVARIANT_UUID);
701     }
702
703     @Test
704     public void checkIfAuditEventIsExternal() {
705         assertThat(utils.isExternalApiEvent(AuditingActionEnum.ARTIFACT_UPLOAD_BY_API)).isTrue();
706         assertThat(utils.isExternalApiEvent(AuditingActionEnum.ARTIFACT_UPLOAD)).isFalse();
707     }
708
709 }