Catalog alignment
[sdc.git] / catalog-be / src / test / java / org / openecomp / sdc / be / auditing / impl / resourceadmin / AuditResourceAdminEventFuncTest.java
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.resourceadmin;
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.dao.cassandra.AuditCassandraDao;
34 import org.openecomp.sdc.be.dao.cassandra.CassandraOperationStatus;
35 import org.openecomp.sdc.be.model.LifecycleStateEnum;
36 import org.openecomp.sdc.be.model.Resource;
37 import org.openecomp.sdc.be.resources.data.auditing.AuditingActionEnum;
38 import org.openecomp.sdc.be.resources.data.auditing.AuditingGenericEvent;
39 import org.openecomp.sdc.be.resources.data.auditing.ResourceAdminEvent;
40 import org.openecomp.sdc.be.resources.data.auditing.model.CommonAuditData;
41 import org.openecomp.sdc.be.resources.data.auditing.model.ResourceCommonInfo;
42 import org.openecomp.sdc.be.resources.data.auditing.model.ResourceVersionInfo;
43 import org.openecomp.sdc.common.api.Constants;
44 import org.openecomp.sdc.common.util.ThreadLocalsHolder;
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.Mockito.verify;
50 import static org.mockito.Mockito.when;
51 import static org.openecomp.sdc.be.auditing.impl.AuditTestUtils.ARTIFACT_DATA;
52 import static org.openecomp.sdc.be.auditing.impl.AuditTestUtils.ARTIFACT_UUID;
53 import static org.openecomp.sdc.be.auditing.impl.AuditTestUtils.COMMENT;
54 import static org.openecomp.sdc.be.auditing.impl.AuditTestUtils.CURRENT_STATE;
55 import static org.openecomp.sdc.be.auditing.impl.AuditTestUtils.CURRENT_VERSION;
56 import static org.openecomp.sdc.be.auditing.impl.AuditTestUtils.DCURR_STATUS;
57 import static org.openecomp.sdc.be.auditing.impl.AuditTestUtils.DESCRIPTION;
58 import static org.openecomp.sdc.be.auditing.impl.AuditTestUtils.DIST_ID;
59 import static org.openecomp.sdc.be.auditing.impl.AuditTestUtils.DPREV_STATUS;
60 import static org.openecomp.sdc.be.auditing.impl.AuditTestUtils.EXPECTED_ARTIFACT_UPLOAD_LOG_STR;
61 import static org.openecomp.sdc.be.auditing.impl.AuditTestUtils.EXPECTED_CHECK_IN_RESOURCE_LOG_STR;
62 import static org.openecomp.sdc.be.auditing.impl.AuditTestUtils.EXPECTED_CREATE_RESOURCE_LOG_STR;
63 import static org.openecomp.sdc.be.auditing.impl.AuditTestUtils.EXPECTED_DIST_STATE_CHANGE_APPROV;
64 import static org.openecomp.sdc.be.auditing.impl.AuditTestUtils.EXPECTED_DIST_STATE_CHANGE_REQUEST;
65 import static org.openecomp.sdc.be.auditing.impl.AuditTestUtils.EXPECTED_IMPORT_RESOURCE_LOG_STR;
66 import static org.openecomp.sdc.be.auditing.impl.AuditTestUtils.INVARIANT_UUID;
67 import static org.openecomp.sdc.be.auditing.impl.AuditTestUtils.MODIFIER_UID;
68 import static org.openecomp.sdc.be.auditing.impl.AuditTestUtils.PREV_RESOURCE_STATE;
69 import static org.openecomp.sdc.be.auditing.impl.AuditTestUtils.PREV_RESOURCE_VERSION;
70 import static org.openecomp.sdc.be.auditing.impl.AuditTestUtils.REQUEST_ID;
71 import static org.openecomp.sdc.be.auditing.impl.AuditTestUtils.RESOURCE_NAME;
72 import static org.openecomp.sdc.be.auditing.impl.AuditTestUtils.RESOURCE_TYPE_VFC;
73 import static org.openecomp.sdc.be.auditing.impl.AuditTestUtils.SERVICE_INSTANCE_ID;
74 import static org.openecomp.sdc.be.auditing.impl.AuditTestUtils.STATUS_OK;
75 import static org.openecomp.sdc.be.auditing.impl.AuditTestUtils.TOSCA_NODE_TYPE;
76 import static org.openecomp.sdc.be.auditing.impl.AuditTestUtils.init;
77 import static org.openecomp.sdc.be.auditing.impl.AuditTestUtils.modifier;
78
79 @RunWith(MockitoJUnitRunner.class)
80 public class AuditResourceAdminEventFuncTest {
81     private AuditingManager auditingManager;
82
83     @Mock
84     private static AuditCassandraDao cassandraDao;
85
86     @Captor
87     private ArgumentCaptor<ResourceAdminEvent> eventCaptor;
88
89     @Before
90     public void setUp() {
91         init();
92         auditingManager = new AuditingManager(cassandraDao, new TestConfigurationProvider());
93         ThreadLocalsHolder.setUuid(REQUEST_ID);
94     }
95
96     @Test
97     public void testCheckInResourceAdminEvent() {
98        Resource resource = new Resource();
99         resource.setName(RESOURCE_NAME);
100         resource.setUUID(SERVICE_INSTANCE_ID);
101         resource.setInvariantUUID(INVARIANT_UUID);
102         resource.setLifecycleState(LifecycleStateEnum.CERTIFIED);
103         resource.setVersion(CURRENT_VERSION);
104
105         AuditEventFactory factory = new AuditCertificationResourceAdminEventFactory(
106                 AuditingActionEnum.CHECKIN_RESOURCE,
107                 CommonAuditData.newBuilder()
108                         .description(DESCRIPTION)
109                         .status(STATUS_OK)
110                         .requestId(REQUEST_ID)
111                         .serviceInstanceId(SERVICE_INSTANCE_ID)
112                         .build(),
113                 new ResourceCommonInfo(RESOURCE_NAME, RESOURCE_TYPE_VFC),
114                 ResourceVersionInfo.newBuilder()
115                         .artifactUuid(ARTIFACT_UUID)
116                         .state(PREV_RESOURCE_STATE)
117                         .version(PREV_RESOURCE_VERSION)
118                         .distributionStatus(DPREV_STATUS)
119                         .build(),
120                 ResourceVersionInfo.newBuilder()
121                         .artifactUuid(ARTIFACT_UUID)
122                         .state(CURRENT_STATE)
123                         .version(CURRENT_VERSION)
124                         .distributionStatus(DCURR_STATUS)
125                         .build(),
126                 INVARIANT_UUID, modifier,
127                 ARTIFACT_DATA, COMMENT, DIST_ID);
128
129         when(cassandraDao.saveRecord(any(AuditingGenericEvent.class))).thenReturn(CassandraOperationStatus.OK);
130
131         assertThat(auditingManager.auditEvent(factory)).isEqualTo(EXPECTED_CHECK_IN_RESOURCE_LOG_STR);
132         verifyResourceAdminEvent(AuditingActionEnum.CHECKIN_RESOURCE.getName());
133     }
134
135     @Test
136     public void testCreateResourceAdminEvent() {
137
138       AuditEventFactory factory = new AuditCreateUpdateResourceAdminEventFactory(
139              AuditingActionEnum.CREATE_RESOURCE,
140              CommonAuditData.newBuilder()
141                      .description(DESCRIPTION)
142                      .status(STATUS_OK)
143                      .requestId(REQUEST_ID)
144                      .serviceInstanceId(SERVICE_INSTANCE_ID)
145                      .build(),
146               new ResourceCommonInfo(RESOURCE_NAME, RESOURCE_TYPE_VFC),
147               ResourceVersionInfo.newBuilder()
148                       .artifactUuid(ARTIFACT_UUID)
149                       .state(PREV_RESOURCE_STATE)
150                       .version(PREV_RESOURCE_VERSION)
151                       .distributionStatus(DPREV_STATUS)
152                       .build(),
153               ResourceVersionInfo.newBuilder()
154                       .artifactUuid(ARTIFACT_UUID)
155                       .state(CURRENT_STATE)
156                       .version(CURRENT_VERSION)
157                       .distributionStatus(DCURR_STATUS)
158                       .build(),
159              INVARIANT_UUID, modifier,
160              ARTIFACT_DATA, COMMENT, DIST_ID, Constants.EMPTY_STRING);
161
162         when(cassandraDao.saveRecord(any(AuditingGenericEvent.class))).thenReturn(CassandraOperationStatus.OK);
163
164         assertThat(auditingManager.auditEvent(factory)).isEqualTo(EXPECTED_CREATE_RESOURCE_LOG_STR);
165         verifyResourceAdminEvent(AuditingActionEnum.CREATE_RESOURCE.getName());
166     }
167
168     @Test
169     public void testImportResourceAdminEvent() {
170
171         Resource resource = new Resource();
172         resource.setName(RESOURCE_NAME);
173         resource.setVersion(CURRENT_VERSION);
174         resource.setInvariantUUID(INVARIANT_UUID);
175         resource.setUUID(SERVICE_INSTANCE_ID);
176         resource.setState(LifecycleStateEnum.CERTIFIED);
177         resource.setToscaType(TOSCA_NODE_TYPE);
178
179         AuditEventFactory factory = new AuditImportResourceAdminEventFactory(
180                 CommonAuditData.newBuilder()
181                         .description(DESCRIPTION)
182                         .status(STATUS_OK)
183                         .requestId(REQUEST_ID)
184                         .serviceInstanceId(SERVICE_INSTANCE_ID)
185                         .build(),
186                 new ResourceCommonInfo(RESOURCE_NAME, RESOURCE_TYPE_VFC),
187                 ResourceVersionInfo.newBuilder()
188                         .artifactUuid(ARTIFACT_UUID)
189                         .state(PREV_RESOURCE_STATE)
190                         .version(PREV_RESOURCE_VERSION)
191                         .distributionStatus(DPREV_STATUS)
192                         .build(),
193                 ResourceVersionInfo.newBuilder()
194                         .artifactUuid(ARTIFACT_UUID)
195                         .state(CURRENT_STATE)
196                         .version(CURRENT_VERSION)
197                         .distributionStatus(DCURR_STATUS)
198                         .build(),
199                 INVARIANT_UUID, modifier,
200                 ARTIFACT_DATA, COMMENT, DIST_ID, TOSCA_NODE_TYPE);
201
202         when(cassandraDao.saveRecord(any(AuditingGenericEvent.class))).thenReturn(CassandraOperationStatus.OK);
203
204         assertThat(auditingManager.auditEvent(factory)).isEqualTo(EXPECTED_IMPORT_RESOURCE_LOG_STR);
205         verifyResourceAdminEvent(AuditingActionEnum.IMPORT_RESOURCE.getName());
206     }
207
208     @Test
209     public void testArtifactUploadResourceAdminEvent() {
210
211         AuditEventFactory factory = new AuditArtifactResourceAdminEventFactory(
212                 AuditingActionEnum.ARTIFACT_UPLOAD,
213                 CommonAuditData.newBuilder()
214                         .description(DESCRIPTION)
215                         .status(STATUS_OK)
216                         .requestId(REQUEST_ID)
217                         .serviceInstanceId(SERVICE_INSTANCE_ID)
218                         .build(),
219                 new ResourceCommonInfo(RESOURCE_NAME, RESOURCE_TYPE_VFC),
220                 ResourceVersionInfo.newBuilder()
221                         .artifactUuid(ARTIFACT_UUID)
222                         .state(PREV_RESOURCE_STATE)
223                         .version(PREV_RESOURCE_VERSION)
224                         .distributionStatus(DPREV_STATUS)
225                         .build(),
226                 ResourceVersionInfo.newBuilder()
227                         .artifactUuid(ARTIFACT_UUID)
228                         .state(CURRENT_STATE)
229                         .version(CURRENT_VERSION)
230                         .distributionStatus(DCURR_STATUS)
231                         .build(),
232                 INVARIANT_UUID, modifier,
233                 ARTIFACT_DATA, COMMENT, DIST_ID);
234
235         when(cassandraDao.saveRecord(any(AuditingGenericEvent.class))).thenReturn(CassandraOperationStatus.OK);
236
237         assertThat(auditingManager.auditEvent(factory)).isEqualTo(EXPECTED_ARTIFACT_UPLOAD_LOG_STR);
238         verifyResourceAdminEvent(AuditingActionEnum.ARTIFACT_UPLOAD.getName());
239     }
240
241     @Test
242     public void testDistStateChangeRequestResourceAdminEvent() {
243
244         AuditEventFactory factory = new AuditDistStateChangeRequestResourceAdminEventFactory(
245                 CommonAuditData.newBuilder()
246                         .description(DESCRIPTION)
247                         .status(STATUS_OK)
248                         .requestId(REQUEST_ID)
249                         .serviceInstanceId(SERVICE_INSTANCE_ID)
250                         .build(),
251                 new ResourceCommonInfo(RESOURCE_NAME, RESOURCE_TYPE_VFC),
252                 ResourceVersionInfo.newBuilder()
253                         .artifactUuid(ARTIFACT_UUID)
254                         .state(PREV_RESOURCE_STATE)
255                         .version(PREV_RESOURCE_VERSION)
256                         .distributionStatus(DPREV_STATUS)
257                         .build(),
258                 ResourceVersionInfo.newBuilder()
259                         .artifactUuid(ARTIFACT_UUID)
260                         .state(CURRENT_STATE)
261                         .version(CURRENT_VERSION)
262                         .distributionStatus(DCURR_STATUS)
263                         .build(),
264                 INVARIANT_UUID, modifier,
265                 ARTIFACT_DATA, COMMENT, DIST_ID);
266
267         when(cassandraDao.saveRecord(any(AuditingGenericEvent.class))).thenReturn(CassandraOperationStatus.OK);
268
269         assertThat(auditingManager.auditEvent(factory)).isEqualTo(EXPECTED_DIST_STATE_CHANGE_REQUEST);
270         verifyResourceAdminEvent(AuditingActionEnum.DISTRIBUTION_STATE_CHANGE_REQUEST.getName());
271     }
272
273     @Test
274     public void testDistStateChangeApprovResourceAdminEvent() {
275
276         AuditEventFactory factory = new AuditDistStateChangeResourceAdminEventFactory(
277                 AuditingActionEnum.DISTRIBUTION_STATE_CHANGE_APPROV,
278                 CommonAuditData.newBuilder()
279                         .description(DESCRIPTION)
280                         .status(STATUS_OK)
281                         .requestId(REQUEST_ID)
282                         .serviceInstanceId(SERVICE_INSTANCE_ID)
283                         .build(),
284                 new ResourceCommonInfo(RESOURCE_NAME, RESOURCE_TYPE_VFC),
285                  ResourceVersionInfo.newBuilder()
286                         .artifactUuid(ARTIFACT_UUID)
287                         .state(PREV_RESOURCE_STATE)
288                         .version(PREV_RESOURCE_VERSION)
289                         .distributionStatus(DPREV_STATUS)
290                         .build(),
291                 ResourceVersionInfo.newBuilder()
292                         .artifactUuid(ARTIFACT_UUID)
293                         .state(CURRENT_STATE)
294                         .version(CURRENT_VERSION)
295                         .distributionStatus(DCURR_STATUS)
296                         .build(),
297                 INVARIANT_UUID, modifier,
298                 ARTIFACT_DATA, COMMENT, DIST_ID);
299
300         when(cassandraDao.saveRecord(any(AuditingGenericEvent.class))).thenReturn(CassandraOperationStatus.OK);
301
302         assertThat(auditingManager.auditEvent(factory)).isEqualTo(EXPECTED_DIST_STATE_CHANGE_APPROV);
303         verifyResourceAdminEvent(AuditingActionEnum.DISTRIBUTION_STATE_CHANGE_APPROV.getName());
304     }
305
306     private void verifyResourceAdminEvent(String action) {
307         verify(cassandraDao).saveRecord(eventCaptor.capture());
308         ResourceAdminEvent storedEvent = eventCaptor.getValue();
309         assertThat(storedEvent.getModifier()).isEqualTo(MODIFIER_UID);
310         assertThat(storedEvent.getDesc()).isEqualTo(DESCRIPTION);
311         assertThat(storedEvent.getRequestId()).isEqualTo(REQUEST_ID);
312         assertThat(storedEvent.getServiceInstanceId()).isEqualTo(SERVICE_INSTANCE_ID);
313         assertThat(storedEvent.getAction()).isEqualTo(action);
314         assertThat(storedEvent.getArtifactData()).isEqualTo(ARTIFACT_DATA);
315         assertThat(storedEvent.getComment()).isEqualTo(COMMENT);
316         assertThat(storedEvent.getCurrArtifactUUID()).isEqualTo(ARTIFACT_UUID);
317         assertThat(storedEvent.getPrevArtifactUUID()).isEqualTo(ARTIFACT_UUID);
318         assertThat(storedEvent.getPrevState()).isEqualTo(PREV_RESOURCE_STATE);
319         assertThat(storedEvent.getCurrState()).isEqualTo(CURRENT_STATE);
320         assertThat(storedEvent.getPrevVersion()).isEqualTo(PREV_RESOURCE_VERSION);
321         assertThat(storedEvent.getCurrVersion()).isEqualTo(CURRENT_VERSION);
322         assertThat(storedEvent.getDcurrStatus()).isEqualTo(DCURR_STATUS);
323         assertThat(storedEvent.getDprevStatus()).isEqualTo(DPREV_STATUS);
324         assertThat(storedEvent.getDid()).isEqualTo(DIST_ID);
325         assertThat(storedEvent.getInvariantUUID()).isEqualTo(INVARIANT_UUID);
326         assertThat(storedEvent.getResourceName()).isEqualTo(RESOURCE_NAME);
327         assertThat(storedEvent.getResourceType()).isEqualTo(RESOURCE_TYPE_VFC);
328
329         if (action.equals(AuditingActionEnum.IMPORT_RESOURCE.getName())) {
330             assertThat(storedEvent.getToscaNodeType()).isEqualTo(TOSCA_NODE_TYPE);
331         } else {
332             assertThat(storedEvent.getToscaNodeType()).isEmpty();
333         }
334     }
335 }