From c2a53cb447789e15e85663ae253ab2d90b972934 Mon Sep 17 00:00:00 2001 From: Divyang Patel Date: Tue, 8 Oct 2019 15:19:53 +1100 Subject: Add service creation API as an External API Issue-ID: SDC-2383 Change-Id: I8490163310e892a7ebb6d7e7275edfdeba24045e Signed-off-by: Atif Husain --- .../sdc/be/auditing/impl/AuditTestUtils.java | 7 ++ ...itCreateServiceExternalApiEventFactoryTest.java | 131 +++++++++++++++++++++ .../be/components/impl/ComponentsUtilsTest.java | 79 ++++++++++++- .../externalapi/servlet/AssetsDataServletTest.java | 42 +++---- .../openecomp/sdc/be/impl/ComponentsUtilsTest.java | 12 -- 5 files changed, 238 insertions(+), 33 deletions(-) create mode 100644 catalog-be/src/test/java/org/openecomp/sdc/be/auditing/impl/externalapi/AuditCreateServiceExternalApiEventFactoryTest.java (limited to 'catalog-be/src/test/java/org') diff --git a/catalog-be/src/test/java/org/openecomp/sdc/be/auditing/impl/AuditTestUtils.java b/catalog-be/src/test/java/org/openecomp/sdc/be/auditing/impl/AuditTestUtils.java index f08db3799b..9178900142 100644 --- a/catalog-be/src/test/java/org/openecomp/sdc/be/auditing/impl/AuditTestUtils.java +++ b/catalog-be/src/test/java/org/openecomp/sdc/be/auditing/impl/AuditTestUtils.java @@ -242,6 +242,13 @@ public class AuditTestUtils { "\" CURR_STATE = \"" + CURRENT_STATE + "\" CURR_ARTIFACT_UUID = \"" + ARTIFACT_UUID + "\" STATUS = \"" + STATUS_OK + "\" SERVICE_INSTANCE_ID = \"" + SERVICE_INSTANCE_ID + "\" INVARIANT_UUID = \"" + INVARIANT_UUID + "\" DESC = \"" + DESCRIPTION + "\""; + public final static String EXPECTED_EXTERNAL_CREATE_SERVICE_LOG_STR = "ACTION = \"" + AuditingActionEnum.CREATE_SERVICE_BY_API.getName() + + "\" RESOURCE_TYPE = \"" + RESOURCE_TYPE + "\" CONSUMER_ID = \"" + DIST_CONSUMER_ID + + "\" RESOURCE_URL = \"" + DIST_RESOURCE_URL + "\" MODIFIER = \"" + MODIFIER_UID + + "\" STATUS = \"" + STATUS_OK + "\" SERVICE_INSTANCE_ID = \"" + SERVICE_INSTANCE_ID + + "\" INVARIANT_UUID = \"" + INVARIANT_UUID + "\" DESC = \"" + DESCRIPTION + "\""; + + public static User user; public static User modifier; diff --git a/catalog-be/src/test/java/org/openecomp/sdc/be/auditing/impl/externalapi/AuditCreateServiceExternalApiEventFactoryTest.java b/catalog-be/src/test/java/org/openecomp/sdc/be/auditing/impl/externalapi/AuditCreateServiceExternalApiEventFactoryTest.java new file mode 100644 index 0000000000..71160eda8e --- /dev/null +++ b/catalog-be/src/test/java/org/openecomp/sdc/be/auditing/impl/externalapi/AuditCreateServiceExternalApiEventFactoryTest.java @@ -0,0 +1,131 @@ +/*- + * ============LICENSE_START======================================================= + * SDC + * ================================================================================ + * Copyright (C) 2019 Telstra Intellectual Property. All rights reserved. + * ================================================================================ + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * ============LICENSE_END========================================================= + */ + +package org.openecomp.sdc.be.auditing.impl.externalapi; + +import org.junit.Before; +import org.junit.Test; +import static org.assertj.core.api.Assertions.assertThat; +import static org.mockito.ArgumentMatchers.any; +import static org.mockito.ArgumentMatchers.eq; +import static org.mockito.Mockito.verify; +import static org.mockito.Mockito.when; +import static org.openecomp.sdc.be.auditing.impl.AuditTestUtils.*; + +import org.junit.runner.RunWith; +import org.mockito.ArgumentCaptor; +import org.mockito.Captor; +import org.mockito.Mock; +import org.mockito.junit.MockitoJUnitRunner; +import org.openecomp.sdc.be.auditing.api.AuditEventFactory; +import org.openecomp.sdc.be.auditing.impl.AuditingManager; +import org.openecomp.sdc.be.config.Configuration; +import org.openecomp.sdc.be.dao.api.ActionStatus; +import org.openecomp.sdc.be.dao.cassandra.AuditCassandraDao; +import org.openecomp.sdc.be.dao.cassandra.CassandraOperationStatus; +import org.openecomp.sdc.be.dao.impl.AuditingDao; +import org.openecomp.sdc.be.resources.data.auditing.AuditingActionEnum; +import org.openecomp.sdc.be.resources.data.auditing.AuditingGenericEvent; +import org.openecomp.sdc.be.resources.data.auditing.ExternalApiEvent; +import org.openecomp.sdc.be.resources.data.auditing.model.CommonAuditData; +import org.openecomp.sdc.be.resources.data.auditing.model.DistributionData; +import org.openecomp.sdc.be.resources.data.auditing.model.ResourceCommonInfo; +import org.openecomp.sdc.test.utils.TestConfigurationProvider; + +@RunWith(MockitoJUnitRunner.class) +public class AuditCreateServiceExternalApiEventFactoryTest { + + private AuditCreateServiceExternalApiEventFactory createTestSubject() { + CommonAuditData.Builder newBuilder = CommonAuditData.newBuilder() + .description(DESCRIPTION) + .status(STATUS_OK) + .requestId(REQUEST_ID) + .serviceInstanceId(SERVICE_INSTANCE_ID); + CommonAuditData commonAuData = newBuilder.build(); + return new AuditCreateServiceExternalApiEventFactory(commonAuData, + new ResourceCommonInfo(RESOURCE_TYPE), + new DistributionData(DIST_CONSUMER_ID,DIST_RESOURCE_URL),INVARIANT_UUID, + modifier); + } + + private AuditingManager auditingManager; + + @Mock + private static AuditCassandraDao cassandraDao; + @Mock + private static AuditingDao auditingDao; + @Mock + private static Configuration.ElasticSearchConfig esConfig; + + @Captor + private ArgumentCaptor eventCaptor; + + @Before + public void setUp() { + init(esConfig); + auditingManager = new AuditingManager(auditingDao, cassandraDao, new TestConfigurationProvider()); + } + + @Test + public void testGetLogMessage() throws Exception { + AuditCreateServiceExternalApiEventFactory testSubject; + + // default test + testSubject = createTestSubject(); + testSubject.getLogMessage(); + assertThat(testSubject.getLogMessage()).isNotBlank(); + assertThat(testSubject.getLogMessage()).isEqualTo(EXPECTED_EXTERNAL_CREATE_SERVICE_LOG_STR); + + } + + @Test + public void testCreateServiceEvent() { + AuditEventFactory factory = new AuditCreateServiceExternalApiEventFactory( + CommonAuditData.newBuilder() + .description(DESCRIPTION) + .status(STATUS_OK) + .requestId(REQUEST_ID) + .serviceInstanceId(SERVICE_INSTANCE_ID) + .build(), + new ResourceCommonInfo(RESOURCE_TYPE), + new DistributionData(DIST_CONSUMER_ID, DIST_RESOURCE_URL), + INVARIANT_UUID, modifier); + + when(auditingDao.addRecord(any(AuditingGenericEvent.class), eq(AuditingActionEnum.CREATE_SERVICE_BY_API.getAuditingEsType()))) + .thenReturn(ActionStatus.OK); + when(cassandraDao.saveRecord(any(AuditingGenericEvent.class))).thenReturn(CassandraOperationStatus.OK); + + assertThat(auditingManager.auditEvent(factory)).isEqualTo(EXPECTED_EXTERNAL_CREATE_SERVICE_LOG_STR); + verifyExternalApiEvent(AuditingActionEnum.CREATE_SERVICE_BY_API.getName()); + } + + private void verifyExternalApiEvent(String action) { + verify(cassandraDao).saveRecord(eventCaptor.capture()); + ExternalApiEvent storedEvent = eventCaptor.getValue(); + assertThat(storedEvent.getModifier()).isEqualTo(MODIFIER_UID); + assertThat(storedEvent.getDesc()).isEqualTo(DESCRIPTION); + assertThat(storedEvent.getStatus()).isEqualTo(STATUS_OK); + assertThat(storedEvent.getServiceInstanceId()).isEqualTo(SERVICE_INSTANCE_ID); + assertThat(storedEvent.getAction()).isEqualTo(action); + assertThat(storedEvent.getResourceType()).isEqualTo(RESOURCE_TYPE); + } +} + + diff --git a/catalog-be/src/test/java/org/openecomp/sdc/be/components/impl/ComponentsUtilsTest.java b/catalog-be/src/test/java/org/openecomp/sdc/be/components/impl/ComponentsUtilsTest.java index 22247fc000..15166fc590 100644 --- a/catalog-be/src/test/java/org/openecomp/sdc/be/components/impl/ComponentsUtilsTest.java +++ b/catalog-be/src/test/java/org/openecomp/sdc/be/components/impl/ComponentsUtilsTest.java @@ -33,6 +33,7 @@ import org.openecomp.sdc.be.auditing.api.AuditEventFactory; import org.openecomp.sdc.be.auditing.impl.AuditingManager; import org.openecomp.sdc.be.auditing.impl.externalapi.AuditChangeLifecycleExternalApiEventFactory; import org.openecomp.sdc.be.auditing.impl.externalapi.AuditCreateResourceExternalApiEventFactory; +import org.openecomp.sdc.be.auditing.impl.externalapi.AuditCreateServiceExternalApiEventFactory; import org.openecomp.sdc.be.datatypes.enums.ComponentTypeEnum; import org.openecomp.sdc.be.impl.ComponentsUtils; import org.openecomp.sdc.be.model.*; @@ -560,7 +561,7 @@ public class ComponentsUtilsTest { when(request.getRequestURI()).thenReturn(DIST_RESOURCE_URL); utils.auditCreateResourceExternalApi(responseFormat, new ResourceCommonInfo(RESOURCE_NAME, ComponentTypeEnum.RESOURCE.getValue()), - request, null); + request, null); verify(manager).auditEvent(factoryCaptor.capture()); AuditCreateResourceExternalApiEventFactory factory = (AuditCreateResourceExternalApiEventFactory)factoryCaptor.getValue(); @@ -637,6 +638,82 @@ public class ComponentsUtilsTest { assertThat(event.getResourceType()).isEqualTo(ComponentTypeEnum.RESOURCE.getValue()); } + @Test + public void auditExternalCreateServiceEventWhenResourceObjectIsNull() { + when(responseFormat.getStatus()).thenReturn(Integer.valueOf(STATUS_500)); + when(responseFormat.getFormattedMessage()).thenReturn(DESC_ERROR); + + when(request.getHeader(Constants.USER_ID_HEADER)).thenReturn(USER_ID); + when(request.getHeader(Constants.X_ECOMP_INSTANCE_ID_HEADER)).thenReturn(DIST_CONSUMER_ID); + when(request.getHeader(Constants.X_ECOMP_REQUEST_ID_HEADER)).thenReturn(REQUEST_ID); + when(request.getRequestURI()).thenReturn(DIST_RESOURCE_URL); + + utils.auditCreateServiceExternalApi(responseFormat, request, null); + + verify(manager).auditEvent(factoryCaptor.capture()); + AuditCreateServiceExternalApiEventFactory factory = (AuditCreateServiceExternalApiEventFactory)factoryCaptor.getValue(); + + ExternalApiEvent event = (ExternalApiEvent)factory.getDbEvent(); + verifyCommonDataForExternalApiEvent(event, false); + verifyPreviousResourceVersionInfoForExternalApiEvent(event, true); + verifyCurrentResourceVersionInfoForExternalApiEvent(event, true); + verifyDistributionDataForExternalApiEvent(event); + assertThat(event.getModifier()).isEqualTo("(" + USER_ID + ")"); + assertThat(event.getInvariantUuid()).isNull(); + } + + @Test + public void auditExternalCreateServiceEventWhenResourceObjectIsNullAndRequestDataIsNotProvided() { + when(responseFormat.getStatus()).thenReturn(Integer.valueOf(STATUS_500)); + when(responseFormat.getFormattedMessage()).thenReturn(DESC_ERROR); + + when(request.getHeader(Constants.USER_ID_HEADER)).thenReturn(null); + when(request.getHeader(Constants.X_ECOMP_INSTANCE_ID_HEADER)).thenReturn(null); + when(request.getHeader(Constants.X_ECOMP_REQUEST_ID_HEADER)).thenReturn(REQUEST_ID); + when(request.getRequestURI()).thenReturn(null); + + utils.auditCreateServiceExternalApi(responseFormat, request, null); + + verify(manager).auditEvent(factoryCaptor.capture()); + AuditCreateServiceExternalApiEventFactory factory = (AuditCreateServiceExternalApiEventFactory)factoryCaptor.getValue(); + + ExternalApiEvent event = (ExternalApiEvent)factory.getDbEvent(); + verifyCommonDataForExternalApiEvent(event, false); + verifyPreviousResourceVersionInfoForExternalApiEvent(event, true); + verifyCurrentResourceVersionInfoForExternalApiEvent(event, true); + verifyDistributionDataNotSetForExternalApiEvent(event); + assertThat(event.getModifier()).isEmpty(); + assertThat(event.getInvariantUuid()).isNull(); + } + + @Test + public void auditExternalCreateServiceEventWhenResourceObjectAndRequestDataProvided() { + when(responseFormat.getStatus()).thenReturn(Integer.valueOf(STATUS_OK)); + when(responseFormat.getFormattedMessage()).thenReturn(DESCRIPTION); + + when(request.getHeader(Constants.USER_ID_HEADER)).thenReturn(USER_ID); + when(request.getHeader(Constants.X_ECOMP_INSTANCE_ID_HEADER)).thenReturn(DIST_CONSUMER_ID); + when(request.getHeader(Constants.X_ECOMP_REQUEST_ID_HEADER)).thenReturn(REQUEST_ID); + when(request.getRequestURI()).thenReturn(DIST_RESOURCE_URL); + + Service service = new Service(); + service.setInvariantUUID(INVARIANT_UUID); + service.setUUID(SERVICE_INSTANCE_ID); + + utils.auditCreateServiceExternalApi(responseFormat, request, service); + + verify(manager).auditEvent(factoryCaptor.capture()); + AuditCreateServiceExternalApiEventFactory factory = (AuditCreateServiceExternalApiEventFactory)factoryCaptor.getValue(); + + ExternalApiEvent event = (ExternalApiEvent)factory.getDbEvent(); + verifyCommonDataForExternalApiEvent(event, true); + verifyPreviousResourceVersionInfoForExternalApiEvent(event, true); + verifyDistributionDataForExternalApiEvent(event); + assertThat(event.getCurrArtifactUuid()).isNull(); + assertThat(event.getModifier()).isEqualTo("(" + USER_ID + ")"); + assertThat(event.getInvariantUuid()).isEqualTo(INVARIANT_UUID); + } + @Test public void checkIfAuditEventIsExternal() { assertThat(utils.isExternalApiEvent(AuditingActionEnum.ARTIFACT_UPLOAD_BY_API)).isTrue(); diff --git a/catalog-be/src/test/java/org/openecomp/sdc/be/externalapi/servlet/AssetsDataServletTest.java b/catalog-be/src/test/java/org/openecomp/sdc/be/externalapi/servlet/AssetsDataServletTest.java index 24f06b4543..9b4a98de01 100644 --- a/catalog-be/src/test/java/org/openecomp/sdc/be/externalapi/servlet/AssetsDataServletTest.java +++ b/catalog-be/src/test/java/org/openecomp/sdc/be/externalapi/servlet/AssetsDataServletTest.java @@ -37,6 +37,7 @@ import org.openecomp.sdc.be.components.impl.ComponentInstanceBusinessLogic; import org.openecomp.sdc.be.components.impl.ElementBusinessLogic; import org.openecomp.sdc.be.components.impl.ResourceBusinessLogic; import org.openecomp.sdc.be.components.impl.ResourceImportManager; +import org.openecomp.sdc.be.components.impl.ServiceBusinessLogic; import org.openecomp.sdc.be.components.lifecycle.LifecycleBusinessLogic; import org.openecomp.sdc.be.config.SpringConfig; import org.openecomp.sdc.be.dao.api.ActionStatus; @@ -72,25 +73,25 @@ import static org.mockito.Mockito.when; public class AssetsDataServletTest extends JerseyTest { - public static final HttpServletRequest request = Mockito.mock(HttpServletRequest.class); - public static final HttpSession session = Mockito.mock(HttpSession.class); - public static final ServletContext servletContext = Mockito.mock(ServletContext.class); - public static final WebAppContextWrapper webAppContextWrapper = Mockito.mock(WebAppContextWrapper.class); - public static final WebApplicationContext webApplicationContext = Mockito.mock(WebApplicationContext.class); - public static final ResponseFormat responseFormat = Mockito.mock(ResponseFormat.class); - public static final ServletUtils servletUtils = Mockito.mock(ServletUtils.class); - public static final ComponentsUtils componentsUtils = Mockito.mock(ComponentsUtils.class); - public static final ResourceImportManager resourceImportManager = Mockito.mock(ResourceImportManager.class); - public static final ResourceBusinessLogic resourceBusinessLogic = Mockito.mock(ResourceBusinessLogic.class); - public static final ElementBusinessLogic elementBusinessLogic = Mockito.mock(ElementBusinessLogic.class); - public static final Resource resource = Mockito.mock(Resource.class); - public static final CategoryDefinition categoryDefinition = Mockito.mock(CategoryDefinition.class); - public static final SubCategoryDefinition subCategoryDefinition = Mockito.mock(SubCategoryDefinition.class); - public static final AssetMetadataConverter assetMetadataConverter = Mockito.mock(AssetMetadataConverter.class); - public static final ResourceAssetMetadata resourceAssetMetadata = new ResourceAssetMetadata(); - public static final UserBusinessLogic userBusinessLogic = Mockito.mock(UserBusinessLogic.class); - public static final ComponentInstanceBusinessLogic componentInstanceBusinessLogic = Mockito.mock(ComponentInstanceBusinessLogic.class); - public static final LifecycleBusinessLogic lifecycleBusinessLogic = Mockito.mock(LifecycleBusinessLogic.class); + private static final HttpServletRequest request = Mockito.mock(HttpServletRequest.class); + private static final HttpSession session = Mockito.mock(HttpSession.class); + private static final ServletContext servletContext = Mockito.mock(ServletContext.class); + private static final WebAppContextWrapper webAppContextWrapper = Mockito.mock(WebAppContextWrapper.class); + private static final WebApplicationContext webApplicationContext = Mockito.mock(WebApplicationContext.class); + private static final ServletUtils servletUtils = Mockito.mock(ServletUtils.class); + private static final ComponentsUtils componentsUtils = Mockito.mock(ComponentsUtils.class); + private static final ResourceImportManager resourceImportManager = Mockito.mock(ResourceImportManager.class); + private static final ResourceBusinessLogic resourceBusinessLogic = Mockito.mock(ResourceBusinessLogic.class); + private static final ServiceBusinessLogic serviceBusinessLogic = Mockito.mock(ServiceBusinessLogic.class); + private static final ElementBusinessLogic elementBusinessLogic = Mockito.mock(ElementBusinessLogic.class); + private static final Resource resource = Mockito.mock(Resource.class); + private static final CategoryDefinition categoryDefinition = Mockito.mock(CategoryDefinition.class); + private static final SubCategoryDefinition subCategoryDefinition = Mockito.mock(SubCategoryDefinition.class); + private static final AssetMetadataConverter assetMetadataConverter = Mockito.mock(AssetMetadataConverter.class); + private static final ResourceAssetMetadata resourceAssetMetadata = new ResourceAssetMetadata(); + private static final UserBusinessLogic userBusinessLogic = Mockito.mock(UserBusinessLogic.class); + private static final ComponentInstanceBusinessLogic componentInstanceBusinessLogic = Mockito.mock(ComponentInstanceBusinessLogic.class); + private static final LifecycleBusinessLogic lifecycleBusinessLogic = Mockito.mock(LifecycleBusinessLogic.class); @@ -203,8 +204,9 @@ public class AssetsDataServletTest extends JerseyTest { bind(assetMetadataConverter).to(AssetMetadataConverter.class); bind(lifecycleBusinessLogic).to(LifecycleBusinessLogic.class); bind(resourceBusinessLogic).to(ResourceBusinessLogic.class); + bind(serviceBusinessLogic).to(ServiceBusinessLogic.class); } }) .property("contextConfig", context); } -} \ No newline at end of file +} diff --git a/catalog-be/src/test/java/org/openecomp/sdc/be/impl/ComponentsUtilsTest.java b/catalog-be/src/test/java/org/openecomp/sdc/be/impl/ComponentsUtilsTest.java index b76ac2e70f..a4c8a3dd7d 100644 --- a/catalog-be/src/test/java/org/openecomp/sdc/be/impl/ComponentsUtilsTest.java +++ b/catalog-be/src/test/java/org/openecomp/sdc/be/impl/ComponentsUtilsTest.java @@ -432,11 +432,7 @@ public class ComponentsUtilsTest { testSubject.auditComponent(responseFormat, modifier, component, actionEnum, prevComponent,info); } - - - - @Test public void testAuditComponent_1() throws Exception { ComponentsUtils testSubject; @@ -452,14 +448,6 @@ public class ComponentsUtilsTest { testSubject.auditComponent(responseFormat, modifier, component, actionEnum, type, prevComponent); } - - - - - - - - @Test public void testValidateStringNotEmpty() throws Exception { -- cgit 1.2.3-korg