From 1f7c57414533b9886962ede7b19a29669fe7a59a Mon Sep 17 00:00:00 2001 From: mojahidi Date: Fri, 1 Mar 2019 17:50:15 +0530 Subject: Requirement and capabilities feature 1. Enhance Service/VF/PNF to support Req & Cap 2. Added Type fetch APIs to fetch types from global types Change-Id: I2b749ec9da34e488421b8ebe311ccf03c4b7c0fd Issue-ID: SDC-2142 Signed-off-by: mojahidi --- .../impl/CapabilitiesBusinessLogicTest.java | 443 +++++++++++++++++++++ .../impl/RequirementBusinessLogicTest.java | 412 +++++++++++++++++++ .../validation/CapabilitiesValidationTest.java | 159 ++++++++ .../validation/RequirementValidationTest.java | 156 ++++++++ .../sdc/be/servlets/TypesUploadServletTest.java | 2 +- 5 files changed, 1171 insertions(+), 1 deletion(-) create mode 100644 catalog-be/src/test/java/org/openecomp/sdc/be/components/impl/CapabilitiesBusinessLogicTest.java create mode 100644 catalog-be/src/test/java/org/openecomp/sdc/be/components/impl/RequirementBusinessLogicTest.java create mode 100644 catalog-be/src/test/java/org/openecomp/sdc/be/components/validation/CapabilitiesValidationTest.java create mode 100644 catalog-be/src/test/java/org/openecomp/sdc/be/components/validation/RequirementValidationTest.java (limited to 'catalog-be/src/test') diff --git a/catalog-be/src/test/java/org/openecomp/sdc/be/components/impl/CapabilitiesBusinessLogicTest.java b/catalog-be/src/test/java/org/openecomp/sdc/be/components/impl/CapabilitiesBusinessLogicTest.java new file mode 100644 index 0000000000..393ef58d1d --- /dev/null +++ b/catalog-be/src/test/java/org/openecomp/sdc/be/components/impl/CapabilitiesBusinessLogicTest.java @@ -0,0 +1,443 @@ +/* + * Copyright © 2016-2018 European Support Limited + * + * 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. + */ + +package org.openecomp.sdc.be.components.impl; + +import fj.data.Either; +import org.junit.Assert; +import org.junit.Before; +import org.junit.Test; +import org.mockito.InjectMocks; +import org.mockito.Mockito; +import org.mockito.MockitoAnnotations; +import org.openecomp.sdc.be.auditing.impl.AuditingManager; +import org.openecomp.sdc.be.components.validation.CapabilitiesValidation; +import org.openecomp.sdc.be.components.validation.UserValidations; +import org.openecomp.sdc.be.config.ConfigurationManager; +import org.openecomp.sdc.be.dao.api.ActionStatus; +import org.openecomp.sdc.be.dao.jsongraph.TitanDao; +import org.openecomp.sdc.be.dao.titan.TitanOperationStatus; +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.CapabilityDefinition; +import org.openecomp.sdc.be.model.CapabilityRequirementRelationship; +import org.openecomp.sdc.be.model.ComponentParametersView; +import org.openecomp.sdc.be.model.LifecycleStateEnum; +import org.openecomp.sdc.be.model.RelationshipInfo; +import org.openecomp.sdc.be.model.RequirementCapabilityRelDef; +import org.openecomp.sdc.be.model.Resource; +import org.openecomp.sdc.be.model.Service; +import org.openecomp.sdc.be.model.User; +import org.openecomp.sdc.be.model.jsontitan.operations.CapabilitiesOperation; +import org.openecomp.sdc.be.model.jsontitan.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.user.Role; +import org.openecomp.sdc.be.user.UserBusinessLogic; +import org.openecomp.sdc.common.api.ConfigurationSource; +import org.openecomp.sdc.common.impl.ExternalConfiguration; +import org.openecomp.sdc.common.impl.FSConfigurationSource; +import org.openecomp.sdc.exception.ResponseFormat; + +import java.util.ArrayList; +import java.util.Collections; +import java.util.HashMap; +import java.util.List; +import java.util.Map; +import java.util.stream.Collectors; +import java.util.stream.Stream; + +import static org.mockito.ArgumentMatchers.any; +import static org.mockito.ArgumentMatchers.anyBoolean; +import static org.mockito.ArgumentMatchers.anyCollection; +import static org.mockito.ArgumentMatchers.anyObject; +import static org.mockito.ArgumentMatchers.anyString; +import static org.mockito.ArgumentMatchers.eq; +import static org.mockito.Mockito.when; + +public class CapabilitiesBusinessLogicTest { + private final String componentId = "resourceId1"; + private final String capabilityId = "uniqueId1"; + + private final TitanDao mockTitanDao = Mockito.mock(TitanDao.class); + private final UserBusinessLogic mockUserAdmin = Mockito.mock(UserBusinessLogic.class); + private final ToscaOperationFacade toscaOperationFacade = Mockito.mock(ToscaOperationFacade.class); + private final UserValidations userValidations = Mockito.mock(UserValidations.class); + private final CapabilitiesOperation capabilitiesOperation = Mockito.mock(CapabilitiesOperation.class); + private final CapabilitiesValidation capabilitiesValidation = Mockito.mock(CapabilitiesValidation.class); + + private final GraphLockOperation graphLockOperation = Mockito.mock(GraphLockOperation.class); + private User user = null; + + @InjectMocks + private CapabilitiesBusinessLogic capabilitiesBusinessLogicMock = new CapabilitiesBusinessLogic(); + + @Before + public void setup() { + MockitoAnnotations.initMocks(this); + ExternalConfiguration.setAppName("catalog-be"); + + // init Configuration + String appConfigDir = "src/test/resources/config/catalog-be"; + ConfigurationSource configurationSource = new FSConfigurationSource(ExternalConfiguration.getChangeListener(), appConfigDir); + ConfigurationManager configurationManager = new ConfigurationManager(configurationSource); + + ComponentsUtils componentsUtils = new ComponentsUtils(Mockito.mock(AuditingManager.class)); + + // User data and management + user = new User(); + user.setUserId("jh0003"); + user.setFirstName("Jimmi"); + user.setLastName("Hendrix"); + user.setRole(Role.ADMIN.name()); + + Either eitherGetUser = Either.left(user); + when(mockUserAdmin.getUser("jh0003", false)).thenReturn(eitherGetUser); + when(graphLockOperation.lockComponent(Mockito.anyString(), eq(NodeTypeEnum.Resource))) + .thenReturn(StorageOperationStatus.OK); + + //CapabilityOperation + when(capabilitiesValidation.validateCapabilities(anyCollection(), anyObject(), anyBoolean()) + ).thenReturn(Either.left(true)); + when(capabilitiesOperation.addCapabilities(anyString(), anyObject())) + .thenReturn(Either.left(createMockCapabilityListToReturn( + createCapability("capName", "capDesc", "capType", "source1", + "0", "10")))); + + when(capabilitiesOperation.updateCapabilities(anyString(), anyObject())) + .thenReturn(Either.left(createMockCapabilityListToReturn( + createCapability("capName", "capDesc", "capType", "source1", + "0", "10")))); + when(capabilitiesOperation.deleteCapabilities( anyObject(), anyString())) + .thenReturn(StorageOperationStatus.OK); + when(mockTitanDao.commit()).thenReturn(TitanOperationStatus.OK); + + capabilitiesBusinessLogicMock = new CapabilitiesBusinessLogic(); + capabilitiesBusinessLogicMock.setComponentsUtils(componentsUtils); + capabilitiesBusinessLogicMock.setUserAdmin(mockUserAdmin); + capabilitiesBusinessLogicMock.setGraphLockOperation(graphLockOperation); + capabilitiesBusinessLogicMock.setTitanGenericDao(mockTitanDao); + capabilitiesBusinessLogicMock.setToscaOperationFacade(toscaOperationFacade); + capabilitiesBusinessLogicMock.setUserValidations(userValidations); + capabilitiesBusinessLogicMock.setCapabilitiesOperation(capabilitiesOperation); + capabilitiesBusinessLogicMock.setCapabilitiesValidation(capabilitiesValidation); + } + + @Test + public void shouldPassCreateCapabilitiesFirstTimeInComponentForHappyScenario(){ + List capabilityDefinitions = createMockCapabilityListToReturn( + createCapability("capName", "capDesc", "capType", "source1", + "0", "10")); + Resource resource = createComponent(false); + resource.setComponentType(ComponentTypeEnum.RESOURCE); + validateUserRoles(Role.ADMIN, Role.DESIGNER); + when(toscaOperationFacade.getToscaElement(anyString(), any(ComponentParametersView.class))) + .thenReturn(Either.left(resource)); + Either, ResponseFormat> capabilities = capabilitiesBusinessLogicMock + .createCapabilities(componentId, capabilityDefinitions, user, + "createCapabilities", true); + Assert.assertTrue(capabilities.isLeft()); + Assert.assertTrue(capabilities.left().value().stream().anyMatch(capabilityDefinition -> + capabilityDefinition.getName().equals("capName"))); + } + + @Test + public void shouldPassCreateCapabilitiesForHappyScenario(){ + List capabilityDefinitions = createMockCapabilityListToReturn( + createCapability("capName2", "capDesc", "capType", "source1", + "0", "10")); + Resource resource = createComponent(true); + resource.setComponentType(ComponentTypeEnum.RESOURCE); + validateUserRoles(Role.ADMIN, Role.DESIGNER); + when(toscaOperationFacade.getToscaElement(anyString(), any(ComponentParametersView.class))) + .thenReturn(Either.left(resource)); + Either, ResponseFormat> capabilities = capabilitiesBusinessLogicMock + .createCapabilities(componentId, capabilityDefinitions, user, + "createCapabilities", false); + + Assert.assertTrue(capabilities.isLeft()); + Assert.assertTrue(capabilities.left().value().stream().anyMatch(capabilityDefinition -> + capabilityDefinition.getName().equals("capName2"))); + } + + @Test + public void shouldFailCreateCapabilitiesWhenOperationFailedInTitan(){ + List capabilityDefinitions = createMockCapabilityListToReturn( + createCapability("capName2", "capDesc", "capType", "source1", + "0", "10")); + Resource resource = createComponent(true); + resource.setComponentType(ComponentTypeEnum.RESOURCE); + validateUserRoles(Role.ADMIN, Role.DESIGNER); + when(capabilitiesOperation.addCapabilities(anyString(), anyObject())) + .thenReturn(Either.right(StorageOperationStatus.GENERAL_ERROR)); + when(capabilitiesOperation.updateCapabilities(anyString(), anyObject())) + .thenReturn(Either.right(StorageOperationStatus.GENERAL_ERROR)); + when(toscaOperationFacade.getToscaElement(anyString(), any(ComponentParametersView.class))) + .thenReturn(Either.left(resource)); + Either, ResponseFormat> capabilities = capabilitiesBusinessLogicMock + .createCapabilities(componentId, capabilityDefinitions, user, + "createCapabilities", true); + + Assert.assertTrue(capabilities.isRight()); + } + @Test + public void shouldPassUpdateCapabilitiesForHappyScenario(){ + + List capabilityDefinitions = createMockCapabilityListToReturn( + createCapability("capName", "capDesc updated", "capType", "source1", + "6", "11")); + Resource resource = createComponent(true); + resource.setComponentType(ComponentTypeEnum.RESOURCE); + validateUserRoles(Role.ADMIN, Role.DESIGNER); + when(toscaOperationFacade.getToscaElement(anyString(), any(ComponentParametersView.class))) + .thenReturn(Either.left(resource)); + when(toscaOperationFacade.getParentComponents(anyString())) + .thenReturn(Either.right(StorageOperationStatus.NOT_FOUND)); + Either, ResponseFormat> capabilities = capabilitiesBusinessLogicMock + .updateCapabilities(componentId, capabilityDefinitions, user + , "updateCapabilities", true); + Assert.assertTrue(capabilities.isLeft()); + Assert.assertTrue(capabilities.left().value().stream().anyMatch(capabilityDefinition -> + capabilityDefinition.getMaxOccurrences().equals("11"))); + } + + @Test + public void shouldPassUpdateCapabilityTypeUpdateWhenTypeIsNotAvailable(){ + + List capabilityDefinitions = createMockCapabilityListToReturn( + createCapability("capName", "capDesc updated", "capTypeUpdate", "source1", + "6", "11")); + Resource resource = createComponent(true); + resource.setComponentType(ComponentTypeEnum.RESOURCE); + validateUserRoles(Role.ADMIN, Role.DESIGNER); + when(toscaOperationFacade.getToscaElement(anyString(), any(ComponentParametersView.class))) + .thenReturn(Either.left(resource)); + when(toscaOperationFacade.getParentComponents(anyString())) + .thenReturn(Either.right(StorageOperationStatus.NOT_FOUND)); + Either, ResponseFormat> capabilities = capabilitiesBusinessLogicMock + .updateCapabilities(componentId, capabilityDefinitions, user, + "updateCapabilities",true); + Assert.assertTrue(capabilities.isLeft()); + Assert.assertTrue(capabilities.left().value().stream().anyMatch(capabilityDefinition -> + capabilityDefinition.getMaxOccurrences().equals("11"))); + } + + @Test + public void shouldPassUpdateCapabilityTypeUpdateWhenTypeIsAvailable(){ + + List capabilityDefinitions = createMockCapabilityListToReturn( + createCapability("capName", "capDesc updated", "capTypeUpdate1", "source1", + "6", "11")); + Resource resource = createComponent(true); + resource.setComponentType(ComponentTypeEnum.RESOURCE); + + CapabilityDefinition capability = createCapability("capName", "capDesc", "capTypeUpdate1", + "source1", "0", "10"); + capability.setUniqueId("unique2"); + List capabilityDefinitions1 = createMockCapabilityListToReturn(capability); + Map> capabilityMap = new HashMap<>(); + capabilityMap.put("capTypeUpdate1", capabilityDefinitions1); + resource.getCapabilities().putAll(capabilityMap); + + validateUserRoles(Role.ADMIN, Role.DESIGNER); + when(toscaOperationFacade.getToscaElement(anyString(), any(ComponentParametersView.class))) + .thenReturn(Either.left(resource)); + when(toscaOperationFacade.getParentComponents(anyString())) + .thenReturn(Either.right(StorageOperationStatus.NOT_FOUND)); + Either, ResponseFormat> capabilities = capabilitiesBusinessLogicMock + .updateCapabilities(componentId, capabilityDefinitions, user, + "updateCapabilities",true); + Assert.assertTrue(capabilities.isLeft()); + Assert.assertTrue(capabilities.left().value().stream().anyMatch(capabilityDefinition -> + capabilityDefinition.getMaxOccurrences().equals("11"))); + } + + @Test + public void shouldFailUpdateCapabilitiesWhenOperaitonFailedInTitan(){ + List capabilityDefinitions = createMockCapabilityListToReturn( + createCapability("capName2", "capDesc", "capType", "source1", + "0", "10")); + Resource resource = createComponent(true); + resource.setComponentType(ComponentTypeEnum.RESOURCE); + validateUserRoles(Role.ADMIN, Role.DESIGNER); + when(capabilitiesOperation.addCapabilities(anyString(), anyObject())) + .thenReturn(Either.right(StorageOperationStatus.GENERAL_ERROR)); + when(capabilitiesOperation.updateCapabilities(anyString(), anyObject())) + .thenReturn(Either.right(StorageOperationStatus.GENERAL_ERROR)); + when(toscaOperationFacade.getToscaElement(anyString(), any(ComponentParametersView.class))) + .thenReturn(Either.left(resource)); + Either, ResponseFormat> capabilities = capabilitiesBusinessLogicMock + .updateCapabilities(componentId, capabilityDefinitions, user, + "updateCapabilities", true); + + Assert.assertTrue(capabilities.isRight()); + } + + @Test + public void shouldPassDeleteCapabilitiesForHappyScenario(){ + Resource resource = createComponent(true); + resource.setComponentType(ComponentTypeEnum.RESOURCE); + validateUserRoles(Role.ADMIN, Role.DESIGNER); + when(toscaOperationFacade.getToscaElement(anyString(), any(ComponentParametersView.class))) + .thenReturn(Either.left(resource)); + when(toscaOperationFacade.getParentComponents(anyString())) + .thenReturn(Either.right(StorageOperationStatus.NOT_FOUND)); + Either deleteCapabilityEither = + capabilitiesBusinessLogicMock.deleteCapability(componentId, capabilityId, user, true); + Assert.assertTrue(deleteCapabilityEither.isLeft()); + + } + + @Test + public void shouldFailDeleteCapabilitiesWhenOperationFailedInTitan(){ + Resource resource = createComponent(true); + resource.setComponentType(ComponentTypeEnum.RESOURCE); + validateUserRoles(Role.ADMIN, Role.DESIGNER); + when(toscaOperationFacade.getToscaElement(anyString(), any(ComponentParametersView.class))) + .thenReturn(Either.left(resource)); + when(toscaOperationFacade.getParentComponents(anyString())) + .thenReturn(Either.right(StorageOperationStatus.NOT_FOUND)); + when(capabilitiesOperation.deleteCapabilities(anyObject(), anyString())) + .thenReturn(StorageOperationStatus.GENERAL_ERROR); + Either deleteCapabilityEither + = capabilitiesBusinessLogicMock.deleteCapability(componentId, capabilityId, user, true); + Assert.assertTrue(deleteCapabilityEither.isRight()); + } + + @Test + public void shouldFailDeleteCapabilitiesWhenCapabilityUsedInServiceComposition(){ + Resource resource = createComponent(true); + resource.setComponentType(ComponentTypeEnum.RESOURCE); + validateUserRoles(Role.ADMIN, Role.DESIGNER); + when(toscaOperationFacade.getToscaElement(anyString(), any(ComponentParametersView.class))) + .thenReturn(Either.left(resource)); + when(toscaOperationFacade.getParentComponents(anyString())) + .thenReturn(Either.left(Collections.singletonList(createParentService()))); + Either deleteCapabilityEither + = capabilitiesBusinessLogicMock.deleteCapability(componentId, capabilityId, user, true); + Assert.assertTrue(deleteCapabilityEither.isRight()); + } + + @Test + public void shouldPassGetCapabilitiesForHappyScenario(){ + Resource resource = createComponent(true); + resource.setComponentType(ComponentTypeEnum.RESOURCE); + validateUserRoles(Role.ADMIN, Role.DESIGNER); + when(toscaOperationFacade.getToscaElement(anyString(), any(ComponentParametersView.class))) + .thenReturn(Either.left(resource)); + when(toscaOperationFacade.getParentComponents(anyString())) + .thenReturn(Either.right(StorageOperationStatus.NOT_FOUND)); + Either getCapabilityEither + = capabilitiesBusinessLogicMock.getCapability(componentId, capabilityId, user, true); + Assert.assertTrue(getCapabilityEither.isLeft()); + + } + + @Test + public void shouldFailGetCapabilitiesWhenCapabilityNotExist(){ + Resource resource = createComponent(true); + resource.setComponentType(ComponentTypeEnum.RESOURCE); + validateUserRoles(Role.ADMIN, Role.DESIGNER); + when(toscaOperationFacade.getToscaElement(anyString(), any(ComponentParametersView.class))) + .thenReturn(Either.left(resource)); + when(toscaOperationFacade.getParentComponents(anyString())) + .thenReturn(Either.right(StorageOperationStatus.NOT_FOUND)); + Either getCapabilityEither + = capabilitiesBusinessLogicMock.getCapability(componentId, "capId1", user, true); + Assert.assertTrue(getCapabilityEither.isRight()); + + } + + private Resource createComponent(boolean needCapability) { + Resource resource = new Resource(); + resource.setName("Resource1"); + resource.addCategory("Network Layer 2-3", "Router"); + resource.setDescription("My short description"); + List tgs = new ArrayList<>(); + tgs.add("test"); + tgs.add(resource.getName()); + resource.setTags(tgs); + + if(needCapability) { + List capabilityDefinitions = createMockCapabilityListToReturn( + createCapability("capName", "capDesc", "capType", "source1", + "0", "10")); + Map> capabilityMap = new HashMap<>(); + capabilityMap.put("capType", capabilityDefinitions); + resource.setCapabilities(capabilityMap); + } + resource.setName(resource.getName()); + resource.setVersion("0.1"); + resource.setUniqueId(resource.getName().toLowerCase() + ":" + resource.getVersion()); + resource.setCreatorUserId(user.getUserId()); + resource.setCreatorFullName(user.getFirstName() + " " + user.getLastName()); + resource.setLifecycleState(LifecycleStateEnum.NOT_CERTIFIED_CHECKOUT); + return resource; + } + + private List createMockCapabilityListToReturn(CapabilityDefinition capabilityDefinition) { + List capabilityDefinitions = new ArrayList<>(); + capabilityDefinitions.add(capabilityDefinition); + return capabilityDefinitions; + } + + private void validateUserRoles(Role... roles) { + List listOfRoles = Stream.of(roles).collect(Collectors.toList()); + } + + private CapabilityDefinition createCapability(String name, String description, String type, + String validSourceTypes, String minOccurrences, + String maxOccurrences) { + CapabilityDefinition capabilityDefinition = new CapabilityDefinition(); + capabilityDefinition.setName(name); + capabilityDefinition.setDescription(description); + capabilityDefinition.setType(type); + capabilityDefinition.setValidSourceTypes(Collections.singletonList(validSourceTypes)); + capabilityDefinition.setMaxOccurrences(maxOccurrences); + capabilityDefinition.setMinOccurrences(minOccurrences); + capabilityDefinition.setUniqueId(capabilityId); + + return capabilityDefinition; + } + + private Service createParentService() { + Service service = new Service(); + service.setUniqueId("serviceUniqueId"); + + List resourceInstancesRelations = new ArrayList<>(); + RequirementCapabilityRelDef relationDef = new RequirementCapabilityRelDef(); + relationDef.setFromNode("fromNode"); + relationDef.setToNode("toNode"); + + List relationships = new ArrayList<>(); + CapabilityRequirementRelationship capabilityRequirementRelationship = new CapabilityRequirementRelationship(); + + RelationshipInfo relation = new RelationshipInfo(); + relation.setCapabilityUid(capabilityId); + relation.setRequirementUid("reqUniqueId1"); + capabilityRequirementRelationship.setRelation(relation); + + relationships.add(capabilityRequirementRelationship); + relationDef.setRelationships(relationships); + resourceInstancesRelations.add(relationDef); + + service.setComponentInstancesRelations(resourceInstancesRelations); + + return service; + } + +} \ No newline at end of file diff --git a/catalog-be/src/test/java/org/openecomp/sdc/be/components/impl/RequirementBusinessLogicTest.java b/catalog-be/src/test/java/org/openecomp/sdc/be/components/impl/RequirementBusinessLogicTest.java new file mode 100644 index 0000000000..3cac5f5b15 --- /dev/null +++ b/catalog-be/src/test/java/org/openecomp/sdc/be/components/impl/RequirementBusinessLogicTest.java @@ -0,0 +1,412 @@ +/* + * Copyright © 2016-2018 European Support Limited + * + * 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. + */ + +package org.openecomp.sdc.be.components.impl; + +import fj.data.Either; +import org.junit.Assert; +import org.junit.Before; +import org.junit.Test; +import org.mockito.InjectMocks; +import org.mockito.Mockito; +import org.mockito.MockitoAnnotations; +import org.openecomp.sdc.be.auditing.impl.AuditingManager; +import org.openecomp.sdc.be.components.validation.RequirementValidation; +import org.openecomp.sdc.be.components.validation.UserValidations; +import org.openecomp.sdc.be.config.ConfigurationManager; +import org.openecomp.sdc.be.dao.api.ActionStatus; +import org.openecomp.sdc.be.dao.jsongraph.TitanDao; +import org.openecomp.sdc.be.dao.titan.TitanOperationStatus; +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.CapabilityRequirementRelationship; +import org.openecomp.sdc.be.model.ComponentParametersView; +import org.openecomp.sdc.be.model.LifecycleStateEnum; +import org.openecomp.sdc.be.model.RelationshipInfo; +import org.openecomp.sdc.be.model.RequirementCapabilityRelDef; +import org.openecomp.sdc.be.model.RequirementDefinition; +import org.openecomp.sdc.be.model.Resource; +import org.openecomp.sdc.be.model.Service; +import org.openecomp.sdc.be.model.User; +import org.openecomp.sdc.be.model.jsontitan.operations.RequirementOperation; +import org.openecomp.sdc.be.model.jsontitan.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.user.Role; +import org.openecomp.sdc.be.user.UserBusinessLogic; +import org.openecomp.sdc.common.api.ConfigurationSource; +import org.openecomp.sdc.common.impl.ExternalConfiguration; +import org.openecomp.sdc.common.impl.FSConfigurationSource; +import org.openecomp.sdc.exception.ResponseFormat; + +import java.util.ArrayList; +import java.util.Collections; +import java.util.HashMap; +import java.util.List; +import java.util.Map; +import java.util.stream.Collectors; +import java.util.stream.Stream; + +import static org.mockito.ArgumentMatchers.any; +import static org.mockito.ArgumentMatchers.anyBoolean; +import static org.mockito.ArgumentMatchers.anyCollection; +import static org.mockito.ArgumentMatchers.anyObject; +import static org.mockito.ArgumentMatchers.anyString; +import static org.mockito.ArgumentMatchers.eq; +import static org.mockito.Mockito.when; + +public class RequirementBusinessLogicTest { + private final String componentId = "resourceId1"; + private final String requirementId = "uniqueId1"; + + private final TitanDao mockTitanDao = Mockito.mock(TitanDao.class); + private final UserBusinessLogic mockUserAdmin = Mockito.mock(UserBusinessLogic.class); + private final ToscaOperationFacade toscaOperationFacade = Mockito.mock(ToscaOperationFacade.class); + private final UserValidations userValidations = Mockito.mock(UserValidations.class); + private final RequirementOperation requirementOperation = Mockito.mock(RequirementOperation.class); + private final RequirementValidation requirementValidation = Mockito.mock(RequirementValidation.class); + + private final GraphLockOperation graphLockOperation = Mockito.mock(GraphLockOperation.class); + private User user = null; + + @InjectMocks + private RequirementBusinessLogic requirementsBusinessLogicMock = new RequirementBusinessLogic(); + + @Before + public void setup() { + MockitoAnnotations.initMocks(this); + ExternalConfiguration.setAppName("catalog-be"); + + // init Configuration + String appConfigDir = "src/test/resources/config/catalog-be"; + ConfigurationSource configurationSource = new FSConfigurationSource(ExternalConfiguration + .getChangeListener(), appConfigDir); + ConfigurationManager configurationManager = new ConfigurationManager(configurationSource); + + ComponentsUtils componentsUtils = new ComponentsUtils(Mockito.mock(AuditingManager.class)); + // User data and management + user = new User(); + user.setUserId("jh0003"); + user.setFirstName("Jimmi"); + user.setLastName("Hendrix"); + user.setRole(Role.ADMIN.name()); + + Either eitherGetUser = Either.left(user); + when(mockUserAdmin.getUser("jh0003", false)).thenReturn(eitherGetUser); + when(graphLockOperation.lockComponent(Mockito.anyString(), eq(NodeTypeEnum.Resource))) + .thenReturn(StorageOperationStatus.OK); + + //CapabilityOperation + when(requirementValidation.validateRequirements(anyCollection(), anyObject(), anyBoolean())) + .thenReturn(Either.left(true)); + when(requirementOperation.addRequirement(anyString(), anyObject())) + .thenReturn(Either.left(createMockRequirementListToReturn(createRequirement( + "reqName", "capType", "node", "source1", + "0", "10")))); + + when(requirementOperation.updateRequirement(anyString(), anyObject())) + .thenReturn(Either.left(createMockRequirementListToReturn(createRequirement( + "reqName", "capType", "node", "source1", + "0", "10")))); + when(requirementOperation.deleteRequirements( anyObject(), anyString())) + .thenReturn(StorageOperationStatus.OK); + when(mockTitanDao.commit()).thenReturn(TitanOperationStatus.OK); + + requirementsBusinessLogicMock = new RequirementBusinessLogic(); + + requirementsBusinessLogicMock.setComponentsUtils(componentsUtils); + requirementsBusinessLogicMock.setUserAdmin(mockUserAdmin); + requirementsBusinessLogicMock.setGraphLockOperation(graphLockOperation); + requirementsBusinessLogicMock.setTitanGenericDao(mockTitanDao); + requirementsBusinessLogicMock.setToscaOperationFacade(toscaOperationFacade); + requirementsBusinessLogicMock.setUserValidations(userValidations); + requirementsBusinessLogicMock.setRequirementOperation(requirementOperation); + requirementsBusinessLogicMock.setRequirementValidation(requirementValidation); + } + + @Test + public void shouldPassCreateRequirementsFirstTimeInComponentForHappyScenario(){ + List requirementDefinitions = createMockRequirementListToReturn( + createRequirement("reqName", "reqDesc", "capType", "source1", + "0", "10")); + Resource resource = createComponent(false); + resource.setComponentType(ComponentTypeEnum.RESOURCE); + validateUserRoles(Role.ADMIN, Role.DESIGNER); + when(toscaOperationFacade.getToscaElement(anyString(), any(ComponentParametersView.class))) + .thenReturn(Either.left(resource)); + Either, ResponseFormat> requirements = requirementsBusinessLogicMock + .createRequirements(componentId, requirementDefinitions, user, + "createRequirements", true); + Assert.assertTrue(requirements.isLeft()); + Assert.assertTrue(requirements.left().value().stream().anyMatch(requirementDefinition -> + requirementDefinition.getName().equals("reqName"))); + } + + @Test + public void shouldPassCreateRequirementsForHappyScenario(){ + List requirementDefinitions = createMockRequirementListToReturn( + createRequirement("reqName2", "capType", "node", "source1", + "0", "10")); + Resource resource = createComponent(true); + resource.setComponentType(ComponentTypeEnum.RESOURCE); + validateUserRoles(Role.ADMIN, Role.DESIGNER); + when(toscaOperationFacade.getToscaElement(anyString(), any(ComponentParametersView.class))) + .thenReturn(Either.left(resource)); + Either, ResponseFormat> requirements = requirementsBusinessLogicMock + .createRequirements(componentId, requirementDefinitions, user, + "createRequirements", true); + + Assert.assertTrue(requirements.isLeft()); + Assert.assertTrue(requirements.left().value().stream().anyMatch(requirementDefinition -> + requirementDefinition.getName().equals("reqName2"))); + } + + @Test + public void shouldPassUpdateRequirementsForHappyScenario(){ + + List requirementDefinitions = createMockRequirementListToReturn( + createRequirement("reqName", "capType", "node", "source1", + "6", "11")); + Resource resource = createComponent(true); + resource.setComponentType(ComponentTypeEnum.RESOURCE); + validateUserRoles(Role.ADMIN, Role.DESIGNER); + when(toscaOperationFacade.getToscaElement(anyString(), any(ComponentParametersView.class))) + .thenReturn(Either.left(resource)); + when(toscaOperationFacade.getParentComponents(anyString())) + .thenReturn(Either.right(StorageOperationStatus.NOT_FOUND)); + Either, ResponseFormat> capabilities = requirementsBusinessLogicMock + .updateRequirements(componentId, requirementDefinitions, user, + "updateRequirements", true); + Assert.assertTrue(capabilities.isLeft()); + Assert.assertTrue(capabilities.left().value().stream().anyMatch(requirementDefinition -> + requirementDefinition.getMaxOccurrences().equals("11"))); + } + + @Test + public void shouldPassDeleteRequirementsForHappyScenario(){ + Resource resource = createComponent(true); + resource.setComponentType(ComponentTypeEnum.RESOURCE); + validateUserRoles(Role.ADMIN, Role.DESIGNER); + when(toscaOperationFacade.getToscaElement(anyString(), any(ComponentParametersView.class))) + .thenReturn(Either.left(resource)); + when(toscaOperationFacade.getParentComponents(anyString())) + .thenReturn(Either.right(StorageOperationStatus.NOT_FOUND)); + Either deleteRequirementEither + = requirementsBusinessLogicMock.deleteRequirement(componentId, requirementId, user, true); + Assert.assertTrue(deleteRequirementEither.isLeft()); + + } + + @Test + public void shouldPassUpdateRequirementCapabilityUpdateWhenCapabilityNotExist(){ + + List requirementDefinitions + = createMockRequirementListToReturn(createRequirement( + "reqName", "capTypeUpdate", "node", "source1", + "6", "11")); + Resource resource = createComponent(true); + resource.setComponentType(ComponentTypeEnum.RESOURCE); + validateUserRoles(Role.ADMIN, Role.DESIGNER); + when(toscaOperationFacade.getToscaElement(anyString(), any(ComponentParametersView.class))) + .thenReturn(Either.left(resource)); + when(toscaOperationFacade.getParentComponents(anyString())) + .thenReturn(Either.right(StorageOperationStatus.NOT_FOUND)); + Either, ResponseFormat> updateRequirements + = requirementsBusinessLogicMock.updateRequirements(componentId, requirementDefinitions, + user, "updateRequirements", true); + Assert.assertTrue(updateRequirements.isLeft()); + Assert.assertTrue(updateRequirements.left().value().stream().anyMatch(requirementDefinition -> + requirementDefinition.getMaxOccurrences().equals("11"))); + } + + @Test + public void shouldPassUpdateRequirementTypeWhenCapabilityExist(){ + + List requirementDefinitions = createMockRequirementListToReturn( + createRequirement("reqName", "capTypeUpdate1", "node", + "source1","6", "11")); + Resource resource = createComponent(true); + resource.setComponentType(ComponentTypeEnum.RESOURCE); + + RequirementDefinition requirement = createRequirement("reqName", + "capTypeUpdate1", "node", "source1", + "6", "11"); + requirement.setUniqueId("unique2"); + List requirementDefinitions1 = createMockRequirementListToReturn(requirement); + Map> requirementMap = new HashMap<>(); + requirementMap.put("capTypeUpdate1", requirementDefinitions1); + resource.getRequirements().putAll(requirementMap); + + validateUserRoles(Role.ADMIN, Role.DESIGNER); + when(toscaOperationFacade.getToscaElement(anyString(), any(ComponentParametersView.class))) + .thenReturn(Either.left(resource)); + when(toscaOperationFacade.getParentComponents(anyString())) + .thenReturn(Either.right(StorageOperationStatus.NOT_FOUND)); + Either, ResponseFormat> capabilities + = requirementsBusinessLogicMock.updateRequirements(componentId, requirementDefinitions, + user, "updateRequirements", true); + Assert.assertTrue(capabilities.isLeft()); + Assert.assertTrue(capabilities.left().value().stream().anyMatch(capabilityDefinition -> + capabilityDefinition.getMaxOccurrences().equals("11"))); + } + + @Test + public void shouldFailUpdateRequirementWhenOperationFailedInTitan(){ + List requirementDefinitions = createMockRequirementListToReturn( + createRequirement("reqName", "capType", "node", "source1", + "6", "11")); + Resource resource = createComponent(true); + resource.setComponentType(ComponentTypeEnum.RESOURCE); + validateUserRoles(Role.ADMIN, Role.DESIGNER); + when(requirementOperation.addRequirement(anyString(), anyObject())) + .thenReturn(Either.right(StorageOperationStatus.GENERAL_ERROR)); + when(requirementOperation.updateRequirement(anyString(), anyObject())) + .thenReturn(Either.right(StorageOperationStatus.GENERAL_ERROR)); + when(toscaOperationFacade.getToscaElement(anyString(), any(ComponentParametersView.class))) + .thenReturn(Either.left(resource)); + Either, ResponseFormat> capabilities = requirementsBusinessLogicMock + .updateRequirements(componentId, requirementDefinitions, user, + "updateRequirements", true); + + Assert.assertTrue(capabilities.isRight()); + } + + + @Test + public void shouldFailDeleteRequirementWhenOperationFailedInTitan(){ + Resource resource = createComponent(true); + resource.setComponentType(ComponentTypeEnum.RESOURCE); + validateUserRoles(Role.ADMIN, Role.DESIGNER); + when(toscaOperationFacade.getToscaElement(anyString(), any(ComponentParametersView.class))) + .thenReturn(Either.left(resource)); + when(toscaOperationFacade.getParentComponents(anyString())) + .thenReturn(Either.right(StorageOperationStatus.NOT_FOUND)); + when(requirementOperation.deleteRequirements(anyObject(), anyString())) + .thenReturn(StorageOperationStatus.GENERAL_ERROR); + Either deleteRequirementEither + = requirementsBusinessLogicMock.deleteRequirement(componentId, requirementId, user, true); + Assert.assertTrue(deleteRequirementEither.isRight()); + } + + @Test + public void shouldFailDeleteRequirementWhenRequirementUsedInServiceComposition(){ + Resource resource = createComponent(true); + resource.setComponentType(ComponentTypeEnum.RESOURCE); + validateUserRoles(Role.ADMIN, Role.DESIGNER); + when(toscaOperationFacade.getToscaElement(anyString(), any(ComponentParametersView.class))) + .thenReturn(Either.left(resource)); + when(toscaOperationFacade.getParentComponents(anyString())) + .thenReturn(Either.left(Collections.singletonList(createParentService()))); + Either deleteRequirementEither + = requirementsBusinessLogicMock.deleteRequirement(componentId, requirementId, user, true); + Assert.assertTrue(deleteRequirementEither.isRight()); + } + + @Test + public void shouldPassGetRequirementsForHappyScenario(){ + Resource resource = createComponent(true); + resource.setComponentType(ComponentTypeEnum.RESOURCE); + validateUserRoles(Role.ADMIN, Role.DESIGNER); + when(toscaOperationFacade.getToscaElement(anyString(), any(ComponentParametersView.class))) + .thenReturn(Either.left(resource)); + when(toscaOperationFacade.getParentComponents(anyString())) + .thenReturn(Either.right(StorageOperationStatus.NOT_FOUND)); + Either getRequirementEither + = requirementsBusinessLogicMock.getRequirement(componentId, requirementId, user, true); + Assert.assertTrue(getRequirementEither.isLeft()); + + } + + private Resource createComponent(boolean needRequirements) { + Resource resource = new Resource(); + resource.setName("Resource1"); + resource.addCategory("Network Layer 2-3", "Router"); + resource.setDescription("My short description"); + List tgs = new ArrayList<>(); + tgs.add("test"); + tgs.add(resource.getName()); + resource.setTags(tgs); + + if(needRequirements) { + List requirementDefinitions = createMockRequirementListToReturn( + createRequirement("reqName", "capType", "node", "source1", + "0", "10")); + Map> requirementsMap = new HashMap<>(); + requirementsMap.put("capType", requirementDefinitions); + resource.setRequirements(requirementsMap); + } + resource.setName(resource.getName()); + resource.setVersion("0.1"); + resource.setUniqueId(resource.getName().toLowerCase() + ":" + resource.getVersion()); + resource.setCreatorUserId(user.getUserId()); + resource.setCreatorFullName(user.getFirstName() + " " + user.getLastName()); + resource.setLifecycleState(LifecycleStateEnum.NOT_CERTIFIED_CHECKOUT); + return resource; + } + + private List createMockRequirementListToReturn(RequirementDefinition requirementDefinition) { + List requirementDefinitions = new ArrayList<>(); + requirementDefinitions.add(requirementDefinition); + return requirementDefinitions; + } + + private void validateUserRoles(Role... roles) { + List listOfRoles = Stream.of(roles).collect(Collectors.toList()); + } + + private RequirementDefinition createRequirement(String name, String capability, String node, + String relationship, String minOccurrences, + String maxOccurrences) { + RequirementDefinition requirementDefinition = new RequirementDefinition(); + requirementDefinition.setName(name); + requirementDefinition.setCapability(capability); + requirementDefinition.setNode(node); + requirementDefinition.setRelationship(relationship); + requirementDefinition.setMaxOccurrences(maxOccurrences); + requirementDefinition.setMinOccurrences(minOccurrences); + requirementDefinition.setUniqueId(requirementId); + + return requirementDefinition; + } + + private Service createParentService() { + Service service = new Service(); + service.setUniqueId("serviceUniqueId"); + + List resourceInstancesRelations = new ArrayList<>(); + RequirementCapabilityRelDef relationDef = new RequirementCapabilityRelDef(); + relationDef.setFromNode("fromNode"); + relationDef.setToNode("toNode"); + + List relationships = new ArrayList<>(); + CapabilityRequirementRelationship capabilityRequirementRelationship = new CapabilityRequirementRelationship(); + + RelationshipInfo relation = new RelationshipInfo(); + relation.setCapabilityUid("capabilityId"); + relation.setRequirementUid(requirementId); + capabilityRequirementRelationship.setRelation(relation); + + relationships.add(capabilityRequirementRelationship); + relationDef.setRelationships(relationships); + resourceInstancesRelations.add(relationDef); + + service.setComponentInstancesRelations(resourceInstancesRelations); + + return service; + } + +} \ No newline at end of file diff --git a/catalog-be/src/test/java/org/openecomp/sdc/be/components/validation/CapabilitiesValidationTest.java b/catalog-be/src/test/java/org/openecomp/sdc/be/components/validation/CapabilitiesValidationTest.java new file mode 100644 index 0000000000..b4856b96b4 --- /dev/null +++ b/catalog-be/src/test/java/org/openecomp/sdc/be/components/validation/CapabilitiesValidationTest.java @@ -0,0 +1,159 @@ +/* + * Copyright © 2016-2018 European Support Limited + * + * 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. + */ + +package org.openecomp.sdc.be.components.validation; + + +import fj.data.Either; +import org.junit.Assert; +import org.junit.Before; +import org.junit.Test; +import org.mockito.Mockito; +import org.mockito.MockitoAnnotations; +import org.openecomp.sdc.be.components.impl.ResponseFormatManager; +import org.openecomp.sdc.be.model.CapabilityDefinition; +import org.openecomp.sdc.be.model.Component; +import org.openecomp.sdc.be.model.Resource; +import org.openecomp.sdc.exception.ResponseFormat; + +import java.util.ArrayList; +import java.util.Collections; +import java.util.HashMap; +import java.util.List; +import java.util.Map; + +import static org.mockito.ArgumentMatchers.any; +import static org.mockito.Mockito.when; + +public class CapabilitiesValidationTest { + private ResponseFormatManager responseFormatManagerMock; + private final Component component = createComponent(); + private final CapabilitiesValidationUtilTest capabilitiesValidationUtilTest = new CapabilitiesValidationUtilTest(); + @Before + public void init() { + MockitoAnnotations.initMocks(this); + responseFormatManagerMock = Mockito.mock(ResponseFormatManager.class); + when(responseFormatManagerMock.getResponseFormat(any())).thenReturn(new ResponseFormat()); + when(responseFormatManagerMock.getResponseFormat(any(), any())).thenReturn(new ResponseFormat()); + when(responseFormatManagerMock.getResponseFormat(any(), any(), any())).thenReturn(new ResponseFormat()); + } + + @Test + public void shouldPassCapabilitiesValidationForHappyScenario() { + List capabilityDefinitions = new ArrayList<>(); + capabilityDefinitions.add(createCapability("capName", "capDesc", "capType", "source1", + "0", "10")); + Either validateCapabilitiesResponseEither = capabilitiesValidationUtilTest + .validateCapabilities(capabilityDefinitions, component, false); + Assert.assertTrue(validateCapabilitiesResponseEither.isLeft()); + } + + @Test + public void shouldFailWhenCapabilityNameAlreadyExist() { + List capabilityDefinitions = new ArrayList<>(); + capabilityDefinitions.add(createCapability("capNameC", "capDesc", "capType", "source1", + "0", "10")); + Either validateCapabilitiesResponseEither = capabilitiesValidationUtilTest + .validateCapabilities(capabilityDefinitions, component, false); + Assert.assertTrue(validateCapabilitiesResponseEither.isRight()); + } + + @Test + public void shouldFailWhenCapabilityNameEmpty() { + List capabilityDefinitions = new ArrayList<>(); + capabilityDefinitions.add(createCapability("", "capDesc", "capType", "source1", + "0", "10")); + Either validateCapabilitiesResponseEither = capabilitiesValidationUtilTest + .validateCapabilities(capabilityDefinitions, component, false); + Assert.assertTrue(validateCapabilitiesResponseEither.isRight()); + } + + @Test + public void shouldFailWhenCapabilityTypeEmpty() { + List capabilityDefinitions = new ArrayList<>(); + capabilityDefinitions.add(createCapability("capName1", "capDesc", "", "source1", + "0", "10")); + Either validateCapabilitiesResponseEither = capabilitiesValidationUtilTest + .validateCapabilities(capabilityDefinitions, component, false); + Assert.assertTrue(validateCapabilitiesResponseEither.isRight()); + } + + @Test + public void shouldFailWhenCapabilityMaxOccurrencesLessThanMinOccurrences() { + List capabilityDefinitions = new ArrayList<>(); + capabilityDefinitions.add(createCapability("capName1", "capDesc", "capType", "source1", + "111", "3")); + Either validateCapabilitiesResponseEither = capabilitiesValidationUtilTest + .validateCapabilities(capabilityDefinitions, component, false); + Assert.assertTrue(validateCapabilitiesResponseEither.isRight()); + } + + @Test + public void shouldFailWhenCapabilityNotFoundForUpdate() { + List capabilityDefinitions = new ArrayList<>(); + CapabilityDefinition capabilityToUpdate = createCapability("capName1", "capDesc", "capType", "source1", + "1", "3"); + capabilityToUpdate.setUniqueId("uniqueId2"); + + capabilityDefinitions.add(capabilityToUpdate); + Either validateCapabilitiesResponseEither = capabilitiesValidationUtilTest + .validateCapabilities(capabilityDefinitions, component, true); + Assert.assertTrue(validateCapabilitiesResponseEither.isRight()); + } + + private CapabilityDefinition createCapability(String name, String description, String type, + String validSourceTypes, String minOccurrences, + String maxOccurrences) { + CapabilityDefinition capabilityDefinition = new CapabilityDefinition(); + capabilityDefinition.setName(name); + capabilityDefinition.setDescription(description); + capabilityDefinition.setType(type); + capabilityDefinition.setValidSourceTypes(Collections.singletonList(validSourceTypes)); + capabilityDefinition.setMaxOccurrences(maxOccurrences); + capabilityDefinition.setMinOccurrences(minOccurrences); + capabilityDefinition.setUniqueId("uniqueId"); + + + return capabilityDefinition; + } + + private Resource createComponent() { + Resource resource = new Resource(); + resource.setName("Resource1"); + resource.addCategory("Network Layer 2-3", "Router"); + resource.setDescription("My short description"); + List tgs = new ArrayList<>(); + tgs.add("test"); + tgs.add(resource.getName()); + resource.setTags(tgs); + + List capabilityDefinitions = new ArrayList<>(); + capabilityDefinitions.add(createCapability("capNameC", "capDesc", "capType", "source1", + "0", "10")); + Map> capabilityMap = new HashMap<>(); + capabilityMap.put("capTypeC", capabilityDefinitions); + resource.setCapabilities(capabilityMap); + + return resource; + } + + private class CapabilitiesValidationUtilTest extends CapabilitiesValidation { + + protected ResponseFormatManager getResponseFormatManager() { + return responseFormatManagerMock; + } + } +} \ No newline at end of file diff --git a/catalog-be/src/test/java/org/openecomp/sdc/be/components/validation/RequirementValidationTest.java b/catalog-be/src/test/java/org/openecomp/sdc/be/components/validation/RequirementValidationTest.java new file mode 100644 index 0000000000..ff9bcd1bac --- /dev/null +++ b/catalog-be/src/test/java/org/openecomp/sdc/be/components/validation/RequirementValidationTest.java @@ -0,0 +1,156 @@ +/* + * Copyright © 2016-2018 European Support Limited + * + * 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. + */ + +package org.openecomp.sdc.be.components.validation; + +import fj.data.Either; +import org.junit.Assert; +import org.junit.Before; +import org.junit.Test; +import org.mockito.Mockito; +import org.mockito.MockitoAnnotations; +import org.openecomp.sdc.be.components.impl.ResponseFormatManager; +import org.openecomp.sdc.be.model.Component; +import org.openecomp.sdc.be.model.RequirementDefinition; +import org.openecomp.sdc.be.model.Resource; +import org.openecomp.sdc.exception.ResponseFormat; + +import java.util.ArrayList; +import java.util.HashMap; +import java.util.List; +import java.util.Map; + +import static org.mockito.ArgumentMatchers.any; +import static org.mockito.Mockito.when; + +public class RequirementValidationTest { + private ResponseFormatManager responseFormatManagerMock; + private final Component component = createComponent(); + private final RequirementValidationUtilTest requirementValidationUtilTest = new RequirementValidationUtilTest(); + @Before + public void init() { + MockitoAnnotations.initMocks(this); + responseFormatManagerMock = Mockito.mock(ResponseFormatManager.class); + when(responseFormatManagerMock.getResponseFormat(any())).thenReturn(new ResponseFormat()); + when(responseFormatManagerMock.getResponseFormat(any(), any())).thenReturn(new ResponseFormat()); + when(responseFormatManagerMock.getResponseFormat(any(), any(), any())).thenReturn(new ResponseFormat()); + } + + @Test + public void shouldPassRequirementsValidationForHappyScenario() { + List requirementsDefinitions = new ArrayList<>(); + requirementsDefinitions.add(createRequirement("reqName", "capType", "node", "source1", + "0", "10")); + Either validateRequirementsResponseEither = requirementValidationUtilTest + .validateRequirements(requirementsDefinitions, component, false); + Assert.assertTrue(validateRequirementsResponseEither.isLeft()); + } + + @Test + public void shouldFailWhenRequirementNameAlreadyExist() { + List requirementsDefinitions = new ArrayList<>(); + requirementsDefinitions.add(createRequirement("ReqNameC", "capType", "node", "source1", + "0", "10")); + Either validateRequirementsResponseEither = requirementValidationUtilTest + .validateRequirements(requirementsDefinitions, component, false); + Assert.assertTrue(validateRequirementsResponseEither.isRight()); + } + + @Test + public void shouldFailWhenRequirementNameEmpty() { + List requirementsDefinitions = new ArrayList<>(); + requirementsDefinitions.add(createRequirement("", "capType", "node", "source1", + "0", "10")); + Either validateRequirementsResponseEither = requirementValidationUtilTest + .validateRequirements(requirementsDefinitions, component, false); + Assert.assertTrue(validateRequirementsResponseEither.isRight()); + } + + @Test + public void shouldFailWhenRequirementCapabilityEmpty() { + List requirementsDefinitions = new ArrayList<>(); + requirementsDefinitions.add(createRequirement("reqName1", "", "node", "source1", + "0", "10")); + Either validateRequirementsResponseEither = requirementValidationUtilTest + .validateRequirements(requirementsDefinitions, component, false); + Assert.assertTrue(validateRequirementsResponseEither.isRight()); + } + + @Test + public void shouldFailWhenRequirementMaxOccurrencesLessThanMinOccurrences() { + List requirementsDefinitions = new ArrayList<>(); + requirementsDefinitions.add(createRequirement("reqName1", "capType", "node", "source1", + "111", "3")); + Either validateRequirementsResponseEither = requirementValidationUtilTest + .validateRequirements(requirementsDefinitions, component, false); + Assert.assertTrue(validateRequirementsResponseEither.isRight()); + } + + @Test + public void shouldFailWhenRequirementNotFoundForUpdate() { + List requirementsDefinitions = new ArrayList<>(); + RequirementDefinition requirementsToUpdate = createRequirement("reqName1", "capType", "node", "source1", + "1", "3"); + requirementsToUpdate.setUniqueId("uniqueId2"); + + requirementsDefinitions.add(requirementsToUpdate); + Either validateRequirementsResponseEither = requirementValidationUtilTest + .validateRequirements(requirementsDefinitions, component, true); + Assert.assertTrue(validateRequirementsResponseEither.isRight()); + } + + private RequirementDefinition createRequirement(String name, String capability, String node, + String relationship, String minOccurrences, + String maxOccurrences) { + RequirementDefinition requirementDefinition = new RequirementDefinition(); + requirementDefinition.setName(name); + requirementDefinition.setCapability(capability); + requirementDefinition.setNode(node); + requirementDefinition.setRelationship(relationship); + requirementDefinition.setMaxOccurrences(maxOccurrences); + requirementDefinition.setMinOccurrences(minOccurrences); + requirementDefinition.setUniqueId("uniqueId"); + + return requirementDefinition; + } + + private Resource createComponent() { + Resource resource = new Resource(); + resource.setName("Resource1"); + resource.addCategory("Network Layer 2-3", "Router"); + resource.setDescription("My short description"); + List tgs = new ArrayList<>(); + tgs.add("test"); + tgs.add(resource.getName()); + resource.setTags(tgs); + + List requirementsDefinitions = new ArrayList<>(); + requirementsDefinitions.add(createRequirement("ReqNameC", "reqDesc", "capType", "source1", + "0", "10")); + Map> requirementsMap = new HashMap<>(); + requirementsMap.put("capTypeC", requirementsDefinitions); + resource.setRequirements(requirementsMap); + + return resource; + } + + private class RequirementValidationUtilTest extends RequirementValidation { + + protected ResponseFormatManager getResponseFormatManager() { + return responseFormatManagerMock; + } + } +} \ No newline at end of file diff --git a/catalog-be/src/test/java/org/openecomp/sdc/be/servlets/TypesUploadServletTest.java b/catalog-be/src/test/java/org/openecomp/sdc/be/servlets/TypesUploadServletTest.java index e97f13c716..807ded8f5c 100644 --- a/catalog-be/src/test/java/org/openecomp/sdc/be/servlets/TypesUploadServletTest.java +++ b/catalog-be/src/test/java/org/openecomp/sdc/be/servlets/TypesUploadServletTest.java @@ -125,7 +125,7 @@ public class TypesUploadServletTest extends JerseyTest { protected ResourceConfig configure() { ResourceConfig resourceConfig = new ResourceConfig() - .register(new TypesUploadServlet(importManager, null, null, null, null, null)); + .register(new TypesUploadServlet(importManager, null, null, null, null, null, null)); resourceConfig.register(MultiPartFeature.class); resourceConfig.register(new AbstractBinder() { -- cgit 1.2.3-korg