diff options
Diffstat (limited to 'catalog-be/src')
4 files changed, 515 insertions, 25 deletions
diff --git a/catalog-be/src/main/java/org/openecomp/sdc/be/components/impl/BaseBusinessLogic.java b/catalog-be/src/main/java/org/openecomp/sdc/be/components/impl/BaseBusinessLogic.java index 544f030d91..85453e33d2 100644 --- a/catalog-be/src/main/java/org/openecomp/sdc/be/components/impl/BaseBusinessLogic.java +++ b/catalog-be/src/main/java/org/openecomp/sdc/be/components/impl/BaseBusinessLogic.java @@ -120,7 +120,7 @@ public abstract class BaseBusinessLogic { protected IGroupInstanceOperation groupInstanceOperation; protected InterfaceLifecycleOperation interfaceLifecycleTypeOperation; protected PolicyTypeOperation policyTypeOperation; - protected ArtifactsOperations artifactToscaOperation; + protected ArtifactsOperations artifactToscaOperation; protected UserValidations userValidations; DataTypeValidatorConverter dataTypeValidatorConverter = DataTypeValidatorConverter.getInstance(); diff --git a/catalog-be/src/main/java/org/openecomp/sdc/be/components/validation/NodeFilterValidator.java b/catalog-be/src/main/java/org/openecomp/sdc/be/components/validation/NodeFilterValidator.java index 3c770d15aa..27daaf2aae 100644 --- a/catalog-be/src/main/java/org/openecomp/sdc/be/components/validation/NodeFilterValidator.java +++ b/catalog-be/src/main/java/org/openecomp/sdc/be/components/validation/NodeFilterValidator.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. @@ -96,22 +96,21 @@ public class NodeFilterValidator { final List<String> uiConstraints, final NodeFilterConstraintAction action) { try { - for (final String uiConstraint : uiConstraints) { - if (NodeFilterConstraintAction.ADD != action && NodeFilterConstraintAction.UPDATE != action) { - break; - } - final UIConstraint constraint = new ConstraintConvertor().convert(uiConstraint); - if (ConstraintConvertor.PROPERTY_CONSTRAINT.equals(constraint.getSourceType())) { - final Either<Boolean, ResponseFormat> booleanResponseFormatEither = + if (NodeFilterConstraintAction.ADD == action || NodeFilterConstraintAction.UPDATE == action) { + for (final String uiConstraint : uiConstraints) { + final UIConstraint constraint = new ConstraintConvertor().convert(uiConstraint); + if (ConstraintConvertor.PROPERTY_CONSTRAINT.equals(constraint.getSourceType())) { + final Either<Boolean, ResponseFormat> booleanResponseFormatEither = validatePropertyConstraint(parentComponent, componentInstanceId, constraint); - if (booleanResponseFormatEither.isRight()) { - return booleanResponseFormatEither; - } - } else if (ConstraintConvertor.STATIC_CONSTRAINT.equals(constraint.getSourceType())) { - final Either<Boolean, ResponseFormat> booleanResponseFormatEither = + if (booleanResponseFormatEither.isRight()) { + return booleanResponseFormatEither; + } + } else if (ConstraintConvertor.STATIC_CONSTRAINT.equals(constraint.getSourceType())) { + final Either<Boolean, ResponseFormat> booleanResponseFormatEither = validateStaticValueAndOperator(parentComponent, componentInstanceId, constraint); - if (booleanResponseFormatEither.isRight()) { - return booleanResponseFormatEither; + if (booleanResponseFormatEither.isRight()) { + return booleanResponseFormatEither; + } } } } diff --git a/catalog-be/src/main/java/org/openecomp/sdc/be/servlets/ComponentNodeFilterServlet.java b/catalog-be/src/main/java/org/openecomp/sdc/be/servlets/ComponentNodeFilterServlet.java index 4a43ab23a6..7141379fe3 100644 --- a/catalog-be/src/main/java/org/openecomp/sdc/be/servlets/ComponentNodeFilterServlet.java +++ b/catalog-be/src/main/java/org/openecomp/sdc/be/servlets/ComponentNodeFilterServlet.java @@ -145,9 +145,9 @@ public class ComponentNodeFilterServlet extends AbstractValidationsServlet { LOGGER.debug(FAILED_TO_PARSE_COMPONENT); return buildErrorResponse(convertResponse.right().value()); } - final Optional<CINodeFilterDataDefinition> actionResponse; final String constraint = new ConstraintConvertor().convert(uiConstraint); - actionResponse = componentNodeFilterBusinessLogic + + final Optional<CINodeFilterDataDefinition> actionResponse = componentNodeFilterBusinessLogic .addNodeFilter(componentId.toLowerCase(), componentInstanceId, NodeFilterConstraintAction.ADD, uiConstraint.getServicePropertyName(), constraint, true, componentTypeEnum); @@ -163,7 +163,6 @@ public class ComponentNodeFilterServlet extends AbstractValidationsServlet { BeEcompErrorManager.getInstance().logBeRestApiGeneralError(NODE_FILTER_CREATION); LOGGER.error(CREATE_NODE_FILTER_WITH_AN_ERROR, e); return buildErrorResponse(getComponentsUtils().getResponseFormat(ActionStatus.GENERAL_ERROR)); - } } @@ -214,10 +213,8 @@ public class ComponentNodeFilterServlet extends AbstractValidationsServlet { LOGGER.debug("failed to parse data"); return buildErrorResponse(convertResponse.right().value()); } - final Optional<CINodeFilterDataDefinition> actionResponse; final List<String> constraints = new ConstraintConvertor().convertToList(uiConstraints); - - actionResponse = componentNodeFilterBusinessLogic + final Optional<CINodeFilterDataDefinition> actionResponse = componentNodeFilterBusinessLogic .updateNodeFilter(componentId.toLowerCase(), componentInstanceId, constraints, true, componentTypeEnum); @@ -262,8 +259,7 @@ public class ComponentNodeFilterServlet extends AbstractValidationsServlet { componentNodeFilterBusinessLogic.validateUser(userId); try { - final Optional<CINodeFilterDataDefinition> actionResponse; - actionResponse = componentNodeFilterBusinessLogic + final Optional<CINodeFilterDataDefinition>actionResponse = componentNodeFilterBusinessLogic .deleteNodeFilter(componentId.toLowerCase(), componentInstanceId, NodeFilterConstraintAction.DELETE, null, index, true, ComponentTypeEnum.findByParamName(componentType)); diff --git a/catalog-be/src/test/java/org/openecomp/sdc/be/servlets/ComponentNodeFilterServletTest.java b/catalog-be/src/test/java/org/openecomp/sdc/be/servlets/ComponentNodeFilterServletTest.java new file mode 100644 index 0000000000..dcd8a3b9c5 --- /dev/null +++ b/catalog-be/src/test/java/org/openecomp/sdc/be/servlets/ComponentNodeFilterServletTest.java @@ -0,0 +1,495 @@ +/* + * ============LICENSE_START======================================================= + * Copyright (C) 2020 Nordix Foundation + * ================================================================================ + * 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. + * + * SPDX-License-Identifier: Apache-2.0 + * ============LICENSE_END========================================================= + */ + +package org.openecomp.sdc.be.servlets; + +import static org.assertj.core.api.Assertions.assertThat; +import static org.junit.Assert.assertNotNull; +import static org.mockito.ArgumentMatchers.any; +import static org.mockito.ArgumentMatchers.anyBoolean; +import static org.mockito.ArgumentMatchers.anyInt; +import static org.mockito.ArgumentMatchers.anyList; +import static org.mockito.ArgumentMatchers.anyString; +import static org.mockito.ArgumentMatchers.nullable; +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.reset; +import static org.mockito.Mockito.times; +import static org.mockito.Mockito.verify; +import static org.mockito.Mockito.when; +import static org.openecomp.sdc.common.api.Constants.USER_ID_HEADER; + +import com.fasterxml.jackson.core.JsonProcessingException; +import com.fasterxml.jackson.databind.ObjectMapper; +import fj.data.Either; +import java.util.Arrays; +import java.util.Collections; +import java.util.LinkedList; +import java.util.List; +import java.util.Map; +import java.util.Optional; +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.MediaType; +import javax.ws.rs.core.Response; +import org.eclipse.jetty.http.HttpStatus; +import org.glassfish.hk2.utilities.binding.AbstractBinder; +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.mockito.ArgumentMatchers; +import org.openecomp.sdc.be.components.impl.ComponentInstanceBusinessLogic; +import org.openecomp.sdc.be.components.impl.ComponentNodeFilterBusinessLogic; +import org.openecomp.sdc.be.components.impl.ResourceImportManager; +import org.openecomp.sdc.be.components.impl.exceptions.BusinessLogicException; +import org.openecomp.sdc.be.components.impl.utils.NodeFilterConstraintAction; +import org.openecomp.sdc.be.components.validation.UserValidations; +import org.openecomp.sdc.be.config.SpringConfig; +import org.openecomp.sdc.be.dao.api.ActionStatus; +import org.openecomp.sdc.be.datamodel.utils.ConstraintConvertor; +import org.openecomp.sdc.be.datatypes.elements.CINodeFilterDataDefinition; +import org.openecomp.sdc.be.datatypes.elements.ListDataDefinition; +import org.openecomp.sdc.be.datatypes.elements.RequirementNodeFilterPropertyDataDefinition; +import org.openecomp.sdc.be.datatypes.enums.ComponentTypeEnum; +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.User; +import org.openecomp.sdc.be.resources.data.auditing.AuditingActionEnum; +import org.openecomp.sdc.be.ui.model.UIConstraint; +import org.openecomp.sdc.be.user.Role; +import org.openecomp.sdc.be.user.UserBusinessLogic; +import org.openecomp.sdc.common.api.Constants; +import org.openecomp.sdc.exception.ResponseFormat; +import org.springframework.context.ApplicationContext; +import org.springframework.context.annotation.AnnotationConfigApplicationContext; +import org.springframework.web.context.WebApplicationContext; + +public class ComponentNodeFilterServletTest extends JerseyTest { + private static final String USER_ID = "jh0003"; + private static final String servicePropertyName = "resourceType"; + private static final String constraintOperator = "equal"; + private static final String sourceType = "static"; + private static final String sourceName = sourceType; + private static final String propertyValue = "resourceTypeValue"; + private static final String componentId = "dac65869-dfb4-40d2-aa20-084324659ec1"; + private static final String componentInstance = "dac65869-dfb4-40d2-aa20-084324659ec1.resource0"; + private static final String componentType = "resources"; + + private static HttpServletRequest request; + private static HttpSession session; + private static ServletContext servletContext; + private static WebAppContextWrapper webAppContextWrapper; + private static WebApplicationContext webApplicationContext; + private static UserBusinessLogic userBusinessLogic; + private static ComponentInstanceBusinessLogic componentInstanceBusinessLogic; + private static ComponentsUtils componentsUtils; + private static ServletUtils servletUtils; + private static ResourceImportManager resourceImportManager; + private static ComponentNodeFilterBusinessLogic componentNodeFilterBusinessLogic; + private static ResponseFormat responseFormat; + private static UserValidations userValidations; + + private CINodeFilterDataDefinition ciNodeFilterDataDefinition; + private RequirementNodeFilterPropertyDataDefinition requirementNodeFilterPropertyDataDefinition; + private UIConstraint uiConstraint; + private String constraint; + private String inputJson; + private User user; + + @BeforeAll + public static void initClass() { + createMocks(); + when(request.getSession()).thenReturn(session); + when(session.getServletContext()).thenReturn(servletContext); + when(servletContext.getAttribute(Constants.WEB_APPLICATION_CONTEXT_WRAPPER_ATTR)).thenReturn(webAppContextWrapper); + when(webAppContextWrapper.getWebAppContext(servletContext)).thenReturn(webApplicationContext); + when(webApplicationContext.getBean(ComponentNodeFilterBusinessLogic.class)).thenReturn(componentNodeFilterBusinessLogic); + when(request.getHeader("USER_ID")).thenReturn(USER_ID); + when(webApplicationContext.getBean(ServletUtils.class)).thenReturn(servletUtils); + when(servletUtils.getComponentsUtils()).thenReturn(componentsUtils); + } + + @BeforeEach + public void resetMock() throws Exception { + super.setUp(); + reset(componentNodeFilterBusinessLogic); + } + + @AfterEach + void after() throws Exception { + super.tearDown(); + } + + @Test + public void addNodeFilterSuccessTest() throws BusinessLogicException, JsonProcessingException { + initComponentData(); + final String pathFormat = "/v1/catalog/%s/%s/resourceInstances/%s/nodeFilter"; + final String path = String.format(pathFormat, componentType, componentId, componentInstance); + + when(userValidations.validateUserExists(user)).thenReturn(user); + when(componentNodeFilterBusinessLogic.validateUser(USER_ID)).thenReturn(user); + when(responseFormat.getStatus()).thenReturn(HttpStatus.OK_200); + when(componentsUtils.getResponseFormat(ActionStatus.OK)).thenReturn(responseFormat); + + assertNotNull(uiConstraint); + assertThat(servicePropertyName).isEqualToIgnoringCase(uiConstraint.getServicePropertyName()); + assertThat(constraintOperator).isEqualToIgnoringCase(uiConstraint.getConstraintOperator()); + assertThat(sourceType).isEqualToIgnoringCase(uiConstraint.getSourceType()); + assertThat(sourceName).isEqualToIgnoringCase(uiConstraint.getSourceName()); + assertThat(propertyValue).isEqualToIgnoringCase(uiConstraint.getValue().toString()); + when(componentsUtils.convertJsonToObjectUsingObjectMapper(anyString(), any(User.class), + ArgumentMatchers.<Class<UIConstraint>>any(), + nullable(AuditingActionEnum.class), nullable(ComponentTypeEnum.class))).thenReturn(Either.left(uiConstraint)); + + assertNotNull(constraint); + assertNotNull(ciNodeFilterDataDefinition); + assertThat(ciNodeFilterDataDefinition.getProperties().getListToscaDataDefinition()).hasSize(1); + assertThat("resourceType: {equal: resourceTypeValue}\n").isEqualToIgnoringCase(constraint); + when(componentNodeFilterBusinessLogic + .addNodeFilter(componentId, componentInstance, NodeFilterConstraintAction.ADD, + uiConstraint.getServicePropertyName(), constraint, true, ComponentTypeEnum.RESOURCE)) + .thenReturn(Optional.of(ciNodeFilterDataDefinition)); + + final Response response = target() + .path(path) + .request(MediaType.APPLICATION_JSON) + .header(USER_ID_HEADER, USER_ID) + .post(Entity.entity(inputJson, MediaType.APPLICATION_JSON)); + + verify(componentNodeFilterBusinessLogic, times(1)) + .addNodeFilter(anyString(), anyString(), ArgumentMatchers.any(NodeFilterConstraintAction.class), anyString(), anyString(), anyBoolean(), + ArgumentMatchers.any(ComponentTypeEnum.class)); + + assertThat(response.getStatus()).isEqualTo(HttpStatus.OK_200); + } + + @Test + public void addNodeFilterFailTest() throws BusinessLogicException, JsonProcessingException { + initComponentData(); + final String pathFormat = "/v1/catalog/%s/%s/resourceInstances/%s/nodeFilter"; + final String path = String.format(pathFormat, componentType, componentId, componentInstance); + + when(userValidations.validateUserExists(user)).thenReturn(user); + when(componentNodeFilterBusinessLogic.validateUser(USER_ID)).thenReturn(user); + when(responseFormat.getStatus()).thenReturn(HttpStatus.INTERNAL_SERVER_ERROR_500); + when(componentsUtils.getResponseFormat(ActionStatus.OK)).thenReturn(responseFormat); + + when(componentsUtils.convertJsonToObjectUsingObjectMapper(anyString(), any(User.class), + ArgumentMatchers.<Class<UIConstraint>>any(), + nullable(AuditingActionEnum.class), nullable(ComponentTypeEnum.class))).thenReturn(Either.left(uiConstraint)); + + when(componentNodeFilterBusinessLogic + .addNodeFilter(componentId, componentInstance, NodeFilterConstraintAction.ADD, + uiConstraint.getServicePropertyName(), constraint, true, ComponentTypeEnum.RESOURCE)) + .thenReturn(Optional.empty()); + + final Response response = target() + .path(path) + .request(MediaType.APPLICATION_JSON) + .header(USER_ID_HEADER, USER_ID) + .post(Entity.entity(inputJson, MediaType.APPLICATION_JSON)); + + verify(componentNodeFilterBusinessLogic, times(1)) + .addNodeFilter(anyString(), anyString(), ArgumentMatchers.any(NodeFilterConstraintAction.class), anyString(), anyString(), anyBoolean(), + ArgumentMatchers.any(ComponentTypeEnum.class)); + + assertThat(response.getStatus()).isEqualTo(HttpStatus.INTERNAL_SERVER_ERROR_500); + } + + @Test + public void addNodeFilterFailConstraintParseTest() throws JsonProcessingException { + initComponentData(); + final String pathFormat = "/v1/catalog/%s/%s/resourceInstances/%s/nodeFilter"; + final String path = String.format(pathFormat, componentType, componentId, componentInstance); + + when(userValidations.validateUserExists(user)).thenReturn(user); + when(componentNodeFilterBusinessLogic.validateUser(USER_ID)).thenReturn(user); + + when(responseFormat.getStatus()).thenReturn(HttpStatus.INTERNAL_SERVER_ERROR_500); + when(componentsUtils.getResponseFormat(ActionStatus.OK)).thenReturn(responseFormat); + + when(componentsUtils.convertJsonToObjectUsingObjectMapper(anyString(), any(User.class), + ArgumentMatchers.<Class<UIConstraint>>any(), + nullable(AuditingActionEnum.class), nullable(ComponentTypeEnum.class))) + .thenReturn(Either.right(new ResponseFormat(HttpStatus.INTERNAL_SERVER_ERROR_500))); + + final Response response = target() + .path(path) + .request(MediaType.APPLICATION_JSON) + .header(USER_ID_HEADER, USER_ID) + .post(Entity.entity(inputJson, MediaType.APPLICATION_JSON)); + + assertThat(response.getStatus()).isEqualTo(HttpStatus.INTERNAL_SERVER_ERROR_500); + } + + @Test + public void addNodeFilterFailConvertTest() throws JsonProcessingException, BusinessLogicException { + initComponentData(); + final String pathFormat = "/v1/catalog/%s/%s/resourceInstances/%s/nodeFilter"; + final String path = String.format(pathFormat, componentType, componentId, componentInstance); + + when(userValidations.validateUserExists(user)).thenReturn(user); + when(componentNodeFilterBusinessLogic.validateUser(USER_ID)).thenReturn(user); + + when(responseFormat.getStatus()).thenReturn(HttpStatus.INTERNAL_SERVER_ERROR_500); + when(componentsUtils.getResponseFormat(ActionStatus.OK)).thenReturn(responseFormat); + + when(componentsUtils.convertJsonToObjectUsingObjectMapper(anyString(), any(User.class), + ArgumentMatchers.<Class<UIConstraint>>any(), + nullable(AuditingActionEnum.class), nullable(ComponentTypeEnum.class))) + .thenReturn(Either.left(null)); + + final Response response = target() + .path(path) + .request(MediaType.APPLICATION_JSON) + .header(USER_ID_HEADER, USER_ID) + .post(Entity.entity(inputJson, MediaType.APPLICATION_JSON)); + + assertThat(response.getStatus()).isEqualTo(HttpStatus.INTERNAL_SERVER_ERROR_500); + } + + @Test + public void updateNodeFilterSuccessTest() throws BusinessLogicException, JsonProcessingException { + initComponentData(); + final String pathFormat = "/v1/catalog/%s/%s/resourceInstances/%s/nodeFilter"; + final String path = String.format(pathFormat, componentType, componentId, componentInstance); + + when(userValidations.validateUserExists(user)).thenReturn(user); + when(componentNodeFilterBusinessLogic.validateUser(USER_ID)).thenReturn(user); + + when(responseFormat.getStatus()).thenReturn(HttpStatus.OK_200); + when(componentsUtils.getResponseFormat(ActionStatus.OK)).thenReturn(responseFormat); + + when(componentsUtils.convertJsonToObjectUsingObjectMapper(anyString(), any(User.class), + ArgumentMatchers.<Class<List>>any(), + nullable(AuditingActionEnum.class), nullable(ComponentTypeEnum.class))) + .thenReturn(Either.left(Arrays.asList(new ObjectMapper().convertValue(uiConstraint, Map.class)))); + + when(componentNodeFilterBusinessLogic + .updateNodeFilter(componentId, componentInstance, Collections.singletonList(constraint), + true, ComponentTypeEnum.RESOURCE)).thenReturn(Optional.of(ciNodeFilterDataDefinition)); + final Response response = target() + .path(path) + .request(MediaType.APPLICATION_JSON) + .header(USER_ID_HEADER, USER_ID) + .put(Entity.entity(inputJson, MediaType.APPLICATION_JSON)); + + verify(componentNodeFilterBusinessLogic, times(1)) + .updateNodeFilter(anyString(), anyString(), anyList(), anyBoolean(), + ArgumentMatchers.any(ComponentTypeEnum.class)); + + assertThat(response.getStatus()).isEqualTo(HttpStatus.OK_200); + } + + @Test + public void updateNodeFilterFailTest() throws BusinessLogicException, JsonProcessingException { + initComponentData(); + final String pathFormat = "/v1/catalog/%s/%s/resourceInstances/%s/nodeFilter"; + final String path = String.format(pathFormat, componentType, componentId, componentInstance); + + when(userValidations.validateUserExists(user)).thenReturn(user); + when(componentNodeFilterBusinessLogic.validateUser(USER_ID)).thenReturn(user); + + when(componentsUtils.getResponseFormat(ActionStatus.OK)).thenReturn(responseFormat); + + when(componentsUtils.convertJsonToObjectUsingObjectMapper(anyString(), any(User.class), + ArgumentMatchers.<Class<List>>any(), + nullable(AuditingActionEnum.class), nullable(ComponentTypeEnum.class))) + .thenReturn(Either.left(Arrays.asList(new ObjectMapper().convertValue(uiConstraint, Map.class)))); + + when(componentNodeFilterBusinessLogic + .updateNodeFilter(componentId, componentInstance, Collections.singletonList(constraint), + true, ComponentTypeEnum.RESOURCE)) + .thenReturn(Optional.empty()); + final Response response = target() + .path(path) + .request(MediaType.APPLICATION_JSON) + .header(USER_ID_HEADER, USER_ID) + .put(Entity.entity(inputJson, MediaType.APPLICATION_JSON)); + + verify(componentNodeFilterBusinessLogic, times(1)) + .updateNodeFilter(anyString(), anyString(), anyList(), anyBoolean(), + ArgumentMatchers.any(ComponentTypeEnum.class)); + + + assertThat(response.getStatus()).isEqualTo(HttpStatus.INTERNAL_SERVER_ERROR_500); + } + + @Test + public void updateNodeFilterFailConstraintParseTest() throws JsonProcessingException { + initComponentData(); + final String pathFormat = "/v1/catalog/%s/%s/resourceInstances/%s/nodeFilter"; + final String path = String.format(pathFormat, componentType, componentId, componentInstance); + + when(userValidations.validateUserExists(user)).thenReturn(user); + when(componentNodeFilterBusinessLogic.validateUser(USER_ID)).thenReturn(user); + when(componentsUtils.getResponseFormat(ActionStatus.OK)).thenReturn(responseFormat); + + when(componentsUtils.convertJsonToObjectUsingObjectMapper(anyString(), any(User.class), + ArgumentMatchers.<Class<List>>any(), + nullable(AuditingActionEnum.class), nullable(ComponentTypeEnum.class))) + .thenReturn(Either.right(new ResponseFormat(HttpStatus.INTERNAL_SERVER_ERROR_500))); + + final Response response = target() + .path(path) + .request(MediaType.APPLICATION_JSON) + .header(USER_ID_HEADER, USER_ID) + .put(Entity.entity(inputJson, MediaType.APPLICATION_JSON)); + + assertThat(response.getStatus()).isEqualTo(HttpStatus.INTERNAL_SERVER_ERROR_500); + } + + @Test + public void updateNodeFilterFailConvertTest() throws JsonProcessingException { + initComponentData(); + final String pathFormat = "/v1/catalog/%s/%s/resourceInstances/%s/nodeFilter"; + final String path = String.format(pathFormat, componentType, componentId, componentInstance); + + when(userValidations.validateUserExists(user)).thenReturn(user); + when(componentNodeFilterBusinessLogic.validateUser(USER_ID)).thenReturn(user); + when(componentsUtils.getResponseFormat(ActionStatus.OK)).thenReturn(responseFormat); + + when(componentsUtils.convertJsonToObjectUsingObjectMapper(anyString(), any(User.class), + ArgumentMatchers.<Class<List>>any(), + nullable(AuditingActionEnum.class), nullable(ComponentTypeEnum.class))) + .thenReturn(Either.left(null)); + + final Response response = target() + .path(path) + .request(MediaType.APPLICATION_JSON) + .header(USER_ID_HEADER, USER_ID) + .put(Entity.entity(inputJson, MediaType.APPLICATION_JSON)); + + assertThat(response.getStatus()).isEqualTo(HttpStatus.INTERNAL_SERVER_ERROR_500); + } + + @Test + public void deleteNodeFilterSuccessTest() throws BusinessLogicException, JsonProcessingException { + initComponentData(); + final String pathFormat = "/v1/catalog/%s/%s/resourceInstances/%s/nodeFilter/%s"; + final String path = String.format(pathFormat, componentType, componentId, componentInstance, 0); + + when(userValidations.validateUserExists(user)).thenReturn(user); + when(componentNodeFilterBusinessLogic.validateUser(USER_ID)).thenReturn(user); + + when(responseFormat.getStatus()).thenReturn(HttpStatus.OK_200); + when(componentsUtils.getResponseFormat(ActionStatus.OK)).thenReturn(responseFormat); + + when(componentNodeFilterBusinessLogic.deleteNodeFilter(componentId, componentInstance, + NodeFilterConstraintAction.DELETE, null, 0, true, ComponentTypeEnum.RESOURCE)) + .thenReturn(Optional.of(ciNodeFilterDataDefinition)); + + final Response response = target() + .path(path) + .request(MediaType.APPLICATION_JSON) + .accept(MediaType.APPLICATION_JSON) + .header(USER_ID_HEADER, USER_ID) + .delete(Response.class); + + verify(componentNodeFilterBusinessLogic, times(1)) + .deleteNodeFilter(anyString(), anyString(), ArgumentMatchers.any(NodeFilterConstraintAction.class), + nullable(String.class), anyInt(), anyBoolean(), ArgumentMatchers.any(ComponentTypeEnum.class)); + + assertThat(response.getStatus()).isEqualTo(HttpStatus.OK_200); + } + + @Test + public void deleteNodeFilterFailTest() { + final String pathFormat = "/v1/catalog/%s/%s/resourceInstances/%s/nodeFilter/%s"; + final String path = String.format(pathFormat, componentType, componentId, componentInstance, 1); + final Response response = target() + .path(path) + .request(MediaType.APPLICATION_JSON) + .accept(MediaType.APPLICATION_JSON) + .header(USER_ID_HEADER, USER_ID) + .delete(Response.class); + + assertThat(response.getStatus()).isEqualTo(HttpStatus.INTERNAL_SERVER_ERROR_500); + } + + private static void createMocks() { + request = mock(HttpServletRequest.class); + userBusinessLogic = mock(UserBusinessLogic.class); + componentInstanceBusinessLogic = mock(ComponentInstanceBusinessLogic.class); + componentsUtils = mock(ComponentsUtils.class); + servletUtils = mock(ServletUtils.class); + resourceImportManager = mock(ResourceImportManager.class); + componentNodeFilterBusinessLogic = mock(ComponentNodeFilterBusinessLogic.class); + + session = mock(HttpSession.class); + servletContext = mock(ServletContext.class); + webAppContextWrapper = mock(WebAppContextWrapper.class); + webApplicationContext = mock(WebApplicationContext.class); + responseFormat = mock(ResponseFormat.class); + userValidations = mock(UserValidations.class); + } + + @Override + protected ResourceConfig configure() { + forceSet(TestProperties.CONTAINER_PORT, "0"); + final ApplicationContext context = new AnnotationConfigApplicationContext(SpringConfig.class); + return new ResourceConfig(ComponentNodeFilterServlet.class) + .register(new AbstractBinder() { + @Override + protected void configure() { + bind(request).to(HttpServletRequest.class); + bind(userBusinessLogic).to(UserBusinessLogic.class); + bind(componentInstanceBusinessLogic).to(ComponentInstanceBusinessLogic.class); + bind(componentsUtils).to(ComponentsUtils.class); + bind(servletUtils).to(ServletUtils.class); + bind(resourceImportManager).to(ResourceImportManager.class); + bind(componentNodeFilterBusinessLogic).to(ComponentNodeFilterBusinessLogic.class); + } + }) + .property("contextConfig", context); + } + + private void initComponentData() throws JsonProcessingException { + uiConstraint = new UIConstraint("resourceType", "equal", "static", "static", "resourceTypeValue"); + constraint = new ConstraintConvertor().convert(uiConstraint); + inputJson = buildConstraintDataJson(uiConstraint); + + requirementNodeFilterPropertyDataDefinition = new RequirementNodeFilterPropertyDataDefinition(); + requirementNodeFilterPropertyDataDefinition.setName(uiConstraint.getServicePropertyName()); + requirementNodeFilterPropertyDataDefinition.setConstraints(new LinkedList<>(Arrays.asList(constraint))); + + final ListDataDefinition<RequirementNodeFilterPropertyDataDefinition> listDataDefinition = + new ListDataDefinition<>(new LinkedList<>(Arrays.asList(requirementNodeFilterPropertyDataDefinition))); + + ciNodeFilterDataDefinition = new CINodeFilterDataDefinition(); + ciNodeFilterDataDefinition.setProperties(listDataDefinition); + ciNodeFilterDataDefinition.setID("NODE_FILTER_UID"); + + user = new User(); + user.setUserId(USER_ID); + user.setRole(Role.ADMIN.name()); + } + + private String buildConstraintDataJson(final UIConstraint uiConstraint) throws JsonProcessingException { + final ObjectMapper mapper = new ObjectMapper(); + return mapper.writeValueAsString(uiConstraint); + } + +}
\ No newline at end of file |