4c0a672f29fd1e77fbe42e2729b968a6665c8f34
[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.category;
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.CategoryEvent;
41 import org.openecomp.sdc.be.resources.data.auditing.model.CommonAuditData;
42 import org.openecomp.sdc.common.util.ThreadLocalsHolder;
43 import org.openecomp.sdc.test.utils.TestConfigurationProvider;
44
45 import static org.assertj.core.api.Assertions.assertThat;
46 import static org.mockito.ArgumentMatchers.any;
47 import static org.mockito.ArgumentMatchers.eq;
48 import static org.mockito.Mockito.verify;
49 import static org.mockito.Mockito.when;
50 import static org.openecomp.sdc.be.auditing.impl.AuditTestUtils.*;
51
52 @RunWith(MockitoJUnitRunner.class)
53 public class AuditCategoryEventFuncTest {
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<CategoryEvent> eventCaptor;
65
66     @Before
67     public void setUp() {
68         init(esConfig);
69         auditingManager = new AuditingManager(auditingDao, cassandraDao, new TestConfigurationProvider());
70         ThreadLocalsHolder.setUuid(REQUEST_ID);
71     }
72
73     @Test
74     public void testAddCategoryEvent() {
75         AuditEventFactory builder = new AuditCategoryEventFactory(
76                 AuditingActionEnum.ADD_CATEGORY,
77                 CommonAuditData.newBuilder()
78                         .description(DESCRIPTION)
79                         .status(STATUS_OK)
80                         .requestId(REQUEST_ID)
81                         .serviceInstanceId(SERVICE_INSTANCE_ID)
82                         .build(),
83                 modifier, CATEGORY, SUB_CATEGORY, GROUPING_NAME, RESOURCE_TYPE);
84
85         when(auditingDao.addRecord(any(AuditingGenericEvent.class), eq(AuditingActionEnum.ADD_CATEGORY.getAuditingEsType())))
86                 .thenReturn(ActionStatus.OK);
87         when(cassandraDao.saveRecord(any(AuditingGenericEvent.class))).thenReturn(CassandraOperationStatus.OK);
88
89         assertThat(auditingManager.auditEvent(builder)).isEqualTo(EXPECTED_ADD_CATEGORY_LOG_STR);
90         verifyCategoryEvent(AuditingActionEnum.ADD_CATEGORY.getName());
91     }
92
93     private void verifyCategoryEvent(String action) {
94         verify(cassandraDao).saveRecord(eventCaptor.capture());
95         CategoryEvent storedEvent = eventCaptor.getValue();
96         assertThat(storedEvent.getModifier()).isEqualTo(MODIFIER_UID);
97         assertThat(storedEvent.getDesc()).isEqualTo(DESCRIPTION);
98         assertThat(storedEvent.getStatus()).isEqualTo(STATUS_OK);
99         assertThat(storedEvent.getRequestId()).isEqualTo(REQUEST_ID);
100         assertThat(storedEvent.getServiceInstanceId()).isEqualTo(SERVICE_INSTANCE_ID);
101         assertThat(storedEvent.getAction()).isEqualTo(action);
102         assertThat(storedEvent.getCategoryName()).isEqualTo(CATEGORY);
103         assertThat(storedEvent.getSubCategoryName()).isEqualTo(SUB_CATEGORY);
104         assertThat(storedEvent.getGroupingName()).isEqualTo(GROUPING_NAME);
105         assertThat(storedEvent.getResourceType()).isEqualTo(RESOURCE_TYPE);
106
107     }
108 }