Catalog alignment
[sdc.git] / catalog-be / src / test / java / org / openecomp / sdc / be / auditing / impl / ecompopenv / AuditEcompOpEnvEventTest.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.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.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.EcompOperationalEnvironmentEvent;
39 import org.openecomp.sdc.test.utils.TestConfigurationProvider;
40
41 import static org.assertj.core.api.Assertions.assertThat;
42 import static org.mockito.ArgumentMatchers.any;
43 import static org.mockito.Mockito.verify;
44 import static org.mockito.Mockito.when;
45 import static org.openecomp.sdc.be.auditing.impl.AuditTestUtils.EXPECTED_CREATE_OP_ENV_LOG_STR;
46 import static org.openecomp.sdc.be.auditing.impl.AuditTestUtils.EXPECTED_UNKNOWN_NOTIFICATION_OP_ENV_LOG_STR;
47 import static org.openecomp.sdc.be.auditing.impl.AuditTestUtils.EXPECTED_UNSUPPORTED_TYPE_OP_ENV_LOG_STR;
48 import static org.openecomp.sdc.be.auditing.impl.AuditTestUtils.OP_ENV_ACTION;
49 import static org.openecomp.sdc.be.auditing.impl.AuditTestUtils.OP_ENV_ID;
50 import static org.openecomp.sdc.be.auditing.impl.AuditTestUtils.OP_ENV_NAME;
51 import static org.openecomp.sdc.be.auditing.impl.AuditTestUtils.OP_ENV_TYPE;
52 import static org.openecomp.sdc.be.auditing.impl.AuditTestUtils.TENANT_CONTEXT;
53 import static org.openecomp.sdc.be.auditing.impl.AuditTestUtils.init;
54
55 @RunWith(MockitoJUnitRunner.class)
56 public class AuditEcompOpEnvEventTest {
57
58     private AuditingManager auditingManager;
59
60     @Mock
61     private static AuditCassandraDao cassandraDao;
62
63     @Captor
64     private ArgumentCaptor<EcompOperationalEnvironmentEvent> eventCaptor;
65
66     @Before
67     public void setUp() {
68         init();
69         auditingManager = new AuditingManager(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(cassandraDao.saveRecord(any(AuditingGenericEvent.class))).thenReturn(CassandraOperationStatus.OK);
78
79         assertThat(auditingManager.auditEvent(factory)).isEqualTo(EXPECTED_CREATE_OP_ENV_LOG_STR);
80         verifyEvent(AuditingActionEnum.CREATE_ENVIRONMENT.getName());
81     }
82
83     @Test
84     public void testUnsupportedTypeOpEnvEvent() {
85         AuditEventFactory factory = new AuditEcompOpEnvEventFactory(AuditingActionEnum.UNSUPPORTED_ENVIRONMENT_TYPE,
86                 OP_ENV_ID, OP_ENV_NAME, OP_ENV_TYPE, OP_ENV_ACTION, TENANT_CONTEXT);
87
88         when(cassandraDao.saveRecord(any(AuditingGenericEvent.class))).thenReturn(CassandraOperationStatus.OK);
89
90         assertThat(auditingManager.auditEvent(factory)).isEqualTo(EXPECTED_UNSUPPORTED_TYPE_OP_ENV_LOG_STR);
91         verifyEvent(AuditingActionEnum.UNSUPPORTED_ENVIRONMENT_TYPE.getName());
92     }
93
94     @Test
95     public void testUnknownNotificationOpEnvEvent() {
96         AuditEventFactory factory = new AuditEcompOpEnvEventFactory(AuditingActionEnum.UNKNOWN_ENVIRONMENT_NOTIFICATION,
97                 OP_ENV_ID, OP_ENV_NAME, OP_ENV_TYPE, OP_ENV_ACTION, TENANT_CONTEXT);
98
99         when(cassandraDao.saveRecord(any(AuditingGenericEvent.class))).thenReturn(CassandraOperationStatus.OK);
100
101         assertThat(auditingManager.auditEvent(factory)).isEqualTo(EXPECTED_UNKNOWN_NOTIFICATION_OP_ENV_LOG_STR);
102         verifyEvent(AuditingActionEnum.UNKNOWN_ENVIRONMENT_NOTIFICATION.getName());
103     }
104
105     private void verifyEvent(String action) {
106         verify(cassandraDao).saveRecord(eventCaptor.capture());
107         EcompOperationalEnvironmentEvent storedEvent = eventCaptor.getValue();
108         assertThat(storedEvent.getAction()).isEqualTo(action);
109         assertThat(storedEvent.getOperationalEnvironmentId()).isEqualTo(OP_ENV_ID);
110         assertThat(storedEvent.getOperationalEnvironmentName()).isEqualTo(OP_ENV_NAME);
111         assertThat(storedEvent.getOperationalEnvironmentType()).isEqualTo(OP_ENV_TYPE);
112         assertThat(storedEvent.getTenantContext()).isEqualTo(TENANT_CONTEXT);
113     }
114
115 }