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.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;
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.*;
51 @RunWith(MockitoJUnitRunner.class)
52 public class AuditDistributionEventFuncTest {
54 private AuditingManager auditingManager;
57 private static AuditCassandraDao cassandraDao;
59 private static AuditingDao auditingDao;
61 private static Configuration.ElasticSearchConfig esConfig;
64 private ArgumentCaptor<AuditingGenericEvent> eventCaptor;
69 auditingManager = new AuditingManager(auditingDao, cassandraDao, new TestConfigurationProvider());
70 ThreadLocalsHolder.setUuid(REQUEST_ID);
74 public void testNotifyEvent() {
75 AuditEventFactory factory = new AuditDistributionNotificationEventFactory(
76 CommonAuditData.newBuilder()
77 .description(DESCRIPTION)
79 .requestId(REQUEST_ID)
80 .serviceInstanceId(SERVICE_INSTANCE_ID)
82 new ResourceCommonInfo(RESOURCE_NAME,RESOURCE_TYPE),
83 ResourceVersionInfo.newBuilder()
85 .version(CURRENT_VERSION)
87 DIST_ID, user, TOPIC_NAME,
88 new OperationalEnvAuditData(OP_ENV_ID, VNF_WORKLOAD_CONTEXT, TENANT_CONTEXT));
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);
94 assertThat(auditingManager.auditEvent(factory)).isEqualTo(EXPECTED_DISTRIB_NOTIFICATION_LOG_STR);
99 public void testStatusEvent() {
100 AuditEventFactory factory = new AuditDistributionStatusEventFactory(
101 CommonAuditData.newBuilder()
102 .description(DESCRIPTION)
104 .requestId(REQUEST_ID)
105 .serviceInstanceId(SERVICE_INSTANCE_ID)
107 new DistributionData(DIST_CONSUMER_ID, DIST_RESOURCE_URL),
108 DIST_ID, TOPIC_NAME, DIST_STATUS_TIME);
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);
114 assertThat(auditingManager.auditEvent(factory)).isEqualTo(EXPECTED_DIST_STATUS_LOG_STR);
119 public void testDownloadEvent() {
120 AuditEventFactory factory = new AuditDistributionDownloadEventFactory(
121 CommonAuditData.newBuilder()
122 .description(DESCRIPTION)
124 .requestId(REQUEST_ID)
125 .serviceInstanceId(SERVICE_INSTANCE_ID)
127 new DistributionData(DIST_CONSUMER_ID, DIST_RESOURCE_URL));
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);
133 assertThat(auditingManager.auditEvent(factory)).isEqualTo(EXPECTED_DIST_DOWNLOAD_LOG_STR);
134 verifyDownloadsEvent();
138 public void testDeployEvent() {
139 AuditEventFactory factory = new AuditDistributionDeployEventFactory(
140 CommonAuditData.newBuilder()
141 .description(DESCRIPTION)
143 .requestId(REQUEST_ID)
144 .serviceInstanceId(SERVICE_INSTANCE_ID)
146 new ResourceCommonInfo(RESOURCE_NAME,RESOURCE_TYPE),
147 DIST_ID, user, CURRENT_VERSION);
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);
153 assertThat(auditingManager.auditEvent(factory)).isEqualTo(EXPECTED_DISTRIB_DEPLOY_LOG_STR);
158 public void testGetUebClusterEvent() {
159 AuditEventFactory factory = new AuditGetUebClusterEventFactory(
160 CommonAuditData.newBuilder()
161 .description(DESCRIPTION)
163 .requestId(REQUEST_ID)
164 .serviceInstanceId(SERVICE_INSTANCE_ID)
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);
172 assertThat(auditingManager.auditEvent(factory)).contains(EXPECTED_GET_UEB_CLUSTER_LOG_STR);
173 verifyGetUebClusterEvent();
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);
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);
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);
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);
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);