/* * Copyright (c) 2018 AT&T Intellectual Property. * * 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.model.jsonjanusgraph.operations; import static org.assertj.core.api.Assertions.assertThat; import static org.junit.jupiter.api.Assertions.assertEquals; import static org.junit.jupiter.api.Assertions.assertFalse; import static org.junit.jupiter.api.Assertions.assertNotNull; import static org.junit.jupiter.api.Assertions.assertNull; import static org.junit.jupiter.api.Assertions.assertSame; import static org.junit.jupiter.api.Assertions.assertThrows; import static org.junit.jupiter.api.Assertions.assertTrue; import static org.mockito.ArgumentMatchers.any; import static org.mockito.ArgumentMatchers.anyInt; import static org.mockito.ArgumentMatchers.anyMap; import static org.mockito.ArgumentMatchers.anyString; import static org.mockito.ArgumentMatchers.eq; import static org.mockito.Mockito.doReturn; 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.EnumMap; import java.util.HashMap; import java.util.HashSet; import java.util.List; import java.util.Map; import java.util.Set; import java.util.stream.Collectors; import java.util.stream.IntStream; import org.apache.commons.collections.map.HashedMap; import org.apache.commons.lang3.tuple.ImmutablePair; import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; import org.junit.jupiter.api.extension.ExtendWith; import org.mockito.ArgumentCaptor; import org.mockito.ArgumentMatchers; 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.config.ComponentType; import org.openecomp.sdc.be.dao.api.ActionStatus; import org.openecomp.sdc.be.dao.api.exception.JanusGraphException; import org.openecomp.sdc.be.dao.janusgraph.HealingJanusGraphDao; import org.openecomp.sdc.be.dao.janusgraph.JanusGraphOperationStatus; import org.openecomp.sdc.be.dao.jsongraph.GraphVertex; import org.openecomp.sdc.be.dao.jsongraph.types.EdgeLabelEnum; import org.openecomp.sdc.be.dao.jsongraph.types.JsonParseFlagEnum; import org.openecomp.sdc.be.dao.jsongraph.types.VertexTypeEnum; import org.openecomp.sdc.be.datatypes.elements.AttributeDataDefinition; import org.openecomp.sdc.be.datatypes.elements.DataTypeDataDefinition; import org.openecomp.sdc.be.datatypes.elements.ListCapabilityDataDefinition; import org.openecomp.sdc.be.datatypes.elements.ListRequirementDataDefinition; import org.openecomp.sdc.be.datatypes.elements.RequirementDataDefinition; import org.openecomp.sdc.be.datatypes.enums.ComponentTypeEnum; import org.openecomp.sdc.be.datatypes.enums.GraphPropertyEnum; import org.openecomp.sdc.be.datatypes.enums.JsonPresentationFields; import org.openecomp.sdc.be.datatypes.enums.NodeTypeEnum; import org.openecomp.sdc.be.datatypes.enums.OriginTypeEnum; import org.openecomp.sdc.be.datatypes.enums.PromoteVersionEnum; import org.openecomp.sdc.be.datatypes.enums.ResourceTypeEnum; import org.openecomp.sdc.be.model.AttributeDefinition; import org.openecomp.sdc.be.model.Component; import org.openecomp.sdc.be.model.ComponentInstance; import org.openecomp.sdc.be.model.ComponentInstanceAttribute; import org.openecomp.sdc.be.model.ComponentInstanceOutput; import org.openecomp.sdc.be.model.ComponentInstanceProperty; import org.openecomp.sdc.be.model.ComponentParametersView; import org.openecomp.sdc.be.model.DataTypeDefinition; import org.openecomp.sdc.be.model.InputDefinition; import org.openecomp.sdc.be.model.LifecycleStateEnum; import org.openecomp.sdc.be.model.OutputDefinition; import org.openecomp.sdc.be.model.PolicyDefinition; import org.openecomp.sdc.be.model.PropertyDefinition; 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.jsonjanusgraph.datamodel.NodeType; import org.openecomp.sdc.be.model.jsonjanusgraph.datamodel.TopologyTemplate; import org.openecomp.sdc.be.model.jsonjanusgraph.datamodel.ToscaElement; import org.openecomp.sdc.be.model.jsonjanusgraph.datamodel.ToscaElementTypeEnum; import org.openecomp.sdc.be.model.jsonjanusgraph.operations.exception.OperationException; import org.openecomp.sdc.be.model.jsonjanusgraph.utils.ModelConverter; import org.openecomp.sdc.be.model.operations.StorageException; import org.openecomp.sdc.be.model.operations.api.IGraphLockOperation; import org.openecomp.sdc.be.model.operations.api.StorageOperationStatus; @ExtendWith(MockitoExtension.class) class ToscaOperationFacadeTest { private static final String COMPONENT_ID = "componentId"; private static final String PROPERTY1_NAME = "prop1"; private static final String PROPERTY1_TYPE = "string"; private static final String PROPERTY2_NAME = "prop2"; private static final String PROPERTY2_TYPE = "integer"; private static final String ICON_NAME = "icon"; private static final String SERVICE_MODEL_NAME = "Test_Service"; private static final String SERVICE_PROXY_INSTANCE0_NAME = "testservice_proxy0"; private static final String SERVICE_SUBSTITUTION_INSTANCE0_NAME = "testservice0"; @InjectMocks private ToscaOperationFacade testInstance; @Mock private HealingJanusGraphDao janusGraphDaoMock; @Mock private TopologyTemplateOperation topologyTemplateOperationMock; @Mock private NodeTypeOperation nodeTypeOperationMock; @Mock private NodeTemplateOperation nodeTemplateOperationMock; @Mock private IGraphLockOperation graphLockOperationMock; @BeforeEach void setUp() throws Exception { testInstance = new ToscaOperationFacade(); MockitoAnnotations.openMocks(this); } @SuppressWarnings("unchecked") @Test void fetchMetaDataByResourceType() throws Exception { ArgumentCaptor criteriaCapture = ArgumentCaptor.forClass(Map.class); ArgumentCaptor criteriaNotCapture = ArgumentCaptor.forClass(Map.class); ComponentParametersView dataFilter = new ComponentParametersView(); List mockVertices = getMockVertices(2); Either, JanusGraphOperationStatus> returnedVertices = Either.left(mockVertices); when(janusGraphDaoMock.getByCriteria(eq(null), criteriaCapture.capture(), criteriaNotCapture.capture(), eq(JsonParseFlagEnum.ParseMetadata))) .thenReturn(returnedVertices); when(topologyTemplateOperationMock.getToscaElement(mockVertices.get(0), dataFilter)).thenReturn(Either.left(getResourceToscaElement("0"))); when(topologyTemplateOperationMock.getToscaElement(mockVertices.get(1), dataFilter)).thenReturn(Either.left(getResourceToscaElement("1"))); Either, StorageOperationStatus> fetchedComponents = testInstance .fetchMetaDataByResourceType(ResourceTypeEnum.VF.getValue(), dataFilter); verifyCriteriaForHighestVersionAndVfResourceType(criteriaCapture); verifyCriteriaNotIsDeleted(criteriaNotCapture); assertTrue(fetchedComponents.isLeft()); List cmpts = fetchedComponents.left().value(); assertEquals(2, cmpts.size()); assertEquals("0", cmpts.get(0).getUniqueId()); assertEquals("1", cmpts.get(1).getUniqueId()); } private void verifyCriteriaForHighestVersionAndVfResourceType(ArgumentCaptor criteriaCapture) { Map criteria = (Map) criteriaCapture.getValue(); assertEquals(2, criteria.size()); assertEquals("VF", criteria.get(GraphPropertyEnum.RESOURCE_TYPE)); assertEquals(true, criteria.get(GraphPropertyEnum.IS_HIGHEST_VERSION)); } private void verifyCriteriaNotIsDeleted(ArgumentCaptor criteriaNotCapture) { Map notCriteria = (Map) criteriaNotCapture.getValue(); assertEquals(1, notCriteria.size()); assertEquals(true, notCriteria.get(GraphPropertyEnum.IS_DELETED)); } @SuppressWarnings("unchecked") @Test void fetchMetaDataByResourceType_failedToGetData() throws Exception { when(janusGraphDaoMock.getByCriteria(eq(null), anyMap(), anyMap(), eq(JsonParseFlagEnum.ParseMetadata))).thenReturn(Either.right( JanusGraphOperationStatus.GENERAL_ERROR)); Either, StorageOperationStatus> fetchedComponents = testInstance .fetchMetaDataByResourceType(ResourceTypeEnum.VF.getValue(), new ComponentParametersView()); assertTrue(fetchedComponents.isRight()); assertEquals(StorageOperationStatus.GENERAL_ERROR, fetchedComponents.right().value()); } @Test void associatePolicyToComponentSuccessTest() { Either result = associatePolicyToComponentWithStatus(StorageOperationStatus.OK); assertTrue(result.isLeft()); } @Test void associatePolicyToComponentFailureTest() { Either result = associatePolicyToComponentWithStatus(StorageOperationStatus.BAD_REQUEST); assertTrue(result.isRight() && result.right().value() == StorageOperationStatus.BAD_REQUEST); } @Test void updatePolicyOfComponentSuccessTest() { Either result = updatePolicyOfComponentWithStatus(StorageOperationStatus.OK); assertTrue(result.isLeft()); } @Test void updatePolicyOfComponentFailureTest() { Either result = updatePolicyOfComponentWithStatus(StorageOperationStatus.NOT_FOUND); assertTrue(result.isRight() && result.right().value() == StorageOperationStatus.NOT_FOUND); } @Test void removePolicyFromComponentSuccessTest() { removePolicyFromComponentWithStatus(StorageOperationStatus.OK); } @Test void removePolicyFromComponentFailureTest() { removePolicyFromComponentWithStatus(StorageOperationStatus.NOT_FOUND); } @Test void testFindLastCertifiedToscaElementByUUID() { Either result; Component component = new Resource(); List list = new ArrayList<>(); GraphVertex graphVertex = getTopologyTemplateVertex(); list.add(graphVertex); Map props = new EnumMap<>(GraphPropertyEnum.class); props.put(GraphPropertyEnum.UUID, component.getUUID()); props.put(GraphPropertyEnum.STATE, LifecycleStateEnum.CERTIFIED.name()); props.put(GraphPropertyEnum.IS_HIGHEST_VERSION, true); ToscaElement toscaElement = getToscaElementForTest(); when(topologyTemplateOperationMock.getToscaElement(ArgumentMatchers.eq(graphVertex), any(ComponentParametersView.class))) .thenReturn(Either.left(toscaElement)); when(janusGraphDaoMock.getByCriteria(ModelConverter.getVertexType(component), props)).thenReturn(Either.left(list)); result = testInstance.findLastCertifiedToscaElementByUUID(component); Component resultComp = result.left().value(); assertEquals(resultComp.getToscaType(), ToscaElementTypeEnum.TOPOLOGY_TEMPLATE.getValue()); } @Test void testLatestComponentByToscaResourceName() { Either result; TopologyTemplate toscaElement = new TopologyTemplate(); toscaElement.setComponentType(ComponentTypeEnum.SERVICE); List list = new ArrayList<>(); GraphVertex graphVertex = getTopologyTemplateVertex(); Map props = new HashMap<>(); props.put(GraphPropertyEnum.VERSION, "1.0"); graphVertex.setMetadataProperties(props); list.add(graphVertex); Map propertiesToMatch = new EnumMap<>(GraphPropertyEnum.class); Map propertiesNotToMatch = new EnumMap<>(GraphPropertyEnum.class); propertiesToMatch.put(GraphPropertyEnum.TOSCA_RESOURCE_NAME, "toscaResourceName"); propertiesToMatch.put(GraphPropertyEnum.IS_HIGHEST_VERSION, true); propertiesNotToMatch.put(GraphPropertyEnum.IS_DELETED, true); when(janusGraphDaoMock.getByCriteria(null, propertiesToMatch, propertiesNotToMatch, JsonParseFlagEnum.ParseAll, null)) .thenReturn(Either.left(list)); when(topologyTemplateOperationMock.getToscaElement(ArgumentMatchers.eq(graphVertex), any(ComponentParametersView.class))) .thenReturn(Either.left(toscaElement)); result = testInstance.getFullLatestComponentByToscaResourceName("toscaResourceName"); assertTrue(result.isLeft()); } @Test void testValidateCsarUuidUniqueness() { StorageOperationStatus result; String csarUUID = "csarUUID"; Map properties = new EnumMap<>(GraphPropertyEnum.class); properties.put(GraphPropertyEnum.CSAR_UUID, csarUUID); List vertexList = new ArrayList<>(); when(janusGraphDaoMock.getByCriteria(null, properties, JsonParseFlagEnum.ParseMetadata)).thenReturn(Either.left(vertexList)); result = testInstance.validateCsarUuidUniqueness(csarUUID); assertEquals(StorageOperationStatus.ENTITY_ALREADY_EXISTS, result); } @Test void testValidateCsarUuidUnique_true() { StorageOperationStatus result; String csarUUID = "csarUUID"; Map properties = new EnumMap<>(GraphPropertyEnum.class); properties.put(GraphPropertyEnum.CSAR_UUID, csarUUID); when(janusGraphDaoMock.getByCriteria(null, properties, JsonParseFlagEnum.ParseMetadata)).thenReturn(Either.right( JanusGraphOperationStatus.NOT_FOUND)); result = testInstance.validateCsarUuidUniqueness(csarUUID); assertEquals(StorageOperationStatus.OK, result); } @Test void testGetLatestCertiNodeTypeByToscaResourceName() { Either result; String toscaResourceName = "resourceName"; String uniqueId = "uniqueId"; GraphVertex graphVertex = getTopologyTemplateVertex(); graphVertex.setJsonMetadataField(JsonPresentationFields.VERSION, "1.0"); graphVertex.setUniqueId(uniqueId); List vertexList = new ArrayList<>(); vertexList.add(graphVertex); Map props = new EnumMap<>(GraphPropertyEnum.class); props.put(GraphPropertyEnum.TOSCA_RESOURCE_NAME, toscaResourceName); props.put(GraphPropertyEnum.IS_HIGHEST_VERSION, true); props.put(GraphPropertyEnum.STATE, LifecycleStateEnum.CERTIFIED.name()); ToscaElement topologyTemplate = new TopologyTemplate(); topologyTemplate.setComponentType(ComponentTypeEnum.SERVICE); when(janusGraphDaoMock.getByCriteria(VertexTypeEnum.NODE_TYPE, props, JsonParseFlagEnum.ParseMetadata)).thenReturn(Either.left(vertexList)); when(janusGraphDaoMock.getVertexById(uniqueId, JsonParseFlagEnum.ParseAll)).thenReturn(Either.left(graphVertex)); when(topologyTemplateOperationMock.getToscaElement(any(GraphVertex.class), any(ComponentParametersView.class))) .thenReturn(Either.left(topologyTemplate)); result = testInstance.getLatestCertifiedNodeTypeByToscaResourceName(toscaResourceName); assertTrue(result.isLeft()); } @Test void testValidateCompExists() { Either result; String componentId = "componentId"; GraphVertex graphVertex = getTopologyTemplateVertex(); when(janusGraphDaoMock.getVertexById(componentId, JsonParseFlagEnum.NoParse)).thenReturn(Either.left(graphVertex)); result = testInstance.validateComponentExists(componentId); assertEquals(true, result.left().value()); } @Test void testValidateCompExists_NotFound() { Either result; String componentId = "componentId"; when(janusGraphDaoMock.getVertexById(componentId, JsonParseFlagEnum.NoParse)).thenReturn(Either.right( JanusGraphOperationStatus.NOT_FOUND)); result = testInstance.validateComponentExists(componentId); assertEquals(false, result.left().value()); } @Test void testValidateToscaResourceNameExists() { Either result; String templateName = "templateName"; Map properties = new EnumMap<>(GraphPropertyEnum.class); properties.put(GraphPropertyEnum.TOSCA_RESOURCE_NAME, templateName); List graphVertexList = new ArrayList<>(); GraphVertex graphVertex = getTopologyTemplateVertex(); graphVertexList.add(graphVertex); when(janusGraphDaoMock.getByCriteria(null, properties, JsonParseFlagEnum.ParseMetadata)).thenReturn(Either.left(graphVertexList)); result = testInstance.validateToscaResourceNameExists(templateName); assertEquals(true, result.left().value()); } @Test void testValidateToscaResourceNameExists_false() { Either result; String templateName = "templateName"; Map properties = new EnumMap<>(GraphPropertyEnum.class); properties.put(GraphPropertyEnum.TOSCA_RESOURCE_NAME, templateName); List graphVertexList = new ArrayList<>(); GraphVertex graphVertex = getTopologyTemplateVertex(); graphVertexList.add(graphVertex); when(janusGraphDaoMock.getByCriteria(null, properties, JsonParseFlagEnum.ParseMetadata)).thenReturn(Either.right( JanusGraphOperationStatus.NOT_FOUND)); result = testInstance.validateToscaResourceNameExists(templateName); assertEquals(false, result.left().value()); } @Test void testOverrideComponent() { Either result; Resource resource = new Resource(); String id = "id"; resource.setUniqueId(id); GraphVertex graphVertex = getTopologyTemplateVertex(); graphVertex.setLabel(VertexTypeEnum.TOPOLOGY_TEMPLATE); NodeType nodeType = new NodeType(); nodeType.setComponentType(ComponentTypeEnum.RESOURCE); ToscaElement toscaElement = new TopologyTemplate(); toscaElement.setComponentType(ComponentTypeEnum.SERVICE); when(janusGraphDaoMock.getVertexById(id, JsonParseFlagEnum.NoParse)).thenReturn(Either.left(graphVertex)); when(janusGraphDaoMock.getParentVertex(graphVertex, EdgeLabelEnum.VERSION, JsonParseFlagEnum.NoParse)).thenReturn(Either.left(graphVertex)); when(topologyTemplateOperationMock.deleteToscaElement(graphVertex)).thenReturn(Either.left(toscaElement)); when(nodeTypeOperationMock.createToscaElement(any(ToscaElement.class))).thenReturn(Either.left(nodeType)); when(janusGraphDaoMock.getVertexById(null, JsonParseFlagEnum.NoParse)).thenReturn(Either.left(graphVertex)); when(janusGraphDaoMock.createEdge(graphVertex, graphVertex, EdgeLabelEnum.VERSION, null)).thenReturn( JanusGraphOperationStatus.OK); result = testInstance.overrideComponent(resource, resource); assertTrue(result.isLeft()); } @Test void testGetToscaElement() { Either result; String id = "id"; GraphVertex graphVertex = getTopologyTemplateVertex(); ToscaElement toscaElement = getToscaElementForTest(); when(janusGraphDaoMock.getVertexById(id, JsonParseFlagEnum.ParseAll)).thenReturn(Either.left(graphVertex)); when(topologyTemplateOperationMock.getToscaElement(any(GraphVertex.class), any(ComponentParametersView.class))) .thenReturn(Either.left(toscaElement)); result = testInstance.getToscaElement(id, JsonParseFlagEnum.ParseAll); assertTrue(result.isLeft()); } @Test void testDeleteService_ServiceInUse() { String invariantUUID = "12345"; String serviceUid = "1"; GraphVertex service1 = getTopologyTemplateVertex(); service1.setUniqueId(serviceUid); List allResourcesToDelete = new ArrayList<>(); allResourcesToDelete.add(service1); Map propertiesToMatch = new EnumMap<>(GraphPropertyEnum.class); propertiesToMatch.put(GraphPropertyEnum.INVARIANT_UUID, invariantUUID); ToscaElement toscaElement = getToscaElementForTest(); toscaElement.setUniqueId(serviceUid); String service2Name = "service2Name"; Map service2MetadataJson = new HashMap<>(); service2MetadataJson.put(GraphPropertyEnum.COMPONENT_TYPE.getProperty(), ComponentType.SERVICE); service2MetadataJson.put(GraphPropertyEnum.NAME.getProperty(), service2Name); String service2Uid = "2"; GraphVertex usingService = getTopologyTemplateVertex(); usingService.setUniqueId(service2Uid); usingService.setMetadataJson(service2MetadataJson); List inUseBy = new ArrayList<>(); inUseBy.add(usingService); when(janusGraphDaoMock.findAllVertexByInvariantUuid(invariantUUID, Collections.emptyMap())). thenReturn(allResourcesToDelete); when(janusGraphDaoMock.getParentVertices(any(GraphVertex.class), any(), eq(JsonParseFlagEnum.ParseAll))). thenReturn(Either.left(inUseBy)).thenReturn(Either.left(inUseBy)); final OperationException actualException = assertThrows(OperationException.class, () -> testInstance.deleteService(invariantUUID, true)); assertEquals(ActionStatus.COMPONENT_IN_USE_BY_ANOTHER_COMPONENT, actualException.getActionStatus()); assertEquals(actualException.getParams()[0], ComponentType.SERVICE + " " + service2Name); } @Test void testDeleteService_WithOneVersion() { String invariantUUID = "12345"; String serviceUid = "1"; Map propertiesToMatch = new EnumMap<>(GraphPropertyEnum.class); propertiesToMatch.put(GraphPropertyEnum.INVARIANT_UUID, invariantUUID); GraphVertex service1 = getTopologyTemplateVertex(); service1.setUniqueId(serviceUid); List allResourcesToDelete = new ArrayList<>(); allResourcesToDelete.add(service1); ToscaElement toscaElement = getToscaElementForTest(); toscaElement.setUniqueId(serviceUid); List affectedComponentIds = new ArrayList<>(); affectedComponentIds.add(service1.getUniqueId()); when(janusGraphDaoMock.findAllVertexByInvariantUuid(invariantUUID, Collections.emptyMap())). thenReturn(allResourcesToDelete); when(janusGraphDaoMock.getParentVertices(eq(service1), any(), eq(JsonParseFlagEnum.ParseAll))). thenReturn(Either.right(JanusGraphOperationStatus.OK)); when(graphLockOperationMock.lockComponent(service1.getUniqueId(), NodeTypeEnum.Service)). thenReturn(StorageOperationStatus.OK); when(topologyTemplateOperationMock.deleteToscaElement(service1)).thenReturn(Either.left(toscaElement)); assertEquals(affectedComponentIds, testInstance.deleteService(invariantUUID, true)); } @Test void testDeleteService_WithTwoVersions() { String invariantUUID = "12345"; String serviceUid = "1"; String service2Uid = "2"; GraphVertex service = getTopologyTemplateVertex(); service.setUniqueId(serviceUid); GraphVertex serviceV2 = getTopologyTemplateVertex(); serviceV2.setUniqueId(service2Uid); ToscaElement toscaElement = getToscaElementForTest(); toscaElement.setUniqueId(serviceUid); ToscaElement toscaElement2 = getToscaElementForTest(); toscaElement2.setUniqueId(service2Uid); List affectedComponentIds = new ArrayList<>(); affectedComponentIds.add(service.getUniqueId()); affectedComponentIds.add(serviceV2.getUniqueId()); List allResourcesToDelete = new ArrayList<>(); allResourcesToDelete.add(service); allResourcesToDelete.add(serviceV2); Map propertiesToMatch = new EnumMap<>(GraphPropertyEnum.class); propertiesToMatch.put(GraphPropertyEnum.INVARIANT_UUID, invariantUUID); when(janusGraphDaoMock.findAllVertexByInvariantUuid(invariantUUID, Collections.emptyMap())). thenReturn(allResourcesToDelete); when(janusGraphDaoMock.getParentVertices(any(GraphVertex.class), any(), eq(JsonParseFlagEnum.ParseAll))). thenReturn(Either.right(JanusGraphOperationStatus.OK)); when(graphLockOperationMock.lockComponent(service.getUniqueId(), NodeTypeEnum.Service)). thenReturn(StorageOperationStatus.OK); when(graphLockOperationMock.lockComponent(serviceV2.getUniqueId(), NodeTypeEnum.Service)). thenReturn(StorageOperationStatus.OK); when(topologyTemplateOperationMock.deleteToscaElement(service)).thenReturn(Either.left(toscaElement)); when(topologyTemplateOperationMock.deleteToscaElement(serviceV2)).thenReturn(Either.left(toscaElement)); assertEquals(affectedComponentIds, testInstance.deleteService(invariantUUID, true)); } @Test void testDeleteService_FailDelete() { String invariantUUID = "12345"; String serviceUid = "1"; GraphVertex service = getTopologyTemplateVertex(); service.setUniqueId(serviceUid); ToscaElement toscaElement = getToscaElementForTest(); toscaElement.setUniqueId(serviceUid); List allResourcesToDelete = new ArrayList<>(); allResourcesToDelete.add(service); when(janusGraphDaoMock.findAllVertexByInvariantUuid(invariantUUID, Collections.emptyMap())). thenReturn(allResourcesToDelete); when(janusGraphDaoMock.getParentVertices(eq(service), any(), eq(JsonParseFlagEnum.ParseAll))). thenReturn(Either.right(JanusGraphOperationStatus.OK)); when(graphLockOperationMock.lockComponent(service.getUniqueId(), NodeTypeEnum.Service)). thenReturn(StorageOperationStatus.OK); when(topologyTemplateOperationMock.deleteToscaElement(service)) .thenReturn(Either.right(StorageOperationStatus.NOT_FOUND)); StorageException actualException = assertThrows(StorageException.class, () -> testInstance.deleteService(invariantUUID, false)); assertEquals(StorageOperationStatus.NOT_FOUND, actualException.getStorageOperationStatus()); assertEquals(0, actualException.getParams().length); } @Test void testDeleteService_NotFound() { String invariantUUID = "12345"; when(janusGraphDaoMock.findAllVertexByInvariantUuid(invariantUUID, Collections.emptyMap())). thenReturn(Collections.emptyList()); assertEquals(0, testInstance.deleteService(invariantUUID, true).size()); } @Test void testDeleteService_GeneralErrorInJanusGraphDao() { String invariantUUID = "12345"; JanusGraphException janusException = new JanusGraphException(JanusGraphOperationStatus.GENERAL_ERROR, "General error"); when(janusGraphDaoMock.findAllVertexByInvariantUuid(invariantUUID, Collections.emptyMap())). thenThrow(janusException); StorageException actualException = assertThrows(StorageException.class, () -> testInstance.deleteService(invariantUUID, false)); assertEquals(StorageOperationStatus.GENERAL_ERROR, actualException.getStorageOperationStatus()); assertEquals(0, actualException.getParams().length); } @Test void testMarkComponentToDelete() { StorageOperationStatus result; Component component = new Resource(); String id = "id"; component.setUniqueId(id); GraphVertex graphVertex = getTopologyTemplateVertex(); when(janusGraphDaoMock.getVertexById(id, JsonParseFlagEnum.ParseAll)).thenReturn(Either.left(graphVertex)); when(nodeTypeOperationMock.markComponentToDelete(graphVertex)).thenReturn(Either.left(graphVertex)); result = testInstance.markComponentToDelete(component); assertEquals(StorageOperationStatus.OK, result); } @Test void testDelToscaComponent() { Either result; String componentId = "compId"; GraphVertex graphVertex = getTopologyTemplateVertex(); ToscaElement toscaElement = getToscaElementForTest(); when(janusGraphDaoMock.getVertexById(componentId, JsonParseFlagEnum.ParseAll)).thenReturn(Either.left(graphVertex)); when(topologyTemplateOperationMock.deleteToscaElement(graphVertex)).thenReturn(Either.left(toscaElement)); result = testInstance.deleteToscaComponent(componentId); assertTrue(result.isLeft()); } @Test void testGetLatestByToscaResourceName() { Either result; String toscaResourceName = "name"; String model = "testModel"; ToscaElement toscaElement = getToscaElementForTest(); Map propertiesToMatch = new EnumMap<>(GraphPropertyEnum.class); propertiesToMatch.put(GraphPropertyEnum.TOSCA_RESOURCE_NAME, toscaResourceName); propertiesToMatch.put(GraphPropertyEnum.IS_HIGHEST_VERSION, true); Map propertiesNotToMatch = new EnumMap<>(GraphPropertyEnum.class); propertiesNotToMatch.put(GraphPropertyEnum.IS_DELETED, true); List graphVertexList = new ArrayList<>(); GraphVertex graphVertex = getTopologyTemplateVertex(); graphVertex.setUniqueId(toscaResourceName); Map props = new HashMap<>(); props.put(GraphPropertyEnum.VERSION, "1.0"); graphVertex.setMetadataProperties(props); graphVertexList.add(graphVertex); when(janusGraphDaoMock.getByCriteria(null, propertiesToMatch, propertiesNotToMatch, JsonParseFlagEnum.ParseMetadata, model)) .thenReturn(Either.left(graphVertexList)); when(topologyTemplateOperationMock.getToscaElement(any(GraphVertex.class), any(ComponentParametersView.class))) .thenReturn(Either.left(toscaElement)); result = testInstance.getLatestByToscaResourceName(toscaResourceName, model); assertTrue(result.isLeft()); } @Test void testGetLatestResourceByToscaResourceName() { Either result; String toscaResourceName = "org.openecomp.resource.vf"; ToscaElement toscaElement = getToscaElementForTest(); Map propertiesToMatch = new EnumMap<>(GraphPropertyEnum.class); propertiesToMatch.put(GraphPropertyEnum.TOSCA_RESOURCE_NAME, toscaResourceName); propertiesToMatch.put(GraphPropertyEnum.IS_HIGHEST_VERSION, true); if (!toscaResourceName.contains("org.openecomp.resource.vf")) { propertiesToMatch.put(GraphPropertyEnum.STATE, LifecycleStateEnum.CERTIFIED.name()); } List graphVertexList = new ArrayList<>(); GraphVertex graphVertex = getTopologyTemplateVertex(); graphVertex.setUniqueId(toscaResourceName); Map props = new HashMap<>(); props.put(JsonPresentationFields.VERSION, "1.0"); graphVertex.setJsonMetadataField(JsonPresentationFields.VERSION, props.get(JsonPresentationFields.VERSION)); graphVertexList.add(graphVertex); when(janusGraphDaoMock.getByCriteria(VertexTypeEnum.TOPOLOGY_TEMPLATE, propertiesToMatch, JsonParseFlagEnum.ParseMetadata)) .thenReturn(Either.left(graphVertexList)); when(topologyTemplateOperationMock.getToscaElement(any(GraphVertex.class), any(ComponentParametersView.class))) .thenReturn(Either.left(toscaElement)); when(janusGraphDaoMock.getVertexById(toscaResourceName, JsonParseFlagEnum.ParseAll)).thenReturn(Either.left(graphVertex)); result = testInstance.getLatestResourceByToscaResourceName(toscaResourceName); assertTrue(result.isLeft()); } @Test void testGetFollowed() { Either, StorageOperationStatus> result; String userId = "id"; Set lifecycleStates = new HashSet<>(); Set lastStateStates = new HashSet<>(); lifecycleStates.add(LifecycleStateEnum.NOT_CERTIFIED_CHECKIN); lifecycleStates.add(LifecycleStateEnum.NOT_CERTIFIED_CHECKOUT); lifecycleStates.add(LifecycleStateEnum.CERTIFIED); ComponentTypeEnum componentType = ComponentTypeEnum.RESOURCE; List toscaEleList = new ArrayList<>(); ToscaElement toscaElement = getToscaElementForTest(); toscaEleList.add(toscaElement); when(nodeTypeOperationMock.getFollowedComponent(userId, lifecycleStates, lastStateStates, componentType)) .thenReturn(Either.left(toscaEleList)); result = testInstance.getFollowed(userId, lifecycleStates, lastStateStates, componentType); assertTrue(result.isLeft()); assertEquals(1, result.left().value().size()); } @Test void testGetBySystemNameAndVersion() { Either result; String sysName = "sysName"; String version = "version"; ComponentTypeEnum componentTypeEnum = ComponentTypeEnum.RESOURCE; ToscaElement toscaElement = getToscaElementForTest(); List componentVertices = new ArrayList<>(); GraphVertex graphVertex = getTopologyTemplateVertex(); componentVertices.add(graphVertex); Map propertiesToMatch = new EnumMap<>(GraphPropertyEnum.class); Map propertiesNotToMatch = new EnumMap<>(GraphPropertyEnum.class); propertiesToMatch.put(GraphPropertyEnum.SYSTEM_NAME, sysName); propertiesToMatch.put(GraphPropertyEnum.COMPONENT_TYPE, componentTypeEnum.name()); propertiesToMatch.put(GraphPropertyEnum.VERSION, version); propertiesNotToMatch.put(GraphPropertyEnum.IS_DELETED, true); when(janusGraphDaoMock.getByCriteria(null, propertiesToMatch, propertiesNotToMatch, JsonParseFlagEnum.ParseAll)) .thenReturn(Either.left(componentVertices)); when(topologyTemplateOperationMock.getToscaElement(any(GraphVertex.class), any(ComponentParametersView.class))) .thenReturn(Either.left(toscaElement)); result = testInstance.getBySystemNameAndVersion(componentTypeEnum, sysName, version); assertTrue(result.isLeft()); assertNotNull(result.left().value()); } @Test void testGetCompByNameAndVersion() { Either result; ComponentTypeEnum componentType = ComponentTypeEnum.RESOURCE; String name = "name"; String version = "1.0"; JsonParseFlagEnum parseFlag = JsonParseFlagEnum.ParseAll; List graphVertexList = new ArrayList<>(); GraphVertex graphVertex = getTopologyTemplateVertex(); graphVertexList.add(graphVertex); ToscaElement toscaElement = getToscaElementForTest(); Map hasProperties = new EnumMap<>(GraphPropertyEnum.class); Map hasNotProperties = new EnumMap<>(GraphPropertyEnum.class); hasProperties.put(GraphPropertyEnum.NAME, name); hasProperties.put(GraphPropertyEnum.VERSION, version); hasNotProperties.put(GraphPropertyEnum.IS_DELETED, true); hasProperties.put(GraphPropertyEnum.COMPONENT_TYPE, componentType.name()); when(janusGraphDaoMock.getByCriteria(null, hasProperties, hasNotProperties, parseFlag)).thenReturn(Either.left(graphVertexList)); when(topologyTemplateOperationMock.getToscaElement(any(GraphVertex.class), any(ComponentParametersView.class))) .thenReturn(Either.left(toscaElement)); result = testInstance.getComponentByNameAndVersion(componentType, name, version, parseFlag); assertTrue(result.isLeft()); } private ToscaElement getToscaElementForTest() { ToscaElement toscaElement = new TopologyTemplate(); toscaElement.setComponentType(ComponentTypeEnum.RESOURCE); return toscaElement; } @Test void addDataTypesToComponentSuccessTest() { Either, StorageOperationStatus> result = addDataTypesToComponentWithStatus(StorageOperationStatus.OK); assertTrue(result.isLeft()); } @Test void addDataTypesToComponentFailureTest_BadRequest() { Either, StorageOperationStatus> result = addDataTypesToComponentWithStatus(StorageOperationStatus.BAD_REQUEST); assertTrue(result.isRight() && result.right().value() == StorageOperationStatus.BAD_REQUEST); } private Either, StorageOperationStatus> addDataTypesToComponentWithStatus(StorageOperationStatus status) { Map dataTypes = new HashMap<>(); String componentId = "componentid"; String Id = "id"; PropertyDefinition noDefaultProp = new PropertyDefinition(); noDefaultProp.setName("noDefaultProp"); PropertyDefinition prop1 = new PropertyDefinition(); prop1.setDefaultValue("def1"); prop1.setName("prop1"); PropertyDefinition prop2 = new PropertyDefinition(); prop2.setType("dataType1"); prop2.setName("prop2"); PropertyDefinition prop3 = new PropertyDefinition(); prop3.setDefaultValue("def3"); prop3.setName("prop3"); DataTypeDefinition noDefaultValue = new DataTypeDefinition(); noDefaultValue.setProperties(Collections.singletonList(noDefaultProp)); noDefaultValue.setDerivedFromName("name0"); DataTypeDefinition dataType1 = new DataTypeDefinition(); dataType1.setProperties(Arrays.asList(prop1, prop3)); dataType1.setName("name1"); dataType1.setDerivedFromName("derivedfromname1"); DataTypeDefinition dataType2 = new DataTypeDefinition(); dataType2.setDerivedFrom(dataType1); dataType2.setName("name2"); dataType2.setDerivedFromName("derivedfromname2"); DataTypeDefinition dataType3 = new DataTypeDefinition(); dataType3.setProperties(Collections.singletonList(prop2)); dataType3.setDerivedFrom(noDefaultValue); dataType3.setName("name3"); dataType3.setDerivedFromName("derivedfromname3"); dataTypes.put("noDefault", noDefaultValue); dataTypes.put("dataType1", dataType1); dataTypes.put("dataType2", dataType2); dataTypes.put("dataType3", dataType3); GraphVertex vertex; if (status == StorageOperationStatus.OK) { vertex = getTopologyTemplateVertex(); } else { vertex = getNodeTypeVertex(); } Either getVertexEither = Either.left(vertex); when(janusGraphDaoMock.getVertexById(componentId, JsonParseFlagEnum.NoParse)).thenReturn(getVertexEither); when(topologyTemplateOperationMock.addToscaDataToToscaElement(eq(vertex), eq(EdgeLabelEnum.DATA_TYPES), eq(VertexTypeEnum.DATA_TYPES), anyMap(), eq(JsonPresentationFields.NAME))).thenReturn(status); return testInstance.addDataTypesToComponent(dataTypes, componentId); } @Test void testDataTypesToComponentFailureTest_NotFound() { Either, StorageOperationStatus> result; String componentId = "componentId"; GraphVertex vertex = getNodeTypeVertex(); Map dataTypes = new HashMap<>(); when(janusGraphDaoMock.getVertexById(componentId, JsonParseFlagEnum.NoParse)).thenReturn(Either.right( JanusGraphOperationStatus.NOT_FOUND)); result = testInstance.addDataTypesToComponent(dataTypes, componentId); assertTrue(result.isRight() && result.right().value() == StorageOperationStatus.NOT_FOUND); } @Test void testDeleteDataTypeOfComponent() { Component component = new Resource(); String id = "id"; component.setUniqueId(id); DataTypeDefinition dataType1 = new DataTypeDefinition(); dataType1.setName("name1"); Map dataTypeDataMap = new HashMap<>(); dataTypeDataMap.put("datatype1", dataType1); List dataTypeMap = dataTypeDataMap.values().stream().map(e -> { return new DataTypeDefinition(e); }).collect(Collectors.toList()); component.setDataTypes(dataTypeMap); GraphVertex graphVertex = getTopologyTemplateVertex(); assertNull(testInstance.deleteDataTypeOfComponent(component, "datatype1")); } @Test void testAddComponentInstancePropertiesToComponent() { // set up component object Component component = new Resource(); component.setUniqueId(COMPONENT_ID); List instanceProps = new ArrayList<>(); ComponentInstanceProperty instanceProp = new ComponentInstanceProperty(); instanceProp.setName(PROPERTY1_NAME); instanceProp.setType(PROPERTY1_TYPE); instanceProps.add(instanceProp); instanceProp = new ComponentInstanceProperty(); instanceProp.setName(PROPERTY2_NAME); instanceProp.setType(PROPERTY2_TYPE); instanceProps.add(instanceProp); Map> instancePropsMap = Collections.singletonMap(COMPONENT_ID, instanceProps); component.setComponentInstancesProperties(Collections.singletonMap(COMPONENT_ID, new ArrayList<>())); when(nodeTemplateOperationMock.addComponentInstanceProperty(any(), any(), any())) .thenReturn(StorageOperationStatus.OK); Either>, StorageOperationStatus> result = testInstance.addComponentInstancePropertiesToComponent(component, instancePropsMap); assertTrue(result.isLeft()); verify(nodeTemplateOperationMock, times(2)).addComponentInstanceProperty(any(), any(), any()); List resultProps = result.left().value().get(COMPONENT_ID); assertTrue(resultProps.stream().anyMatch(e -> e.getName().equals(PROPERTY1_NAME))); assertTrue(resultProps.stream().anyMatch(e -> e.getName().equals(PROPERTY2_NAME))); } @Test void testAddComponentInstanceToTopologyTemplate_ServiceProxy() { Component containerComponent = new Service(); Component originalComponent = new Service(); ComponentInstance componentInstance = new ComponentInstance(); ComponentInstance existingComponentInstance = new ComponentInstance(); User user = new User(); containerComponent.setComponentType(ComponentTypeEnum.SERVICE); originalComponent.setComponentType(ComponentTypeEnum.SERVICE); originalComponent.setIcon(ICON_NAME); componentInstance.setOriginType(OriginTypeEnum.ServiceProxy); componentInstance.setSourceModelName(SERVICE_MODEL_NAME); List existingInstances = new ArrayList<>(); existingComponentInstance.setNormalizedName(SERVICE_PROXY_INSTANCE0_NAME); existingInstances.add(existingComponentInstance); containerComponent.setComponentInstances(existingInstances); when(nodeTemplateOperationMock .addComponentInstanceToTopologyTemplate(any(), any(), eq("1"), eq(componentInstance), eq(false), eq(user))) .thenReturn(Either.left(new ImmutablePair<>(new TopologyTemplate(), COMPONENT_ID))); TopologyTemplate topologyTemplate = new TopologyTemplate(); // preset COMPONENT_TYPE field for internal ModelConverter call topologyTemplate.setMetadataValue(JsonPresentationFields.COMPONENT_TYPE, ComponentTypeEnum.SERVICE.name()); when(topologyTemplateOperationMock.getToscaElement(containerComponent.getUniqueId())) .thenReturn(Either.left(topologyTemplate)); Either, StorageOperationStatus> result = testInstance.addComponentInstanceToTopologyTemplate( containerComponent, originalComponent, componentInstance, false, user); assertTrue(result.isLeft()); assertEquals(ICON_NAME, componentInstance.getIcon()); assertEquals(COMPONENT_ID, result.left().value().getRight()); // the instance counter must be 1 because the service proxy instance with suffix 0 already exists. verify(nodeTemplateOperationMock, times(1)) .addComponentInstanceToTopologyTemplate(any(), any(), eq("1"), eq(componentInstance), eq(false), eq(user)); } @Test void testAddComponentInstanceToTopologyTemplate_ServiceSubstitution() { Component containerComponent = new Service(); Component originalComponent = new Service(); ComponentInstance componentInstance = new ComponentInstance(); ComponentInstance existingComponentInstance = new ComponentInstance(); User user = new User(); containerComponent.setComponentType(ComponentTypeEnum.SERVICE); originalComponent.setComponentType(ComponentTypeEnum.SERVICE); originalComponent.setIcon(ICON_NAME); componentInstance.setOriginType(OriginTypeEnum.ServiceSubstitution); componentInstance.setSourceModelName(SERVICE_MODEL_NAME); List existingInstances = new ArrayList<>(); existingComponentInstance.setNormalizedName(SERVICE_SUBSTITUTION_INSTANCE0_NAME); existingInstances.add(existingComponentInstance); containerComponent.setComponentInstances(existingInstances); when(nodeTemplateOperationMock .addComponentInstanceToTopologyTemplate(any(), any(), eq("1"), eq(componentInstance), eq(false), eq(user))) .thenReturn(Either.left(new ImmutablePair<>(new TopologyTemplate(), COMPONENT_ID))); TopologyTemplate topologyTemplate = new TopologyTemplate(); topologyTemplate.setMetadataValue(JsonPresentationFields.COMPONENT_TYPE, ComponentTypeEnum.SERVICE.name()); when(topologyTemplateOperationMock.getToscaElement(containerComponent.getUniqueId())) .thenReturn(Either.left(topologyTemplate)); Either, StorageOperationStatus> result = testInstance.addComponentInstanceToTopologyTemplate( containerComponent, originalComponent, componentInstance, false, user); assertTrue(result.isLeft()); assertEquals(ICON_NAME, componentInstance.getIcon()); assertEquals(COMPONENT_ID, result.left().value().getRight()); verify(nodeTemplateOperationMock, times(1)) .addComponentInstanceToTopologyTemplate(any(), any(), eq("1"), eq(componentInstance), eq(false), eq(user)); } @Test void testUpdateComponentInstanceRequirement() { String containerComponentId = "containerComponentId"; String componentInstanceUniqueId = "componentInstanceUniqueId"; RequirementDataDefinition requirementDataDefinition = Mockito.mock(RequirementDataDefinition.class); when(nodeTemplateOperationMock.updateComponentInstanceRequirement(containerComponentId, componentInstanceUniqueId, requirementDataDefinition)) .thenReturn(StorageOperationStatus.OK); StorageOperationStatus result = testInstance .updateComponentInstanceRequirement(containerComponentId, componentInstanceUniqueId, requirementDataDefinition); assertEquals(StorageOperationStatus.OK, result); verify(nodeTemplateOperationMock, times(1)) .updateComponentInstanceRequirement(containerComponentId, componentInstanceUniqueId, requirementDataDefinition); } @Test void associateCapabilitiesToServiceFailureTest() { StorageOperationStatus result = associateCapabilitiesToServiceWithStatus(StorageOperationStatus.BAD_REQUEST); assertSame(StorageOperationStatus.BAD_REQUEST, result); } @Test void associateCapabilitiesToServiceSuccessTest() { StorageOperationStatus result = associateCapabilitiesToServiceWithStatus(StorageOperationStatus.OK); assertSame(StorageOperationStatus.OK, result); } private StorageOperationStatus associateCapabilitiesToServiceWithStatus(StorageOperationStatus status) { Map capabilitiesMap = new HashedMap(); String componentId = "componentid"; ListCapabilityDataDefinition listCapabilityDataDefinition1 = new ListCapabilityDataDefinition(); capabilitiesMap.put("capabilities1", listCapabilityDataDefinition1); GraphVertex vertex; if (status == StorageOperationStatus.OK) { vertex = getTopologyTemplateVertex(); } else { vertex = getNodeTypeVertex(); } Either getVertexEither = Either.left(vertex); when(janusGraphDaoMock.getVertexById(componentId, JsonParseFlagEnum.NoParse)).thenReturn(getVertexEither); when(topologyTemplateOperationMock.associateElementToData(eq(vertex), eq(VertexTypeEnum.CAPABILITIES), eq(EdgeLabelEnum.CAPABILITIES), anyMap())).thenReturn(Either.right(status)); return testInstance.associateCapabilitiesToService(capabilitiesMap, componentId); } @Test void associateRequirementsToServiceFailureTest() { StorageOperationStatus result = associateRequirementsToServiceWithStatus(StorageOperationStatus.BAD_REQUEST); assertSame(StorageOperationStatus.BAD_REQUEST, result); } @Test void associateRequirementsToServiceSuccessTest() { StorageOperationStatus result = associateRequirementsToServiceWithStatus(StorageOperationStatus.OK); assertSame(StorageOperationStatus.OK, result); } @Test void test_addOutputsToComponent() { final GraphVertex graphVertex = getTopologyTemplateVertex(); doReturn(Either.left(graphVertex)).when(janusGraphDaoMock).getVertexById(COMPONENT_ID, JsonParseFlagEnum.NoParse); doReturn(StorageOperationStatus.OK).when(topologyTemplateOperationMock) .addToscaDataToToscaElement( any(GraphVertex.class), eq(EdgeLabelEnum.OUTPUTS), eq(VertexTypeEnum.OUTPUTS), anyMap(), eq(JsonPresentationFields.NAME)); final Map outputs = new HashMap<>(); final OutputDefinition outputDefinition = new OutputDefinition(); outputs.put("mock", outputDefinition); final Either, StorageOperationStatus> result = testInstance.addOutputsToComponent(outputs, COMPONENT_ID); assertNotNull(result); assertTrue(result.isLeft()); assertFalse(result.left().value().isEmpty()); assertThat(result.left().value().get(0)).isInstanceOf(OutputDefinition.class); verify(janusGraphDaoMock, times(1)).getVertexById(COMPONENT_ID, JsonParseFlagEnum.NoParse); verify(topologyTemplateOperationMock, times(1)).addToscaDataToToscaElement( any(GraphVertex.class), eq(EdgeLabelEnum.OUTPUTS), eq(VertexTypeEnum.OUTPUTS), anyMap(), eq(JsonPresentationFields.NAME)); } @Test void test_addComponentInstanceOutputsToComponent_updateComponentInstanceOutput() { final Component component = new Resource(); component.setUniqueId(COMPONENT_ID); final Map> map = new HashMap<>(); final List componentInstanceOutputList = new ArrayList<>(); final ComponentInstanceOutput componentInstanceOutput = new ComponentInstanceOutput(); componentInstanceOutput.setComponentInstanceId(COMPONENT_ID); componentInstanceOutput.setComponentInstanceName(COMPONENT_ID); componentInstanceOutput.setName(COMPONENT_ID); componentInstanceOutputList.add(componentInstanceOutput); map.put("mock", componentInstanceOutputList); component.setComponentInstancesOutputs(map); doReturn(StorageOperationStatus.OK).when(nodeTemplateOperationMock) .updateComponentInstanceOutput(any(Component.class), anyString(), any(ComponentInstanceOutput.class)); final Either>, StorageOperationStatus> result = testInstance.addComponentInstanceOutputsToComponent(component, map); assertNotNull(result); assertTrue(result.isLeft()); assertFalse(result.left().value().isEmpty()); assertSame(result.left().value(), map); verify(nodeTemplateOperationMock, times(1)) .updateComponentInstanceOutput(any(Component.class), anyString(), any(ComponentInstanceOutput.class)); } @Test void test_addComponentInstanceOutputsToComponent_addComponentInstanceOutput() { final Component component = new Resource(); component.setUniqueId(COMPONENT_ID); Map> map = new HashMap<>(); List componentInstanceOutputList = new ArrayList<>(); ComponentInstanceOutput componentInstanceOutput = new ComponentInstanceOutput(); componentInstanceOutput.setComponentInstanceId(COMPONENT_ID); componentInstanceOutput.setComponentInstanceName(COMPONENT_ID); componentInstanceOutput.setName(COMPONENT_ID); componentInstanceOutputList.add(componentInstanceOutput); map.put("mock", componentInstanceOutputList); component.setComponentInstancesOutputs(map); map = new HashMap<>(); componentInstanceOutputList = new ArrayList<>(); componentInstanceOutput = new ComponentInstanceOutput(); componentInstanceOutput.setComponentInstanceId("mock"); componentInstanceOutput.setComponentInstanceName("mock"); componentInstanceOutput.setName("mock"); componentInstanceOutputList.add(componentInstanceOutput); map.put("mock", componentInstanceOutputList); final Either>, StorageOperationStatus> result = testInstance .addComponentInstanceOutputsToComponent(component, map); assertNotNull(result); assertTrue(result.isRight()); } @Test void test_addComponentInstanceAttributesToComponent() { final Component component = new Resource(); component.setUniqueId(COMPONENT_ID); Map> map = new HashMap<>(); List componentInstanceOutputList = new ArrayList<>(); ComponentInstanceAttribute componentInstanceAttribute = new ComponentInstanceAttribute(); componentInstanceAttribute.setComponentInstanceId(COMPONENT_ID); componentInstanceAttribute.setUniqueId(COMPONENT_ID); componentInstanceOutputList.add(componentInstanceAttribute); map.put("mock", componentInstanceOutputList); component.setComponentInstancesAttributes(map); doReturn(StorageOperationStatus.OK).when(nodeTemplateOperationMock) .updateComponentInstanceAttribute(any(Component.class), anyString(), any(ComponentInstanceAttribute.class)); final Either>, StorageOperationStatus> result = testInstance.addComponentInstanceAttributesToComponent(component, map); assertNotNull(result); assertTrue(result.isLeft()); assertFalse(result.left().value().isEmpty()); assertSame(result.left().value(), map); verify(nodeTemplateOperationMock, times(1)) .updateComponentInstanceAttribute(any(Component.class), anyString(), any(ComponentInstanceAttribute.class)); } @Test void test_updateAttributeOfComponent_success() { final GraphVertex graphVertex = getTopologyTemplateVertex(); final String componentId = "componentId"; final Component component = new Resource(); component.setUniqueId(COMPONENT_ID); doReturn(StorageOperationStatus.OK).when(nodeTypeOperationMock).updateToscaDataOfToscaElement( anyString(), eq(EdgeLabelEnum.ATTRIBUTES), eq(VertexTypeEnum.ATTRIBUTES), any(AttributeDefinition.class), eq(JsonPresentationFields.NAME)); doReturn(Either.left(graphVertex)).when(janusGraphDaoMock).getVertexById(eq(componentId), any(JsonParseFlagEnum.class)); final ToscaElement toscaElement = getToscaElementForTest(); final Map attributes = new HashMap<>(); final AttributeDefinition attributeDefinition = new AttributeDefinition(); attributeDefinition.setName("mock"); attributes.put("mock", attributeDefinition); toscaElement.setAttributes(attributes); doReturn(Either.left(toscaElement)).when(topologyTemplateOperationMock) .getToscaElement(ArgumentMatchers.eq(graphVertex), any(ComponentParametersView.class)); final Either result = testInstance.updateAttributeOfComponent(component, attributeDefinition); assertNotNull(result); } @Test void test_updateAttributeOfComponent_isNotPresent() { final GraphVertex graphVertex = getTopologyTemplateVertex(); final String componentId = "componentId"; final Component component = new Resource(); component.setUniqueId(COMPONENT_ID); doReturn(StorageOperationStatus.OK).when(nodeTypeOperationMock).updateToscaDataOfToscaElement( anyString(), eq(EdgeLabelEnum.ATTRIBUTES), eq(VertexTypeEnum.ATTRIBUTES), any(AttributeDefinition.class), eq(JsonPresentationFields.NAME)); doReturn(Either.left(graphVertex)).when(janusGraphDaoMock).getVertexById(eq(componentId), any(JsonParseFlagEnum.class)); final ToscaElement toscaElement = getToscaElementForTest(); final Map attributes = new HashMap<>(); final AttributeDefinition attributeDefinition = new AttributeDefinition(); attributeDefinition.setName("mock"); attributes.put("mock", attributeDefinition); toscaElement.setAttributes(attributes); doReturn(Either.left(toscaElement)).when(topologyTemplateOperationMock) .getToscaElement(ArgumentMatchers.eq(graphVertex), any(ComponentParametersView.class)); final AttributeDefinition attributeDefinitionOneMore = new AttributeDefinition(); attributeDefinitionOneMore.setName("Anothermock"); final Either result = testInstance.updateAttributeOfComponent(component, attributeDefinitionOneMore); assertNotNull(result); } @Test void test_updateComponentInstanceAttributes() { final GraphVertex graphVertex = getTopologyTemplateVertex(); final String componentId = "componentId"; final Component component = new Resource(); component.setUniqueId(COMPONENT_ID); final List attributes = new ArrayList<>(); final ComponentInstanceAttribute attributeDefinition = new ComponentInstanceAttribute(); attributeDefinition.setName("mock"); attributes.add(attributeDefinition); doReturn(StorageOperationStatus.OK).when(nodeTemplateOperationMock).updateComponentInstanceAttributes(component, componentId, attributes); final StorageOperationStatus result = testInstance.updateComponentInstanceAttributes(component, componentId, attributes); assertNotNull(result); assertEquals(StorageOperationStatus.OK, result); verify(nodeTemplateOperationMock, times(1)).updateComponentInstanceAttributes(component, componentId, attributes); } @Test void test_updateComponentInstanceOutputs() { final GraphVertex graphVertex = getTopologyTemplateVertex(); final String componentId = "componentId"; final Component component = new Resource(); component.setUniqueId(COMPONENT_ID); final List list = new ArrayList<>(); final ComponentInstanceOutput output = new ComponentInstanceOutput(); output.setName("mock"); list.add(output); doReturn(StorageOperationStatus.OK).when(nodeTemplateOperationMock).updateComponentInstanceOutputs(component, componentId, list); final StorageOperationStatus result = testInstance.updateComponentInstanceOutputs(component, componentId, list); assertNotNull(result); assertEquals(StorageOperationStatus.OK, result); verify(nodeTemplateOperationMock, times(1)).updateComponentInstanceOutputs(component, componentId, list); } @Test void test_deleteOutputOfResource() { final Component component = new Resource(); component.setUniqueId(COMPONENT_ID); doReturn(StorageOperationStatus.OK).when(nodeTypeOperationMock) .deleteToscaDataElement(anyString(), eq(EdgeLabelEnum.OUTPUTS), eq(VertexTypeEnum.OUTPUTS), anyString(), eq(JsonPresentationFields.NAME)); final StorageOperationStatus result = testInstance.deleteOutputOfResource(component, "mock"); assertNotNull(result); assertEquals(StorageOperationStatus.OK, result); verify(nodeTypeOperationMock, times(1)) .deleteToscaDataElement(anyString(), eq(EdgeLabelEnum.OUTPUTS), eq(VertexTypeEnum.OUTPUTS), anyString(), eq(JsonPresentationFields.NAME)); } @Test void testDeleteResource_ResourceInUse() { GraphVertex graphVertex = getTopologyTemplateVertex(); String invariantUuid = "1"; graphVertex.setUniqueId(invariantUuid); GraphVertex usingComponent = getTopologyTemplateVertex(); usingComponent.setUniqueId("2"); Map metadataJson = new HashMap<>(); metadataJson.put("COMPONENT_TYPE", "SERVICE"); metadataJson.put("NAME", "serviceName"); usingComponent.setMetadataJson(metadataJson); List inUseBy = new ArrayList<>(); inUseBy.add(usingComponent); Map metadata = new HashMap<>(); metadata.put("ex1", new Object()); graphVertex.setMetadataJson(metadata); ToscaElement toscaElement = getToscaElementForTest(); toscaElement.setUniqueId(invariantUuid); List allResourcesToDelete = new ArrayList<>(); allResourcesToDelete.add(graphVertex); when(janusGraphDaoMock.findAllVertexByInvariantUuid(invariantUuid, Collections.emptyMap())). thenReturn(allResourcesToDelete); when(janusGraphDaoMock.getParentVertices(any(GraphVertex.class), any(), eq(JsonParseFlagEnum.ParseAll))).thenReturn(Either.left(inUseBy)); final OperationException actualException = assertThrows(OperationException.class, () -> testInstance.deleteComponent(invariantUuid, NodeTypeEnum.Resource, true)); assertEquals(ActionStatus.COMPONENT_IN_USE_BY_ANOTHER_COMPONENT, actualException.getActionStatus()); } @Test void testDeleteResource_WithTwoVersions() { GraphVertex graphVertex = getTopologyTemplateVertex(); String invariantUuid = "1"; graphVertex.setUniqueId(invariantUuid); Map metadata1 = new HashMap<>(); metadata1.put("ex1", new Object()); graphVertex.setMetadataJson(metadata1); ToscaElement toscaElement1 = getToscaElementForTest(); toscaElement1.setUniqueId(invariantUuid); ToscaElement toscaElement2 = getToscaElementForTest(); toscaElement2.setUniqueId("2"); GraphVertex graphVertex2 = getTopologyTemplateVertex(); graphVertex2.setUniqueId("2"); Map metadata2 = new HashMap<>(); metadata2.put("ex2", new Object()); graphVertex.setMetadataJson(metadata2); List parentVertices = new ArrayList<>(); parentVertices.add(graphVertex2); List affectedComponentIds = new ArrayList<>(); affectedComponentIds.add(graphVertex.getUniqueId()); affectedComponentIds.add(graphVertex2.getUniqueId()); when(graphLockOperationMock.lockComponent(graphVertex.getUniqueId(), NodeTypeEnum.Resource)). thenReturn(StorageOperationStatus.OK); when(graphLockOperationMock.lockComponent(graphVertex2.getUniqueId(), NodeTypeEnum.Resource)). thenReturn(StorageOperationStatus.OK); when(topologyTemplateOperationMock.deleteToscaElement(graphVertex)).thenReturn(Either.left(toscaElement1)); when(topologyTemplateOperationMock.deleteToscaElement(graphVertex2)).thenReturn(Either.left(toscaElement2)); List allResourcesToDelete = new ArrayList<>(); allResourcesToDelete.add(graphVertex); allResourcesToDelete.add(graphVertex2); when(janusGraphDaoMock.findAllVertexByInvariantUuid(invariantUuid, Collections.emptyMap())). thenReturn(allResourcesToDelete); when(janusGraphDaoMock.getParentVertices(any(GraphVertex.class), any(), any())).thenReturn(Either.right(JanusGraphOperationStatus.OK)); assertEquals(affectedComponentIds, testInstance.deleteComponent(invariantUuid, NodeTypeEnum.Resource, false)); } @Test void testDeleteResource_WithOneVersion() { GraphVertex graphVertex = getTopologyTemplateVertex(); graphVertex.setUniqueId("1"); Map metadata = new HashMap<>(); metadata.put("ex1", new Object()); graphVertex.setMetadataJson(metadata); ToscaElement toscaElement = getToscaElementForTest(); List affectedComponentIds = new ArrayList<>(); affectedComponentIds.add(graphVertex.getUniqueId()); when(graphLockOperationMock.lockComponent(graphVertex.getUniqueId(), NodeTypeEnum.Resource)). thenReturn(StorageOperationStatus.OK); when(topologyTemplateOperationMock.deleteToscaElement(graphVertex)).thenReturn(Either.left(toscaElement)); List allResourcesToDelete = new ArrayList<>(); allResourcesToDelete.add(graphVertex); when(janusGraphDaoMock.findAllVertexByInvariantUuid(graphVertex.getUniqueId(), Collections.emptyMap())). thenReturn(allResourcesToDelete); when(janusGraphDaoMock.getParentVertices(any(GraphVertex.class), any(), any())).thenReturn(Either.right(JanusGraphOperationStatus.OK)); assertEquals(affectedComponentIds, testInstance.deleteComponent("1", NodeTypeEnum.Resource, true)); } @Test void testDeleteResource_FailDelete() { Map metadataProperties = new HashMap<>(); metadataProperties.put(GraphPropertyEnum.NAME, "graphVertex"); GraphVertex graphVertex = getTopologyTemplateVertex(); String invariantUUID = "1"; graphVertex.setUniqueId(invariantUUID); graphVertex.setMetadataProperties(metadataProperties); ToscaElement toscaElement1 = getToscaElementForTest(); toscaElement1.setUniqueId(invariantUUID); List affectedComponentIds = new ArrayList<>(); affectedComponentIds.add(graphVertex.getUniqueId()); List allResourcesToDelete = new ArrayList<>(); allResourcesToDelete.add(graphVertex); when(janusGraphDaoMock.findAllVertexByInvariantUuid(graphVertex.getUniqueId(), Collections.emptyMap())). thenReturn(allResourcesToDelete); when(graphLockOperationMock.lockComponent(graphVertex.getUniqueId(), NodeTypeEnum.Resource)). thenReturn(StorageOperationStatus.OK); when(topologyTemplateOperationMock.deleteToscaElement(graphVertex)) .thenReturn(Either.right(StorageOperationStatus.NOT_FOUND)); when(janusGraphDaoMock.getParentVertices(any(GraphVertex.class), any(), any())).thenReturn(Either.right(JanusGraphOperationStatus.OK)); StorageException actualException = assertThrows(StorageException.class, () -> testInstance.deleteComponent(invariantUUID, NodeTypeEnum.Resource, false)); assertEquals(StorageOperationStatus.NOT_FOUND, actualException.getStorageOperationStatus()); assertEquals(0, actualException.getParams().length); } @Test void testDeleteResource_NotFound() { String invariantUUID = "12345"; when(janusGraphDaoMock.findAllVertexByInvariantUuid(invariantUUID, Collections.emptyMap())).thenReturn(Collections.emptyList()); assertEquals(0, testInstance.deleteComponent(invariantUUID, NodeTypeEnum.Resource, true).size()); } @Test void testCreateAndAssociateInputs_OK() { GraphVertex vertex = getTopologyTemplateVertex(); when(janusGraphDaoMock.getVertexById(COMPONENT_ID, JsonParseFlagEnum.NoParse)).thenReturn(Either.left(vertex)); when(topologyTemplateOperationMock.associateInputsToComponent(eq(vertex), anyMap(), anyString())).thenReturn(StorageOperationStatus.OK); Map inputs = new HashMap<>(); inputs.put("1", new InputDefinition()); inputs.put("2", new InputDefinition()); final var result = testInstance.createAndAssociateInputs(inputs, COMPONENT_ID); assertTrue(result.isLeft()); assertNotNull(result.left()); assertEquals(2, result.left().value().size()); } @Test void testCreateAndAssociateInputs_NegativeAssociateInputsToComponentStatus() { GraphVertex vertex = getTopologyTemplateVertex(); when(janusGraphDaoMock.getVertexById(COMPONENT_ID, JsonParseFlagEnum.NoParse)).thenReturn(Either.left(vertex)); when(topologyTemplateOperationMock.associateInputsToComponent(eq(vertex), anyMap(), anyString())) .thenReturn(StorageOperationStatus.NOT_FOUND); final var result = testInstance.createAndAssociateInputs(new HashMap<>(), COMPONENT_ID); assertTrue(result.isRight()); assertEquals(StorageOperationStatus.NOT_FOUND, result.right().value()); } @Test void testCreateAndAssociateInputs_componentNotFound() { when(janusGraphDaoMock.getVertexById(COMPONENT_ID, JsonParseFlagEnum.NoParse)).thenReturn(Either.right(JanusGraphOperationStatus.NOT_FOUND)); final var result = testInstance.createAndAssociateInputs(new HashMap<>(), COMPONENT_ID); assertTrue(result.isRight()); assertEquals(StorageOperationStatus.NOT_FOUND, result.right().value()); } @Test void testCreateAndAssociateOutputs_OK() { GraphVertex vertex = getTopologyTemplateVertex(); when(janusGraphDaoMock.getVertexById(COMPONENT_ID, JsonParseFlagEnum.NoParse)).thenReturn(Either.left(vertex)); when(topologyTemplateOperationMock.associateOutputsToComponent(eq(vertex), anyMap(), anyString())).thenReturn(StorageOperationStatus.OK); Map outputs = new HashMap<>(); outputs.put("1", new OutputDefinition()); outputs.put("2", new OutputDefinition()); final var result = testInstance.createAndAssociateOutputs(outputs, COMPONENT_ID); assertTrue(result.isLeft()); assertNotNull(result.left()); assertEquals(2, result.left().value().size()); } @Test void testCreateAndAssociateOutputs_NegativeAssociateOutputsToComponentStatus() { GraphVertex vertex = getTopologyTemplateVertex(); when(janusGraphDaoMock.getVertexById(COMPONENT_ID, JsonParseFlagEnum.NoParse)).thenReturn(Either.left(vertex)); when(topologyTemplateOperationMock.associateOutputsToComponent(eq(vertex), anyMap(), anyString())) .thenReturn(StorageOperationStatus.NOT_FOUND); final var result = testInstance.createAndAssociateOutputs(new HashMap<>(), COMPONENT_ID); assertTrue(result.isRight()); assertEquals(StorageOperationStatus.NOT_FOUND, result.right().value()); } @Test void testCreateAndAssociateOutputs_componentNotFound() { when(janusGraphDaoMock.getVertexById(COMPONENT_ID, JsonParseFlagEnum.NoParse)).thenReturn(Either.right(JanusGraphOperationStatus.NOT_FOUND)); final var result = testInstance.createAndAssociateOutputs(new HashMap<>(), COMPONENT_ID); assertTrue(result.isRight()); assertEquals(StorageOperationStatus.NOT_FOUND, result.right().value()); } private StorageOperationStatus associateRequirementsToServiceWithStatus(StorageOperationStatus status) { Map requirementsMap = new HashedMap(); String componentId = "componentid"; ListRequirementDataDefinition listRequirementDataDefinition1 = new ListRequirementDataDefinition(); requirementsMap.put("requirements1", listRequirementDataDefinition1); GraphVertex vertex; if (status == StorageOperationStatus.OK) { vertex = getTopologyTemplateVertex(); } else { vertex = getNodeTypeVertex(); } Either getVertexEither = Either.left(vertex); when(janusGraphDaoMock.getVertexById(componentId, JsonParseFlagEnum.NoParse)).thenReturn(getVertexEither); when(topologyTemplateOperationMock.associateElementToData(eq(vertex), eq(VertexTypeEnum.REQUIREMENTS), eq(EdgeLabelEnum.REQUIREMENTS), anyMap())).thenReturn(Either.right(status)); return testInstance.associateRequirementsToService(requirementsMap, componentId); } private Either associatePolicyToComponentWithStatus(StorageOperationStatus status) { PolicyDefinition policy = new PolicyDefinition(); String componentId = "componentId"; int counter = 0; GraphVertex vertex; if (status == StorageOperationStatus.OK) { vertex = getTopologyTemplateVertex(); } else { vertex = getNodeTypeVertex(); } Either getVertexEither = Either.left(vertex); when(janusGraphDaoMock.getVertexById(componentId, JsonParseFlagEnum.ParseMetadata)).thenReturn(getVertexEither); if (status == StorageOperationStatus.OK) { when(topologyTemplateOperationMock.addPolicyToToscaElement(eq(vertex), any(PolicyDefinition.class), anyInt())).thenReturn(status); } return testInstance.associatePolicyToComponent(componentId, policy, counter); } private Either updatePolicyOfComponentWithStatus(StorageOperationStatus status) { PolicyDefinition policy = new PolicyDefinition(); String componentId = "componentId"; GraphVertex vertex = getTopologyTemplateVertex(); when(janusGraphDaoMock.getVertexById(componentId, JsonParseFlagEnum.NoParse)).thenReturn(Either.left(vertex)); when(topologyTemplateOperationMock.updatePolicyOfToscaElement(eq(vertex), any(PolicyDefinition.class))).thenReturn(status); return testInstance.updatePolicyOfComponent(componentId, policy, PromoteVersionEnum.NONE); } private void removePolicyFromComponentWithStatus(StorageOperationStatus status) { String componentId = "componentId"; String policyId = "policyId"; GraphVertex vertex = getTopologyTemplateVertex(); Either getVertexEither = Either.left(vertex); when(janusGraphDaoMock.getVertexById(componentId, JsonParseFlagEnum.NoParse)).thenReturn(getVertexEither); when(topologyTemplateOperationMock.removePolicyFromToscaElement(vertex, policyId)).thenReturn(status); StorageOperationStatus result = testInstance.removePolicyFromComponent(componentId, policyId); assertSame(result, status); } private List getMockVertices(int numOfVertices) { return IntStream.range(0, numOfVertices).mapToObj(i -> getTopologyTemplateVertex()).collect(Collectors.toList()); } private ToscaElement getResourceToscaElement(String id) { ToscaElement toscaElement = new TopologyTemplate(); toscaElement.setMetadata(new HashMap<>()); toscaElement.getMetadata().put(JsonPresentationFields.COMPONENT_TYPE.getPresentation(), "RESOURCE"); toscaElement.getMetadata().put(JsonPresentationFields.UNIQUE_ID.getPresentation(), id); return toscaElement; } private GraphVertex getTopologyTemplateVertex() { GraphVertex graphVertex = new GraphVertex(); graphVertex.setLabel(VertexTypeEnum.TOPOLOGY_TEMPLATE); return graphVertex; } private GraphVertex getNodeTypeVertex() { GraphVertex graphVertex = new GraphVertex(); graphVertex.setLabel(VertexTypeEnum.NODE_TYPE); return graphVertex; } }