Catalog alignment
[sdc.git] / catalog-be / src / test / java / org / openecomp / sdc / be / auditing / impl / externalapi / AuditExternalApiEventFuncTest.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * SDC
4  * ================================================================================
5  * Copyright (C) 2017 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  * Modifications copyright (c) 2019 Nokia
20  * ================================================================================
21  */
22 package org.openecomp.sdc.be.auditing.impl.externalapi;
23
24 import org.junit.Before;
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.Mock;
30 import org.mockito.junit.MockitoJUnitRunner;
31 import org.openecomp.sdc.be.auditing.api.AuditEventFactory;
32 import org.openecomp.sdc.be.auditing.impl.AuditingManager;
33 import org.openecomp.sdc.be.dao.cassandra.AuditCassandraDao;
34 import org.openecomp.sdc.be.dao.cassandra.CassandraOperationStatus;
35 import org.openecomp.sdc.be.resources.data.auditing.AuditingActionEnum;
36 import org.openecomp.sdc.be.resources.data.auditing.AuditingGenericEvent;
37 import org.openecomp.sdc.be.resources.data.auditing.ExternalApiEvent;
38 import org.openecomp.sdc.be.resources.data.auditing.model.CommonAuditData;
39 import org.openecomp.sdc.be.resources.data.auditing.model.DistributionData;
40 import org.openecomp.sdc.be.resources.data.auditing.model.ResourceCommonInfo;
41 import org.openecomp.sdc.be.resources.data.auditing.model.ResourceVersionInfo;
42 import org.openecomp.sdc.test.utils.TestConfigurationProvider;
43
44 import static org.assertj.core.api.Assertions.assertThat;
45 import static org.mockito.ArgumentMatchers.any;
46 import static org.mockito.Mockito.verify;
47 import static org.mockito.Mockito.when;
48 import static org.openecomp.sdc.be.auditing.impl.AuditTestUtils.ARTIFACT_DATA;
49 import static org.openecomp.sdc.be.auditing.impl.AuditTestUtils.ARTIFACT_UUID;
50 import static org.openecomp.sdc.be.auditing.impl.AuditTestUtils.CURRENT_STATE;
51 import static org.openecomp.sdc.be.auditing.impl.AuditTestUtils.CURRENT_VERSION;
52 import static org.openecomp.sdc.be.auditing.impl.AuditTestUtils.DESCRIPTION;
53 import static org.openecomp.sdc.be.auditing.impl.AuditTestUtils.DIST_CONSUMER_ID;
54 import static org.openecomp.sdc.be.auditing.impl.AuditTestUtils.DIST_RESOURCE_URL;
55 import static org.openecomp.sdc.be.auditing.impl.AuditTestUtils.EXPECTED_ACTIVATE_SERVICE_API_LOG_STR;
56 import static org.openecomp.sdc.be.auditing.impl.AuditTestUtils.EXPECTED_CHANGE_LIFECYCLE_EXTERNAL_API_LOG_STR;
57 import static org.openecomp.sdc.be.auditing.impl.AuditTestUtils.EXPECTED_DELETE_ARTIFACT_EXTERNAL_API_LOG_STR;
58 import static org.openecomp.sdc.be.auditing.impl.AuditTestUtils.EXPECTED_DOWNLOAD_ARTIFACT_EXTERNAL_API_LOG_STR;
59 import static org.openecomp.sdc.be.auditing.impl.AuditTestUtils.EXPECTED_EXTERNAL_ASSET_LOG_STR;
60 import static org.openecomp.sdc.be.auditing.impl.AuditTestUtils.EXPECTED_EXTERNAL_CREATE_RESOURCE_LOG_STR;
61 import static org.openecomp.sdc.be.auditing.impl.AuditTestUtils.EXPECTED_GET_ASSET_LIST_LOG_STR;
62 import static org.openecomp.sdc.be.auditing.impl.AuditTestUtils.EXPECTED_GET_TOSCA_MODEL_LOG_STR;
63 import static org.openecomp.sdc.be.auditing.impl.AuditTestUtils.INVARIANT_UUID;
64 import static org.openecomp.sdc.be.auditing.impl.AuditTestUtils.MODIFIER_UID;
65 import static org.openecomp.sdc.be.auditing.impl.AuditTestUtils.PREV_RESOURCE_STATE;
66 import static org.openecomp.sdc.be.auditing.impl.AuditTestUtils.PREV_RESOURCE_VERSION;
67 import static org.openecomp.sdc.be.auditing.impl.AuditTestUtils.REQUEST_ID;
68 import static org.openecomp.sdc.be.auditing.impl.AuditTestUtils.RESOURCE_NAME;
69 import static org.openecomp.sdc.be.auditing.impl.AuditTestUtils.RESOURCE_TYPE;
70 import static org.openecomp.sdc.be.auditing.impl.AuditTestUtils.SERVICE_INSTANCE_ID;
71 import static org.openecomp.sdc.be.auditing.impl.AuditTestUtils.STATUS_OK;
72 import static org.openecomp.sdc.be.auditing.impl.AuditTestUtils.init;
73 import static org.openecomp.sdc.be.auditing.impl.AuditTestUtils.modifier;
74
75
76 @RunWith(MockitoJUnitRunner.class)
77 public class AuditExternalApiEventFuncTest {
78
79     private AuditingManager auditingManager;
80
81     @Mock
82     private static AuditCassandraDao cassandraDao;
83
84     @Captor
85     private ArgumentCaptor<ExternalApiEvent> eventCaptor;
86
87     @Before
88     public void setUp() {
89         init();
90         auditingManager = new AuditingManager(cassandraDao, new TestConfigurationProvider());
91     }
92
93     @Test
94     public void testActivateServiceEvent() {
95         AuditEventFactory builder = new AuditActivateServiceExternalApiEventFactory(
96                 CommonAuditData.newBuilder()
97                         .description(DESCRIPTION)
98                         .status(STATUS_OK)
99                         .requestId(REQUEST_ID)
100                         .serviceInstanceId(SERVICE_INSTANCE_ID)
101                         .build(),
102                 new ResourceCommonInfo(RESOURCE_NAME, RESOURCE_TYPE),
103                 new DistributionData(DIST_CONSUMER_ID, DIST_RESOURCE_URL),
104                 INVARIANT_UUID, modifier);
105
106         when(cassandraDao.saveRecord(any(AuditingGenericEvent.class))).thenReturn(CassandraOperationStatus.OK);
107
108         assertThat(auditingManager.auditEvent(builder)).isEqualTo(EXPECTED_ACTIVATE_SERVICE_API_LOG_STR);
109         verifyExternalApiEventWithoutVersionInfo(AuditingActionEnum.ACTIVATE_SERVICE_BY_API.getName());
110
111     }
112
113     @Test
114     public void testDownloadArtifactEvent() {
115         AuditEventFactory builder = new AuditDownloadArtifactExternalApiEventFactory(
116                 CommonAuditData.newBuilder()
117                         .description(DESCRIPTION)
118                         .status(STATUS_OK)
119                         .requestId(REQUEST_ID)
120                         .serviceInstanceId(SERVICE_INSTANCE_ID)
121                         .build(),
122                 new ResourceCommonInfo(RESOURCE_NAME, RESOURCE_TYPE),
123                 new DistributionData(DIST_CONSUMER_ID, DIST_RESOURCE_URL),
124                 ResourceVersionInfo.newBuilder()
125                         .version(CURRENT_VERSION)
126                         .state(CURRENT_STATE)
127                         .artifactUuid(ARTIFACT_UUID)
128                         .build(),
129                 modifier);
130
131         when(cassandraDao.saveRecord(any(AuditingGenericEvent.class))).thenReturn(CassandraOperationStatus.OK);
132
133         assertThat(auditingManager.auditEvent(builder)).isEqualTo(EXPECTED_DOWNLOAD_ARTIFACT_EXTERNAL_API_LOG_STR);
134         verifyExternalApiEvent(AuditingActionEnum.DOWNLOAD_ARTIFACT.getName());
135     }
136
137     @Test
138     public void testChangeLifecycleEvent() {
139         AuditEventFactory factory = new AuditChangeLifecycleExternalApiEventFactory(
140                 CommonAuditData.newBuilder()
141                         .description(DESCRIPTION)
142                         .status(STATUS_OK)
143                         .requestId(REQUEST_ID)
144                         .serviceInstanceId(SERVICE_INSTANCE_ID)
145                         .build(),
146                 new ResourceCommonInfo(RESOURCE_NAME, RESOURCE_TYPE),
147                 new DistributionData(DIST_CONSUMER_ID, DIST_RESOURCE_URL),
148                 ResourceVersionInfo.newBuilder()
149                         .artifactUuid(ARTIFACT_UUID)
150                         .state(PREV_RESOURCE_STATE)
151                         .version(PREV_RESOURCE_VERSION)
152                         .build(),
153                 ResourceVersionInfo.newBuilder()
154                         .artifactUuid(ARTIFACT_UUID)
155                         .state(CURRENT_STATE)
156                         .version(CURRENT_VERSION)
157                         .build(),
158                 INVARIANT_UUID, modifier);
159
160         when(cassandraDao.saveRecord(any(AuditingGenericEvent.class))).thenReturn(CassandraOperationStatus.OK);
161
162         assertThat(auditingManager.auditEvent(factory)).isEqualTo(EXPECTED_CHANGE_LIFECYCLE_EXTERNAL_API_LOG_STR);
163         verifyExternalApiEvent(AuditingActionEnum.CHANGE_LIFECYCLE_BY_API.getName());
164     }
165
166     @Test
167     public void testDeleteArtifactEvent() {
168         AuditEventFactory factory = new AuditCrudExternalApiArtifactEventFactory(
169                 AuditingActionEnum.ARTIFACT_DELETE_BY_API,
170                 CommonAuditData.newBuilder()
171                         .description(DESCRIPTION)
172                         .status(STATUS_OK)
173                         .requestId(REQUEST_ID)
174                         .serviceInstanceId(SERVICE_INSTANCE_ID)
175                         .build(),
176                 new ResourceCommonInfo(RESOURCE_NAME, RESOURCE_TYPE),
177                 new DistributionData(DIST_CONSUMER_ID, DIST_RESOURCE_URL),
178                 ResourceVersionInfo.newBuilder()
179                         .artifactUuid(ARTIFACT_UUID)
180                         .state(PREV_RESOURCE_STATE)
181                         .version(PREV_RESOURCE_VERSION)
182                         .build(),
183                 ResourceVersionInfo.newBuilder()
184                         .artifactUuid(ARTIFACT_UUID)
185                         .state(CURRENT_STATE)
186                         .version(CURRENT_VERSION)
187                         .build(),
188                 INVARIANT_UUID, modifier, ARTIFACT_DATA);
189
190         when(cassandraDao.saveRecord(any(AuditingGenericEvent.class))).thenReturn(CassandraOperationStatus.OK);
191
192         assertThat(auditingManager.auditEvent(factory)).isEqualTo(EXPECTED_DELETE_ARTIFACT_EXTERNAL_API_LOG_STR);
193         verifyExternalApiEvent(AuditingActionEnum.ARTIFACT_DELETE_BY_API.getName());
194     }
195
196     @Test
197     public void testGetAssetEvent() {
198         AuditEventFactory factory = new AuditAssetExternalApiEventFactory(AuditingActionEnum.GET_ASSET_METADATA,
199                 CommonAuditData.newBuilder()
200                         .status(STATUS_OK)
201                         .description(DESCRIPTION)
202                         .requestId(REQUEST_ID)
203                         .serviceInstanceId(SERVICE_INSTANCE_ID)
204                         .build(),
205                 new ResourceCommonInfo(RESOURCE_NAME, RESOURCE_TYPE),
206                 new DistributionData(DIST_CONSUMER_ID, DIST_RESOURCE_URL));
207         when(cassandraDao.saveRecord(any(AuditingGenericEvent.class))).thenReturn(CassandraOperationStatus.OK);
208
209         assertThat(auditingManager.auditEvent(factory)).isEqualTo(EXPECTED_EXTERNAL_ASSET_LOG_STR);
210         verifyExternalApiEventWithoutVersionInfo(AuditingActionEnum.GET_ASSET_METADATA.getName());
211
212     }
213
214     @Test
215     public void testGetAssetsListEvent() {
216         AuditEventFactory factory = new AuditAssetListExternalApiEventFactory(
217                 AuditingActionEnum.GET_ASSET_LIST,
218                 CommonAuditData.newBuilder()
219                         .description(DESCRIPTION)
220                         .status(STATUS_OK)
221                         .requestId(REQUEST_ID)
222                         .serviceInstanceId(SERVICE_INSTANCE_ID)
223                         .build(),
224                 new DistributionData(DIST_CONSUMER_ID, DIST_RESOURCE_URL));
225
226         when(cassandraDao.saveRecord(any(AuditingGenericEvent.class))).thenReturn(CassandraOperationStatus.OK);
227
228         assertThat(auditingManager.auditEvent(factory)).isEqualTo(EXPECTED_GET_ASSET_LIST_LOG_STR);
229         verifyExternalApiEventWithoutResourceInfo(AuditingActionEnum.GET_ASSET_LIST.getName());
230     }
231
232     @Test
233     public void testGetToscaModelEvent() {
234         AuditEventFactory factory = new AuditAssetExternalApiEventFactory(
235                 AuditingActionEnum.GET_TOSCA_MODEL,
236                 CommonAuditData.newBuilder()
237                         .description(DESCRIPTION)
238                         .status(STATUS_OK)
239                         .requestId(REQUEST_ID)
240                         .serviceInstanceId(SERVICE_INSTANCE_ID)
241                         .build(),
242                 new ResourceCommonInfo(RESOURCE_NAME, RESOURCE_TYPE),
243                 new DistributionData(DIST_CONSUMER_ID, DIST_RESOURCE_URL));
244
245         when(cassandraDao.saveRecord(any(AuditingGenericEvent.class))).thenReturn(CassandraOperationStatus.OK);
246
247         assertThat(auditingManager.auditEvent(factory)).isEqualTo(EXPECTED_GET_TOSCA_MODEL_LOG_STR);
248         verifyExternalApiEventWithoutVersionInfo(AuditingActionEnum.GET_TOSCA_MODEL.getName());
249     }
250
251     @Test
252     public void testCreateResourceEvent() {
253         AuditEventFactory factory = new AuditCreateResourceExternalApiEventFactory(
254                 CommonAuditData.newBuilder()
255                         .description(DESCRIPTION)
256                         .status(STATUS_OK)
257                         .requestId(REQUEST_ID)
258                         .serviceInstanceId(SERVICE_INSTANCE_ID)
259                         .build(),
260                 new ResourceCommonInfo(RESOURCE_NAME, RESOURCE_TYPE),
261                 new DistributionData(DIST_CONSUMER_ID, DIST_RESOURCE_URL),
262                 ResourceVersionInfo.newBuilder()
263                         .artifactUuid(ARTIFACT_UUID)
264                         .state(CURRENT_STATE)
265                         .version(CURRENT_VERSION)
266                         .build(),
267                 INVARIANT_UUID, modifier);
268
269         when(cassandraDao.saveRecord(any(AuditingGenericEvent.class))).thenReturn(CassandraOperationStatus.OK);
270
271         assertThat(auditingManager.auditEvent(factory)).isEqualTo(EXPECTED_EXTERNAL_CREATE_RESOURCE_LOG_STR);
272         verifyExternalApiEvent(AuditingActionEnum.CREATE_RESOURCE_BY_API.getName());
273     }
274
275     private void verifyExternalApiEvent(String action) {
276         verify(cassandraDao).saveRecord(eventCaptor.capture());
277         ExternalApiEvent storedEvent = eventCaptor.getValue();
278         assertThat(storedEvent.getModifier()).isEqualTo(MODIFIER_UID);
279         assertThat(storedEvent.getDesc()).isEqualTo(DESCRIPTION);
280         assertThat(storedEvent.getStatus()).isEqualTo(STATUS_OK);
281         assertThat(storedEvent.getServiceInstanceId()).isEqualTo(SERVICE_INSTANCE_ID);
282         assertThat(storedEvent.getAction()).isEqualTo(action);
283
284         assertThat(storedEvent.getResourceName()).isEqualTo(RESOURCE_NAME);
285         assertThat(storedEvent.getResourceType()).isEqualTo(RESOURCE_TYPE);
286         assertThat(storedEvent.getCurrArtifactUuid()).isEqualTo(ARTIFACT_UUID);
287
288         if (!action.equals(AuditingActionEnum.DOWNLOAD_ARTIFACT.getName())
289             && !action.equals(AuditingActionEnum.CREATE_RESOURCE_BY_API.getName())) {
290             assertThat(storedEvent.getPrevArtifactUuid()).isEqualTo(ARTIFACT_UUID);
291             assertThat(storedEvent.getCurrVersion()).isEqualTo(CURRENT_VERSION);
292             assertThat(storedEvent.getCurrState()).isEqualTo(CURRENT_STATE);
293             assertThat(storedEvent.getPrevState()).isEqualTo(PREV_RESOURCE_STATE);
294             assertThat(storedEvent.getPrevVersion()).isEqualTo(PREV_RESOURCE_VERSION);
295         }
296         else {
297             assertThat(storedEvent.getPrevArtifactUuid()).isNull();
298             assertThat(storedEvent.getCurrVersion()).isEqualTo(CURRENT_VERSION);
299             assertThat(storedEvent.getCurrState()).isEqualTo(CURRENT_STATE);
300             assertThat(storedEvent.getPrevState()).isNull();
301             assertThat(storedEvent.getPrevVersion()).isNull();
302             if (action.equals(AuditingActionEnum.DOWNLOAD_ARTIFACT.getName())) {
303                 assertThat(storedEvent.getInvariantUuid()).isNull();
304             }
305             else {
306                 assertThat(storedEvent.getInvariantUuid()).isEqualTo(INVARIANT_UUID);
307             }
308         }
309         if (!action.equals(AuditingActionEnum.CHANGE_LIFECYCLE_BY_API.getName())
310                 && !action.equals(AuditingActionEnum.DOWNLOAD_ARTIFACT.getName())
311                 && !action.equals(AuditingActionEnum.CREATE_RESOURCE_BY_API.getName())) {
312             assertThat(storedEvent.getArtifactData()).isEqualTo(ARTIFACT_DATA);
313         }
314         else {
315             assertThat(storedEvent.getArtifactData()).isNull();
316         }
317
318     }
319
320     private void verifyExternalApiEventWithoutVersionInfo(String action) {
321         verify(cassandraDao).saveRecord(eventCaptor.capture());
322         ExternalApiEvent storedEvent = eventCaptor.getValue();
323         if (action.equals(AuditingActionEnum.ACTIVATE_SERVICE_BY_API.getName())) {
324             assertThat(storedEvent.getModifier()).isEqualTo(MODIFIER_UID);
325             assertThat(storedEvent.getInvariantUuid()).isEqualTo(INVARIANT_UUID);
326         }
327         else {
328             assertThat(storedEvent.getModifier()).isEmpty();
329             assertThat(storedEvent.getInvariantUuid()).isNull();
330         }
331         assertThat(storedEvent.getDesc()).isEqualTo(DESCRIPTION);
332         assertThat(storedEvent.getStatus()).isEqualTo(STATUS_OK);
333         assertThat(storedEvent.getServiceInstanceId()).isEqualTo(SERVICE_INSTANCE_ID);
334         assertThat(storedEvent.getAction()).isEqualTo(action);
335         assertThat(storedEvent.getArtifactData()).isNull();
336         assertThat(storedEvent.getCurrArtifactUuid()).isNull();
337         assertThat(storedEvent.getPrevArtifactUuid()).isNull();
338         assertThat(storedEvent.getPrevState()).isNull();
339         assertThat(storedEvent.getCurrState()).isNull();
340         assertThat(storedEvent.getPrevVersion()).isNull();
341         assertThat(storedEvent.getCurrVersion()).isNull();
342
343         assertThat(storedEvent.getResourceName()).isEqualTo(RESOURCE_NAME);
344         assertThat(storedEvent.getResourceType()).isEqualTo(RESOURCE_TYPE);
345
346     }
347
348     private void verifyExternalApiEventWithoutResourceInfo(String action) {
349         verify(cassandraDao).saveRecord(eventCaptor.capture());
350         ExternalApiEvent storedEvent = eventCaptor.getValue();
351         assertThat(storedEvent.getModifier()).isEmpty();
352         assertThat(storedEvent.getDesc()).isEqualTo(DESCRIPTION);
353         assertThat(storedEvent.getStatus()).isEqualTo(STATUS_OK);
354         assertThat(storedEvent.getServiceInstanceId()).isEqualTo(SERVICE_INSTANCE_ID);
355         assertThat(storedEvent.getAction()).isEqualTo(action);
356         assertThat(storedEvent.getArtifactData()).isNull();
357         assertThat(storedEvent.getCurrArtifactUuid()).isNull();
358         assertThat(storedEvent.getPrevArtifactUuid()).isNull();
359         assertThat(storedEvent.getPrevState()).isNull();
360         assertThat(storedEvent.getCurrState()).isNull();
361         assertThat(storedEvent.getPrevVersion()).isNull();
362         assertThat(storedEvent.getCurrVersion()).isNull();
363         assertThat(storedEvent.getInvariantUuid()).isNull();
364         assertThat(storedEvent.getResourceName()).isNull();
365         assertThat(storedEvent.getResourceType()).isNull();
366     }
367
368 }