2 * ============LICENSE_START=======================================================
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
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=========================================================
21 package org.openecomp.sdc.be.auditing.impl.externalapi;
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.*;
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;
53 @RunWith(MockitoJUnitRunner.class)
54 public class AuditChangeLifecycleExternalApiEventFactoryTest {
56 private AuditChangeLifecycleExternalApiEventFactory createTestSubject() {
57 CommonAuditData.Builder newBuilder = CommonAuditData.newBuilder()
58 .description(DESCRIPTION)
60 .requestId(REQUEST_ID)
61 .serviceInstanceId(SERVICE_INSTANCE_ID);
62 CommonAuditData commonAuData = newBuilder.build();
63 ResourceVersionInfo.Builder newBuilder2 = ResourceVersionInfo.newBuilder()
64 .artifactUuid(ARTIFACT_UUID)
65 .state(PREV_RESOURCE_STATE)
66 .version(PREV_RESOURCE_VERSION);
67 ResourceVersionInfo resAuData1 = newBuilder2.build();
68 ResourceVersionInfo.Builder newBuilder3 = ResourceVersionInfo.newBuilder()
69 .artifactUuid(ARTIFACT_UUID)
71 .version(CURRENT_VERSION);
72 ResourceVersionInfo resAuData2 = newBuilder3.build();
73 return new AuditChangeLifecycleExternalApiEventFactory(commonAuData,
74 new ResourceCommonInfo(RESOURCE_NAME, RESOURCE_TYPE),
75 new DistributionData(DIST_CONSUMER_ID,DIST_RESOURCE_URL),
82 private AuditingManager auditingManager;
84 private static AuditCassandraDao cassandraDao;
86 private static AuditingDao auditingDao;
88 private static Configuration.ElasticSearchConfig esConfig;
91 private ArgumentCaptor<ExternalApiEvent> eventCaptor;
96 auditingManager = new AuditingManager(auditingDao, cassandraDao, new TestConfigurationProvider());
100 public void testGetLogMessage() throws Exception {
101 AuditChangeLifecycleExternalApiEventFactory testSubject;
104 testSubject = createTestSubject();
105 assertThat(testSubject.getLogMessage()).isNotBlank();
106 assertThat(testSubject.getLogMessage()).isEqualTo(EXPECTED_CHANGE_LIFECYCLE_EXTERNAL_API_LOG_STR);
110 public void testChangeLifecycleEvent() {
111 AuditEventFactory factory = new AuditChangeLifecycleExternalApiEventFactory(
112 CommonAuditData.newBuilder()
113 .description(DESCRIPTION)
115 .requestId(REQUEST_ID)
116 .serviceInstanceId(SERVICE_INSTANCE_ID)
118 new ResourceCommonInfo(RESOURCE_NAME, RESOURCE_TYPE),
119 new DistributionData(DIST_CONSUMER_ID, DIST_RESOURCE_URL),
120 ResourceVersionInfo.newBuilder()
121 .artifactUuid(ARTIFACT_UUID)
122 .state(PREV_RESOURCE_STATE)
123 .version(PREV_RESOURCE_VERSION)
125 ResourceVersionInfo.newBuilder()
126 .artifactUuid(ARTIFACT_UUID)
127 .state(CURRENT_STATE)
128 .version(CURRENT_VERSION)
130 INVARIANT_UUID, modifier);
132 when(auditingDao.addRecord(any(AuditingGenericEvent.class), eq(AuditingActionEnum.CHANGE_LIFECYCLE_BY_API.getAuditingEsType())))
133 .thenReturn(ActionStatus.OK);
134 when(cassandraDao.saveRecord(any(AuditingGenericEvent.class))).thenReturn(CassandraOperationStatus.OK);
136 assertThat(auditingManager.auditEvent(factory)).isEqualTo(EXPECTED_CHANGE_LIFECYCLE_EXTERNAL_API_LOG_STR);
137 verifyExternalApiEvent(AuditingActionEnum.CHANGE_LIFECYCLE_BY_API.getName());
140 private void verifyExternalApiEvent(String action) {
141 verify(cassandraDao).saveRecord(eventCaptor.capture());
142 ExternalApiEvent storedEvent = eventCaptor.getValue();
143 assertThat(storedEvent.getModifier()).isEqualTo(MODIFIER_UID);
144 assertThat(storedEvent.getDesc()).isEqualTo(DESCRIPTION);
145 assertThat(storedEvent.getStatus()).isEqualTo(STATUS_OK);
146 assertThat(storedEvent.getServiceInstanceId()).isEqualTo(SERVICE_INSTANCE_ID);
147 assertThat(storedEvent.getAction()).isEqualTo(action);
148 assertThat(storedEvent.getResourceType()).isEqualTo(RESOURCE_TYPE);