Increased code coverage using junits
[sdc.git] / catalog-be / src / test / java / org / openecomp / sdc / be / auditing / impl / externalapi / AuditCreateResourceExternalApiEventFactoryTest.java
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.be.resources.data.auditing.model.ResourceVersionInfo;
51 import org.openecomp.sdc.test.utils.TestConfigurationProvider;
52
53 @RunWith(MockitoJUnitRunner.class)
54 public class AuditCreateResourceExternalApiEventFactoryTest {
55
56         private AuditCreateResourceExternalApiEventFactory createTestSubject() {
57                 CommonAuditData.Builder newBuilder = CommonAuditData.newBuilder()
58                                 .description(DESCRIPTION)
59                                 .status(STATUS_OK)
60                                 .requestId(REQUEST_ID)
61                                 .serviceInstanceId(SERVICE_INSTANCE_ID);
62                 CommonAuditData commonAuData = newBuilder.build();
63                 ResourceVersionInfo.Builder newBuilder2 = ResourceVersionInfo.newBuilder()
64                                 .version(CURRENT_VERSION)
65                                 .state(CURRENT_STATE)
66                                 .artifactUuid(ARTIFACT_UUID);
67                 ResourceVersionInfo resAuData = newBuilder2.build();
68                 return new AuditCreateResourceExternalApiEventFactory(commonAuData,
69                                 new ResourceCommonInfo(RESOURCE_NAME,RESOURCE_TYPE),
70                                 new DistributionData(DIST_CONSUMER_ID,DIST_RESOURCE_URL),
71                                 resAuData,
72                                 INVARIANT_UUID,
73                                 modifier);
74         }
75
76         private AuditingManager auditingManager;
77         @Mock
78         private static AuditCassandraDao cassandraDao;
79         @Mock
80         private static AuditingDao auditingDao;
81         @Mock
82         private static Configuration.ElasticSearchConfig esConfig;
83
84         @Captor
85         private ArgumentCaptor<ExternalApiEvent> eventCaptor;
86
87         @Before
88         public void setUp() {
89                 init(esConfig);
90                 auditingManager = new AuditingManager(auditingDao, cassandraDao, new TestConfigurationProvider());
91         }
92
93
94         @Test
95         public void testGetLogMessage() throws Exception {
96                 AuditCreateResourceExternalApiEventFactory testSubject;
97
98                 // default test
99                 testSubject = createTestSubject();
100                 assertThat(testSubject.getLogMessage()).isNotBlank();
101                 assertThat(testSubject.getLogMessage()).isEqualTo(EXPECTED_EXTERNAL_CREATE_RESOURCE_LOG_STR);
102         }
103
104         @Test
105         public void testCreateResourceEvent() {
106                 AuditEventFactory factory = new AuditCreateResourceExternalApiEventFactory(
107                                 CommonAuditData.newBuilder()
108                                                 .description(DESCRIPTION)
109                                                 .status(STATUS_OK)
110                                                 .requestId(REQUEST_ID)
111                                                 .serviceInstanceId(SERVICE_INSTANCE_ID)
112                                                 .build(),
113                                 new ResourceCommonInfo(RESOURCE_NAME, RESOURCE_TYPE),
114                                 new DistributionData(DIST_CONSUMER_ID, DIST_RESOURCE_URL),
115                                 ResourceVersionInfo.newBuilder()
116                                                 .artifactUuid(ARTIFACT_UUID)
117                                                 .state(CURRENT_STATE)
118                                                 .version(CURRENT_VERSION)
119                                                 .build(),
120                                 INVARIANT_UUID, modifier);
121
122                 when(auditingDao.addRecord(any(AuditingGenericEvent.class), eq(AuditingActionEnum.CREATE_RESOURCE_BY_API.getAuditingEsType())))
123                                 .thenReturn(ActionStatus.OK);
124                 when(cassandraDao.saveRecord(any(AuditingGenericEvent.class))).thenReturn(CassandraOperationStatus.OK);
125
126                 assertThat(auditingManager.auditEvent(factory)).isEqualTo(EXPECTED_EXTERNAL_CREATE_RESOURCE_LOG_STR);
127                 verifyExternalApiEvent(AuditingActionEnum.CREATE_RESOURCE_BY_API.getName());
128         }
129
130         private void verifyExternalApiEvent(String action) {
131                 verify(cassandraDao).saveRecord(eventCaptor.capture());
132                 ExternalApiEvent storedEvent = eventCaptor.getValue();
133                 assertThat(storedEvent.getModifier()).isEqualTo(MODIFIER_UID);
134                 assertThat(storedEvent.getDesc()).isEqualTo(DESCRIPTION);
135                 assertThat(storedEvent.getStatus()).isEqualTo(STATUS_OK);
136                 assertThat(storedEvent.getServiceInstanceId()).isEqualTo(SERVICE_INSTANCE_ID);
137                 assertThat(storedEvent.getAction()).isEqualTo(action);
138                 assertThat(storedEvent.getResourceType()).isEqualTo(RESOURCE_TYPE);
139         }
140 }