e6aaec97aeed8ed770020411056c80d966e5fd27
[sdc.git] /
1 /*-
2  * ============LICENSE_START=======================================================
3  * SDC
4  * ================================================================================
5  * Copyright (C) 2019 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  */
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 AuditActivateServiceExternalApiEventFactoryTest {
54
55         private AuditActivateServiceExternalApiEventFactory 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 AuditActivateServiceExternalApiEventFactory(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                 AuditActivateServiceExternalApiEventFactory testSubject;
89
90                 // default test
91                 testSubject = createTestSubject();
92                 testSubject.getLogMessage();
93                 assertThat(testSubject.getLogMessage()).isNotBlank();
94                 assertThat(testSubject.getLogMessage()).isEqualTo(EXPECTED_ACTIVATE_SERVICE_API_LOG_STR);
95         }
96
97         @Test
98         public void testActivateServiceEvent() {
99                 AuditEventFactory builder = new AuditActivateServiceExternalApiEventFactory(
100                                 CommonAuditData.newBuilder()
101                                                 .description(DESCRIPTION)
102                                                 .status(STATUS_OK)
103                                                 .requestId(REQUEST_ID)
104                                                 .serviceInstanceId(SERVICE_INSTANCE_ID)
105                                                 .build(),
106                                 new ResourceCommonInfo(RESOURCE_NAME, RESOURCE_TYPE),
107                                 new DistributionData(DIST_CONSUMER_ID, DIST_RESOURCE_URL),
108                                 INVARIANT_UUID, modifier);
109
110                 when(auditingDao.addRecord(any(AuditingGenericEvent.class), eq(AuditingActionEnum.ACTIVATE_SERVICE_BY_API.getAuditingEsType())))
111                                 .thenReturn(ActionStatus.OK);
112                 when(cassandraDao.saveRecord(any(AuditingGenericEvent.class))).thenReturn(CassandraOperationStatus.OK);
113
114                 assertThat(auditingManager.auditEvent(builder)).isEqualTo(EXPECTED_ACTIVATE_SERVICE_API_LOG_STR);
115                 verifyExternalApiEvent(AuditingActionEnum.ACTIVATE_SERVICE_BY_API.getName());
116
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 }