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.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;
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;
76 @RunWith(MockitoJUnitRunner.class)
77 public class AuditExternalApiEventFuncTest {
79 private AuditingManager auditingManager;
82 private static AuditCassandraDao cassandraDao;
85 private ArgumentCaptor<ExternalApiEvent> eventCaptor;
90 auditingManager = new AuditingManager(cassandraDao, new TestConfigurationProvider());
94 public void testActivateServiceEvent() {
95 AuditEventFactory builder = new AuditActivateServiceExternalApiEventFactory(
96 CommonAuditData.newBuilder()
97 .description(DESCRIPTION)
99 .requestId(REQUEST_ID)
100 .serviceInstanceId(SERVICE_INSTANCE_ID)
102 new ResourceCommonInfo(RESOURCE_NAME, RESOURCE_TYPE),
103 new DistributionData(DIST_CONSUMER_ID, DIST_RESOURCE_URL),
104 INVARIANT_UUID, modifier);
106 when(cassandraDao.saveRecord(any(AuditingGenericEvent.class))).thenReturn(CassandraOperationStatus.OK);
108 assertThat(auditingManager.auditEvent(builder)).isEqualTo(EXPECTED_ACTIVATE_SERVICE_API_LOG_STR);
109 verifyExternalApiEventWithoutVersionInfo(AuditingActionEnum.ACTIVATE_SERVICE_BY_API.getName());
114 public void testDownloadArtifactEvent() {
115 AuditEventFactory builder = new AuditDownloadArtifactExternalApiEventFactory(
116 CommonAuditData.newBuilder()
117 .description(DESCRIPTION)
119 .requestId(REQUEST_ID)
120 .serviceInstanceId(SERVICE_INSTANCE_ID)
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)
131 when(cassandraDao.saveRecord(any(AuditingGenericEvent.class))).thenReturn(CassandraOperationStatus.OK);
133 assertThat(auditingManager.auditEvent(builder)).isEqualTo(EXPECTED_DOWNLOAD_ARTIFACT_EXTERNAL_API_LOG_STR);
134 verifyExternalApiEvent(AuditingActionEnum.DOWNLOAD_ARTIFACT.getName());
138 public void testChangeLifecycleEvent() {
139 AuditEventFactory factory = new AuditChangeLifecycleExternalApiEventFactory(
140 CommonAuditData.newBuilder()
141 .description(DESCRIPTION)
143 .requestId(REQUEST_ID)
144 .serviceInstanceId(SERVICE_INSTANCE_ID)
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)
153 ResourceVersionInfo.newBuilder()
154 .artifactUuid(ARTIFACT_UUID)
155 .state(CURRENT_STATE)
156 .version(CURRENT_VERSION)
158 INVARIANT_UUID, modifier);
160 when(cassandraDao.saveRecord(any(AuditingGenericEvent.class))).thenReturn(CassandraOperationStatus.OK);
162 assertThat(auditingManager.auditEvent(factory)).isEqualTo(EXPECTED_CHANGE_LIFECYCLE_EXTERNAL_API_LOG_STR);
163 verifyExternalApiEvent(AuditingActionEnum.CHANGE_LIFECYCLE_BY_API.getName());
167 public void testDeleteArtifactEvent() {
168 AuditEventFactory factory = new AuditCrudExternalApiArtifactEventFactory(
169 AuditingActionEnum.ARTIFACT_DELETE_BY_API,
170 CommonAuditData.newBuilder()
171 .description(DESCRIPTION)
173 .requestId(REQUEST_ID)
174 .serviceInstanceId(SERVICE_INSTANCE_ID)
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)
183 ResourceVersionInfo.newBuilder()
184 .artifactUuid(ARTIFACT_UUID)
185 .state(CURRENT_STATE)
186 .version(CURRENT_VERSION)
188 INVARIANT_UUID, modifier, ARTIFACT_DATA);
190 when(cassandraDao.saveRecord(any(AuditingGenericEvent.class))).thenReturn(CassandraOperationStatus.OK);
192 assertThat(auditingManager.auditEvent(factory)).isEqualTo(EXPECTED_DELETE_ARTIFACT_EXTERNAL_API_LOG_STR);
193 verifyExternalApiEvent(AuditingActionEnum.ARTIFACT_DELETE_BY_API.getName());
197 public void testGetAssetEvent() {
198 AuditEventFactory factory = new AuditAssetExternalApiEventFactory(AuditingActionEnum.GET_ASSET_METADATA,
199 CommonAuditData.newBuilder()
201 .description(DESCRIPTION)
202 .requestId(REQUEST_ID)
203 .serviceInstanceId(SERVICE_INSTANCE_ID)
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);
209 assertThat(auditingManager.auditEvent(factory)).isEqualTo(EXPECTED_EXTERNAL_ASSET_LOG_STR);
210 verifyExternalApiEventWithoutVersionInfo(AuditingActionEnum.GET_ASSET_METADATA.getName());
215 public void testGetAssetsListEvent() {
216 AuditEventFactory factory = new AuditAssetListExternalApiEventFactory(
217 AuditingActionEnum.GET_ASSET_LIST,
218 CommonAuditData.newBuilder()
219 .description(DESCRIPTION)
221 .requestId(REQUEST_ID)
222 .serviceInstanceId(SERVICE_INSTANCE_ID)
224 new DistributionData(DIST_CONSUMER_ID, DIST_RESOURCE_URL));
226 when(cassandraDao.saveRecord(any(AuditingGenericEvent.class))).thenReturn(CassandraOperationStatus.OK);
228 assertThat(auditingManager.auditEvent(factory)).isEqualTo(EXPECTED_GET_ASSET_LIST_LOG_STR);
229 verifyExternalApiEventWithoutResourceInfo(AuditingActionEnum.GET_ASSET_LIST.getName());
233 public void testGetToscaModelEvent() {
234 AuditEventFactory factory = new AuditAssetExternalApiEventFactory(
235 AuditingActionEnum.GET_TOSCA_MODEL,
236 CommonAuditData.newBuilder()
237 .description(DESCRIPTION)
239 .requestId(REQUEST_ID)
240 .serviceInstanceId(SERVICE_INSTANCE_ID)
242 new ResourceCommonInfo(RESOURCE_NAME, RESOURCE_TYPE),
243 new DistributionData(DIST_CONSUMER_ID, DIST_RESOURCE_URL));
245 when(cassandraDao.saveRecord(any(AuditingGenericEvent.class))).thenReturn(CassandraOperationStatus.OK);
247 assertThat(auditingManager.auditEvent(factory)).isEqualTo(EXPECTED_GET_TOSCA_MODEL_LOG_STR);
248 verifyExternalApiEventWithoutVersionInfo(AuditingActionEnum.GET_TOSCA_MODEL.getName());
252 public void testCreateResourceEvent() {
253 AuditEventFactory factory = new AuditCreateResourceExternalApiEventFactory(
254 CommonAuditData.newBuilder()
255 .description(DESCRIPTION)
257 .requestId(REQUEST_ID)
258 .serviceInstanceId(SERVICE_INSTANCE_ID)
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)
267 INVARIANT_UUID, modifier);
269 when(cassandraDao.saveRecord(any(AuditingGenericEvent.class))).thenReturn(CassandraOperationStatus.OK);
271 assertThat(auditingManager.auditEvent(factory)).isEqualTo(EXPECTED_EXTERNAL_CREATE_RESOURCE_LOG_STR);
272 verifyExternalApiEvent(AuditingActionEnum.CREATE_RESOURCE_BY_API.getName());
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);
284 assertThat(storedEvent.getResourceName()).isEqualTo(RESOURCE_NAME);
285 assertThat(storedEvent.getResourceType()).isEqualTo(RESOURCE_TYPE);
286 assertThat(storedEvent.getCurrArtifactUuid()).isEqualTo(ARTIFACT_UUID);
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);
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();
306 assertThat(storedEvent.getInvariantUuid()).isEqualTo(INVARIANT_UUID);
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);
315 assertThat(storedEvent.getArtifactData()).isNull();
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);
328 assertThat(storedEvent.getModifier()).isEmpty();
329 assertThat(storedEvent.getInvariantUuid()).isNull();
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();
343 assertThat(storedEvent.getResourceName()).isEqualTo(RESOURCE_NAME);
344 assertThat(storedEvent.getResourceType()).isEqualTo(RESOURCE_TYPE);
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();