ab3b054fd05685f6b3cf78365bad976a8917e3b5
[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.distribution;
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.AuditGetUebClusterEventFactory;
33 import org.openecomp.sdc.be.auditing.impl.AuditingManager;
34 import org.openecomp.sdc.be.config.Configuration;
35 import org.openecomp.sdc.be.dao.api.ActionStatus;
36 import org.openecomp.sdc.be.dao.cassandra.AuditCassandraDao;
37 import org.openecomp.sdc.be.dao.cassandra.CassandraOperationStatus;
38 import org.openecomp.sdc.be.dao.impl.AuditingDao;
39 import org.openecomp.sdc.be.resources.data.auditing.*;
40 import org.openecomp.sdc.be.resources.data.auditing.model.*;
41 import org.openecomp.sdc.common.util.ThreadLocalsHolder;
42 import org.openecomp.sdc.test.utils.TestConfigurationProvider;
43
44 import static org.assertj.core.api.Assertions.assertThat;
45 import static org.mockito.ArgumentMatchers.any;
46 import static org.mockito.ArgumentMatchers.eq;
47 import static org.mockito.Mockito.verify;
48 import static org.mockito.Mockito.when;
49 import static org.openecomp.sdc.be.auditing.impl.AuditTestUtils.*;
50
51 @RunWith(MockitoJUnitRunner.class)
52 public class AuditDistributionEventFuncTest {
53
54     private AuditingManager auditingManager;
55
56     @Mock
57     private static AuditCassandraDao cassandraDao;
58     @Mock
59     private static AuditingDao auditingDao;
60     @Mock
61     private static Configuration.ElasticSearchConfig esConfig;
62
63     @Captor
64     private ArgumentCaptor<AuditingGenericEvent> eventCaptor;
65
66     @Before
67     public void setUp() {
68         init(esConfig);
69         auditingManager = new AuditingManager(auditingDao, cassandraDao, new TestConfigurationProvider());
70         ThreadLocalsHolder.setUuid(REQUEST_ID);
71     }
72
73     @Test
74     public void testNotifyEvent() {
75         AuditEventFactory factory = new AuditDistributionNotificationEventFactory(
76                 CommonAuditData.newBuilder()
77                     .description(DESCRIPTION)
78                     .status(STATUS_OK)
79                     .requestId(REQUEST_ID)
80                     .serviceInstanceId(SERVICE_INSTANCE_ID)
81                     .build(),
82                 new ResourceCommonInfo(RESOURCE_NAME,RESOURCE_TYPE),
83                 ResourceVersionInfo.newBuilder()
84                     .state(CURRENT_STATE)
85                     .version(CURRENT_VERSION)
86                     .build(),
87                 DIST_ID, user, TOPIC_NAME,
88                 new OperationalEnvAuditData(OP_ENV_ID, VNF_WORKLOAD_CONTEXT, TENANT_CONTEXT));
89
90         when(auditingDao.addRecord(any(AuditingGenericEvent.class), eq(AuditingActionEnum.DISTRIBUTION_NOTIFY.getAuditingEsType())))
91                 .thenReturn(ActionStatus.OK);
92         when(cassandraDao.saveRecord(any(AuditingGenericEvent.class))).thenReturn(CassandraOperationStatus.OK);
93
94         assertThat(auditingManager.auditEvent(factory)).isEqualTo(EXPECTED_DISTRIB_NOTIFICATION_LOG_STR);
95         verifyNotifyEvent();
96     }
97
98     @Test
99     public void testStatusEvent() {
100         AuditEventFactory factory = new AuditDistributionStatusEventFactory(
101                 CommonAuditData.newBuilder()
102                         .description(DESCRIPTION)
103                         .status(STATUS_OK)
104                         .requestId(REQUEST_ID)
105                         .serviceInstanceId(SERVICE_INSTANCE_ID)
106                         .build(),
107                 new DistributionData(DIST_CONSUMER_ID, DIST_RESOURCE_URL),
108                 DIST_ID, TOPIC_NAME, DIST_STATUS_TIME);
109
110         when(auditingDao.addRecord(any(AuditingGenericEvent.class), eq(AuditingActionEnum.DISTRIBUTION_STATUS.getAuditingEsType())))
111                 .thenReturn(ActionStatus.OK);
112         when(cassandraDao.saveRecord(any(AuditingGenericEvent.class))).thenReturn(CassandraOperationStatus.OK);
113
114         assertThat(auditingManager.auditEvent(factory)).isEqualTo(EXPECTED_DIST_STATUS_LOG_STR);
115         verifyStatusEvent();
116     }
117
118     @Test
119     public void testDownloadEvent() {
120         AuditEventFactory factory = new AuditDistributionDownloadEventFactory(
121                 CommonAuditData.newBuilder()
122                         .description(DESCRIPTION)
123                         .status(STATUS_OK)
124                         .requestId(REQUEST_ID)
125                         .serviceInstanceId(SERVICE_INSTANCE_ID)
126                         .build(),
127                 new DistributionData(DIST_CONSUMER_ID, DIST_RESOURCE_URL));
128
129         when(auditingDao.addRecord(any(AuditingGenericEvent.class), eq(AuditingActionEnum.DISTRIBUTION_ARTIFACT_DOWNLOAD.getAuditingEsType())))
130                 .thenReturn(ActionStatus.OK);
131         when(cassandraDao.saveRecord(any(AuditingGenericEvent.class))).thenReturn(CassandraOperationStatus.OK);
132
133         assertThat(auditingManager.auditEvent(factory)).isEqualTo(EXPECTED_DIST_DOWNLOAD_LOG_STR);
134         verifyDownloadsEvent();
135     }
136
137     @Test
138     public void testDeployEvent() {
139         AuditEventFactory factory = new AuditDistributionDeployEventFactory(
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),
147                 DIST_ID, user, CURRENT_VERSION);
148
149         when(auditingDao.addRecord(any(AuditingGenericEvent.class), eq(AuditingActionEnum.DISTRIBUTION_DEPLOY.getAuditingEsType())))
150                 .thenReturn(ActionStatus.OK);
151         when(cassandraDao.saveRecord(any(AuditingGenericEvent.class))).thenReturn(CassandraOperationStatus.OK);
152
153         assertThat(auditingManager.auditEvent(factory)).isEqualTo(EXPECTED_DISTRIB_DEPLOY_LOG_STR);
154         verifyDeployEvent();
155     }
156
157     @Test
158     public void testGetUebClusterEvent() {
159         AuditEventFactory factory = new AuditGetUebClusterEventFactory(
160                 CommonAuditData.newBuilder()
161                         .description(DESCRIPTION)
162                         .status(STATUS_OK)
163                         .requestId(REQUEST_ID)
164                         .serviceInstanceId(SERVICE_INSTANCE_ID)
165                         .build(),
166                 DIST_CONSUMER_ID);
167
168         when(auditingDao.addRecord(any(AuditingGenericEvent.class), eq(AuditingActionEnum.GET_UEB_CLUSTER.getAuditingEsType())))
169                 .thenReturn(ActionStatus.OK);
170         when(cassandraDao.saveRecord(any(AuditingGenericEvent.class))).thenReturn(CassandraOperationStatus.OK);
171
172         assertThat(auditingManager.auditEvent(factory)).contains(EXPECTED_GET_UEB_CLUSTER_LOG_STR);
173         verifyGetUebClusterEvent();
174     }
175
176     private void verifyNotifyEvent() {
177         verify(cassandraDao).saveRecord(eventCaptor.capture());
178         DistributionNotificationEvent storedEvent = (DistributionNotificationEvent) eventCaptor.getValue();
179         assertThat(storedEvent.getCurrState()).isEqualTo(CURRENT_STATE);
180         assertThat(storedEvent.getCurrVersion()).isEqualTo(CURRENT_VERSION);
181         assertThat(storedEvent.getDesc()).isEqualTo(DESCRIPTION);
182         assertThat(storedEvent.getStatus()).isEqualTo(STATUS_OK);
183         assertThat(storedEvent.getRequestId()).isEqualTo(REQUEST_ID);
184         assertThat(storedEvent.getServiceInstanceId()).isEqualTo(SERVICE_INSTANCE_ID);
185         assertThat(storedEvent.getAction()).isEqualTo(AuditingActionEnum.DISTRIBUTION_NOTIFY.getName());
186         assertThat(storedEvent.getDid()).isEqualTo(DIST_ID);
187         assertThat(storedEvent.getModifier()).isEqualTo(USER_UID);
188         assertThat(storedEvent.getResourceName()).isEqualTo(RESOURCE_NAME);
189         assertThat(storedEvent.getResourceType()).isEqualTo(RESOURCE_TYPE);
190         assertThat(storedEvent.getTopicName()).isEqualTo(TOPIC_NAME);
191         assertThat(storedEvent.getVnfWorkloadContext()).isEqualTo(VNF_WORKLOAD_CONTEXT);
192         assertThat(storedEvent.getEnvId()).isEqualTo(OP_ENV_ID);
193         assertThat(storedEvent.getTenant()).isEqualTo(TENANT_CONTEXT);
194     }
195
196     private void verifyStatusEvent() {
197         verify(cassandraDao).saveRecord(eventCaptor.capture());
198         DistributionStatusEvent storedEvent = (DistributionStatusEvent) eventCaptor.getValue();
199         assertThat(storedEvent.getConsumerId()).isEqualTo(DIST_CONSUMER_ID);
200         assertThat(storedEvent.getDid()).isEqualTo(DIST_ID);
201         assertThat(storedEvent.getDesc()).isEqualTo(DESCRIPTION);
202         assertThat(storedEvent.getStatus()).isEqualTo(STATUS_OK);
203         assertThat(storedEvent.getRequestId()).isEqualTo(REQUEST_ID);
204         assertThat(storedEvent.getServiceInstanceId()).isEqualTo(SERVICE_INSTANCE_ID);
205         assertThat(storedEvent.getAction()).isEqualTo(AuditingActionEnum.DISTRIBUTION_STATUS.getName());
206         assertThat(storedEvent.getStatusTime()).isEqualTo(DIST_STATUS_TIME);
207         assertThat(storedEvent.getResoureURL()).isEqualTo(DIST_RESOURCE_URL);
208         assertThat(storedEvent.getTopicName()).isEqualTo(TOPIC_NAME);
209     }
210
211     private void verifyDownloadsEvent() {
212         verify(cassandraDao).saveRecord(eventCaptor.capture());
213         DistributionDownloadEvent storedEvent = (DistributionDownloadEvent) eventCaptor.getValue();
214         assertThat(storedEvent.getConsumerId()).isEqualTo(DIST_CONSUMER_ID);
215         assertThat(storedEvent.getDesc()).isEqualTo(DESCRIPTION);
216         assertThat(storedEvent.getStatus()).isEqualTo(STATUS_OK);
217         assertThat(storedEvent.getRequestId()).isEqualTo(REQUEST_ID);
218         assertThat(storedEvent.getServiceInstanceId()).isEqualTo(SERVICE_INSTANCE_ID);
219         assertThat(storedEvent.getResourceUrl()).isEqualTo(DIST_RESOURCE_URL);
220     }
221
222     private void verifyDeployEvent() {
223         verify(cassandraDao).saveRecord(eventCaptor.capture());
224         DistributionDeployEvent storedEvent = (DistributionDeployEvent) eventCaptor.getValue();
225         assertThat(storedEvent.getCurrVersion()).isEqualTo(CURRENT_VERSION);
226         assertThat(storedEvent.getDesc()).isEqualTo(DESCRIPTION);
227         assertThat(storedEvent.getStatus()).isEqualTo(STATUS_OK);
228         assertThat(storedEvent.getRequestId()).isEqualTo(REQUEST_ID);
229         assertThat(storedEvent.getServiceInstanceId()).isEqualTo(SERVICE_INSTANCE_ID);
230         assertThat(storedEvent.getDid()).isEqualTo(DIST_ID);
231         assertThat(storedEvent.getModifier()).isEqualTo(USER_UID);
232         assertThat(storedEvent.getResourceName()).isEqualTo(RESOURCE_NAME);
233         assertThat(storedEvent.getResourceType()).isEqualTo(RESOURCE_TYPE);
234     }
235
236     private void verifyGetUebClusterEvent() {
237         verify(cassandraDao).saveRecord(eventCaptor.capture());
238         AuditingGetUebClusterEvent storedEvent = (AuditingGetUebClusterEvent) eventCaptor.getValue();
239         assertThat(storedEvent.getConsumerId()).isEqualTo(DIST_CONSUMER_ID);
240         assertThat(storedEvent.getDesc()).isEqualTo(DESCRIPTION);
241         assertThat(storedEvent.getStatus()).isEqualTo(STATUS_OK);
242         assertThat(storedEvent.getRequestId()).isEqualTo(REQUEST_ID);
243         assertThat(storedEvent.getServiceInstanceId()).isEqualTo(SERVICE_INSTANCE_ID);
244      }
245
246 }