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.ecompopenv;
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;
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 AuditEcompOpEnvEventTest {
54 private AuditingManager auditingManager;
57 private static AuditCassandraDao cassandraDao;
59 private static AuditingDao auditingDao;
61 private static Configuration.ElasticSearchConfig esConfig;
64 private ArgumentCaptor<EcompOperationalEnvironmentEvent> eventCaptor;
69 auditingManager = new AuditingManager(auditingDao, cassandraDao, new TestConfigurationProvider());
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);
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);
81 assertThat(auditingManager.auditEvent(factory)).isEqualTo(EXPECTED_CREATE_OP_ENV_LOG_STR);
82 verifyEvent(AuditingActionEnum.CREATE_ENVIRONMENT.getName());
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);
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);
94 assertThat(auditingManager.auditEvent(factory)).isEqualTo(EXPECTED_UNSUPPORTED_TYPE_OP_ENV_LOG_STR);
95 verifyEvent(AuditingActionEnum.UNSUPPORTED_ENVIRONMENT_TYPE.getName());
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);
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);
107 assertThat(auditingManager.auditEvent(factory)).isEqualTo(EXPECTED_UNKNOWN_NOTIFICATION_OP_ENV_LOG_STR);
108 verifyEvent(AuditingActionEnum.UNKNOWN_ENVIRONMENT_NOTIFICATION.getName());
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);