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