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