b6671fe429fc52786d3cf2c4b6db3bcb1e763788
[sdc.git] /
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.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;
49
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.*;
56
57 @RunWith(MockitoJUnitRunner.class)
58 public class AuditResourceAdminEventFuncTest {
59     private AuditingManager auditingManager;
60
61     @Mock
62     private static AuditCassandraDao cassandraDao;
63     @Mock
64     private static AuditingDao auditingDao;
65     @Mock
66     private static Configuration.ElasticSearchConfig esConfig;
67
68     @Captor
69     private ArgumentCaptor<ResourceAdminEvent> eventCaptor;
70
71     @Before
72     public void setUp() {
73         init(esConfig);
74         auditingManager = new AuditingManager(auditingDao, cassandraDao, new TestConfigurationProvider());
75         ThreadLocalsHolder.setUuid(REQUEST_ID);
76     }
77
78     @Test
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);
86
87         AuditEventFactory factory = new AuditCertificationResourceAdminEventFactory(
88                 AuditingActionEnum.CHECKIN_RESOURCE,
89                 CommonAuditData.newBuilder()
90                         .description(DESCRIPTION)
91                         .status(STATUS_OK)
92                         .requestId(REQUEST_ID)
93                         .serviceInstanceId(SERVICE_INSTANCE_ID)
94                         .build(),
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)
101                         .build(),
102                 ResourceVersionInfo.newBuilder()
103                         .artifactUuid(ARTIFACT_UUID)
104                         .state(CURRENT_STATE)
105                         .version(CURRENT_VERSION)
106                         .distributionStatus(DCURR_STATUS)
107                         .build(),
108                 INVARIANT_UUID, modifier,
109                 ARTIFACT_DATA, COMMENT, DIST_ID);
110
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);
114
115         assertThat(auditingManager.auditEvent(factory)).isEqualTo(EXPECTED_CHECK_IN_RESOURCE_LOG_STR);
116         verifyResourceAdminEvent(AuditingActionEnum.CHECKIN_RESOURCE.getName());
117     }
118
119     @Test
120     public void testCreateResourceAdminEvent() {
121
122       AuditEventFactory factory = new AuditCreateUpdateResourceAdminEventFactory(
123              AuditingActionEnum.CREATE_RESOURCE,
124              CommonAuditData.newBuilder()
125                      .description(DESCRIPTION)
126                      .status(STATUS_OK)
127                      .requestId(REQUEST_ID)
128                      .serviceInstanceId(SERVICE_INSTANCE_ID)
129                      .build(),
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)
136                       .build(),
137               ResourceVersionInfo.newBuilder()
138                       .artifactUuid(ARTIFACT_UUID)
139                       .state(CURRENT_STATE)
140                       .version(CURRENT_VERSION)
141                       .distributionStatus(DCURR_STATUS)
142                       .build(),
143              INVARIANT_UUID, modifier,
144              ARTIFACT_DATA, COMMENT, DIST_ID, Constants.EMPTY_STRING);
145
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);
149
150         assertThat(auditingManager.auditEvent(factory)).isEqualTo(EXPECTED_CREATE_RESOURCE_LOG_STR);
151         verifyResourceAdminEvent(AuditingActionEnum.CREATE_RESOURCE.getName());
152     }
153
154     @Test
155     public void testImportResourceAdminEvent() {
156
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);
164
165         AuditEventFactory factory = new AuditImportResourceAdminEventFactory(
166                 CommonAuditData.newBuilder()
167                         .description(DESCRIPTION)
168                         .status(STATUS_OK)
169                         .requestId(REQUEST_ID)
170                         .serviceInstanceId(SERVICE_INSTANCE_ID)
171                         .build(),
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)
178                         .build(),
179                 ResourceVersionInfo.newBuilder()
180                         .artifactUuid(ARTIFACT_UUID)
181                         .state(CURRENT_STATE)
182                         .version(CURRENT_VERSION)
183                         .distributionStatus(DCURR_STATUS)
184                         .build(),
185                 INVARIANT_UUID, modifier,
186                 ARTIFACT_DATA, COMMENT, DIST_ID, TOSCA_NODE_TYPE);
187
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);
191
192         assertThat(auditingManager.auditEvent(factory)).isEqualTo(EXPECTED_IMPORT_RESOURCE_LOG_STR);
193         verifyResourceAdminEvent(AuditingActionEnum.IMPORT_RESOURCE.getName());
194     }
195
196     @Test
197     public void testArtifactUploadResourceAdminEvent() {
198
199         AuditEventFactory factory = new AuditArtifactResourceAdminEventFactory(
200                 AuditingActionEnum.ARTIFACT_UPLOAD,
201                 CommonAuditData.newBuilder()
202                         .description(DESCRIPTION)
203                         .status(STATUS_OK)
204                         .requestId(REQUEST_ID)
205                         .serviceInstanceId(SERVICE_INSTANCE_ID)
206                         .build(),
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)
213                         .build(),
214                 ResourceVersionInfo.newBuilder()
215                         .artifactUuid(ARTIFACT_UUID)
216                         .state(CURRENT_STATE)
217                         .version(CURRENT_VERSION)
218                         .distributionStatus(DCURR_STATUS)
219                         .build(),
220                 INVARIANT_UUID, modifier,
221                 ARTIFACT_DATA, COMMENT, DIST_ID);
222
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);
226
227         assertThat(auditingManager.auditEvent(factory)).isEqualTo(EXPECTED_ARTIFACT_UPLOAD_LOG_STR);
228         verifyResourceAdminEvent(AuditingActionEnum.ARTIFACT_UPLOAD.getName());
229     }
230
231     @Test
232     public void testDistStateChangeRequestResourceAdminEvent() {
233
234         AuditEventFactory factory = new AuditDistStateChangeRequestResourceAdminEventFactory(
235                 CommonAuditData.newBuilder()
236                         .description(DESCRIPTION)
237                         .status(STATUS_OK)
238                         .requestId(REQUEST_ID)
239                         .serviceInstanceId(SERVICE_INSTANCE_ID)
240                         .build(),
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)
247                         .build(),
248                 ResourceVersionInfo.newBuilder()
249                         .artifactUuid(ARTIFACT_UUID)
250                         .state(CURRENT_STATE)
251                         .version(CURRENT_VERSION)
252                         .distributionStatus(DCURR_STATUS)
253                         .build(),
254                 INVARIANT_UUID, modifier,
255                 ARTIFACT_DATA, COMMENT, DIST_ID);
256
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);
260
261         assertThat(auditingManager.auditEvent(factory)).isEqualTo(EXPECTED_DIST_STATE_CHANGE_REQUEST);
262         verifyResourceAdminEvent(AuditingActionEnum.DISTRIBUTION_STATE_CHANGE_REQUEST.getName());
263     }
264
265     @Test
266     public void testDistStateChangeApprovResourceAdminEvent() {
267
268         AuditEventFactory factory = new AuditDistStateChangeResourceAdminEventFactory(
269                 AuditingActionEnum.DISTRIBUTION_STATE_CHANGE_APPROV,
270                 CommonAuditData.newBuilder()
271                         .description(DESCRIPTION)
272                         .status(STATUS_OK)
273                         .requestId(REQUEST_ID)
274                         .serviceInstanceId(SERVICE_INSTANCE_ID)
275                         .build(),
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)
282                         .build(),
283                 ResourceVersionInfo.newBuilder()
284                         .artifactUuid(ARTIFACT_UUID)
285                         .state(CURRENT_STATE)
286                         .version(CURRENT_VERSION)
287                         .distributionStatus(DCURR_STATUS)
288                         .build(),
289                 INVARIANT_UUID, modifier,
290                 ARTIFACT_DATA, COMMENT, DIST_ID);
291
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);
295
296         assertThat(auditingManager.auditEvent(factory)).isEqualTo(EXPECTED_DIST_STATE_CHANGE_APPROV);
297         verifyResourceAdminEvent(AuditingActionEnum.DISTRIBUTION_STATE_CHANGE_APPROV.getName());
298     }
299
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);
322
323         if (action.equals(AuditingActionEnum.IMPORT_RESOURCE.getName())) {
324             assertThat(storedEvent.getToscaNodeType()).isEqualTo(TOSCA_NODE_TYPE);
325         } else {
326             assertThat(storedEvent.getToscaNodeType()).isEmpty();
327         }
328     }
329 }