1 package org.openecomp.sdc.be.auditing.impl.distribution;
3 import org.junit.Before;
5 import org.junit.runner.RunWith;
6 import org.mockito.ArgumentCaptor;
7 import org.mockito.Captor;
8 import org.mockito.Mock;
9 import org.mockito.junit.MockitoJUnitRunner;
10 import org.openecomp.sdc.be.auditing.api.AuditEventFactory;
11 import org.openecomp.sdc.be.auditing.impl.AuditGetUebClusterEventFactory;
12 import org.openecomp.sdc.be.auditing.impl.AuditingManager;
13 import org.openecomp.sdc.be.config.Configuration;
14 import org.openecomp.sdc.be.dao.api.ActionStatus;
15 import org.openecomp.sdc.be.dao.cassandra.AuditCassandraDao;
16 import org.openecomp.sdc.be.dao.cassandra.CassandraOperationStatus;
17 import org.openecomp.sdc.be.dao.impl.AuditingDao;
18 import org.openecomp.sdc.be.resources.data.auditing.*;
19 import org.openecomp.sdc.be.resources.data.auditing.model.*;
20 import org.openecomp.sdc.common.util.ThreadLocalsHolder;
22 import static org.assertj.core.api.Assertions.assertThat;
23 import static org.mockito.ArgumentMatchers.any;
24 import static org.mockito.ArgumentMatchers.eq;
25 import static org.mockito.Mockito.verify;
26 import static org.mockito.Mockito.when;
27 import static org.openecomp.sdc.be.auditing.impl.AuditTestUtils.*;
29 @RunWith(MockitoJUnitRunner.class)
30 public class AuditDistributionEventFuncTest {
32 private AuditingManager auditingManager;
35 private static AuditCassandraDao cassandraDao;
37 private static AuditingDao auditingDao;
39 private static Configuration.ElasticSearchConfig esConfig;
42 private ArgumentCaptor<AuditingGenericEvent> eventCaptor;
47 auditingManager = new AuditingManager(auditingDao, cassandraDao);
48 ThreadLocalsHolder.setUuid(REQUEST_ID);
52 public void testNotifyEvent() {
53 AuditEventFactory factory = new AuditDistributionNotificationEventFactory(
54 CommonAuditData.newBuilder()
55 .description(DESCRIPTION)
57 .requestId(REQUEST_ID)
58 .serviceInstanceId(SERVICE_INSTANCE_ID)
60 new ResourceCommonInfo(RESOURCE_NAME,RESOURCE_TYPE),
61 ResourceVersionInfo.newBuilder()
63 .version(CURRENT_VERSION)
65 DIST_ID, user, TOPIC_NAME,
66 new OperationalEnvAuditData(OP_ENV_ID, VNF_WORKLOAD_CONTEXT, TENANT_CONTEXT));
68 when(auditingDao.addRecord(any(AuditingGenericEvent.class), eq(AuditingActionEnum.DISTRIBUTION_NOTIFY.getAuditingEsType())))
69 .thenReturn(ActionStatus.OK);
70 when(cassandraDao.saveRecord(any(AuditingGenericEvent.class))).thenReturn(CassandraOperationStatus.OK);
72 assertThat(auditingManager.auditEvent(factory)).isEqualTo(EXPECTED_DISTRIB_NOTIFICATION_LOG_STR);
77 public void testStatusEvent() {
78 AuditEventFactory factory = new AuditDistributionStatusEventFactory(
79 CommonAuditData.newBuilder()
80 .description(DESCRIPTION)
82 .requestId(REQUEST_ID)
83 .serviceInstanceId(SERVICE_INSTANCE_ID)
85 new DistributionData(DIST_CONSUMER_ID, DIST_RESOURCE_URL),
86 DIST_ID, TOPIC_NAME, DIST_STATUS_TIME);
88 when(auditingDao.addRecord(any(AuditingGenericEvent.class), eq(AuditingActionEnum.DISTRIBUTION_STATUS.getAuditingEsType())))
89 .thenReturn(ActionStatus.OK);
90 when(cassandraDao.saveRecord(any(AuditingGenericEvent.class))).thenReturn(CassandraOperationStatus.OK);
92 assertThat(auditingManager.auditEvent(factory)).isEqualTo(EXPECTED_DIST_STATUS_LOG_STR);
97 public void testDownloadEvent() {
98 AuditEventFactory factory = new AuditDistributionDownloadEventFactory(
99 CommonAuditData.newBuilder()
100 .description(DESCRIPTION)
102 .requestId(REQUEST_ID)
103 .serviceInstanceId(SERVICE_INSTANCE_ID)
105 new DistributionData(DIST_CONSUMER_ID, DIST_RESOURCE_URL));
107 when(auditingDao.addRecord(any(AuditingGenericEvent.class), eq(AuditingActionEnum.DISTRIBUTION_ARTIFACT_DOWNLOAD.getAuditingEsType())))
108 .thenReturn(ActionStatus.OK);
109 when(cassandraDao.saveRecord(any(AuditingGenericEvent.class))).thenReturn(CassandraOperationStatus.OK);
111 assertThat(auditingManager.auditEvent(factory)).isEqualTo(EXPECTED_DIST_DOWNLOAD_LOG_STR);
112 verifyDownloadsEvent();
116 public void testDeployEvent() {
117 AuditEventFactory factory = new AuditDistributionDeployEventFactory(
118 CommonAuditData.newBuilder()
119 .description(DESCRIPTION)
121 .requestId(REQUEST_ID)
122 .serviceInstanceId(SERVICE_INSTANCE_ID)
124 new ResourceCommonInfo(RESOURCE_NAME,RESOURCE_TYPE),
125 DIST_ID, user, CURRENT_VERSION);
127 when(auditingDao.addRecord(any(AuditingGenericEvent.class), eq(AuditingActionEnum.DISTRIBUTION_DEPLOY.getAuditingEsType())))
128 .thenReturn(ActionStatus.OK);
129 when(cassandraDao.saveRecord(any(AuditingGenericEvent.class))).thenReturn(CassandraOperationStatus.OK);
131 assertThat(auditingManager.auditEvent(factory)).isEqualTo(EXPECTED_DISTRIB_DEPLOY_LOG_STR);
136 public void testGetUebClusterEvent() {
137 AuditEventFactory factory = new AuditGetUebClusterEventFactory(
138 CommonAuditData.newBuilder()
139 .description(DESCRIPTION)
141 .requestId(REQUEST_ID)
142 .serviceInstanceId(SERVICE_INSTANCE_ID)
146 when(auditingDao.addRecord(any(AuditingGenericEvent.class), eq(AuditingActionEnum.GET_UEB_CLUSTER.getAuditingEsType())))
147 .thenReturn(ActionStatus.OK);
148 when(cassandraDao.saveRecord(any(AuditingGenericEvent.class))).thenReturn(CassandraOperationStatus.OK);
150 assertThat(auditingManager.auditEvent(factory)).contains(EXPECTED_GET_UEB_CLUSTER_LOG_STR);
151 verifyGetUebClusterEvent();
154 private void verifyNotifyEvent() {
155 verify(cassandraDao).saveRecord(eventCaptor.capture());
156 DistributionNotificationEvent storedEvent = (DistributionNotificationEvent) eventCaptor.getValue();
157 assertThat(storedEvent.getCurrState()).isEqualTo(CURRENT_STATE);
158 assertThat(storedEvent.getCurrVersion()).isEqualTo(CURRENT_VERSION);
159 assertThat(storedEvent.getDesc()).isEqualTo(DESCRIPTION);
160 assertThat(storedEvent.getStatus()).isEqualTo(STATUS_OK);
161 assertThat(storedEvent.getRequestId()).isEqualTo(REQUEST_ID);
162 assertThat(storedEvent.getServiceInstanceId()).isEqualTo(SERVICE_INSTANCE_ID);
163 assertThat(storedEvent.getAction()).isEqualTo(AuditingActionEnum.DISTRIBUTION_NOTIFY.getName());
164 assertThat(storedEvent.getDid()).isEqualTo(DIST_ID);
165 assertThat(storedEvent.getModifier()).isEqualTo(USER_UID);
166 assertThat(storedEvent.getResourceName()).isEqualTo(RESOURCE_NAME);
167 assertThat(storedEvent.getResourceType()).isEqualTo(RESOURCE_TYPE);
168 assertThat(storedEvent.getTopicName()).isEqualTo(TOPIC_NAME);
169 assertThat(storedEvent.getVnfWorkloadContext()).isEqualTo(VNF_WORKLOAD_CONTEXT);
170 assertThat(storedEvent.getEnvId()).isEqualTo(OP_ENV_ID);
171 assertThat(storedEvent.getTenant()).isEqualTo(TENANT_CONTEXT);
174 private void verifyStatusEvent() {
175 verify(cassandraDao).saveRecord(eventCaptor.capture());
176 DistributionStatusEvent storedEvent = (DistributionStatusEvent) eventCaptor.getValue();
177 assertThat(storedEvent.getConsumerId()).isEqualTo(DIST_CONSUMER_ID);
178 assertThat(storedEvent.getDid()).isEqualTo(DIST_ID);
179 assertThat(storedEvent.getDesc()).isEqualTo(DESCRIPTION);
180 assertThat(storedEvent.getStatus()).isEqualTo(STATUS_OK);
181 assertThat(storedEvent.getRequestId()).isEqualTo(REQUEST_ID);
182 assertThat(storedEvent.getServiceInstanceId()).isEqualTo(SERVICE_INSTANCE_ID);
183 assertThat(storedEvent.getAction()).isEqualTo(AuditingActionEnum.DISTRIBUTION_STATUS.getName());
184 assertThat(storedEvent.getStatusTime()).isEqualTo(DIST_STATUS_TIME);
185 assertThat(storedEvent.getResoureURL()).isEqualTo(DIST_RESOURCE_URL);
186 assertThat(storedEvent.getTopicName()).isEqualTo(TOPIC_NAME);
189 private void verifyDownloadsEvent() {
190 verify(cassandraDao).saveRecord(eventCaptor.capture());
191 DistributionDownloadEvent storedEvent = (DistributionDownloadEvent) eventCaptor.getValue();
192 assertThat(storedEvent.getConsumerId()).isEqualTo(DIST_CONSUMER_ID);
193 assertThat(storedEvent.getDesc()).isEqualTo(DESCRIPTION);
194 assertThat(storedEvent.getStatus()).isEqualTo(STATUS_OK);
195 assertThat(storedEvent.getRequestId()).isEqualTo(REQUEST_ID);
196 assertThat(storedEvent.getServiceInstanceId()).isEqualTo(SERVICE_INSTANCE_ID);
197 assertThat(storedEvent.getResourceUrl()).isEqualTo(DIST_RESOURCE_URL);
200 private void verifyDeployEvent() {
201 verify(cassandraDao).saveRecord(eventCaptor.capture());
202 DistributionDeployEvent storedEvent = (DistributionDeployEvent) eventCaptor.getValue();
203 assertThat(storedEvent.getCurrVersion()).isEqualTo(CURRENT_VERSION);
204 assertThat(storedEvent.getDesc()).isEqualTo(DESCRIPTION);
205 assertThat(storedEvent.getStatus()).isEqualTo(STATUS_OK);
206 assertThat(storedEvent.getRequestId()).isEqualTo(REQUEST_ID);
207 assertThat(storedEvent.getServiceInstanceId()).isEqualTo(SERVICE_INSTANCE_ID);
208 assertThat(storedEvent.getDid()).isEqualTo(DIST_ID);
209 assertThat(storedEvent.getModifier()).isEqualTo(USER_UID);
210 assertThat(storedEvent.getResourceName()).isEqualTo(RESOURCE_NAME);
211 assertThat(storedEvent.getResourceType()).isEqualTo(RESOURCE_TYPE);
214 private void verifyGetUebClusterEvent() {
215 verify(cassandraDao).saveRecord(eventCaptor.capture());
216 AuditingGetUebClusterEvent storedEvent = (AuditingGetUebClusterEvent) eventCaptor.getValue();
217 assertThat(storedEvent.getConsumerId()).isEqualTo(DIST_CONSUMER_ID);
218 assertThat(storedEvent.getDesc()).isEqualTo(DESCRIPTION);
219 assertThat(storedEvent.getStatus()).isEqualTo(STATUS_OK);
220 assertThat(storedEvent.getRequestId()).isEqualTo(REQUEST_ID);
221 assertThat(storedEvent.getServiceInstanceId()).isEqualTo(SERVICE_INSTANCE_ID);