diff options
Diffstat (limited to 'openecomp-be/lib/openecomp-tosca-lib/src')
7 files changed, 1347 insertions, 376 deletions
diff --git a/openecomp-be/lib/openecomp-tosca-lib/src/main/java/org/openecomp/sdc/tosca/datatypes/ToscaArtifactType.java b/openecomp-be/lib/openecomp-tosca-lib/src/main/java/org/openecomp/sdc/tosca/datatypes/ToscaArtifactType.java index c43fcaa2bb..376d643d0d 100644 --- a/openecomp-be/lib/openecomp-tosca-lib/src/main/java/org/openecomp/sdc/tosca/datatypes/ToscaArtifactType.java +++ b/openecomp-be/lib/openecomp-tosca-lib/src/main/java/org/openecomp/sdc/tosca/datatypes/ToscaArtifactType.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. @@ -27,11 +27,11 @@ import org.openecomp.sdc.tosca.services.ConfigConstants; public class ToscaArtifactType { - private static Configuration config = ConfigurationManager.lookup(); + private static Configuration config = ConfigurationManager.lookup(); - public static final String ARTIFACT_TYPE_PREFIX = - config.getAsString(ConfigConstants.NAMESPACE, ConfigConstants.PREFIX_ARTIFACT_TYPE); + public static final String ARTIFACT_TYPE_PREFIX = + config.getAsString(ConfigConstants.NAMESPACE, ConfigConstants.PREFIX_ARTIFACT_TYPE); - public static final String NATIVE_DEPLOYMENT = "tosca.artifacts.Deployment"; + public static final String NATIVE_DEPLOYMENT = "tosca.artifacts.Deployment"; } diff --git a/openecomp-be/lib/openecomp-tosca-lib/src/main/java/org/openecomp/sdc/tosca/datatypes/ToscaServiceModel.java b/openecomp-be/lib/openecomp-tosca-lib/src/main/java/org/openecomp/sdc/tosca/datatypes/ToscaServiceModel.java index 3493d933e3..91a25783b5 100644 --- a/openecomp-be/lib/openecomp-tosca-lib/src/main/java/org/openecomp/sdc/tosca/datatypes/ToscaServiceModel.java +++ b/openecomp-be/lib/openecomp-tosca-lib/src/main/java/org/openecomp/sdc/tosca/datatypes/ToscaServiceModel.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,113 +20,114 @@ package org.openecomp.sdc.tosca.datatypes; +import java.util.Collections; +import java.util.HashMap; +import java.util.Map; +import java.util.Optional; + import org.apache.commons.collections.MapUtils; import org.onap.sdc.tosca.datatypes.model.ServiceTemplate; import org.openecomp.core.utilities.file.FileContentHandler; import org.openecomp.sdc.datatypes.model.AsdcModel; import org.openecomp.sdc.tosca.services.DataModelUtil; -import java.util.Collections; -import java.util.HashMap; -import java.util.Map; -import java.util.Optional; - /** * Tosca service model. */ public class ToscaServiceModel implements AsdcModel { - private FileContentHandler artifactFiles; - private Map<String, ServiceTemplate> serviceTemplates; - private String entryDefinitionServiceTemplate; - - public ToscaServiceModel() { - } - - /** - * Instantiates a new Tosca service model. - * - * @param artifactFiles the artifact files - * @param serviceTemplates the service templates - * @param entryDefinitionServiceTemplate the entry definition service template - */ - public ToscaServiceModel(FileContentHandler artifactFiles, - Map<String, ServiceTemplate> serviceTemplates, - String entryDefinitionServiceTemplate) { - this.artifactFiles = artifactFiles; - this.serviceTemplates = serviceTemplates; - this.entryDefinitionServiceTemplate = entryDefinitionServiceTemplate; - } - - /** - * Gets artifact files. - * - * @return the artifact files - */ - public FileContentHandler getArtifactFiles() { - return artifactFiles; - } - - public void setArtifactFiles(FileContentHandler artifactFiles) { - this.artifactFiles = artifactFiles; - } - - /** - * Gets service templates. - * - * @return the service templates - */ - public Map<String, ServiceTemplate> getServiceTemplates() { - return Collections.unmodifiableMap(serviceTemplates); - } - - public Optional<ServiceTemplate> getServiceTemplate(String serviceTemplateName) { - return MapUtils.isEmpty(this.serviceTemplates) ? Optional.empty() - : Optional.of(this.serviceTemplates.get(serviceTemplateName)); - } - - public void addServiceTemplate(String serviceTemplateName, - ServiceTemplate serviceTemplate) { - if(MapUtils.isEmpty(serviceTemplates)){ - serviceTemplates = new HashMap<>(); + + private FileContentHandler artifactFiles; + private Map<String, ServiceTemplate> serviceTemplates; + private String entryDefinitionServiceTemplate; + + public ToscaServiceModel() { + } + + /** + * Instantiates a new Tosca service model. + * + * @param artifactFiles the artifact files + * @param serviceTemplates the service templates + * @param entryDefinitionServiceTemplate the entry definition service template + */ + public ToscaServiceModel(FileContentHandler artifactFiles, + Map<String, ServiceTemplate> serviceTemplates, + String entryDefinitionServiceTemplate) { + this.artifactFiles = artifactFiles; + this.serviceTemplates = serviceTemplates; + this.entryDefinitionServiceTemplate = entryDefinitionServiceTemplate; + } + + /** + * Gets artifact files. + * + * @return the artifact files + */ + public FileContentHandler getArtifactFiles() { + return artifactFiles; } - serviceTemplates.put(serviceTemplateName, serviceTemplate); - } - - /** - * Sets service templates. - * - * @param serviceTemplates the service templates - */ - public void setServiceTemplates(Map<String, ServiceTemplate> serviceTemplates) { - this.serviceTemplates = serviceTemplates; - } - - /** - * Gets entry definition service template. - * - * @return the entry definition service template - */ - public String getEntryDefinitionServiceTemplate() { - return entryDefinitionServiceTemplate; - } - - /** - * Sets entry definition service template. - * - * @param entryDefinitionServiceTemplate the entry definition service template - */ - public void setEntryDefinitionServiceTemplate(String entryDefinitionServiceTemplate) { - this.entryDefinitionServiceTemplate = entryDefinitionServiceTemplate; - } - - /** - * Gets cloned service model. - * - * @param toscaServiceModel the tosca service model - * @return the cloned service model - */ - public static ToscaServiceModel getClonedServiceModel(ToscaServiceModel toscaServiceModel) { - return ToscaServiceModel.class.cast(DataModelUtil.getClonedObject(toscaServiceModel)); - } + public void setArtifactFiles(FileContentHandler artifactFiles) { + this.artifactFiles = artifactFiles; + } + + /** + * Gets service templates. + * + * @return the service templates + */ + public Map<String, ServiceTemplate> getServiceTemplates() { + return Collections.unmodifiableMap(serviceTemplates); + } + + public Optional<ServiceTemplate> getServiceTemplate(String serviceTemplateName) { + return MapUtils.isEmpty(this.serviceTemplates) ? Optional.empty() + : Optional.of(this.serviceTemplates.get(serviceTemplateName)); + } + + public void addServiceTemplate(String serviceTemplateName, + ServiceTemplate serviceTemplate) { + if (MapUtils.isEmpty(serviceTemplates)) { + serviceTemplates = new HashMap<>(); + } + + serviceTemplates.put(serviceTemplateName, serviceTemplate); + } + + /** + * Sets service templates. + * + * @param serviceTemplates the service templates + */ + public void setServiceTemplates(Map<String, ServiceTemplate> serviceTemplates) { + this.serviceTemplates = serviceTemplates; + } + + /** + * Gets entry definition service template. + * + * @return the entry definition service template + */ + public String getEntryDefinitionServiceTemplate() { + return entryDefinitionServiceTemplate; + } + + /** + * Sets entry definition service template. + * + * @param entryDefinitionServiceTemplate the entry definition service template + */ + public void setEntryDefinitionServiceTemplate(String entryDefinitionServiceTemplate) { + this.entryDefinitionServiceTemplate = entryDefinitionServiceTemplate; + } + + /** + * Gets cloned service model. + * + * @param toscaServiceModel the tosca service model + * @return the cloned service model + */ + public static ToscaServiceModel getClonedServiceModel(ToscaServiceModel toscaServiceModel) { + return ToscaServiceModel.class.cast(DataModelUtil.getClonedObject(toscaServiceModel)); + } } diff --git a/openecomp-be/lib/openecomp-tosca-lib/src/main/java/org/openecomp/sdc/tosca/services/DataModelUtil.java b/openecomp-be/lib/openecomp-tosca-lib/src/main/java/org/openecomp/sdc/tosca/services/DataModelUtil.java index 9b9cd80385..2fc10d2d2b 100644 --- a/openecomp-be/lib/openecomp-tosca-lib/src/main/java/org/openecomp/sdc/tosca/services/DataModelUtil.java +++ b/openecomp-be/lib/openecomp-tosca-lib/src/main/java/org/openecomp/sdc/tosca/services/DataModelUtil.java @@ -30,6 +30,9 @@ import java.util.ListIterator; import java.util.Map; import java.util.Objects; import java.util.Optional; + +import lombok.AccessLevel; +import lombok.NoArgsConstructor; import org.apache.commons.collections4.CollectionUtils; import org.apache.commons.collections4.MapUtils; import org.onap.sdc.tosca.datatypes.model.AttributeDefinition; @@ -67,16 +70,13 @@ import org.openecomp.sdc.tosca.services.impl.ToscaAnalyzerServiceImpl; /** * The type Data model util. */ +@NoArgsConstructor(access = AccessLevel.PRIVATE) public class DataModelUtil { private static final Logger LOGGER = LoggerFactory.getLogger(DataModelUtil.class); private static final String SERVICE_TEMPLATE = "Service Template"; private static final String NODE_TYPE = "Node Type"; - private DataModelUtil() { - // prevent instantiation - } - /** * Add substitution mapping. * @@ -87,7 +87,7 @@ public class DataModelUtil { SubstitutionMapping substitutionMapping) { if (serviceTemplate == null) { throw new CoreException(new InvalidAddActionNullEntityErrorBuilder("Substitution Mapping", SERVICE_TEMPLATE) - .build()); + .build()); } if (serviceTemplate.getTopology_template() == null) { @@ -117,7 +117,8 @@ public class DataModelUtil { * @param substitutionMappingRequirementList the substitution mapping requirement list */ public static void addSubstitutionMappingReq(ServiceTemplate serviceTemplate, - String substitutionMappingRequirementId, List<String> substitutionMappingRequirementList) { + String substitutionMappingRequirementId, + List<String> substitutionMappingRequirementList) { if (serviceTemplate == null) { throw new CoreException( new InvalidAddActionNullEntityErrorBuilder("Substitution Mapping Requirements", SERVICE_TEMPLATE) @@ -135,7 +136,7 @@ public class DataModelUtil { } serviceTemplate.getTopology_template().getSubstitution_mappings().getRequirements() - .put(substitutionMappingRequirementId, substitutionMappingRequirementList); + .put(substitutionMappingRequirementId, substitutionMappingRequirementList); } /** @@ -146,10 +147,11 @@ public class DataModelUtil { * @param substitutionMappingCapabilityList the substitution mapping capability list */ public static void addSubstitutionMappingCapability(ServiceTemplate serviceTemplate, - String substitutionMappingCapabilityId, List<String> substitutionMappingCapabilityList) { + String substitutionMappingCapabilityId, + List<String> substitutionMappingCapabilityList) { if (serviceTemplate == null) { throw new CoreException(new InvalidAddActionNullEntityErrorBuilder("Substitution Mapping Capabilities", - SERVICE_TEMPLATE).build()); + SERVICE_TEMPLATE).build()); } if (serviceTemplate.getTopology_template() == null) { @@ -163,7 +165,7 @@ public class DataModelUtil { } serviceTemplate.getTopology_template().getSubstitution_mappings().getCapabilities() - .putIfAbsent(substitutionMappingCapabilityId, substitutionMappingCapabilityList); + .putIfAbsent(substitutionMappingCapabilityId, substitutionMappingCapabilityList); } /** @@ -174,7 +176,7 @@ public class DataModelUtil { */ public static Map<String, NodeTemplate> getNodeTemplates(ServiceTemplate serviceTemplate) { if (Objects.isNull(serviceTemplate) || Objects.isNull(serviceTemplate.getTopology_template()) - || MapUtils.isEmpty(serviceTemplate.getTopology_template().getNode_templates())) { + || MapUtils.isEmpty(serviceTemplate.getTopology_template().getNode_templates())) { return new HashMap<>(); } @@ -189,7 +191,7 @@ public class DataModelUtil { */ public static Map<String, GroupDefinition> getGroups(ServiceTemplate serviceTemplate) { if (Objects.isNull(serviceTemplate) || Objects.isNull(serviceTemplate.getTopology_template()) - || MapUtils.isEmpty(serviceTemplate.getTopology_template().getGroups())) { + || MapUtils.isEmpty(serviceTemplate.getTopology_template().getGroups())) { return new HashMap<>(); } @@ -204,7 +206,7 @@ public class DataModelUtil { * @param nodeTemplate the node template */ public static void addNodeTemplate(ServiceTemplate serviceTemplate, String nodeTemplateId, - NodeTemplate nodeTemplate) { + NodeTemplate nodeTemplate) { if (serviceTemplate == null) { throw new CoreException( new InvalidAddActionNullEntityErrorBuilder("Node Template", SERVICE_TEMPLATE).build()); @@ -227,7 +229,7 @@ public class DataModelUtil { * @param capabilities the capability definitions */ public static void addNodeTypeCapabilitiesDef(NodeType nodeType, Map<String, CapabilityDefinition> capabilities) { - if (MapUtils.isEmpty(capabilities) || capabilities.entrySet().isEmpty()) { + if (MapUtils.isEmpty(capabilities)) { return; } @@ -239,12 +241,12 @@ public class DataModelUtil { if (MapUtils.isEmpty(nodeType.getCapabilities())) { nodeType.setCapabilities(new HashMap<>()); } - + for (Map.Entry<String, CapabilityDefinition> entry : capabilities.entrySet()) { nodeType.getCapabilities().put(entry.getKey(), entry.getValue()); } } - + /** * Set capabilities def to node type. * @@ -252,23 +254,25 @@ public class DataModelUtil { * @param capabilities the capability definitions */ public static void setNodeTypeCapabilitiesDef(NodeType nodeType, Map<String, CapabilityDefinition> capabilities) { - if (MapUtils.isEmpty(capabilities) || capabilities.entrySet().isEmpty()) { + if (MapUtils.isEmpty(capabilities)) { return; } if (nodeType == null) { throw new CoreException(new InvalidAddActionNullEntityErrorBuilder("Capability Definition", NODE_TYPE) - .build()); + .build()); } if (MapUtils.isEmpty(nodeType.getCapabilities())) { nodeType.setCapabilities(new HashMap<>()); } - if (capabilities.size() > 0) { + + if (MapUtils.isNotEmpty(capabilities)) { nodeType.setCapabilities(new HashMap<>()); - } - for (Map.Entry<String, CapabilityDefinition> entry : capabilities.entrySet()) { - nodeType.getCapabilities().put(entry.getKey(), entry.getValue()); + + for (Map.Entry<String, CapabilityDefinition> entry : capabilities.entrySet()) { + nodeType.getCapabilities().put(entry.getKey(), entry.getValue()); + } } } @@ -280,7 +284,7 @@ public class DataModelUtil { * @param policyDefinition the policy definition */ public static void addPolicyDefinition(ServiceTemplate serviceTemplate, String policyId, - PolicyDefinition policyDefinition) { + PolicyDefinition policyDefinition) { if (serviceTemplate == null) { throw new CoreException( new InvalidAddActionNullEntityErrorBuilder("Policy Definition", SERVICE_TEMPLATE).build()); @@ -321,7 +325,7 @@ public class DataModelUtil { * @param relationshipTemplate the relationship template */ public static void addRelationshipTemplate(ServiceTemplate serviceTemplate, String relationshipTemplateId, - RelationshipTemplate relationshipTemplate) { + RelationshipTemplate relationshipTemplate) { if (serviceTemplate == null) { throw new CoreException( new InvalidAddActionNullEntityErrorBuilder("Relationship Template", SERVICE_TEMPLATE).build()); @@ -333,7 +337,7 @@ public class DataModelUtil { serviceTemplate.getTopology_template().setRelationship_templates(new HashMap<>()); } serviceTemplate.getTopology_template().getRelationship_templates() - .put(relationshipTemplateId, relationshipTemplate); + .put(relationshipTemplateId, relationshipTemplate); } /** @@ -344,11 +348,11 @@ public class DataModelUtil { * @param requirementAssignment the requirement assignment */ public static void addRequirementAssignment(NodeTemplate nodeTemplate, String requirementId, - RequirementAssignment requirementAssignment) { + RequirementAssignment requirementAssignment) { if (nodeTemplate == null) { throw new CoreException( new InvalidAddActionNullEntityErrorBuilder("Requirement Assignment", - "Node Template").build()); + "Node Template").build()); } if (requirementAssignment.getNode() == null) { throw new CoreException(new InvalidRequirementAssignmentErrorBuilder(requirementId).build()); @@ -385,7 +389,7 @@ public class DataModelUtil { */ public static NodeTemplate getNodeTemplate(ServiceTemplate serviceTemplate, String nodeTemplateId) { if (serviceTemplate == null || serviceTemplate.getTopology_template() == null - || serviceTemplate.getTopology_template().getNode_templates() == null) { + || serviceTemplate.getTopology_template().getNode_templates() == null) { return null; } return serviceTemplate.getTopology_template().getNode_templates().get(nodeTemplateId); @@ -413,7 +417,7 @@ public class DataModelUtil { * @return the requirement definition */ public static Optional<RequirementDefinition> getRequirementDefinition(NodeType nodeType, - String requirementDefinitionId) { + String requirementDefinitionId) { if (nodeType == null || nodeType.getRequirements() == null || requirementDefinitionId == null) { return Optional.empty(); } @@ -452,7 +456,7 @@ public class DataModelUtil { * @return the capability definition */ public static Optional<CapabilityDefinition> getCapabilityDefinition(NodeType nodeType, - String capabilityDefinitionId) { + String capabilityDefinitionId) { if (nodeType == null || nodeType.getCapabilities() == null || capabilityDefinitionId == null) { return Optional.empty(); } @@ -467,7 +471,7 @@ public class DataModelUtil { * @param group the group */ public static void addGroupDefinitionToTopologyTemplate(ServiceTemplate serviceTemplate, String groupName, - GroupDefinition group) { + GroupDefinition group) { if (serviceTemplate == null) { throw new CoreException( new InvalidAddActionNullEntityErrorBuilder("Group Definition", SERVICE_TEMPLATE).build()); @@ -499,7 +503,7 @@ public class DataModelUtil { public static void addGroupMember(ServiceTemplate serviceTemplate, String groupName, String groupMemberId) { TopologyTemplate topologyTemplate = serviceTemplate.getTopology_template(); if (Objects.isNull(topologyTemplate) || topologyTemplate.getGroups() == null - || topologyTemplate.getGroups().get(groupName) == null) { + || topologyTemplate.getGroups().get(groupName) == null) { return; } @@ -525,7 +529,8 @@ public class DataModelUtil { * @return the property definition */ public static ParameterDefinition createParameterDefinition(String type, String description, boolean required, - List<Constraint> constraints, EntrySchema entrySchema, Object defaultVal) { + List<Constraint> constraints, EntrySchema entrySchema, + Object defaultVal) { ParameterDefinition paramDef = new ParameterDefinition(); paramDef.setType(type); paramDef.setDescription(description); @@ -546,7 +551,7 @@ public class DataModelUtil { * @return the requirement definition */ public static RequirementDefinition createRequirement(String capability, String node, String relationship, - Object[] occurrences) { + Object[] occurrences) { RequirementDefinition requirementDefinition = new RequirementDefinition(); requirementDefinition.setCapability(capability); requirementDefinition.setNode(node); @@ -586,7 +591,7 @@ public class DataModelUtil { * @return the map */ public static Map createGetInputPropertyValueFromListParameter(String inputPropertyListName, int indexInTheList, - String... nestedPropertyName) { + String... nestedPropertyName) { List<Object> propertyList = new ArrayList<>(); propertyList.add(inputPropertyListName); propertyList.add(indexInTheList); @@ -617,7 +622,7 @@ public class DataModelUtil { parameterDefinition.setStatus(propertyDefinition.getStatus()); parameterDefinition.setConstraints(propertyDefinition.getConstraints()); parameterDefinition.setEntry_schema(Objects.isNull(propertyDefinition.getEntry_schema()) ? null : - propertyDefinition.getEntry_schema().clone()); + propertyDefinition.getEntry_schema().clone()); parameterDefinition.setHidden(false); parameterDefinition.setImmutable(false); return parameterDefinition; @@ -631,7 +636,7 @@ public class DataModelUtil { * @return the parameter definition ext */ public static ParameterDefinitionExt convertAttributeDefToParameterDef(AttributeDefinition attributeDefinition, - Map<String, List> outputValue) { + Map<String, List> outputValue) { if (attributeDefinition == null) { return null; } @@ -643,7 +648,7 @@ public class DataModelUtil { public static boolean isNodeTemplate(String entryId, ServiceTemplate serviceTemplate) { return serviceTemplate.getTopology_template().getNode_templates() != null - && serviceTemplate.getTopology_template().getNode_templates().get(entryId) != null; + && serviceTemplate.getTopology_template().getNode_templates().get(entryId) != null; } /** @@ -654,7 +659,8 @@ public class DataModelUtil { * @param parameterDefinition the parameter definition */ public static void addInputParameterToTopologyTemplate(ServiceTemplate serviceTemplate, - String parameterDefinitionId, ParameterDefinition parameterDefinition) { + String parameterDefinitionId, + ParameterDefinition parameterDefinition) { if (Objects.isNull(serviceTemplate)) { throw new CoreException( new InvalidAddActionNullEntityErrorBuilder("Topology Template Input Parameter", SERVICE_TEMPLATE) @@ -679,7 +685,8 @@ public class DataModelUtil { * @param parameterDefinition the parameter definition */ public static void addOutputParameterToTopologyTemplate(ServiceTemplate serviceTemplate, - String parameterDefinitionId, ParameterDefinition parameterDefinition) { + String parameterDefinitionId, + ParameterDefinition parameterDefinition) { if (Objects.isNull(serviceTemplate)) { throw new CoreException( new InvalidAddActionNullEntityErrorBuilder("Topology Template Output Parameter", SERVICE_TEMPLATE) @@ -703,7 +710,7 @@ public class DataModelUtil { * @param requirementDef added requirement def */ public static void addRequirementToList(List<Map<String, RequirementDefinition>> requirementList, - Map<String, RequirementDefinition> requirementDef) { + Map<String, RequirementDefinition> requirementDef) { if (requirementDef == null) { return; } @@ -809,7 +816,7 @@ public class DataModelUtil { * @param requirementKey requirement key */ public static void removeRequirementsDefinition(List<Map<String, RequirementDefinition>> requirementsDefinitionList, - String requirementKey) { + String requirementKey) { if (requirementsDefinitionList == null) { return; } @@ -833,7 +840,7 @@ public class DataModelUtil { * @param requirementKey requirement key */ public static void removeRequirementsAssignment(List<Map<String, RequirementAssignment>> requirementsAssignmentList, - String requirementKey) { + String requirementKey) { if (requirementsAssignmentList == null) { return; } @@ -859,7 +866,7 @@ public class DataModelUtil { * @param requirementAssignmentToBeDeleted the requirement assignment to be deleted */ public static void removeRequirementAssignment(NodeTemplate nodeTemplate, String requirementKey, - RequirementAssignment requirementAssignmentToBeDeleted) { + RequirementAssignment requirementAssignmentToBeDeleted) { ToscaAnalyzerService toscaAnalyzerService = new ToscaAnalyzerServiceImpl(); List<Map<String, RequirementAssignment>> nodeTemplateRequirements = nodeTemplate.getRequirements(); if (nodeTemplateRequirements == null) { @@ -872,12 +879,12 @@ public class DataModelUtil { RequirementAssignment requirementAssignment = reqMap.get(requirementKey); if (requirementAssignment != null) { boolean isDesiredRequirementAssignment = toscaAnalyzerService - .isDesiredRequirementAssignment(requirementAssignment, - requirementAssignmentToBeDeleted - .getCapability(), - requirementAssignmentToBeDeleted.getNode(), - requirementAssignmentToBeDeleted - .getRelationship()); + .isDesiredRequirementAssignment(requirementAssignment, + requirementAssignmentToBeDeleted + .getCapability(), + requirementAssignmentToBeDeleted.getNode(), + requirementAssignmentToBeDeleted + .getRelationship()); if (isDesiredRequirementAssignment) { iter.remove(); } @@ -927,7 +934,7 @@ public class DataModelUtil { */ public static ParameterDefinition getOuputParameter(ServiceTemplate serviceTemplate, String outputParameterId) { if (serviceTemplate == null || serviceTemplate.getTopology_template() == null - || serviceTemplate.getTopology_template().getOutputs() == null) { + || serviceTemplate.getTopology_template().getOutputs() == null) { return null; } return serviceTemplate.getTopology_template().getOutputs().get(outputParameterId); @@ -941,7 +948,7 @@ public class DataModelUtil { */ public static Map<String, ParameterDefinition> getInputParameters(ServiceTemplate serviceTemplate) { if (serviceTemplate == null || serviceTemplate.getTopology_template() == null - || serviceTemplate.getTopology_template().getInputs() == null) { + || serviceTemplate.getTopology_template().getInputs() == null) { return null; } return serviceTemplate.getTopology_template().getInputs(); @@ -955,7 +962,7 @@ public class DataModelUtil { */ public static Map<String, RelationshipTemplate> getRelationshipTemplates(ServiceTemplate serviceTemplate) { if (serviceTemplate == null || serviceTemplate.getTopology_template() == null - || serviceTemplate.getTopology_template().getRelationship_templates() == null) { + || serviceTemplate.getTopology_template().getRelationship_templates() == null) { return null; } return serviceTemplate.getTopology_template().getRelationship_templates(); @@ -983,10 +990,10 @@ public class DataModelUtil { * @return node template properties */ public static Map<String, Object> getNodeTemplateProperties(ServiceTemplate serviceTemplate, - String nodeTemplateId) { + String nodeTemplateId) { if (serviceTemplate == null || serviceTemplate.getTopology_template() == null - || serviceTemplate.getTopology_template().getNode_templates() == null - || serviceTemplate.getTopology_template().getNode_templates().get(nodeTemplateId) == null) { + || serviceTemplate.getTopology_template().getNode_templates() == null + || serviceTemplate.getTopology_template().getNode_templates().get(nodeTemplateId) == null) { return null; } return serviceTemplate.getTopology_template().getNode_templates().get(nodeTemplateId).getProperties(); @@ -1019,7 +1026,7 @@ public class DataModelUtil { */ public static SubstitutionMapping getSubstitutionMappings(ServiceTemplate serviceTemplate) { if (serviceTemplate == null || serviceTemplate.getTopology_template() == null - || serviceTemplate.getTopology_template().getSubstitution_mappings() == null) { + || serviceTemplate.getTopology_template().getSubstitution_mappings() == null) { return null; } return serviceTemplate.getTopology_template().getSubstitution_mappings(); @@ -1035,7 +1042,7 @@ public class DataModelUtil { */ public static boolean compareRequirementAssignment(RequirementAssignment first, RequirementAssignment second) { return (first.getCapability().equals(second.getCapability()) && first.getNode().equals(second.getNode()) - && first.getRelationship().equals(second.getRelationship())); + && first.getRelationship().equals(second.getRelationship())); } /** @@ -1120,7 +1127,8 @@ public class DataModelUtil { * @return the substitution mapping */ public static SubstitutionMapping createSubstitutionTemplateSubMapping(String nodeTypeKey, - NodeType substitutionNodeType, Map<String, Map<String, List<String>>> mapping) { + NodeType substitutionNodeType, + Map<String, Map<String, List<String>>> mapping) { SubstitutionMapping substitutionMapping = new SubstitutionMapping(); substitutionMapping.setNode_type(nodeTypeKey); substitutionMapping.setCapabilities(manageCapabilityMapping(substitutionNodeType.getCapabilities(), @@ -1139,7 +1147,8 @@ public class DataModelUtil { * @param capabilityAttributes the capability attributes */ public static void addNodeTemplateCapability(NodeTemplate nodeTemplate, String capabilityId, - Map<String, Object> capabilityProperties, Map<String, Object> capabilityAttributes) { + Map<String, Object> capabilityProperties, + Map<String, Object> capabilityAttributes) { Map<String, CapabilityAssignment> capabilities = nodeTemplate.getCapabilities(); if (Objects.isNull(capabilities)) { capabilities = new HashMap<>(); @@ -1171,7 +1180,7 @@ public class DataModelUtil { } private static Map<String, List<String>> manageCapabilityMapping(Map<String, CapabilityDefinition> capabilities, - Map<String, List<String>> capabilitySubstitutionMapping) { + Map<String, List<String>> capabilitySubstitutionMapping) { if (capabilities == null) { return null; } @@ -1188,7 +1197,8 @@ public class DataModelUtil { } public static void addSubstitutionNodeTypeRequirements(NodeType substitutionNodeType, - List<Map<String, RequirementDefinition>> requirementsList, String templateName) { + List<Map<String, RequirementDefinition>> requirementsList, + String templateName) { if (CollectionUtils.isEmpty(requirementsList)) { return; } @@ -1218,10 +1228,10 @@ public class DataModelUtil { * @return the relationship template */ public static Optional<RelationshipTemplate> getRelationshipTemplate(ServiceTemplate serviceTemplate, - String relationshipId) { + String relationshipId) { if (serviceTemplate == null || serviceTemplate.getTopology_template() == null - || serviceTemplate.getTopology_template().getRelationship_templates() == null - || serviceTemplate.getTopology_template().getRelationship_templates().get(relationshipId) == null) { + || serviceTemplate.getTopology_template().getRelationship_templates() == null + || serviceTemplate.getTopology_template().getRelationship_templates().get(relationshipId) == null) { return Optional.empty(); } return Optional.of(serviceTemplate.getTopology_template().getRelationship_templates().get(relationshipId)); diff --git a/openecomp-be/lib/openecomp-tosca-lib/src/main/java/org/openecomp/sdc/tosca/services/ToscaConstants.java b/openecomp-be/lib/openecomp-tosca-lib/src/main/java/org/openecomp/sdc/tosca/services/ToscaConstants.java index d81983057d..14b216a040 100644 --- a/openecomp-be/lib/openecomp-tosca-lib/src/main/java/org/openecomp/sdc/tosca/services/ToscaConstants.java +++ b/openecomp-be/lib/openecomp-tosca-lib/src/main/java/org/openecomp/sdc/tosca/services/ToscaConstants.java @@ -70,6 +70,7 @@ public class ToscaConstants { public static final String HEAT_NODE_TYPE_SUFFIX = "heat."; public static final String CAPABILITY = "capability"; public static final String REQUIREMENT = "requirement"; + public static final String SERVICE_TEMPLATE_FILE_POSTFIX = "ServiceTemplate.yaml"; private ToscaConstants() { //Hiding the implicit public constructor diff --git a/openecomp-be/lib/openecomp-tosca-lib/src/main/java/org/openecomp/sdc/tosca/services/ToscaUtil.java b/openecomp-be/lib/openecomp-tosca-lib/src/main/java/org/openecomp/sdc/tosca/services/ToscaUtil.java index 0cbc71fcb6..7c8e48ac88 100644 --- a/openecomp-be/lib/openecomp-tosca-lib/src/main/java/org/openecomp/sdc/tosca/services/ToscaUtil.java +++ b/openecomp-be/lib/openecomp-tosca-lib/src/main/java/org/openecomp/sdc/tosca/services/ToscaUtil.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,11 +20,6 @@ package org.openecomp.sdc.tosca.services; -import org.apache.commons.collections4.MapUtils; -import org.onap.sdc.tosca.datatypes.model.NodeTemplate; -import org.onap.sdc.tosca.datatypes.model.ServiceTemplate; -import org.openecomp.core.utilities.file.FileUtils; - import java.util.Collections; import java.util.HashMap; import java.util.Map; @@ -32,102 +27,111 @@ import java.util.Objects; import java.util.Optional; import java.util.UUID; +import lombok.AccessLevel; +import lombok.NoArgsConstructor; +import org.apache.commons.collections4.MapUtils; +import org.onap.sdc.tosca.datatypes.model.NodeTemplate; +import org.onap.sdc.tosca.datatypes.model.ServiceTemplate; +import org.openecomp.core.utilities.file.FileUtils; + /** * The type Tosca util. */ +@NoArgsConstructor(access = AccessLevel.PRIVATE) public class ToscaUtil { - /** - * Gets service template file name. - * - * @param serviceTemplate the service template - * @return the service template file name - */ - public static String getServiceTemplateFileName(ServiceTemplate serviceTemplate) { - if (serviceTemplate == null) { - return null; + /** + * Gets service template file name. + * + * @param serviceTemplate the service template + * @return the service template file name + */ + public static String getServiceTemplateFileName(ServiceTemplate serviceTemplate) { + if (serviceTemplate == null) { + return null; + } + if (serviceTemplate.getMetadata() == null) { + return UUID.randomUUID().toString() + ToscaConstants.SERVICE_TEMPLATE_FILE_POSTFIX; + } + return getServiceTemplateFileName(serviceTemplate.getMetadata()); } - if (serviceTemplate.getMetadata() == null) { - return UUID.randomUUID().toString() + "ServiceTemplate.yaml"; + + /** + * Gets service template file name. + * + * @param metadata the file name + * @return the service template file name + */ + public static String getServiceTemplateFileName(Map<String, String> metadata) { + if (metadata.get(ToscaConstants.ST_METADATA_FILE_NAME) != null) { + return metadata.get(ToscaConstants.ST_METADATA_FILE_NAME); + } else if (metadata.get(ToscaConstants.ST_METADATA_TEMPLATE_NAME) != null) { + return metadata.get(ToscaConstants.ST_METADATA_TEMPLATE_NAME) + + ToscaConstants.SERVICE_TEMPLATE_FILE_POSTFIX; + } + return UUID.randomUUID().toString() + ToscaConstants.SERVICE_TEMPLATE_FILE_POSTFIX; + } - return getServiceTemplateFileName(serviceTemplate.getMetadata()); - } - - /** - * Gets service template file name. - * - * @param metadata the file name - * @return the service template file name - */ - public static String getServiceTemplateFileName(Map<String, String> metadata) { - if (metadata.get(ToscaConstants.ST_METADATA_FILE_NAME) != null) { - return metadata.get(ToscaConstants.ST_METADATA_FILE_NAME); - } else if (metadata.get(ToscaConstants.ST_METADATA_TEMPLATE_NAME) != null) { - return metadata.get(ToscaConstants.ST_METADATA_TEMPLATE_NAME) + "ServiceTemplate.yaml"; + + public static String getServiceTemplateFileName(String templateName) { + Map<String, String> metadata = new HashMap<>(); + metadata.put(ToscaConstants.ST_METADATA_TEMPLATE_NAME, templateName); + return getServiceTemplateFileName(metadata); } - return UUID.randomUUID().toString() + "ServiceTemplate.yaml"; - } + public static Optional<String> getSubstitutableGroupMemberId(String heatFileName, + ServiceTemplate serviceTemplate) { + + Map<String, NodeTemplate> nodeTemplates = + DataModelUtil.getNodeTemplates(serviceTemplate); + + if (MapUtils.isEmpty(nodeTemplates)) { + return Optional.empty(); + } - public static Optional<String> getSubstitutableGroupMemberId(String heatFileName, - ServiceTemplate serviceTemplate){ + String heatFileNameWithoutExt = FileUtils.getFileWithoutExtention(heatFileName); - Map<String, NodeTemplate> nodeTemplates = - DataModelUtil.getNodeTemplates(serviceTemplate); + for (Map.Entry<String, NodeTemplate> nodeTemplateEntry : nodeTemplates.entrySet()) { + String subServiceTemplateName = + getSubstitutionServiceTemplateNameFromProperties(nodeTemplateEntry); - if(MapUtils.isEmpty(nodeTemplates)){ - return Optional.empty(); + if (Objects.nonNull(subServiceTemplateName) + && isGroupMemberIdSubstitutable(heatFileNameWithoutExt, subServiceTemplateName)) { + return Optional.of(nodeTemplateEntry.getKey()); + } + } + + return Optional.empty(); + } + + private static boolean isGroupMemberIdSubstitutable(String heatFileNameWithoutExt, + String subServiceTemplateName) { + return subServiceTemplateName.startsWith(heatFileNameWithoutExt); } - String heatFileNameWithoutExt = FileUtils.getFileWithoutExtention(heatFileName); + private static String getSubstitutionServiceTemplateNameFromProperties( + Map.Entry<String, NodeTemplate> nodeTemplateEntry) { + Map<String, Object> properties = + nodeTemplateEntry.getValue().getProperties() == null ? Collections.emptyMap() : + nodeTemplateEntry.getValue().getProperties(); - for(Map.Entry<String, NodeTemplate> nodeTemplateEntry : nodeTemplates.entrySet()){ - String subServiceTemplateName = - getSubstitutionServiceTemplateNameFromProperties(nodeTemplateEntry); + Map<String, Object> serviceTemplateFilter = + properties.containsKey(ToscaConstants.SERVICE_TEMPLATE_FILTER_PROPERTY_NAME) ? + (Map<String, Object>) properties.get(ToscaConstants + .SERVICE_TEMPLATE_FILTER_PROPERTY_NAME) : Collections.emptyMap(); - if(Objects.nonNull(subServiceTemplateName) - && isGroupMemberIdSubstitutable(heatFileNameWithoutExt, subServiceTemplateName)){ - return Optional.of(nodeTemplateEntry.getKey()); - } + return (String) serviceTemplateFilter.get(ToscaConstants.SUBSTITUTE_SERVICE_TEMPLATE_PROPERTY_NAME); } - return Optional.empty(); - } - - private static boolean isGroupMemberIdSubstitutable(String heatFileNameWithoutExt, - String subServiceTemplateName) { - return subServiceTemplateName.startsWith(heatFileNameWithoutExt); - } - - private static String getSubstitutionServiceTemplateNameFromProperties( - Map.Entry<String, NodeTemplate> nodeTemplateEntry) { - Map<String, Object> properties = - nodeTemplateEntry.getValue().getProperties() == null ? Collections.emptyMap() : - nodeTemplateEntry.getValue().getProperties(); - - Map<String, Object> serviceTemplateFilter = - properties.containsKey(ToscaConstants.SERVICE_TEMPLATE_FILTER_PROPERTY_NAME)? - (Map<String, Object>) properties.get(ToscaConstants - .SERVICE_TEMPLATE_FILTER_PROPERTY_NAME) : Collections.emptyMap(); - - return (String) serviceTemplateFilter.get(ToscaConstants.SUBSTITUTE_SERVICE_TEMPLATE_PROPERTY_NAME); - } - - - /** - * Add service template to map with key file name. - * - * @param serviceTemplateMap the service template map - * @param serviceTemplate the service template - */ - public static void addServiceTemplateToMapWithKeyFileName( - Map<String, ServiceTemplate> serviceTemplateMap, ServiceTemplate serviceTemplate) { - serviceTemplateMap.put(ToscaUtil.getServiceTemplateFileName(serviceTemplate), serviceTemplate); - } - - public static String getServiceTemplateFileName(String templateName) { - Map<String, String> metadata = new HashMap<>(); - metadata.put(ToscaConstants.ST_METADATA_TEMPLATE_NAME, templateName); - return getServiceTemplateFileName(metadata); - } + + /** + * Add service template to map with key file name. + * + * @param serviceTemplateMap the service template map + * @param serviceTemplate the service template + */ + public static void addServiceTemplateToMapWithKeyFileName( + Map<String, ServiceTemplate> serviceTemplateMap, ServiceTemplate serviceTemplate) { + serviceTemplateMap.put(ToscaUtil.getServiceTemplateFileName(serviceTemplate), serviceTemplate); + } } diff --git a/openecomp-be/lib/openecomp-tosca-lib/src/test/java/org/openecomp/sdc/tosca/services/DataModelUtilTest.java b/openecomp-be/lib/openecomp-tosca-lib/src/test/java/org/openecomp/sdc/tosca/services/DataModelUtilTest.java index a009899003..88bcc94d12 100644 --- a/openecomp-be/lib/openecomp-tosca-lib/src/test/java/org/openecomp/sdc/tosca/services/DataModelUtilTest.java +++ b/openecomp-be/lib/openecomp-tosca-lib/src/test/java/org/openecomp/sdc/tosca/services/DataModelUtilTest.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,18 +20,49 @@ package org.openecomp.sdc.tosca.services; +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertFalse; +import static org.junit.Assert.assertNotNull; +import static org.junit.Assert.assertNotSame; +import static org.junit.Assert.assertNull; +import static org.junit.Assert.assertTrue; + +import java.util.AbstractMap; +import java.util.ArrayList; +import java.util.Collections; +import java.util.HashMap; +import java.util.List; +import java.util.Map; +import java.util.Optional; +import java.util.stream.Collectors; +import java.util.stream.Stream; + +import org.apache.commons.collections4.MapUtils; import org.junit.Assert; import org.junit.Rule; import org.junit.Test; import org.junit.rules.ExpectedException; import org.junit.runner.RunWith; import org.mockito.runners.MockitoJUnitRunner; -import org.onap.sdc.tosca.datatypes.model.*; +import org.onap.sdc.tosca.datatypes.model.AttributeDefinition; +import org.onap.sdc.tosca.datatypes.model.CapabilityDefinition; +import org.onap.sdc.tosca.datatypes.model.EntrySchema; +import org.onap.sdc.tosca.datatypes.model.GroupDefinition; +import org.onap.sdc.tosca.datatypes.model.Import; +import org.onap.sdc.tosca.datatypes.model.NodeTemplate; +import org.onap.sdc.tosca.datatypes.model.NodeType; +import org.onap.sdc.tosca.datatypes.model.ParameterDefinition; +import org.onap.sdc.tosca.datatypes.model.PolicyDefinition; +import org.onap.sdc.tosca.datatypes.model.PropertyDefinition; +import org.onap.sdc.tosca.datatypes.model.RelationshipTemplate; +import org.onap.sdc.tosca.datatypes.model.RequirementAssignment; +import org.onap.sdc.tosca.datatypes.model.RequirementDefinition; +import org.onap.sdc.tosca.datatypes.model.ServiceTemplate; +import org.onap.sdc.tosca.datatypes.model.SubstitutionMapping; +import org.onap.sdc.tosca.datatypes.model.TopologyTemplate; +import org.onap.sdc.tosca.datatypes.model.heatextend.ParameterDefinitionExt; import org.openecomp.sdc.common.errors.CoreException; - -import java.util.ArrayList; -import java.util.HashMap; -import java.util.Optional; +import org.openecomp.sdc.tosca.datatypes.ToscaFunctions; /** * @author shiria @@ -40,116 +71,924 @@ import java.util.Optional; @RunWith(MockitoJUnitRunner.class) public class DataModelUtilTest { - @Rule - public ExpectedException thrown = ExpectedException.none(); - - @Test - public void testAddSubstitutionMapping() throws Exception { - thrown.expect(CoreException.class); - thrown.expectMessage( - "Invalid action, can't add 'Substitution Mapping' to 'Service Template', 'Service Template' entity is NULL."); - DataModelUtil.addSubstitutionMapping(null, new SubstitutionMapping()); - } - - @Test - public void testAddSubstitutionMappingReq() throws Exception { - thrown.expect(CoreException.class); - thrown.expectMessage( - "Invalid action, can't add 'Substitution Mapping Requirements' to 'Service Template', 'Service Template' entity is NULL."); - DataModelUtil.addSubstitutionMappingReq(null, "123", new ArrayList<>()); - } - - @Test - public void testAddNodeTemplate() throws Exception { - thrown.expect(CoreException.class); - thrown.expectMessage( - "Invalid action, can't add 'Node Template' to 'Service Template', 'Service Template' entity is NULL."); - DataModelUtil.addNodeTemplate(null, "123", new NodeTemplate()); - } - - @Test - public void testAddPolicyDefinition() throws Exception { - thrown.expect(CoreException.class); - thrown.expectMessage( - "Invalid action, can't add 'Policy Definition' to 'Service Template', 'Service Template' entity is NULL."); - DataModelUtil.addPolicyDefinition(null, "123", new PolicyDefinition()); - } - - @Test - public void testAddNodeType() throws Exception { - thrown.expect(CoreException.class); - thrown.expectMessage( - "Invalid action, can't add 'Node Type' to 'Service Template', 'Service Template' entity is NULL."); - DataModelUtil.addNodeType(null, "123", new NodeType()); - } - - @Test - public void testAddRelationshipTemplate() throws Exception { - thrown.expect(CoreException.class); - thrown.expectMessage( - "Invalid action, can't add 'Relationship Template' to 'Service Template', 'Service Template' entity is NULL."); - DataModelUtil.addRelationshipTemplate(null, "123", new RelationshipTemplate()); - } - - @Test - public void testAddRequirementAssignment() throws Exception { - thrown.expect(CoreException.class); - thrown.expectMessage( - "Invalid action, can't add 'Requirement Assignment' to 'Node Template', 'Node Template' entity is NULL."); - DataModelUtil.addRequirementAssignment(null, "123", new RequirementAssignment()); - } - - @Test - public void testGetNodeTemplate() throws Exception { - thrown.expect(CoreException.class); - thrown.expectMessage( - "Invalid action, can't add 'Node Template' to 'Service Template', 'Service Template' entity is NULL."); - DataModelUtil.addNodeTemplate(null, "123", new NodeTemplate()); - } - - @Test - public void testGetNodeType() throws Exception { - thrown.expect(CoreException.class); - thrown.expectMessage( - "Invalid action, can't add 'Node Type' to 'Service Template', 'Service Template' entity is NULL."); - DataModelUtil.addNodeType(null, "123", new NodeType()); - } - - @Test - public void testAddGroupToTopologyTemplate() throws Exception { - thrown.expect(CoreException.class); - thrown.expectMessage( - "Invalid action, can't add 'Group Definition' to 'Service Template', 'Service Template' entity is NULL."); - DataModelUtil.addGroupDefinitionToTopologyTemplate(null, "123", new GroupDefinition()); - } - - - @Test - public void testGetRelationshipTemplate(){ - RelationshipTemplate relationshipTemplate = new RelationshipTemplate(); - String testingRelationshipType = "testingRelationshipType"; - relationshipTemplate.setType(testingRelationshipType); - TopologyTemplate topologyTemplate = new TopologyTemplate(); - topologyTemplate.setRelationship_templates(new HashMap<>()); - String relationId = "rtest"; - topologyTemplate.getRelationship_templates().put(relationId, relationshipTemplate); - ServiceTemplate serviceTemplate = new ServiceTemplate(); - serviceTemplate.setTopology_template(topologyTemplate); - - Optional<RelationshipTemplate> relationshipTemplateOut = - DataModelUtil.getRelationshipTemplate(serviceTemplate, relationId); - Assert.assertNotNull(relationshipTemplateOut); - Assert.assertEquals(true,relationshipTemplateOut.isPresent()); - Assert.assertEquals(testingRelationshipType, relationshipTemplateOut.get().getType()); - } - - @Test - public void testGetEmptyRelationshipTemplate(){ - ServiceTemplate serviceTemplate = new ServiceTemplate(); - String relationId = "rtest"; - Optional<RelationshipTemplate> relationshipTemplateOut = - DataModelUtil.getRelationshipTemplate(serviceTemplate, relationId); - Assert.assertNotNull(relationshipTemplateOut); - Assert.assertEquals(false,relationshipTemplateOut.isPresent()); - } + private static final String REQUIREMENT_ID = "requirementId"; + private static final String REQUIREMENT_DEFINITION_ID = "requirementDefinitionId"; + private static final String NODE_TEMPLATE_ID = "nodeTemplateId"; + private static final String NODE_TYPE_ID = "nodeTypeId"; + private static final String CAPABILITY_ID = "capabilityId"; + private static final String PROPERTY_ID = "propertyId"; + private static final String NODE_TYPE_KEY = "nodeTypeKey"; + private static final String TEMPLATE_NAME = "templateName"; + private static final String OUTPUT_ID = "outputId"; + private static final String REQUIREMENT_KEY = "requirementKey"; + private static final String NODE_ID = "nodeId"; + private static final String PARAMETER_ID = "parameterId"; + private static final String ENTRY_ID = "entryId"; + private static final String PROPERTY_DEF_TYPE = "propertyDefType"; + + @Rule + public ExpectedException thrown = ExpectedException.none(); + + @Test + public void testAddSubstitutionMappingTopolgyTemplateNull() { + ServiceTemplate serviceTemplate = new ServiceTemplate(); + DataModelUtil.addSubstitutionMapping(serviceTemplate, new SubstitutionMapping()); + + Assert.assertNotNull(serviceTemplate.getTopology_template()); + Assert.assertNotNull(serviceTemplate.getTopology_template().getSubstitution_mappings()); + } + + @Test + public void testAddSubstitutionMapping() throws Exception { + thrown.expect(CoreException.class); + thrown.expectMessage( + "Invalid action, can't add 'Substitution Mapping' to 'Service Template', 'Service Template' entity is NULL."); + DataModelUtil.addSubstitutionMapping(null, new SubstitutionMapping()); + } + + @Test + public void testGetDirectivesNodeTemplateNull() { + assertTrue(DataModelUtil.getDirectives(null).isEmpty()); + } + + @Test + public void testGetDirectivesWhenDirectivesNull() { + assertTrue(DataModelUtil.getDirectives(new NodeTemplate()).isEmpty()); + } + + @Test + public void testGetDirectives() { + NodeTemplate nodeTemplate = new NodeTemplate(); + nodeTemplate.setDirectives(Collections.singletonList("directive")); + + Assert.assertEquals(1, DataModelUtil.getDirectives(nodeTemplate).size()); + } + + @Test + public void testAddSubstitutionMappingReq() { + ServiceTemplate serviceTemplate = new ServiceTemplate(); + DataModelUtil.addSubstitutionMappingReq(serviceTemplate, + REQUIREMENT_ID, Collections.singletonList("requirement")); + + Assert.assertNotNull(serviceTemplate.getTopology_template().getSubstitution_mappings().getRequirements()); + Assert.assertEquals(1, + serviceTemplate.getTopology_template().getSubstitution_mappings().getRequirements().size()); + } + + @Test + public void testAddSubstitutionMappingReqServiceTemplateNull() { + thrown.expect(CoreException.class); + thrown.expectMessage( + "Invalid action, can't add 'Substitution Mapping Requirements' to 'Service Template', 'Service Template' entity is NULL."); + DataModelUtil.addSubstitutionMappingReq(null, REQUIREMENT_ID, Collections.emptyList()); + } + + @Test + public void testAddSubstitutionMappingCapabilityServiceTemplateNull() { + thrown.expect(CoreException.class); + thrown.expectMessage( + "Invalid action, can't add 'Substitution Mapping Capabilities' to 'Service Template', 'Service Template' entity is NULL."); + DataModelUtil.addSubstitutionMappingCapability(null, CAPABILITY_ID, Collections.emptyList()); + } + + @Test + public void testAddSubstitutionMappingCapability() { + ServiceTemplate serviceTemplate = new ServiceTemplate(); + DataModelUtil.addSubstitutionMappingCapability(serviceTemplate, + CAPABILITY_ID, Collections.singletonList("requirement")); + + Assert.assertNotNull(serviceTemplate.getTopology_template().getSubstitution_mappings().getCapabilities()); + Assert.assertEquals(1, + serviceTemplate.getTopology_template().getSubstitution_mappings().getCapabilities().size()); + } + + @Test + public void testGetNodeTemplatesNull() { + ServiceTemplate serviceTemplate = new ServiceTemplate(); + serviceTemplate.setTopology_template(new TopologyTemplate()); + + Map<String, NodeTemplate> nodeTemplateMap = DataModelUtil.getNodeTemplates(serviceTemplate); + + Assert.assertNotNull(nodeTemplateMap); + assertTrue(MapUtils.isEmpty(nodeTemplateMap)); + } + + @Test + public void testGetNodeTemplates() { + ServiceTemplate serviceTemplate = new ServiceTemplate(); + serviceTemplate.setTopology_template(new TopologyTemplate()); + + Map<String, NodeTemplate> nodeTemplateMap = Stream.of(new AbstractMap.SimpleEntry<>("nodeTemplate1", new + NodeTemplate())).collect( + Collectors.toMap(AbstractMap.SimpleEntry::getKey, AbstractMap.SimpleEntry::getValue)); + + serviceTemplate.getTopology_template().setNode_templates(nodeTemplateMap); + + nodeTemplateMap = DataModelUtil.getNodeTemplates(serviceTemplate); + + Assert.assertNotNull(nodeTemplateMap); + Assert.assertEquals(1, nodeTemplateMap.size()); + } + + @Test + public void testGetGroupsNull() { + ServiceTemplate serviceTemplate = new ServiceTemplate(); + serviceTemplate.setTopology_template(new TopologyTemplate()); + + Map<String, GroupDefinition> nodeTemplateMap = DataModelUtil.getGroups(serviceTemplate); + + Assert.assertNotNull(nodeTemplateMap); + assertTrue(MapUtils.isEmpty(nodeTemplateMap)); + } + + @Test + public void testGetGroups() { + ServiceTemplate serviceTemplate = new ServiceTemplate(); + serviceTemplate.setTopology_template(new TopologyTemplate()); + + Map<String, GroupDefinition> nodeTemplateMap = Stream.of(new AbstractMap.SimpleEntry<>("group1", new + GroupDefinition())).collect( + Collectors.toMap(AbstractMap.SimpleEntry::getKey, AbstractMap.SimpleEntry::getValue)); + + serviceTemplate.getTopology_template().setGroups(nodeTemplateMap); + + nodeTemplateMap = DataModelUtil.getGroups(serviceTemplate); + + Assert.assertNotNull(nodeTemplateMap); + Assert.assertEquals(1, nodeTemplateMap.size()); + } + + @Test + public void testAddNodeTemplateServiceTemplateNull() { + thrown.expect(CoreException.class); + thrown.expectMessage( + "Invalid action, can't add 'Node Template' to 'Service Template', 'Service Template' entity is NULL."); + DataModelUtil.addNodeTemplate(null, "123", new NodeTemplate()); + } + + @Test + public void testAddNodeTemplate() { + ServiceTemplate serviceTemplate = new ServiceTemplate(); + DataModelUtil.addNodeTemplate(serviceTemplate, NODE_TEMPLATE_ID, new NodeTemplate()); + + assertEquals(1, serviceTemplate.getTopology_template().getNode_templates().size()); + } + + @Test + public void testAddNodeTypeCapabilitiesDefCapabilitiesNull() { + NodeType nodeType = new NodeType(); + DataModelUtil.addNodeTypeCapabilitiesDef(nodeType, null); + + assertNull(nodeType.getCapabilities()); + } + + @Test(expected = CoreException.class) + public void testAddNodeTypeCapabilitiesDefThrowsException() { + Map<String, CapabilityDefinition> capabilityDefinitionMap = + Stream.of(new AbstractMap.SimpleEntry<>(CAPABILITY_ID, new + CapabilityDefinition())).collect( + Collectors.toMap(AbstractMap.SimpleEntry::getKey, AbstractMap.SimpleEntry::getValue)); + + DataModelUtil.addNodeTypeCapabilitiesDef(null, capabilityDefinitionMap); + } + + @Test + public void testAddNodeTypeCapabilitiesDef() { + NodeType nodeType = new NodeType(); + Map<String, CapabilityDefinition> capabilityDefinitionMap = + Stream.of(new AbstractMap.SimpleEntry<>(CAPABILITY_ID, new + CapabilityDefinition())).collect( + Collectors.toMap(AbstractMap.SimpleEntry::getKey, AbstractMap.SimpleEntry::getValue)); + + DataModelUtil.addNodeTypeCapabilitiesDef(nodeType, capabilityDefinitionMap); + + Assert.assertEquals(1, nodeType.getCapabilities().size()); + } + + @Test + public void testSetNodeTypeCapabilitiesDefCapabilitiesNull() { + NodeType nodeType = new NodeType(); + DataModelUtil.setNodeTypeCapabilitiesDef(nodeType, null); + + assertNull(nodeType.getCapabilities()); + } + + @Test(expected = CoreException.class) + public void testSetNodeTypeCapabilitiesDefThrowsException() { + Map<String, CapabilityDefinition> capabilityDefinitionMap = + Stream.of(new AbstractMap.SimpleEntry<>(CAPABILITY_ID, + new CapabilityDefinition())).collect( + Collectors.toMap(AbstractMap.SimpleEntry::getKey, AbstractMap.SimpleEntry::getValue)); + + DataModelUtil.setNodeTypeCapabilitiesDef(null, capabilityDefinitionMap); + } + + @Test + public void testSetNodeTypeCapabilitiesDef() { + NodeType nodeType = new NodeType(); + Map<String, CapabilityDefinition> capabilityDefinitionMap = + Stream.of(new AbstractMap.SimpleEntry<>(CAPABILITY_ID, new + CapabilityDefinition())).collect( + Collectors.toMap(AbstractMap.SimpleEntry::getKey, AbstractMap.SimpleEntry::getValue)); + + DataModelUtil.setNodeTypeCapabilitiesDef(nodeType, capabilityDefinitionMap); + + Assert.assertEquals(1, nodeType.getCapabilities().size()); + } + + @Test + public void testGetCapabilityDefinitionCapabilityDefinitionIdNull() { + NodeType nodeType = new NodeType(); + nodeType.setCapabilities(new HashMap<>()); + assertFalse(DataModelUtil.getCapabilityDefinition(nodeType, null).isPresent()); + } + + @Test + public void testGetCapabilityDefinition() { + NodeType nodeType = new NodeType(); + nodeType.setCapabilities(Collections.singletonMap("capabilityDefinitionId", new CapabilityDefinition())); + assertTrue(DataModelUtil.getCapabilityDefinition(nodeType, "capabilityDefinitionId").isPresent()); + } + + @Test + public void testAddPolicyDefinitionThrowsException() { + thrown.expect(CoreException.class); + thrown.expectMessage( + "Invalid action, can't add 'Policy Definition' to 'Service Template', 'Service Template' entity is NULL."); + DataModelUtil.addPolicyDefinition(null, "policyId", new PolicyDefinition()); + } + + @Test + public void testAddPolicyDefinition() { + ServiceTemplate serviceTemplate = new ServiceTemplate(); + DataModelUtil.addPolicyDefinition(serviceTemplate, "policyId", new PolicyDefinition()); + assertEquals(1, serviceTemplate.getTopology_template().getPolicies().size()); + } + + @Test + public void testAddNodeTypeThrowsException() { + thrown.expect(CoreException.class); + thrown.expectMessage( + "Invalid action, can't add 'Node Type' to 'Service Template', 'Service Template' entity is NULL."); + DataModelUtil.addNodeType(null, NODE_TYPE_ID, new NodeType()); + } + + @Test + public void testAddNodeType() { + ServiceTemplate serviceTemplate = new ServiceTemplate(); + DataModelUtil.addNodeType(serviceTemplate, NODE_TYPE_ID, new NodeType()); + + assertEquals(1, serviceTemplate.getNode_types().size()); + } + + @Test + public void testAddRelationshipTemplateThrowsException() { + thrown.expect(CoreException.class); + thrown.expectMessage( + "Invalid action, can't add 'Relationship Template' to 'Service Template', 'Service Template' entity is NULL."); + DataModelUtil.addRelationshipTemplate(null, "relationshipTemplateId", new RelationshipTemplate()); + } + + @Test + public void testAddRelationshipTemplate() { + ServiceTemplate serviceTemplate = new ServiceTemplate(); + DataModelUtil.addRelationshipTemplate(serviceTemplate, "relationshipTemplateId", new RelationshipTemplate()); + + assertEquals(1, serviceTemplate.getTopology_template().getRelationship_templates().size()); + } + + @Test + public void testAddRequirementAssignmentThrowsException() { + thrown.expect(CoreException.class); + thrown.expectMessage( + "Invalid action, can't add 'Requirement Assignment' to 'Node Template', 'Node Template' entity is NULL."); + DataModelUtil.addRequirementAssignment(null, REQUIREMENT_ID, new RequirementAssignment()); + } + + @Test(expected = CoreException.class) + public void testAddRequirementAssignmentNodeNotAssigned() { + DataModelUtil.addRequirementAssignment(new NodeTemplate(), REQUIREMENT_ID, new RequirementAssignment()); + } + + @Test + public void testAddRequirementAssignment() { + NodeTemplate nodeTemplate = new NodeTemplate(); + RequirementAssignment requirementAssignment = new RequirementAssignment(); + requirementAssignment.setNode("node"); + DataModelUtil.addRequirementAssignment(nodeTemplate, REQUIREMENT_ID, requirementAssignment); + + assertEquals(1, nodeTemplate.getRequirements().size()); + } + + @Test + public void testCreateAttachmentRequirementAssignment() { + assertNotNull(DataModelUtil.createAttachmentRequirementAssignment("node")); + } + + @Test + public void testGetNodeTemplate() { + ServiceTemplate serviceTemplate = new ServiceTemplate(); + serviceTemplate.setTopology_template(new TopologyTemplate()); + + assertNull(DataModelUtil.getNodeTemplate(serviceTemplate, NODE_TEMPLATE_ID)); + } + + @Test + public void testGetNodeType() { + assertNull(DataModelUtil.getNodeType(new ServiceTemplate(), NODE_TYPE_ID)); + } + + @Test + public void testGetRequirementDefinitionRequirementIdIsNull() { + assertFalse(DataModelUtil.getRequirementDefinition(new NodeType(), null).isPresent()); + } + + @Test + public void testGetRequirementDefinitionListIsEmpty() { + NodeType nodeType = new NodeType(); + + nodeType.setRequirements(Collections.emptyList()); + assertFalse(DataModelUtil.getRequirementDefinition(new NodeType(), REQUIREMENT_DEFINITION_ID).isPresent()); + } + + @Test + public void testGetRequirementDefinitionWrongKey() { + Map<String, RequirementDefinition> requirementDefinitionMap = + Stream.of(new AbstractMap.SimpleEntry<>(REQUIREMENT_ID, new RequirementDefinition())) + .collect(Collectors.toMap(AbstractMap.SimpleEntry::getKey, AbstractMap.SimpleEntry::getValue)); + + assertFalse(DataModelUtil.getRequirementDefinition(Collections.singletonList(requirementDefinitionMap), + "wrongKey").isPresent()); + } + + @Test + public void testAddGroupToTopologyTemplateThrowsException() { + thrown.expect(CoreException.class); + thrown.expectMessage( + "Invalid action, can't add 'Group Definition' to 'Service Template', 'Service Template' entity is NULL."); + DataModelUtil.addGroupDefinitionToTopologyTemplate(null, "groupId", new GroupDefinition()); + } + + @Test + public void testAddGroupToTopologyTemplate() { + ServiceTemplate serviceTemplate = new ServiceTemplate(); + + DataModelUtil.addGroupDefinitionToTopologyTemplate(serviceTemplate, "groupId", new GroupDefinition()); + + assertEquals(1, serviceTemplate.getTopology_template().getGroups().size()); + } + + @Test + public void testAddRequirementToList() { + Map<String, RequirementDefinition> requirementDefinitionMap = new HashMap<>(); + requirementDefinitionMap.put("requirmentDefinitionId", new RequirementDefinition()); + + List<Map<String, RequirementDefinition>> mapList = new ArrayList<>(); + + DataModelUtil.addRequirementToList(mapList, requirementDefinitionMap); + + assertEquals(1, mapList.size()); + } + + @Test + public void testAddGroupMemberGroupNotExist() { + ServiceTemplate serviceTemplate = new ServiceTemplate(); + serviceTemplate.setTopology_template(new TopologyTemplate()); + DataModelUtil.addGroupMember(serviceTemplate, "groupName", "memberId"); + + assertNull(serviceTemplate.getTopology_template().getGroups()); + } + + @Test + public void testAddGroupMemberGroup() { + ServiceTemplate serviceTemplate = new ServiceTemplate(); + serviceTemplate.setTopology_template(new TopologyTemplate()); + Map<String, GroupDefinition> groupDefinitionMap = + Stream.of(new AbstractMap.SimpleEntry<>("groupName", new GroupDefinition())) + .collect(Collectors.toMap(AbstractMap.SimpleEntry::getKey, AbstractMap.SimpleEntry::getValue)); + + serviceTemplate.getTopology_template().setGroups(groupDefinitionMap); + + DataModelUtil.addGroupMember(serviceTemplate, "groupName", "memberId"); + + assertEquals(1, serviceTemplate.getTopology_template().getGroups().size()); + } + + @Test + public void testCreateParameterDefinition() { + assertNotNull(DataModelUtil + .createParameterDefinition("parameterType", "description", true, Collections.emptyList(), new + EntrySchema(), null)); + } + + @Test + public void testCreateRequirement() { + assertNotNull(DataModelUtil.createRequirement("capability", "node", "relationShip", new Object[1])); + } + + @Test + public void testCreateEntrySchema() { + assertNull(DataModelUtil.createEntrySchema(null, null, null)); + } + + @Test + public void testCreateGetInputPropertyValueFromListParameter() { + Map inputPropertyMap = DataModelUtil.createGetInputPropertyValueFromListParameter("inputPropertyList", 1, + "nestedPropertyName"); + + assertNotNull(inputPropertyMap.get(ToscaFunctions.GET_INPUT.getDisplayName())); + } + + @Test + public void testConvertPropertyDefToParameterDefNull() { + assertNull(DataModelUtil.convertPropertyDefToParameterDef(null)); + } + + @Test + public void testConvertPropertyDefToParameterDef() { + PropertyDefinition propertyDefinition = new PropertyDefinition(); + propertyDefinition.setType(PROPERTY_DEF_TYPE); + + ParameterDefinitionExt parameterDefinitionExt = + DataModelUtil.convertPropertyDefToParameterDef(propertyDefinition); + assertNotNull(parameterDefinitionExt); + assertEquals(PROPERTY_DEF_TYPE, parameterDefinitionExt.getType()); + } + + @Test + public void testConvertAttributeDefToParameterDefAttDefNull() { + assertNull(DataModelUtil.convertAttributeDefToParameterDef(null, null)); + } + + @Test + public void testConvertAttributeDefToParameterDef() { + ParameterDefinitionExt parameterDefinitionExt = + DataModelUtil.convertAttributeDefToParameterDef(new AttributeDefinition(), Collections.emptyMap()); + + assertTrue(MapUtils.isEmpty((Map) parameterDefinitionExt.getValue())); + } + + @Test + public void testIsNodeTemplateTrue() { + ServiceTemplate serviceTemplate = new ServiceTemplate(); + TopologyTemplate topologyTemplate = new TopologyTemplate(); + + serviceTemplate.setTopology_template(topologyTemplate); + + Map<String, NodeTemplate> nodeTemplateMap = + Stream.of(new AbstractMap.SimpleEntry<>(ENTRY_ID, new NodeTemplate())) + .collect(Collectors.toMap(AbstractMap.SimpleEntry::getKey, AbstractMap.SimpleEntry::getValue)); + topologyTemplate.setNode_templates(nodeTemplateMap); + + assertTrue(DataModelUtil.isNodeTemplate(ENTRY_ID, serviceTemplate)); + } + + @Test + public void testIsNodeTemplateEntryMissing() { + ServiceTemplate serviceTemplate = new ServiceTemplate(); + TopologyTemplate topologyTemplate = new TopologyTemplate(); + + serviceTemplate.setTopology_template(topologyTemplate); + topologyTemplate.setNode_templates(Collections.emptyMap()); + + assertFalse(DataModelUtil.isNodeTemplate(ENTRY_ID, serviceTemplate)); + } + + @Test(expected = CoreException.class) + public void testAddInputParameterToTopologyTemplateServiceTemplateNull() { + DataModelUtil.addInputParameterToTopologyTemplate(null, null, null); + } + + @Test + public void testAddInputParameterToTopologyTemplate() { + ServiceTemplate serviceTemplate = new ServiceTemplate(); + DataModelUtil.addInputParameterToTopologyTemplate(serviceTemplate, PARAMETER_ID, new ParameterDefinition()); + + assertEquals(1, serviceTemplate.getTopology_template().getInputs().size()); + } + + @Test(expected = CoreException.class) + public void testAddOutputParameterToTopologyTemplateServiceTemplateNull() { + DataModelUtil.addOutputParameterToTopologyTemplate(null, null, null); + } + + @Test + public void testAddOutputParameterToTopologyTemplate() { + ServiceTemplate serviceTemplate = new ServiceTemplate(); + DataModelUtil.addOutputParameterToTopologyTemplate(serviceTemplate, PARAMETER_ID, new ParameterDefinition()); + + assertEquals(1, serviceTemplate.getTopology_template().getOutputs().size()); + } + + @Test + public void testGetNodeTemplateRequirementsNodeTemplateNull() { + assertNull(DataModelUtil.getNodeTemplateRequirements(null)); + } + + @Test + public void testGetNodeTemplateRequirementsNodeTemplateRequirementsNull() { + assertTrue(MapUtils.isEmpty(DataModelUtil.getNodeTemplateRequirements(new NodeTemplate()))); + } + + @Test + public void testGetNodeTemplateRequirementListNodeTemplateRequirementListNull() { + assertNull(DataModelUtil.getNodeTemplateRequirementList(new NodeTemplate())); + } + + @Test + public void testGetNodeTemplateRequirementList() { + Map<String, RequirementAssignment> requirementAssignmentMap = + Stream.of(new AbstractMap.SimpleEntry<>(REQUIREMENT_ID, new RequirementAssignment())) + .collect(Collectors.toMap(AbstractMap.SimpleEntry::getKey, AbstractMap.SimpleEntry::getValue)); + + List<Map<String, RequirementAssignment>> requirementList = new ArrayList<>(); + requirementList.add(requirementAssignmentMap); + + NodeTemplate nodeTemplate = new NodeTemplate(); + nodeTemplate.setRequirements(requirementList); + + assertEquals(requirementList.size(), DataModelUtil.getNodeTemplateRequirementList(nodeTemplate).size()); + } + + @Test + public void testGetRequirementAssignmentRequirementAssignmentListEmpty() { + assertFalse(DataModelUtil.getRequirementAssignment(null, null).isPresent()); + } + + @Test + public void testGetRequirementAssignmentRequirementAssignmentListDoseNotContainsKeyPassed() { + assertFalse(DataModelUtil.getRequirementAssignment( + Collections.singletonList(new HashMap<>()), REQUIREMENT_KEY).isPresent()); + } + + @Test + public void testGetRequirementAssignmentRequirementAssignmentListContainsKeyPassed() { + Map<String, RequirementAssignment> requirementAssignmentMap = + Stream.of(new AbstractMap.SimpleEntry<>(REQUIREMENT_KEY, new RequirementAssignment())) + .collect(Collectors.toMap(AbstractMap.SimpleEntry::getKey, AbstractMap.SimpleEntry::getValue)); + + assertTrue(DataModelUtil.getRequirementAssignment( + Collections.singletonList(requirementAssignmentMap), REQUIREMENT_KEY).isPresent()); + } + + @Test + public void testRemoveRequirementsDefinition() { + Map<String, RequirementDefinition> requirementDefinitionMap = + Stream.of(new AbstractMap.SimpleEntry<>(REQUIREMENT_KEY, new RequirementDefinition())) + .collect(Collectors.toMap(AbstractMap.SimpleEntry::getKey, AbstractMap.SimpleEntry::getValue)); + + + List<Map<String, RequirementDefinition>> requirementList = + Stream.of(requirementDefinitionMap).collect(Collectors.toList()); + DataModelUtil.removeRequirementsDefinition(requirementList, REQUIREMENT_KEY); + + assertTrue(requirementList.isEmpty()); + } + + @Test + public void testRemoveRequirementsAssignment() { + Map<String, RequirementAssignment> requirementDefinitionMap = + Stream.of(new AbstractMap.SimpleEntry<>(REQUIREMENT_KEY, new RequirementAssignment())) + .collect(Collectors.toMap(AbstractMap.SimpleEntry::getKey, AbstractMap.SimpleEntry::getValue)); + + + List<Map<String, RequirementAssignment>> requirementList = + Stream.of(requirementDefinitionMap).collect(Collectors.toList()); + DataModelUtil.removeRequirementsAssignment(requirementList, REQUIREMENT_KEY); + + assertTrue(requirementList.isEmpty()); + } + + @Test + public void testRemoveRequirementAssignmentNodeTemplate() { + + RequirementAssignment requirementAssignment = new RequirementAssignment(); + requirementAssignment.setNode(NODE_ID); + requirementAssignment.setCapability(CAPABILITY_ID); + requirementAssignment.setRelationship("relationshipId"); + Map<String, RequirementAssignment> requirementDefinitionMap = + Stream.of(new AbstractMap.SimpleEntry<>(REQUIREMENT_KEY, requirementAssignment)) + .collect(Collectors.toMap(AbstractMap.SimpleEntry::getKey, AbstractMap.SimpleEntry::getValue)); + + List<Map<String, RequirementAssignment>> requirementList = + Stream.of(requirementDefinitionMap).collect(Collectors.toList()); + NodeTemplate nodeTemplate = new NodeTemplate(); + nodeTemplate.setRequirements(requirementList); + + RequirementAssignment requirementAssignment1 = new RequirementAssignment(); + requirementAssignment1.setNode(NODE_ID); + requirementAssignment1.setCapability(CAPABILITY_ID); + requirementAssignment1.setRelationship("relationshipId"); + DataModelUtil.removeRequirementAssignment(nodeTemplate, REQUIREMENT_KEY, requirementAssignment); + + assertTrue(requirementList.isEmpty()); + } + + @Test + public void testGetNamespaceSuffixNull() { + assertNull(DataModelUtil.getNamespaceSuffix(null)); + } + + @Test + public void testGetNamespaceSuffix() { + assertEquals("suffix", DataModelUtil.getNamespaceSuffix("name.suffix")); + } + + @Test + public void testIsImportAddedToServiceTemplateImportIdExists() { + Map<String, Import> importMap = Stream.of(new AbstractMap.SimpleEntry<>("imp1", new Import())).collect + (Collectors.toMap(AbstractMap.SimpleEntry::getKey, AbstractMap.SimpleEntry::getValue)); + + assertTrue(DataModelUtil.isImportAddedToServiceTemplate(Collections.singletonList(importMap), "imp1")); + } + + @Test + public void testIsImportAddedToServiceTemplateImportIdNotExists() { + Map<String, Import> importMap = Stream.of(new AbstractMap.SimpleEntry<>("imp1", new Import())).collect + (Collectors.toMap(AbstractMap.SimpleEntry::getKey, AbstractMap.SimpleEntry::getValue)); + + assertFalse(DataModelUtil.isImportAddedToServiceTemplate(Collections.singletonList(importMap), "imp2")); + } + + @Test + public void testGetOutputParameterOutputIsNull() { + ServiceTemplate serviceTemplate = new ServiceTemplate(); + serviceTemplate.setTopology_template(new TopologyTemplate()); + + assertNull(DataModelUtil.getOuputParameter(serviceTemplate, OUTPUT_ID)); + } + + @Test + public void testGetOutputParameter() { + ServiceTemplate serviceTemplate = new ServiceTemplate(); + serviceTemplate.setTopology_template(new TopologyTemplate()); + + Map<String, ParameterDefinition> outputMap = Stream.of(new AbstractMap.SimpleEntry<>(OUTPUT_ID, new + ParameterDefinition())).collect + (Collectors.toMap(AbstractMap.SimpleEntry::getKey, AbstractMap.SimpleEntry::getValue)); + serviceTemplate.getTopology_template().setOutputs(outputMap); + + assertNotNull(DataModelUtil.getOuputParameter(serviceTemplate, OUTPUT_ID)); + } + + @Test + public void testGetInputParametersNull() { + ServiceTemplate serviceTemplate = new ServiceTemplate(); + serviceTemplate.setTopology_template(new TopologyTemplate()); + + assertNull(DataModelUtil.getInputParameters(serviceTemplate)); + } + + @Test + public void testGetInputParameters() { + ServiceTemplate serviceTemplate = new ServiceTemplate(); + serviceTemplate.setTopology_template(new TopologyTemplate()); + + serviceTemplate.getTopology_template().setInputs(new HashMap<>()); + + assertNotNull(DataModelUtil.getInputParameters(serviceTemplate)); + } + + @Test + public void testGetRelationshipTemplatesNull() { + ServiceTemplate serviceTemplate = new ServiceTemplate(); + serviceTemplate.setTopology_template(new TopologyTemplate()); + + assertNull(DataModelUtil.getInputParameters(serviceTemplate)); + } + + @Test + public void testGetRelationshipTemplates() { + ServiceTemplate serviceTemplate = new ServiceTemplate(); + serviceTemplate.setTopology_template(new TopologyTemplate()); + + serviceTemplate.getTopology_template().setRelationship_templates(new HashMap<>()); + + assertNotNull(DataModelUtil.getRelationshipTemplates(serviceTemplate)); + } + + @Test + public void testGetPropertyValuePropertyNotPresent() { + NodeTemplate nodeTemplate = new NodeTemplate(); + Map<String, Object> objectMap = Stream.of(new AbstractMap.SimpleEntry<>(PROPERTY_ID, new PropertyDefinition())) + .collect(Collectors.toMap(AbstractMap.SimpleEntry::getKey, AbstractMap.SimpleEntry::getValue)); + + nodeTemplate.setProperties(objectMap); + assertNull(DataModelUtil.getPropertyValue(nodeTemplate, "wrongId")); + } + + @Test + public void testGetPropertyValue() { + NodeTemplate nodeTemplate = new NodeTemplate(); + Map<String, Object> objectMap = Stream.of(new AbstractMap.SimpleEntry<>(PROPERTY_ID, new PropertyDefinition())) + .collect(Collectors.toMap(AbstractMap.SimpleEntry::getKey, AbstractMap.SimpleEntry::getValue)); + + nodeTemplate.setProperties(objectMap); + assertNotNull(DataModelUtil.getPropertyValue(nodeTemplate, PROPERTY_ID)); + } + + @Test + public void testGetNodeTemplatePropertiesPresent() { + ServiceTemplate serviceTemplate = new ServiceTemplate(); + serviceTemplate.setTopology_template(new TopologyTemplate()); + NodeTemplate nodeTemplate = new NodeTemplate(); + nodeTemplate.setProperties(new HashMap<>()); + Map<String, NodeTemplate> nodeTemplateMap = + Stream.of(new AbstractMap.SimpleEntry<>(NODE_TEMPLATE_ID, nodeTemplate)) + .collect(Collectors.toMap(AbstractMap.SimpleEntry::getKey, AbstractMap.SimpleEntry::getValue)); + + serviceTemplate.getTopology_template().setNode_templates(nodeTemplateMap); + + assertNotNull(DataModelUtil.getNodeTemplateProperties(serviceTemplate, NODE_TEMPLATE_ID)); + } + + @Test + public void testGetNodeTemplatePropertiesNotPresent() { + ServiceTemplate serviceTemplate = new ServiceTemplate(); + serviceTemplate.setTopology_template(new TopologyTemplate()); + NodeTemplate nodeTemplate = new NodeTemplate(); + nodeTemplate.setProperties(new HashMap<>()); + + serviceTemplate.getTopology_template().setNode_templates(new HashMap<>()); + + assertNull(DataModelUtil.getNodeTemplateProperties(serviceTemplate, NODE_TEMPLATE_ID)); + } + + @Test + public void testGetSubstitutionMappingsNullCheck() { + ServiceTemplate serviceTemplate = new ServiceTemplate(); + serviceTemplate.setTopology_template(new TopologyTemplate()); + assertNull(DataModelUtil.getSubstitutionMappings(serviceTemplate)); + } + + @Test + public void testGetClonedObject() { + RequirementAssignment requirementAssignment = new RequirementAssignment(); + requirementAssignment.setNode(NODE_ID); + + Object obj = DataModelUtil.getClonedObject(requirementAssignment, requirementAssignment.getClass()); + + assertTrue(obj instanceof RequirementAssignment); + RequirementAssignment assignment = (RequirementAssignment) obj; + assertNotSame(assignment, requirementAssignment); + } + + @Test + public void testGetCloneObject() { + RequirementAssignment requirementAssignment = new RequirementAssignment(); + requirementAssignment.setNode(NODE_ID); + + assertNotSame(DataModelUtil.getClonedObject(requirementAssignment), requirementAssignment); + } + + @Test + public void testGetCloneObjectSerializableObject() { + List<String> stringList = new ArrayList<>(); + + assertNotSame(DataModelUtil.getClonedObject(stringList), stringList); + } + + @Test + public void testAddSubstitutionFilteringProperty() { + NodeTemplate nodeTemplate = new NodeTemplate(); + nodeTemplate.setProperties(new HashMap<>()); + + DataModelUtil.addSubstitutionFilteringProperty(TEMPLATE_NAME, nodeTemplate, 5); + + assertNotNull(nodeTemplate.getProperties().get(ToscaConstants.SERVICE_TEMPLATE_FILTER_PROPERTY_NAME)); + } + + @Test + public void testAddBindingReqFromPortToCompute() { + NodeTemplate nodeTemplate = new NodeTemplate(); + + DataModelUtil.addBindingReqFromPortToCompute("computeNodeId", nodeTemplate); + + assertNotNull(nodeTemplate.getRequirements()); + assertEquals(1, nodeTemplate.getRequirements().size()); + } + + @Test + public void testCreateSubstitutionTemplateSubMappingCapabilityAndRequirementNull() { + Map<String, Map<String, List<String>>> mapping = new HashMap<>(); + + Map<String, List<String>> capabilityStringList = new HashMap<>(); + capabilityStringList.put(CAPABILITY_ID, Collections.singletonList("test")); + + Map<String, List<String>> requirementStringList = new HashMap<>(); + capabilityStringList.put(REQUIREMENT_ID, Collections.singletonList("test")); + + mapping.put(ToscaConstants.CAPABILITY, capabilityStringList); + mapping.put(ToscaConstants.REQUIREMENT, requirementStringList); + + NodeType nodeType = new NodeType(); + + Map<String, CapabilityDefinition> capabilityMap = new HashMap<>(); + capabilityMap.put(CAPABILITY_ID, new CapabilityDefinition()); + + Map<String, RequirementDefinition> requirementMap = new HashMap<>(); + requirementMap.put(REQUIREMENT_ID, new RequirementDefinition()); + + nodeType.setRequirements(Collections.singletonList(requirementMap)); + nodeType.setCapabilities(capabilityMap); + + assertNotNull(DataModelUtil.createSubstitutionTemplateSubMapping(NODE_TYPE_KEY, nodeType, mapping)); + } + + @Test + public void testCreateSubstitutionTemplateSubMapping() { + Map<String, Map<String, List<String>>> mapping = new HashMap<>(); + mapping.put(ToscaConstants.CAPABILITY, null); + mapping.put(ToscaConstants.REQUIREMENT, null); + + assertNotNull(DataModelUtil.createSubstitutionTemplateSubMapping(NODE_TYPE_KEY, new NodeType(), mapping)); + } + + @Test + public void testAddNodeTemplateCapability() { + NodeTemplate nodeTemplate = new NodeTemplate(); + + DataModelUtil.addNodeTemplateCapability(nodeTemplate, CAPABILITY_ID, null, null); + assertNotNull(nodeTemplate.getCapabilities()); + } + + @Test + public void testAddSubstitutionNodeTypeRequirements() { + + NodeType nodeType = new NodeType(); + + Map<String, RequirementDefinition> requirementDefinitionMap = new HashMap<>(); + requirementDefinitionMap.put(REQUIREMENT_ID, new RequirementDefinition()); + + DataModelUtil.addSubstitutionNodeTypeRequirements( + nodeType, Collections.singletonList(requirementDefinitionMap), TEMPLATE_NAME); + + assertEquals(1, nodeType.getRequirements().size()); + assertNotNull(nodeType.getRequirements().get(0).get("requirementId_templateName")); + } + + @Test + public void testIsNodeTemplateSectionMissingFromServiceTemplateNodeTemplateMissing() { + ServiceTemplate serviceTemplate = new ServiceTemplate(); + serviceTemplate.setTopology_template(new TopologyTemplate()); + + assertTrue(DataModelUtil.isNodeTemplateSectionMissingFromServiceTemplate(serviceTemplate)); + } + + @Test + public void testIsNodeTemplateSectionMissingFromServiceTemplateTopologyTemplateMissing() { + ServiceTemplate serviceTemplate = new ServiceTemplate(); + + assertTrue(DataModelUtil.isNodeTemplateSectionMissingFromServiceTemplate(serviceTemplate)); + } + + @Test + public void testIsNodeTemplateSectionMissingFromServiceTemplate() { + ServiceTemplate serviceTemplate = new ServiceTemplate(); + serviceTemplate.setTopology_template(new TopologyTemplate()); + + Map<String, NodeTemplate> nodeTemplateMap = new HashMap<>(); + nodeTemplateMap.put(NODE_TEMPLATE_ID, new NodeTemplate()); + serviceTemplate.getTopology_template().setNode_templates(nodeTemplateMap); + + assertFalse(DataModelUtil.isNodeTemplateSectionMissingFromServiceTemplate(serviceTemplate)); + } + + @Test + public void testGetRelationshipTemplate() { + RelationshipTemplate relationshipTemplate = new RelationshipTemplate(); + String testingRelationshipType = "testingRelationshipType"; + relationshipTemplate.setType(testingRelationshipType); + TopologyTemplate topologyTemplate = new TopologyTemplate(); + topologyTemplate.setRelationship_templates(new HashMap<>()); + String relationId = "rtest"; + topologyTemplate.getRelationship_templates().put(relationId, relationshipTemplate); + ServiceTemplate serviceTemplate = new ServiceTemplate(); + serviceTemplate.setTopology_template(topologyTemplate); + + Optional<RelationshipTemplate> relationshipTemplateOut = + DataModelUtil.getRelationshipTemplate(serviceTemplate, relationId); + Assert.assertNotNull(relationshipTemplateOut); + Assert.assertEquals(true, relationshipTemplateOut.isPresent()); + Assert.assertEquals(testingRelationshipType, relationshipTemplateOut.get().getType()); + } + + @Test + public void testGetEmptyRelationshipTemplate() { + ServiceTemplate serviceTemplate = new ServiceTemplate(); + String relationId = "rtest"; + Optional<RelationshipTemplate> relationshipTemplateOut = + DataModelUtil.getRelationshipTemplate(serviceTemplate, relationId); + Assert.assertNotNull(relationshipTemplateOut); + Assert.assertEquals(false, relationshipTemplateOut.isPresent()); + } + + @Test + public void testAddNodeTemplateProperty() { + NodeTemplate nodeTemplate = new NodeTemplate(); + + DataModelUtil.addNodeTemplateProperty(nodeTemplate, PROPERTY_ID, null); + + assertNotNull(nodeTemplate.getProperties()); + } } diff --git a/openecomp-be/lib/openecomp-tosca-lib/src/test/java/org/openecomp/sdc/tosca/services/ToscaUtilTest.java b/openecomp-be/lib/openecomp-tosca-lib/src/test/java/org/openecomp/sdc/tosca/services/ToscaUtilTest.java new file mode 100644 index 0000000000..ea0726dee6 --- /dev/null +++ b/openecomp-be/lib/openecomp-tosca-lib/src/test/java/org/openecomp/sdc/tosca/services/ToscaUtilTest.java @@ -0,0 +1,116 @@ +/* + * + * Copyright © 2017-2018 European Support Limited + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * / + * + */ + +package org.openecomp.sdc.tosca.services; + +import java.util.Collections; +import java.util.HashMap; +import java.util.Map; + +import org.junit.Assert; +import org.junit.Test; +import org.onap.sdc.tosca.datatypes.model.NodeTemplate; +import org.onap.sdc.tosca.datatypes.model.ServiceTemplate; +import org.onap.sdc.tosca.datatypes.model.TopologyTemplate; + +public class ToscaUtilTest { + + @Test + public void testGetServiceTemplateFileNameServiceTemplateNull() { + ServiceTemplate serviceTemplate = null; + Assert.assertNull(ToscaUtil.getServiceTemplateFileName(serviceTemplate)); + } + + @Test + public void testGetServiceTemplateFileNameMetadataNull() { + ServiceTemplate serviceTemplate = new ServiceTemplate(); + Assert.assertTrue(ToscaUtil.getServiceTemplateFileName(serviceTemplate) + .endsWith(ToscaConstants.SERVICE_TEMPLATE_FILE_POSTFIX)); + } + + @Test + public void testGetServiceTemplateFileName() { + ServiceTemplate serviceTemplate = new ServiceTemplate(); + serviceTemplate.setMetadata( + Collections.singletonMap(ToscaConstants.ST_METADATA_FILE_NAME, ToscaConstants.ST_METADATA_FILE_NAME)); + Assert.assertEquals(ToscaConstants.ST_METADATA_FILE_NAME, + ToscaUtil.getServiceTemplateFileName(serviceTemplate)); + } + + @Test + public void testGetServiceTemplateFileNameMap() { + Assert.assertTrue(ToscaUtil.getServiceTemplateFileName(new HashMap<>()).endsWith("ServiceTemplate.yaml")); + } + + @Test + public void testGetSubstitutableGroupMemberIdNodeTemplateNotPresent() { + ServiceTemplate serviceTemplate = new ServiceTemplate(); + Assert.assertFalse(ToscaUtil.getSubstitutableGroupMemberId("main.yaml", serviceTemplate).isPresent()); + } + + @Test + public void testGetSubstitutableGroupMemberIdSubstitutable() { + ServiceTemplate serviceTemplate = new ServiceTemplate(); + serviceTemplate.setTopology_template(new TopologyTemplate()); + + Map<String, Object> nodeTemplatePropertyMap = Collections.singletonMap(ToscaConstants + .SERVICE_TEMPLATE_FILTER_PROPERTY_NAME, Collections.singletonMap(ToscaConstants + .SUBSTITUTE_SERVICE_TEMPLATE_PROPERTY_NAME, "mainSubServiceTemplateName")); + + NodeTemplate nodeTemplate = new NodeTemplate(); + nodeTemplate.setProperties(nodeTemplatePropertyMap); + Map<String, NodeTemplate> nodeTemplateMap = Collections.singletonMap(ToscaConstants + .SERVICE_TEMPLATE_FILTER_PROPERTY_NAME, nodeTemplate); + serviceTemplate.getTopology_template().setNode_templates(nodeTemplateMap); + + Assert.assertTrue(ToscaUtil.getSubstitutableGroupMemberId("main.yaml", serviceTemplate).isPresent()); + } + + @Test + public void testGetSubstitutableGroupMemberIdNotSubstitutable() { + ServiceTemplate serviceTemplate = new ServiceTemplate(); + serviceTemplate.setTopology_template(new TopologyTemplate()); + + Map<String, Object> nodeTemplatePropertyMap = Collections.singletonMap(ToscaConstants + .SUBSTITUTE_SERVICE_TEMPLATE_PROPERTY_NAME, "subServiceTemplateName"); + + NodeTemplate nodeTemplate = new NodeTemplate(); + nodeTemplate.setProperties(nodeTemplatePropertyMap); + Map<String, NodeTemplate> nodeTemplateMap = Collections.singletonMap(ToscaConstants + .SERVICE_TEMPLATE_FILTER_PROPERTY_NAME, nodeTemplate); + serviceTemplate.getTopology_template().setNode_templates(nodeTemplateMap); + + Assert.assertFalse(ToscaUtil.getSubstitutableGroupMemberId("main.yaml", serviceTemplate).isPresent()); + } + + @Test + public void testAddServiceTemplateToMapWithKeyFileName() { + Map<String, ServiceTemplate> serviceTemplateMap = new HashMap<>(); + ServiceTemplate serviceTemplate = new ServiceTemplate(); + + ToscaUtil.addServiceTemplateToMapWithKeyFileName(serviceTemplateMap, serviceTemplate); + + Assert.assertEquals(1, serviceTemplateMap.size()); + } + + @Test + public void testGetServiceTemplateFileNameWithTemplateName() { + Assert.assertEquals("nestedServiceTemplate.yaml", ToscaUtil.getServiceTemplateFileName("nested")); + } +} |