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