2 * ============LICENSE_START=======================================================
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
11 * http://www.apache.org/licenses/LICENSE-2.0
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 * ================================================================================
22 package org.openecomp.sdc.be.auditing.impl.externalapi;
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;
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.*;
55 @RunWith(MockitoJUnitRunner.class)
56 public class AuditExternalApiEventFuncTest {
58 private AuditingManager auditingManager;
61 private static AuditCassandraDao cassandraDao;
63 private static AuditingDao auditingDao;
65 private static Configuration.ElasticSearchConfig esConfig;
68 private ArgumentCaptor<ExternalApiEvent> eventCaptor;
73 auditingManager = new AuditingManager(auditingDao, cassandraDao, new TestConfigurationProvider());
77 public void testActivateServiceEvent() {
78 AuditEventFactory builder = new AuditActivateServiceExternalApiEventFactory(
79 CommonAuditData.newBuilder()
80 .description(DESCRIPTION)
82 .requestId(REQUEST_ID)
83 .serviceInstanceId(SERVICE_INSTANCE_ID)
85 new ResourceCommonInfo(RESOURCE_NAME, RESOURCE_TYPE),
86 new DistributionData(DIST_CONSUMER_ID, DIST_RESOURCE_URL),
87 INVARIANT_UUID, modifier);
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);
93 assertThat(auditingManager.auditEvent(builder)).isEqualTo(EXPECTED_ACTIVATE_SERVICE_API_LOG_STR);
94 verifyExternalApiEventWithoutVersionInfo(AuditingActionEnum.ACTIVATE_SERVICE_BY_API.getName());
99 public void testDownloadArtifactEvent() {
100 AuditEventFactory builder = new AuditDownloadArtifactExternalApiEventFactory(
101 CommonAuditData.newBuilder()
102 .description(DESCRIPTION)
104 .requestId(REQUEST_ID)
105 .serviceInstanceId(SERVICE_INSTANCE_ID)
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)
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);
120 assertThat(auditingManager.auditEvent(builder)).isEqualTo(EXPECTED_DOWNLOAD_ARTIFACT_EXTERNAL_API_LOG_STR);
121 verifyExternalApiEvent(AuditingActionEnum.DOWNLOAD_ARTIFACT.getName());
125 public void testChangeLifecycleEvent() {
126 AuditEventFactory factory = new AuditChangeLifecycleExternalApiEventFactory(
127 CommonAuditData.newBuilder()
128 .description(DESCRIPTION)
130 .requestId(REQUEST_ID)
131 .serviceInstanceId(SERVICE_INSTANCE_ID)
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)
140 ResourceVersionInfo.newBuilder()
141 .artifactUuid(ARTIFACT_UUID)
142 .state(CURRENT_STATE)
143 .version(CURRENT_VERSION)
145 INVARIANT_UUID, modifier);
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);
151 assertThat(auditingManager.auditEvent(factory)).isEqualTo(EXPECTED_CHANGE_LIFECYCLE_EXTERNAL_API_LOG_STR);
152 verifyExternalApiEvent(AuditingActionEnum.CHANGE_LIFECYCLE_BY_API.getName());
156 public void testDeleteArtifactEvent() {
157 AuditEventFactory factory = new AuditCrudExternalApiArtifactEventFactory(
158 AuditingActionEnum.ARTIFACT_DELETE_BY_API,
159 CommonAuditData.newBuilder()
160 .description(DESCRIPTION)
162 .requestId(REQUEST_ID)
163 .serviceInstanceId(SERVICE_INSTANCE_ID)
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)
172 ResourceVersionInfo.newBuilder()
173 .artifactUuid(ARTIFACT_UUID)
174 .state(CURRENT_STATE)
175 .version(CURRENT_VERSION)
177 INVARIANT_UUID, modifier, ARTIFACT_DATA);
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);
183 assertThat(auditingManager.auditEvent(factory)).isEqualTo(EXPECTED_DELETE_ARTIFACT_EXTERNAL_API_LOG_STR);
184 verifyExternalApiEvent(AuditingActionEnum.ARTIFACT_DELETE_BY_API.getName());
188 public void testGetAssetEvent() {
189 AuditEventFactory factory = new AuditAssetExternalApiEventFactory(AuditingActionEnum.GET_ASSET_METADATA,
190 CommonAuditData.newBuilder()
192 .description(DESCRIPTION)
193 .requestId(REQUEST_ID)
194 .serviceInstanceId(SERVICE_INSTANCE_ID)
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);
202 assertThat(auditingManager.auditEvent(factory)).isEqualTo(EXPECTED_EXTERNAL_ASSET_LOG_STR);
203 verifyExternalApiEventWithoutVersionInfo(AuditingActionEnum.GET_ASSET_METADATA.getName());
208 public void testGetAssetsListEvent() {
209 AuditEventFactory factory = new AuditAssetListExternalApiEventFactory(
210 AuditingActionEnum.GET_ASSET_LIST,
211 CommonAuditData.newBuilder()
212 .description(DESCRIPTION)
214 .requestId(REQUEST_ID)
215 .serviceInstanceId(SERVICE_INSTANCE_ID)
217 new DistributionData(DIST_CONSUMER_ID, DIST_RESOURCE_URL));
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);
223 assertThat(auditingManager.auditEvent(factory)).isEqualTo(EXPECTED_GET_ASSET_LIST_LOG_STR);
224 verifyExternalApiEventWithoutResourceInfo(AuditingActionEnum.GET_ASSET_LIST.getName());
228 public void testGetToscaModelEvent() {
229 AuditEventFactory factory = new AuditAssetExternalApiEventFactory(
230 AuditingActionEnum.GET_TOSCA_MODEL,
231 CommonAuditData.newBuilder()
232 .description(DESCRIPTION)
234 .requestId(REQUEST_ID)
235 .serviceInstanceId(SERVICE_INSTANCE_ID)
237 new ResourceCommonInfo(RESOURCE_NAME, RESOURCE_TYPE),
238 new DistributionData(DIST_CONSUMER_ID, DIST_RESOURCE_URL));
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);
244 assertThat(auditingManager.auditEvent(factory)).isEqualTo(EXPECTED_GET_TOSCA_MODEL_LOG_STR);
245 verifyExternalApiEventWithoutVersionInfo(AuditingActionEnum.GET_TOSCA_MODEL.getName());
249 public void testCreateResourceEvent() {
250 AuditEventFactory factory = new AuditCreateResourceExternalApiEventFactory(
251 CommonAuditData.newBuilder()
252 .description(DESCRIPTION)
254 .requestId(REQUEST_ID)
255 .serviceInstanceId(SERVICE_INSTANCE_ID)
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)
264 INVARIANT_UUID, modifier);
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);
270 assertThat(auditingManager.auditEvent(factory)).isEqualTo(EXPECTED_EXTERNAL_CREATE_RESOURCE_LOG_STR);
271 verifyExternalApiEvent(AuditingActionEnum.CREATE_RESOURCE_BY_API.getName());
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);
283 assertThat(storedEvent.getResourceName()).isEqualTo(RESOURCE_NAME);
284 assertThat(storedEvent.getResourceType()).isEqualTo(RESOURCE_TYPE);
285 assertThat(storedEvent.getCurrArtifactUuid()).isEqualTo(ARTIFACT_UUID);
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);
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();
305 assertThat(storedEvent.getInvariantUuid()).isEqualTo(INVARIANT_UUID);
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);
314 assertThat(storedEvent.getArtifactData()).isNull();
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);
327 assertThat(storedEvent.getModifier()).isEmpty();
328 assertThat(storedEvent.getInvariantUuid()).isNull();
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();
342 assertThat(storedEvent.getResourceName()).isEqualTo(RESOURCE_NAME);
343 assertThat(storedEvent.getResourceType()).isEqualTo(RESOURCE_TYPE);
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();