diff options
Diffstat (limited to 'catalog-be/src/test/java/org/openecomp/sdc/be/servlets/ComponentNodeFilterServletTest.java')
-rw-r--r-- | catalog-be/src/test/java/org/openecomp/sdc/be/servlets/ComponentNodeFilterServletTest.java | 303 |
1 files changed, 111 insertions, 192 deletions
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 index 7674ea9698..fac1586a8b 100644 --- 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 @@ -25,9 +25,9 @@ import static org.mockito.ArgumentMatchers.any; import static org.mockito.ArgumentMatchers.anyBoolean; import static org.mockito.ArgumentMatchers.anyInt; import static org.mockito.ArgumentMatchers.anyString; +import static org.mockito.ArgumentMatchers.eq; import static org.mockito.ArgumentMatchers.nullable; -import static org.mockito.Mockito.mock; -import static org.mockito.Mockito.reset; +import static org.mockito.Mockito.doReturn; import static org.mockito.Mockito.times; import static org.mockito.Mockito.verify; import static org.mockito.Mockito.when; @@ -35,12 +35,8 @@ 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; @@ -53,18 +49,24 @@ 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.AfterAll; 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.junit.jupiter.api.TestInstance; +import org.junit.jupiter.api.TestInstance.Lifecycle; +import org.junit.jupiter.api.extension.ExtendWith; import org.mockito.ArgumentMatchers; +import org.mockito.Mock; +import org.mockito.MockitoAnnotations; +import org.mockito.junit.jupiter.MockitoExtension; 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.Configuration; import org.openecomp.sdc.be.config.ConfigurationManager; import org.openecomp.sdc.be.config.SpringConfig; import org.openecomp.sdc.be.dao.api.ActionStatus; @@ -79,12 +81,11 @@ 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.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; @@ -92,7 +93,10 @@ import org.springframework.context.ApplicationContext; import org.springframework.context.annotation.AnnotationConfigApplicationContext; import org.springframework.web.context.WebApplicationContext; -public class ComponentNodeFilterServletTest extends JerseyTest { +@ExtendWith(MockitoExtension.class) +@TestInstance(Lifecycle.PER_CLASS) +class ComponentNodeFilterServletTest extends JerseyTest { + private static final String USER_ID = "jh0003"; private static final String servicePropertyName = "resourceType"; private static final String constraintOperator = "equal"; @@ -103,30 +107,47 @@ public class ComponentNodeFilterServletTest extends JerseyTest { private static final String componentInstance = "dac65869-dfb4-40d2-aa20-084324659ec1.resource0"; private static final String componentType = "resources"; private static final String capabilityName = "MyCapabilityName"; + private static final String V_1_CATALOG_S_S_COMPONENT_INSTANCE_S_S_S_NODE_FILTER = "/v1/catalog/%s/%s/componentInstance/%s/%s/%s/nodeFilter"; + private static final String V_1_CATALOG_S_S_COMPONENT_INSTANCE_S_S_NODE_FILTER = "/v1/catalog/%s/%s/componentInstance/%s/%s/nodeFilter"; + private final UIConstraint uiConstraint = new UIConstraint("resourceType", "equal", "static", "static", "resourceTypeValue"); + private final String constraint = new ConstraintConvertor().convert(uiConstraint); + private final String inputJson = buildConstraintDataJson(uiConstraint); + private final User user = new User("", "", USER_ID, "", Role.ADMIN.name(), null); + @Mock + private HttpServletRequest request; + @Mock + private HttpSession session; + @Mock + private ServletContext servletContext; + @Mock + private WebAppContextWrapper webAppContextWrapper; + @Mock + private WebApplicationContext webApplicationContext; + @Mock + private UserBusinessLogic userBusinessLogic; + @Mock + private ComponentInstanceBusinessLogic componentInstanceBusinessLogic; + @Mock + private ComponentsUtils componentsUtils; + @Mock + private ServletUtils servletUtils; + @Mock + private ResourceImportManager resourceImportManager; + @Mock + private ComponentNodeFilterBusinessLogic componentNodeFilterBusinessLogic; + @Mock + private ResponseFormat responseFormat; + @Mock + private UserValidations userValidations; - 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 static ConfigurationManager configurationManager; private CINodeFilterDataDefinition ciNodeFilterDataDefinition; - private UIConstraint uiConstraint; - private String constraint; - private String inputJson; - private User user; + + public ComponentNodeFilterServletTest() throws JsonProcessingException { + } @BeforeAll - public static void initClass() { - createMocks(); + public void initClass() { + MockitoAnnotations.initMocks(this); when(request.getSession()).thenReturn(session); when(session.getServletContext()).thenReturn(servletContext); when(servletContext.getAttribute(Constants.WEB_APPLICATION_CONTEXT_WRAPPER_ATTR)).thenReturn(webAppContextWrapper); @@ -135,10 +156,10 @@ public class ComponentNodeFilterServletTest extends JerseyTest { when(request.getHeader("USER_ID")).thenReturn(USER_ID); when(webApplicationContext.getBean(ServletUtils.class)).thenReturn(servletUtils); when(servletUtils.getComponentsUtils()).thenReturn(componentsUtils); - String appConfigDir = "src/test/resources/config/catalog-be"; - 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(); + final String appConfigDir = "src/test/resources/config/catalog-be"; + final ConfigurationSource configurationSource = new FSConfigurationSource(ExternalConfiguration.getChangeListener(), appConfigDir); + final ConfigurationManager configurationManager = new ConfigurationManager(configurationSource); + final org.openecomp.sdc.be.config.Configuration configuration = new org.openecomp.sdc.be.config.Configuration(); configuration.setJanusGraphInMemoryGraph(true); configurationManager.setConfiguration(configuration); ExternalConfiguration.setAppName("catalog-be"); @@ -147,7 +168,6 @@ public class ComponentNodeFilterServletTest extends JerseyTest { @BeforeEach public void resetMock() throws Exception { super.setUp(); - reset(componentNodeFilterBusinessLogic); } @AfterEach @@ -156,17 +176,16 @@ public class ComponentNodeFilterServletTest extends JerseyTest { } @Test - public void addNodeFilterPropertiesSuccessTest() throws BusinessLogicException, JsonProcessingException { + void addNodeFilterPropertiesSuccessTest() throws BusinessLogicException, JsonProcessingException { initComponentData(); - final String pathFormat = "/v1/catalog/%s/%s/componentInstance/%s/%s/nodeFilter"; + final String pathFormat = V_1_CATALOG_S_S_COMPONENT_INSTANCE_S_S_NODE_FILTER; final String path = String.format(pathFormat, componentType, componentId, componentInstance, NodeFilterConstraintType.PROPERTIES_PARAM_NAME); - 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); - + doReturn(user).when(componentNodeFilterBusinessLogic).validateUser(USER_ID); + doReturn(HttpStatus.OK_200).when(responseFormat).getStatus(); + doReturn(responseFormat).when(componentsUtils).getResponseFormat(ActionStatus.OK); + doReturn(componentsUtils).when(servletUtils).getComponentsUtils(); assertNotNull(uiConstraint); assertThat(servicePropertyName).isEqualToIgnoringCase(uiConstraint.getServicePropertyName()); assertThat(constraintOperator).isEqualToIgnoringCase(uiConstraint.getConstraintOperator()); @@ -174,18 +193,18 @@ public class ComponentNodeFilterServletTest extends JerseyTest { assertThat(sourceName).isEqualToIgnoringCase(uiConstraint.getSourceName()); assertThat(propertyValue).isEqualToIgnoringCase(uiConstraint.getValue().toString()); - when(componentsUtils.parseToConstraint(anyString(), any(User.class), ArgumentMatchers.any(ComponentTypeEnum.class))) - .thenReturn(Optional.of(uiConstraint)); + doReturn(Optional.of(uiConstraint)).when(componentsUtils) + .parseToConstraint(anyString(), any(User.class), ArgumentMatchers.any(ComponentTypeEnum.class)); assertNotNull(constraint); assertNotNull(ciNodeFilterDataDefinition); assertThat(ciNodeFilterDataDefinition.getProperties().getListToscaDataDefinition()).hasSize(1); assertThat("resourceType: {equal: resourceTypeValue}\n").isEqualToIgnoringCase(constraint); - when(componentNodeFilterBusinessLogic + + doReturn(Optional.of(ciNodeFilterDataDefinition)).when(componentNodeFilterBusinessLogic) .addNodeFilter(componentId, componentInstance, NodeFilterConstraintAction.ADD, uiConstraint.getServicePropertyName(), constraint, true, ComponentTypeEnum.RESOURCE, - NodeFilterConstraintType.PROPERTIES, "")) - .thenReturn(Optional.of(ciNodeFilterDataDefinition)); + NodeFilterConstraintType.PROPERTIES, ""); final Response response = target() .path(path) @@ -199,24 +218,26 @@ public class ComponentNodeFilterServletTest extends JerseyTest { ArgumentMatchers.any(NodeFilterConstraintType.class), anyString()); assertThat(response.getStatus()).isEqualTo(HttpStatus.OK_200); + verify(componentNodeFilterBusinessLogic,times(1)).validateUser(USER_ID); } @Test - public void addNodeFilterCapabilitiesSuccessTest() throws BusinessLogicException, JsonProcessingException { + void addNodeFilterCapabilitiesSuccessTest() throws BusinessLogicException, JsonProcessingException { initComponentData(); - final String pathFormat = "/v1/catalog/%s/%s/componentInstance/%s/%s/nodeFilter"; + final String pathFormat = V_1_CATALOG_S_S_COMPONENT_INSTANCE_S_S_NODE_FILTER; final String path = String.format(pathFormat, componentType, componentId, componentInstance, NodeFilterConstraintType.CAPABILITIES_PARAM_NAME); - - final UIConstraint uiConstraint1 = uiConstraint; - when(userValidations.validateUserExists(user)).thenReturn(user); + final UIConstraint uiConstraint1 = new UIConstraint(uiConstraint.getServicePropertyName(), uiConstraint.getConstraintOperator(), + uiConstraint.getSourceType(), uiConstraint.getSourceName(), uiConstraint.getValue()); when(componentNodeFilterBusinessLogic.validateUser(USER_ID)).thenReturn(user); when(responseFormat.getStatus()).thenReturn(HttpStatus.OK_200); when(componentsUtils.getResponseFormat(ActionStatus.OK)).thenReturn(responseFormat); - when(componentsUtils.parseToConstraint(anyString(), any(User.class),ArgumentMatchers.any(ComponentTypeEnum.class))) - .thenReturn(Optional.of(uiConstraint)); - + doReturn(componentsUtils).when(servletUtils).getComponentsUtils(); uiConstraint1.setCapabilityName(capabilityName); + + when(componentsUtils.parseToConstraint(anyString(), any(User.class), ArgumentMatchers.any(ComponentTypeEnum.class))) + .thenReturn(Optional.of(uiConstraint1)); + assertThat(ciNodeFilterDataDefinition.getProperties().getListToscaDataDefinition()).hasSize(1); when(componentNodeFilterBusinessLogic .addNodeFilter(componentId, componentInstance, NodeFilterConstraintAction.ADD, @@ -236,29 +257,17 @@ public class ComponentNodeFilterServletTest extends JerseyTest { ArgumentMatchers.any(NodeFilterConstraintType.class), anyString()); assertThat(response.getStatus()).isEqualTo(HttpStatus.OK_200); + verify(componentNodeFilterBusinessLogic,times(1)).validateUser(USER_ID); } @Test - public void addNodeFilterFailTest() throws BusinessLogicException, JsonProcessingException { + void addNodeFilterFailTest() throws BusinessLogicException, JsonProcessingException { initComponentData(); - final String pathFormat = "/v1/catalog/%s/%s/componentInstance/%s/%s/nodeFilter"; + final String pathFormat = V_1_CATALOG_S_S_COMPONENT_INSTANCE_S_S_NODE_FILTER; final String path = String.format(pathFormat, componentType, componentId, componentInstance, NodeFilterConstraintType.PROPERTIES_PARAM_NAME); - 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, - NodeFilterConstraintType.PROPERTIES, "")) - .thenReturn(Optional.empty()); final Response response = target() .path(path) @@ -267,25 +276,17 @@ public class ComponentNodeFilterServletTest extends JerseyTest { .post(Entity.entity(inputJson, MediaType.APPLICATION_JSON)); assertThat(response.getStatus()).isEqualTo(HttpStatus.INTERNAL_SERVER_ERROR_500); + verify(componentNodeFilterBusinessLogic,times(1)).validateUser(USER_ID); } @Test - public void addNodeFilterFailConstraintParseTest() throws JsonProcessingException { + void addNodeFilterFailConstraintParseTest() throws JsonProcessingException { initComponentData(); - final String pathFormat = "/v1/catalog/%s/%s/componentInstance/%s/%s/nodeFilter"; + final String pathFormat = V_1_CATALOG_S_S_COMPONENT_INSTANCE_S_S_NODE_FILTER; final String path = String.format(pathFormat, componentType, componentId, componentInstance, NodeFilterConstraintType.PROPERTIES_PARAM_NAME); - 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) @@ -293,26 +294,18 @@ public class ComponentNodeFilterServletTest extends JerseyTest { .post(Entity.entity(inputJson, MediaType.APPLICATION_JSON)); assertThat(response.getStatus()).isEqualTo(HttpStatus.INTERNAL_SERVER_ERROR_500); + verify(componentNodeFilterBusinessLogic,times(1)).validateUser(USER_ID); } @Test - public void addNodeFilterFailConvertTest() throws JsonProcessingException { + void addNodeFilterFailConvertTest() throws JsonProcessingException { initComponentData(); - final String pathFormat = "/v1/catalog/%s/%s/componentInstance/%s/%s/nodeFilter"; + final String pathFormat = V_1_CATALOG_S_S_COMPONENT_INSTANCE_S_S_NODE_FILTER; final String path = String.format(pathFormat, componentType, componentId, componentInstance, NodeFilterConstraintType.PROPERTIES.getType()); - 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) @@ -320,26 +313,23 @@ public class ComponentNodeFilterServletTest extends JerseyTest { .post(Entity.entity(inputJson, MediaType.APPLICATION_JSON)); assertThat(response.getStatus()).isEqualTo(HttpStatus.INTERNAL_SERVER_ERROR_500); + verify(componentNodeFilterBusinessLogic,times(1)).validateUser(USER_ID); } @Test - public void updateNodeFilterPropertiesSuccessTest() throws BusinessLogicException, JsonProcessingException { + void updateNodeFilterPropertiesSuccessTest() throws BusinessLogicException, JsonProcessingException { initComponentData(); - final String pathFormat = "/v1/catalog/%s/%s/componentInstance/%s/%s/%s/nodeFilter"; + final String pathFormat = V_1_CATALOG_S_S_COMPONENT_INSTANCE_S_S_S_NODE_FILTER; final String path = String.format(pathFormat, componentType, componentId, componentInstance, NodeFilterConstraintType.PROPERTIES_PARAM_NAME, 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(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)))); - + doReturn(componentsUtils).when(servletUtils).getComponentsUtils(); + doReturn(Optional.of(uiConstraint)).when(componentsUtils) + .parseToConstraint(anyString(), any(User.class), eq(ComponentTypeEnum.RESOURCE)); when(componentNodeFilterBusinessLogic .updateNodeFilter(componentId, componentInstance, uiConstraint, ComponentTypeEnum.RESOURCE, NodeFilterConstraintType.PROPERTIES, 0)).thenReturn(Optional.of(ciNodeFilterDataDefinition)); @@ -349,49 +339,25 @@ public class ComponentNodeFilterServletTest extends JerseyTest { .header(USER_ID_HEADER, USER_ID) .put(Entity.entity(inputJson, MediaType.APPLICATION_JSON)); - verify(componentNodeFilterBusinessLogic, times(1)) - .updateNodeFilter(anyString(), anyString(), ArgumentMatchers.any(UIConstraint.class), - ArgumentMatchers.any(ComponentTypeEnum.class), ArgumentMatchers.any(NodeFilterConstraintType.class), - anyInt()); - assertThat(response.getStatus()).isEqualTo(HttpStatus.OK_200); + verify(componentNodeFilterBusinessLogic,times(1)).validateUser(USER_ID); } @Test - public void updateNodeFilterCapabilitiesSuccessTest() throws BusinessLogicException, JsonProcessingException { + void updateNodeFilterCapabilitiesSuccessTest() throws BusinessLogicException, JsonProcessingException { initComponentData(); - final String pathFormat = "/v1/catalog/%s/%s/componentInstance/%s/%s/%s/nodeFilter"; + final String pathFormat = V_1_CATALOG_S_S_COMPONENT_INSTANCE_S_S_S_NODE_FILTER; final String path = String.format(pathFormat, componentType, componentId, componentInstance, NodeFilterConstraintType.CAPABILITIES_PARAM_NAME, 0); - when(userValidations.validateUserExists(user)).thenReturn(user); when(componentNodeFilterBusinessLogic.validateUser(USER_ID)).thenReturn(user); when(componentsUtils.parseToConstraint(anyString(), any(User.class), ArgumentMatchers.any(ComponentTypeEnum.class))) .thenReturn(Optional.of(uiConstraint)); - + doReturn(componentsUtils).when(servletUtils).getComponentsUtils(); when(responseFormat.getStatus()).thenReturn(HttpStatus.OK_200); when(componentsUtils.getResponseFormat(ActionStatus.OK)).thenReturn(responseFormat); - when(componentsUtils.validateAndParseConstraint(ArgumentMatchers.any(ComponentTypeEnum.class), anyString(), any(User.class))) - .thenReturn(Collections.singletonList(uiConstraint)); - - 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.deleteNodeFilter(componentId, componentInstance, - NodeFilterConstraintAction.DELETE, null, 0, true, ComponentTypeEnum.RESOURCE, - NodeFilterConstraintType.PROPERTIES)) - .thenReturn(Optional.of(ciNodeFilterDataDefinition)); - - when(componentNodeFilterBusinessLogic - .addNodeFilter(componentId, componentInstance, NodeFilterConstraintAction.ADD, - uiConstraint.getServicePropertyName(), constraint, true, ComponentTypeEnum.RESOURCE, - NodeFilterConstraintType.PROPERTIES, "")) - .thenReturn(Optional.of(ciNodeFilterDataDefinition)); - when(componentNodeFilterBusinessLogic .updateNodeFilter(componentId, componentInstance, uiConstraint, ComponentTypeEnum.RESOURCE, NodeFilterConstraintType.CAPABILITIES, 0)) @@ -409,54 +375,36 @@ public class ComponentNodeFilterServletTest extends JerseyTest { anyInt()); assertThat(response.getStatus()).isEqualTo(HttpStatus.OK_200); + verify(componentNodeFilterBusinessLogic,times(1)).validateUser(USER_ID); } @Test - public void updateNodeFilterFailTest() throws BusinessLogicException, JsonProcessingException { + void updateNodeFilterFailTest() throws BusinessLogicException, JsonProcessingException { initComponentData(); - final String pathFormat = "/v1/catalog/%s/%s/componentInstance/%s/%s/%s/nodeFilter"; + final String pathFormat = V_1_CATALOG_S_S_COMPONENT_INSTANCE_S_S_S_NODE_FILTER; final String path = String.format(pathFormat, componentType, componentId, componentInstance, NodeFilterConstraintType.PROPERTIES_PARAM_NAME, 0); - when(userValidations.validateUserExists(user)).thenReturn(user); when(componentNodeFilterBusinessLogic.validateUser(USER_ID)).thenReturn(user); - when(componentsUtils.getResponseFormat(ActionStatus.OK)).thenReturn(responseFormat); - - when(componentsUtils.validateAndParseConstraint(ArgumentMatchers.any(ComponentTypeEnum.class), anyString(), any(User.class))) - .thenReturn(Collections.singletonList(uiConstraint)); - - when(componentNodeFilterBusinessLogic - .updateNodeFilter(componentId, componentInstance, uiConstraint, - ComponentTypeEnum.RESOURCE, NodeFilterConstraintType.PROPERTIES, 0)) - .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(), ArgumentMatchers.any(UIConstraint.class), - ArgumentMatchers.any(ComponentTypeEnum.class), ArgumentMatchers.any(NodeFilterConstraintType.class), - anyInt()); - assertThat(response.getStatus()).isEqualTo(HttpStatus.INTERNAL_SERVER_ERROR_500); + verify(componentNodeFilterBusinessLogic,times(1)).validateUser(USER_ID); } @Test - public void updateNodeFilterFailConstraintParseTest() throws JsonProcessingException { + void updateNodeFilterFailConstraintParseTest() throws JsonProcessingException { initComponentData(); - final String pathFormat = "/v1/catalog/%s/%s/componentInstance/%s/%s/%s/nodeFilter"; + final String pathFormat = V_1_CATALOG_S_S_COMPONENT_INSTANCE_S_S_S_NODE_FILTER; final String path = String.format(pathFormat, componentType, componentId, componentInstance, NodeFilterConstraintType.PROPERTIES_PARAM_NAME, 0); - when(userValidations.validateUserExists(user)).thenReturn(user); when(componentNodeFilterBusinessLogic.validateUser(USER_ID)).thenReturn(user); - when(componentsUtils.getResponseFormat(ActionStatus.OK)).thenReturn(responseFormat); - - when(componentsUtils.validateAndParseConstraint(ArgumentMatchers.any(ComponentTypeEnum.class), anyString(), any(User.class))) - .thenReturn(Collections.emptyList()); final Response response = target() .path(path) @@ -465,23 +413,17 @@ public class ComponentNodeFilterServletTest extends JerseyTest { .put(Entity.entity(inputJson, MediaType.APPLICATION_JSON)); assertThat(response.getStatus()).isEqualTo(HttpStatus.INTERNAL_SERVER_ERROR_500); + verify(componentNodeFilterBusinessLogic,times(1)).validateUser(USER_ID); } @Test - public void updateNodeFilterFailConvertTest() throws JsonProcessingException { + void updateNodeFilterFailConvertTest() throws JsonProcessingException { initComponentData(); - final String pathFormat = "/v1/catalog/%s/%s/componentInstance/%s/%s/%s/nodeFilter"; + final String pathFormat = V_1_CATALOG_S_S_COMPONENT_INSTANCE_S_S_S_NODE_FILTER; final String path = String.format(pathFormat, componentType, componentId, componentInstance, NodeFilterConstraintType.PROPERTIES_PARAM_NAME, 0); - 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) @@ -490,18 +432,18 @@ public class ComponentNodeFilterServletTest extends JerseyTest { .put(Entity.entity(inputJson, MediaType.APPLICATION_JSON)); assertThat(response.getStatus()).isEqualTo(HttpStatus.INTERNAL_SERVER_ERROR_500); + verify(componentNodeFilterBusinessLogic,times(1)).validateUser(USER_ID); } @Test - public void deleteNodeFilterSuccessTest() throws BusinessLogicException, JsonProcessingException { + void deleteNodeFilterSuccessTest() throws BusinessLogicException, JsonProcessingException { initComponentData(); - final String pathFormat = "/v1/catalog/%s/%s/componentInstance/%s/%s/%s/nodeFilter"; + final String pathFormat = V_1_CATALOG_S_S_COMPONENT_INSTANCE_S_S_S_NODE_FILTER; final String path = String.format(pathFormat, componentType, componentId, componentInstance, NodeFilterConstraintType.PROPERTIES_PARAM_NAME, 0); - when(userValidations.validateUserExists(user)).thenReturn(user); when(componentNodeFilterBusinessLogic.validateUser(USER_ID)).thenReturn(user); - + doReturn(componentsUtils).when(servletUtils).getComponentsUtils(); when(responseFormat.getStatus()).thenReturn(HttpStatus.OK_200); when(componentsUtils.getResponseFormat(ActionStatus.OK)).thenReturn(responseFormat); @@ -523,11 +465,12 @@ public class ComponentNodeFilterServletTest extends JerseyTest { ArgumentMatchers.any(NodeFilterConstraintType.class)); assertThat(response.getStatus()).isEqualTo(HttpStatus.OK_200); + verify(componentNodeFilterBusinessLogic,times(1)).validateUser(USER_ID); } @Test - public void deleteNodeFilterFailTest() { - final String pathFormat = "/v1/catalog/%s/%s/componentInstance/%s/%s/%s/nodeFilter"; + void deleteNodeFilterFailTest() { + final String pathFormat = V_1_CATALOG_S_S_COMPONENT_INSTANCE_S_S_S_NODE_FILTER; final String path = String.format(pathFormat, componentType, componentId, componentInstance, NodeFilterConstraintType.PROPERTIES_PARAM_NAME, 0); final Response response = target() @@ -540,23 +483,6 @@ public class ComponentNodeFilterServletTest extends JerseyTest { 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"); @@ -578,10 +504,6 @@ public class ComponentNodeFilterServletTest extends JerseyTest { } private void initComponentData() throws JsonProcessingException { - uiConstraint = new UIConstraint("resourceType", "equal", "static", "static", "resourceTypeValue"); - constraint = new ConstraintConvertor().convert(uiConstraint); - inputJson = buildConstraintDataJson(uiConstraint); - final RequirementNodeFilterPropertyDataDefinition requirementNodeFilterPropertyDataDefinition = new RequirementNodeFilterPropertyDataDefinition(); requirementNodeFilterPropertyDataDefinition.setName(uiConstraint.getServicePropertyName()); @@ -603,9 +525,6 @@ public class ComponentNodeFilterServletTest extends JerseyTest { ciNodeFilterDataDefinition.setCapabilities(capabilityDataDefinitionList); 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 { |