From 7027b33eb41947e6799392d392283b09110158d1 Mon Sep 17 00:00:00 2001 From: aribeiro Date: Wed, 17 Jun 2020 14:20:46 +0100 Subject: Add Node Filter Business Logic Unit Tests Issue-ID: SDC-3125 Signed-off-by: aribeiro Change-Id: I80c755f4fffa813396ceb85c6aa7fde5b32590cd --- .../impl/ComponentNodeFilterBusinessLogic.java | 13 +- .../components/validation/NodeFilterValidator.java | 32 +- .../impl/ComponentNodeFilterBusinessLogicTest.java | 480 +++++++++++++++++++++ .../operations/NodeFilterOperation.java | 12 +- 4 files changed, 509 insertions(+), 28 deletions(-) create mode 100644 catalog-be/src/test/java/org/openecomp/sdc/be/components/impl/ComponentNodeFilterBusinessLogicTest.java diff --git a/catalog-be/src/main/java/org/openecomp/sdc/be/components/impl/ComponentNodeFilterBusinessLogic.java b/catalog-be/src/main/java/org/openecomp/sdc/be/components/impl/ComponentNodeFilterBusinessLogic.java index 0356b63793..48cb80d11d 100644 --- a/catalog-be/src/main/java/org/openecomp/sdc/be/components/impl/ComponentNodeFilterBusinessLogic.java +++ b/catalog-be/src/main/java/org/openecomp/sdc/be/components/impl/ComponentNodeFilterBusinessLogic.java @@ -276,11 +276,6 @@ public class ComponentNodeFilterBusinessLogic extends BaseBusinessLogic { throw new BusinessLogicException(componentsUtils .getResponseFormat(ActionStatus.NODE_FILTER_NOT_FOUND, response.right().value().getFormattedMessage())); } - boolean wasLocked = false; - if (shouldLock) { - lockComponent(component.getUniqueId(), component,"Update Node Filter on Component"); - wasLocked = true; - } final Optional componentInstance = getComponentInstance(componentInstanceId, component); if (!componentInstance.isPresent()) { @@ -289,10 +284,14 @@ public class ComponentNodeFilterBusinessLogic extends BaseBusinessLogic { } CINodeFilterDataDefinition nodeFilterDataDefinition = componentInstance.get().getNodeFilter(); if (nodeFilterDataDefinition == null) { - throw new BusinessLogicException(componentsUtils - .getResponseFormat(ActionStatus.NODE_FILTER_NOT_FOUND, response.right().value().getFormattedMessage())); + throw new BusinessLogicException(componentsUtils.getResponseFormat(ActionStatus.NODE_FILTER_NOT_FOUND)); } + boolean wasLocked = false; try { + if (shouldLock) { + lockComponent(component.getUniqueId(), component,"Update Node Filter on Component"); + wasLocked = true; + } final List properties = constraints.stream() .map(this::getRequirementNodeFilterPropertyDataDefinition).collect(Collectors.toList()); final Either result = nodeFilterOperation 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 27daaf2aae..30632e9012 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 @@ -59,28 +59,28 @@ public class NodeFilterValidator { public static final Set comparableConstraintsOperators = ImmutableSet.of(ConstraintConvertor.GREATER_THAN_OPERATOR, ConstraintConvertor.LESS_THAN_OPERATOR); - @Autowired - protected ToscaOperationFacade toscaOperationFacade; - - @Autowired - protected ComponentsUtils componentsUtils; + protected final ToscaOperationFacade toscaOperationFacade; + protected final ComponentsUtils componentsUtils; private static final Logger LOGGER = LoggerFactory.getLogger(NodeFilterValidator.class); - public Either validateComponentInstanceExist( - final Component component, - final String componentInstanceId) { + @Autowired + public NodeFilterValidator(final ToscaOperationFacade toscaOperationFacade, + final ComponentsUtils componentsUtils) { + this.toscaOperationFacade = toscaOperationFacade; + this.componentsUtils = componentsUtils; + } + + public Either validateComponentInstanceExist(final Component component, + final String componentInstanceId) { if (component == null || StringUtils.isEmpty(componentInstanceId)) { - LOGGER.debug("Input data cannot be empty"); - return getErrorResponse(ActionStatus.NODE_FILTER_NOT_FOUND); - } - if (CollectionUtils.isEmpty(component.getComponentInstances())) { - LOGGER.debug("Component Instance list is empty"); + LOGGER.error("Input data cannot be empty"); return getErrorResponse(ActionStatus.NODE_FILTER_NOT_FOUND); } - if (component.getComponentInstances().stream() - .noneMatch(ci -> ci.getUniqueId().equals(componentInstanceId))) { - LOGGER.debug("Component Instance list is empty"); + if (CollectionUtils.isEmpty(component.getComponentInstances()) || + component.getComponentInstances().stream() + .noneMatch(ci -> ci.getUniqueId().equals(componentInstanceId))) { + LOGGER.error("Component Instance list is empty"); return getErrorResponse(ActionStatus.NODE_FILTER_NOT_FOUND); } return Either.left(Boolean.TRUE); diff --git a/catalog-be/src/test/java/org/openecomp/sdc/be/components/impl/ComponentNodeFilterBusinessLogicTest.java b/catalog-be/src/test/java/org/openecomp/sdc/be/components/impl/ComponentNodeFilterBusinessLogicTest.java new file mode 100644 index 0000000000..d75a731b04 --- /dev/null +++ b/catalog-be/src/test/java/org/openecomp/sdc/be/components/impl/ComponentNodeFilterBusinessLogicTest.java @@ -0,0 +1,480 @@ +/* + * ============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.components.impl; + +import static java.util.Collections.singletonList; +import static org.assertj.core.api.Assertions.assertThat; +import static org.junit.jupiter.api.Assertions.assertNotNull; +import static org.junit.jupiter.api.Assertions.assertThrows; +import static org.junit.jupiter.api.Assertions.assertTrue; +import static org.junit.jupiter.api.Assertions.fail; +import static org.mockito.Mockito.times; +import static org.mockito.Mockito.verify; +import static org.mockito.Mockito.when; + +import fj.data.Either; +import java.util.ArrayList; +import java.util.Arrays; +import java.util.Collections; +import java.util.HashMap; +import java.util.LinkedList; +import java.util.List; +import java.util.Map; +import java.util.Optional; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.extension.ExtendWith; +import org.mockito.InjectMocks; +import org.mockito.Mock; +import org.mockito.Mockito; +import org.mockito.MockitoAnnotations; +import org.mockito.junit.jupiter.MockitoExtension; +import org.openecomp.sdc.be.components.impl.exceptions.BusinessLogicException; +import org.openecomp.sdc.be.components.impl.utils.DirectivesEnum; +import org.openecomp.sdc.be.components.impl.utils.NodeFilterConstraintAction; +import org.openecomp.sdc.be.components.validation.NodeFilterValidator; +import org.openecomp.sdc.be.components.validation.UserValidations; +import org.openecomp.sdc.be.dao.api.ActionStatus; +import org.openecomp.sdc.be.dao.janusgraph.JanusGraphGenericDao; +import org.openecomp.sdc.be.dao.jsongraph.JanusGraphDao; +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.datatypes.enums.NodeTypeEnum; +import org.openecomp.sdc.be.impl.ComponentsUtils; +import org.openecomp.sdc.be.model.ComponentInstance; +import org.openecomp.sdc.be.model.ComponentInstanceProperty; +import org.openecomp.sdc.be.model.PropertyDefinition; +import org.openecomp.sdc.be.model.Resource; +import org.openecomp.sdc.be.model.User; +import org.openecomp.sdc.be.model.jsonjanusgraph.operations.NodeFilterOperation; +import org.openecomp.sdc.be.model.jsonjanusgraph.operations.ToscaOperationFacade; +import org.openecomp.sdc.be.model.operations.api.StorageOperationStatus; +import org.openecomp.sdc.be.model.operations.impl.GraphLockOperation; +import org.openecomp.sdc.be.ui.model.UIConstraint; +import org.openecomp.sdc.be.user.Role; + +@ExtendWith(MockitoExtension.class) +public class ComponentNodeFilterBusinessLogicTest extends BaseBusinessLogicMock { + + 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 componentInstanceId = "dac65869-dfb4-40d2-aa20-084324659ec1.resource0"; + + @InjectMocks + private ComponentNodeFilterBusinessLogic componentNodeFilterBusinessLogic; + @Mock + private NodeFilterValidator nodeFilterValidator; + @Mock + private NodeFilterOperation nodeFilterOperation; + @Mock + private ToscaOperationFacade toscaOperationFacade; + @Mock + private GraphLockOperation graphLockOperation; + @Mock + private JanusGraphDao janusGraphDao; + @Mock + private JanusGraphGenericDao janusGraphGenericDao; + @Mock + private ComponentsUtils componentsUtils; + @Mock + private UserValidations userValidations; + + private Resource resource; + private ComponentInstance componentInstance; + private CINodeFilterDataDefinition ciNodeFilterDataDefinition; + private RequirementNodeFilterPropertyDataDefinition requirementNodeFilterPropertyDataDefinition; + private String constraint; + + @BeforeEach + public void init() { + MockitoAnnotations.initMocks(this); + componentNodeFilterBusinessLogic = + new ComponentNodeFilterBusinessLogic(elementDao, groupOperation, groupInstanceOperation, + groupTypeOperation, interfaceOperation, interfaceLifecycleTypeOperation, artifactToscaOperation, + nodeFilterOperation, nodeFilterValidator); + componentNodeFilterBusinessLogic.setToscaOperationFacade(toscaOperationFacade); + componentNodeFilterBusinessLogic.setGraphLockOperation(graphLockOperation); + componentNodeFilterBusinessLogic.setComponentsUtils(componentsUtils); + componentNodeFilterBusinessLogic.setUserValidations(userValidations); + componentNodeFilterBusinessLogic.setJanusGraphGenericDao(janusGraphGenericDao); + componentNodeFilterBusinessLogic.setJanusGraphDao(janusGraphDao); + + initResource(); + } + + @Test + public void createWhenNodeFilterExistsTest() throws BusinessLogicException { + componentInstance.setNodeFilter(ciNodeFilterDataDefinition); + + when(toscaOperationFacade.getToscaElement(componentId)).thenReturn(Either.left(resource)); + + final Optional result = componentNodeFilterBusinessLogic + .createNodeFilterIfNotExist(componentId, componentInstanceId, true, ComponentTypeEnum.RESOURCE); + + assertThat(result).isPresent(); + assertThat(result.get().getProperties()).isEqualTo(ciNodeFilterDataDefinition.getProperties()); + verify(toscaOperationFacade, times(1)).getToscaElement(componentId); + } + + @Test + public void createNodeFilterFailTest() { + when(toscaOperationFacade.getToscaElement(componentId)).thenReturn(Either.left(resource)); + when(graphLockOperation.lockComponent(componentId, NodeTypeEnum.Resource)) + .thenReturn(StorageOperationStatus.OK); + when(componentsUtils.convertFromStorageResponse(Mockito.any())).thenReturn(ActionStatus.GENERAL_ERROR); + when(nodeFilterOperation.createNodeFilter(componentId, componentInstanceId)) + .thenReturn(Either.right(StorageOperationStatus.GENERAL_ERROR)); + when(graphLockOperation.unlockComponent(componentId, NodeTypeEnum.Resource)) + .thenReturn(StorageOperationStatus.OK); + + assertThrows(BusinessLogicException.class, () -> componentNodeFilterBusinessLogic + .createNodeFilterIfNotExist(componentId, componentInstanceId, true, ComponentTypeEnum.RESOURCE)); + + verify(toscaOperationFacade, times(1)).getToscaElement(componentId); + verify(graphLockOperation, times(1)).lockComponent(componentId, NodeTypeEnum.Resource); + verify(nodeFilterOperation, times(1)).createNodeFilter(componentId, componentInstanceId); + verify(graphLockOperation, times(1)).unlockComponent(componentId, NodeTypeEnum.Resource); + } + + @Test + public void createNodeFilterIfNotExist() throws BusinessLogicException { + when(toscaOperationFacade.getToscaElement(componentId)).thenReturn(Either.left(resource)); + when(graphLockOperation.lockComponent(componentId, NodeTypeEnum.Resource)) + .thenReturn(StorageOperationStatus.OK); + when(nodeFilterOperation.createNodeFilter(componentId, componentInstanceId)) + .thenReturn(Either.left(ciNodeFilterDataDefinition)); + when(graphLockOperation.unlockComponent(componentId, NodeTypeEnum.Resource)) + .thenReturn(StorageOperationStatus.OK); + + final Optional result = componentNodeFilterBusinessLogic + .createNodeFilterIfNotExist(componentId, componentInstanceId, true, ComponentTypeEnum.RESOURCE); + + assertThat(result).isPresent(); + assertThat(result.get().getProperties()).isEqualTo(ciNodeFilterDataDefinition.getProperties()); + + verify(toscaOperationFacade, times(1)).getToscaElement(componentId); + verify(graphLockOperation, times(1)).lockComponent(componentId, NodeTypeEnum.Resource); + verify(nodeFilterOperation, times(1)).createNodeFilter(componentId, componentInstanceId); + verify(graphLockOperation, times(1)).unlockComponent(componentId, NodeTypeEnum.Resource); + } + + @Test + public void deleteNodeFilterIfExistsTest() throws BusinessLogicException { + when(toscaOperationFacade.getToscaElement(componentId)).thenReturn(Either.left(resource)); + when(nodeFilterValidator.validateComponentInstanceExist(resource, componentInstanceId)) + .thenReturn(Either.left(true)); + + final Optional result = componentNodeFilterBusinessLogic + .deleteNodeFilterIfExists(componentId, componentInstanceId, true, ComponentTypeEnum.RESOURCE); + + assertThat(result).isPresent(); + verify(toscaOperationFacade, times(1)).getToscaElement(componentId); + verify(nodeFilterValidator, times(1)).validateComponentInstanceExist(resource, componentInstanceId); + } + + @Test + public void deleteWhenNodeFilterExistsTest() throws BusinessLogicException { + componentInstance.setNodeFilter(ciNodeFilterDataDefinition); + + when(toscaOperationFacade.getToscaElement(componentId)).thenReturn(Either.left(resource)); + when(graphLockOperation.lockComponent(componentId, NodeTypeEnum.Resource)) + .thenReturn(StorageOperationStatus.OK); + when(nodeFilterValidator.validateComponentInstanceExist(resource, componentInstanceId)) + .thenReturn(Either.left(true)); + when(nodeFilterOperation.deleteNodeFilter(resource, componentInstanceId)) + .thenReturn(Either.left(componentInstanceId)); + when(graphLockOperation.unlockComponent(componentId, NodeTypeEnum.Resource)) + .thenReturn(StorageOperationStatus.OK); + + final Optional result = componentNodeFilterBusinessLogic + .deleteNodeFilterIfExists(componentId, componentInstanceId, true, ComponentTypeEnum.RESOURCE); + + assertThat(result).isPresent(); + verify(toscaOperationFacade, times(1)).getToscaElement(componentId); + verify(graphLockOperation, times(1)).lockComponent(componentId, NodeTypeEnum.Resource); + verify(nodeFilterValidator, times(1)).validateComponentInstanceExist(resource, componentInstanceId); + verify(nodeFilterOperation, times(1)).deleteNodeFilter(resource, componentInstanceId); + verify(graphLockOperation, times(1)).unlockComponent(componentId, NodeTypeEnum.Resource); + } + + @Test + public void deleteNodeFilterIfExistsFailTest() { + componentInstance.setNodeFilter(ciNodeFilterDataDefinition); + + when(toscaOperationFacade.getToscaElement(componentId)).thenReturn(Either.left(resource)); + when(graphLockOperation.lockComponent(componentId, NodeTypeEnum.Resource)) + .thenReturn(StorageOperationStatus.OK); + when(nodeFilterValidator.validateComponentInstanceExist(resource, componentInstanceId)) + .thenReturn(Either.left(true)); + when(nodeFilterOperation.deleteNodeFilter(resource, componentInstanceId)) + .thenReturn(Either.right(StorageOperationStatus.GENERAL_ERROR)); + when(graphLockOperation.unlockComponent(componentId, NodeTypeEnum.Resource)) + .thenReturn(StorageOperationStatus.OK); + + assertThrows(BusinessLogicException.class, () -> componentNodeFilterBusinessLogic + .deleteNodeFilterIfExists(componentId, componentInstanceId, true, ComponentTypeEnum.RESOURCE)); + + verify(toscaOperationFacade, times(1)).getToscaElement(componentId); + verify(graphLockOperation, times(1)).lockComponent(componentId, NodeTypeEnum.Resource); + verify(nodeFilterValidator, times(1)).validateComponentInstanceExist(resource, componentInstanceId); + verify(nodeFilterOperation, times(1)).deleteNodeFilter(resource, componentInstanceId); + verify(graphLockOperation, times(1)).unlockComponent(componentId, NodeTypeEnum.Resource); + } + + @Test + public void addNodeFilterFailTest() { + componentInstance.setNodeFilter(ciNodeFilterDataDefinition); + + when(toscaOperationFacade.getToscaElement(componentId)).thenReturn(Either.left(resource)); + when(nodeFilterValidator + .validateNodeFilter(resource, componentInstanceId, + requirementNodeFilterPropertyDataDefinition.getConstraints(), + NodeFilterConstraintAction.ADD)).thenReturn(Either.left(true)); + when(nodeFilterValidator.validateComponentInstanceExist(resource, componentInstanceId)) + .thenReturn(Either.left(true)); + when(graphLockOperation.lockComponent(componentId, NodeTypeEnum.Resource)) + .thenReturn(StorageOperationStatus.OK); + when(graphLockOperation.unlockComponent(componentId, NodeTypeEnum.Resource)) + .thenReturn(StorageOperationStatus.OK); + + final List constraints = requirementNodeFilterPropertyDataDefinition.getConstraints(); + assertThrows(BusinessLogicException.class, () -> componentNodeFilterBusinessLogic + .addNodeFilter(componentId, componentInstanceId, NodeFilterConstraintAction.ADD, + "MyPropertyName", constraint, true, ComponentTypeEnum.RESOURCE)); + + verify(toscaOperationFacade, times(1)).getToscaElement(componentId); + verify(graphLockOperation, times(1)).lockComponent(componentId, NodeTypeEnum.Resource); + verify(nodeFilterValidator, times(1)).validateNodeFilter(resource, componentInstanceId, + constraints, NodeFilterConstraintAction.ADD); + verify(nodeFilterOperation, times(0)) + .addNewProperty(componentId, componentInstanceId, ciNodeFilterDataDefinition, + requirementNodeFilterPropertyDataDefinition); + verify(graphLockOperation, times(1)).unlockComponent(componentId, NodeTypeEnum.Resource); + } + + @Test + public void addNodeFilterFailFetchComponentTest() { + componentInstance.setNodeFilter(ciNodeFilterDataDefinition); + + when(toscaOperationFacade.getToscaElement(componentId)) + .thenReturn(Either.right(StorageOperationStatus.GENERAL_ERROR)); + + assertThrows(BusinessLogicException.class, () -> componentNodeFilterBusinessLogic + .addNodeFilter(componentId, componentInstanceId, NodeFilterConstraintAction.ADD, + "MyPropertyName", constraint, true, ComponentTypeEnum.RESOURCE)); + } + + @Test + public void deleteNodeFilterTest() throws BusinessLogicException { + componentInstance.setNodeFilter(ciNodeFilterDataDefinition); + + when(toscaOperationFacade.getToscaElement(componentId)).thenReturn(Either.left(resource)); + when(nodeFilterValidator.validateComponentInstanceExist(resource, componentInstanceId)) + .thenReturn(Either.left(true)); + when(nodeFilterValidator.validateNodeFilter(resource, componentInstanceId, singletonList(constraint), + NodeFilterConstraintAction.DELETE)).thenReturn(Either.left(true)); + when(graphLockOperation.lockComponent(componentId, NodeTypeEnum.Resource)) + .thenReturn(StorageOperationStatus.OK); + + when(nodeFilterOperation + .deleteConstraint(componentId, componentInstanceId, ciNodeFilterDataDefinition, 0)) + .thenReturn(Either.left(ciNodeFilterDataDefinition)); + + when(graphLockOperation.unlockComponent(componentId, NodeTypeEnum.Resource)) + .thenReturn(StorageOperationStatus.OK); + + final Optional deleteNodeFilterResult = componentNodeFilterBusinessLogic + .deleteNodeFilter(componentId, componentInstanceId, NodeFilterConstraintAction.DELETE, constraint, + 0, true, ComponentTypeEnum.RESOURCE); + + assertThat(deleteNodeFilterResult).isPresent(); + + verify(toscaOperationFacade, times(1)).getToscaElement(componentId); + verify(graphLockOperation, times(1)).lockComponent(componentId, NodeTypeEnum.Resource); + verify(nodeFilterValidator, times(1)) + .validateComponentInstanceExist(resource, componentInstanceId); + verify(nodeFilterValidator, times(1)) + .validateNodeFilter(resource, componentInstanceId, singletonList(constraint), + NodeFilterConstraintAction.DELETE); + verify(nodeFilterOperation, times(1)) + .deleteConstraint(componentId, componentInstanceId, ciNodeFilterDataDefinition, 0); + verify(graphLockOperation, times(1)).unlockComponent(componentId, NodeTypeEnum.Resource); + } + + @Test + public void deleteNodeFilterFailTest() { + componentInstance.setNodeFilter(ciNodeFilterDataDefinition); + + when(toscaOperationFacade.getToscaElement(componentId)).thenReturn(Either.left(resource)); + when(nodeFilterValidator.validateComponentInstanceExist(resource, componentInstanceId)) + .thenReturn(Either.left(true)); + when(nodeFilterValidator.validateNodeFilter(resource, componentInstanceId, singletonList(constraint), + NodeFilterConstraintAction.DELETE)).thenReturn(Either.left(true)); + when(graphLockOperation.lockComponent(componentId, NodeTypeEnum.Resource)) + .thenReturn(StorageOperationStatus.OK); + + when(nodeFilterOperation + .deleteConstraint(componentId, componentInstanceId, ciNodeFilterDataDefinition, 0)) + .thenReturn(Either.right(StorageOperationStatus.GENERAL_ERROR)); + + when(graphLockOperation.unlockComponent(componentId, NodeTypeEnum.Resource)) + .thenReturn(StorageOperationStatus.OK); + + assertThrows(BusinessLogicException.class, () -> componentNodeFilterBusinessLogic + .deleteNodeFilter(componentId, componentInstanceId, NodeFilterConstraintAction.DELETE, constraint, + 0, true, ComponentTypeEnum.RESOURCE)); + + verify(toscaOperationFacade, times(1)).getToscaElement(componentId); + verify(graphLockOperation, times(1)).lockComponent(componentId, NodeTypeEnum.Resource); + verify(nodeFilterValidator, times(1)) + .validateNodeFilter(resource, componentInstanceId, singletonList(constraint), + NodeFilterConstraintAction.DELETE); + verify(nodeFilterValidator, times(1)) + .validateComponentInstanceExist(resource, componentInstanceId); + verify(graphLockOperation, times(1)).unlockComponent(componentId, NodeTypeEnum.Resource); + } + + @Test + public void deleteNodeFilterFailValidationTest() { + when(toscaOperationFacade.getToscaElement(componentId)).thenReturn(Either.left(resource)); + when(nodeFilterValidator.validateComponentInstanceExist(resource, componentInstanceId)) + .thenReturn(Either.left(true)); + when(nodeFilterValidator.validateNodeFilter(resource, componentInstanceId, singletonList(constraint), + NodeFilterConstraintAction.DELETE)).thenReturn(Either.left(true)); + + assertThrows(BusinessLogicException.class, () -> componentNodeFilterBusinessLogic + .deleteNodeFilter(componentId, componentInstanceId, NodeFilterConstraintAction.DELETE, constraint, + 0, true, ComponentTypeEnum.RESOURCE)); + + verify(toscaOperationFacade, times(1)).getToscaElement(componentId); + verify(nodeFilterValidator, times(1)) + .validateNodeFilter(resource, componentInstanceId, singletonList(constraint), + NodeFilterConstraintAction.DELETE); + verify(nodeFilterValidator, times(1)) + .validateComponentInstanceExist(resource, componentInstanceId); + } + + @Test + public void updateNodeFilterFailTest() { + componentInstance.setNodeFilter(ciNodeFilterDataDefinition); + + when(toscaOperationFacade.getToscaElement(componentId)).thenReturn(Either.left(resource)); + when(nodeFilterValidator.validateNodeFilter(resource, componentInstanceId, + Collections.singletonList(constraint), NodeFilterConstraintAction.UPDATE)).thenReturn(Either.left(true)); + when(graphLockOperation.lockComponent(componentId, NodeTypeEnum.Resource)) + .thenReturn(StorageOperationStatus.OK); + when(graphLockOperation.unlockComponent(componentId, NodeTypeEnum.Resource)) + .thenReturn(StorageOperationStatus.OK); + + final List constraints = requirementNodeFilterPropertyDataDefinition.getConstraints(); + assertThrows(BusinessLogicException.class, () -> componentNodeFilterBusinessLogic + .updateNodeFilter(componentId, componentInstanceId, constraints, true, ComponentTypeEnum.RESOURCE)); + + verify(toscaOperationFacade, times(1)).getToscaElement(componentId); + verify(nodeFilterValidator, times(1)).validateNodeFilter(resource, componentInstanceId, + constraints, NodeFilterConstraintAction.UPDATE); + } + + @Test + public void updateNodeFilterFailValidationTest() { + final List constraints = requirementNodeFilterPropertyDataDefinition.getConstraints(); + when(toscaOperationFacade.getToscaElement(componentId)).thenReturn(Either.left(resource)); + when(nodeFilterValidator.validateNodeFilter(resource, componentInstanceId, + constraints, NodeFilterConstraintAction.UPDATE)).thenReturn(Either.left(true)); + + assertThrows(BusinessLogicException.class, () -> componentNodeFilterBusinessLogic + .updateNodeFilter(componentId, componentInstanceId, constraints, true, ComponentTypeEnum.RESOURCE)); + + verify(toscaOperationFacade, times(1)).getToscaElement(componentId); + verify(nodeFilterValidator, times(1)).validateNodeFilter(resource, componentInstanceId, + constraints, NodeFilterConstraintAction.UPDATE); + } + + @Test + public void validateUserTes() { + final String USER_ID = "jh0003"; + final User user = new User(); + user.setUserId(USER_ID); + user.setRole(Role.ADMIN.name()); + when(userValidations.validateUserExists(USER_ID)).thenReturn(user).thenReturn(user); + final User result = componentNodeFilterBusinessLogic.validateUser(USER_ID); + assertNotNull(result); + assertTrue(USER_ID.equalsIgnoreCase(result.getUserId())); + assertTrue(Role.ADMIN.name().equalsIgnoreCase(result.getRole())); + } + + public void initResource() { + try { + resource = new Resource(); + resource.setName("MyResource"); + resource.setUniqueId(componentId); + resource.setToscaResourceName("My_Resource_Tosca_Name"); + resource.addCategory("Network Layer 2-3", "Router"); + resource.setDescription("My short description"); + + componentInstance = new ComponentInstance(); + componentInstance.setUniqueId(componentInstanceId); + componentInstance.setName("myComponentInstance"); + componentInstance.setDirectives(new LinkedList<>(Arrays.asList(DirectivesEnum.SELECT.getValue()))); + + final UIConstraint uiConstraint = + new UIConstraint(servicePropertyName, constraintOperator, sourceType, sourceName, propertyValue); + constraint = new ConstraintConvertor().convert(uiConstraint); + + requirementNodeFilterPropertyDataDefinition = new RequirementNodeFilterPropertyDataDefinition(); + requirementNodeFilterPropertyDataDefinition.setName(uiConstraint.getServicePropertyName()); + requirementNodeFilterPropertyDataDefinition.setConstraints(new LinkedList<>(Arrays.asList(constraint))); + + final ListDataDefinition listDataDefinition = + new ListDataDefinition<>(new LinkedList<>(Arrays.asList(requirementNodeFilterPropertyDataDefinition))); + + ciNodeFilterDataDefinition = new CINodeFilterDataDefinition(); + ciNodeFilterDataDefinition.setProperties(listDataDefinition); + ciNodeFilterDataDefinition.setID("NODE_FILTER_UID"); + + resource.setComponentInstances(singletonList(componentInstance)); + + final PropertyDefinition property = new PropertyDefinition(); + property.setName(uiConstraint.getServicePropertyName()); + + final List origProperties = new ArrayList<>(); + final ComponentInstanceProperty origProperty = new ComponentInstanceProperty(); + origProperty.setName(uiConstraint.getServicePropertyName()); + origProperty.setValue(propertyValue); + origProperty.setType(uiConstraint.getSourceType()); + origProperties.add(origProperty); + + final Map> componentInstanceProps = new HashMap<>(); + componentInstanceProps.put(componentInstanceId, origProperties); + + resource.setComponentInstancesProperties(componentInstanceProps); + resource.setProperties(new LinkedList<>(Arrays.asList(property))); + } catch (final Exception e) { + fail(e.getMessage()); + } + } +} \ No newline at end of file diff --git a/catalog-model/src/main/java/org/openecomp/sdc/be/model/jsonjanusgraph/operations/NodeFilterOperation.java b/catalog-model/src/main/java/org/openecomp/sdc/be/model/jsonjanusgraph/operations/NodeFilterOperation.java index 7ed7818ab4..5f0284a159 100644 --- a/catalog-model/src/main/java/org/openecomp/sdc/be/model/jsonjanusgraph/operations/NodeFilterOperation.java +++ b/catalog-model/src/main/java/org/openecomp/sdc/be/model/jsonjanusgraph/operations/NodeFilterOperation.java @@ -97,18 +97,20 @@ public class NodeFilterOperation extends BaseOperation { return addOrUpdateNodeFilter(true, serviceId, componentInstanceId, nodeFilterDataDefinition); } - public Either addNewProperty(String serviceId, - String componentInstanceId, CINodeFilterDataDefinition nodeFilterDataDefinition, - RequirementNodeFilterPropertyDataDefinition requirementNodeFilterPropertyDataDefinition) { + public Either addNewProperty( + final String componentId, final String componentInstanceId, + final CINodeFilterDataDefinition nodeFilterDataDefinition, + final RequirementNodeFilterPropertyDataDefinition requirementNodeFilterPropertyDataDefinition) { + ListDataDefinition properties = - nodeFilterDataDefinition.getProperties(); + nodeFilterDataDefinition.getProperties(); if (properties == null) { properties = new ListDataDefinition<>(); nodeFilterDataDefinition.setProperties(properties); } properties.getListToscaDataDefinition().add(requirementNodeFilterPropertyDataDefinition); nodeFilterDataDefinition.setProperties(properties); - return addOrUpdateNodeFilter(true, serviceId, componentInstanceId, nodeFilterDataDefinition); + return addOrUpdateNodeFilter(true, componentId, componentInstanceId, nodeFilterDataDefinition); } public Either updateProperties(String serviceId, -- cgit 1.2.3-korg