diff options
Diffstat (limited to 'catalog-be')
20 files changed, 1369 insertions, 616 deletions
diff --git a/catalog-be/src/main/java/org/openecomp/sdc/be/exception/ToscaExportException.java b/catalog-be/src/main/java/org/openecomp/sdc/be/exception/ToscaExportException.java new file mode 100644 index 0000000000..66357c118e --- /dev/null +++ b/catalog-be/src/main/java/org/openecomp/sdc/be/exception/ToscaExportException.java @@ -0,0 +1,28 @@ +/* + * ============LICENSE_START======================================================= + * Copyright (C) 2021 Nordix Foundation + * ================================================================================ + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * SPDX-License-Identifier: Apache-2.0 + * ============LICENSE_END========================================================= + */ + +package org.openecomp.sdc.be.exception; + +public class ToscaExportException extends Exception { + + public ToscaExportException(String message) { + super(message); + } + +} diff --git a/catalog-be/src/main/java/org/openecomp/sdc/be/tosca/InterfacesOperationsConverter.java b/catalog-be/src/main/java/org/openecomp/sdc/be/tosca/InterfacesOperationsConverter.java index 1f1dcfed8c..8fb835e0b9 100644 --- a/catalog-be/src/main/java/org/openecomp/sdc/be/tosca/InterfacesOperationsConverter.java +++ b/catalog-be/src/main/java/org/openecomp/sdc/be/tosca/InterfacesOperationsConverter.java @@ -29,6 +29,8 @@ import java.util.List; import java.util.Map; import java.util.Map.Entry; import java.util.Objects; +import java.util.Set; +import java.util.stream.Collectors; import org.apache.commons.collections.MapUtils; import org.openecomp.sdc.be.datatypes.elements.InputDataDefinition; import org.openecomp.sdc.be.datatypes.elements.OperationDataDefinition; @@ -155,12 +157,15 @@ public class InterfacesOperationsConverter { Map<String, Object> toscaInterfaceDefinitions = new HashMap<>(); for (InterfaceDefinition interfaceDefinition : interfaces.values()) { ToscaInterfaceDefinition toscaInterfaceDefinition = new ToscaInterfaceDefinition(); - String interfaceType; + final String interfaceType; if(componentInstance != null && LOCAL_INTERFACE_TYPE.equals(interfaceDefinition.getType())) { interfaceType = DERIVED_FROM_BASE_DEFAULT + componentInstance.getSourceModelName(); } else { interfaceType = getInterfaceType(component, interfaceDefinition.getType()); } + if (componentInstance == null) { + toscaInterfaceDefinition.setType(interfaceType); + } toscaInterfaceDefinition.setType(interfaceType); final Map<String, OperationDataDefinition> operations = interfaceDefinition.getOperations(); Map<String, Object> toscaOperationMap = new HashMap<>(); @@ -204,6 +209,25 @@ public class InterfacesOperationsConverter { return toscaInterfaceDefinitions; } + public void removeInterfacesWithoutOperations(final Map<String, Object> interfaceMap) { + if (MapUtils.isEmpty(interfaceMap)) { + return; + } + final Set<String> emptyInterfaces = interfaceMap.entrySet().stream() + .filter(entry -> { + final Object value = entry.getValue(); + if (value instanceof ToscaInterfaceDefinition) { + final ToscaInterfaceDefinition interfaceDefinition = (ToscaInterfaceDefinition) value; + return MapUtils.isEmpty(interfaceDefinition.getOperations()); + } else if (value instanceof Map) { + final Map<String, Object> interfaceDefMap = (Map<String, Object>) value; + return MapUtils.isEmpty(interfaceDefMap); + } + return false; + }).map(Entry::getKey).collect(Collectors.toSet()); + emptyInterfaces.forEach(interfaceMap::remove); + } + private Map<String, Object> createInterfaceInputMap(final InterfaceDefinition interfaceDefinition, final Map<String, DataTypeDefinition> allDataTypeMap) { final Map<String, InputDataDefinition> inputMap = interfaceDefinition.getInputs(); diff --git a/catalog-be/src/main/java/org/openecomp/sdc/be/tosca/ToscaExportHandler.java b/catalog-be/src/main/java/org/openecomp/sdc/be/tosca/ToscaExportHandler.java index 6131eb5850..b8da23085d 100644 --- a/catalog-be/src/main/java/org/openecomp/sdc/be/tosca/ToscaExportHandler.java +++ b/catalog-be/src/main/java/org/openecomp/sdc/be/tosca/ToscaExportHandler.java @@ -20,17 +20,20 @@ package org.openecomp.sdc.be.tosca; -import static org.apache.commons.collections.CollectionUtils.isEmpty; import static org.apache.commons.collections.CollectionUtils.isNotEmpty; import static org.apache.commons.collections.MapUtils.isNotEmpty; import static org.openecomp.sdc.be.components.utils.PropertiesUtils.resolvePropertyValueFromInput; import static org.openecomp.sdc.be.tosca.InterfacesOperationsConverter.addInterfaceTypeElement; +import static org.openecomp.sdc.tosca.datatypes.ToscaFunctions.GET_ATTRIBUTE; +import static org.openecomp.sdc.tosca.datatypes.ToscaFunctions.GET_INPUT; +import static org.openecomp.sdc.tosca.datatypes.ToscaFunctions.GET_PROPERTY; import com.fasterxml.jackson.databind.ObjectMapper; import com.fasterxml.jackson.databind.SerializationFeature; import fj.data.Either; import java.beans.IntrospectionException; import java.util.ArrayList; +import java.util.Arrays; import java.util.Collection; import java.util.Collections; import java.util.HashMap; @@ -71,8 +74,10 @@ import org.openecomp.sdc.be.datatypes.enums.ComponentTypeEnum; import org.openecomp.sdc.be.datatypes.enums.JsonPresentationFields; import org.openecomp.sdc.be.datatypes.enums.OriginTypeEnum; import org.openecomp.sdc.be.datatypes.enums.ResourceTypeEnum; +import org.openecomp.sdc.be.exception.ToscaExportException; import org.openecomp.sdc.be.model.ArtifactDefinition; import org.openecomp.sdc.be.model.CapabilityDefinition; +import org.openecomp.sdc.be.model.CapabilityRequirementRelationship; import org.openecomp.sdc.be.model.Component; import org.openecomp.sdc.be.model.ComponentInstance; import org.openecomp.sdc.be.model.ComponentInstanceInput; @@ -95,7 +100,7 @@ import org.openecomp.sdc.be.model.jsonjanusgraph.operations.ToscaOperationFacade import org.openecomp.sdc.be.model.jsonjanusgraph.utils.ModelConverter; import org.openecomp.sdc.be.model.operations.api.StorageOperationStatus; import org.openecomp.sdc.be.model.operations.impl.InterfaceLifecycleOperation; -import org.openecomp.sdc.be.model.tosca.converters.ToscaValueBaseConverter; +import org.openecomp.sdc.be.tosca.builder.ToscaRelationshipBuilder; import org.openecomp.sdc.be.tosca.model.CapabilityFilter; import org.openecomp.sdc.be.tosca.model.NodeFilter; import org.openecomp.sdc.be.tosca.model.SubstitutionMapping; @@ -107,6 +112,8 @@ import org.openecomp.sdc.be.tosca.model.ToscaNodeTemplate; import org.openecomp.sdc.be.tosca.model.ToscaNodeType; import org.openecomp.sdc.be.tosca.model.ToscaPolicyTemplate; import org.openecomp.sdc.be.tosca.model.ToscaProperty; +import org.openecomp.sdc.be.tosca.model.ToscaPropertyAssignment; +import org.openecomp.sdc.be.tosca.model.ToscaRelationshipTemplate; import org.openecomp.sdc.be.tosca.model.ToscaRequirement; import org.openecomp.sdc.be.tosca.model.ToscaTemplate; import org.openecomp.sdc.be.tosca.model.ToscaTemplateArtifact; @@ -330,6 +337,12 @@ public class ToscaExportHandler { log.debug("node templates converted"); topologyTemplate.setNode_templates(nodeTemplates.left().value()); } + final Map<String, ToscaRelationshipTemplate> relationshipTemplatesMap = + new ToscaExportRelationshipTemplatesHandler() + .createFrom(topologyTemplate.getNode_templates()); + if (!relationshipTemplatesMap.isEmpty()) { + topologyTemplate.setRelationshipTemplates(relationshipTemplatesMap); + } SubstitutionMapping substitutionMapping = new SubstitutionMapping(); convertSubstitutionMappingFilter(component, substitutionMapping); @@ -950,30 +963,31 @@ public class ToscaExportHandler { String instanceUniqueId, Component parentComponent) { - Map<String, Object> interfaces; - + final Map<String, Object> interfaceMap; // we need to handle service proxy interfaces if (isComponentOfTypeServiceProxy(componentInstance)) { if (MapUtils.isEmpty(componentInstanceInterfaces) || !componentInstanceInterfaces.containsKey(instanceUniqueId)) { - interfaces = null; - } else { - List<ComponentInstanceInterface> currServiceInterfaces = - componentInstanceInterfaces.get(instanceUniqueId); + nodeTemplate.setInterfaces(null); + return; + } - Map<String, InterfaceDefinition> tmpInterfaces = new HashMap<>(); - currServiceInterfaces.forEach(instInterface -> tmpInterfaces.put(instInterface - .getUniqueId(), instInterface)); + final List<ComponentInstanceInterface> currServiceInterfaces = + componentInstanceInterfaces.get(instanceUniqueId); - interfaces = interfacesOperationsConverter - .getInterfacesMap(parentComponent, componentInstance, tmpInterfaces, dataTypes, true, true); - } + final Map<String, InterfaceDefinition> tmpInterfaces = new HashMap<>(); + currServiceInterfaces.forEach(instInterface -> tmpInterfaces.put(instInterface + .getUniqueId(), instInterface)); + + interfaceMap = interfacesOperationsConverter + .getInterfacesMap(parentComponent, componentInstance, tmpInterfaces, dataTypes, true, true); } else { - interfaces = + interfaceMap = getComponentInstanceInterfaceInstances(componentInstanceInterfaces, componentInstance, instanceUniqueId); } - nodeTemplate.setInterfaces(interfaces); + interfacesOperationsConverter.removeInterfacesWithoutOperations(interfaceMap); + nodeTemplate.setInterfaces(MapUtils.isEmpty(interfaceMap) ? null : interfaceMap); } private boolean isComponentOfTypeServiceProxy(ComponentInstance componentInstance) { @@ -1050,30 +1064,6 @@ public class ToscaExportHandler { } } - /** - * - */ - private void convertAndAddValue(Map<String, DataTypeDefinition> dataTypes, ComponentInstance componentInstance, - Map<String, Object> props, PropertyDefinition prop, Supplier<String> supplier) { - Object convertedValue = convertValue(dataTypes, componentInstance, prop, supplier); - if (!ToscaValueBaseConverter.isEmptyObjectValue(convertedValue)) { - props.put(prop.getName(), convertedValue); - } - } - - private <T extends PropertyDefinition> Object convertValue(Map<String, DataTypeDefinition> dataTypes, - ComponentInstance componentInstance, T input, - Supplier<String> supplier) { - log.debug("Convert property or input value {} for instance {}", input.getName(), - componentInstance.getUniqueId()); - String propertyType = input.getType(); - String innerType = null; - if (input.getSchema() != null && input.getSchema().getProperty() != null) { - innerType = input.getSchema().getProperty().getType(); - } - return propertyConvertor.convertToToscaObject(input, supplier.get(), dataTypes, true); - } - private ToscaNodeType createNodeType(Component component) { ToscaNodeType toscaNodeType = new ToscaNodeType(); if (ModelConverter.isAtomicComponent(component)) { @@ -1265,7 +1255,11 @@ public class ToscaExportHandler { proxyProperties.ifPresent(toscaNodeType::setProperties); Optional<Map<String, Object>> proxyInterfaces = getProxyNodeTypeInterfaces(proxyComponent, dataTypes); - proxyInterfaces.ifPresent(toscaNodeType::setInterfaces); + if (proxyInterfaces.isPresent()) { + final Map<String, Object> interfaceMap = proxyInterfaces.get(); + interfacesOperationsConverter.removeInterfacesWithoutOperations(interfaceMap); + toscaNodeType.setInterfaces(MapUtils.isEmpty(interfaceMap) ? null : interfaceMap); + } return toscaNodeType; } @@ -1277,92 +1271,110 @@ public class ToscaExportHandler { Component originComponent, Map<String, Component> componentCache) { - List<Map<String, ToscaTemplateRequirement>> toscaRequirements = new ArrayList<>(); - if (!addRequirements(component, componentInstance, relations, originComponent, toscaRequirements, - componentCache)) { - log.debug("Failed to convert component instance requirements for the component instance {}. ", - componentInstance.getName()); - return Either.right(ToscaError.NODE_TYPE_REQUIREMENT_ERROR); - } - if (!toscaRequirements.isEmpty()) { - nodeTypeTemplate.setRequirements(toscaRequirements); + final List<Map<String, ToscaTemplateRequirement>> toscaRequirements; + final List<RequirementCapabilityRelDef> requirementDefinitionList = filterRequirements(componentInstance, + relations); + if (isNotEmpty(requirementDefinitionList)) { + try { + toscaRequirements = buildRequirements(component, componentInstance, + requirementDefinitionList, originComponent, componentCache); + if (!toscaRequirements.isEmpty()) { + nodeTypeTemplate.setRequirements(toscaRequirements); + } + } catch (final Exception e) { + log.debug("Failed to convert component instance requirements for the component instance {}. ", + componentInstance.getName(), e); + return Either.right(ToscaError.NODE_TYPE_REQUIREMENT_ERROR); + } } log.debug("Finished to convert requirements for the node type {} ", componentInstance.getName()); return Either.left(nodeTypeTemplate); } - private boolean addRequirements(Component component, ComponentInstance componentInstance, - List<RequirementCapabilityRelDef> relations, Component originComponent, - List<Map<String, ToscaTemplateRequirement>> toscaRequirements, - Map<String, Component> componentCache) { - List<RequirementCapabilityRelDef> filteredRelations = relations.stream() + private List<Map<String, ToscaTemplateRequirement>> buildRequirements(final Component component, + final ComponentInstance componentInstance, + final List<RequirementCapabilityRelDef> filteredRelations, + final Component originComponent, + final Map<String, Component> componentCache) + throws ToscaExportException { + + final List<Map<String, ToscaTemplateRequirement>> toscaRequirements = new ArrayList<>(); + for (RequirementCapabilityRelDef relationshipDefinition : filteredRelations) { + final Map<String, ToscaTemplateRequirement> toscaTemplateRequirementMap = + buildRequirement(componentInstance, originComponent, component.getComponentInstances(), + relationshipDefinition, componentCache); + toscaRequirements.add(toscaTemplateRequirementMap); + } + + return toscaRequirements; + } + + private List<RequirementCapabilityRelDef> filterRequirements(ComponentInstance componentInstance, + List<RequirementCapabilityRelDef> relations) { + return relations.stream() .filter(p -> componentInstance.getUniqueId().equals(p.getFromNode())).collect(Collectors.toList()); - return isEmpty(filteredRelations) || - filteredRelations.stream() - .allMatch( - rel -> addRequirement(componentInstance, originComponent, component.getComponentInstances(), rel, - toscaRequirements, componentCache)); - } - - private boolean addRequirement(ComponentInstance fromInstance, Component fromOriginComponent, - List<ComponentInstance> instancesList, RequirementCapabilityRelDef rel, - List<Map<String, ToscaTemplateRequirement>> toscaRequirements, - Map<String, Component> componentCache) { - - boolean result = true; - Map<String, List<RequirementDefinition>> reqMap = fromOriginComponent.getRequirements(); - RelationshipInfo reqAndRelationshipPair = rel.getRelationships().get(0).getRelation(); - Either<Component, StorageOperationStatus> getOriginRes = null; - Optional<RequirementDefinition> reqOpt = Optional.empty(); - Component toOriginComponent = null; - Optional<CapabilityDefinition> capOpt = Optional.empty(); - - ComponentInstance toInstance = instancesList.stream().filter(i -> rel.getToNode().equals(i.getUniqueId())) + } + + private Map<String, ToscaTemplateRequirement> buildRequirement(final ComponentInstance fromInstance, + final Component fromOriginComponent, + final List<ComponentInstance> instancesList, + final RequirementCapabilityRelDef relationshipDefinition, + final Map<String, Component> componentCache) + throws ToscaExportException { + + final Map<String, List<RequirementDefinition>> reqMap = fromOriginComponent.getRequirements(); + final CapabilityRequirementRelationship capabilityRequirementRelationship = relationshipDefinition + .getRelationships().get(0); + final RelationshipInfo relationshipInfo = capabilityRequirementRelationship.getRelation(); + + final ComponentInstance toInstance = instancesList.stream() + .filter(i -> relationshipDefinition.getToNode().equals(i.getUniqueId())) .findFirst().orElse(null); if (toInstance == null) { - log.debug("Failed to find a relation from the node {} to the node {}", fromInstance.getName(), - rel.getToNode()); - result = false; - } - if (result) { - reqOpt = findRequirement(fromOriginComponent, reqMap, reqAndRelationshipPair, fromInstance.getUniqueId()); - if (!reqOpt.isPresent()) { - log.debug("Failed to find a requirement with uniqueId {} on a component with uniqueId {}", - reqAndRelationshipPair.getRequirementUid(), fromOriginComponent.getUniqueId()); - result = false; + final String errorMsg = String + .format("Failed to find a relation from the node %s to the node %s", fromInstance.getName(), + relationshipDefinition.getToNode()); + log.debug(errorMsg); + throw new ToscaExportException(errorMsg); + } + final Optional<RequirementDefinition> reqOpt = + findRequirement(fromOriginComponent, reqMap, relationshipInfo, fromInstance.getUniqueId()); + if (reqOpt.isEmpty()) { + final String errorMsg = String + .format("Failed to find a requirement with uniqueId %s on a component with uniqueId %s", + relationshipInfo.getRequirementUid(), fromOriginComponent.getUniqueId()); + log.debug(errorMsg); + throw new ToscaExportException(errorMsg); + } + final ComponentParametersView filter = new ComponentParametersView(true); + filter.setIgnoreComponentInstances(false); + filter.setIgnoreCapabilities(false); + filter.setIgnoreGroups(false); + final Either<Component, StorageOperationStatus> getOriginRes = + toscaOperationFacade.getToscaElement(toInstance.getActualComponentUid(), filter); + if (getOriginRes.isRight()) { + final String errorMsg = String.format( + "Failed to build substituted name for the requirement %s. " + + "Failed to get an origin component with uniqueId %s", + reqOpt.get().getName(), toInstance.getActualComponentUid()); + log.debug(errorMsg); + throw new ToscaExportException(errorMsg); + } + final Component toOriginComponent = getOriginRes.left().value(); + Optional<CapabilityDefinition> capOpt = toOriginComponent.getCapabilities().get(reqOpt.get().getCapability()).stream() + .filter(c -> isCapabilityBelongToRelation(relationshipInfo, c)).findFirst(); + if (capOpt.isEmpty()) { + capOpt = findCapability(relationshipInfo, toOriginComponent, fromOriginComponent, reqOpt.get()); + if (capOpt.isEmpty()) { + final String errorMsg = String + .format("Failed to find a capability with name %s on a component with uniqueId %s", + relationshipInfo.getCapability(), fromOriginComponent.getUniqueId()); + log.debug(errorMsg); + throw new ToscaExportException(errorMsg); } } - if (result) { - ComponentParametersView filter = new ComponentParametersView(true); - filter.setIgnoreComponentInstances(false); - filter.setIgnoreCapabilities(false); - filter.setIgnoreGroups(false); - getOriginRes = toscaOperationFacade.getToscaElement(toInstance.getActualComponentUid(), filter); - if (getOriginRes.isRight()) { - log.debug( - "Failed to build substituted name for the requirement {}. Failed to get an origin component with uniqueId {}", - reqOpt.get().getName(), toInstance.getActualComponentUid()); - result = false; - } - } - if (result) { - toOriginComponent = getOriginRes.left().value(); - capOpt = toOriginComponent.getCapabilities().get(reqOpt.get().getCapability()).stream() - .filter(c -> isCapabilityBelongToRelation(reqAndRelationshipPair, c)).findFirst(); - if (!capOpt.isPresent()) { - capOpt = findCapability(reqAndRelationshipPair, toOriginComponent, fromOriginComponent, reqOpt.get()); - if (!capOpt.isPresent()) { - result = false; - log.debug("Failed to find a capability with name {} on a component with uniqueId {}", - reqAndRelationshipPair.getCapability(), fromOriginComponent.getUniqueId()); - } - } - } - if (result) { - result = buildAndAddRequirement(toscaRequirements, fromOriginComponent, toOriginComponent, capOpt.get(), - reqOpt.get(), reqAndRelationshipPair, toInstance, componentCache); - } - return result; + return buildRequirement(fromOriginComponent, toOriginComponent, capOpt.get(), reqOpt.get(), + capabilityRequirementRelationship, toInstance, componentCache); } private boolean isCapabilityBelongToRelation(RelationshipInfo reqAndRelationshipPair, @@ -1383,40 +1395,50 @@ public class ToscaExportHandler { return cap; } - private boolean buildAndAddRequirement(List<Map<String, ToscaTemplateRequirement>> toscaRequirements, - Component fromOriginComponent, Component toOriginComponent, - CapabilityDefinition capability, RequirementDefinition requirement, - RelationshipInfo reqAndRelationshipPair, ComponentInstance toInstance, - Map<String, Component> componentCache) { + private Map<String, ToscaTemplateRequirement> buildRequirement(final Component fromOriginComponent, + final Component toOriginComponent, + final CapabilityDefinition capability, + final RequirementDefinition requirement, + final CapabilityRequirementRelationship capabilityRequirementRelationship, + final ComponentInstance toInstance, + final Map<String, Component> componentCache) + throws ToscaExportException { + List<String> reducedPath = capability.getPath(); if (capability.getOwnerId() != null) { reducedPath = capabilityRequirementConverter .getReducedPathByOwner(capability.getPath(), capability.getOwnerId()); } - Either<String, Boolean> buildCapNameRes = capabilityRequirementConverter.buildSubstitutedName(componentCache, - toOriginComponent, reducedPath, reqAndRelationshipPair.getCapability(), capability.getPreviousName()); - if (buildCapNameRes.isRight()) { + final RelationshipInfo relationshipInfo = capabilityRequirementRelationship.getRelation(); + final Either<String, Boolean> capabilityNameEither = capabilityRequirementConverter.buildSubstitutedName(componentCache, + toOriginComponent, reducedPath, relationshipInfo.getCapability(), capability.getPreviousName()); + if (capabilityNameEither.isRight()) { + final String errorMsg = String.format( + "Failed to build a substituted capability name for the capability with name %s on a component with uniqueId %s", + capabilityRequirementRelationship.getCapability(), toOriginComponent.getUniqueId()); log.debug( - "Failed to build a substituted capability name for the capability with name {} on a component with uniqueId {}", - reqAndRelationshipPair.getCapability(), fromOriginComponent.getUniqueId()); - return false; + errorMsg); + throw new ToscaExportException(errorMsg); } - Either<String, Boolean> buildReqNameRes = capabilityRequirementConverter + final Either<String, Boolean> requirementNameEither = capabilityRequirementConverter .buildSubstitutedName(componentCache, fromOriginComponent, - requirement.getPath(), reqAndRelationshipPair.getRequirement(), requirement.getPreviousName()); - if (buildReqNameRes.isRight()) { - log.debug( - "Failed to build a substituted requirement name for the requirement with name {} on a component with uniqueId {}", - reqAndRelationshipPair.getRequirement(), fromOriginComponent.getUniqueId()); - return false; - } - ToscaTemplateRequirement toscaRequirement = new ToscaTemplateRequirement(); - Map<String, ToscaTemplateRequirement> toscaReqMap = new HashMap<>(); + requirement.getPath(), relationshipInfo.getRequirement(), requirement.getPreviousName()); + if (requirementNameEither.isRight()) { + final String errorMsg = String.format("Failed to build a substituted requirement name for the requirement " + + "with name %s on a component with uniqueId %s", + capabilityRequirementRelationship.getRequirement(), fromOriginComponent.getUniqueId()); + log.debug(errorMsg); + throw new ToscaExportException(errorMsg); + } + final ToscaTemplateRequirement toscaRequirement = new ToscaTemplateRequirement(); + final Map<String, ToscaTemplateRequirement> toscaReqMap = new HashMap<>(); toscaRequirement.setNode(toInstance.getName()); - toscaRequirement.setCapability(buildCapNameRes.left().value()); - toscaReqMap.put(buildReqNameRes.left().value(), toscaRequirement); - toscaRequirements.add(toscaReqMap); - return true; + toscaRequirement.setCapability(capabilityNameEither.left().value()); + if (isNotEmpty(capabilityRequirementRelationship.getOperations())) { + toscaRequirement.setRelationship(new ToscaRelationshipBuilder().from(capabilityRequirementRelationship)); + } + toscaReqMap.put(requirementNameEither.left().value(), toscaRequirement); + return toscaReqMap; } private Optional<RequirementDefinition> findRequirement(Component fromOriginComponent, @@ -1673,12 +1695,83 @@ public class ToscaExportHandler { CustomRepresenter() { super(); + this.representers.put(ToscaPropertyAssignment.class, new RepresentToscaPropertyAssignment()); // null representer is exceptional and it is stored as an instance // variable. this.nullRepresenter = new RepresentNull(); } + private class RepresentToscaPropertyAssignment implements Represent { + public Node representData(Object data) { + final ToscaPropertyAssignment toscaOperationAssignment = (ToscaPropertyAssignment) data; + if (toscaOperationAssignment.getValue() instanceof String) { + final String stringValue = (String) toscaOperationAssignment.getValue(); + if (isPropertyOrAttributeFunction(stringValue)) { + return representGetAttribute(stringValue); + } + + return representScalar(Tag.STR, stringValue); + } + return represent(null); + } + + public Node representGetAttribute(final String getAttributeFunction) { + return represent(new Yaml().load(getAttributeFunction)); + } + + public boolean isPropertyOrAttributeFunction(final String value) { + try { + final Yaml yaml = new Yaml(); + final Object yamlObj = yaml.load(value); + if (!(yamlObj instanceof Map)) { + return false; + } + final Map<String, Object> getAttributeMap = (Map) yamlObj; + if (getAttributeMap.size() != 1) { + return false; + } + final List<String> functionList = Arrays + .asList(GET_ATTRIBUTE.getFunctionName(), GET_INPUT.getFunctionName(), + GET_PROPERTY.getFunctionName()); + final Optional<String> function = getAttributeMap.keySet().stream() + .filter(key -> functionList.stream().anyMatch(function1 -> function1.equals(key))).findFirst(); + + if (function.isEmpty()) { + return false; + } + final String functionName = function.get(); + final Object getAttributeValueObj = getAttributeMap.get(functionName); + if (GET_INPUT.getFunctionName().equals(functionName)) { + return validateGetInputValue(getAttributeValueObj); + } else { + return validateGetPropertyOrAttributeValue(getAttributeValueObj); + } + } catch (final Exception ignored) { + return false; + } + } + } + + public boolean validateGetInputValue(final Object valueObj) { + if (!(valueObj instanceof List) && !(valueObj instanceof String)) { + return false; + } + if (valueObj instanceof List) { + return ((List) valueObj).size() > 1; + } + + return true; + } + + public boolean validateGetPropertyOrAttributeValue(final Object valueObj) { + if (valueObj instanceof List) { + return ((List) valueObj).size() > 1; + } + + return false; + } + @Override protected NodeTuple representJavaBeanProperty(Object javaBean, Property property, Object propertyValue, Tag customTag) { @@ -1689,9 +1782,17 @@ public class ToscaExportHandler { if ("dependencies".equals(property.getName())) { return null; } + if (javaBean instanceof ToscaRelationshipTemplate && "name".equals(property.getName())) { + return null; + } + removeDefaultP(propertyValue); NodeTuple defaultNode = super.representJavaBeanProperty(javaBean, property, propertyValue, customTag); + if (javaBean instanceof ToscaTopolgyTemplate && "relationshipTemplates".equals(property.getName())) { + return new NodeTuple(representData("relationship_templates"), defaultNode.getValueNode()); + } + return "_defaultp_".equals(property.getName()) ? new NodeTuple(representData("default"), defaultNode.getValueNode()) : defaultNode; } diff --git a/catalog-be/src/main/java/org/openecomp/sdc/be/tosca/ToscaExportRelationshipTemplatesHandler.java b/catalog-be/src/main/java/org/openecomp/sdc/be/tosca/ToscaExportRelationshipTemplatesHandler.java new file mode 100644 index 0000000000..c706063d75 --- /dev/null +++ b/catalog-be/src/main/java/org/openecomp/sdc/be/tosca/ToscaExportRelationshipTemplatesHandler.java @@ -0,0 +1,83 @@ +/* + * ============LICENSE_START======================================================= + * Copyright (C) 2021 Nordix Foundation + * ================================================================================ + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * SPDX-License-Identifier: Apache-2.0 + * ============LICENSE_END========================================================= + */ + +package org.openecomp.sdc.be.tosca; + +import static org.apache.commons.collections.CollectionUtils.isEmpty; + +import java.util.Collections; +import java.util.HashMap; +import java.util.Map; +import java.util.Map.Entry; +import java.util.concurrent.atomic.AtomicInteger; +import org.apache.commons.collections.MapUtils; +import org.openecomp.sdc.be.tosca.model.ToscaNodeTemplate; +import org.openecomp.sdc.be.tosca.model.ToscaRelationship; +import org.openecomp.sdc.be.tosca.model.ToscaRelationshipTemplate; +import org.openecomp.sdc.be.tosca.model.ToscaTemplateRequirement; + +/** + * Handles the relationship_templates in the TOSCA export + */ +public class ToscaExportRelationshipTemplatesHandler { + + /** + * Creates the relationship_templates map based on the node_templates requirements. + * + * @param nodeTemplateMap the node template map + * @return the relationship_templates map + */ + public Map<String, ToscaRelationshipTemplate> createFrom(final Map<String, ToscaNodeTemplate> nodeTemplateMap) { + if (MapUtils.isEmpty(nodeTemplateMap)) { + return Collections.emptyMap(); + } + + final Map<String, ToscaRelationshipTemplate> relationshipTemplates = new HashMap<>(); + for (final Entry<String, ToscaNodeTemplate> nodeEntry : nodeTemplateMap.entrySet()) { + final ToscaNodeTemplate nodeTemplate = nodeEntry.getValue(); + if (isEmpty(nodeTemplate.getRequirements())) { + continue; + } + final AtomicInteger relationshipTemplateCount = new AtomicInteger(1); + for (final Map<String, ToscaTemplateRequirement> requirementMap : nodeTemplate.getRequirements()) { + requirementMap.entrySet().stream() + .filter(entry -> entry.getValue().isRelationshipComplexNotation()) + .forEach(requirementEntry -> { + final ToscaTemplateRequirement requirement = requirementEntry.getValue(); + final ToscaRelationship relationship = requirement.getRelationshipAsComplexType(); + final ToscaRelationshipTemplate relationshipTemplate = new ToscaRelationshipTemplate(); + relationshipTemplate.setType(relationship.getType()); + relationshipTemplate.setInterfaces(relationship.getInterfaces()); + final String relationshipName = String.format("%s.%s", + ToscaRelationshipTemplate + .createRelationshipName(nodeEntry.getKey(), requirementEntry.getKey()), + relationshipTemplateCount); + + requirement.setRelationship(relationshipName); + relationshipTemplate.setName(relationshipName); + relationshipTemplates.put(relationshipName, relationshipTemplate); + relationshipTemplateCount.incrementAndGet(); + }); + } + } + + return relationshipTemplates; + } + +} diff --git a/catalog-be/src/main/java/org/openecomp/sdc/be/tosca/builder/ToscaRelationshipBuilder.java b/catalog-be/src/main/java/org/openecomp/sdc/be/tosca/builder/ToscaRelationshipBuilder.java new file mode 100644 index 0000000000..c3c9607664 --- /dev/null +++ b/catalog-be/src/main/java/org/openecomp/sdc/be/tosca/builder/ToscaRelationshipBuilder.java @@ -0,0 +1,71 @@ +/* + * ============LICENSE_START======================================================= + * Copyright (C) 2021 Nordix Foundation + * ================================================================================ + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * SPDX-License-Identifier: Apache-2.0 + * ============LICENSE_END========================================================= + */ + +package org.openecomp.sdc.be.tosca.builder; + +import java.util.HashMap; +import java.util.List; +import java.util.Map; +import java.util.Map.Entry; +import java.util.stream.Collectors; +import org.apache.commons.collections.CollectionUtils; +import org.openecomp.sdc.be.model.CapabilityRequirementRelationship; +import org.openecomp.sdc.be.tosca.model.ToscaInterfaceDefinition; +import org.openecomp.sdc.be.tosca.model.ToscaOperationAssignment; +import org.openecomp.sdc.be.tosca.model.ToscaPropertyAssignment; +import org.openecomp.sdc.be.tosca.model.ToscaRelationship; +import org.openecomp.sdc.be.tosca.utils.InterfaceTypesNameUtil; +import org.openecomp.sdc.be.ui.model.OperationUi; + +public class ToscaRelationshipBuilder { + + public ToscaRelationship from(final CapabilityRequirementRelationship capabilityRequirementRelationship) { + final ToscaRelationship toscaRelationship = new ToscaRelationship(); + final List<OperationUi> operations = capabilityRequirementRelationship.getOperations(); + toscaRelationship.setType(capabilityRequirementRelationship.getRelation().getRelationship().getType()); + final Map<String, List<OperationUi>> operationsByInterfaceType = operations.stream() + .collect(Collectors.groupingBy(OperationUi::getInterfaceType)); + final Map<String, ToscaInterfaceDefinition> interfaceMap = new HashMap<>(); + for (final Entry<String, List<OperationUi>> interfaceTypeEntry : operationsByInterfaceType.entrySet()) { + final ToscaInterfaceDefinition toscaInterfaceDefinition = new ToscaInterfaceDefinition(); + final String interfaceType = interfaceTypeEntry.getKey(); + final Map<String, Object> operationDefinitionMap = new HashMap<>(); + for (final OperationUi operationUi : interfaceTypeEntry.getValue()) { + final ToscaOperationAssignment toscaOperationAssignment = new ToscaOperationAssignment(); + toscaOperationAssignment.setImplementation(operationUi.getImplementation()); + if (CollectionUtils.isNotEmpty(operationUi.getInputs())) { + final Map<String, ToscaPropertyAssignment> inputMap = new HashMap<>(); + operationUi.getInputs().forEach(propertyAssignmentUi -> { + final ToscaPropertyAssignment toscaProperty = new ToscaPropertyAssignment(); + toscaProperty.setValue(propertyAssignmentUi.getValue()); + inputMap.put(propertyAssignmentUi.getName(), toscaProperty); + }); + + toscaOperationAssignment.setInputs(inputMap); + } + operationDefinitionMap.put(operationUi.getOperationType(), toscaOperationAssignment); + } + toscaInterfaceDefinition.setOperations(operationDefinitionMap); + interfaceMap.put(InterfaceTypesNameUtil.buildShortName(interfaceType), toscaInterfaceDefinition); + } + toscaRelationship.setInterfaces(interfaceMap); + return toscaRelationship; + } + +} diff --git a/catalog-be/src/main/java/org/openecomp/sdc/be/tosca/model/ToscaOperationAssignment.java b/catalog-be/src/main/java/org/openecomp/sdc/be/tosca/model/ToscaOperationAssignment.java new file mode 100644 index 0000000000..2aba85027e --- /dev/null +++ b/catalog-be/src/main/java/org/openecomp/sdc/be/tosca/model/ToscaOperationAssignment.java @@ -0,0 +1,34 @@ +/* + * ============LICENSE_START======================================================= + * Copyright (C) 2021 Nordix Foundation + * ================================================================================ + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * SPDX-License-Identifier: Apache-2.0 + * ============LICENSE_END========================================================= + */ + +package org.openecomp.sdc.be.tosca.model; + +import java.util.Map; +import lombok.Getter; +import lombok.Setter; + +@Getter +@Setter +public class ToscaOperationAssignment { + + private String description; + private String implementation; + private Map<String, ToscaPropertyAssignment> inputs; + +} diff --git a/catalog-be/src/main/java/org/openecomp/sdc/be/tosca/model/ToscaPropertyAssignment.java b/catalog-be/src/main/java/org/openecomp/sdc/be/tosca/model/ToscaPropertyAssignment.java new file mode 100644 index 0000000000..f42b655cbf --- /dev/null +++ b/catalog-be/src/main/java/org/openecomp/sdc/be/tosca/model/ToscaPropertyAssignment.java @@ -0,0 +1,31 @@ +/* + * ============LICENSE_START======================================================= + * Copyright (C) 2021 Nordix Foundation + * ================================================================================ + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * SPDX-License-Identifier: Apache-2.0 + * ============LICENSE_END========================================================= + */ + +package org.openecomp.sdc.be.tosca.model; + +import lombok.Getter; +import lombok.Setter; + +@Getter +@Setter +public class ToscaPropertyAssignment { + + private Object value; + +} diff --git a/catalog-be/src/main/java/org/openecomp/sdc/be/tosca/model/ToscaRelationship.java b/catalog-be/src/main/java/org/openecomp/sdc/be/tosca/model/ToscaRelationship.java new file mode 100644 index 0000000000..49286ca0e2 --- /dev/null +++ b/catalog-be/src/main/java/org/openecomp/sdc/be/tosca/model/ToscaRelationship.java @@ -0,0 +1,35 @@ +/* + * ============LICENSE_START======================================================= + * Copyright (C) 2021 Nordix Foundation + * ================================================================================ + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * SPDX-License-Identifier: Apache-2.0 + * ============LICENSE_END========================================================= + */ + +package org.openecomp.sdc.be.tosca.model; + +import java.util.Map; +import lombok.Getter; +import lombok.Setter; + +/** + * Represents a relationship entry in a requirement fulfilment. + */ +@Getter +@Setter +public class ToscaRelationship { + + private String type; + private Map<String, ToscaInterfaceDefinition> interfaces; +} diff --git a/catalog-be/src/main/java/org/openecomp/sdc/be/tosca/model/ToscaRelationshipTemplate.java b/catalog-be/src/main/java/org/openecomp/sdc/be/tosca/model/ToscaRelationshipTemplate.java new file mode 100644 index 0000000000..f42effab6b --- /dev/null +++ b/catalog-be/src/main/java/org/openecomp/sdc/be/tosca/model/ToscaRelationshipTemplate.java @@ -0,0 +1,40 @@ +/* + * ============LICENSE_START======================================================= + * Copyright (C) 2021 Nordix Foundation + * ================================================================================ + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * SPDX-License-Identifier: Apache-2.0 + * ============LICENSE_END========================================================= + */ + +package org.openecomp.sdc.be.tosca.model; + +import java.util.Map; +import lombok.Getter; +import lombok.Setter; + +/** + * Represents a relationship_template in the topology_template. + */ +@Getter +@Setter +public class ToscaRelationshipTemplate { + + private String name; + private String type; + private Map<String, ToscaInterfaceDefinition> interfaces; + + public static String createRelationshipName(final String nodeName, final String requirementName) { + return String.format("%s.%s", nodeName, requirementName); + } +} diff --git a/catalog-be/src/main/java/org/openecomp/sdc/be/tosca/model/ToscaRequirement.java b/catalog-be/src/main/java/org/openecomp/sdc/be/tosca/model/ToscaRequirement.java index 35d5c4e6e1..c8e0527191 100644 --- a/catalog-be/src/main/java/org/openecomp/sdc/be/tosca/model/ToscaRequirement.java +++ b/catalog-be/src/main/java/org/openecomp/sdc/be/tosca/model/ToscaRequirement.java @@ -21,25 +21,13 @@ package org.openecomp.sdc.be.tosca.model; import java.util.List; -import java.util.Map; +import lombok.Getter; +import lombok.Setter; +@Getter +@Setter public class ToscaRequirement extends ToscaTemplateRequirement { private List<Object> occurrences; - public ToscaRequirement() { - } - - public List<Object> getOccurrences() { - return occurrences; - } - - public void setOccurrences(List<Object> occurrences) { - this.occurrences = occurrences; - } - - public Map<String, Object> toMap() throws IllegalArgumentException, IllegalAccessException { - return super.toMap(); - } - } diff --git a/catalog-be/src/main/java/org/openecomp/sdc/be/tosca/model/ToscaTemplateRequirement.java b/catalog-be/src/main/java/org/openecomp/sdc/be/tosca/model/ToscaTemplateRequirement.java index 7a77c4f47c..85d01e1469 100644 --- a/catalog-be/src/main/java/org/openecomp/sdc/be/tosca/model/ToscaTemplateRequirement.java +++ b/catalog-be/src/main/java/org/openecomp/sdc/be/tosca/model/ToscaTemplateRequirement.java @@ -20,49 +20,51 @@ package org.openecomp.sdc.be.tosca.model; -import java.lang.reflect.Field; -import java.util.HashMap; -import java.util.Map; +import lombok.Getter; +import lombok.Setter; +import org.openecomp.sdc.exception.InvalidArgumentException; +@Getter +@Setter public class ToscaTemplateRequirement { private String capability; private String node; - private String relationship; + private Object relationship; - public ToscaTemplateRequirement() { - } - - public String getCapability() { - return capability; - } - - public void setCapability(String capability) { - this.capability = capability; - } - - public String getNode() { - return node; - } - - public void setNode(String node) { - this.node = node; - } - - public String getRelationship() { - return relationship; + public ToscaRelationship getRelationshipAsComplexType() { + if (relationship == null) { + return null; + } + if (relationship instanceof ToscaRelationship) { + return (ToscaRelationship) relationship; + } + final ToscaRelationship toscaRelationship = new ToscaRelationship(); + toscaRelationship.setType((String) relationship); + return toscaRelationship; } - public void setRelationship(String relationship) { + public void setRelationship(final Object relationship) { + if (relationship == null) { + this.relationship = null; + return; + } + if (!(relationship instanceof ToscaRelationship) && !(relationship instanceof String)) { + throw new InvalidArgumentException(String.format("relationship %s type not expected. " + + "Supported types are %s and %s", relationship.getClass(), ToscaRelationship.class, String.class)); + } this.relationship = relationship; } - public Map<String, Object> toMap() throws IllegalArgumentException, IllegalAccessException { - Map<String, Object> map = new HashMap<>(); - Field[] fields = this.getClass().getDeclaredFields(); - for (Field field : fields) { - field.setAccessible(true); - map.put(field.getName(), field.get(this)); - } - return map; + /** + * Checks if the relationship entry is a complex type ({@link ToscaRelationship}). + * + * The relationship can be a simple notation (string) (see Tosca 1.3, Section 3.7.3.2.2), or a multi-line grammar + * notation (complex) (see Tosca 1.3, Section 3.7.3.2.3). + * + * @return {@code true} if the relationship is a complex type, {@code false} otherwise + */ + public boolean isRelationshipComplexNotation() { + return relationship instanceof ToscaRelationship; } + } diff --git a/catalog-be/src/main/java/org/openecomp/sdc/be/tosca/model/ToscaTopolgyTemplate.java b/catalog-be/src/main/java/org/openecomp/sdc/be/tosca/model/ToscaTopolgyTemplate.java index 0d62521e59..00ab08b677 100644 --- a/catalog-be/src/main/java/org/openecomp/sdc/be/tosca/model/ToscaTopolgyTemplate.java +++ b/catalog-be/src/main/java/org/openecomp/sdc/be/tosca/model/ToscaTopolgyTemplate.java @@ -22,25 +22,22 @@ package org.openecomp.sdc.be.tosca.model; import java.util.HashMap; import java.util.Map; +import lombok.Getter; +import lombok.Setter; +@Getter public class ToscaTopolgyTemplate { + + @Setter private Map<String, ToscaProperty> inputs; + @Setter private Map<String, ToscaNodeTemplate> node_templates; private Map<String, ToscaGroupTemplate> groups; private Map<String, ToscaPolicyTemplate> policies; + @Setter private SubstitutionMapping substitution_mappings; - - public Map<String, ToscaNodeTemplate> getNode_templates() { - return node_templates; - } - - public void setNode_templates(Map<String, ToscaNodeTemplate> node_templates) { - this.node_templates = node_templates; - } - - public Map<String, ToscaGroupTemplate> getGroups() { - return groups; - } + @Setter + private Map<String, ToscaRelationshipTemplate> relationshipTemplates; public void addGroups(Map<String, ToscaGroupTemplate> groups) { if ( this.groups == null ){ @@ -49,30 +46,10 @@ public class ToscaTopolgyTemplate { this.groups.putAll(groups); } - public SubstitutionMapping getSubstitution_mappings() { - return substitution_mappings; - } - - public void setSubstitution_mappings(SubstitutionMapping substitution_mapping) { - this.substitution_mappings = substitution_mapping; - } - - public Map<String, ToscaProperty> getInputs() { - return inputs; - } - - public void setInputs(Map<String, ToscaProperty> inputs) { - this.inputs = inputs; - } - public void addPolicies(Map<String, ToscaPolicyTemplate> policiesMap) { if ( this.policies == null ){ this.policies = new HashMap<>(); } this.policies.putAll(policiesMap); } - - public Map<String, ToscaPolicyTemplate> getPolicies() { - return policies; - } } diff --git a/catalog-be/src/main/java/org/openecomp/sdc/be/tosca/utils/InterfaceTypesNameUtil.java b/catalog-be/src/main/java/org/openecomp/sdc/be/tosca/utils/InterfaceTypesNameUtil.java new file mode 100644 index 0000000000..eda8cef250 --- /dev/null +++ b/catalog-be/src/main/java/org/openecomp/sdc/be/tosca/utils/InterfaceTypesNameUtil.java @@ -0,0 +1,43 @@ +/* + * ============LICENSE_START======================================================= + * Copyright (C) 2021 Nordix Foundation + * ================================================================================ + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * SPDX-License-Identifier: Apache-2.0 + * ============LICENSE_END========================================================= + */ + +package org.openecomp.sdc.be.tosca.utils; + +public class InterfaceTypesNameUtil { + + private InterfaceTypesNameUtil() { + + } + + /** + * Build the short name of an interface_type by grabbing the final name in its path. E.g. + * "tosca.interfaces.relationship.Configure" will be shortened to "Configure". + * + * @param interfaceName the full interface name + * @return the shortened name of the interface + */ + public static String buildShortName(final String interfaceName) { + if (interfaceName == null) { + throw new IllegalArgumentException("interfaceName cannot be null"); + } + final int index = interfaceName.lastIndexOf('.'); + return index > 0 && interfaceName.length() > index + 1 ? interfaceName.substring(index + 1) : interfaceName; + } + +} diff --git a/catalog-be/src/test/java/org/openecomp/sdc/be/tosca/InterfacesOperationsConverterTest.java b/catalog-be/src/test/java/org/openecomp/sdc/be/tosca/InterfacesOperationsConverterTest.java index 0281317b89..4f569f91cd 100644 --- a/catalog-be/src/test/java/org/openecomp/sdc/be/tosca/InterfacesOperationsConverterTest.java +++ b/catalog-be/src/test/java/org/openecomp/sdc/be/tosca/InterfacesOperationsConverterTest.java @@ -21,9 +21,12 @@ package org.openecomp.sdc.be.tosca; import static org.hamcrest.MatcherAssert.assertThat; import static org.hamcrest.Matchers.allOf; +import static org.hamcrest.Matchers.anEmptyMap; import static org.hamcrest.Matchers.containsInAnyOrder; import static org.hamcrest.Matchers.containsString; import static org.hamcrest.Matchers.equalTo; +import static org.hamcrest.Matchers.is; +import static org.hamcrest.Matchers.nullValue; import static org.junit.jupiter.api.Assertions.assertEquals; import static org.junit.jupiter.api.Assertions.assertFalse; import static org.junit.jupiter.api.Assertions.assertTrue; @@ -70,6 +73,7 @@ import org.openecomp.sdc.be.model.InterfaceDefinition; import org.openecomp.sdc.be.model.Resource; import org.openecomp.sdc.be.model.Service; import org.openecomp.sdc.be.model.ServiceMetadataDefinition; +import org.openecomp.sdc.be.tosca.model.ToscaInterfaceDefinition; import org.openecomp.sdc.be.tosca.model.ToscaNodeType; import org.openecomp.sdc.be.tosca.model.ToscaTemplate; import org.openecomp.sdc.common.util.YamlToObjectConverter; @@ -666,4 +670,48 @@ class InterfacesOperationsConverterTest { assertTrue(MapUtils.isNotEmpty(resultMap) && resultMap.containsKey("NotLocal")); } + + @Test + void testRemoveInterfacesWithoutOperationsEmptyMap() { + final Map<String, Object> interfaceMap = new HashMap<>(); + interfacesOperationsConverter.removeInterfacesWithoutOperations(interfaceMap); + assertThat(interfaceMap, is(anEmptyMap())); + } + + @Test + void testRemoveInterfacesWithoutOperationsNullParameter() { + final Map<String, Object> interfaceMap = null; + interfacesOperationsConverter.removeInterfacesWithoutOperations(interfaceMap); + assertThat(interfaceMap, is(nullValue())); + } + + @Test + void testRemoveInterfacesWithoutOperationsSuccess() { + final Map<String, Object> interfaceMap = new HashMap<>(); + final ToscaInterfaceDefinition toscaInterfaceDefinition1 = new ToscaInterfaceDefinition(); + interfaceMap.put("toscaInterfaceDefinition1", toscaInterfaceDefinition1); + + final ToscaInterfaceDefinition toscaInterfaceDefinition2 = new ToscaInterfaceDefinition(); + final Map<String, Object> toscaInterfaceDefinition2OperationMap = new HashMap<>(); + toscaInterfaceDefinition2OperationMap.put("operation1", new Object()); + toscaInterfaceDefinition2.setOperations(toscaInterfaceDefinition2OperationMap); + interfaceMap.put("toscaInterfaceDefinition2", toscaInterfaceDefinition2); + + final Map<String, Object> toscaInterfaceDefinition3 = new HashMap<>(); + interfaceMap.put("toscaInterfaceDefinition3", toscaInterfaceDefinition3); + + final Map<String, Object> toscaInterfaceDefinition4 = new HashMap<>(); + toscaInterfaceDefinition4.put("operation1", new Object()); + interfaceMap.put("toscaInterfaceDefinition4", toscaInterfaceDefinition4); + + final Object notAToscaInterfaceDefinition = new Object(); + interfaceMap.put("notAToscaInterfaceDefinition", notAToscaInterfaceDefinition); + + interfacesOperationsConverter.removeInterfacesWithoutOperations(interfaceMap); + assertFalse(interfaceMap.containsKey("toscaInterfaceDefinition1")); + assertTrue(interfaceMap.containsKey("toscaInterfaceDefinition2")); + assertFalse(interfaceMap.containsKey("toscaInterfaceDefinition3")); + assertTrue(interfaceMap.containsKey("toscaInterfaceDefinition4")); + assertTrue(interfaceMap.containsKey("notAToscaInterfaceDefinition")); + } } diff --git a/catalog-be/src/test/java/org/openecomp/sdc/be/tosca/ToscaExportHandlerTest.java b/catalog-be/src/test/java/org/openecomp/sdc/be/tosca/ToscaExportHandlerTest.java index b03e12fb6e..a87460fa93 100644 --- a/catalog-be/src/test/java/org/openecomp/sdc/be/tosca/ToscaExportHandlerTest.java +++ b/catalog-be/src/test/java/org/openecomp/sdc/be/tosca/ToscaExportHandlerTest.java @@ -16,12 +16,19 @@ * See the License for the specific language governing permissions and * limitations under the License. * ============LICENSE_END========================================================= + * Modifications copyright (c) 2020, Nordix Foundation + * ================================================================================ */ package org.openecomp.sdc.be.tosca; import static org.hamcrest.CoreMatchers.is; +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertFalse; import static org.junit.Assert.assertThat; +import static org.junit.Assert.assertTrue; +import static org.junit.jupiter.api.Assertions.assertNotNull; +import static org.junit.jupiter.api.Assertions.assertThrows; import static org.mockito.ArgumentMatchers.any; import static org.mockito.ArgumentMatchers.anyBoolean; import static org.mockito.ArgumentMatchers.anyList; @@ -30,6 +37,7 @@ import static org.mockito.ArgumentMatchers.anyString; import static org.mockito.ArgumentMatchers.eq; import static org.mockito.ArgumentMatchers.isNull; import static org.mockito.Mockito.doReturn; +import static org.mockito.Mockito.when; import static org.openecomp.sdc.be.tosca.PropertyConvertor.PropertyType.PROPERTY; import fj.data.Either; @@ -50,7 +58,6 @@ import org.junit.Before; import org.junit.Test; import org.mockito.InjectMocks; import org.mockito.Mock; -import org.mockito.Mockito; import org.mockito.MockitoAnnotations; import org.openecomp.sdc.be.components.BeConfDependentTest; import org.openecomp.sdc.be.components.utils.PropertyDataDefinitionBuilder; @@ -65,6 +72,7 @@ import org.openecomp.sdc.be.datatypes.elements.RequirementDataDefinition; import org.openecomp.sdc.be.datatypes.elements.ToscaArtifactDataDefinition; import org.openecomp.sdc.be.datatypes.enums.ComponentTypeEnum; import org.openecomp.sdc.be.datatypes.enums.OriginTypeEnum; +import org.openecomp.sdc.be.exception.ToscaExportException; import org.openecomp.sdc.be.model.ArtifactDefinition; import org.openecomp.sdc.be.model.CapabilityDefinition; import org.openecomp.sdc.be.model.CapabilityRequirementRelationship; @@ -122,7 +130,7 @@ public class ToscaExportHandlerTest extends BeConfDependentTest { private ToscaOperationFacade toscaOperationFacade; @Mock - private CapabilityRequirementConverter capabiltyRequirementConvertor; + private CapabilityRequirementConverter capabilityRequirementConverter; @Mock private InputConverter inputConverter; @@ -146,7 +154,7 @@ public class ToscaExportHandlerTest extends BeConfDependentTest { private PolicyExportParser policyExportParser; @Before - public void setUpMock() throws Exception { + public void setUpMock() { MockitoAnnotations.initMocks(this); doReturn(new ToscaProperty()).when(propertyConvertor).convertProperty(any(), any(), eq(PROPERTY)); doReturn(new HashMap<String, Object>()).when(interfacesOperationsConverter) @@ -217,11 +225,10 @@ public class ToscaExportHandlerTest extends BeConfDependentTest { Component component = getNewResource(); Either<ToscaRepresentation, ToscaError> result; - Mockito.when(dataTypeCache.getAll()).thenReturn(Either.left(new HashMap<>())); - Mockito - .when(capabiltyRequirementConvertor.convertRequirements(any(Map.class), any(Resource.class), + when(dataTypeCache.getAll()).thenReturn(Either.left(new HashMap<>())); + when(capabilityRequirementConverter.convertRequirements(any(Map.class), any(Resource.class), any(ToscaNodeType.class))).thenReturn(Either.left(new ToscaNodeType())); - Mockito.when(interfaceLifecycleOperation.getAllInterfaceLifecycleTypes()) + when(interfaceLifecycleOperation.getAllInterfaceLifecycleTypes()) .thenReturn(Either.left(Collections.emptyMap())); // default test when component is Resource @@ -229,10 +236,9 @@ public class ToscaExportHandlerTest extends BeConfDependentTest { Assert.assertNotNull(result); component = getNewService(); - Mockito - .when(capabiltyRequirementConvertor.convertRequirements(any(Map.class), any(Service.class), + when(capabilityRequirementConverter.convertRequirements(any(Map.class), any(Service.class), any(ToscaNodeType.class))).thenReturn(Either.left(new ToscaNodeType())); - Mockito.when(dataTypeCache.getAll()).thenReturn(Either.right(JanusGraphOperationStatus.NOT_FOUND)); + when(dataTypeCache.getAll()).thenReturn(Either.right(JanusGraphOperationStatus.NOT_FOUND)); // default test when component is Service result = testSubject.exportComponent(component); @@ -246,16 +252,15 @@ public class ToscaExportHandlerTest extends BeConfDependentTest { ((Resource) component).setInterfaces(new HashMap<>()); - Mockito.when(dataTypeCache.getAll()).thenReturn(Either.right(JanusGraphOperationStatus.NOT_FOUND)); - Mockito.when(interfaceLifecycleOperation.getAllInterfaceLifecycleTypes()) + when(dataTypeCache.getAll()).thenReturn(Either.right(JanusGraphOperationStatus.NOT_FOUND)); + when(interfaceLifecycleOperation.getAllInterfaceLifecycleTypes()) .thenReturn(Either.left(Collections.emptyMap())); // default test when convertInterfaceNodeType is right result = testSubject.exportComponentInterface(component, false); Assert.assertNotNull(result); - Mockito.when(dataTypeCache.getAll()).thenReturn(Either.left(new HashMap<>())); - Mockito - .when(capabiltyRequirementConvertor.convertRequirements(any(Map.class), any(Resource.class), + when(dataTypeCache.getAll()).thenReturn(Either.left(new HashMap<>())); + when(capabilityRequirementConverter.convertRequirements(any(Map.class), any(Resource.class), any(ToscaNodeType.class))).thenReturn(Either.left(new ToscaNodeType())); // default test when convertInterfaceNodeType is left @@ -280,12 +285,11 @@ public class ToscaExportHandlerTest extends BeConfDependentTest { component.setName(RESOURCE_NAME); component.setToscaResourceName(RESOURCE_NAME); - Mockito.when(interfaceLifecycleOperation.getAllInterfaceLifecycleTypes()) + when(interfaceLifecycleOperation.getAllInterfaceLifecycleTypes()) .thenReturn(Either.left(Collections.emptyMap())); - Mockito.when(dataTypeCache.getAll()).thenReturn(Either.left(new HashMap<>())); + when(dataTypeCache.getAll()).thenReturn(Either.left(new HashMap<>())); // when convertRequirements is called, make it return the same value as 3rd (index=2) argument. - Mockito - .when(capabiltyRequirementConvertor.convertRequirements(any(Map.class), any(Resource.class), + when(capabilityRequirementConverter.convertRequirements(any(Map.class), any(Resource.class), any(ToscaNodeType.class))).thenAnswer(i -> Either.left(i.getArgument(2))); Either<ToscaTemplate, ToscaError> result = (Either<ToscaTemplate, ToscaError>) Deencapsulation @@ -339,8 +343,8 @@ public class ToscaExportHandlerTest extends BeConfDependentTest { component.setComponentInstances(resourceInstances); - Mockito.when(dataTypeCache.getAll()).thenReturn(Either.left(new HashMap<>())); - Mockito.when(capabiltyRequirementConvertor.getOriginComponent(any(Map.class), + when(dataTypeCache.getAll()).thenReturn(Either.left(new HashMap<>())); + when(capabilityRequirementConverter.getOriginComponent(any(Map.class), any(ComponentInstance.class))).thenReturn(Either.right(false)); // default test @@ -368,19 +372,18 @@ public class ToscaExportHandlerTest extends BeConfDependentTest { String[] array = {"value1", "value2"}; substitutionMappingMap.put("key", array); - Mockito.when(capabiltyRequirementConvertor.convertSubstitutionMappingCapabilities(any(Map.class), + when(capabilityRequirementConverter.convertSubstitutionMappingCapabilities(any(Map.class), any(Component.class))).thenReturn(Either.left(substitutionMappingMap)); - Mockito.when(capabiltyRequirementConvertor.convertSubstitutionMappingRequirements(any(Map.class), + when(capabilityRequirementConverter.convertSubstitutionMappingRequirements(any(Map.class), any(Component.class), any(SubstitutionMapping.class))) .thenReturn(Either.left(new SubstitutionMapping())); - Mockito.when(dataTypeCache.getAll()).thenReturn(Either.left(new HashMap<>())); + when(dataTypeCache.getAll()).thenReturn(Either.left(new HashMap<>())); - Mockito.when(inputConverter.convertInputs(any(List.class), any(Map.class))) - .thenReturn(new HashMap<>()); + when(inputConverter.convertInputs(any(List.class), any(Map.class))).thenReturn(new HashMap<>()); - Mockito.when(groupExportParser.getGroups(component)) + when(groupExportParser.getGroups(component)) .thenReturn(null); // test component contains group @@ -408,17 +411,16 @@ public class ToscaExportHandlerTest extends BeConfDependentTest { String[] array = {"value1", "value2"}; substitutionMappingMap.put("key", array); - Mockito - .when(capabiltyRequirementConvertor.convertSubstitutionMappingCapabilities(anyMap(), any(Component.class))) + when(capabilityRequirementConverter.convertSubstitutionMappingCapabilities(anyMap(), any(Component.class))) .thenReturn(Either.left(substitutionMappingMap)); - Mockito.when(capabiltyRequirementConvertor + when(capabilityRequirementConverter .convertSubstitutionMappingRequirements(anyMap(), any(Component.class), any(SubstitutionMapping.class))) .thenReturn(Either.left(new SubstitutionMapping())); - Mockito.when(dataTypeCache.getAll()).thenReturn(Either.left(new HashMap<>())); + when(dataTypeCache.getAll()).thenReturn(Either.left(new HashMap<>())); - Mockito.when(inputConverter.convertInputs(anyList(), anyMap())).thenReturn(new HashMap<>()); + when(inputConverter.convertInputs(anyList(), anyMap())).thenReturn(new HashMap<>()); // test component contains group result = Deencapsulation.invoke(testSubject, "convertToscaTemplate", component, toscaNode); Assert.assertNotNull(result); @@ -456,7 +458,7 @@ public class ToscaExportHandlerTest extends BeConfDependentTest { toscaArtifacts.put("assettoscatemplate", artifact); component.setToscaArtifacts(toscaArtifacts); - Mockito.when(toscaOperationFacade.getToscaFullElement(any(String.class))) + when(toscaOperationFacade.getToscaFullElement(any(String.class))) .thenReturn(Either.left(component)); // default test @@ -482,9 +484,9 @@ public class ToscaExportHandlerTest extends BeConfDependentTest { ci.setOriginType(OriginTypeEnum.ServiceProxy); ci.setSourceModelUid("modelName"); - Mockito.when(toscaOperationFacade.getToscaFullElement(eq("name"))).thenReturn(Either.left(component)); + when(toscaOperationFacade.getToscaFullElement(eq("name"))).thenReturn(Either.left(component)); - Mockito.when(toscaOperationFacade.getToscaFullElement(eq("modelName"))) + when(toscaOperationFacade.getToscaFullElement(eq("modelName"))) .thenReturn(Either.left(new Service())); // default test @@ -508,8 +510,8 @@ public class ToscaExportHandlerTest extends BeConfDependentTest { Map<String, ToscaNodeType> nodeTypes = new HashMap<>(); Either<ToscaTemplate, ToscaError> result; - Mockito.when(dataTypeCache.getAll()).thenReturn(Either.right(JanusGraphOperationStatus.ALREADY_EXIST)); - Mockito.when(interfaceLifecycleOperation.getAllInterfaceLifecycleTypes()) + when(dataTypeCache.getAll()).thenReturn(Either.right(JanusGraphOperationStatus.ALREADY_EXIST)); + when(interfaceLifecycleOperation.getAllInterfaceLifecycleTypes()) .thenReturn(Either.left(Collections.emptyMap())); // default test result = Deencapsulation @@ -527,12 +529,11 @@ public class ToscaExportHandlerTest extends BeConfDependentTest { inputs.add(new InputDefinition()); component.setInputs(inputs); - Mockito.when(dataTypeCache.getAll()).thenReturn(Either.left(new HashMap<>())); - Mockito.when(interfaceLifecycleOperation.getAllInterfaceLifecycleTypes()) + when(dataTypeCache.getAll()).thenReturn(Either.left(new HashMap<>())); + when(interfaceLifecycleOperation.getAllInterfaceLifecycleTypes()) .thenReturn(Either.left(Collections.emptyMap())); - Mockito - .when(capabiltyRequirementConvertor.convertRequirements(any(Map.class), any(Resource.class), + when(capabilityRequirementConverter.convertRequirements(any(Map.class), any(Resource.class), any(ToscaNodeType.class))).thenReturn(Either.left(new ToscaNodeType())); // default test @@ -550,13 +551,12 @@ public class ToscaExportHandlerTest extends BeConfDependentTest { Map<String, DataTypeDefinition> dataTypes = new HashMap<>(); Either<ToscaTemplate, ToscaError> result; - Mockito.when( - capabiltyRequirementConvertor + when( + capabilityRequirementConverter .convertCapabilities(any(Map.class), any(Resource.class), any(Map.class))) .thenReturn(new HashMap<>()); - Mockito - .when(capabiltyRequirementConvertor.convertRequirements(any(Map.class), any(Resource.class), + when(capabilityRequirementConverter.convertRequirements(any(Map.class), any(Resource.class), any(ToscaNodeType.class))).thenReturn(Either.left(new ToscaNodeType())); // default test @@ -567,8 +567,7 @@ public class ToscaExportHandlerTest extends BeConfDependentTest { component = new Service(); - Mockito - .when(capabiltyRequirementConvertor.convertRequirements(any(Map.class), any(Service.class), + when(capabilityRequirementConverter.convertRequirements(any(Map.class), any(Service.class), any(ToscaNodeType.class))).thenReturn(Either.left(new ToscaNodeType())); // test when component is service @@ -621,10 +620,10 @@ public class ToscaExportHandlerTest extends BeConfDependentTest { componentInstancesProperties.put("id", new ArrayList<>()); componentInstancesInputs.put("id", new ArrayList<>()); - Mockito.when(capabiltyRequirementConvertor.getOriginComponent(any(Map.class), + when(capabilityRequirementConverter.getOriginComponent(any(Map.class), any(ComponentInstance.class))).thenReturn(Either.left(component)); - Mockito.when(capabiltyRequirementConvertor.convertComponentInstanceCapabilities( + when(capabilityRequirementConverter.convertComponentInstanceCapabilities( any(ComponentInstance.class), any(Map.class), any(ToscaNodeTemplate.class))) .thenReturn(Either.left(new ToscaNodeTemplate())); @@ -687,10 +686,10 @@ public class ToscaExportHandlerTest extends BeConfDependentTest { componentInstancesInterfaces.put("id", new ArrayList<>()); componentInstancesInputs.put("id", new ArrayList<>()); - Mockito.when(capabiltyRequirementConvertor.getOriginComponent(any(Map.class), + when(capabilityRequirementConverter.getOriginComponent(any(Map.class), any(ComponentInstance.class))).thenReturn(Either.left(component)); - Mockito.when(capabiltyRequirementConvertor.convertComponentInstanceCapabilities( + when(capabilityRequirementConverter.convertComponentInstanceCapabilities( any(ComponentInstance.class), any(Map.class), any(ToscaNodeTemplate.class))) .thenReturn(Either.left(new ToscaNodeTemplate())); @@ -733,10 +732,10 @@ public class ToscaExportHandlerTest extends BeConfDependentTest { componentCache.put("uid", component); - Mockito.when(capabiltyRequirementConvertor.getOriginComponent(any(Map.class), + when(capabilityRequirementConverter.getOriginComponent(any(Map.class), any(ComponentInstance.class))).thenReturn(Either.left(component)); - Mockito.when(capabiltyRequirementConvertor.convertComponentInstanceCapabilities( + when(capabilityRequirementConverter.convertComponentInstanceCapabilities( any(ComponentInstance.class), any(Map.class), any(ToscaNodeTemplate.class))) .thenReturn(Either.right(ToscaError.GENERAL_ERROR)); @@ -779,7 +778,7 @@ public class ToscaExportHandlerTest extends BeConfDependentTest { componentCache.put("uid", component); - Mockito.when(capabiltyRequirementConvertor.getOriginComponent(any(Map.class), + when(capabilityRequirementConverter.getOriginComponent(any(Map.class), any(ComponentInstance.class))).thenReturn(Either.right(false)); // default test @@ -821,7 +820,7 @@ public class ToscaExportHandlerTest extends BeConfDependentTest { component.setComponentInstancesInputs(componentInstancesInputs); component.setComponentInstances(componentInstances); - Mockito.when(capabiltyRequirementConvertor.getOriginComponent(any(Map.class), + when(capabilityRequirementConverter.getOriginComponent(any(Map.class), any(ComponentInstance.class))).thenReturn(Either.left(component)); // default test @@ -918,7 +917,7 @@ public class ToscaExportHandlerTest extends BeConfDependentTest { componentInstances.add(instance); container.setComponentInstances(componentInstances); - Mockito.when(toscaOperationFacade.getToscaElement(any(String.class), + when(toscaOperationFacade.getToscaElement(any(String.class), any(ComponentParametersView.class))) .thenReturn(Either.right(StorageOperationStatus.BAD_REQUEST)); result = Deencapsulation.invoke(testSubject, "createProxyInterfaceTypes", container); @@ -937,10 +936,10 @@ public class ToscaExportHandlerTest extends BeConfDependentTest { componentInstances.add(instance); container.setComponentInstances(componentInstances); - Mockito.when(toscaOperationFacade.getToscaElement(any(String.class), + when(toscaOperationFacade.getToscaElement(any(String.class), any(ComponentParametersView.class))) .thenReturn(Either.left(new Resource())); - Mockito.when(interfaceLifecycleOperation.getAllInterfaceLifecycleTypes()) + when(interfaceLifecycleOperation.getAllInterfaceLifecycleTypes()) .thenReturn(Either.right(StorageOperationStatus.BAD_REQUEST)); result = Deencapsulation.invoke(testSubject, "createProxyInterfaceTypes", container); Assert.assertTrue(result.isRight()); @@ -958,14 +957,14 @@ public class ToscaExportHandlerTest extends BeConfDependentTest { componentInstances.add(instance); container.setComponentInstances(componentInstances); - Mockito.when(interfaceLifecycleOperation.getAllInterfaceLifecycleTypes()) + when(interfaceLifecycleOperation.getAllInterfaceLifecycleTypes()) .thenReturn(Either.left(Collections.emptyMap())); Component proxyResource = new Resource(); Map<String, InterfaceDefinition> proxyInterfaces = new HashMap<>(); proxyInterfaces.put("Local", new InterfaceDefinition("Local", "desc", new HashMap<>())); proxyResource.setInterfaces(proxyInterfaces); - Mockito.when(toscaOperationFacade.getToscaElement(any(String.class), + when(toscaOperationFacade.getToscaElement(any(String.class), any(ComponentParametersView.class))) .thenReturn(Either.left(proxyResource)); @@ -988,18 +987,18 @@ public class ToscaExportHandlerTest extends BeConfDependentTest { componentInstances.add(instance); container.setComponentInstances(componentInstances); - Mockito.when(toscaOperationFacade.getLatestByName("serviceProxy")) + when(toscaOperationFacade.getLatestByName("serviceProxy")) .thenReturn(Either.right(StorageOperationStatus.BAD_REQUEST)); // test when getLatestByName return is right result = Deencapsulation.invoke(testSubject, "createProxyNodeTypes", componentCache, container); Assert.assertNotNull(result); } - + @Test public void testCreateServiceSubstitutionNodeTypes() throws Exception { Map<String, Component> componentCache = new HashMap<>(); - + Component referencedService = getNewService(); referencedService.setInvariantUUID("uuid"); referencedService.setUUID("uuid"); @@ -1015,11 +1014,11 @@ public class ToscaExportHandlerTest extends BeConfDependentTest { componentInstances.add(instance); containerService.setComponentInstances(componentInstances); - - Mockito.when(interfaceLifecycleOperation.getAllInterfaceLifecycleTypes()) + + when(interfaceLifecycleOperation.getAllInterfaceLifecycleTypes()) .thenReturn(Either.left(Collections.emptyMap())); - Mockito.when(dataTypeCache.getAll()).thenReturn(Either.left(new HashMap<>())); - Mockito.when(capabiltyRequirementConvertor.convertRequirements(any(Map.class), any(Service.class), + when(dataTypeCache.getAll()).thenReturn(Either.left(new HashMap<>())); + when(capabilityRequirementConverter.convertRequirements(any(Map.class), any(Service.class), any(ToscaNodeType.class))).thenReturn(Either.left(new ToscaNodeType())); ToscaTemplate toscaNode = new ToscaTemplate("1_1"); @@ -1041,17 +1040,17 @@ public class ToscaExportHandlerTest extends BeConfDependentTest { componentInstances.add(instance); container.setComponentInstances(componentInstances); - Mockito.when(toscaOperationFacade.getLatestByName("serviceProxy")).thenReturn(Either.left(new Resource())); + when(toscaOperationFacade.getLatestByName("serviceProxy")).thenReturn(Either.left(new Resource())); ComponentParametersView parameterView = new ComponentParametersView(); parameterView.disableAll(); parameterView.setIgnoreCategories(false); - Mockito.when(toscaOperationFacade.getToscaElement(any(String.class), + when(toscaOperationFacade.getToscaElement(any(String.class), any(ComponentParametersView.class))) .thenReturn(Either.right(StorageOperationStatus.BAD_REQUEST)); - Mockito.when(dataTypeCache.getAll()).thenReturn(Either.left(new HashMap<>())); + when(dataTypeCache.getAll()).thenReturn(Either.left(new HashMap<>())); // test when getLatestByName is left result = Deencapsulation.invoke(testSubject, "createProxyNodeTypes", componentCache, container); @@ -1066,7 +1065,7 @@ public class ToscaExportHandlerTest extends BeConfDependentTest { ComponentInstance instance = new ComponentInstance(); ToscaNodeType result; - Mockito.when(dataTypeCache.getAll()).thenReturn(Either.left(new HashMap<>())); + when(dataTypeCache.getAll()).thenReturn(Either.left(new HashMap<>())); // default test result = Deencapsulation.invoke(testSubject, "createProxyNodeType", componentCache, origComponent, @@ -1111,138 +1110,185 @@ public class ToscaExportHandlerTest extends BeConfDependentTest { } @Test - public void testAddRequirement() throws Exception { - ComponentInstance fromInstance = new ComponentInstance(); - Component fromOriginComponent = new Resource(); - List<ComponentInstance> instancesList = new ArrayList<>(); - RequirementCapabilityRelDef rel = new RequirementCapabilityRelDef(); - List<Map<String, ToscaTemplateRequirement>> toscaRequirements = new ArrayList<>(); - Map<String, Component> componentCache = new HashMap<>(); - boolean result; - - List<CapabilityRequirementRelationship> relationships = new ArrayList<>(); - CapabilityRequirementRelationship cap = new CapabilityRequirementRelationship(); - cap.setRequirement(new RequirementDataDefinition()); - RelationshipInfo relation = new RelationshipInfo(); - relation.setRequirementUid("Uid"); - relation.setRequirement("requirment"); - relation.setCapability("cap"); - relation.setCapabilityOwnerId("id1"); - cap.setRelation(relation); - relationships.add(cap); - rel.setRelationships(relationships); - rel.setToNode("name"); - fromInstance.setUniqueId("name"); - fromInstance.setComponentUid("string"); + public void buildRequirementFailure() { + final Component fromOriginComponent = new Resource(); + final ComponentInstance fromInstance = new ComponentInstance(); + final String fromInstanceUid = "fromInstanceUid"; + fromInstance.setUniqueId(fromInstanceUid); + fromInstance.setComponentUid("componentUid"); + final RequirementCapabilityRelDef relationshipDefinition = new RequirementCapabilityRelDef(); + relationshipDefinition.setToNode("wrongNodeUid"); + final List<CapabilityRequirementRelationship> relationshipList = new ArrayList<>(); + final CapabilityRequirementRelationship relationship = new CapabilityRequirementRelationship(); + relationship.setRequirement(new RequirementDataDefinition()); + relationshipList.add(relationship); + relationshipDefinition.setRelationships(relationshipList); + final List<ComponentInstance> instancesList = new ArrayList<>(); instancesList.add(fromInstance); - Map<String, List<RequirementDefinition>> requirements = new HashMap<>(); - fromOriginComponent.setRequirements(requirements); - - // default test - result = Deencapsulation.invoke(testSubject, "addRequirement", fromInstance, fromOriginComponent, instancesList, - rel, toscaRequirements, componentCache); - Assert.assertNotNull(result); + String expectedError = String + .format("Failed to find a relation from the node %s to the node %s", fromInstance.getName(), + relationshipDefinition.getToNode()); + assertThrows(ToscaExportException.class, () -> + Deencapsulation.invoke(testSubject, "buildRequirement", fromInstance, fromOriginComponent, + instancesList, relationshipDefinition, new HashMap<>()), expectedError); + + try { + Deencapsulation.invoke(testSubject, "buildRequirement", fromInstance, fromOriginComponent, + instancesList, relationshipDefinition, new HashMap<>()); + } catch (Exception e) { + assertTrue(e instanceof ToscaExportException); + assertEquals(expectedError, e.getMessage()); + } + + final RelationshipInfo relation = new RelationshipInfo(); + final String requirementUid = "Uid"; + relation.setRequirementUid(requirementUid); + final String requirementName = "requirementName"; + relation.setRequirement(requirementName); + final String capabilityName = "capabilityName"; + relation.setCapability(capabilityName); + final String capabilityOwnerId = "capabilityOwnerId"; + relation.setCapabilityOwnerId(capabilityOwnerId); + relationship.setRelation(relation); + + final Map<String, List<RequirementDefinition>> requirementMap = new HashMap<>(); + final RequirementDefinition requirementDefinition = new RequirementDefinition(); + requirementMap.put(requirementUid, Collections.singletonList(requirementDefinition)); + fromOriginComponent.setRequirements(requirementMap); + relationshipDefinition.setToNode(fromInstanceUid); + + expectedError = String + .format("Failed to find a requirement with uniqueId %s on a component with uniqueId %s", + relation.getRequirementUid(), fromOriginComponent.getUniqueId()); + + + assertThrows(ToscaExportException.class, () -> + Deencapsulation.invoke(testSubject, "buildRequirement", fromInstance, fromOriginComponent, + instancesList, relationshipDefinition, new HashMap<>()), expectedError); + + requirementDefinition.setName(requirementName); + + when(toscaOperationFacade.getToscaElement(any(String.class), any(ComponentParametersView.class))) + .thenReturn(Either.right(StorageOperationStatus.NOT_FOUND)); + + expectedError = String.format( + "Failed to build substituted name for the requirement %s. " + + "Failed to get an origin component with uniqueId %s", + requirementName, fromInstance.getActualComponentUid()); + assertThrows(ToscaExportException.class, () -> Deencapsulation + .invoke(testSubject, "buildRequirement", fromInstance, fromOriginComponent, instancesList, + relationshipDefinition, new HashMap<>()), expectedError); + + final Component toOriginComponent = new Resource(); + final Map<String, List<CapabilityDefinition>> capabilityMap = new HashMap<>(); + final CapabilityDefinition capabilityDefinition = new CapabilityDefinition(); + + capabilityDefinition.setName(capabilityName); + capabilityDefinition.setOwnerId(capabilityOwnerId); + capabilityDefinition.setType("aType"); + final String capabilityPreviousName = "capabilityPreviousName"; + capabilityDefinition.setPreviousName(capabilityPreviousName); + capabilityMap.put(capabilityName, Collections.singletonList(capabilityDefinition)); + toOriginComponent.setCapabilities(capabilityMap); + when(toscaOperationFacade.getToscaElement(any(String.class), any(ComponentParametersView.class))) + .thenReturn(Either.left(toOriginComponent)); + + + requirementDefinition.setCapability(capabilityName); + relation.setCapability("wrong"); + final String requirementPreviousName = "requirementPreviousName"; + requirementDefinition.setPreviousName(requirementPreviousName); + requirementDefinition.setPath(new ArrayList<>()); + + expectedError = String + .format("Failed to find a capability with name %s on a component with uniqueId %s", + relation.getCapability(), fromOriginComponent.getUniqueId()); + + assertThrows(ToscaExportException.class, () -> Deencapsulation + .invoke(testSubject, "buildRequirement", fromInstance, fromOriginComponent, instancesList, + relationshipDefinition, new HashMap<>()), + expectedError); } @Test - public void testAddRequirmentsWhenFindRequirmentsReturnsValue() { - - ComponentInstance fromInstance = new ComponentInstance(); - Component fromOriginComponent = new Resource(); - List<ComponentInstance> instancesList = new ArrayList<>(); - RequirementCapabilityRelDef rel = new RequirementCapabilityRelDef(); - List<Map<String, ToscaTemplateRequirement>> toscaRequirements = new ArrayList<>(); - Map<String, Component> componentCache = new HashMap<>(); - boolean result; - - List<CapabilityRequirementRelationship> relationships = new ArrayList<>(); - CapabilityRequirementRelationship cap = new CapabilityRequirementRelationship(); - cap.setRequirement(new RequirementDataDefinition()); - RelationshipInfo relation = new RelationshipInfo(); - relation.setRequirementUid("Uid"); - relation.setRequirement("requirment"); - relation.setCapability("cap"); - relation.setCapabilityOwnerId("id1"); - cap.setRelation(relation); - relationships.add(cap); - rel.setRelationships(relationships); - rel.setToNode("name"); + public void testBuildRequirement() { + final ComponentInstance fromInstance = new ComponentInstance(); fromInstance.setUniqueId("name"); fromInstance.setComponentUid("string"); + final List<ComponentInstance> instancesList = new ArrayList<>(); + + final Map<String, Component> componentCache = new HashMap<>(); + final List<CapabilityRequirementRelationship> relationshipList = new ArrayList<>(); + final CapabilityRequirementRelationship relationship = new CapabilityRequirementRelationship(); + relationship.setRequirement(new RequirementDataDefinition()); + final RelationshipInfo relation = new RelationshipInfo(); + final String requirementUid = "Uid"; + relation.setRequirementUid(requirementUid); + final String requirementName = "requirementName"; + relation.setRequirement(requirementName); + final String capabilityName = "capabilityName"; + relation.setCapability(capabilityName); + final String capabilityOwnerId = "capabilityOwnerId"; + relation.setCapabilityOwnerId(capabilityOwnerId); + relationship.setRelation(relation); + relationshipList.add(relationship); + final RequirementCapabilityRelDef relationshipDefinition = new RequirementCapabilityRelDef(); + relationshipDefinition.setRelationships(relationshipList); + relationshipDefinition.setToNode("name"); instancesList.add(fromInstance); - Map<String, List<RequirementDefinition>> requirements = new HashMap<>(); - - List<RequirementDefinition> defs = new ArrayList<>(); - RequirementDefinition def = new RequirementDefinition(); - def.setName("requirment"); - def.setCapability("cap"); - defs.add(def); - requirements.put("key", defs); - fromOriginComponent.setRequirements(requirements); - - Mockito.when(toscaOperationFacade.getToscaElement(any(String.class), - any(ComponentParametersView.class))) - .thenReturn(Either.right(StorageOperationStatus.BAD_REQUEST)); - - // default test - result = Deencapsulation.invoke(testSubject, "addRequirement", fromInstance, fromOriginComponent, instancesList, - rel, toscaRequirements, componentCache); - Assert.assertNotNull(result); - } - - @Test - public void testAddRequirmentsWhenCapabilityBelongsToRelation() { - ComponentInstance fromInstance = new ComponentInstance(); - Component fromOriginComponent = new Resource(); - List<ComponentInstance> instancesList = new ArrayList<>(); - RequirementCapabilityRelDef rel = new RequirementCapabilityRelDef(); - List<Map<String, ToscaTemplateRequirement>> toscaRequirements = new ArrayList<>(); - Map<String, Component> componentCache = new HashMap<>(); - boolean result; - - List<CapabilityRequirementRelationship> relationships = new ArrayList<>(); - CapabilityRequirementRelationship cap = new CapabilityRequirementRelationship(); - cap.setRequirement(new RequirementDataDefinition()); - RelationshipInfo relation = new RelationshipInfo(); - relation.setRequirementUid("Uid"); - relation.setRequirement("requirment"); - relation.setCapability("cap"); - relation.setCapabilityOwnerId("id1"); - cap.setRelation(relation); - relationships.add(cap); - rel.setRelationships(relationships); - rel.setToNode("name"); - fromInstance.setUniqueId("name"); - fromInstance.setComponentUid("string"); - instancesList.add(fromInstance); - Map<String, List<RequirementDefinition>> requirements = new HashMap<>(); - - List<RequirementDefinition> defs = new ArrayList<>(); - RequirementDefinition def = new RequirementDefinition(); - def.setName("requirment"); - def.setCapability("cap"); - defs.add(def); - requirements.put("key", defs); - fromOriginComponent.setRequirements(requirements); - - Map<String, List<CapabilityDefinition>> capabilities = new HashMap<>(); - List<CapabilityDefinition> caps = new ArrayList<>(); - CapabilityDefinition capdef = new CapabilityDefinition(); - capdef.setOwnerId("id"); - capdef.setName("name"); - capdef.setType("type"); - caps.add(capdef); - capabilities.put("cap", caps); - - fromOriginComponent.setCapabilities(capabilities); - - Mockito.when(toscaOperationFacade.getToscaElement(any(String.class), - any(ComponentParametersView.class))).thenReturn(Either.left(fromOriginComponent)); - - // default test - result = Deencapsulation.invoke(testSubject, "addRequirement", fromInstance, fromOriginComponent, instancesList, - rel, toscaRequirements, componentCache); - Assert.assertNotNull(result); + final RequirementDefinition requirementDefinition = new RequirementDefinition(); + requirementDefinition.setName(requirementName); + requirementDefinition.setCapability(capabilityName); + final String requirementPreviousName = "requirementPreviousName"; + requirementDefinition.setPreviousName(requirementPreviousName); + requirementDefinition.setPath(new ArrayList<>()); + final Map<String, List<RequirementDefinition>> requirementMap = new HashMap<>(); + requirementMap.put(requirementUid, Collections.singletonList(requirementDefinition)); + final Component fromOriginComponent = new Resource(); + fromOriginComponent.setRequirements(requirementMap); + + final Map<String, List<CapabilityDefinition>> capabilityMap = new HashMap<>(); + final CapabilityDefinition capabilityDefinition = new CapabilityDefinition(); + capabilityDefinition.setName(capabilityName); + capabilityDefinition.setOwnerId(capabilityOwnerId); + final String capabilityPreviousName = "capabilityPreviousName"; + capabilityDefinition.setPreviousName(capabilityPreviousName); + capabilityMap.put(capabilityName, Collections.singletonList(capabilityDefinition)); + final Component toOriginComponent = new Resource(); + toOriginComponent.setCapabilities(capabilityMap); + + when(toscaOperationFacade.getToscaElement(any(String.class), any(ComponentParametersView.class))) + .thenReturn(Either.left(toOriginComponent)); + final String builtCapabilityName = "builtCapabilityName"; + when( + capabilityRequirementConverter + .buildSubstitutedName(anyMap(), eq(toOriginComponent), anyList(), eq(capabilityName), eq( + capabilityPreviousName))) + .thenReturn(Either.left(builtCapabilityName)); + + final String builtRequirementName = "builtRequirementName"; + when( + capabilityRequirementConverter + .buildSubstitutedName(anyMap(), eq(fromOriginComponent), anyList(), eq(requirementName), eq( + requirementPreviousName))) + .thenReturn(Either.left(builtRequirementName)); + + final Map<String, ToscaTemplateRequirement> actualRequirementMap = + Deencapsulation.invoke(testSubject, "buildRequirement", fromInstance, fromOriginComponent, + instancesList, relationshipDefinition, componentCache); + assertNotNull(actualRequirementMap); + assertFalse(actualRequirementMap.isEmpty()); + assertTrue(actualRequirementMap.containsKey(builtRequirementName)); + final ToscaTemplateRequirement actualToscaTemplateRequirement = actualRequirementMap.get(builtRequirementName); + assertNotNull(actualToscaTemplateRequirement); + assertEquals(builtCapabilityName, actualToscaTemplateRequirement.getCapability()); + + //to toOriginComponent not found + when(toscaOperationFacade.getToscaElement(any(String.class), any(ComponentParametersView.class))) + .thenReturn(Either.right(StorageOperationStatus.NOT_FOUND)); + + assertThrows(ToscaExportException.class, () -> Deencapsulation.invoke(testSubject, "buildRequirement", fromInstance, fromOriginComponent, + instancesList, relationshipDefinition, componentCache)); } @Test @@ -1253,7 +1299,6 @@ public class ToscaExportHandlerTest extends BeConfDependentTest { RequirementCapabilityRelDef rel = new RequirementCapabilityRelDef(); List<Map<String, ToscaTemplateRequirement>> toscaRequirements = new ArrayList<>(); Map<String, Component> componentCache = new HashMap<>(); - boolean result; List<CapabilityRequirementRelationship> relationships = new ArrayList<>(); CapabilityRequirementRelationship cap = new CapabilityRequirementRelationship(); @@ -1291,23 +1336,26 @@ public class ToscaExportHandlerTest extends BeConfDependentTest { capabilities.put("cap", caps); fromOriginComponent.setCapabilities(capabilities); - Mockito.when(toscaOperationFacade.getToscaElement(any(String.class), + when(toscaOperationFacade.getToscaElement(any(String.class), any(ComponentParametersView.class))).thenReturn(Either.left(fromOriginComponent)); - Mockito.when( - capabiltyRequirementConvertor + when( + capabilityRequirementConverter .buildSubstitutedName(anyMap(), any(Component.class), anyList(), anyString(), anyString())) .thenReturn(Either.right(false)); - // default test - result = Deencapsulation.invoke(testSubject, "addRequirement", fromInstance, fromOriginComponent, instancesList, - rel, toscaRequirements, componentCache); - Assert.assertNotNull(result); + final String expectedErrorMsg = + String.format("Failed to build a substituted capability name for the capability " + + "with name %s on a component with uniqueId %s", + cap.getRequirement(), fromOriginComponent.getUniqueId()); + + assertThrows(ToscaExportException.class, () -> + Deencapsulation.invoke(testSubject, "buildRequirement", fromInstance, fromOriginComponent, instancesList, + rel, componentCache), expectedErrorMsg); } @Test - public void testBuildAndAddRequirement() throws Exception { - List<Map<String, ToscaTemplateRequirement>> toscaRequirements = new ArrayList<>(); + public void testBuildAndAddRequirement() { Component fromOriginComponent = new Resource(); Component toOriginComponent = new Resource(); CapabilityDefinition capability = new CapabilityDefinition(); @@ -1315,7 +1363,8 @@ public class ToscaExportHandlerTest extends BeConfDependentTest { RelationshipInfo reqAndRelationshipPair = new RelationshipInfo(); ComponentInstance toInstance = new ComponentInstance(); Map<String, Component> componentCache = new HashMap<>(); - boolean result; + final CapabilityRequirementRelationship capabilityRequirementRelationship = new CapabilityRequirementRelationship(); + capabilityRequirementRelationship.setRelation(reqAndRelationshipPair); capability.setPath(new ArrayList<>()); capability.setPreviousName("before cap"); reqAndRelationshipPair.setCapability("cap"); @@ -1323,46 +1372,57 @@ public class ToscaExportHandlerTest extends BeConfDependentTest { requirement.setPreviousName("before req"); reqAndRelationshipPair.setRequirement("req"); - Mockito.when( - capabiltyRequirementConvertor + when( + capabilityRequirementConverter .buildSubstitutedName(anyMap(), eq(toOriginComponent), anyList(), eq("cap"), anyString())) .thenReturn(Either.left("buildCapNameRes")); - Mockito.when( - capabiltyRequirementConvertor + when( + capabilityRequirementConverter .buildSubstitutedName(anyMap(), eq(fromOriginComponent), anyList(), eq("req"), anyString())) - .thenReturn(Either.right(false)); + .thenReturn(Either.left("buildReqNameRes")); // default test - result = Deencapsulation.invoke(testSubject, "buildAndAddRequirement", toscaRequirements, fromOriginComponent, - toOriginComponent, capability, requirement, reqAndRelationshipPair, toInstance, componentCache); - Assert.assertNotNull(result); + final Map<String, ToscaTemplateRequirement> requirementMap = + Deencapsulation.invoke(testSubject, "buildRequirement", fromOriginComponent, toOriginComponent, + capability, requirement, capabilityRequirementRelationship, toInstance, componentCache); + assertNotNull(requirementMap); + assertFalse(requirementMap.isEmpty()); + assertTrue(requirementMap.containsKey("buildReqNameRes")); + final ToscaTemplateRequirement actualToscaTemplateRequirement = requirementMap.get("buildReqNameRes"); + assertNotNull(actualToscaTemplateRequirement); + assertEquals("buildCapNameRes", actualToscaTemplateRequirement.getCapability()); } @Test - public void testBuildAndAddRequirementBuildSubtitutedNameReturnsValueTwice() { - List<Map<String, ToscaTemplateRequirement>> toscaRequirements = new ArrayList<>(); - Component fromOriginComponent = new Resource(); - Component toOriginComponent = new Resource(); - CapabilityDefinition capability = new CapabilityDefinition(); - RequirementDefinition requirement = new RequirementDefinition(); - RelationshipInfo reqAndRelationshipPair = new RelationshipInfo(); + public void testBuildRequirementBuildSubstitutedNameReturnsValueTwice() { + final Component fromOriginComponent = new Resource(); + final Component toOriginComponent = new Resource(); + final CapabilityDefinition capability = new CapabilityDefinition(); + final RequirementDefinition requirement = new RequirementDefinition(); + final RelationshipInfo relationship = new RelationshipInfo(); + final CapabilityRequirementRelationship capabilityRequirementRelationship = new CapabilityRequirementRelationship(); + capabilityRequirementRelationship.setRelation(relationship); ComponentInstance toInstance = new ComponentInstance(); Map<String, Component> componentCache = new HashMap<>(); - boolean result; capability.setPath(new ArrayList<>()); - reqAndRelationshipPair.setCapability("cap"); + relationship.setCapability("cap"); requirement.setPath(new ArrayList<>()); - reqAndRelationshipPair.setRequirement("req"); - - Mockito.when(capabiltyRequirementConvertor.buildSubstitutedName(any(), any(), any(), any(), any())) - .thenReturn(Either.left("buildCapNameRes")); - - // default test - result = Deencapsulation.invoke(testSubject, "buildAndAddRequirement", toscaRequirements, fromOriginComponent, - toOriginComponent, capability, requirement, reqAndRelationshipPair, toInstance, componentCache); - Assert.assertNotNull(result); - Assert.assertTrue(result); + relationship.setRequirement("req"); + + final String builtCapabilityOrRequirementName = "builtCapabilityOrRequirementName"; + when(capabilityRequirementConverter.buildSubstitutedName(any(), any(), any(), any(), any())) + .thenReturn(Either.left(builtCapabilityOrRequirementName)); + + final Map<String, ToscaTemplateRequirement> requirementMap = Deencapsulation + .invoke(testSubject, "buildRequirement", fromOriginComponent, toOriginComponent, capability, requirement, + capabilityRequirementRelationship, toInstance, componentCache); + assertNotNull(requirementMap); + assertFalse(requirementMap.isEmpty()); + assertTrue(requirementMap.containsKey(builtCapabilityOrRequirementName)); + final ToscaTemplateRequirement actualToscaTemplateRequirement = requirementMap.get(builtCapabilityOrRequirementName); + assertNotNull(actualToscaTemplateRequirement); + assertEquals(builtCapabilityOrRequirementName, actualToscaTemplateRequirement.getCapability()); } @Test @@ -1435,7 +1495,7 @@ public class ToscaExportHandlerTest extends BeConfDependentTest { Map<String, Component> componentCache = new HashMap<>(); Either<SubstitutionMapping, ToscaError> result; - Mockito.when(capabiltyRequirementConvertor.convertSubstitutionMappingCapabilities(componentCache, component)) + when(capabilityRequirementConverter.convertSubstitutionMappingCapabilities(componentCache, component)) .thenReturn(Either.right(ToscaError.NODE_TYPE_CAPABILITY_ERROR)); // default test return isRight diff --git a/catalog-be/src/test/java/org/openecomp/sdc/be/tosca/ToscaExportRelationshipTemplatesHandlerTest.java b/catalog-be/src/test/java/org/openecomp/sdc/be/tosca/ToscaExportRelationshipTemplatesHandlerTest.java new file mode 100644 index 0000000000..3225999974 --- /dev/null +++ b/catalog-be/src/test/java/org/openecomp/sdc/be/tosca/ToscaExportRelationshipTemplatesHandlerTest.java @@ -0,0 +1,111 @@ +/* + * ============LICENSE_START======================================================= + * Copyright (C) 2021 Nordix Foundation + * ================================================================================ + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * SPDX-License-Identifier: Apache-2.0 + * ============LICENSE_END========================================================= + */ + +package org.openecomp.sdc.be.tosca; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertTrue; +import static org.junit.jupiter.api.Assertions.assertNotNull; + +import java.util.ArrayList; +import java.util.Collections; +import java.util.HashMap; +import java.util.List; +import java.util.Map; +import org.junit.jupiter.api.Test; +import org.openecomp.sdc.be.tosca.model.ToscaInterfaceDefinition; +import org.openecomp.sdc.be.tosca.model.ToscaNodeTemplate; +import org.openecomp.sdc.be.tosca.model.ToscaRelationship; +import org.openecomp.sdc.be.tosca.model.ToscaRelationshipTemplate; +import org.openecomp.sdc.be.tosca.model.ToscaTemplateRequirement; + +class ToscaExportRelationshipTemplatesHandlerTest { + + @Test + void testCreateFromEmptyNodeTemplateMapReturnsEmptyMap() { + final Map<String, ToscaRelationshipTemplate> actualRelationshipTemplateMap = + new ToscaExportRelationshipTemplatesHandler().createFrom(Collections.emptyMap()); + assertNotNull(actualRelationshipTemplateMap); + assertTrue(actualRelationshipTemplateMap.isEmpty()); + } + + @Test + void testCreateFromSuccess() { + final Map<String, ToscaNodeTemplate> nodeTemplateMap = new HashMap<>(); + + final ToscaNodeTemplate nodeTemplateWithNoRequirements = new ToscaNodeTemplate(); + nodeTemplateMap.put("nodeTemplateWithNoRequirements", nodeTemplateWithNoRequirements); + + final ToscaNodeTemplate nodeTemplateWithRequirements = new ToscaNodeTemplate(); + final List<Map<String, ToscaTemplateRequirement>> requirements = new ArrayList<>(); + + final Map<String, ToscaTemplateRequirement> requirementMap = new HashMap<>(); + final ToscaTemplateRequirement complexRequirement = new ToscaTemplateRequirement(); + complexRequirement.setNode("aNode"); + + final ToscaRelationship toscaRelationship = new ToscaRelationship(); + final String relationshipType = "tosca.relationships.ConnectsTo"; + toscaRelationship.setType(relationshipType); + + final Map<String, ToscaInterfaceDefinition> interfaces = new HashMap<>(); + final ToscaInterfaceDefinition toscaInterfaceDefinition = new ToscaInterfaceDefinition(); + final String interfaceConfigureType = "tosca.interfaces.relationship.Configure"; + toscaInterfaceDefinition.setType(interfaceConfigureType); + final HashMap<String, Object> operationMap = new HashMap<>(); + final String preConfigSourceOperationType = "pre_configure_source"; + operationMap.put(preConfigSourceOperationType, new Object()); + toscaInterfaceDefinition.setOperations(operationMap); + + interfaces.put(interfaceConfigureType, toscaInterfaceDefinition); + toscaRelationship.setInterfaces(interfaces); + complexRequirement.setRelationship(toscaRelationship); + requirementMap.put("requirement1", complexRequirement); + + final ToscaTemplateRequirement simpleRequirement = new ToscaTemplateRequirement(); + simpleRequirement.setNode("anotherNode"); + simpleRequirement.setRelationship("aRelationship"); + requirementMap.put("requirement2", simpleRequirement); + + requirements.add(requirementMap); + nodeTemplateWithRequirements.setRequirements(requirements); + nodeTemplateMap.put("nodeTemplateWithRequirements", nodeTemplateWithRequirements); + + final Map<String, ToscaRelationshipTemplate> actualRelationshipTemplateMap = + new ToscaExportRelationshipTemplatesHandler().createFrom(nodeTemplateMap); + + assertNotNull(actualRelationshipTemplateMap); + assertEquals(1, actualRelationshipTemplateMap.size()); + final ToscaRelationshipTemplate actualRelationshipTemplate = actualRelationshipTemplateMap.values().iterator().next(); + assertEquals(relationshipType, actualRelationshipTemplate.getType()); + + final Map<String, ToscaInterfaceDefinition> actualInterfaceMap = actualRelationshipTemplate.getInterfaces(); + assertNotNull(actualInterfaceMap); + assertEquals(1, actualInterfaceMap.size()); + assertTrue(actualInterfaceMap.containsKey(interfaceConfigureType)); + + final ToscaInterfaceDefinition actualToscaInterfaceDefinition = + actualInterfaceMap.get(interfaceConfigureType); + assertEquals(toscaInterfaceDefinition.getType(), actualToscaInterfaceDefinition.getType()); + + final Map<String, Object> actualOperationMap = actualToscaInterfaceDefinition.getOperations(); + assertNotNull(actualOperationMap); + assertEquals(1, actualOperationMap.size()); + assertTrue(actualOperationMap.containsKey(preConfigSourceOperationType)); + } +}
\ No newline at end of file diff --git a/catalog-be/src/test/java/org/openecomp/sdc/be/tosca/builder/ToscaRelationshipBuilderTest.java b/catalog-be/src/test/java/org/openecomp/sdc/be/tosca/builder/ToscaRelationshipBuilderTest.java new file mode 100644 index 0000000000..cee70661be --- /dev/null +++ b/catalog-be/src/test/java/org/openecomp/sdc/be/tosca/builder/ToscaRelationshipBuilderTest.java @@ -0,0 +1,122 @@ +/* + * ============LICENSE_START======================================================= + * Copyright (C) 2021 Nordix Foundation + * ================================================================================ + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * SPDX-License-Identifier: Apache-2.0 + * ============LICENSE_END========================================================= + */ + +package org.openecomp.sdc.be.tosca.builder; + +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.assertTrue; + +import java.util.ArrayList; +import java.util.List; +import java.util.Map; +import org.junit.jupiter.api.Test; +import org.openecomp.sdc.be.model.CapabilityRequirementRelationship; +import org.openecomp.sdc.be.model.RelationshipImpl; +import org.openecomp.sdc.be.model.RelationshipInfo; +import org.openecomp.sdc.be.tosca.model.ToscaInterfaceDefinition; +import org.openecomp.sdc.be.tosca.model.ToscaOperationAssignment; +import org.openecomp.sdc.be.tosca.model.ToscaPropertyAssignment; +import org.openecomp.sdc.be.tosca.model.ToscaRelationship; +import org.openecomp.sdc.be.ui.model.OperationUi; +import org.openecomp.sdc.be.ui.model.PropertyAssignmentUi; + +class ToscaRelationshipBuilderTest { + + @Test + void testBuildFromCapabilityRequirementRelationshipSuccess() { + final CapabilityRequirementRelationship relationship = new CapabilityRequirementRelationship(); + final RelationshipImpl relationshipImpl = new RelationshipImpl(); + final String relationshipType = "relationshipType"; + relationshipImpl.setType(relationshipType); + final RelationshipInfo relationshipInfo = new RelationshipInfo(); + relationshipInfo.setRelationships(relationshipImpl); + relationship.setRelation(relationshipInfo); + + final List<OperationUi> operationList = new ArrayList<>(); + final OperationUi operationUi1 = new OperationUi(); + operationUi1.setInterfaceType("interfaceType1"); + operationUi1.setOperationType("operation1"); + operationUi1.setImplementation("implementation"); + operationList.add(operationUi1); + + final OperationUi operationUi2 = new OperationUi(); + operationUi2.setInterfaceType("interfaceType1"); + operationUi2.setOperationType("operation2"); + operationUi2.setImplementation("implementation"); + operationList.add(operationUi2); + final List<PropertyAssignmentUi> operation2InputList = new ArrayList<>(); + final PropertyAssignmentUi propertyAssignmentUi1 = new PropertyAssignmentUi(); + propertyAssignmentUi1.setValue("propertyAssignmentUi1Value"); + propertyAssignmentUi1.setType("string"); + propertyAssignmentUi1.setName("propertyAssignmentUi1"); + operation2InputList.add(propertyAssignmentUi1); + final PropertyAssignmentUi propertyAssignmentUi2 = new PropertyAssignmentUi(); + propertyAssignmentUi2.setValue("propertyAssignmentUi2Value"); + propertyAssignmentUi2.setType("string"); + propertyAssignmentUi2.setName("propertyAssignmentUi2"); + operation2InputList.add(propertyAssignmentUi2); + operationUi2.setInputs(operation2InputList); + + final OperationUi operationUi3 = new OperationUi(); + operationUi3.setInterfaceType("interfaceType2"); + operationUi3.setOperationType("operation1"); + operationUi3.setImplementation("implementation"); + operationList.add(operationUi3); + + relationship.setOperations(operationList); + + + final ToscaRelationship toscaRelationship = new ToscaRelationshipBuilder().from(relationship); + assertEquals(toscaRelationship.getType(), relationshipType); + final Map<String, ToscaInterfaceDefinition> interfaceMap = toscaRelationship.getInterfaces(); + assertNotNull(interfaceMap); + assertFalse(interfaceMap.isEmpty()); + assertEquals(2, interfaceMap.size()); + final ToscaInterfaceDefinition toscaInterfaceDefinition = interfaceMap.get(operationUi1.getInterfaceType()); + assertNull(toscaInterfaceDefinition.getType()); + assertNotNull(toscaInterfaceDefinition.getOperations()); + assertEquals(2, toscaInterfaceDefinition.getOperations().size()); + final Object actualOperation1Obj = toscaInterfaceDefinition.getOperations().get(operationUi1.getOperationType()); + assertTrue(actualOperation1Obj instanceof ToscaOperationAssignment); + final ToscaOperationAssignment actualOperation1 = (ToscaOperationAssignment) actualOperation1Obj; + assertOperationUi(actualOperation1, operationUi1); + } + + private void assertOperationUi(final ToscaOperationAssignment toscaOperationAssignment, final OperationUi operationUi1) { + if (operationUi1 == null) { + assertNull(toscaOperationAssignment); + return; + } + assertEquals(toscaOperationAssignment.getImplementation(), operationUi1.getImplementation()); + if (operationUi1.getInputs() == null) { + assertNull(toscaOperationAssignment.getInputs()); + return; + } + assertEquals(toscaOperationAssignment.getInputs().size(), operationUi1.getInputs().size()); + operationUi1.getInputs().forEach(propertyAssignmentUi -> { + final ToscaPropertyAssignment toscaPropertyAssignment = toscaOperationAssignment.getInputs() + .get(propertyAssignmentUi.getName()); + assertNotNull(toscaPropertyAssignment); + assertEquals(propertyAssignmentUi.getValue(), toscaPropertyAssignment.getValue()); + }); + } +}
\ No newline at end of file diff --git a/catalog-be/src/test/java/org/openecomp/sdc/be/tosca/model/ToscaRequirementTest.java b/catalog-be/src/test/java/org/openecomp/sdc/be/tosca/model/ToscaRequirementTest.java deleted file mode 100644 index bb3af40e36..0000000000 --- a/catalog-be/src/test/java/org/openecomp/sdc/be/tosca/model/ToscaRequirementTest.java +++ /dev/null @@ -1,67 +0,0 @@ -/*- - * ============LICENSE_START======================================================= - * SDC - * ================================================================================ - * Copyright (C) 2019 AT&T Intellectual Property. All rights reserved. - * ================================================================================ - * 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. - * ============LICENSE_END========================================================= - */ - -package org.openecomp.sdc.be.tosca.model; - -import org.junit.Test; - -import java.util.List; -import java.util.Map; - - -public class ToscaRequirementTest { - - private ToscaRequirement createTestSubject() { - return new ToscaRequirement(); - } - - - @Test - public void testGetOccurrences() throws Exception { - ToscaRequirement testSubject; - List<Object> result; - - // default test - testSubject = createTestSubject(); - result = testSubject.getOccurrences(); - } - - - @Test - public void testSetOccurrences() throws Exception { - ToscaRequirement testSubject; - List<Object> occurrences = null; - - // default test - testSubject = createTestSubject(); - testSubject.setOccurrences(occurrences); - } - - - @Test - public void testToMap() throws Exception { - ToscaRequirement testSubject; - Map<String, Object> result; - - // default test - testSubject = createTestSubject(); - result = testSubject.toMap(); - } -} diff --git a/catalog-be/src/test/java/org/openecomp/sdc/be/tosca/model/ToscaTemplateRequirementTest.java b/catalog-be/src/test/java/org/openecomp/sdc/be/tosca/model/ToscaTemplateRequirementTest.java index 5d45407843..bb5a3e50f0 100644 --- a/catalog-be/src/test/java/org/openecomp/sdc/be/tosca/model/ToscaTemplateRequirementTest.java +++ b/catalog-be/src/test/java/org/openecomp/sdc/be/tosca/model/ToscaTemplateRequirementTest.java @@ -7,9 +7,9 @@ * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at - * + * * http://www.apache.org/licenses/LICENSE-2.0 - * + * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. @@ -20,91 +20,60 @@ package org.openecomp.sdc.be.tosca.model; -import org.junit.Test; - -import java.util.Map; - - -public class ToscaTemplateRequirementTest { - - private ToscaTemplateRequirement createTestSubject() { - return new ToscaTemplateRequirement(); - } - - - @Test - public void testGetCapability() throws Exception { - ToscaTemplateRequirement testSubject; - String result; - - // default test - testSubject = createTestSubject(); - result = testSubject.getCapability(); - } - - - @Test - public void testSetCapability() throws Exception { - ToscaTemplateRequirement testSubject; - String capability = ""; - - // default test - testSubject = createTestSubject(); - testSubject.setCapability(capability); - } - - - @Test - public void testGetNode() throws Exception { - ToscaTemplateRequirement testSubject; - String result; - - // default test - testSubject = createTestSubject(); - result = testSubject.getNode(); - } - - - @Test - public void testSetNode() throws Exception { - ToscaTemplateRequirement testSubject; - String node = ""; - - // default test - testSubject = createTestSubject(); - testSubject.setNode(node); - } - - - @Test - public void testGetRelationship() throws Exception { - ToscaTemplateRequirement testSubject; - String result; - - // default test - testSubject = createTestSubject(); - result = testSubject.getRelationship(); - } - - - @Test - public void testSetRelationship() throws Exception { - ToscaTemplateRequirement testSubject; - String relationship = ""; - - // default test - testSubject = createTestSubject(); - testSubject.setRelationship(relationship); - } - - - @Test - public void testToMap() throws Exception { - ToscaTemplateRequirement testSubject; - Map<String, Object> result; - - // default test - testSubject = createTestSubject(); - result = testSubject.toMap(); - } +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertFalse; +import static org.junit.jupiter.api.Assertions.assertNull; +import static org.junit.jupiter.api.Assertions.assertThrows; +import static org.junit.jupiter.api.Assertions.assertTrue; + +import org.junit.jupiter.api.Test; +import org.openecomp.sdc.exception.InvalidArgumentException; + +class ToscaTemplateRequirementTest { + + @Test + void testSetRelationship() { + final ToscaTemplateRequirement toscaTemplateRequirement = new ToscaTemplateRequirement(); + toscaTemplateRequirement.setRelationship(null); + assertNull(toscaTemplateRequirement.getRelationship()); + + final String relationshipType = "aType"; + toscaTemplateRequirement.setRelationship(relationshipType); + Object actualRelationship = toscaTemplateRequirement.getRelationship(); + assertEquals(relationshipType, actualRelationship); + + final ToscaRelationship toscaRelationship = new ToscaRelationship(); + toscaRelationship.setType(relationshipType); + toscaTemplateRequirement.setRelationship(toscaRelationship); + actualRelationship = toscaTemplateRequirement.getRelationship(); + assertEquals(toscaRelationship, actualRelationship); + + assertThrows(InvalidArgumentException.class, () -> toscaTemplateRequirement.setRelationship(1)); + } + + @Test + void testIsRelationshipComplexNotation() { + final ToscaTemplateRequirement toscaTemplateRequirement = new ToscaTemplateRequirement(); + assertFalse(toscaTemplateRequirement.isRelationshipComplexNotation()); + toscaTemplateRequirement.setRelationship(""); + assertFalse(toscaTemplateRequirement.isRelationshipComplexNotation()); + toscaTemplateRequirement.setRelationship(new ToscaRelationship()); + assertTrue(toscaTemplateRequirement.isRelationshipComplexNotation()); + } + + @Test + void testGetRelationshipAsComplexType() { + final ToscaTemplateRequirement toscaTemplateRequirement = new ToscaTemplateRequirement(); + ToscaRelationship actualRelationship = toscaTemplateRequirement.getRelationshipAsComplexType(); + assertNull(actualRelationship); + final String relationshipType = "aType"; + toscaTemplateRequirement.setRelationship(relationshipType); + actualRelationship = toscaTemplateRequirement.getRelationshipAsComplexType(); + assertEquals(relationshipType, actualRelationship.getType()); + + final ToscaRelationship expectedRelationship = new ToscaRelationship(); + toscaTemplateRequirement.setRelationship(expectedRelationship); + actualRelationship = toscaTemplateRequirement.getRelationshipAsComplexType(); + assertEquals(expectedRelationship, actualRelationship); + } } diff --git a/catalog-be/src/test/java/org/openecomp/sdc/be/tosca/utils/InterfaceTypesNameUtilTest.java b/catalog-be/src/test/java/org/openecomp/sdc/be/tosca/utils/InterfaceTypesNameUtilTest.java new file mode 100644 index 0000000000..8eebebe5cf --- /dev/null +++ b/catalog-be/src/test/java/org/openecomp/sdc/be/tosca/utils/InterfaceTypesNameUtilTest.java @@ -0,0 +1,53 @@ +/* + * ============LICENSE_START======================================================= + * Copyright (C) 2021 Nordix Foundation + * ================================================================================ + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * SPDX-License-Identifier: Apache-2.0 + * ============LICENSE_END========================================================= + */ + +package org.openecomp.sdc.be.tosca.utils; + +import static org.junit.jupiter.api.Assertions.*; + +import org.junit.jupiter.api.Test; + +class InterfaceTypesNameUtilTest { + + @Test + void testBuildInterfaceShortNameSuccess() { + String interfaceShortName = InterfaceTypesNameUtil.buildShortName("an.interface.name"); + assertEquals("name", interfaceShortName); + + interfaceShortName = InterfaceTypesNameUtil.buildShortName("name"); + assertEquals("name", interfaceShortName); + + interfaceShortName = InterfaceTypesNameUtil.buildShortName(""); + assertEquals("", interfaceShortName); + + interfaceShortName = InterfaceTypesNameUtil.buildShortName("an."); + assertEquals("an.", interfaceShortName); + + interfaceShortName = InterfaceTypesNameUtil.buildShortName("."); + assertEquals(".", interfaceShortName); + + interfaceShortName = InterfaceTypesNameUtil.buildShortName("."); + assertEquals(".", interfaceShortName); + } + + @Test + void testBuildInterfaceShortNameNullArgument() { + assertThrows(IllegalArgumentException.class, () -> InterfaceTypesNameUtil.buildShortName(null)); + } +}
\ No newline at end of file |