2 * ============LICENSE_START=======================================================
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
11 * http://www.apache.org/licenses/LICENSE-2.0
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 * ================================================================================
22 package org.openecomp.sdc.be.auditing.impl.distribution;
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.dao.cassandra.AuditCassandraDao;
35 import org.openecomp.sdc.be.dao.cassandra.CassandraOperationStatus;
36 import org.openecomp.sdc.be.resources.data.auditing.AuditingActionEnum;
37 import org.openecomp.sdc.be.resources.data.auditing.AuditingGenericEvent;
38 import org.openecomp.sdc.be.resources.data.auditing.AuditingGetUebClusterEvent;
39 import org.openecomp.sdc.be.resources.data.auditing.DistributionDeployEvent;
40 import org.openecomp.sdc.be.resources.data.auditing.DistributionDownloadEvent;
41 import org.openecomp.sdc.be.resources.data.auditing.DistributionNotificationEvent;
42 import org.openecomp.sdc.be.resources.data.auditing.DistributionStatusEvent;
43 import org.openecomp.sdc.be.resources.data.auditing.model.CommonAuditData;
44 import org.openecomp.sdc.be.resources.data.auditing.model.DistributionData;
45 import org.openecomp.sdc.be.resources.data.auditing.model.OperationalEnvAuditData;
46 import org.openecomp.sdc.be.resources.data.auditing.model.ResourceCommonInfo;
47 import org.openecomp.sdc.be.resources.data.auditing.model.ResourceVersionInfo;
48 import org.openecomp.sdc.common.util.ThreadLocalsHolder;
49 import org.openecomp.sdc.test.utils.TestConfigurationProvider;
51 import static org.assertj.core.api.Assertions.assertThat;
52 import static org.mockito.ArgumentMatchers.any;
53 import static org.mockito.Mockito.verify;
54 import static org.mockito.Mockito.when;
55 import static org.openecomp.sdc.be.auditing.impl.AuditTestUtils.CURRENT_STATE;
56 import static org.openecomp.sdc.be.auditing.impl.AuditTestUtils.CURRENT_VERSION;
57 import static org.openecomp.sdc.be.auditing.impl.AuditTestUtils.DESCRIPTION;
58 import static org.openecomp.sdc.be.auditing.impl.AuditTestUtils.DIST_CONSUMER_ID;
59 import static org.openecomp.sdc.be.auditing.impl.AuditTestUtils.DIST_ID;
60 import static org.openecomp.sdc.be.auditing.impl.AuditTestUtils.DIST_RESOURCE_URL;
61 import static org.openecomp.sdc.be.auditing.impl.AuditTestUtils.DIST_STATUS_TIME;
62 import static org.openecomp.sdc.be.auditing.impl.AuditTestUtils.EXPECTED_DISTRIB_DEPLOY_LOG_STR;
63 import static org.openecomp.sdc.be.auditing.impl.AuditTestUtils.EXPECTED_DISTRIB_NOTIFICATION_LOG_STR;
64 import static org.openecomp.sdc.be.auditing.impl.AuditTestUtils.EXPECTED_DIST_DOWNLOAD_LOG_STR;
65 import static org.openecomp.sdc.be.auditing.impl.AuditTestUtils.EXPECTED_DIST_STATUS_LOG_STR;
66 import static org.openecomp.sdc.be.auditing.impl.AuditTestUtils.EXPECTED_GET_UEB_CLUSTER_LOG_STR;
67 import static org.openecomp.sdc.be.auditing.impl.AuditTestUtils.OP_ENV_ID;
68 import static org.openecomp.sdc.be.auditing.impl.AuditTestUtils.REQUEST_ID;
69 import static org.openecomp.sdc.be.auditing.impl.AuditTestUtils.RESOURCE_NAME;
70 import static org.openecomp.sdc.be.auditing.impl.AuditTestUtils.RESOURCE_TYPE;
71 import static org.openecomp.sdc.be.auditing.impl.AuditTestUtils.SERVICE_INSTANCE_ID;
72 import static org.openecomp.sdc.be.auditing.impl.AuditTestUtils.STATUS_OK;
73 import static org.openecomp.sdc.be.auditing.impl.AuditTestUtils.TENANT_CONTEXT;
74 import static org.openecomp.sdc.be.auditing.impl.AuditTestUtils.TOPIC_NAME;
75 import static org.openecomp.sdc.be.auditing.impl.AuditTestUtils.USER_UID;
76 import static org.openecomp.sdc.be.auditing.impl.AuditTestUtils.VNF_WORKLOAD_CONTEXT;
77 import static org.openecomp.sdc.be.auditing.impl.AuditTestUtils.init;
78 import static org.openecomp.sdc.be.auditing.impl.AuditTestUtils.user;
80 @RunWith(MockitoJUnitRunner.class)
81 public class AuditDistributionEventFuncTest {
83 private AuditingManager auditingManager;
86 private static AuditCassandraDao cassandraDao;
89 private ArgumentCaptor<AuditingGenericEvent> eventCaptor;
94 auditingManager = new AuditingManager(cassandraDao, new TestConfigurationProvider());
95 ThreadLocalsHolder.setUuid(REQUEST_ID);
99 public void testNotifyEvent() {
100 AuditEventFactory factory = new AuditDistributionNotificationEventFactory(
101 CommonAuditData.newBuilder()
102 .description(DESCRIPTION)
104 .requestId(REQUEST_ID)
105 .serviceInstanceId(SERVICE_INSTANCE_ID)
107 new ResourceCommonInfo(RESOURCE_NAME,RESOURCE_TYPE),
108 ResourceVersionInfo.newBuilder()
109 .state(CURRENT_STATE)
110 .version(CURRENT_VERSION)
112 DIST_ID, user, TOPIC_NAME,
113 new OperationalEnvAuditData(OP_ENV_ID, VNF_WORKLOAD_CONTEXT, TENANT_CONTEXT));
115 when(cassandraDao.saveRecord(any(AuditingGenericEvent.class))).thenReturn(CassandraOperationStatus.OK);
117 assertThat(auditingManager.auditEvent(factory)).isEqualTo(EXPECTED_DISTRIB_NOTIFICATION_LOG_STR);
122 public void testStatusEvent() {
123 AuditEventFactory factory = new AuditDistributionStatusEventFactory(
124 CommonAuditData.newBuilder()
125 .description(DESCRIPTION)
127 .requestId(REQUEST_ID)
128 .serviceInstanceId(SERVICE_INSTANCE_ID)
130 new DistributionData(DIST_CONSUMER_ID, DIST_RESOURCE_URL),
131 DIST_ID, TOPIC_NAME, DIST_STATUS_TIME);
133 when(cassandraDao.saveRecord(any(AuditingGenericEvent.class))).thenReturn(CassandraOperationStatus.OK);
135 assertThat(auditingManager.auditEvent(factory)).isEqualTo(EXPECTED_DIST_STATUS_LOG_STR);
140 public void testDownloadEvent() {
141 AuditEventFactory factory = new AuditDistributionDownloadEventFactory(
142 CommonAuditData.newBuilder()
143 .description(DESCRIPTION)
145 .requestId(REQUEST_ID)
146 .serviceInstanceId(SERVICE_INSTANCE_ID)
148 new DistributionData(DIST_CONSUMER_ID, DIST_RESOURCE_URL));
150 when(cassandraDao.saveRecord(any(AuditingGenericEvent.class))).thenReturn(CassandraOperationStatus.OK);
152 assertThat(auditingManager.auditEvent(factory)).isEqualTo(EXPECTED_DIST_DOWNLOAD_LOG_STR);
153 verifyDownloadsEvent();
157 public void testDeployEvent() {
158 AuditEventFactory factory = new AuditDistributionDeployEventFactory(
159 CommonAuditData.newBuilder()
160 .description(DESCRIPTION)
162 .requestId(REQUEST_ID)
163 .serviceInstanceId(SERVICE_INSTANCE_ID)
165 new ResourceCommonInfo(RESOURCE_NAME,RESOURCE_TYPE),
166 DIST_ID, user, CURRENT_VERSION);
168 when(cassandraDao.saveRecord(any(AuditingGenericEvent.class))).thenReturn(CassandraOperationStatus.OK);
170 assertThat(auditingManager.auditEvent(factory)).isEqualTo(EXPECTED_DISTRIB_DEPLOY_LOG_STR);
175 public void testGetUebClusterEvent() {
176 AuditEventFactory factory = new AuditGetUebClusterEventFactory(
177 CommonAuditData.newBuilder()
178 .description(DESCRIPTION)
180 .requestId(REQUEST_ID)
181 .serviceInstanceId(SERVICE_INSTANCE_ID)
185 when(cassandraDao.saveRecord(any(AuditingGenericEvent.class))).thenReturn(CassandraOperationStatus.OK);
187 assertThat(auditingManager.auditEvent(factory)).contains(EXPECTED_GET_UEB_CLUSTER_LOG_STR);
188 verifyGetUebClusterEvent();
191 private void verifyNotifyEvent() {
192 verify(cassandraDao).saveRecord(eventCaptor.capture());
193 DistributionNotificationEvent storedEvent = (DistributionNotificationEvent) eventCaptor.getValue();
194 assertThat(storedEvent.getCurrState()).isEqualTo(CURRENT_STATE);
195 assertThat(storedEvent.getCurrVersion()).isEqualTo(CURRENT_VERSION);
196 assertThat(storedEvent.getDesc()).isEqualTo(DESCRIPTION);
197 assertThat(storedEvent.getStatus()).isEqualTo(STATUS_OK);
198 assertThat(storedEvent.getRequestId()).isEqualTo(REQUEST_ID);
199 assertThat(storedEvent.getServiceInstanceId()).isEqualTo(SERVICE_INSTANCE_ID);
200 assertThat(storedEvent.getAction()).isEqualTo(AuditingActionEnum.DISTRIBUTION_NOTIFY.getName());
201 assertThat(storedEvent.getDid()).isEqualTo(DIST_ID);
202 assertThat(storedEvent.getModifier()).isEqualTo(USER_UID);
203 assertThat(storedEvent.getResourceName()).isEqualTo(RESOURCE_NAME);
204 assertThat(storedEvent.getResourceType()).isEqualTo(RESOURCE_TYPE);
205 assertThat(storedEvent.getTopicName()).isEqualTo(TOPIC_NAME);
206 assertThat(storedEvent.getVnfWorkloadContext()).isEqualTo(VNF_WORKLOAD_CONTEXT);
207 assertThat(storedEvent.getEnvId()).isEqualTo(OP_ENV_ID);
208 assertThat(storedEvent.getTenant()).isEqualTo(TENANT_CONTEXT);
211 private void verifyStatusEvent() {
212 verify(cassandraDao).saveRecord(eventCaptor.capture());
213 DistributionStatusEvent storedEvent = (DistributionStatusEvent) eventCaptor.getValue();
214 assertThat(storedEvent.getConsumerId()).isEqualTo(DIST_CONSUMER_ID);
215 assertThat(storedEvent.getDid()).isEqualTo(DIST_ID);
216 assertThat(storedEvent.getDesc()).isEqualTo(DESCRIPTION);
217 assertThat(storedEvent.getStatus()).isEqualTo(STATUS_OK);
218 assertThat(storedEvent.getRequestId()).isEqualTo(REQUEST_ID);
219 assertThat(storedEvent.getServiceInstanceId()).isEqualTo(SERVICE_INSTANCE_ID);
220 assertThat(storedEvent.getAction()).isEqualTo(AuditingActionEnum.DISTRIBUTION_STATUS.getName());
221 assertThat(storedEvent.getStatusTime()).isEqualTo(DIST_STATUS_TIME);
222 assertThat(storedEvent.getResoureURL()).isEqualTo(DIST_RESOURCE_URL);
223 assertThat(storedEvent.getTopicName()).isEqualTo(TOPIC_NAME);
226 private void verifyDownloadsEvent() {
227 verify(cassandraDao).saveRecord(eventCaptor.capture());
228 DistributionDownloadEvent storedEvent = (DistributionDownloadEvent) eventCaptor.getValue();
229 assertThat(storedEvent.getConsumerId()).isEqualTo(DIST_CONSUMER_ID);
230 assertThat(storedEvent.getDesc()).isEqualTo(DESCRIPTION);
231 assertThat(storedEvent.getStatus()).isEqualTo(STATUS_OK);
232 assertThat(storedEvent.getRequestId()).isEqualTo(REQUEST_ID);
233 assertThat(storedEvent.getServiceInstanceId()).isEqualTo(SERVICE_INSTANCE_ID);
234 assertThat(storedEvent.getResourceUrl()).isEqualTo(DIST_RESOURCE_URL);
237 private void verifyDeployEvent() {
238 verify(cassandraDao).saveRecord(eventCaptor.capture());
239 DistributionDeployEvent storedEvent = (DistributionDeployEvent) eventCaptor.getValue();
240 assertThat(storedEvent.getCurrVersion()).isEqualTo(CURRENT_VERSION);
241 assertThat(storedEvent.getDesc()).isEqualTo(DESCRIPTION);
242 assertThat(storedEvent.getStatus()).isEqualTo(STATUS_OK);
243 assertThat(storedEvent.getRequestId()).isEqualTo(REQUEST_ID);
244 assertThat(storedEvent.getServiceInstanceId()).isEqualTo(SERVICE_INSTANCE_ID);
245 assertThat(storedEvent.getDid()).isEqualTo(DIST_ID);
246 assertThat(storedEvent.getModifier()).isEqualTo(USER_UID);
247 assertThat(storedEvent.getResourceName()).isEqualTo(RESOURCE_NAME);
248 assertThat(storedEvent.getResourceType()).isEqualTo(RESOURCE_TYPE);
251 private void verifyGetUebClusterEvent() {
252 verify(cassandraDao).saveRecord(eventCaptor.capture());
253 AuditingGetUebClusterEvent storedEvent = (AuditingGetUebClusterEvent) eventCaptor.getValue();
254 assertThat(storedEvent.getConsumerId()).isEqualTo(DIST_CONSUMER_ID);
255 assertThat(storedEvent.getDesc()).isEqualTo(DESCRIPTION);
256 assertThat(storedEvent.getStatus()).isEqualTo(STATUS_OK);
257 assertThat(storedEvent.getRequestId()).isEqualTo(REQUEST_ID);
258 assertThat(storedEvent.getServiceInstanceId()).isEqualTo(SERVICE_INSTANCE_ID);