re base code
[sdc.git] / catalog-be / src / test / java / org / openecomp / sdc / be / auditing / impl / distribution / AuditDistributionEventFuncTest.java
1 package org.openecomp.sdc.be.auditing.impl.distribution;
2
3 import org.junit.Before;
4 import org.junit.Test;
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;
21
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.*;
28
29 @RunWith(MockitoJUnitRunner.class)
30 public class AuditDistributionEventFuncTest {
31
32     private AuditingManager auditingManager;
33
34     @Mock
35     private static AuditCassandraDao cassandraDao;
36     @Mock
37     private static AuditingDao auditingDao;
38     @Mock
39     private static Configuration.ElasticSearchConfig esConfig;
40
41     @Captor
42     private ArgumentCaptor<AuditingGenericEvent> eventCaptor;
43
44     @Before
45     public void setUp() {
46         init(esConfig);
47         auditingManager = new AuditingManager(auditingDao, cassandraDao);
48         ThreadLocalsHolder.setUuid(REQUEST_ID);
49     }
50
51     @Test
52     public void testNotifyEvent() {
53         AuditEventFactory factory = new AuditDistributionNotificationEventFactory(
54                 CommonAuditData.newBuilder()
55                     .description(DESCRIPTION)
56                     .status(STATUS_OK)
57                     .requestId(REQUEST_ID)
58                     .serviceInstanceId(SERVICE_INSTANCE_ID)
59                     .build(),
60                 new ResourceCommonInfo(RESOURCE_NAME,RESOURCE_TYPE),
61                 ResourceVersionInfo.newBuilder()
62                     .state(CURRENT_STATE)
63                     .version(CURRENT_VERSION)
64                     .build(),
65                 DIST_ID, user, TOPIC_NAME,
66                 new OperationalEnvAuditData(OP_ENV_ID, VNF_WORKLOAD_CONTEXT, TENANT_CONTEXT));
67
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);
71
72         assertThat(auditingManager.auditEvent(factory)).isEqualTo(EXPECTED_DISTRIB_NOTIFICATION_LOG_STR);
73         verifyNotifyEvent();
74     }
75
76     @Test
77     public void testStatusEvent() {
78         AuditEventFactory factory = new AuditDistributionStatusEventFactory(
79                 CommonAuditData.newBuilder()
80                         .description(DESCRIPTION)
81                         .status(STATUS_OK)
82                         .requestId(REQUEST_ID)
83                         .serviceInstanceId(SERVICE_INSTANCE_ID)
84                         .build(),
85                 new DistributionData(DIST_CONSUMER_ID, DIST_RESOURCE_URL),
86                 DIST_ID, TOPIC_NAME, DIST_STATUS_TIME);
87
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);
91
92         assertThat(auditingManager.auditEvent(factory)).isEqualTo(EXPECTED_DIST_STATUS_LOG_STR);
93         verifyStatusEvent();
94     }
95
96     @Test
97     public void testDownloadEvent() {
98         AuditEventFactory factory = new AuditDistributionDownloadEventFactory(
99                 CommonAuditData.newBuilder()
100                         .description(DESCRIPTION)
101                         .status(STATUS_OK)
102                         .requestId(REQUEST_ID)
103                         .serviceInstanceId(SERVICE_INSTANCE_ID)
104                         .build(),
105                 new DistributionData(DIST_CONSUMER_ID, DIST_RESOURCE_URL));
106
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);
110
111         assertThat(auditingManager.auditEvent(factory)).isEqualTo(EXPECTED_DIST_DOWNLOAD_LOG_STR);
112         verifyDownloadsEvent();
113     }
114
115     @Test
116     public void testDeployEvent() {
117         AuditEventFactory factory = new AuditDistributionDeployEventFactory(
118                 CommonAuditData.newBuilder()
119                         .description(DESCRIPTION)
120                         .status(STATUS_OK)
121                         .requestId(REQUEST_ID)
122                         .serviceInstanceId(SERVICE_INSTANCE_ID)
123                         .build(),
124                 new ResourceCommonInfo(RESOURCE_NAME,RESOURCE_TYPE),
125                 DIST_ID, user, CURRENT_VERSION);
126
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);
130
131         assertThat(auditingManager.auditEvent(factory)).isEqualTo(EXPECTED_DISTRIB_DEPLOY_LOG_STR);
132         verifyDeployEvent();
133     }
134
135     @Test
136     public void testGetUebClusterEvent() {
137         AuditEventFactory factory = new AuditGetUebClusterEventFactory(
138                 CommonAuditData.newBuilder()
139                         .description(DESCRIPTION)
140                         .status(STATUS_OK)
141                         .requestId(REQUEST_ID)
142                         .serviceInstanceId(SERVICE_INSTANCE_ID)
143                         .build(),
144                 DIST_CONSUMER_ID);
145
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);
149
150         assertThat(auditingManager.auditEvent(factory)).contains(EXPECTED_GET_UEB_CLUSTER_LOG_STR);
151         verifyGetUebClusterEvent();
152     }
153
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);
172     }
173
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);
187     }
188
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);
198     }
199
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);
212     }
213
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);
222      }
223
224 }