From 7353fb39790b51c593cb0f72c6ab46d906758244 Mon Sep 17 00:00:00 2001 From: vasraz Date: Mon, 8 Nov 2021 17:45:10 +0000 Subject: Fix broken TC (ExternalRefsServletTest) Signed-off-by: Vasyl Razinkov Issue-ID: SDC-3776 Change-Id: Iac19fea2b6f8a36dc29f4a8d5cea277d226342b2 --- .../externalapi/servlet/ExternalRefsServlet.java | 6 +- .../openecomp/sdc/be/servlets/ArchiveEndpoint.java | 4 +- .../servlet/ExternalRefServletTest.java | 836 --------------------- .../servlet/ExternalRefsServletTest.java | 790 +++++++++++++++++++ .../sdc/be/servlets/ArchiveEndpointTest.java | 36 +- .../ci/tests/execute/category/GroupingTest.java | 3 - .../tests/execute/category/SubCategoriesTest.java | 9 - .../resource/UpdateResourceMetadataTest.java | 2 - 8 files changed, 806 insertions(+), 880 deletions(-) delete mode 100644 catalog-be/src/test/java/org/openecomp/sdc/be/externalapi/servlet/ExternalRefServletTest.java create mode 100644 catalog-be/src/test/java/org/openecomp/sdc/be/externalapi/servlet/ExternalRefsServletTest.java diff --git a/catalog-be/src/main/java/org/openecomp/sdc/be/externalapi/servlet/ExternalRefsServlet.java b/catalog-be/src/main/java/org/openecomp/sdc/be/externalapi/servlet/ExternalRefsServlet.java index f57ee48504..0c3595e05d 100644 --- a/catalog-be/src/main/java/org/openecomp/sdc/be/externalapi/servlet/ExternalRefsServlet.java +++ b/catalog-be/src/main/java/org/openecomp/sdc/be/externalapi/servlet/ExternalRefsServlet.java @@ -21,9 +21,7 @@ package org.openecomp.sdc.be.externalapi.servlet; import fj.data.Either; import io.swagger.v3.oas.annotations.servers.Server; -import io.swagger.v3.oas.annotations.servers.Servers; import io.swagger.v3.oas.annotations.tags.Tag; -import io.swagger.v3.oas.annotations.tags.Tags; import java.util.List; import java.util.Map; import javax.inject.Inject; @@ -53,8 +51,8 @@ import org.openecomp.sdc.common.log.wrappers.Logger; import org.springframework.stereotype.Controller; @Path("/v1/catalog") -@Tags({@Tag(name = "SDCE-7 APIs")}) -@Servers({@Server(url = "/sdc")}) +@Tag(name = "SDCE-7 APIs") +@Server(url = "/sdc") @Controller public class ExternalRefsServlet extends BeGenericServlet { diff --git a/catalog-be/src/main/java/org/openecomp/sdc/be/servlets/ArchiveEndpoint.java b/catalog-be/src/main/java/org/openecomp/sdc/be/servlets/ArchiveEndpoint.java index f3fd56baa3..39414a05e2 100644 --- a/catalog-be/src/main/java/org/openecomp/sdc/be/servlets/ArchiveEndpoint.java +++ b/catalog-be/src/main/java/org/openecomp/sdc/be/servlets/ArchiveEndpoint.java @@ -59,8 +59,8 @@ import org.springframework.web.bind.annotation.RequestBody; @Loggable(prepend = true, value = Loggable.DEBUG, trim = false) @Path("/v1/catalog") -@Tags({@Tag(name = "SDCE-2 APIs")}) -@Servers({@Server(url = "/sdc2/rest")}) +@Tag(name = "SDCE-2 APIs") +@Server(url = "/sdc2/rest") @Controller @Consumes(MediaType.APPLICATION_JSON) @Produces(MediaType.APPLICATION_JSON) diff --git a/catalog-be/src/test/java/org/openecomp/sdc/be/externalapi/servlet/ExternalRefServletTest.java b/catalog-be/src/test/java/org/openecomp/sdc/be/externalapi/servlet/ExternalRefServletTest.java deleted file mode 100644 index b9765f2c29..0000000000 --- a/catalog-be/src/test/java/org/openecomp/sdc/be/externalapi/servlet/ExternalRefServletTest.java +++ /dev/null @@ -1,836 +0,0 @@ -/*- - * ============LICENSE_START======================================================= - * SDC - * ================================================================================ - * Copyright (C) 2017 AT&T 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========================================================= - * Modifications copyright (c) 2019 Nokia - * ================================================================================ - */ - -package org.openecomp.sdc.be.externalapi.servlet; - -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.doThrow; -import static org.mockito.Mockito.when; - -import fj.data.Either; -import java.util.HashMap; -import java.util.LinkedList; -import java.util.List; -import java.util.Map; -import javax.servlet.ServletContext; -import javax.servlet.http.HttpServletRequest; -import javax.ws.rs.client.Entity; -import javax.ws.rs.core.Application; -import javax.ws.rs.core.GenericType; -import javax.ws.rs.core.MediaType; -import javax.ws.rs.core.Response; -import org.glassfish.jersey.server.ResourceConfig; -import org.glassfish.jersey.test.JerseyTest; -import org.junit.jupiter.api.AfterEach; -import org.junit.jupiter.api.BeforeAll; -import org.junit.jupiter.api.BeforeEach; -import org.junit.jupiter.api.Disabled; -import org.junit.jupiter.api.Test; -import org.junit.jupiter.api.TestInstance; -import org.junit.jupiter.api.TestInstance.Lifecycle; -import org.junit.jupiter.api.extension.ExtendWith; -import org.mockito.Mock; -import org.mockito.MockitoAnnotations; -import org.mockito.junit.jupiter.MockitoExtension; -import org.openecomp.sdc.be.components.impl.ComponentLocker; -import org.openecomp.sdc.be.components.impl.ExternalRefsBusinessLogic; -import org.openecomp.sdc.be.components.impl.exceptions.ByResponseFormatComponentException; -import org.openecomp.sdc.be.components.path.utils.GraphTestUtils; -import org.openecomp.sdc.be.components.validation.AccessValidations; -import org.openecomp.sdc.be.config.ConfigurationManager; -import org.openecomp.sdc.be.dao.DAOJanusGraphStrategy; -import org.openecomp.sdc.be.dao.JanusGraphClientStrategy; -import org.openecomp.sdc.be.dao.api.ActionStatus; -import org.openecomp.sdc.be.dao.impl.HealingPipelineDao; -import org.openecomp.sdc.be.dao.janusgraph.HealingJanusGraphGenericDao; -import org.openecomp.sdc.be.dao.janusgraph.JanusGraphClient; -import org.openecomp.sdc.be.dao.janusgraph.JanusGraphGenericDao; -import org.openecomp.sdc.be.dao.janusgraph.JanusGraphOperationStatus; -import org.openecomp.sdc.be.dao.jsongraph.GraphVertex; -import org.openecomp.sdc.be.dao.janusgraph.HealingJanusGraphDao; -import org.openecomp.sdc.be.datatypes.enums.ResourceTypeEnum; -import org.openecomp.sdc.be.dto.ExternalRefDTO; -import org.openecomp.sdc.be.impl.ComponentsUtils; -import org.openecomp.sdc.be.impl.ServletUtils; -import org.openecomp.sdc.be.impl.WebAppContextWrapper; -import org.openecomp.sdc.be.model.Component; -import org.openecomp.sdc.be.model.User; -import org.openecomp.sdc.be.model.jsonjanusgraph.config.ContainerInstanceTypesData; -import org.openecomp.sdc.be.model.jsonjanusgraph.operations.ArchiveOperation; -import org.openecomp.sdc.be.model.jsonjanusgraph.operations.CategoryOperation; -import org.openecomp.sdc.be.model.jsonjanusgraph.operations.ExternalReferencesOperation; -import org.openecomp.sdc.be.model.jsonjanusgraph.operations.GroupsOperation; -import org.openecomp.sdc.be.model.jsonjanusgraph.operations.NodeTemplateOperation; -import org.openecomp.sdc.be.model.jsonjanusgraph.operations.NodeTypeOperation; -import org.openecomp.sdc.be.model.jsonjanusgraph.operations.TopologyTemplateOperation; -import org.openecomp.sdc.be.model.jsonjanusgraph.operations.ToscaOperationFacade; -import org.openecomp.sdc.be.model.jsonjanusgraph.utils.IdMapper; -import org.openecomp.sdc.be.model.operations.api.IGraphLockOperation; -import org.openecomp.sdc.be.model.operations.impl.OperationUtils; -import org.openecomp.sdc.be.servlets.exception.ComponentExceptionMapper; -import org.openecomp.sdc.be.servlets.exception.DefaultExceptionMapper; -import org.openecomp.sdc.be.servlets.exception.StorageExceptionMapper; -import org.openecomp.sdc.be.user.Role; -import org.openecomp.sdc.be.user.UserBusinessLogic; -import org.openecomp.sdc.common.api.ConfigurationSource; -import org.openecomp.sdc.common.api.Constants; -import org.openecomp.sdc.common.impl.ExternalConfiguration; -import org.openecomp.sdc.common.impl.FSConfigurationSource; -import org.openecomp.sdc.exception.ResponseFormat; -import org.springframework.context.ApplicationContext; -import org.springframework.context.annotation.AnnotationConfigApplicationContext; -import org.springframework.context.annotation.Bean; -import org.springframework.context.annotation.Configuration; -import org.springframework.context.annotation.PropertySource; -import org.springframework.http.HttpStatus; -import org.springframework.web.context.WebApplicationContext; - -@TestInstance(Lifecycle.PER_CLASS) -@ExtendWith(MockitoExtension.class) -@Disabled("Investigate 'org.springframework.beans.factory.UnsatisfiedDependencyException: " - + "Error creating bean with name 'externalRefServletTest.TestSpringConfig': " - + "Unsatisfied dependency expressed through constructor parameter 0; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: " - + "No qualifying bean of type 'org.openecomp.sdc.be.externalapi.servlet.ExternalRefServletTest' available: expected at least 1 bean which qualifies as autowire candidate." - + "Dependency annotations: {}'") -class ExternalRefServletTest extends JerseyTest { - - private static final String COMPONENT_ID = "ci-MyComponentName"; - private static final String FAKE_COMPONENT_ID = "ci-MyFAKEComponentName"; - private static final String MONITORING_OBJECT_TYPE = "monitoring"; - private static final String WORKFLOW_OBJECT_TYPE = "workflow"; - private static final String VERSION = "0.1"; - private static final String FAKE_VERSION = "0.5"; - private static final String REF_1 = "ref1"; - private static final String REF_2 = "ref2"; - private static final String REF_3 = "ref3"; - private static final String REF_4 = "ref4"; - private static final String REF_5 = "ref5"; - //workflow - private static final String REF_6 = "ref6"; - private static boolean setupDone = false; - private static String serviceVertexUuid; - private static String resourceVertexUuid; - /* Users */ - private static final User adminUser = new User("admin", "admin", "admin", "admin@email.com", Role.ADMIN.name(), - System.currentTimeMillis()); - private static final User designerUser = new User("designer", "designer", "designer", "designer@email.com", - Role.DESIGNER.name(), System - .currentTimeMillis()); - private static final User otherDesignerUser = new User("otherDesigner", "otherDesigner", "otherDesigner", - "otherDesigner@email.com", Role.DESIGNER - .name(), System.currentTimeMillis()); - private static final User otherUser = new User("other", "other", "other", "other@email.com", Role.DESIGNER.name(), - System.currentTimeMillis()); - - @Mock - private IdMapper idMapper; - @Mock - public WebAppContextWrapper webAppContextWrapper; - @Mock - private ServletContext servletContext; - @Mock - private WebApplicationContext webApplicationContext; - @Mock - private ServletUtils servletUtils; - @Mock - private UserBusinessLogic userAdmin; - @Mock - private ComponentsUtils componentUtils; - @Mock - private ResponseFormat responseFormat; - @Mock - private ResponseFormat notFoundResponseFormat; - @Mock - private ResponseFormat badRequestResponseFormat; - @Mock - private ToscaOperationFacade toscaOperationFacadeMock; - @Mock - private AccessValidations accessValidationsMock; - @Mock - private ComponentLocker componentLocker; - @Mock - private HealingJanusGraphGenericDao janusGraphGenericDao; - @Mock - private IGraphLockOperation graphLockOperation; - @Mock - private HttpServletRequest request; - @Mock - private ByResponseFormatComponentException ce; - @Mock - private Component resourceComponentMock; - @Mock - private Component serviceComponentMock; - - @BeforeAll - public void setup() { - - //Needed for User Authorization - //======================================================================================================================== - when(servletContext.getAttribute(Constants.WEB_APPLICATION_CONTEXT_WRAPPER_ATTR)) - .thenReturn(webAppContextWrapper); - when(webAppContextWrapper.getWebAppContext(servletContext)).thenReturn(webApplicationContext); - when(webApplicationContext.getBean(ServletUtils.class)).thenReturn(servletUtils); - when(servletUtils.getUserAdmin()).thenReturn(userAdmin); - when(servletUtils.getComponentsUtils()).thenReturn(componentUtils); - when(componentUtils.getResponseFormat(ActionStatus.RESTRICTED_OPERATION)).thenReturn(responseFormat); - when(responseFormat.getStatus()).thenReturn(HttpStatus.UNAUTHORIZED.value()); - - String[] params = {otherDesignerUser.getUserId()}; - when(ce.getResponseFormat()).thenReturn(responseFormat); - doThrow(ce).when(accessValidationsMock) - .validateUserCanWorkOnComponent(any(), any(), eq(otherDesignerUser.getUserId()), any()); - doThrow(ce).when(accessValidationsMock) - .validateUserCanWorkOnComponent(any(), any(), eq(otherUser.getUserId()), any()); - - //Needed for error configuration - when(notFoundResponseFormat.getStatus()).thenReturn(HttpStatus.NOT_FOUND.value()); - when(badRequestResponseFormat.getStatus()).thenReturn(HttpStatus.BAD_REQUEST.value()); - when(componentUtils.getResponseFormat(eq(ActionStatus.RESOURCE_NOT_FOUND), (String[]) any())) - .thenReturn(notFoundResponseFormat); - when(componentUtils.getResponseFormat(eq(ActionStatus.COMPONENT_VERSION_NOT_FOUND), (String[]) any())) - .thenReturn(notFoundResponseFormat); - when(componentUtils.getResponseFormat(eq(ActionStatus.COMPONENT_INSTANCE_NOT_FOUND), (String[]) any())) - .thenReturn(notFoundResponseFormat); - when(componentUtils.getResponseFormat(eq(ActionStatus.EXT_REF_NOT_FOUND), (String[]) any())) - .thenReturn(notFoundResponseFormat); - when(componentUtils.getResponseFormat(eq(ActionStatus.MISSING_X_ECOMP_INSTANCE_ID), (String[]) any())) - .thenReturn(badRequestResponseFormat); - when(userAdmin.getUser(adminUser.getUserId(), false)).thenReturn(adminUser); - when(userAdmin.getUser(designerUser.getUserId(), false)).thenReturn(designerUser); - when(userAdmin.getUser(otherUser.getUserId(), false)).thenReturn(otherUser); - //======================================================================================================================== - - String appConfigDir = "src/test/resources/config/catalog-be"; - ConfigurationSource configurationSource = new FSConfigurationSource(ExternalConfiguration.getChangeListener(), - appConfigDir); - ConfigurationManager configurationManager = new ConfigurationManager(configurationSource); - - org.openecomp.sdc.be.config.Configuration configuration = new org.openecomp.sdc.be.config.Configuration(); - configuration.setJanusGraphInMemoryGraph(true); - org.openecomp.sdc.be.config.Configuration.HeatDeploymentArtifactTimeout heatDeploymentArtifactTimeout = new org.openecomp.sdc.be.config.Configuration.HeatDeploymentArtifactTimeout(); - heatDeploymentArtifactTimeout.setDefaultMinutes(30); - configuration.setAafAuthNeeded(false); - configuration.setHeatArtifactDeploymentTimeout(heatDeploymentArtifactTimeout); - configurationManager.setConfiguration(configuration); - ExternalConfiguration.setAppName("catalog-be"); - } - - @BeforeEach - public void before() throws Exception { - super.setUp(); - MockitoAnnotations.openMocks(this); - - when(resourceComponentMock.getVersion()).thenReturn(VERSION); - when(resourceComponentMock.getUniqueId()).thenReturn(resourceVertexUuid); - - when(serviceComponentMock.getVersion()).thenReturn(VERSION); - when(serviceComponentMock.getUniqueId()).thenReturn(serviceVertexUuid); - - List listComponents = new LinkedList<>(); - listComponents.add(serviceComponentMock); - - when(toscaOperationFacadeMock.getComponentListByUuid(eq(serviceVertexUuid), any())) - .thenReturn(Either.left(listComponents)); - when(toscaOperationFacadeMock.getComponentByUuidAndVersion(serviceVertexUuid, VERSION)) - .thenReturn(Either.left(serviceComponentMock)); - when(toscaOperationFacadeMock.getComponentByUuidAndVersion(resourceVertexUuid, VERSION)) - .thenReturn(Either.left(resourceComponentMock)); - when(toscaOperationFacadeMock.getLatestComponentByUuid(eq(serviceVertexUuid), any())) - .thenReturn(Either.left(listComponents.get(0))); - when(toscaOperationFacadeMock.getLatestComponentByUuid(eq(resourceVertexUuid), any())) - .thenReturn(Either.left(resourceComponentMock)); - } - - @AfterEach - void after() throws Exception { - super.tearDown(); - } - - @Test - void testGetExternalRefsForExistingComponentInstance() { - String path = String - .format("/v1/catalog/services/%s/version/%s/resourceInstances/%s/externalReferences/%s", serviceVertexUuid, - VERSION, COMPONENT_ID, MONITORING_OBJECT_TYPE); - - Response response = target() - .path(path) - .request() - .accept(MediaType.APPLICATION_JSON) - .header(Constants.X_ECOMP_INSTANCE_ID_HEADER, "mockXEcompInstanceId") - .header(Constants.USER_ID_HEADER, designerUser.getUserId()) - .get(); - - List dto = response.readEntity(new GenericType>() { - }); - assertThat(dto).containsExactly(REF_1, REF_2, REF_3, REF_5); - assertThat(response.getStatus()).isEqualTo(HttpStatus.OK.value()); - } - - @Test - void testGetExternalRefsForAsset() { - String path = String - .format("/v1/catalog/services/%s/version/%s/externalReferences/%s", serviceVertexUuid, VERSION, - MONITORING_OBJECT_TYPE); - - Response response = target() - .path(path) - .request() - .accept(MediaType.APPLICATION_JSON) - .header(Constants.X_ECOMP_INSTANCE_ID_HEADER, "mockXEcompInstanceId") - .header(Constants.USER_ID_HEADER, designerUser.getUserId()) - .get(); - - Map> dtoMap = response.readEntity(new GenericType>>() { - }); - assertThat(dtoMap.get(COMPONENT_ID)).containsExactly(REF_1, REF_2, REF_3, REF_5); - assertThat(response.getStatus()).isEqualTo(HttpStatus.OK.value()); - } - - @Test - void testGetExternalRefsForAssetWithMissingEcompHeader() { - String path = String - .format("/v1/catalog/services/%s/version/%s/externalReferences/%s", serviceVertexUuid, VERSION, - MONITORING_OBJECT_TYPE); - - //No X-Ecomp-Instance-ID header - Response response = target() - .path(path) - .request() - .accept(MediaType.APPLICATION_JSON) - .header(Constants.USER_ID_HEADER, designerUser.getUserId()) - .get(); - - assertThat(response.getStatus()).isEqualTo(HttpStatus.BAD_REQUEST.value()); - } - - @Test - void testAddExternalRefForResource() { - String path = String - .format("/v1/catalog/resources/%s/resourceInstances/%s/externalReferences/%s", resourceVertexUuid, - COMPONENT_ID, MONITORING_OBJECT_TYPE); - String getPath = String - .format("/v1/catalog/resources/%s/version/%s/externalReferences/%s", resourceVertexUuid, VERSION, - MONITORING_OBJECT_TYPE); - - Response response = target() - .path(path) - .request(MediaType.APPLICATION_JSON) - .accept(MediaType.APPLICATION_JSON) - .header(Constants.X_ECOMP_INSTANCE_ID_HEADER, "mockXEcompInstanceId") - .header(Constants.USER_ID_HEADER, designerUser.getUserId()) - .post(Entity.json(new ExternalRefDTO(REF_1))); - - assertThat(response.getStatus()).isEqualTo(HttpStatus.CREATED.value()); - - //Check that GET will include the new reference - response = target() - .path(getPath) - .request() - .accept(MediaType.APPLICATION_JSON) - .header(Constants.X_ECOMP_INSTANCE_ID_HEADER, "mockXEcompInstanceId") - .header(Constants.USER_ID_HEADER, adminUser.getUserId()) - .get(); - - Map> dto = response.readEntity(new GenericType>>() { - }); - assertThat(dto.get(COMPONENT_ID)).containsExactlyInAnyOrder(REF_1); - assertThat(response.getStatus()).isEqualTo(HttpStatus.OK.value()); - } - - @Test - void testAddExternalRefForExistingComponentInstance() { - String path = String - .format("/v1/catalog/services/%s/resourceInstances/%s/externalReferences/%s", serviceVertexUuid, - COMPONENT_ID, MONITORING_OBJECT_TYPE); - String getPath = String - .format("/v1/catalog/services/%s/version/%s/resourceInstances/%s/externalReferences/%s", serviceVertexUuid, - VERSION, COMPONENT_ID, MONITORING_OBJECT_TYPE); - Response response = target() - .path(path) - .request(MediaType.APPLICATION_JSON) - .accept(MediaType.APPLICATION_JSON) - .header(Constants.X_ECOMP_INSTANCE_ID_HEADER, "mockXEcompInstanceId") - .header(Constants.USER_ID_HEADER, designerUser.getUserId()) - .post(Entity.json(new ExternalRefDTO(REF_4))); - - assertThat(response.getStatus()).isEqualTo(HttpStatus.CREATED.value()); - - //Check that GET will include the new reference - response = target() - .path(getPath) - .request() - .accept(MediaType.APPLICATION_JSON) - .header(Constants.X_ECOMP_INSTANCE_ID_HEADER, "mockXEcompInstanceId") - .header(Constants.USER_ID_HEADER, adminUser.getUserId()) - .get(); - - List dto = response.readEntity(new GenericType>() { - }); - assertThat(dto).containsExactlyInAnyOrder(REF_1, REF_2, REF_3, REF_4, REF_5); - assertThat(response.getStatus()).isEqualTo(HttpStatus.OK.value()); - } - - @Test - void testDeleteExternalRefForExistingComponentInstance() { - String deletePath = String - .format("/v1/catalog/services/%s/resourceInstances/%s/externalReferences/%s/%s", serviceVertexUuid, - COMPONENT_ID, MONITORING_OBJECT_TYPE, REF_5); - String getPath = String - .format("/v1/catalog/services/%s/version/%s/resourceInstances/%s/externalReferences/%s", serviceVertexUuid, - VERSION, COMPONENT_ID, MONITORING_OBJECT_TYPE); - Response response = target() - .path(deletePath) - .request(MediaType.APPLICATION_JSON) - .accept(MediaType.APPLICATION_JSON) - .header(Constants.X_ECOMP_INSTANCE_ID_HEADER, "mockXEcompInstanceId") - .header(Constants.USER_ID_HEADER, designerUser.getUserId()) - .delete(); - - //Verify that the deleted reference is returned in body - ExternalRefDTO dto = response.readEntity(ExternalRefDTO.class); - assertThat(dto.getReferenceUUID()).isEqualTo(REF_5); - assertThat(response.getStatus()).isEqualTo(HttpStatus.OK.value()); - - //Check that GET will NOT include the deleted reference - response = target() - .path(getPath) - .request() - .accept(MediaType.APPLICATION_JSON) - .header(Constants.X_ECOMP_INSTANCE_ID_HEADER, "mockXEcompInstanceId") - .header(Constants.USER_ID_HEADER, designerUser.getUserId()) - .get(); - - List getResponse = response.readEntity(new GenericType>() { - }); - assertThat(getResponse).containsExactlyInAnyOrder(REF_1, REF_2, REF_3); - assertThat(response.getStatus()).isEqualTo(HttpStatus.OK.value()); - } - - @Test - void testUpdateExternalRefForExistingComponentInstance() { - String updatePath = String - .format("/v1/catalog/services/%s/resourceInstances/%s/externalReferences/%s/%s", serviceVertexUuid, - COMPONENT_ID, MONITORING_OBJECT_TYPE, REF_5); - String getPath = String - .format("/v1/catalog/services/%s/version/%s/resourceInstances/%s/externalReferences/%s", serviceVertexUuid, - VERSION, COMPONENT_ID, MONITORING_OBJECT_TYPE); - Response response = target() - .path(updatePath) - .request(MediaType.APPLICATION_JSON) - .accept(MediaType.APPLICATION_JSON) - .header(Constants.X_ECOMP_INSTANCE_ID_HEADER, "mockXEcompInstanceId") - .header(Constants.USER_ID_HEADER, designerUser.getUserId()) - .put(Entity.json(new ExternalRefDTO(REF_4))); - - //Verify that the updated reference is returned in body - ExternalRefDTO putResponseBody = response.readEntity(ExternalRefDTO.class); - assertThat(putResponseBody.getReferenceUUID()).isEqualTo(REF_4); - assertThat(response.getStatus()).isEqualTo(HttpStatus.OK.value()); - - //Check that GET will include the updated reference - response = target() - .path(getPath) - .request() - .accept(MediaType.APPLICATION_JSON) - .header(Constants.X_ECOMP_INSTANCE_ID_HEADER, "mockXEcompInstanceId") - .header(Constants.USER_ID_HEADER, designerUser.getUserId()) - .get(); - - List dto = response.readEntity(new GenericType>() { - }); - assertThat(dto).containsExactlyInAnyOrder(REF_1, REF_2, REF_3, REF_4); - assertThat(response.getStatus()).isEqualTo(HttpStatus.OK.value()); - } - - /* - * Rainy Scenarios - */ - @Test - void testAddExternalRefForNonExistingAssetId() { - String path = String - .format("/v1/catalog/services/%s/resourceInstances/%s/externalReferences/%s", "non-existing-uuid", - COMPONENT_ID, MONITORING_OBJECT_TYPE); - Response response = target() - .path(path) - .request(MediaType.APPLICATION_JSON) - .accept(MediaType.APPLICATION_JSON) - .header(Constants.X_ECOMP_INSTANCE_ID_HEADER, "mockXEcompInstanceId") - .header(Constants.USER_ID_HEADER, designerUser.getUserId()) - .post(Entity.json(new ExternalRefDTO(REF_4))); - - assertThat(response.getStatus()).isEqualTo(HttpStatus.NOT_FOUND.value()); - } - - @Test - void testAddExternalRefForNonExistingCompInstId() { - String path = String - .format("/v1/catalog/services/%s/resourceInstances/%s/externalReferences/%s", serviceVertexUuid, - "FAKE_COM_ID", MONITORING_OBJECT_TYPE); - Response response = target() - .path(path) - .request(MediaType.APPLICATION_JSON) - .accept(MediaType.APPLICATION_JSON) - .header(Constants.X_ECOMP_INSTANCE_ID_HEADER, "mockXEcompInstanceId") - .header(Constants.USER_ID_HEADER, designerUser.getUserId()) - .post(Entity.json(new ExternalRefDTO(REF_4))); - - assertThat(response.getStatus()).isEqualTo(HttpStatus.NOT_FOUND.value()); - } - - @Test - void testAddExistingExternalRef() { - String path = String - .format("/v1/catalog/services/%s/resourceInstances/%s/externalReferences/%s", serviceVertexUuid, - COMPONENT_ID, MONITORING_OBJECT_TYPE); - Response response = target() - .path(path) - .request(MediaType.APPLICATION_JSON) - .accept(MediaType.APPLICATION_JSON) - .header(Constants.X_ECOMP_INSTANCE_ID_HEADER, "mockXEcompInstanceId") - .header(Constants.USER_ID_HEADER, designerUser.getUserId()) - .post(Entity.json(new ExternalRefDTO(REF_1))); - - assertThat(response.getStatus()).isEqualTo(HttpStatus.OK.value()); //Ref not created but still OK is returned - } - - @Test - void testUpdateExternalRefForNonExistingAssetId() { - String updatePath = String.format("/v1/catalog/services/%s/resourceInstances/%s/externalReferences/%s/%s", - "nonExistingServiceVertexUuid", COMPONENT_ID, MONITORING_OBJECT_TYPE, REF_5); - - Response response = target() - .path(updatePath) - .request(MediaType.APPLICATION_JSON) - .accept(MediaType.APPLICATION_JSON) - .header(Constants.X_ECOMP_INSTANCE_ID_HEADER, "mockXEcompInstanceId") - .header(Constants.USER_ID_HEADER, designerUser.getUserId()) - .put(Entity.json(new ExternalRefDTO(REF_4))); - - //Verify that the 404 is returned - assertThat(response.getStatus()).isEqualTo(HttpStatus.NOT_FOUND.value()); - - } - - @Test - void testUpdateExternalRefForNonExistingObjectIdOrOldRef() { - String updatePath = String - .format("/v1/catalog/services/%s/resourceInstances/%s/externalReferences/%s/%s", serviceVertexUuid, - COMPONENT_ID, "FAKE_OBJ_TYPE", REF_5); - - Response response = target() - .path(updatePath) - .request(MediaType.APPLICATION_JSON) - .accept(MediaType.APPLICATION_JSON) - .header(Constants.X_ECOMP_INSTANCE_ID_HEADER, "mockXEcompInstanceId") - .header(Constants.USER_ID_HEADER, designerUser.getUserId()) - .put(Entity.json(new ExternalRefDTO(REF_4))); - - assertThat(response.getStatus()).isEqualTo(HttpStatus.NOT_FOUND.value()); - } - - @Test - void testDeleteExternalRefForNonExistingAssetId() { - String deletePath = String - .format("/v1/catalog/services/%s/resourceInstances/%s/externalReferences/%s/%s", "non-existing-asset", - COMPONENT_ID, MONITORING_OBJECT_TYPE, REF_5); - Response response = target() - .path(deletePath) - .request(MediaType.APPLICATION_JSON) - .accept(MediaType.APPLICATION_JSON) - .header(Constants.X_ECOMP_INSTANCE_ID_HEADER, "mockXEcompInstanceId") - .header(Constants.USER_ID_HEADER, designerUser.getUserId()) - .delete(); - - assertThat(response.getStatus()).isEqualTo(HttpStatus.NOT_FOUND.value()); - } - - @Test - void testDeleteExternalRefForNonExistingRef() { - String deletePath = String - .format("/v1/catalog/services/%s/resourceInstances/%s/externalReferences/%s/%s", serviceVertexUuid, - COMPONENT_ID, MONITORING_OBJECT_TYPE, "FAKE_REF"); - Response response = target() - .path(deletePath) - .request(MediaType.APPLICATION_JSON) - .accept(MediaType.APPLICATION_JSON) - .header(Constants.X_ECOMP_INSTANCE_ID_HEADER, "mockXEcompInstanceId") - .delete(); - - assertThat(response.getStatus()).isEqualTo(HttpStatus.NOT_FOUND.value()); - } - - @Test - void testGetExternalRefsForNonExistingAsset() { - String path = String - .format("/v1/catalog/services/%s/version/%s/resourceInstances/%s/externalReferences/%s", "fake-asset-id", - VERSION, COMPONENT_ID, MONITORING_OBJECT_TYPE); - - Response response = target() - .path(path) - .request() - .accept(MediaType.APPLICATION_JSON) - .header(Constants.X_ECOMP_INSTANCE_ID_HEADER, "mockXEcompInstanceId") - .header(Constants.USER_ID_HEADER, designerUser.getUserId()) - .get(); - - assertThat(response.getStatus()).isEqualTo(HttpStatus.NOT_FOUND.value()); - } - - @Test - void testGetExternalRefsForNonExistingVersion() { - String path = String - .format("/v1/catalog/services/%s/version/%s/resourceInstances/%s/externalReferences/%s", serviceVertexUuid, - FAKE_VERSION, COMPONENT_ID, MONITORING_OBJECT_TYPE); - - Response response = target() - .path(path) - .request() - .accept(MediaType.APPLICATION_JSON) - .header(Constants.X_ECOMP_INSTANCE_ID_HEADER, "mockXEcompInstanceId") - .header(Constants.USER_ID_HEADER, designerUser.getUserId()) - .get(); - - assertThat(response.getStatus()).isEqualTo(HttpStatus.NOT_FOUND.value()); - } - - @Test - void testDeleteExternalRefsForExistingComponentInstanceWithUnauthorizedUser() { - String path = String - .format("/v1/catalog/services/%s/resourceInstances/%s/externalReferences/%s/%s", serviceVertexUuid, - COMPONENT_ID, MONITORING_OBJECT_TYPE, REF_5); - - Response response = target() - .path(path) - .request() - .accept(MediaType.APPLICATION_JSON) - .header(Constants.X_ECOMP_INSTANCE_ID_HEADER, "mockXEcompInstanceId") - .header(Constants.USER_ID_HEADER, otherUser.getUserId()) - .delete(); - - assertThat(response.getStatus()).isEqualTo(HttpStatus.UNAUTHORIZED.value()); - } - - @Test - void testDeleteExternalRefForUserWhichIsNotCurrentUpdater() { - String deletePath = String - .format("/v1/catalog/services/%s/resourceInstances/%s/externalReferences/%s/%s", serviceVertexUuid, - COMPONENT_ID, MONITORING_OBJECT_TYPE, REF_5); - Response response = target() - .path(deletePath) - .request(MediaType.APPLICATION_JSON) - .accept(MediaType.APPLICATION_JSON) - .header(Constants.X_ECOMP_INSTANCE_ID_HEADER, "mockXEcompInstanceId") - .header(Constants.USER_ID_HEADER, otherDesignerUser.getUserId()) - .delete(); - - assertThat(response.getStatus()).isEqualTo(HttpStatus.UNAUTHORIZED.value()); - } - - @Override - protected Application configure() { - ApplicationContext context = new AnnotationConfigApplicationContext(TestSpringConfig.class); - return new ResourceConfig(ExternalRefsServlet.class) - .register(DefaultExceptionMapper.class) - .register(ComponentExceptionMapper.class) - .register(StorageExceptionMapper.class) - .property("contextConfig", context); - } - - @Configuration - @PropertySource("classpath:dao.properties") - public class TestSpringConfig { - - private GraphVertex serviceVertex; - private GraphVertex resourceVertex; - private ExternalReferencesOperation externalReferenceOperation; - private HealingJanusGraphDao janusGraphDao; - - @Bean - ExternalRefsServlet externalRefsServlet() { - return new ExternalRefsServlet(userAdmin, componentUtils, externalRefsBusinessLogic()); - } - - @Bean - OperationUtils operationUtils() { - return new OperationUtils(janusGraphDao()); - } - - @Bean - ComponentExceptionMapper componentExceptionMapper() { - return new ComponentExceptionMapper(componentUtils); - } - - @Bean - StorageExceptionMapper storageExceptionMapper() { - return new StorageExceptionMapper(componentUtils); - } - - @Bean - DefaultExceptionMapper defaultExceptionMapper() { - return new DefaultExceptionMapper(); - } - - @Bean - ExternalRefsBusinessLogic externalRefsBusinessLogic() { - return new ExternalRefsBusinessLogic(externalReferencesOperation(), toscaOperationFacade(), - accessValidations(), componentLocker()); - } - - @Bean - AccessValidations accessValidations() { - return accessValidationsMock; - } - - @Bean - ExternalReferencesOperation externalReferencesOperation() { - this.externalReferenceOperation = new ExternalReferencesOperation(janusGraphDao(), nodeTypeOpertaion(), - topologyTemplateOperation(), idMapper()); - this.externalReferenceOperation.setHealingPipelineDao(healingPipelineDao()); - GraphTestUtils.clearGraph(janusGraphDao); - initGraphForTest(); - return this.externalReferenceOperation; - } - - @Bean - ToscaOperationFacade toscaOperationFacade() { - return toscaOperationFacadeMock; - } - - @Bean - IdMapper idMapper() { - when(idMapper.mapComponentNameToUniqueId(eq(COMPONENT_ID), any(GraphVertex.class))) - .thenReturn(COMPONENT_ID); - when(idMapper.mapUniqueIdToComponentNameTo(eq(COMPONENT_ID), any(GraphVertex.class))) - .thenReturn(COMPONENT_ID); - when(idMapper.mapComponentNameToUniqueId(eq(FAKE_COMPONENT_ID), any(GraphVertex.class))).thenReturn(null); - return idMapper; - } - - @Bean - TopologyTemplateOperation topologyTemplateOperation() { - return new TopologyTemplateOperation(); - } - - @Bean - ArchiveOperation archiveOperation() { - return new ArchiveOperation(janusGraphDao(), graphLockOperation()); - } - - @Bean - IGraphLockOperation graphLockOperation() { - return graphLockOperation; - } - - @Bean - NodeTypeOperation nodeTypeOpertaion() { - return new NodeTypeOperation(null); - } - - @Bean - NodeTemplateOperation nodeTemplateOperation() { - return new NodeTemplateOperation(); - } - - @Bean - GroupsOperation groupsOperation() { - return new GroupsOperation(); - } - - @Bean - HealingJanusGraphDao janusGraphDao() { - this.janusGraphDao = new HealingJanusGraphDao(healingPipelineDao(), janusGraphClient()); - return janusGraphDao; - } - - @Bean - JanusGraphClient janusGraphClient() { - return new JanusGraphClient(janusGraphClientStrategy()); - } - - @Bean - JanusGraphClientStrategy janusGraphClientStrategy() { - return new DAOJanusGraphStrategy(); - } - - @Bean - CategoryOperation categoryOperation() { - return new CategoryOperation(); - } - - @Bean - ComponentLocker componentLocker() { - return componentLocker; - } - - @Bean - JanusGraphGenericDao janusGraphGenericDao() { - return janusGraphGenericDao; - } - - @Bean("healingPipelineDao") - HealingPipelineDao healingPipelineDao() { - HealingPipelineDao healingPipelineDao = new HealingPipelineDao(); - healingPipelineDao.setHealVersion(1); - healingPipelineDao.initHealVersion(); - return healingPipelineDao; - } - - @Bean - ContainerInstanceTypesData containerInstanceTypesData() { - return new ContainerInstanceTypesData(); - } - - private void initGraphForTest() { - if (!setupDone) { - - resourceVertex = GraphTestUtils - .createResourceVertex(janusGraphDao, new HashMap<>(), ResourceTypeEnum.VF); - resourceVertexUuid = resourceVertex.getUniqueId(); - - //create a service and add ref - serviceVertex = GraphTestUtils.createServiceVertex(janusGraphDao, new HashMap<>()); - serviceVertexUuid = this.serviceVertex.getUniqueId(); - - //monitoring references - externalReferenceOperation - .addExternalReference(serviceVertexUuid, COMPONENT_ID, MONITORING_OBJECT_TYPE, REF_1); - externalReferenceOperation - .addExternalReference(serviceVertexUuid, COMPONENT_ID, MONITORING_OBJECT_TYPE, REF_2); - externalReferenceOperation - .addExternalReference(serviceVertexUuid, COMPONENT_ID, MONITORING_OBJECT_TYPE, REF_3); - externalReferenceOperation - .addExternalReference(serviceVertexUuid, COMPONENT_ID, MONITORING_OBJECT_TYPE, REF_5); - - //workflow references - externalReferenceOperation - .addExternalReference(serviceVertexUuid, COMPONENT_ID, WORKFLOW_OBJECT_TYPE, REF_6); - - final JanusGraphOperationStatus commit = this.janusGraphDao.commit(); - assertThat(commit).isEqualTo(JanusGraphOperationStatus.OK); - } - } - - - } -} diff --git a/catalog-be/src/test/java/org/openecomp/sdc/be/externalapi/servlet/ExternalRefsServletTest.java b/catalog-be/src/test/java/org/openecomp/sdc/be/externalapi/servlet/ExternalRefsServletTest.java new file mode 100644 index 0000000000..6264e349a1 --- /dev/null +++ b/catalog-be/src/test/java/org/openecomp/sdc/be/externalapi/servlet/ExternalRefsServletTest.java @@ -0,0 +1,790 @@ +/*- + * ============LICENSE_START======================================================= + * SDC + * ================================================================================ + * Copyright (C) 2017 AT&T 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========================================================= + * Modifications copyright (c) 2019 Nokia + * ================================================================================ + */ + +package org.openecomp.sdc.be.externalapi.servlet; + +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.doThrow; +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.when; + +import fj.data.Either; +import java.util.HashMap; +import java.util.LinkedList; +import java.util.List; +import java.util.Map; +import javax.servlet.ServletContext; +import javax.ws.rs.client.Entity; +import javax.ws.rs.core.Application; +import javax.ws.rs.core.GenericType; +import javax.ws.rs.core.MediaType; +import javax.ws.rs.core.Response; +import org.glassfish.jersey.server.ResourceConfig; +import org.glassfish.jersey.test.JerseyTest; +import org.glassfish.jersey.test.TestProperties; +import org.junit.jupiter.api.AfterEach; +import org.junit.jupiter.api.BeforeAll; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; +import org.openecomp.sdc.be.components.impl.ComponentLocker; +import org.openecomp.sdc.be.components.impl.ExternalRefsBusinessLogic; +import org.openecomp.sdc.be.components.impl.exceptions.ByResponseFormatComponentException; +import org.openecomp.sdc.be.components.path.utils.GraphTestUtils; +import org.openecomp.sdc.be.components.validation.AccessValidations; +import org.openecomp.sdc.be.config.Configuration.HeatDeploymentArtifactTimeout; +import org.openecomp.sdc.be.config.ConfigurationManager; +import org.openecomp.sdc.be.dao.DAOJanusGraphStrategy; +import org.openecomp.sdc.be.dao.JanusGraphClientStrategy; +import org.openecomp.sdc.be.dao.api.ActionStatus; +import org.openecomp.sdc.be.dao.impl.HealingPipelineDao; +import org.openecomp.sdc.be.dao.janusgraph.HealingJanusGraphDao; +import org.openecomp.sdc.be.dao.janusgraph.HealingJanusGraphGenericDao; +import org.openecomp.sdc.be.dao.janusgraph.JanusGraphClient; +import org.openecomp.sdc.be.dao.janusgraph.JanusGraphGenericDao; +import org.openecomp.sdc.be.dao.janusgraph.JanusGraphOperationStatus; +import org.openecomp.sdc.be.dao.jsongraph.GraphVertex; +import org.openecomp.sdc.be.datatypes.enums.ResourceTypeEnum; +import org.openecomp.sdc.be.dto.ExternalRefDTO; +import org.openecomp.sdc.be.impl.ComponentsUtils; +import org.openecomp.sdc.be.impl.ServletUtils; +import org.openecomp.sdc.be.impl.WebAppContextWrapper; +import org.openecomp.sdc.be.model.Component; +import org.openecomp.sdc.be.model.User; +import org.openecomp.sdc.be.model.jsonjanusgraph.config.ContainerInstanceTypesData; +import org.openecomp.sdc.be.model.jsonjanusgraph.operations.ArchiveOperation; +import org.openecomp.sdc.be.model.jsonjanusgraph.operations.CategoryOperation; +import org.openecomp.sdc.be.model.jsonjanusgraph.operations.ExternalReferencesOperation; +import org.openecomp.sdc.be.model.jsonjanusgraph.operations.GroupsOperation; +import org.openecomp.sdc.be.model.jsonjanusgraph.operations.NodeTemplateOperation; +import org.openecomp.sdc.be.model.jsonjanusgraph.operations.NodeTypeOperation; +import org.openecomp.sdc.be.model.jsonjanusgraph.operations.TopologyTemplateOperation; +import org.openecomp.sdc.be.model.jsonjanusgraph.operations.ToscaOperationFacade; +import org.openecomp.sdc.be.model.jsonjanusgraph.utils.IdMapper; +import org.openecomp.sdc.be.model.operations.api.IGraphLockOperation; +import org.openecomp.sdc.be.model.operations.impl.ModelElementOperation; +import org.openecomp.sdc.be.model.operations.impl.ModelOperation; +import org.openecomp.sdc.be.model.operations.impl.OperationUtils; +import org.openecomp.sdc.be.servlets.exception.ComponentExceptionMapper; +import org.openecomp.sdc.be.servlets.exception.DefaultExceptionMapper; +import org.openecomp.sdc.be.servlets.exception.StorageExceptionMapper; +import org.openecomp.sdc.be.user.Role; +import org.openecomp.sdc.be.user.UserBusinessLogic; +import org.openecomp.sdc.common.api.ConfigurationSource; +import org.openecomp.sdc.common.api.Constants; +import org.openecomp.sdc.common.impl.ExternalConfiguration; +import org.openecomp.sdc.common.impl.FSConfigurationSource; +import org.openecomp.sdc.exception.ResponseFormat; +import org.springframework.context.ApplicationContext; +import org.springframework.context.annotation.AnnotationConfigApplicationContext; +import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.Configuration; +import org.springframework.context.annotation.PropertySource; +import org.springframework.http.HttpStatus; +import org.springframework.web.context.WebApplicationContext; + +class ExternalRefsServletTest extends JerseyTest { + + private static final String COMPONENT_ID = "ci-MyComponentName"; + private static final String FAKE_COMPONENT_ID = "ci-MyFAKEComponentName"; + private static final String MONITORING_OBJECT_TYPE = "monitoring"; + private static final String WORKFLOW_OBJECT_TYPE = "workflow"; + private static final String VERSION = "0.1"; + private static final String FAKE_VERSION = "0.5"; + private static final String REF_1 = "ref1"; + private static final String REF_2 = "ref2"; + private static final String REF_3 = "ref3"; + private static final String REF_4 = "ref4"; + private static final String REF_5 = "ref5"; + //workflow + private static final String REF_6 = "ref6"; + private static String serviceVertexUuid; + private static String resourceVertexUuid; + /* Users */ + private static final User adminUser = new User("admin", "admin", "admin", "admin@email.com", Role.ADMIN.name(), + System.currentTimeMillis()); + private static final User designerUser = new User("designer", "designer", "designer", "designer@email.com", + Role.DESIGNER.name(), System + .currentTimeMillis()); + private static final User otherDesignerUser = new User("otherDesigner", "otherDesigner", "otherDesigner", + "otherDesigner@email.com", Role.DESIGNER + .name(), System.currentTimeMillis()); + private static final User otherUser = new User("other", "other", "other", "other@email.com", Role.DESIGNER.name(), + System.currentTimeMillis()); + + private static final IdMapper idMapper = mock(IdMapper.class); + private static final WebAppContextWrapper webAppContextWrapper = mock(WebAppContextWrapper.class); + private static final ServletContext servletContext = mock(ServletContext.class); + private static final WebApplicationContext webApplicationContext = mock(WebApplicationContext.class); + private static final ServletUtils servletUtils = mock(ServletUtils.class); + private static final ComponentsUtils componentUtils = mock(ComponentsUtils.class); + private static final ResponseFormat responseFormat = mock(ResponseFormat.class); + private static final ResponseFormat notFoundResponseFormat = mock(ResponseFormat.class); + private static final ResponseFormat badRequestResponseFormat = mock(ResponseFormat.class); + private static final UserBusinessLogic userAdmin = mock(UserBusinessLogic.class); + private static final ToscaOperationFacade toscaOperationFacadeMock = mock(ToscaOperationFacade.class); + private static final AccessValidations accessValidationsMock = mock(AccessValidations.class); + private static final ComponentLocker componentLocker = mock(ComponentLocker.class); + private static final HealingJanusGraphGenericDao janusGraphGenericDao = mock(HealingJanusGraphGenericDao.class); + private static final IGraphLockOperation graphLockOperation = mock(IGraphLockOperation.class); + private static final ByResponseFormatComponentException ce = mock(ByResponseFormatComponentException.class); + private static final Component resourceComponentMock = mock(Component.class); + private static final Component serviceComponentMock = mock(Component.class); + private static final ModelOperation modelOperation = mock(ModelOperation.class); + private static final ModelElementOperation modelElementOperation = mock(ModelElementOperation.class); + + @BeforeAll + public static void setup() { + + //Needed for User Authorization + //======================================================================================================================== + when(servletContext.getAttribute(Constants.WEB_APPLICATION_CONTEXT_WRAPPER_ATTR)).thenReturn(webAppContextWrapper); + when(webAppContextWrapper.getWebAppContext(servletContext)).thenReturn(webApplicationContext); + when(webApplicationContext.getBean(ServletUtils.class)).thenReturn(servletUtils); + when(servletUtils.getUserAdmin()).thenReturn(userAdmin); + when(servletUtils.getComponentsUtils()).thenReturn(componentUtils); + when(componentUtils.getResponseFormat(ActionStatus.RESTRICTED_OPERATION)).thenReturn(responseFormat); + when(responseFormat.getStatus()).thenReturn(HttpStatus.UNAUTHORIZED.value()); + + when(ce.getResponseFormat()).thenReturn(responseFormat); + doThrow(ce).when(accessValidationsMock).validateUserCanWorkOnComponent(any(), any(), eq(otherDesignerUser.getUserId()), any()); + doThrow(ce).when(accessValidationsMock).validateUserCanWorkOnComponent(any(), any(), eq(otherUser.getUserId()), any()); + + //Needed for error configuration + when(notFoundResponseFormat.getStatus()).thenReturn(HttpStatus.NOT_FOUND.value()); + when(badRequestResponseFormat.getStatus()).thenReturn(HttpStatus.BAD_REQUEST.value()); + when(componentUtils.getResponseFormat(eq(ActionStatus.RESOURCE_NOT_FOUND), (String[]) any())).thenReturn(notFoundResponseFormat); + when(componentUtils.getResponseFormat(eq(ActionStatus.COMPONENT_VERSION_NOT_FOUND), (String[]) any())).thenReturn(notFoundResponseFormat); + when(componentUtils.getResponseFormat(eq(ActionStatus.COMPONENT_INSTANCE_NOT_FOUND), (String[]) any())).thenReturn(notFoundResponseFormat); + when(componentUtils.getResponseFormat(eq(ActionStatus.EXT_REF_NOT_FOUND), (String[]) any())).thenReturn(notFoundResponseFormat); + when(componentUtils.getResponseFormat(eq(ActionStatus.MISSING_X_ECOMP_INSTANCE_ID), (String[]) any())).thenReturn(badRequestResponseFormat); + when(userAdmin.getUser(adminUser.getUserId(), false)).thenReturn(adminUser); + when(userAdmin.getUser(designerUser.getUserId(), false)).thenReturn(designerUser); + when(userAdmin.getUser(otherUser.getUserId(), false)).thenReturn(otherUser); + //======================================================================================================================== + + String appConfigDir = "src/test/resources/config/catalog-be"; + ConfigurationSource configurationSource = new FSConfigurationSource(ExternalConfiguration.getChangeListener(), appConfigDir); + ConfigurationManager configurationManager = new ConfigurationManager(configurationSource); + + org.openecomp.sdc.be.config.Configuration configuration = new org.openecomp.sdc.be.config.Configuration(); + configuration.setJanusGraphInMemoryGraph(true); + HeatDeploymentArtifactTimeout heatDeploymentArtifactTimeout = new HeatDeploymentArtifactTimeout(); + heatDeploymentArtifactTimeout.setDefaultMinutes(30); + configuration.setAafAuthNeeded(false); + configuration.setHeatArtifactDeploymentTimeout(heatDeploymentArtifactTimeout); + configurationManager.setConfiguration(configuration); + ExternalConfiguration.setAppName("catalog-be"); + } + + @BeforeEach + public void before() throws Exception { + super.setUp(); + + when(resourceComponentMock.getVersion()).thenReturn(VERSION); + when(resourceComponentMock.getUniqueId()).thenReturn(resourceVertexUuid); + + when(serviceComponentMock.getVersion()).thenReturn(VERSION); + when(serviceComponentMock.getUniqueId()).thenReturn(serviceVertexUuid); + + List listComponents = new LinkedList<>(); + listComponents.add(serviceComponentMock); + + when(toscaOperationFacadeMock.getComponentListByUuid(eq(serviceVertexUuid), any())).thenReturn(Either.left(listComponents)); + when(toscaOperationFacadeMock.getComponentByUuidAndVersion(serviceVertexUuid, VERSION)).thenReturn(Either.left(serviceComponentMock)); + when(toscaOperationFacadeMock.getComponentByUuidAndVersion(resourceVertexUuid, VERSION)).thenReturn(Either.left(resourceComponentMock)); + when(toscaOperationFacadeMock.getLatestComponentByUuid(eq(serviceVertexUuid), any())).thenReturn(Either.left(listComponents.get(0))); + when(toscaOperationFacadeMock.getLatestComponentByUuid(eq(resourceVertexUuid), any())).thenReturn(Either.left(resourceComponentMock)); + } + + @AfterEach + void after() throws Exception { + super.tearDown(); + } + + @Test + void testGetExternalRefsForExistingComponentInstance() { + String path = String + .format("/v1/catalog/services/%s/version/%s/resourceInstances/%s/externalReferences/%s", serviceVertexUuid, + VERSION, COMPONENT_ID, MONITORING_OBJECT_TYPE); + + Response response = target() + .path(path) + .request() + .accept(MediaType.APPLICATION_JSON) + .header(Constants.X_ECOMP_INSTANCE_ID_HEADER, "mockXEcompInstanceId") + .header(Constants.USER_ID_HEADER, designerUser.getUserId()) + .get(); + + List dto = response.readEntity(new GenericType>() { + }); + assertThat(dto).containsExactly(REF_1, REF_2, REF_3, REF_5); + assertThat(response.getStatus()).isEqualTo(HttpStatus.OK.value()); + } + + @Test + void testGetExternalRefsForAsset() { + String path = String + .format("/v1/catalog/services/%s/version/%s/externalReferences/%s", serviceVertexUuid, VERSION, + MONITORING_OBJECT_TYPE); + + Response response = target() + .path(path) + .request() + .accept(MediaType.APPLICATION_JSON) + .header(Constants.X_ECOMP_INSTANCE_ID_HEADER, "mockXEcompInstanceId") + .header(Constants.USER_ID_HEADER, designerUser.getUserId()) + .get(); + + Map> dtoMap = response.readEntity(new GenericType>>() { + }); + assertThat(dtoMap.get(COMPONENT_ID)).containsExactly(REF_1, REF_2, REF_3, REF_5); + assertThat(response.getStatus()).isEqualTo(HttpStatus.OK.value()); + } + + @Test + void testGetExternalRefsForAssetWithMissingEcompHeader() { + String path = String + .format("/v1/catalog/services/%s/version/%s/externalReferences/%s", serviceVertexUuid, VERSION, + MONITORING_OBJECT_TYPE); + + //No X-Ecomp-Instance-ID header + Response response = target() + .path(path) + .request() + .accept(MediaType.APPLICATION_JSON) + .header(Constants.USER_ID_HEADER, designerUser.getUserId()) + .get(); + + assertThat(response.getStatus()).isEqualTo(HttpStatus.BAD_REQUEST.value()); + } + + @Test + void testAddExternalRefForResource() { + String path = String + .format("/v1/catalog/resources/%s/resourceInstances/%s/externalReferences/%s", resourceVertexUuid, + COMPONENT_ID, MONITORING_OBJECT_TYPE); + String getPath = String + .format("/v1/catalog/resources/%s/version/%s/externalReferences/%s", resourceVertexUuid, VERSION, + MONITORING_OBJECT_TYPE); + + Response response = target() + .path(path) + .request(MediaType.APPLICATION_JSON) + .accept(MediaType.APPLICATION_JSON) + .header(Constants.X_ECOMP_INSTANCE_ID_HEADER, "mockXEcompInstanceId") + .header(Constants.USER_ID_HEADER, designerUser.getUserId()) + .post(Entity.json(new ExternalRefDTO(REF_1))); + + assertThat(response.getStatus()).isEqualTo(HttpStatus.CREATED.value()); + + //Check that GET will include the new reference + response = target() + .path(getPath) + .request() + .accept(MediaType.APPLICATION_JSON) + .header(Constants.X_ECOMP_INSTANCE_ID_HEADER, "mockXEcompInstanceId") + .header(Constants.USER_ID_HEADER, adminUser.getUserId()) + .get(); + + Map> dto = response.readEntity(new GenericType>>() { + }); + assertThat(dto.get(COMPONENT_ID)).containsExactlyInAnyOrder(REF_1); + assertThat(response.getStatus()).isEqualTo(HttpStatus.OK.value()); + } + + @Test + void testAddExternalRefForExistingComponentInstance() { + String path = String + .format("/v1/catalog/services/%s/resourceInstances/%s/externalReferences/%s", serviceVertexUuid, + COMPONENT_ID, MONITORING_OBJECT_TYPE); + String getPath = String + .format("/v1/catalog/services/%s/version/%s/resourceInstances/%s/externalReferences/%s", serviceVertexUuid, + VERSION, COMPONENT_ID, MONITORING_OBJECT_TYPE); + Response response = target() + .path(path) + .request(MediaType.APPLICATION_JSON) + .accept(MediaType.APPLICATION_JSON) + .header(Constants.X_ECOMP_INSTANCE_ID_HEADER, "mockXEcompInstanceId") + .header(Constants.USER_ID_HEADER, designerUser.getUserId()) + .post(Entity.json(new ExternalRefDTO(REF_4))); + + assertThat(response.getStatus()).isEqualTo(HttpStatus.CREATED.value()); + + //Check that GET will include the new reference + response = target() + .path(getPath) + .request() + .accept(MediaType.APPLICATION_JSON) + .header(Constants.X_ECOMP_INSTANCE_ID_HEADER, "mockXEcompInstanceId") + .header(Constants.USER_ID_HEADER, adminUser.getUserId()) + .get(); + + List dto = response.readEntity(new GenericType>() { + }); + assertThat(dto).containsExactlyInAnyOrder(REF_1, REF_2, REF_3, REF_4, REF_5); + assertThat(response.getStatus()).isEqualTo(HttpStatus.OK.value()); + } + + @Test + void testDeleteExternalRefForExistingComponentInstance() { + String deletePath = String + .format("/v1/catalog/services/%s/resourceInstances/%s/externalReferences/%s/%s", serviceVertexUuid, + COMPONENT_ID, MONITORING_OBJECT_TYPE, REF_5); + String getPath = String + .format("/v1/catalog/services/%s/version/%s/resourceInstances/%s/externalReferences/%s", serviceVertexUuid, + VERSION, COMPONENT_ID, MONITORING_OBJECT_TYPE); + Response response = target() + .path(deletePath) + .request(MediaType.APPLICATION_JSON) + .accept(MediaType.APPLICATION_JSON) + .header(Constants.X_ECOMP_INSTANCE_ID_HEADER, "mockXEcompInstanceId") + .header(Constants.USER_ID_HEADER, designerUser.getUserId()) + .delete(); + + //Verify that the deleted reference is returned in body + ExternalRefDTO dto = response.readEntity(ExternalRefDTO.class); + assertThat(dto.getReferenceUUID()).isEqualTo(REF_5); + assertThat(response.getStatus()).isEqualTo(HttpStatus.OK.value()); + + //Check that GET will NOT include the deleted reference + response = target() + .path(getPath) + .request() + .accept(MediaType.APPLICATION_JSON) + .header(Constants.X_ECOMP_INSTANCE_ID_HEADER, "mockXEcompInstanceId") + .header(Constants.USER_ID_HEADER, designerUser.getUserId()) + .get(); + + List getResponse = response.readEntity(new GenericType>() { + }); + assertThat(getResponse).containsExactlyInAnyOrder(REF_1, REF_2, REF_3); + assertThat(response.getStatus()).isEqualTo(HttpStatus.OK.value()); + } + + @Test + void testUpdateExternalRefForExistingComponentInstance() { + String updatePath = String + .format("/v1/catalog/services/%s/resourceInstances/%s/externalReferences/%s/%s", serviceVertexUuid, + COMPONENT_ID, MONITORING_OBJECT_TYPE, REF_5); + String getPath = String + .format("/v1/catalog/services/%s/version/%s/resourceInstances/%s/externalReferences/%s", serviceVertexUuid, + VERSION, COMPONENT_ID, MONITORING_OBJECT_TYPE); + Response response = target() + .path(updatePath) + .request(MediaType.APPLICATION_JSON) + .accept(MediaType.APPLICATION_JSON) + .header(Constants.X_ECOMP_INSTANCE_ID_HEADER, "mockXEcompInstanceId") + .header(Constants.USER_ID_HEADER, designerUser.getUserId()) + .put(Entity.json(new ExternalRefDTO(REF_4))); + + //Verify that the updated reference is returned in body + ExternalRefDTO putResponseBody = response.readEntity(ExternalRefDTO.class); + assertThat(putResponseBody.getReferenceUUID()).isEqualTo(REF_4); + assertThat(response.getStatus()).isEqualTo(HttpStatus.OK.value()); + + //Check that GET will include the updated reference + response = target() + .path(getPath) + .request() + .accept(MediaType.APPLICATION_JSON) + .header(Constants.X_ECOMP_INSTANCE_ID_HEADER, "mockXEcompInstanceId") + .header(Constants.USER_ID_HEADER, designerUser.getUserId()) + .get(); + + List dto = response.readEntity(new GenericType>() { + }); + assertThat(dto).containsExactlyInAnyOrder(REF_1, REF_2, REF_3, REF_4); + assertThat(response.getStatus()).isEqualTo(HttpStatus.OK.value()); + } + + /* + * Rainy Scenarios + */ + @Test + void testAddExternalRefForNonExistingAssetId() { + String path = String + .format("/v1/catalog/services/%s/resourceInstances/%s/externalReferences/%s", "non-existing-uuid", + COMPONENT_ID, MONITORING_OBJECT_TYPE); + Response response = target() + .path(path) + .request(MediaType.APPLICATION_JSON) + .accept(MediaType.APPLICATION_JSON) + .header(Constants.X_ECOMP_INSTANCE_ID_HEADER, "mockXEcompInstanceId") + .header(Constants.USER_ID_HEADER, designerUser.getUserId()) + .post(Entity.json(new ExternalRefDTO(REF_4))); + + assertThat(response.getStatus()).isEqualTo(HttpStatus.NOT_FOUND.value()); + } + + @Test + void testAddExternalRefForNonExistingCompInstId() { + String path = String + .format("/v1/catalog/services/%s/resourceInstances/%s/externalReferences/%s", serviceVertexUuid, + "FAKE_COM_ID", MONITORING_OBJECT_TYPE); + Response response = target() + .path(path) + .request(MediaType.APPLICATION_JSON) + .accept(MediaType.APPLICATION_JSON) + .header(Constants.X_ECOMP_INSTANCE_ID_HEADER, "mockXEcompInstanceId") + .header(Constants.USER_ID_HEADER, designerUser.getUserId()) + .post(Entity.json(new ExternalRefDTO(REF_4))); + + assertThat(response.getStatus()).isEqualTo(HttpStatus.NOT_FOUND.value()); + } + + @Test + void testAddExistingExternalRef() { + String path = String + .format("/v1/catalog/services/%s/resourceInstances/%s/externalReferences/%s", serviceVertexUuid, + COMPONENT_ID, MONITORING_OBJECT_TYPE); + Response response = target() + .path(path) + .request(MediaType.APPLICATION_JSON) + .accept(MediaType.APPLICATION_JSON) + .header(Constants.X_ECOMP_INSTANCE_ID_HEADER, "mockXEcompInstanceId") + .header(Constants.USER_ID_HEADER, designerUser.getUserId()) + .post(Entity.json(new ExternalRefDTO(REF_1))); + + assertThat(response.getStatus()).isEqualTo(HttpStatus.OK.value()); //Ref not created but still OK is returned + } + + @Test + void testUpdateExternalRefForNonExistingAssetId() { + String updatePath = String.format("/v1/catalog/services/%s/resourceInstances/%s/externalReferences/%s/%s", + "nonExistingServiceVertexUuid", COMPONENT_ID, MONITORING_OBJECT_TYPE, REF_5); + + Response response = target() + .path(updatePath) + .request(MediaType.APPLICATION_JSON) + .accept(MediaType.APPLICATION_JSON) + .header(Constants.X_ECOMP_INSTANCE_ID_HEADER, "mockXEcompInstanceId") + .header(Constants.USER_ID_HEADER, designerUser.getUserId()) + .put(Entity.json(new ExternalRefDTO(REF_4))); + + //Verify that the 404 is returned + assertThat(response.getStatus()).isEqualTo(HttpStatus.NOT_FOUND.value()); + + } + + @Test + void testUpdateExternalRefForNonExistingObjectIdOrOldRef() { + String updatePath = String + .format("/v1/catalog/services/%s/resourceInstances/%s/externalReferences/%s/%s", serviceVertexUuid, + COMPONENT_ID, "FAKE_OBJ_TYPE", REF_5); + + Response response = target() + .path(updatePath) + .request(MediaType.APPLICATION_JSON) + .accept(MediaType.APPLICATION_JSON) + .header(Constants.X_ECOMP_INSTANCE_ID_HEADER, "mockXEcompInstanceId") + .header(Constants.USER_ID_HEADER, designerUser.getUserId()) + .put(Entity.json(new ExternalRefDTO(REF_4))); + + assertThat(response.getStatus()).isEqualTo(HttpStatus.NOT_FOUND.value()); + } + + @Test + void testDeleteExternalRefForNonExistingAssetId() { + String deletePath = String + .format("/v1/catalog/services/%s/resourceInstances/%s/externalReferences/%s/%s", "non-existing-asset", + COMPONENT_ID, MONITORING_OBJECT_TYPE, REF_5); + Response response = target() + .path(deletePath) + .request(MediaType.APPLICATION_JSON) + .accept(MediaType.APPLICATION_JSON) + .header(Constants.X_ECOMP_INSTANCE_ID_HEADER, "mockXEcompInstanceId") + .header(Constants.USER_ID_HEADER, designerUser.getUserId()) + .delete(); + + assertThat(response.getStatus()).isEqualTo(HttpStatus.NOT_FOUND.value()); + } + + @Test + void testDeleteExternalRefForNonExistingRef() { + String deletePath = String + .format("/v1/catalog/services/%s/resourceInstances/%s/externalReferences/%s/%s", serviceVertexUuid, + COMPONENT_ID, MONITORING_OBJECT_TYPE, "FAKE_REF"); + Response response = target() + .path(deletePath) + .request(MediaType.APPLICATION_JSON) + .accept(MediaType.APPLICATION_JSON) + .header(Constants.X_ECOMP_INSTANCE_ID_HEADER, "mockXEcompInstanceId") + .delete(); + + assertThat(response.getStatus()).isEqualTo(HttpStatus.NOT_FOUND.value()); + } + + @Test + void testGetExternalRefsForNonExistingAsset() { + String path = String + .format("/v1/catalog/services/%s/version/%s/resourceInstances/%s/externalReferences/%s", "fake-asset-id", + VERSION, COMPONENT_ID, MONITORING_OBJECT_TYPE); + + Response response = target() + .path(path) + .request() + .accept(MediaType.APPLICATION_JSON) + .header(Constants.X_ECOMP_INSTANCE_ID_HEADER, "mockXEcompInstanceId") + .header(Constants.USER_ID_HEADER, designerUser.getUserId()) + .get(); + + assertThat(response.getStatus()).isEqualTo(HttpStatus.NOT_FOUND.value()); + } + + @Test + void testGetExternalRefsForNonExistingVersion() { + String path = String + .format("/v1/catalog/services/%s/version/%s/resourceInstances/%s/externalReferences/%s", serviceVertexUuid, + FAKE_VERSION, COMPONENT_ID, MONITORING_OBJECT_TYPE); + + Response response = target() + .path(path) + .request() + .accept(MediaType.APPLICATION_JSON) + .header(Constants.X_ECOMP_INSTANCE_ID_HEADER, "mockXEcompInstanceId") + .header(Constants.USER_ID_HEADER, designerUser.getUserId()) + .get(); + + assertThat(response.getStatus()).isEqualTo(HttpStatus.NOT_FOUND.value()); + } + + @Test + void testDeleteExternalRefsForExistingComponentInstanceWithUnauthorizedUser() { + String path = String + .format("/v1/catalog/services/%s/resourceInstances/%s/externalReferences/%s/%s", serviceVertexUuid, + COMPONENT_ID, MONITORING_OBJECT_TYPE, REF_5); + + Response response = target() + .path(path) + .request() + .accept(MediaType.APPLICATION_JSON) + .header(Constants.X_ECOMP_INSTANCE_ID_HEADER, "mockXEcompInstanceId") + .header(Constants.USER_ID_HEADER, otherUser.getUserId()) + .delete(); + + assertThat(response.getStatus()).isEqualTo(HttpStatus.UNAUTHORIZED.value()); + } + + @Test + void testDeleteExternalRefForUserWhichIsNotCurrentUpdater() { + String deletePath = String + .format("/v1/catalog/services/%s/resourceInstances/%s/externalReferences/%s/%s", serviceVertexUuid, + COMPONENT_ID, MONITORING_OBJECT_TYPE, REF_5); + Response response = target() + .path(deletePath) + .request(MediaType.APPLICATION_JSON) + .accept(MediaType.APPLICATION_JSON) + .header(Constants.X_ECOMP_INSTANCE_ID_HEADER, "mockXEcompInstanceId") + .header(Constants.USER_ID_HEADER, otherDesignerUser.getUserId()) + .delete(); + + assertThat(response.getStatus()).isEqualTo(HttpStatus.UNAUTHORIZED.value()); + } + + @Override + protected Application configure() { + ApplicationContext context = new AnnotationConfigApplicationContext(TestSpringConfig.class); + forceSet(TestProperties.CONTAINER_PORT, "0"); + return new ResourceConfig(ExternalRefsServlet.class) + .register(DefaultExceptionMapper.class) + .register(ComponentExceptionMapper.class) + .register(StorageExceptionMapper.class) + .property("contextConfig", context); + } + + @Configuration + @PropertySource("classpath:dao.properties") + static class TestSpringConfig { + + private GraphVertex serviceVertex; + private GraphVertex resourceVertex; + private ExternalReferencesOperation externalReferenceOperation; + private HealingJanusGraphDao janusGraphDao; + + @Bean + ExternalRefsServlet externalRefsServlet() { + return new ExternalRefsServlet(userAdmin, componentUtils, externalRefsBusinessLogic()); + } + + @Bean + OperationUtils operationUtils() { + return new OperationUtils(janusGraphDao()); + } + + @Bean + ComponentExceptionMapper componentExceptionMapper() { + return new ComponentExceptionMapper(componentUtils); + } + + @Bean + StorageExceptionMapper storageExceptionMapper() { + return new StorageExceptionMapper(componentUtils); + } + + @Bean + DefaultExceptionMapper defaultExceptionMapper() { + return new DefaultExceptionMapper(); + } + + @Bean + ExternalRefsBusinessLogic externalRefsBusinessLogic() { + return new ExternalRefsBusinessLogic(externalReferencesOperation(), toscaOperationFacade(), + accessValidations(), componentLocker()); + } + + @Bean + AccessValidations accessValidations() { + return accessValidationsMock; + } + + @Bean + ExternalReferencesOperation externalReferencesOperation() { + this.externalReferenceOperation = new ExternalReferencesOperation(janusGraphDao(), nodeTypeOpertaion(), + topologyTemplateOperation(), idMapper()); + this.externalReferenceOperation.setHealingPipelineDao(healingPipelineDao()); + GraphTestUtils.clearGraph(janusGraphDao); + initGraphForTest(); + return this.externalReferenceOperation; + } + + @Bean + ToscaOperationFacade toscaOperationFacade() { + return toscaOperationFacadeMock; + } + + @Bean + IdMapper idMapper() { + when(idMapper.mapComponentNameToUniqueId(eq(COMPONENT_ID), any(GraphVertex.class))).thenReturn(COMPONENT_ID); + when(idMapper.mapUniqueIdToComponentNameTo(eq(COMPONENT_ID), any(GraphVertex.class))).thenReturn(COMPONENT_ID); + when(idMapper.mapComponentNameToUniqueId(eq(FAKE_COMPONENT_ID), any(GraphVertex.class))).thenReturn(null); + return idMapper; + } + + @Bean + TopologyTemplateOperation topologyTemplateOperation() { + return new TopologyTemplateOperation(); + } + + @Bean + ArchiveOperation archiveOperation() { + return new ArchiveOperation(janusGraphDao(), graphLockOperation()); + } + + @Bean + IGraphLockOperation graphLockOperation() { + return graphLockOperation; + } + + @Bean + NodeTypeOperation nodeTypeOpertaion() { + return new NodeTypeOperation(null); + } + + @Bean + NodeTemplateOperation nodeTemplateOperation() { + return new NodeTemplateOperation(); + } + + @Bean + GroupsOperation groupsOperation() { + return new GroupsOperation(); + } + + @Bean + HealingJanusGraphDao janusGraphDao() { + this.janusGraphDao = new HealingJanusGraphDao(healingPipelineDao(), janusGraphClient()); + return janusGraphDao; + } + + @Bean + JanusGraphClient janusGraphClient() { + return new JanusGraphClient(janusGraphClientStrategy()); + } + + @Bean + JanusGraphClientStrategy janusGraphClientStrategy() { + return new DAOJanusGraphStrategy(); + } + + @Bean + CategoryOperation categoryOperation() { + return new CategoryOperation(); + } + + @Bean + ComponentLocker componentLocker() { + return componentLocker; + } + + @Bean + JanusGraphGenericDao janusGraphGenericDao() { + return janusGraphGenericDao; + } + + @Bean + ModelOperation modelOperation() { + return modelOperation; + } + + @Bean + ModelElementOperation modelElementOperation() { + return modelElementOperation; + } + + @Bean("healingPipelineDao") + HealingPipelineDao healingPipelineDao() { + HealingPipelineDao healingPipelineDao = new HealingPipelineDao(); + healingPipelineDao.setHealVersion(1); + healingPipelineDao.initHealVersion(); + return healingPipelineDao; + } + + @Bean + ContainerInstanceTypesData containerInstanceTypesData() { + return new ContainerInstanceTypesData(); + } + + private void initGraphForTest() { + resourceVertex = GraphTestUtils.createResourceVertex(janusGraphDao, new HashMap<>(), ResourceTypeEnum.VF); + resourceVertexUuid = resourceVertex.getUniqueId(); + + //create a service and add ref + serviceVertex = GraphTestUtils.createServiceVertex(janusGraphDao, new HashMap<>()); + serviceVertexUuid = this.serviceVertex.getUniqueId(); + + //monitoring references + externalReferenceOperation.addExternalReference(serviceVertexUuid, COMPONENT_ID, MONITORING_OBJECT_TYPE, REF_1); + externalReferenceOperation.addExternalReference(serviceVertexUuid, COMPONENT_ID, MONITORING_OBJECT_TYPE, REF_2); + externalReferenceOperation.addExternalReference(serviceVertexUuid, COMPONENT_ID, MONITORING_OBJECT_TYPE, REF_3); + externalReferenceOperation.addExternalReference(serviceVertexUuid, COMPONENT_ID, MONITORING_OBJECT_TYPE, REF_5); + + //workflow references + externalReferenceOperation.addExternalReference(serviceVertexUuid, COMPONENT_ID, WORKFLOW_OBJECT_TYPE, REF_6); + + final JanusGraphOperationStatus commit = this.janusGraphDao.commit(); + assertThat(commit).isEqualTo(JanusGraphOperationStatus.OK); + } + + } +} diff --git a/catalog-be/src/test/java/org/openecomp/sdc/be/servlets/ArchiveEndpointTest.java b/catalog-be/src/test/java/org/openecomp/sdc/be/servlets/ArchiveEndpointTest.java index 2de9000145..2b870baf62 100644 --- a/catalog-be/src/test/java/org/openecomp/sdc/be/servlets/ArchiveEndpointTest.java +++ b/catalog-be/src/test/java/org/openecomp/sdc/be/servlets/ArchiveEndpointTest.java @@ -57,17 +57,18 @@ import org.openecomp.sdc.be.components.impl.exceptions.ComponentException; import org.openecomp.sdc.be.components.path.utils.GraphTestUtils; import org.openecomp.sdc.be.components.validation.AccessValidations; import org.openecomp.sdc.be.components.validation.ComponentValidations; +import org.openecomp.sdc.be.config.Configuration.HeatDeploymentArtifactTimeout; import org.openecomp.sdc.be.config.ConfigurationManager; import org.openecomp.sdc.be.dao.DAOJanusGraphStrategy; import org.openecomp.sdc.be.dao.JanusGraphClientStrategy; import org.openecomp.sdc.be.dao.api.ActionStatus; import org.openecomp.sdc.be.dao.impl.HealingPipelineDao; +import org.openecomp.sdc.be.dao.janusgraph.HealingJanusGraphDao; import org.openecomp.sdc.be.dao.janusgraph.HealingJanusGraphGenericDao; import org.openecomp.sdc.be.dao.janusgraph.JanusGraphClient; import org.openecomp.sdc.be.dao.janusgraph.JanusGraphGenericDao; import org.openecomp.sdc.be.dao.janusgraph.JanusGraphOperationStatus; import org.openecomp.sdc.be.dao.jsongraph.GraphVertex; -import org.openecomp.sdc.be.dao.janusgraph.HealingJanusGraphDao; import org.openecomp.sdc.be.dao.jsongraph.types.EdgeLabelEnum; import org.openecomp.sdc.be.datatypes.enums.ComponentTypeEnum; import org.openecomp.sdc.be.datatypes.enums.GraphPropertyEnum; @@ -118,16 +119,14 @@ class ArchiveEndpointTest extends JerseyTest { private static final String CSAR_UUID1 = "123456789abcdefgh"; private static final String CSAR_UUID2 = "987654321abcdefgh"; - public static final WebAppContextWrapper webAppContextWrapper = mock(WebAppContextWrapper.class); + private static final WebAppContextWrapper webAppContextWrapper = mock(WebAppContextWrapper.class); private static final WebApplicationContext webApplicationContext = mock(WebApplicationContext.class); private static final ServletUtils servletUtils = mock(ServletUtils.class); private static final UserBusinessLogic userAdmin = mock(UserBusinessLogic.class); private static final ComponentsUtils componentUtils = mock(ComponentsUtils.class); private static final CatalogOperation catalogOperations = mock(CatalogOperation.class); - // private static final ToscaOperationFacade toscaOperationFacade = mock(ToscaOperationFacade.class); private static final ToscaOperationFacade toscaOperationFacade = Mockito.spy(new ToscaOperationFacade()); - private static final ResponseFormat responseFormat = mock(ResponseFormat.class); private static final ResponseFormat notFoundResponseFormat = mock(ResponseFormat.class); private static final ResponseFormat badRequestResponseFormat = mock(ResponseFormat.class); @@ -135,8 +134,7 @@ class ArchiveEndpointTest extends JerseyTest { private static final AccessValidations accessValidationsMock = mock(AccessValidations.class); private static final ComponentValidations componentValidationsMock = mock(ComponentValidations.class); private static final IGraphLockOperation graphLockOperation = mock(IGraphLockOperation.class); - private static final HealingJanusGraphGenericDao - janusGraphGenericDao = mock(HealingJanusGraphGenericDao.class); + private static final HealingJanusGraphGenericDao janusGraphGenericDao = mock(HealingJanusGraphGenericDao.class); private static final HealingPipelineDao HEALING_PIPELINE_DAO = mock(HealingPipelineDao.class); private static GraphVertex serviceVertex; private static GraphVertex resourceVertex; @@ -153,9 +151,7 @@ class ArchiveEndpointTest extends JerseyTest { @Bean ArchiveEndpoint archiveEndpoint() { - UserBusinessLogic userBusinessLogic = mock(UserBusinessLogic.class); - ComponentsUtils componentsUtils = mock(ComponentsUtils.class); - return new ArchiveEndpoint(userBusinessLogic, componentsUtils, archiveBusinessLogic()); + return new ArchiveEndpoint(userAdmin, componentUtils, archiveBusinessLogic()); } @Bean @@ -322,15 +318,13 @@ class ArchiveEndpointTest extends JerseyTest { public static void setup() { //Needed for User Authorization //======================================================================================================================== - when(servletContext.getAttribute(Constants.WEB_APPLICATION_CONTEXT_WRAPPER_ATTR)) - .thenReturn(webAppContextWrapper); + when(servletContext.getAttribute(Constants.WEB_APPLICATION_CONTEXT_WRAPPER_ATTR)).thenReturn(webAppContextWrapper); when(webAppContextWrapper.getWebAppContext(servletContext)).thenReturn(webApplicationContext); when(webApplicationContext.getBean(ServletUtils.class)).thenReturn(servletUtils); when(servletUtils.getUserAdmin()).thenReturn(userAdmin); when(servletUtils.getComponentsUtils()).thenReturn(componentUtils); when(componentUtils.getResponseFormat(ActionStatus.RESTRICTED_OPERATION)).thenReturn(responseFormat); - when(componentUtils.getResponseFormat(eq(ActionStatus.INVALID_SERVICE_STATE), any())) - .thenReturn(invalidServiceStateResponseFormat); + when(componentUtils.getResponseFormat(eq(ActionStatus.INVALID_SERVICE_STATE), any())).thenReturn(invalidServiceStateResponseFormat); when(responseFormat.getStatus()).thenReturn(HttpStatus.UNAUTHORIZED.value()); ComponentException ce = new ByResponseFormatComponentException(responseFormat); @@ -340,33 +334,27 @@ class ArchiveEndpointTest extends JerseyTest { when(notFoundResponseFormat.getStatus()).thenReturn(HttpStatus.NOT_FOUND.value()); when(invalidServiceStateResponseFormat.getStatus()).thenReturn(HttpStatus.CONFLICT.value()); when(badRequestResponseFormat.getStatus()).thenReturn(HttpStatus.BAD_REQUEST.value()); - when(componentUtils.getResponseFormat(eq(ActionStatus.RESOURCE_NOT_FOUND), (String[]) any())) - .thenReturn(notFoundResponseFormat); - when(componentUtils.getResponseFormat(eq(ActionStatus.MISSING_X_ECOMP_INSTANCE_ID), (String[]) any())) - .thenReturn(badRequestResponseFormat); + when(componentUtils.getResponseFormat(eq(ActionStatus.RESOURCE_NOT_FOUND), (String[]) any())).thenReturn(notFoundResponseFormat); + when(componentUtils.getResponseFormat(eq(ActionStatus.MISSING_X_ECOMP_INSTANCE_ID), (String[]) any())).thenReturn(badRequestResponseFormat); - when(graphLockOperation.lockComponent(anyString(), any(NodeTypeEnum.class))) - .thenReturn(StorageOperationStatus.OK); + when(graphLockOperation.lockComponent(anyString(), any(NodeTypeEnum.class))).thenReturn(StorageOperationStatus.OK); when(userAdmin.getUser(adminUser.getUserId(), false)).thenReturn(adminUser); when(userAdmin.getUser(designerUser.getUserId(), false)).thenReturn(designerUser); when(userAdmin.getUser(otherUser.getUserId(), false)).thenReturn(otherUser); //======================================================================================================================== String appConfigDir = "src/test/resources/config/catalog-be"; - ConfigurationSource configurationSource = new FSConfigurationSource(ExternalConfiguration.getChangeListener(), - appConfigDir); + ConfigurationSource configurationSource = new FSConfigurationSource(ExternalConfiguration.getChangeListener(), appConfigDir); ConfigurationManager configurationManager = new ConfigurationManager(configurationSource); org.openecomp.sdc.be.config.Configuration configuration = new org.openecomp.sdc.be.config.Configuration(); configuration.setJanusGraphInMemoryGraph(true); - org.openecomp.sdc.be.config.Configuration.HeatDeploymentArtifactTimeout heatDeploymentArtifactTimeout = new org.openecomp.sdc.be.config.Configuration.HeatDeploymentArtifactTimeout(); + HeatDeploymentArtifactTimeout heatDeploymentArtifactTimeout = new HeatDeploymentArtifactTimeout(); heatDeploymentArtifactTimeout.setDefaultMinutes(30); - ; configuration.setHeatArtifactDeploymentTimeout(heatDeploymentArtifactTimeout); configurationManager.setConfiguration(configuration); - configurationManager.setConfiguration(configuration); ExternalConfiguration.setAppName("catalog-be"); } diff --git a/integration-tests/src/test/java/org/onap/sdc/backend/ci/tests/execute/category/GroupingTest.java b/integration-tests/src/test/java/org/onap/sdc/backend/ci/tests/execute/category/GroupingTest.java index f9580780eb..5a528011bd 100644 --- a/integration-tests/src/test/java/org/onap/sdc/backend/ci/tests/execute/category/GroupingTest.java +++ b/integration-tests/src/test/java/org/onap/sdc/backend/ci/tests/execute/category/GroupingTest.java @@ -442,7 +442,6 @@ public class GroupingTest extends CategoriesBaseTest { STATUS_CODE_RESTRICTED_OPERATION, AUDIT_PRODUCT_TYPE); } - // @Ignore("DE176245") @Test public void createProductGroupForNonExistingComponentType() throws Exception { String nonSupportedComponentType = "NonExistingComponentType"; // instead @@ -467,7 +466,6 @@ public class GroupingTest extends CategoriesBaseTest { STATUS_CODE_INVALID_CONTENT, nonSupportedComponentType); } - // @Ignore("DE176245") @Test public void createResourceGroup() throws Exception { // Resource doesn't have group @@ -491,7 +489,6 @@ public class GroupingTest extends CategoriesBaseTest { STATUS_CODE_INVALID_CONTENT, AUDIT_RESOURCE_TYPE); } - // @Ignore("DE176245") @Test public void createServiceGroup() throws Exception { // Service doesn't have group diff --git a/integration-tests/src/test/java/org/onap/sdc/backend/ci/tests/execute/category/SubCategoriesTest.java b/integration-tests/src/test/java/org/onap/sdc/backend/ci/tests/execute/category/SubCategoriesTest.java index 0d8c7b0a6b..0ee845ff13 100644 --- a/integration-tests/src/test/java/org/onap/sdc/backend/ci/tests/execute/category/SubCategoriesTest.java +++ b/integration-tests/src/test/java/org/onap/sdc/backend/ci/tests/execute/category/SubCategoriesTest.java @@ -460,7 +460,6 @@ public class SubCategoriesTest extends ComponentBaseTest { STATUS_CODE_RESTRICTED_OPERATION, AUDIT_PRODUCT_TYPE); } - // @Ignore("DE176245") @Test public void createResourceSubCategoryForNonExistingComponentType() throws Exception { String nonSupportedComponentType = "NonExistingComponentType"; // instead @@ -482,7 +481,6 @@ public class SubCategoriesTest extends ComponentBaseTest { STATUS_CODE_INVALID_CONTENT, nonSupportedComponentType); } - // @Ignore("DE176245") @Test public void createProductSubCategoryForNonExistingComponentType() throws Exception { String nonSupportedComponentType = "NonExistingComponentType"; // instead @@ -1122,13 +1120,6 @@ public class SubCategoriesTest extends ComponentBaseTest { resourceSubCategoryDefinition, sdncAdminUserDetails, STATUS_CODE_CREATED, AUDIT_RESOURCE_TYPE); } - // Bug - // Desc= - // DESC=SVC4556: Error: InvalidResourcesub-categorynameformat., - // @Ignore @Test public void subCategoryFirstWordStartWithNonAlphaNumeric() throws Exception { // The first word must start with an alpha-numeric character [a-Z A..Z, diff --git a/integration-tests/src/test/java/org/onap/sdc/backend/ci/tests/execute/resource/UpdateResourceMetadataTest.java b/integration-tests/src/test/java/org/onap/sdc/backend/ci/tests/execute/resource/UpdateResourceMetadataTest.java index 0bb2a26a2a..8bfd3df8ea 100644 --- a/integration-tests/src/test/java/org/onap/sdc/backend/ci/tests/execute/resource/UpdateResourceMetadataTest.java +++ b/integration-tests/src/test/java/org/onap/sdc/backend/ci/tests/execute/resource/UpdateResourceMetadataTest.java @@ -613,8 +613,6 @@ public class UpdateResourceMetadataTest extends ComponentBaseTest { } - // TODO DE - // @Ignore("") @Test public void UpdateWithInvaldJsonBodyTest() throws Exception { -- cgit 1.2.3-korg