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.resourceadmin;
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.model.LifecycleStateEnum;
39 import org.openecomp.sdc.be.model.Resource;
40 import org.openecomp.sdc.be.resources.data.auditing.AuditingActionEnum;
41 import org.openecomp.sdc.be.resources.data.auditing.AuditingGenericEvent;
42 import org.openecomp.sdc.be.resources.data.auditing.ResourceAdminEvent;
43 import org.openecomp.sdc.be.resources.data.auditing.model.CommonAuditData;
44 import org.openecomp.sdc.be.resources.data.auditing.model.ResourceCommonInfo;
45 import org.openecomp.sdc.be.resources.data.auditing.model.ResourceVersionInfo;
46 import org.openecomp.sdc.common.api.Constants;
47 import org.openecomp.sdc.common.util.ThreadLocalsHolder;
48 import org.openecomp.sdc.test.utils.TestConfigurationProvider;
50 import static org.assertj.core.api.Assertions.assertThat;
51 import static org.mockito.ArgumentMatchers.any;
52 import static org.mockito.ArgumentMatchers.eq;
53 import static org.mockito.Mockito.verify;
54 import static org.mockito.Mockito.when;
55 import static org.openecomp.sdc.be.auditing.impl.AuditTestUtils.*;
57 @RunWith(MockitoJUnitRunner.class)
58 public class AuditResourceAdminEventFuncTest {
59 private AuditingManager auditingManager;
62 private static AuditCassandraDao cassandraDao;
64 private static AuditingDao auditingDao;
66 private static Configuration.ElasticSearchConfig esConfig;
69 private ArgumentCaptor<ResourceAdminEvent> eventCaptor;
74 auditingManager = new AuditingManager(auditingDao, cassandraDao, new TestConfigurationProvider());
75 ThreadLocalsHolder.setUuid(REQUEST_ID);
79 public void testCheckInResourceAdminEvent() {
80 Resource resource = new Resource();
81 resource.setName(RESOURCE_NAME);
82 resource.setUUID(SERVICE_INSTANCE_ID);
83 resource.setInvariantUUID(INVARIANT_UUID);
84 resource.setLifecycleState(LifecycleStateEnum.CERTIFIED);
85 resource.setVersion(CURRENT_VERSION);
87 AuditEventFactory factory = new AuditCertificationResourceAdminEventFactory(
88 AuditingActionEnum.CHECKIN_RESOURCE,
89 CommonAuditData.newBuilder()
90 .description(DESCRIPTION)
92 .requestId(REQUEST_ID)
93 .serviceInstanceId(SERVICE_INSTANCE_ID)
95 new ResourceCommonInfo(RESOURCE_NAME, RESOURCE_TYPE_VFC),
96 ResourceVersionInfo.newBuilder()
97 .artifactUuid(ARTIFACT_UUID)
98 .state(PREV_RESOURCE_STATE)
99 .version(PREV_RESOURCE_VERSION)
100 .distributionStatus(DPREV_STATUS)
102 ResourceVersionInfo.newBuilder()
103 .artifactUuid(ARTIFACT_UUID)
104 .state(CURRENT_STATE)
105 .version(CURRENT_VERSION)
106 .distributionStatus(DCURR_STATUS)
108 INVARIANT_UUID, modifier,
109 ARTIFACT_DATA, COMMENT, DIST_ID);
111 when(auditingDao.addRecord(any(AuditingGenericEvent.class), eq(AuditingActionEnum.CHECKIN_RESOURCE.getAuditingEsType())))
112 .thenReturn(ActionStatus.OK);
113 when(cassandraDao.saveRecord(any(AuditingGenericEvent.class))).thenReturn(CassandraOperationStatus.OK);
115 assertThat(auditingManager.auditEvent(factory)).isEqualTo(EXPECTED_CHECK_IN_RESOURCE_LOG_STR);
116 verifyResourceAdminEvent(AuditingActionEnum.CHECKIN_RESOURCE.getName());
120 public void testCreateResourceAdminEvent() {
122 AuditEventFactory factory = new AuditCreateUpdateResourceAdminEventFactory(
123 AuditingActionEnum.CREATE_RESOURCE,
124 CommonAuditData.newBuilder()
125 .description(DESCRIPTION)
127 .requestId(REQUEST_ID)
128 .serviceInstanceId(SERVICE_INSTANCE_ID)
130 new ResourceCommonInfo(RESOURCE_NAME, RESOURCE_TYPE_VFC),
131 ResourceVersionInfo.newBuilder()
132 .artifactUuid(ARTIFACT_UUID)
133 .state(PREV_RESOURCE_STATE)
134 .version(PREV_RESOURCE_VERSION)
135 .distributionStatus(DPREV_STATUS)
137 ResourceVersionInfo.newBuilder()
138 .artifactUuid(ARTIFACT_UUID)
139 .state(CURRENT_STATE)
140 .version(CURRENT_VERSION)
141 .distributionStatus(DCURR_STATUS)
143 INVARIANT_UUID, modifier,
144 ARTIFACT_DATA, COMMENT, DIST_ID, Constants.EMPTY_STRING);
146 when(auditingDao.addRecord(any(AuditingGenericEvent.class), eq(AuditingActionEnum.CREATE_RESOURCE.getAuditingEsType())))
147 .thenReturn(ActionStatus.OK);
148 when(cassandraDao.saveRecord(any(AuditingGenericEvent.class))).thenReturn(CassandraOperationStatus.OK);
150 assertThat(auditingManager.auditEvent(factory)).isEqualTo(EXPECTED_CREATE_RESOURCE_LOG_STR);
151 verifyResourceAdminEvent(AuditingActionEnum.CREATE_RESOURCE.getName());
155 public void testImportResourceAdminEvent() {
157 Resource resource = new Resource();
158 resource.setName(RESOURCE_NAME);
159 resource.setVersion(CURRENT_VERSION);
160 resource.setInvariantUUID(INVARIANT_UUID);
161 resource.setUUID(SERVICE_INSTANCE_ID);
162 resource.setState(LifecycleStateEnum.CERTIFIED);
163 resource.setToscaType(TOSCA_NODE_TYPE);
165 AuditEventFactory factory = new AuditImportResourceAdminEventFactory(
166 CommonAuditData.newBuilder()
167 .description(DESCRIPTION)
169 .requestId(REQUEST_ID)
170 .serviceInstanceId(SERVICE_INSTANCE_ID)
172 new ResourceCommonInfo(RESOURCE_NAME, RESOURCE_TYPE_VFC),
173 ResourceVersionInfo.newBuilder()
174 .artifactUuid(ARTIFACT_UUID)
175 .state(PREV_RESOURCE_STATE)
176 .version(PREV_RESOURCE_VERSION)
177 .distributionStatus(DPREV_STATUS)
179 ResourceVersionInfo.newBuilder()
180 .artifactUuid(ARTIFACT_UUID)
181 .state(CURRENT_STATE)
182 .version(CURRENT_VERSION)
183 .distributionStatus(DCURR_STATUS)
185 INVARIANT_UUID, modifier,
186 ARTIFACT_DATA, COMMENT, DIST_ID, TOSCA_NODE_TYPE);
188 when(auditingDao.addRecord(any(AuditingGenericEvent.class), eq(AuditingActionEnum.IMPORT_RESOURCE.getAuditingEsType())))
189 .thenReturn(ActionStatus.OK);
190 when(cassandraDao.saveRecord(any(AuditingGenericEvent.class))).thenReturn(CassandraOperationStatus.OK);
192 assertThat(auditingManager.auditEvent(factory)).isEqualTo(EXPECTED_IMPORT_RESOURCE_LOG_STR);
193 verifyResourceAdminEvent(AuditingActionEnum.IMPORT_RESOURCE.getName());
197 public void testArtifactUploadResourceAdminEvent() {
199 AuditEventFactory factory = new AuditArtifactResourceAdminEventFactory(
200 AuditingActionEnum.ARTIFACT_UPLOAD,
201 CommonAuditData.newBuilder()
202 .description(DESCRIPTION)
204 .requestId(REQUEST_ID)
205 .serviceInstanceId(SERVICE_INSTANCE_ID)
207 new ResourceCommonInfo(RESOURCE_NAME, RESOURCE_TYPE_VFC),
208 ResourceVersionInfo.newBuilder()
209 .artifactUuid(ARTIFACT_UUID)
210 .state(PREV_RESOURCE_STATE)
211 .version(PREV_RESOURCE_VERSION)
212 .distributionStatus(DPREV_STATUS)
214 ResourceVersionInfo.newBuilder()
215 .artifactUuid(ARTIFACT_UUID)
216 .state(CURRENT_STATE)
217 .version(CURRENT_VERSION)
218 .distributionStatus(DCURR_STATUS)
220 INVARIANT_UUID, modifier,
221 ARTIFACT_DATA, COMMENT, DIST_ID);
223 when(auditingDao.addRecord(any(AuditingGenericEvent.class), eq(AuditingActionEnum.ARTIFACT_UPLOAD.getAuditingEsType())))
224 .thenReturn(ActionStatus.OK);
225 when(cassandraDao.saveRecord(any(AuditingGenericEvent.class))).thenReturn(CassandraOperationStatus.OK);
227 assertThat(auditingManager.auditEvent(factory)).isEqualTo(EXPECTED_ARTIFACT_UPLOAD_LOG_STR);
228 verifyResourceAdminEvent(AuditingActionEnum.ARTIFACT_UPLOAD.getName());
232 public void testDistStateChangeRequestResourceAdminEvent() {
234 AuditEventFactory factory = new AuditDistStateChangeRequestResourceAdminEventFactory(
235 CommonAuditData.newBuilder()
236 .description(DESCRIPTION)
238 .requestId(REQUEST_ID)
239 .serviceInstanceId(SERVICE_INSTANCE_ID)
241 new ResourceCommonInfo(RESOURCE_NAME, RESOURCE_TYPE_VFC),
242 ResourceVersionInfo.newBuilder()
243 .artifactUuid(ARTIFACT_UUID)
244 .state(PREV_RESOURCE_STATE)
245 .version(PREV_RESOURCE_VERSION)
246 .distributionStatus(DPREV_STATUS)
248 ResourceVersionInfo.newBuilder()
249 .artifactUuid(ARTIFACT_UUID)
250 .state(CURRENT_STATE)
251 .version(CURRENT_VERSION)
252 .distributionStatus(DCURR_STATUS)
254 INVARIANT_UUID, modifier,
255 ARTIFACT_DATA, COMMENT, DIST_ID);
257 when(auditingDao.addRecord(any(AuditingGenericEvent.class), eq(AuditingActionEnum.DISTRIBUTION_STATE_CHANGE_REQUEST.getAuditingEsType())))
258 .thenReturn(ActionStatus.OK);
259 when(cassandraDao.saveRecord(any(AuditingGenericEvent.class))).thenReturn(CassandraOperationStatus.OK);
261 assertThat(auditingManager.auditEvent(factory)).isEqualTo(EXPECTED_DIST_STATE_CHANGE_REQUEST);
262 verifyResourceAdminEvent(AuditingActionEnum.DISTRIBUTION_STATE_CHANGE_REQUEST.getName());
266 public void testDistStateChangeApprovResourceAdminEvent() {
268 AuditEventFactory factory = new AuditDistStateChangeResourceAdminEventFactory(
269 AuditingActionEnum.DISTRIBUTION_STATE_CHANGE_APPROV,
270 CommonAuditData.newBuilder()
271 .description(DESCRIPTION)
273 .requestId(REQUEST_ID)
274 .serviceInstanceId(SERVICE_INSTANCE_ID)
276 new ResourceCommonInfo(RESOURCE_NAME, RESOURCE_TYPE_VFC),
277 ResourceVersionInfo.newBuilder()
278 .artifactUuid(ARTIFACT_UUID)
279 .state(PREV_RESOURCE_STATE)
280 .version(PREV_RESOURCE_VERSION)
281 .distributionStatus(DPREV_STATUS)
283 ResourceVersionInfo.newBuilder()
284 .artifactUuid(ARTIFACT_UUID)
285 .state(CURRENT_STATE)
286 .version(CURRENT_VERSION)
287 .distributionStatus(DCURR_STATUS)
289 INVARIANT_UUID, modifier,
290 ARTIFACT_DATA, COMMENT, DIST_ID);
292 when(auditingDao.addRecord(any(AuditingGenericEvent.class), eq(AuditingActionEnum.DISTRIBUTION_STATE_CHANGE_REQUEST.getAuditingEsType())))
293 .thenReturn(ActionStatus.OK);
294 when(cassandraDao.saveRecord(any(AuditingGenericEvent.class))).thenReturn(CassandraOperationStatus.OK);
296 assertThat(auditingManager.auditEvent(factory)).isEqualTo(EXPECTED_DIST_STATE_CHANGE_APPROV);
297 verifyResourceAdminEvent(AuditingActionEnum.DISTRIBUTION_STATE_CHANGE_APPROV.getName());
300 private void verifyResourceAdminEvent(String action) {
301 verify(cassandraDao).saveRecord(eventCaptor.capture());
302 ResourceAdminEvent storedEvent = eventCaptor.getValue();
303 assertThat(storedEvent.getModifier()).isEqualTo(MODIFIER_UID);
304 assertThat(storedEvent.getDesc()).isEqualTo(DESCRIPTION);
305 assertThat(storedEvent.getRequestId()).isEqualTo(REQUEST_ID);
306 assertThat(storedEvent.getServiceInstanceId()).isEqualTo(SERVICE_INSTANCE_ID);
307 assertThat(storedEvent.getAction()).isEqualTo(action);
308 assertThat(storedEvent.getArtifactData()).isEqualTo(ARTIFACT_DATA);
309 assertThat(storedEvent.getComment()).isEqualTo(COMMENT);
310 assertThat(storedEvent.getCurrArtifactUUID()).isEqualTo(ARTIFACT_UUID);
311 assertThat(storedEvent.getPrevArtifactUUID()).isEqualTo(ARTIFACT_UUID);
312 assertThat(storedEvent.getPrevState()).isEqualTo(PREV_RESOURCE_STATE);
313 assertThat(storedEvent.getCurrState()).isEqualTo(CURRENT_STATE);
314 assertThat(storedEvent.getPrevVersion()).isEqualTo(PREV_RESOURCE_VERSION);
315 assertThat(storedEvent.getCurrVersion()).isEqualTo(CURRENT_VERSION);
316 assertThat(storedEvent.getDcurrStatus()).isEqualTo(DCURR_STATUS);
317 assertThat(storedEvent.getDprevStatus()).isEqualTo(DPREV_STATUS);
318 assertThat(storedEvent.getDid()).isEqualTo(DIST_ID);
319 assertThat(storedEvent.getInvariantUUID()).isEqualTo(INVARIANT_UUID);
320 assertThat(storedEvent.getResourceName()).isEqualTo(RESOURCE_NAME);
321 assertThat(storedEvent.getResourceType()).isEqualTo(RESOURCE_TYPE_VFC);
323 if (action.equals(AuditingActionEnum.IMPORT_RESOURCE.getName())) {
324 assertThat(storedEvent.getToscaNodeType()).isEqualTo(TOSCA_NODE_TYPE);
326 assertThat(storedEvent.getToscaNodeType()).isEmpty();