ebae81684586b8e33ca71e6d12782dbaf2ea4f33
[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.ecompopenv;
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.AuditEcompOpEnvEventFactory;
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.AuditingActionEnum;
40 import org.openecomp.sdc.be.resources.data.auditing.AuditingGenericEvent;
41 import org.openecomp.sdc.be.resources.data.auditing.EcompOperationalEnvironmentEvent;
42 import org.openecomp.sdc.test.utils.TestConfigurationProvider;
43
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.*;
50
51 @RunWith(MockitoJUnitRunner.class)
52 public class AuditEcompOpEnvEventTest {
53
54     private AuditingManager auditingManager;
55
56     @Mock
57     private static AuditCassandraDao cassandraDao;
58     @Mock
59     private static AuditingDao auditingDao;
60     @Mock
61     private static Configuration.ElasticSearchConfig esConfig;
62
63     @Captor
64     private ArgumentCaptor<EcompOperationalEnvironmentEvent> eventCaptor;
65
66     @Before
67     public void setUp() {
68         init(esConfig);
69         auditingManager = new AuditingManager(auditingDao, cassandraDao, new TestConfigurationProvider());
70     }
71
72     @Test
73     public void testCreateOpEnvEvent() {
74         AuditEventFactory factory = new AuditEcompOpEnvEventFactory(AuditingActionEnum.CREATE_ENVIRONMENT,
75                 OP_ENV_ID, OP_ENV_NAME, OP_ENV_TYPE, OP_ENV_ACTION, TENANT_CONTEXT);
76
77         when(auditingDao.addRecord(any(AuditingGenericEvent.class), eq(AuditingActionEnum.CREATE_ENVIRONMENT.getAuditingEsType())))
78                 .thenReturn(ActionStatus.OK);
79         when(cassandraDao.saveRecord(any(AuditingGenericEvent.class))).thenReturn(CassandraOperationStatus.OK);
80
81         assertThat(auditingManager.auditEvent(factory)).isEqualTo(EXPECTED_CREATE_OP_ENV_LOG_STR);
82         verifyEvent(AuditingActionEnum.CREATE_ENVIRONMENT.getName());
83     }
84
85     @Test
86     public void testUnsupportedTypeOpEnvEvent() {
87         AuditEventFactory factory = new AuditEcompOpEnvEventFactory(AuditingActionEnum.UNSUPPORTED_ENVIRONMENT_TYPE,
88                 OP_ENV_ID, OP_ENV_NAME, OP_ENV_TYPE, OP_ENV_ACTION, TENANT_CONTEXT);
89
90         when(auditingDao.addRecord(any(AuditingGenericEvent.class), eq(AuditingActionEnum.UNSUPPORTED_ENVIRONMENT_TYPE.getAuditingEsType())))
91                 .thenReturn(ActionStatus.OK);
92         when(cassandraDao.saveRecord(any(AuditingGenericEvent.class))).thenReturn(CassandraOperationStatus.OK);
93
94         assertThat(auditingManager.auditEvent(factory)).isEqualTo(EXPECTED_UNSUPPORTED_TYPE_OP_ENV_LOG_STR);
95         verifyEvent(AuditingActionEnum.UNSUPPORTED_ENVIRONMENT_TYPE.getName());
96     }
97
98     @Test
99     public void testUnknownNotificationOpEnvEvent() {
100         AuditEventFactory factory = new AuditEcompOpEnvEventFactory(AuditingActionEnum.UNKNOWN_ENVIRONMENT_NOTIFICATION,
101                 OP_ENV_ID, OP_ENV_NAME, OP_ENV_TYPE, OP_ENV_ACTION, TENANT_CONTEXT);
102
103         when(auditingDao.addRecord(any(AuditingGenericEvent.class), eq(AuditingActionEnum.UNKNOWN_ENVIRONMENT_NOTIFICATION.getAuditingEsType())))
104                 .thenReturn(ActionStatus.OK);
105         when(cassandraDao.saveRecord(any(AuditingGenericEvent.class))).thenReturn(CassandraOperationStatus.OK);
106
107         assertThat(auditingManager.auditEvent(factory)).isEqualTo(EXPECTED_UNKNOWN_NOTIFICATION_OP_ENV_LOG_STR);
108         verifyEvent(AuditingActionEnum.UNKNOWN_ENVIRONMENT_NOTIFICATION.getName());
109     }
110
111     private void verifyEvent(String action) {
112         verify(cassandraDao).saveRecord(eventCaptor.capture());
113         EcompOperationalEnvironmentEvent storedEvent = eventCaptor.getValue();
114         assertThat(storedEvent.getAction()).isEqualTo(action);
115         assertThat(storedEvent.getOperationalEnvironmentId()).isEqualTo(OP_ENV_ID);
116         assertThat(storedEvent.getOperationalEnvironmentName()).isEqualTo(OP_ENV_NAME);
117         assertThat(storedEvent.getOperationalEnvironmentType()).isEqualTo(OP_ENV_TYPE);
118         assertThat(storedEvent.getTenantContext()).isEqualTo(TENANT_CONTEXT);
119     }
120
121 }