71160eda8ea4636022340f0d8953aad5b3f23dd6
[sdc.git] /
1 /*-
2  * ============LICENSE_START=======================================================
3  * SDC
4  * ================================================================================
5  * Copyright (C) 2019 Telstra 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  */
20
21 package org.openecomp.sdc.be.auditing.impl.externalapi;
22
23 import org.junit.Before;
24 import org.junit.Test;
25 import static org.assertj.core.api.Assertions.assertThat;
26 import static org.mockito.ArgumentMatchers.any;
27 import static org.mockito.ArgumentMatchers.eq;
28 import static org.mockito.Mockito.verify;
29 import static org.mockito.Mockito.when;
30 import static org.openecomp.sdc.be.auditing.impl.AuditTestUtils.*;
31
32 import org.junit.runner.RunWith;
33 import org.mockito.ArgumentCaptor;
34 import org.mockito.Captor;
35 import org.mockito.Mock;
36 import org.mockito.junit.MockitoJUnitRunner;
37 import org.openecomp.sdc.be.auditing.api.AuditEventFactory;
38 import org.openecomp.sdc.be.auditing.impl.AuditingManager;
39 import org.openecomp.sdc.be.config.Configuration;
40 import org.openecomp.sdc.be.dao.api.ActionStatus;
41 import org.openecomp.sdc.be.dao.cassandra.AuditCassandraDao;
42 import org.openecomp.sdc.be.dao.cassandra.CassandraOperationStatus;
43 import org.openecomp.sdc.be.dao.impl.AuditingDao;
44 import org.openecomp.sdc.be.resources.data.auditing.AuditingActionEnum;
45 import org.openecomp.sdc.be.resources.data.auditing.AuditingGenericEvent;
46 import org.openecomp.sdc.be.resources.data.auditing.ExternalApiEvent;
47 import org.openecomp.sdc.be.resources.data.auditing.model.CommonAuditData;
48 import org.openecomp.sdc.be.resources.data.auditing.model.DistributionData;
49 import org.openecomp.sdc.be.resources.data.auditing.model.ResourceCommonInfo;
50 import org.openecomp.sdc.test.utils.TestConfigurationProvider;
51
52 @RunWith(MockitoJUnitRunner.class)
53 public class AuditCreateServiceExternalApiEventFactoryTest {
54
55     private AuditCreateServiceExternalApiEventFactory createTestSubject() {
56         CommonAuditData.Builder newBuilder = CommonAuditData.newBuilder()
57                 .description(DESCRIPTION)
58                 .status(STATUS_OK)
59                 .requestId(REQUEST_ID)
60                 .serviceInstanceId(SERVICE_INSTANCE_ID);
61         CommonAuditData commonAuData = newBuilder.build();
62         return new AuditCreateServiceExternalApiEventFactory(commonAuData,
63                 new ResourceCommonInfo(RESOURCE_TYPE),
64                 new DistributionData(DIST_CONSUMER_ID,DIST_RESOURCE_URL),INVARIANT_UUID,
65                 modifier);
66     }
67
68     private AuditingManager auditingManager;
69
70     @Mock
71     private static AuditCassandraDao cassandraDao;
72     @Mock
73     private static AuditingDao auditingDao;
74     @Mock
75     private static Configuration.ElasticSearchConfig esConfig;
76
77     @Captor
78     private ArgumentCaptor<ExternalApiEvent> eventCaptor;
79
80     @Before
81     public void setUp() {
82         init(esConfig);
83         auditingManager = new AuditingManager(auditingDao, cassandraDao, new TestConfigurationProvider());
84     }
85
86     @Test
87     public void testGetLogMessage() throws Exception {
88         AuditCreateServiceExternalApiEventFactory testSubject;
89
90         // default test
91         testSubject = createTestSubject();
92         testSubject.getLogMessage();
93         assertThat(testSubject.getLogMessage()).isNotBlank();
94         assertThat(testSubject.getLogMessage()).isEqualTo(EXPECTED_EXTERNAL_CREATE_SERVICE_LOG_STR);
95
96     }
97
98     @Test
99     public void testCreateServiceEvent() {
100         AuditEventFactory factory = new AuditCreateServiceExternalApiEventFactory(
101                 CommonAuditData.newBuilder()
102                         .description(DESCRIPTION)
103                         .status(STATUS_OK)
104                         .requestId(REQUEST_ID)
105                         .serviceInstanceId(SERVICE_INSTANCE_ID)
106                         .build(),
107                 new ResourceCommonInfo(RESOURCE_TYPE),
108                 new DistributionData(DIST_CONSUMER_ID, DIST_RESOURCE_URL),
109                 INVARIANT_UUID, modifier);
110
111         when(auditingDao.addRecord(any(AuditingGenericEvent.class), eq(AuditingActionEnum.CREATE_SERVICE_BY_API.getAuditingEsType())))
112                 .thenReturn(ActionStatus.OK);
113         when(cassandraDao.saveRecord(any(AuditingGenericEvent.class))).thenReturn(CassandraOperationStatus.OK);
114
115         assertThat(auditingManager.auditEvent(factory)).isEqualTo(EXPECTED_EXTERNAL_CREATE_SERVICE_LOG_STR);
116         verifyExternalApiEvent(AuditingActionEnum.CREATE_SERVICE_BY_API.getName());
117     }
118
119     private void verifyExternalApiEvent(String action) {
120         verify(cassandraDao).saveRecord(eventCaptor.capture());
121         ExternalApiEvent storedEvent = eventCaptor.getValue();
122         assertThat(storedEvent.getModifier()).isEqualTo(MODIFIER_UID);
123         assertThat(storedEvent.getDesc()).isEqualTo(DESCRIPTION);
124         assertThat(storedEvent.getStatus()).isEqualTo(STATUS_OK);
125         assertThat(storedEvent.getServiceInstanceId()).isEqualTo(SERVICE_INSTANCE_ID);
126         assertThat(storedEvent.getAction()).isEqualTo(action);
127         assertThat(storedEvent.getResourceType()).isEqualTo(RESOURCE_TYPE);
128     }
129 }
130
131