diff options
Diffstat (limited to 'catalog-be/src/test/java/org/openecomp/sdc/be/externalapi/servlet/ExternalRefServletTest.java')
-rw-r--r-- | catalog-be/src/test/java/org/openecomp/sdc/be/externalapi/servlet/ExternalRefServletTest.java | 600 |
1 files changed, 357 insertions, 243 deletions
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 index f185bffc17..f7e59e65f1 100644 --- 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 @@ -7,9 +7,9 @@ * 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. @@ -20,35 +20,18 @@ 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.when; - -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.servlet.http.HttpSession; -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.hk2.utilities.binding.AbstractBinder; +import fj.data.Either; import org.glassfish.jersey.server.ResourceConfig; import org.glassfish.jersey.test.JerseyTest; -import org.glassfish.jersey.test.TestProperties; import org.junit.Before; import org.junit.BeforeClass; import org.junit.Test; import org.mockito.Mockito; +import org.openecomp.sdc.be.components.impl.ComponentLocker; import org.openecomp.sdc.be.components.impl.ExternalRefsBusinessLogic; +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.config.ConfigurationManager; import org.openecomp.sdc.be.dao.DAOTitanStrategy; import org.openecomp.sdc.be.dao.TitanClientStrategy; @@ -58,23 +41,21 @@ import org.openecomp.sdc.be.dao.jsongraph.TitanDao; import org.openecomp.sdc.be.dao.titan.TitanGenericDao; import org.openecomp.sdc.be.dao.titan.TitanGraphClient; import org.openecomp.sdc.be.dao.titan.TitanOperationStatus; +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.jsontitan.operations.CategoryOperation; -import org.openecomp.sdc.be.model.jsontitan.operations.ExternalReferencesOperation; -import org.openecomp.sdc.be.model.jsontitan.operations.GroupsOperation; -import org.openecomp.sdc.be.model.jsontitan.operations.NodeTemplateOperation; -import org.openecomp.sdc.be.model.jsontitan.operations.NodeTypeOperation; -import org.openecomp.sdc.be.model.jsontitan.operations.TopologyTemplateOperation; -import org.openecomp.sdc.be.model.jsontitan.operations.ToscaOperationFacade; +import org.openecomp.sdc.be.model.jsontitan.operations.*; import org.openecomp.sdc.be.model.jsontitan.utils.IdMapper; import org.openecomp.sdc.be.model.operations.api.ICacheMangerOperation; -import org.openecomp.sdc.be.model.operations.api.StorageOperationStatus; -import org.openecomp.sdc.be.model.operations.impl.GraphLockOperation; +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; @@ -89,15 +70,30 @@ import org.springframework.context.annotation.Configuration; import org.springframework.http.HttpStatus; import org.springframework.web.context.WebApplicationContext; -import fj.data.Either; +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 java.util.HashMap; +import java.util.LinkedList; +import java.util.List; +import java.util.Map; + +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; public class ExternalRefServletTest extends JerseyTest { - private static ConfigurationManager configurationManager; - static String serviceVertexUuid; - private static final HttpSession session = Mockito.mock(HttpSession.class); + private static boolean setupDone = false; + private static String serviceVertexUuid; + private static String resourceVertexUuid; private static final ServletContext servletContext = Mockito.mock(ServletContext.class); - public 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); @@ -107,9 +103,12 @@ public class ExternalRefServletTest extends JerseyTest { private static final ResponseFormat notFoundResponseFormat = Mockito.mock(ResponseFormat.class); private static final ResponseFormat badRequestResponseFormat = Mockito.mock(ResponseFormat.class); private static final ToscaOperationFacade toscaOperationFacadeMock = Mockito.mock(ToscaOperationFacade.class); - private static final GraphLockOperation graphLockOperation = Mockito.mock(GraphLockOperation.class); + private static final AccessValidations accessValidationsMock = Mockito.mock(AccessValidations.class); + private static final ComponentLocker componentLocker = Mockito.mock(ComponentLocker.class); private static final TitanGenericDao titanGenericDao = Mockito.mock(TitanGenericDao.class); private static final ICacheMangerOperation cacheManagerOperation = Mockito.mock(ICacheMangerOperation.class); + private static final IGraphLockOperation graphLockOperation = Mockito.mock(IGraphLockOperation.class); + private static final String COMPONENT_ID = "ci-MyComponentName"; @@ -130,17 +129,42 @@ public class ExternalRefServletTest extends JerseyTest { static class TestSpringConfig { private GraphVertex serviceVertex; + private GraphVertex resourceVertex; private ExternalReferencesOperation externalReferenceOperation; private TitanDao titanDao; + private OperationUtils operationUtils; @Bean - ExternalRefsServlet externalRefsServlet(){ + ExternalRefsServlet externalRefsServlet() { return new ExternalRefsServlet(externalRefsBusinessLogic(), componentUtils); } @Bean + OperationUtils operationUtils() {return new OperationUtils(titanDao());} + + @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(), graphLockOperation()); + return new ExternalRefsBusinessLogic(externalReferencesOperation(), toscaOperationFacade(), accessValidations(), componentLocker()); + } + + @Bean + AccessValidations accessValidations() { + return accessValidationsMock; } @Bean @@ -152,15 +176,7 @@ public class ExternalRefServletTest extends JerseyTest { } @Bean - ToscaOperationFacade toscaOperationFacade(){ - Component componentMock = Mockito.mock(Component.class); - when(componentMock.getVersion()).thenReturn(VERSION); - when(componentMock.getUniqueId()).thenReturn(serviceVertexUuid); - List<Component> listComponents = new LinkedList<Component>(); - listComponents.add(componentMock); - when(toscaOperationFacadeMock.getComponentListByUuid(eq(serviceVertexUuid), any())).thenReturn(Either.left(listComponents)); - when(toscaOperationFacadeMock.getComponentByUuidAndVersion(eq(serviceVertexUuid), eq(VERSION))).thenReturn(Either.left(componentMock)); - when(toscaOperationFacadeMock.getLatestComponentByUuid(eq(serviceVertexUuid), any())).thenReturn(Either.left(listComponents.get(0))); + ToscaOperationFacade toscaOperationFacade() { return toscaOperationFacadeMock; } @@ -179,22 +195,34 @@ public class ExternalRefServletTest extends JerseyTest { } @Bean + ArchiveOperation archiveOperation() { + return new ArchiveOperation(titanDao(), graphLockOperation()); + } + + @Bean + IGraphLockOperation graphLockOperation() { + return graphLockOperation; + } + + @Bean NodeTypeOperation nodeTypeOpertaion() { return new NodeTypeOperation(null); } @Bean - NodeTemplateOperation nodeTemplateOperation(){ + NodeTemplateOperation nodeTemplateOperation() { return new NodeTemplateOperation(); } @Bean - GroupsOperation groupsOperation(){ + GroupsOperation groupsOperation() { return new GroupsOperation(); } @Bean - ICacheMangerOperation cacheMangerOperation() { return cacheManagerOperation; } + ICacheMangerOperation cacheMangerOperation() { + return cacheManagerOperation; + } @Bean TitanDao titanDao() { @@ -213,32 +241,42 @@ public class ExternalRefServletTest extends JerseyTest { } @Bean - CategoryOperation categoryOperation(){ + CategoryOperation categoryOperation() { return new CategoryOperation(); } @Bean - GraphLockOperation graphLockOperation() { return graphLockOperation; } + ComponentLocker componentLocker() { + return componentLocker; + } @Bean - TitanGenericDao titanGenericDao() { return titanGenericDao; } + TitanGenericDao titanGenericDao() { + return titanGenericDao; + } private void initGraphForTest() { - //create a service and add 1 ref - serviceVertex = GraphTestUtils.createServiceVertex(titanDao, new HashMap<>()); - serviceVertexUuid = this.serviceVertex.getUniqueId(); + if (!setupDone) { + + resourceVertex = GraphTestUtils.createResourceVertex(titanDao, new HashMap<>(), ResourceTypeEnum.VF); + resourceVertexUuid = resourceVertex.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); + //create a service and add ref + serviceVertex = GraphTestUtils.createServiceVertex(titanDao, new HashMap<>()); + serviceVertexUuid = this.serviceVertex.getUniqueId(); - //workflow references - externalReferenceOperation.addExternalReference(serviceVertexUuid, COMPONENT_ID, WORKFLOW_OBJECT_TYPE, REF_6); + //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); - final TitanOperationStatus commit = this.titanDao.commit(); - assertThat(commit).isEqualTo(TitanOperationStatus.OK); + //workflow references + externalReferenceOperation.addExternalReference(serviceVertexUuid, COMPONENT_ID, WORKFLOW_OBJECT_TYPE, REF_6); + + final TitanOperationStatus commit = this.titanDao.commit(); + assertThat(commit).isEqualTo(TitanOperationStatus.OK); + } } @@ -248,7 +286,10 @@ public class ExternalRefServletTest extends JerseyTest { /* Users */ private static User adminUser = new User("admin", "admin", "admin", "admin@email.com", Role.ADMIN.name(), System.currentTimeMillis()); - private static User designerUser = new User("designer", "designer", "designer", "designer@email.com", Role.DESIGNER.name(), System.currentTimeMillis()); + private static User designerUser = new User("designer", "designer", "designer", "designer@email.com", Role.DESIGNER.name(), System + .currentTimeMillis()); + private static User otherDesignerUser = new User("otherDesigner", "otherDesigner", "otherDesigner", "otherDesigner@email.com", Role.DESIGNER + .name(), System.currentTimeMillis()); private static User otherUser = new User("other", "other", "other", "other@email.com", Role.OPS.name(), System.currentTimeMillis()); @@ -265,6 +306,15 @@ public class ExternalRefServletTest extends JerseyTest { when(componentUtils.getResponseFormat(ActionStatus.RESTRICTED_OPERATION)).thenReturn(responseFormat); when(responseFormat.getStatus()).thenReturn(HttpStatus.UNAUTHORIZED.value()); + ComponentException ce = Mockito.mock(ComponentException.class); + String[] params = {otherDesignerUser.getUserId()}; + when(ce.getParams()).thenReturn(params); + when(ce.getResponseFormat()).thenReturn(responseFormat); + doThrow(ce).when(accessValidationsMock) + .validateUserCanWorkOnComponent(any(), eq(otherDesignerUser.getUserId()), any()); + doThrow(ce).when(accessValidationsMock) + .validateUserCanWorkOnComponent(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()); @@ -274,7 +324,6 @@ public class ExternalRefServletTest extends JerseyTest { 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(graphLockOperation.lockComponent(any(), any())).thenReturn(StorageOperationStatus.OK); Either<User, ActionStatus> adminEither = Either.left(adminUser); Either<User, ActionStatus> designerEither = Either.left(designerUser); @@ -284,39 +333,53 @@ public class ExternalRefServletTest extends JerseyTest { when(userAdmin.getUser(designerUser.getUserId(), false)).thenReturn(designerEither); when(userAdmin.getUser(otherUser.getUserId(), false)).thenReturn(otherEither); //======================================================================================================================== - - //TODO DELETE after successfull merge - //if (ConfigurationManager.getConfigurationManager() == null) { - String appConfigDir = "src/test/resources/config"; - ConfigurationSource configurationSource = new FSConfigurationSource(ExternalConfiguration.getChangeListener(), appConfigDir); - configurationManager = new ConfigurationManager(configurationSource); - - org.openecomp.sdc.be.config.Configuration configuration = new org.openecomp.sdc.be.config.Configuration(); - configuration.setTitanInMemoryGraph(true); - - configurationManager.setConfiguration(configuration); - //} + + String appConfigDir = "src/test/resources/config"; + 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.setTitanInMemoryGraph(true); + + configurationManager.setConfiguration(configuration); ExternalConfiguration.setAppName("catalog-be"); } @Before - public void beforeTest() { + public void before(){ + + Component resourceComponentMock = Mockito.mock(Component.class); + when(resourceComponentMock.getVersion()).thenReturn(VERSION); + when(resourceComponentMock.getUniqueId()).thenReturn(resourceVertexUuid); + + Component serviceComponentMock = Mockito.mock(Component.class); + when(serviceComponentMock.getVersion()).thenReturn(VERSION); + when(serviceComponentMock.getUniqueId()).thenReturn(serviceVertexUuid); + + List<Component> listComponents = new LinkedList<>(); + listComponents.add(serviceComponentMock); + when(toscaOperationFacadeMock.getComponentListByUuid(eq(serviceVertexUuid), any())).thenReturn(Either.left(listComponents)); + when(toscaOperationFacadeMock.getComponentByUuidAndVersion(eq(serviceVertexUuid), eq(VERSION))).thenReturn(Either.left(serviceComponentMock)); + when(toscaOperationFacadeMock.getComponentByUuidAndVersion(eq(resourceVertexUuid), eq(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)); } @Test public 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(); + 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<String> dto = response.readEntity(new GenericType<List<String>>() { }); + List<String> dto = response.readEntity(new GenericType<List<String>>() { + }); assertThat(dto).containsExactly(REF_1, REF_2, REF_3, REF_5); assertThat(response.getStatus()).isEqualTo(HttpStatus.OK.value()); } @@ -325,58 +388,105 @@ public class ExternalRefServletTest extends JerseyTest { public 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(); + 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<String, List<String>> dtoMap = response.readEntity(new GenericType<HashMap<String, List<String>>>() { }); + Map<String, List<String>> dtoMap = response.readEntity(new GenericType<HashMap<String, List<String>>>() { + }); assertThat(dtoMap.get(COMPONENT_ID)).containsExactly(REF_1, REF_2, REF_3, REF_5); assertThat(response.getStatus()).isEqualTo(HttpStatus.OK.value()); } @Test - public void testAddExternalRefForExistingComponentInstance(){ + public 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 + public 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<String, List<String>> dto = response.readEntity(new GenericType<Map<String, List<String>>>(){}); + assertThat(dto.get(COMPONENT_ID)).containsExactlyInAnyOrder(REF_1); + assertThat(response.getStatus()).isEqualTo(HttpStatus.OK.value()); + } + + @Test + public 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))); + 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<String> dto = response.readEntity(new GenericType<List<String>>() { }); + 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<String> dto = response.readEntity(new GenericType<List<String>>() { + }); assertThat(dto).containsExactlyInAnyOrder(REF_1, REF_2, REF_3, REF_4, REF_5); assertThat(response.getStatus()).isEqualTo(HttpStatus.OK.value()); } @Test - public void testDeleteExternalRefForExistingComponentInstance(){ + public 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(); + 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); @@ -384,30 +494,31 @@ public class ExternalRefServletTest extends JerseyTest { 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<String> getResponse = response.readEntity(new GenericType<List<String>>() { }); + 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<String> getResponse = response.readEntity(new GenericType<List<String>>() { + }); assertThat(getResponse).containsExactlyInAnyOrder(REF_1, REF_2, REF_3); assertThat(response.getStatus()).isEqualTo(HttpStatus.OK.value()); } @Test - public void testUpdateExternalRefForExistingComponentInstance(){ + public 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))); + 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); @@ -415,77 +526,78 @@ public class ExternalRefServletTest extends JerseyTest { 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(); + 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<String> dto = response.readEntity(new GenericType<List<String>>() { }); + List<String> dto = response.readEntity(new GenericType<List<String>>() { + }); assertThat(dto).containsExactlyInAnyOrder(REF_1, REF_2, REF_3, REF_4); assertThat(response.getStatus()).isEqualTo(HttpStatus.OK.value()); } /* - * Rainy Scenarios - */ + * Rainy Scenarios + */ @Test - public void testAddExternalRefForNonExistingAssetId(){ + public 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))); + 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 - public void testAddExternalRefForNonExistingCompInstId(){ + public 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))); + 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 - public void testAddExistingExternalRef(){ + public 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))); + 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 - public void testUpdateExternalRefForNonExistingAssetId(){ + public 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))); + 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()); @@ -493,61 +605,58 @@ public class ExternalRefServletTest extends JerseyTest { } @Test - public void testUpdateExternalRefForNonExistingObjectIdOrOldRef(){ + public 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))); + 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()); - System.out.println(response.readEntity(String.class)); } @Test - public void testDeleteExternalRefForNonExistingAssetId(){ + public 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(); + 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 - public void testDeleteExternalRefForNonExistingRef(){ + public 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"). - header(Constants.USER_ID_HEADER, designerUser.getUserId()). - delete(); + 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()); - System.out.println(response.readEntity(String.class)); } @Test public 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(); + 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()); } @@ -556,28 +665,42 @@ public class ExternalRefServletTest extends JerseyTest { public 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(); + 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 - public void testGetExternalRefsForExistingComponentInstanceWithUnauthorizedUser() { - String path = String.format("/v1/catalog/services/%s/version/%s/resourceInstances/%s/externalReferences/%s", serviceVertexUuid, VERSION, COMPONENT_ID, MONITORING_OBJECT_TYPE); + public 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()). - get(); + 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 + public 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()); } @@ -585,19 +708,10 @@ public class ExternalRefServletTest extends JerseyTest { @Override protected Application configure() { ApplicationContext context = new AnnotationConfigApplicationContext(TestSpringConfig.class); - forceSet(TestProperties.CONTAINER_PORT, "0"); return new ResourceConfig(ExternalRefsServlet.class) - .register(new AbstractBinder() { - - @Override - protected void configure() { - bind(request).to(HttpServletRequest.class); - when(request.getSession()).thenReturn(session); - when(request.getHeader(eq(Constants.X_ECOMP_INSTANCE_ID_HEADER))).thenReturn("mockXEcompInstIdHeader"); - when(session.getServletContext()).thenReturn(servletContext); - } - }) + .register(DefaultExceptionMapper.class) + .register(ComponentExceptionMapper.class) + .register(StorageExceptionMapper.class) .property("contextConfig", context); } - } |