Catalog alignment
[sdc.git] / catalog-be / src / test / java / org / openecomp / sdc / be / auditing / impl / distribution / AuditDistributionEngineFuncTest.java
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.AuditingManager;
33 import org.openecomp.sdc.be.dao.cassandra.AuditCassandraDao;
34 import org.openecomp.sdc.be.dao.cassandra.CassandraOperationStatus;
35 import org.openecomp.sdc.be.resources.data.auditing.AuditingActionEnum;
36 import org.openecomp.sdc.be.resources.data.auditing.AuditingGenericEvent;
37 import org.openecomp.sdc.be.resources.data.auditing.DistributionEngineEvent;
38 import org.openecomp.sdc.be.resources.data.auditing.model.CommonAuditData;
39 import org.openecomp.sdc.be.resources.data.auditing.model.DistributionTopicData;
40 import org.openecomp.sdc.common.util.ThreadLocalsHolder;
41 import org.openecomp.sdc.test.utils.TestConfigurationProvider;
42
43 import static org.assertj.core.api.Assertions.assertThat;
44 import static org.mockito.ArgumentMatchers.any;
45 import static org.mockito.Mockito.verify;
46 import static org.mockito.Mockito.when;
47 import static org.openecomp.sdc.be.auditing.impl.AuditTestUtils.DESCRIPTION;
48 import static org.openecomp.sdc.be.auditing.impl.AuditTestUtils.DIST_API_KEY;
49 import static org.openecomp.sdc.be.auditing.impl.AuditTestUtils.DIST_CONSUMER_ID;
50 import static org.openecomp.sdc.be.auditing.impl.AuditTestUtils.DIST_ENV_NAME;
51 import static org.openecomp.sdc.be.auditing.impl.AuditTestUtils.DIST_NOTIFY_TOPIC;
52 import static org.openecomp.sdc.be.auditing.impl.AuditTestUtils.DIST_ROLE;
53 import static org.openecomp.sdc.be.auditing.impl.AuditTestUtils.DIST_STATUS_TOPIC;
54 import static org.openecomp.sdc.be.auditing.impl.AuditTestUtils.EXPECTED_DIST_ADD_KEY_ENGINE_LOG_STR;
55 import static org.openecomp.sdc.be.auditing.impl.AuditTestUtils.EXPECTED_DIST_CREATE_TOPIC_ENGINE_LOG_STR;
56 import static org.openecomp.sdc.be.auditing.impl.AuditTestUtils.EXPECTED_DIST_REG_ENGINE_LOG_STR;
57 import static org.openecomp.sdc.be.auditing.impl.AuditTestUtils.REQUEST_ID;
58 import static org.openecomp.sdc.be.auditing.impl.AuditTestUtils.SERVICE_INSTANCE_ID;
59 import static org.openecomp.sdc.be.auditing.impl.AuditTestUtils.STATUS_OK;
60 import static org.openecomp.sdc.be.auditing.impl.AuditTestUtils.init;
61
62 @RunWith(MockitoJUnitRunner.class)
63 public class AuditDistributionEngineFuncTest {
64     private AuditingManager auditingManager;
65
66     @Mock
67     private static AuditCassandraDao cassandraDao;
68
69     @Captor
70     private ArgumentCaptor<DistributionEngineEvent> eventCaptor;
71
72     @Before
73     public void setUp() {
74         init();
75         auditingManager = new AuditingManager(cassandraDao, new TestConfigurationProvider());
76         ThreadLocalsHolder.setUuid(REQUEST_ID);
77     }
78
79     @Test
80     public void testAddKeyEvent() {
81         AuditEventFactory factory = new AuditAddRemoveKeyDistributionEngineEventFactory(
82                 AuditingActionEnum.ADD_KEY_TO_TOPIC_ACL,
83                 CommonAuditData.newBuilder()
84                         .description(DESCRIPTION)
85                         .status(STATUS_OK)
86                         .requestId(REQUEST_ID)
87                         .serviceInstanceId(SERVICE_INSTANCE_ID)
88                         .build(),
89                 DistributionTopicData.newBuilder()
90                         .statusTopic(DIST_STATUS_TOPIC)
91                         .notificationTopic(DIST_NOTIFY_TOPIC)
92                         .build(),
93                 DIST_API_KEY, DIST_ENV_NAME, DIST_ROLE);
94
95         when(cassandraDao.saveRecord(any(AuditingGenericEvent.class))).thenReturn(CassandraOperationStatus.OK);
96
97         assertThat(auditingManager.auditEvent(factory)).isEqualTo(EXPECTED_DIST_ADD_KEY_ENGINE_LOG_STR);
98         verifyEvent(AuditingActionEnum.ADD_KEY_TO_TOPIC_ACL.getName());
99     }
100
101     @Test
102     public void testCreateTopicEvent() {
103         AuditEventFactory factory = new AuditCreateTopicDistributionEngineEventFactory(
104                 CommonAuditData.newBuilder()
105                         .description(DESCRIPTION)
106                         .status(STATUS_OK)
107                         .requestId(REQUEST_ID)
108                         .serviceInstanceId(SERVICE_INSTANCE_ID)
109                         .build(),
110                 DistributionTopicData.newBuilder()
111                         .statusTopic(DIST_STATUS_TOPIC)
112                         .notificationTopic(DIST_NOTIFY_TOPIC)
113                         .build(),
114                 DIST_API_KEY, DIST_ENV_NAME, DIST_ROLE);
115
116         when(cassandraDao.saveRecord(any(AuditingGenericEvent.class))).thenReturn(CassandraOperationStatus.OK);
117
118         assertThat(auditingManager.auditEvent(factory)).isEqualTo(EXPECTED_DIST_CREATE_TOPIC_ENGINE_LOG_STR);
119         verifyEvent(AuditingActionEnum.CREATE_DISTRIBUTION_TOPIC.getName());
120     }
121
122     @Test
123     public void testRegisterEvent() {
124         AuditEventFactory factory = new AuditRegUnregDistributionEngineEventFactory(
125                 AuditingActionEnum.DISTRIBUTION_REGISTER,
126                 CommonAuditData.newBuilder()
127                         .description(DESCRIPTION)
128                         .status(STATUS_OK)
129                         .requestId(REQUEST_ID)
130                         .serviceInstanceId(SERVICE_INSTANCE_ID)
131                         .build(),
132                 DistributionTopicData.newBuilder()
133                         .statusTopic(DIST_STATUS_TOPIC)
134                         .notificationTopic(DIST_NOTIFY_TOPIC)
135                         .build(),
136                 DIST_CONSUMER_ID, DIST_API_KEY, DIST_ENV_NAME);
137
138         when(cassandraDao.saveRecord(any(AuditingGenericEvent.class))).thenReturn(CassandraOperationStatus.OK);
139
140         assertThat(auditingManager.auditEvent(factory)).isEqualTo(EXPECTED_DIST_REG_ENGINE_LOG_STR);
141         verifyEvent(AuditingActionEnum.DISTRIBUTION_REGISTER.getName());
142     }
143
144     private void verifyEvent(String action) {
145         verify(cassandraDao).saveRecord(eventCaptor.capture());
146         DistributionEngineEvent storedEvent = eventCaptor.getValue();
147         assertThat(storedEvent.getDnotifTopic()).isEqualTo(DIST_NOTIFY_TOPIC);
148         assertThat(storedEvent.getDstatusTopic()).isEqualTo(DIST_STATUS_TOPIC);
149         assertThat(storedEvent.getDesc()).isEqualTo(DESCRIPTION);
150         assertThat(storedEvent.getStatus()).isEqualTo(STATUS_OK);
151         assertThat(storedEvent.getRequestId()).isEqualTo(REQUEST_ID);
152         assertThat(storedEvent.getServiceInstanceId()).isEqualTo(SERVICE_INSTANCE_ID);
153         assertThat(storedEvent.getAction()).isEqualTo(action);
154         assertThat(storedEvent.getEnvironmentName()).isEqualTo(DIST_ENV_NAME);
155         assertThat(storedEvent.getApiKey()).isEqualTo(DIST_API_KEY);
156         if (!action.equals(AuditingActionEnum.CREATE_DISTRIBUTION_TOPIC.getName()) &&
157             !action.equals(AuditingActionEnum.ADD_KEY_TO_TOPIC_ACL.getName()) &&
158             !action.equals(AuditingActionEnum.REMOVE_KEY_FROM_TOPIC_ACL.getName())) {
159             assertThat(storedEvent.getConsumerId()).isEqualTo(DIST_CONSUMER_ID);
160         }
161         if (!action.equals(AuditingActionEnum.DISTRIBUTION_REGISTER.getName()) &&
162                 !action.equals(AuditingActionEnum.DISTRIBUTION_UN_REGISTER.getName())) {
163             assertThat(storedEvent.getRole()).isEqualTo(DIST_ROLE);
164         }
165     }
166
167 }