diff options
author | Michael Lando <ml636r@att.com> | 2017-02-19 12:35:04 +0200 |
---|---|---|
committer | Michael Lando <ml636r@att.com> | 2017-02-19 12:35:04 +0200 |
commit | f5f13c4f6b6fe3b4d98e349dfd7db59339803436 (patch) | |
tree | 72caffc93fab394ffa3b761505775331f1c559b9 /openecomp-be/lib/openecomp-tosca-lib/src/test/java/org | |
parent | 451a3400b76511393c62a444f588a4ed15f4a549 (diff) |
push addional code
Change-Id: Ia427bb3460cda3a896f8faced2de69eaf3807b74
Signed-off-by: Michael Lando <ml636r@att.com>
Diffstat (limited to 'openecomp-be/lib/openecomp-tosca-lib/src/test/java/org')
8 files changed, 1240 insertions, 0 deletions
diff --git a/openecomp-be/lib/openecomp-tosca-lib/src/test/java/org/openecomp/sdc/tosca/TestUtil.java b/openecomp-be/lib/openecomp-tosca-lib/src/test/java/org/openecomp/sdc/tosca/TestUtil.java new file mode 100644 index 0000000000..da3ae76f27 --- /dev/null +++ b/openecomp-be/lib/openecomp-tosca-lib/src/test/java/org/openecomp/sdc/tosca/TestUtil.java @@ -0,0 +1,71 @@ +package org.openecomp.sdc.tosca; + +import org.openecomp.sdc.tosca.datatypes.ToscaServiceModel; +import org.openecomp.sdc.tosca.datatypes.model.ServiceTemplate; +import org.openecomp.sdc.tosca.services.yamlutil.ToscaExtensionYamlUtil; + +import java.io.*; +import java.net.URL; +import java.nio.file.NotDirectoryException; +import java.util.HashMap; +import java.util.Map; + +public class TestUtil { + + public static ToscaServiceModel loadToscaServiceModel(String serviceTemplatesPath, + String globalServiceTemplatesPath, + String entryDefinitionServiceTemplate) + throws IOException { + ToscaExtensionYamlUtil toscaExtensionYamlUtil = new ToscaExtensionYamlUtil(); + Map<String, ServiceTemplate> serviceTemplates = new HashMap<>(); + if (entryDefinitionServiceTemplate == null) { + entryDefinitionServiceTemplate = "MainServiceTemplate.yaml"; + } + + loadServiceTemplates(serviceTemplatesPath, toscaExtensionYamlUtil, serviceTemplates); + if (globalServiceTemplatesPath != null) { + loadServiceTemplates(globalServiceTemplatesPath, toscaExtensionYamlUtil, serviceTemplates); + } + + return new ToscaServiceModel(null, serviceTemplates, entryDefinitionServiceTemplate); + } + + private static void loadServiceTemplates(String serviceTemplatesPath, + ToscaExtensionYamlUtil toscaExtensionYamlUtil, + Map<String, ServiceTemplate> serviceTemplates) + throws IOException { + URL urlFile = TestUtil.class.getResource(serviceTemplatesPath); + if (urlFile != null) { + File pathFile = new File(urlFile.getFile()); + File[] files = pathFile.listFiles(); + if (files != null) { + addServiceTemplateFiles(serviceTemplates, files, toscaExtensionYamlUtil); + } else { + throw new NotDirectoryException(serviceTemplatesPath); + } + } else { + throw new NotDirectoryException(serviceTemplatesPath); + } + } + + private static void addServiceTemplateFiles(Map<String, ServiceTemplate> serviceTemplates, + File[] files, + ToscaExtensionYamlUtil toscaExtensionYamlUtil) + throws IOException { + for (File file : files) { + try (InputStream yamlFile = new FileInputStream(file)) { + ServiceTemplate serviceTemplateFromYaml = + toscaExtensionYamlUtil.yamlToObject(yamlFile, ServiceTemplate.class); + serviceTemplates.put(file.getName(), serviceTemplateFromYaml); + try { + yamlFile.close(); + } catch (IOException ignore) { + } + } catch (FileNotFoundException e) { + throw e; + } catch (IOException e) { + throw e; + } + } + } +} diff --git a/openecomp-be/lib/openecomp-tosca-lib/src/test/java/org/openecomp/sdc/tosca/datatypes/PropertyTypeTest.java b/openecomp-be/lib/openecomp-tosca-lib/src/test/java/org/openecomp/sdc/tosca/datatypes/PropertyTypeTest.java new file mode 100644 index 0000000000..470dd9784a --- /dev/null +++ b/openecomp-be/lib/openecomp-tosca-lib/src/test/java/org/openecomp/sdc/tosca/datatypes/PropertyTypeTest.java @@ -0,0 +1,20 @@ +package org.openecomp.sdc.tosca.datatypes; + +import org.openecomp.sdc.tosca.datatypes.model.PropertyType; +import org.junit.Assert; +import org.junit.Test; + +public class PropertyTypeTest { + @Test + public void shouldReturnNullWhenDisplayNameDoesNotExistForAnyProperty() { + String s = "blabla"; + Assert.assertEquals(PropertyType.getPropertyTypeByDisplayName(s), null); + } + + @Test + public void shouldReturnApproppriatePropertyTypeWhenDisplayNameExist() { + String s = "scalar-unit.size"; + Assert + .assertEquals(PropertyType.getPropertyTypeByDisplayName(s), PropertyType.SCALAR_UNIT_SIZE); + } +}
\ No newline at end of file diff --git a/openecomp-be/lib/openecomp-tosca-lib/src/test/java/org/openecomp/sdc/tosca/datatypes/ToscaModelTest.java b/openecomp-be/lib/openecomp-tosca-lib/src/test/java/org/openecomp/sdc/tosca/datatypes/ToscaModelTest.java new file mode 100644 index 0000000000..13b05543fb --- /dev/null +++ b/openecomp-be/lib/openecomp-tosca-lib/src/test/java/org/openecomp/sdc/tosca/datatypes/ToscaModelTest.java @@ -0,0 +1,282 @@ +package org.openecomp.sdc.tosca.datatypes; + +import org.openecomp.sdc.tosca.datatypes.model.ArtifactType; +import org.openecomp.sdc.tosca.datatypes.model.AttributeDefinition; +import org.openecomp.sdc.tosca.datatypes.model.CapabilityAssignment; +import org.openecomp.sdc.tosca.datatypes.model.CapabilityDefinition; +import org.openecomp.sdc.tosca.datatypes.model.Constraint; +import org.openecomp.sdc.tosca.datatypes.model.Directive; +import org.openecomp.sdc.tosca.datatypes.model.Import; +import org.openecomp.sdc.tosca.datatypes.model.Metadata; +import org.openecomp.sdc.tosca.datatypes.model.NodeFilter; +import org.openecomp.sdc.tosca.datatypes.model.NodeTemplate; +import org.openecomp.sdc.tosca.datatypes.model.NodeType; +import org.openecomp.sdc.tosca.datatypes.model.ParameterDefinition; +import org.openecomp.sdc.tosca.datatypes.model.PropertyDefinition; +import org.openecomp.sdc.tosca.datatypes.model.PropertyType; +import org.openecomp.sdc.tosca.datatypes.model.RequirementAssignment; +import org.openecomp.sdc.tosca.datatypes.model.RequirementDefinition; +import org.openecomp.sdc.tosca.datatypes.model.ServiceTemplate; +import org.openecomp.sdc.tosca.datatypes.model.SubstitutionMapping; +import org.openecomp.sdc.tosca.datatypes.model.TopologyTemplate; +import org.openecomp.sdc.tosca.datatypes.model.heatextend.ParameterDefinitionExt; +import org.openecomp.sdc.tosca.services.DataModelUtil; +import org.openecomp.sdc.tosca.services.ToscaConstants; +import org.openecomp.sdc.tosca.services.yamlutil.ToscaExtensionYamlUtil; +import org.openecomp.core.utilities.yaml.YamlUtil; +import org.junit.Assert; +import org.junit.Test; + + +import java.io.InputStream; +import java.util.ArrayList; +import java.util.HashMap; +import java.util.List; +import java.util.Map; + + +public class ToscaModelTest { + + @Test + public void testServiceTemplateJavaToYaml() { + + ServiceTemplate serviceTemplate = new ServiceTemplate(); + Metadata metadata = new Metadata(); + metadata.setTemplate_author("OPENECOMP"); + metadata.setTemplate_name("Test"); + metadata.setTemplate_version("1.0.0"); + serviceTemplate.setTosca_definitions_version("tosca_simple_yaml_1_0_0"); + serviceTemplate.setDescription("testing desc tosca service template"); + serviceTemplate.setMetadata(metadata); + + Import fileImport1 = new Import(); + fileImport1.setFile("path1/path2/file1.yaml"); + Import fileImport2 = new Import(); + fileImport2.setFile("path1/path2/file2.yaml"); + Map<String, Import> imports = new HashMap<>(); + imports.put("myfile1", fileImport1); + imports.put("myfile2", fileImport2); + serviceTemplate.setImports(imports); + + ArtifactType artifact = new ArtifactType(); + artifact.setMime_type("application/java-archive"); + ArrayList<String> ext = new ArrayList<>(); + ext.add("yaml"); + ext.add("xml"); + artifact.setFile_ext(ext); + Map<String, ArtifactType> artifactTypes = new HashMap<>(); + artifactTypes.put("one_artifact", artifact); + serviceTemplate.setArtifact_types(artifactTypes); + + NodeType nodeType = new NodeType(); + nodeType.setDerived_from("tosca.nodes.Root"); + nodeType.setVersion("1.0.0"); + nodeType.setDescription("tosca compute test"); + + PropertyDefinition propDef1 = new PropertyDefinition(); + propDef1.setType("integer"); + propDef1.setDescription("Number of CPUs requested for a software node instance"); + propDef1.setRequired(true); + propDef1.set_default(1); + + Constraint graterOrEqual = new Constraint(); + graterOrEqual.setGreater_or_equal((float) 5.0); + Constraint constraintEqual = new Constraint(); + constraintEqual.setEqual(5); + Constraint greater_than = new Constraint(); + greater_than.setGreater_than(6.02); + Constraint inRange = new Constraint(); + inRange.setIn_range(new Object[2]); + inRange.getIn_range()[0] = 0; + inRange.getIn_range()[1] = ToscaConstants.UNBOUNDED; + + List<Constraint> constraints = new ArrayList<>(); + constraints.add(graterOrEqual); + constraints.add(constraintEqual); + constraints.add(greater_than); + constraints.add(inRange); + propDef1.setConstraints(constraints); + + Map<String, PropertyDefinition> properties = new HashMap<>(); + properties.put("cpu_num", propDef1); + nodeType.setProperties(properties); + + Map<String, AttributeDefinition> attributesDef = new HashMap<>(); + AttributeDefinition attDef = new AttributeDefinition(); + attDef.setType(PropertyType.STRING.getDisplayName()); + attDef.set_default("hi"); + attributesDef.put("attDef1", attDef); + nodeType.setAttributes(attributesDef); + + Map<String, RequirementDefinition> reqsDef = new HashMap<>(); + RequirementDefinition reqDef = new RequirementDefinition(); + reqDef.setCapability("tosca.cap1"); + reqDef.getOccurrences()[0] = 5; + reqsDef.put("re1", reqDef); + List<Map<String, RequirementDefinition>> reqList = new ArrayList<>(); + reqList.add(reqsDef); + nodeType.setRequirements(reqList); + + + Map<String, CapabilityDefinition> capsDef = new HashMap<>(); + CapabilityDefinition capdef = new CapabilityDefinition(); + capdef.setType("tosca.cap"); + List<String> vvSource = new ArrayList<>(); + vvSource.add("node1"); + vvSource.add("node2"); + capdef.setValid_source_types(vvSource); + capsDef.put("cap1", capdef); + nodeType.setCapabilities(capsDef); + + Map<String, NodeType> nodeTypes = new HashMap<>(); + nodeTypes.put("compute_node_type", nodeType); + serviceTemplate.setNode_types(nodeTypes); + + TopologyTemplate topologyTemplate = new TopologyTemplate(); + topologyTemplate.setDescription("topologi template descroption"); + Map<String, ParameterDefinition> inputs = new HashMap<>(); + ParameterDefinition paramDef = new ParameterDefinition(); + paramDef.setType(PropertyType.STRING.getDisplayName()); + paramDef.setDescription("desc"); + paramDef.set_default("my default val"); + paramDef.setRequired(false); + paramDef.setEntry_schema(DataModelUtil.createEntrySchema("tosca.myType", null, null)); + List<Constraint> paramConstraint = new ArrayList<>(); + Constraint paramConst1 = new Constraint(); + paramConst1.setGreater_than(6); + Constraint paramConst2 = new Constraint(); + paramConst2.setGreater_or_equal(9); + paramConstraint.add(paramConst1); + paramConstraint.add(paramConst2); + paramDef.setConstraints(paramConstraint); + inputs.put("inParam1", paramDef); + topologyTemplate.setInputs(inputs); + + Map<String, NodeTemplate> nodeTemplates = new HashMap<>(); + NodeTemplate nodeTemplate = new NodeTemplate(); + nodeTemplate.setType("nodeTypeRef"); + List<String> directives = new ArrayList<>(); + directives.add(Directive.SELECTABLE.getDisplayName()); + directives.add(Directive.SUBSTITUTABLE.getDisplayName()); + nodeTemplate.setDirectives(directives); + Map<String, Object> nodeTemplateProperties = new HashMap<>(); + nodeTemplateProperties.put("prop1", "abcd"); + nodeTemplateProperties.put("prop2", "{ get_input: my_mysql_rootpw }"); + nodeTemplate.setProperties(nodeTemplateProperties); + Map<String, Object> nodeTemplateAtts = new HashMap<>(); + nodeTemplateAtts.put("att1", "att1Val"); + nodeTemplateAtts.put("att2", "{ get_input: my_mysql_rootpw }"); + nodeTemplate.setAttributes(nodeTemplateAtts); + + + RequirementAssignment reqAssignment1 = new RequirementAssignment(); + reqAssignment1.setNode("nodeA"); + reqAssignment1.setCapability("capA"); + reqAssignment1.setRelationship("relationB"); + Object[] reqAssOccurrences = new Object[2]; + reqAssOccurrences[0] = 1; + reqAssOccurrences[1] = 2; + reqAssignment1.setOccurrences(reqAssOccurrences); + NodeFilter reqNodeFilter = new NodeFilter(); + List<Constraint> propConstrain1 = new ArrayList<>(); + Constraint propConst1 = new Constraint(); + propConst1.setGreater_or_equal(9); + propConstrain1.add(propConst1); + List<Constraint> propConstrain2 = new ArrayList<>(); + Constraint propConst2 = new Constraint(); + propConst2.setMin_length(1); + propConstrain2.add(propConst2); + Constraint propConst3 = new Constraint(); + propConst3.setMax_length(2); + propConstrain2.add(propConst3); + Map<String, List<Constraint>> nodeFilterProp = new HashMap<>(); + nodeFilterProp.put("propName1", propConstrain1); + nodeFilterProp.put("propName2", propConstrain2); + reqNodeFilter.setProperties(nodeFilterProp); + reqAssignment1.setNode_filter(reqNodeFilter); + + RequirementAssignment reqAssignment2 = new RequirementAssignment(); + reqAssignment2.setNode("nodeA"); + reqAssignment2.setCapability("capA"); + reqAssignment2.setRelationship("relationB"); + Map<String, RequirementAssignment> nodeTemplateRequirement1 = new HashMap<>(); + Map<String, RequirementAssignment> nodeTemplateRequirement2 = new HashMap<>(); + nodeTemplateRequirement1.put("req1", reqAssignment1); + nodeTemplateRequirement2.put("req2", reqAssignment2); + nodeTemplate.setRequirements(new ArrayList<>()); + nodeTemplate.getRequirements().add(nodeTemplateRequirement1); + nodeTemplate.getRequirements().add(nodeTemplateRequirement2); + + Map<String, CapabilityAssignment> nodeTemplateCapability = new HashMap<>(); + CapabilityAssignment capAss = new CapabilityAssignment(); + Map<String, Object> capProps = new HashMap<>(); + capProps.put("num_cpus", "{ get_input: cpus }"); + capAss.setProperties(capProps); + Map<String, Object> capAtts = new HashMap<>(); + capAtts.put("num_cpus", "66"); + capAss.setAttributes(capAtts); + nodeTemplateCapability.put("cap1", capAss); + nodeTemplate.setCapabilities(new ArrayList<>()); + nodeTemplate.getCapabilities().add(nodeTemplateCapability); + + NodeFilter nodeTemplateNodeFilter = new NodeFilter(); + Map<String, List<Constraint>> ntProp = new HashMap<>(); + Constraint c1 = new Constraint(); + c1.setEqual("1 MB"); + List<Constraint> consList = new ArrayList<>(); + consList.add(c1); + ntProp.put("test1", consList); + nodeTemplateNodeFilter.setProperties(ntProp); + nodeTemplate.setNode_filter(nodeTemplateNodeFilter); + nodeTemplates.put("firatNodeTemplate", nodeTemplate); + topologyTemplate.setNode_templates(nodeTemplates); + + SubstitutionMapping subMap = new SubstitutionMapping(); + subMap.setNode_type("myNodeType.node"); + Map<String, List<String>> mapCapabilities = new HashMap<>(); + List<String> NodeCap = new ArrayList<>(); + NodeCap.add("database"); + NodeCap.add("database_endpoint"); + mapCapabilities.put("database_endpoint", NodeCap); + subMap.setCapabilities(mapCapabilities); + topologyTemplate.setSubstitution_mappings(subMap); + serviceTemplate.setTopology_template(topologyTemplate); + + String yaml = new YamlUtil().objectToYaml(serviceTemplate); + ServiceTemplate serviceTemplateFromYaml = + new YamlUtil().yamlToObject(yaml, ServiceTemplate.class); + Assert.assertNotNull(serviceTemplateFromYaml); + } + + + @Test + public void testYamlToServiceTemplateObj() { + InputStream yamlFile = new YamlUtil().loadYamlFileIs("/mock/model/serviceTemplate.yaml"); + ServiceTemplate serviceTemplateFromYaml = + new YamlUtil().yamlToObject(yamlFile, ServiceTemplate.class); + Assert.assertNotNull(serviceTemplateFromYaml); + } + + + @Test + public void testYamlToServiceTemplateIncludingHeatExtend() { + ToscaExtensionYamlUtil toscaExtensionYamlUtil = new ToscaExtensionYamlUtil(); + InputStream yamlFile = + toscaExtensionYamlUtil.loadYamlFileIs("/mock/model/serviceTemplateHeatExtend.yaml"); + ServiceTemplate serviceTemplateFromYaml = + toscaExtensionYamlUtil.yamlToObject(yamlFile, ServiceTemplate.class); + ParameterDefinitionExt parameterDefinitionExt = + (ParameterDefinitionExt) serviceTemplateFromYaml.getTopology_template().getInputs() + .get("inParam1"); + Assert.assertNotNull(parameterDefinitionExt.getLabel()); + String backToYamlString = toscaExtensionYamlUtil.objectToYaml(serviceTemplateFromYaml); + Assert.assertNotNull(backToYamlString); + } + +} + + + + + + diff --git a/openecomp-be/lib/openecomp-tosca-lib/src/test/java/org/openecomp/sdc/tosca/datatypes/model/CapabilityDefinitionTest.java b/openecomp-be/lib/openecomp-tosca-lib/src/test/java/org/openecomp/sdc/tosca/datatypes/model/CapabilityDefinitionTest.java new file mode 100644 index 0000000000..2b0f88fdea --- /dev/null +++ b/openecomp-be/lib/openecomp-tosca-lib/src/test/java/org/openecomp/sdc/tosca/datatypes/model/CapabilityDefinitionTest.java @@ -0,0 +1,101 @@ +package org.openecomp.sdc.tosca.datatypes.model; + +import org.openecomp.sdc.tosca.services.ToscaConstants; +import org.openecomp.core.utilities.yaml.YamlUtil; +import org.junit.Assert; +import org.junit.Test; + +import java.util.ArrayList; +import java.util.HashMap; +import java.util.List; +import java.util.Map; + +public class CapabilityDefinitionTest { + + @Test + public void cloneTest() { + CapabilityDefinition capDef1 = new CapabilityDefinition(); + Map<String, AttributeDefinition> attributes = new HashMap<>(); + attributes.put("key1", getAttributeDefinition()); + capDef1.setAttributes(attributes); + + capDef1.setDescription("This is my desc"); + capDef1.setOccurrences(getMockOccurrences()); + + Map<String, PropertyDefinition> properties = new HashMap<>(); + PropertyDefinition propertyDefinition = getMockPropertyDefinition(); + properties.put("key1", propertyDefinition); + capDef1.setProperties(properties); + capDef1.setType("My Type"); + List<String> valid_source_types = new ArrayList<>(); + valid_source_types.add("nonono"); + capDef1.setValid_source_types(valid_source_types); + + CapabilityDefinition capDef2 = capDef1.clone(); + NodeType nodeType = new NodeType(); + nodeType.setCapabilities(new HashMap<>()); + nodeType.getCapabilities().put("cap1", capDef1); + nodeType.getCapabilities().put("cap2", capDef2); + + String yamlString = new YamlUtil().objectToYaml(nodeType); + Boolean passResult = !yamlString.contains("&") && !yamlString.contains("*"); + Assert.assertEquals(true, passResult); + } + + private PropertyDefinition getMockPropertyDefinition() { + PropertyDefinition propertyDefinition = new PropertyDefinition(); + propertyDefinition.setConstraints(getMockConstraints()); + propertyDefinition.setDescription("desc"); + propertyDefinition.setType("typeProp"); + propertyDefinition.set_default(5); + propertyDefinition.setEntry_schema(getMockEntrySchema()); + propertyDefinition.setRequired(false); + propertyDefinition.setStatus(Status.UNSUPPORTED); + return propertyDefinition; + } + + private Object[] getMockOccurrences() { + Object[] occurrences = new Object[2]; + occurrences[0] = 2; + occurrences[1] = ToscaConstants.UNBOUNDED; + return occurrences; + } + + private ArtifactDefinition getMockArtifactDefinition() { + ArtifactDefinition artifactDefinition = new ArtifactDefinition(); + artifactDefinition.setType("type1"); + artifactDefinition.setDescription("description of OPENECOMP def"); + artifactDefinition.setDeploy_path("my deployment path"); + artifactDefinition.setFile("my file"); + artifactDefinition.setRepository("my repository"); + return artifactDefinition; + } + + private AttributeDefinition getAttributeDefinition() { + AttributeDefinition attributeDefinition = new AttributeDefinition(); + attributeDefinition.setDescription("desc1"); + attributeDefinition.setType("type1"); + attributeDefinition.set_default("none"); + attributeDefinition.setEntry_schema(getMockEntrySchema()); + attributeDefinition.setStatus(Status.UNSUPPORTED); + return attributeDefinition; + } + + private EntrySchema getMockEntrySchema() { + EntrySchema entrySchema = new EntrySchema(); + entrySchema.setType("string"); + entrySchema.setDescription("string for string"); + List<Constraint> constraints = getMockConstraints(); + entrySchema.setConstraints(constraints); + return entrySchema; + } + + private List<Constraint> getMockConstraints() { + List<Constraint> constraints = new ArrayList<>(); + Constraint constraint = new Constraint(); + constraint.setEqual("5"); + constraints.add(constraint); + return constraints; + } + +}
\ No newline at end of file diff --git a/openecomp-be/lib/openecomp-tosca-lib/src/test/java/org/openecomp/sdc/tosca/datatypes/model/RequirementDefinitionTest.java b/openecomp-be/lib/openecomp-tosca-lib/src/test/java/org/openecomp/sdc/tosca/datatypes/model/RequirementDefinitionTest.java new file mode 100644 index 0000000000..3109a8abff --- /dev/null +++ b/openecomp-be/lib/openecomp-tosca-lib/src/test/java/org/openecomp/sdc/tosca/datatypes/model/RequirementDefinitionTest.java @@ -0,0 +1,40 @@ +package org.openecomp.sdc.tosca.datatypes.model; + +import org.openecomp.core.utilities.yaml.YamlUtil; +import org.junit.Assert; +import org.junit.Test; + +import java.util.ArrayList; +import java.util.HashMap; +import java.util.List; +import java.util.Map; + +public class RequirementDefinitionTest { + + @Test + public void cloneTest() { + RequirementDefinition reqDef1 = new RequirementDefinition(); + reqDef1.setNode("node1"); + reqDef1.setRelationship("my Relationship"); + reqDef1.setCapability("capabilities"); + reqDef1.setOccurrences(new Object[]{1, 1}); + + RequirementDefinition reqDef2 = reqDef1.clone(); + NodeType nodeType = new NodeType(); + + List<Map<String, RequirementDefinition>> requirements = new ArrayList<>(); + Map<String, RequirementDefinition> reqMap1 = new HashMap<>(); + reqMap1.put("req1", reqDef1); + requirements.add(reqMap1); + Map<String, RequirementDefinition> reqMap2 = new HashMap<>(); + reqMap2.put("req2", reqDef2); + requirements.add(reqMap2); + nodeType.setRequirements(requirements); + + String yamlString = new YamlUtil().objectToYaml(nodeType); + Boolean passResult = !yamlString.contains("&") && !yamlString.contains("*"); + Assert.assertEquals(true, passResult); + } + + +}
\ No newline at end of file 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 new file mode 100644 index 0000000000..893f7a1a5e --- /dev/null +++ b/openecomp-be/lib/openecomp-tosca-lib/src/test/java/org/openecomp/sdc/tosca/services/DataModelUtilTest.java @@ -0,0 +1,104 @@ +package org.openecomp.sdc.tosca.services; + +import org.openecomp.sdc.common.errors.CoreException; +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.openecomp.sdc.tosca.datatypes.model.GroupDefinition; +import org.openecomp.sdc.tosca.datatypes.model.NodeTemplate; +import org.openecomp.sdc.tosca.datatypes.model.NodeType; +import org.openecomp.sdc.tosca.datatypes.model.PolicyDefinition; +import org.openecomp.sdc.tosca.datatypes.model.RelationshipTemplate; +import org.openecomp.sdc.tosca.datatypes.model.RequirementAssignment; +import org.openecomp.sdc.tosca.datatypes.model.SubstitutionMapping; + +import java.util.ArrayList; + +@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()); + } +}
\ No newline at end of file diff --git a/openecomp-be/lib/openecomp-tosca-lib/src/test/java/org/openecomp/sdc/tosca/services/impl/ToscaAnalyzerServiceImplTest.java b/openecomp-be/lib/openecomp-tosca-lib/src/test/java/org/openecomp/sdc/tosca/services/impl/ToscaAnalyzerServiceImplTest.java new file mode 100644 index 0000000000..47f0bd9aef --- /dev/null +++ b/openecomp-be/lib/openecomp-tosca-lib/src/test/java/org/openecomp/sdc/tosca/services/impl/ToscaAnalyzerServiceImplTest.java @@ -0,0 +1,472 @@ +package org.openecomp.sdc.tosca.services.impl; + +import org.openecomp.sdc.common.errors.CoreException; +import org.openecomp.sdc.tosca.TestUtil; +import org.openecomp.sdc.tosca.datatypes.ToscaNodeType; +import org.openecomp.sdc.tosca.datatypes.ToscaServiceModel; +import org.openecomp.sdc.tosca.services.ToscaAnalyzerService; +import org.openecomp.sdc.tosca.services.ToscaConstants; +import org.openecomp.sdc.tosca.services.yamlutil.ToscaExtensionYamlUtil; +import org.junit.Before; +import org.junit.BeforeClass; +import org.junit.Rule; +import org.junit.Test; +import org.junit.rules.ExpectedException; +import org.junit.runner.RunWith; +import org.mockito.Mock; +import org.mockito.MockitoAnnotations; +import org.mockito.runners.MockitoJUnitRunner; +import org.openecomp.sdc.tosca.datatypes.model.Import; +import org.openecomp.sdc.tosca.datatypes.model.NodeTemplate; +import org.openecomp.sdc.tosca.datatypes.model.NodeType; +import org.openecomp.sdc.tosca.datatypes.model.RequirementAssignment; +import org.openecomp.sdc.tosca.datatypes.model.ServiceTemplate; +import org.openecomp.sdc.tosca.datatypes.model.SubstitutionMapping; +import org.openecomp.sdc.tosca.datatypes.model.TopologyTemplate; + +import java.io.IOException; +import java.io.InputStream; +import java.util.HashMap; +import java.util.List; +import java.util.Map; +import java.util.Optional; + +import static org.junit.Assert.*; +import static org.mockito.Mockito.when; + +@RunWith(MockitoJUnitRunner.class) +public class ToscaAnalyzerServiceImplTest { + /* + Dictionary: + SrvTmp: ServiceTemplate + NdTmp: NodeTemplate + NdTy: NodeType + */ + + private static ToscaAnalyzerService toscaAnalyzerService; + private static ToscaServiceModel toscaServiceModel; + @Rule + public ExpectedException thrown = ExpectedException.none(); + + @Mock + NodeTemplate nodeTemplateMock; + @Mock + ToscaServiceModel toscaServiceModelMock; + + @BeforeClass + public static void onlyOnceSetUp() throws IOException { + toscaAnalyzerService = new ToscaAnalyzerServiceImpl(); + toscaServiceModel = TestUtil.loadToscaServiceModel("/mock/analyzerService/toscasubstitution/", + "/mock/globalServiceTemplates/", null); + } + + @Before + public void init() throws IOException { + MockitoAnnotations.initMocks(this); + } + + @Test + public void testGetRequirements() throws Exception { + ToscaExtensionYamlUtil toscaExtensionYamlUtil = new ToscaExtensionYamlUtil(); + InputStream yamlFile = toscaExtensionYamlUtil + .loadYamlFileIs("/mock/analyzerService/NestedServiceTemplateReqTest.yaml"); + ServiceTemplate serviceTemplateFromYaml = + toscaExtensionYamlUtil.yamlToObject(yamlFile, ServiceTemplate.class); + + NodeTemplate port_0 = + serviceTemplateFromYaml.getTopology_template().getNode_templates().get("cmaui_port_0"); + List<RequirementAssignment> reqList = + toscaAnalyzerService.getRequirements(port_0, ToscaConstants.BINDING_REQUIREMENT_ID); + assertEquals(1, reqList.size()); + + reqList.clear(); + NodeTemplate port_1 = + serviceTemplateFromYaml.getTopology_template().getNode_templates().get("cmaui1_port_1"); + reqList = toscaAnalyzerService.getRequirements(port_1, ToscaConstants.LINK_REQUIREMENT_ID); + assertEquals(2, reqList.size()); + + reqList.clear(); + reqList = toscaAnalyzerService.getRequirements(port_0, ToscaConstants.LINK_REQUIREMENT_ID); + assertEquals(0, reqList.size()); + } + + @Test + public void testGetNodeTemplateById() throws Exception { + ServiceTemplate emptyServiceTemplate = new ServiceTemplate(); + Optional<NodeTemplate> nodeTemplate = + toscaAnalyzerService.getNodeTemplateById(emptyServiceTemplate, "test_net222"); + assertEquals(false, nodeTemplate.isPresent()); + + ServiceTemplate mainServiceTemplate = toscaServiceModel.getServiceTemplates() + .get(toscaServiceModel.getEntryDefinitionServiceTemplate()); + nodeTemplate = toscaAnalyzerService.getNodeTemplateById(mainServiceTemplate, "test_net"); + assertEquals(true, nodeTemplate.isPresent()); + + nodeTemplate = toscaAnalyzerService.getNodeTemplateById(mainServiceTemplate, "test_net222"); + assertEquals(false, nodeTemplate.isPresent()); + } + + @Test + public void testGetSubstituteServiceTemplateName() throws Exception { + thrown.expect(CoreException.class); + thrown.expectMessage( + "Invalid Substitute Node Template invalid2, mandatory map property service_template_filter with mandatory key substitute_service_template must be defined."); + + ServiceTemplate mainServiceTemplate = toscaServiceModel.getServiceTemplates() + .get(toscaServiceModel.getEntryDefinitionServiceTemplate()); + Optional<NodeTemplate> notSubstitutableNodeTemplate = + toscaAnalyzerService.getNodeTemplateById(mainServiceTemplate, "test_net"); + Optional<String> substituteServiceTemplateName = toscaAnalyzerService + .getSubstituteServiceTemplateName("test_net", notSubstitutableNodeTemplate.get()); + assertEquals(false, substituteServiceTemplateName.isPresent()); + + Optional<NodeTemplate> substitutableNodeTemplate = + toscaAnalyzerService.getNodeTemplateById(mainServiceTemplate, "test_nested"); + substituteServiceTemplateName = toscaAnalyzerService + .getSubstituteServiceTemplateName("test_nested", substitutableNodeTemplate.get()); + assertEquals(true, substituteServiceTemplateName.isPresent()); + assertEquals("nestedServiceTemplate.yaml", substituteServiceTemplateName.get()); + + NodeTemplate invalidSubstitutableNodeTemplate1 = new NodeTemplate(); + substituteServiceTemplateName = toscaAnalyzerService + .getSubstituteServiceTemplateName("invalid1", invalidSubstitutableNodeTemplate1); + assertEquals(false, substituteServiceTemplateName.isPresent()); + + + NodeTemplate invalidSubstitutableNodeTemplate2 = substitutableNodeTemplate.get(); + Object serviceTemplateFilter = invalidSubstitutableNodeTemplate2.getProperties() + .get(ToscaConstants.SERVICE_TEMPLATE_FILTER_PROPERTY_NAME); + ((Map) serviceTemplateFilter).clear(); + toscaAnalyzerService + .getSubstituteServiceTemplateName("invalid2", invalidSubstitutableNodeTemplate2); + + + } + + + @Test + public void testGetSubstitutableNodeTemplates() throws Exception { + ToscaExtensionYamlUtil toscaExtensionYamlUtil = new ToscaExtensionYamlUtil(); + InputStream yamlFile = toscaExtensionYamlUtil + .loadYamlFileIs("/mock/analyzerService/ServiceTemplateSubstituteTest.yaml"); + ServiceTemplate serviceTemplateFromYaml = + toscaExtensionYamlUtil.yamlToObject(yamlFile, ServiceTemplate.class); + + Map<String, NodeTemplate> substitutableNodeTemplates = + toscaAnalyzerService.getSubstitutableNodeTemplates(serviceTemplateFromYaml); + assertEquals(2, substitutableNodeTemplates.size()); + assertNotNull(substitutableNodeTemplates.get("test_nested1")); + assertNotNull(substitutableNodeTemplates.get("test_nested2")); + + ServiceTemplate emptyServiceTemplate = new ServiceTemplate(); + emptyServiceTemplate.setTopology_template(new TopologyTemplate()); + substitutableNodeTemplates = + toscaAnalyzerService.getSubstitutableNodeTemplates(emptyServiceTemplate); + assertEquals(0, substitutableNodeTemplates.size()); + + yamlFile = toscaExtensionYamlUtil + .loadYamlFileIs("/mock/analyzerService/NestedServiceTemplateReqTest.yaml"); + serviceTemplateFromYaml = toscaExtensionYamlUtil.yamlToObject(yamlFile, ServiceTemplate.class); + substitutableNodeTemplates = + toscaAnalyzerService.getSubstitutableNodeTemplates(serviceTemplateFromYaml); + assertEquals(0, substitutableNodeTemplates.size()); + } + + @Test + public void testGetSubstitutionMappedNodeTemplateByExposedReq() throws Exception { + thrown.expect(CoreException.class); + thrown.expectMessage( + "Invalid Tosca model data, missing 'Node Template' entry for 'Node Template' id cmaui_port_9"); + ToscaExtensionYamlUtil toscaExtensionYamlUtil = new ToscaExtensionYamlUtil(); + InputStream yamlFile = toscaExtensionYamlUtil + .loadYamlFileIs("/mock/analyzerService/NestedServiceTemplateReqTest.yaml"); + ServiceTemplate nestedServiceTemplateFromYaml = + toscaExtensionYamlUtil.yamlToObject(yamlFile, ServiceTemplate.class); + + Optional<Map.Entry<String, NodeTemplate>> mappedNodeTemplate = toscaAnalyzerService + .getSubstitutionMappedNodeTemplateByExposedReq("NestedServiceTemplateSubstituteTest.yaml", + nestedServiceTemplateFromYaml, "local_storage_server_cmaui"); + assertEquals("server_cmaui", mappedNodeTemplate.get().getKey()); + assertNotNull(mappedNodeTemplate.get().getValue()); + + mappedNodeTemplate = toscaAnalyzerService + .getSubstitutionMappedNodeTemplateByExposedReq("NestedServiceTemplateSubstituteTest.yaml", + nestedServiceTemplateFromYaml, "link_cmaui_port_invalid"); + assertEquals("server_cmaui", mappedNodeTemplate.get().getKey()); + assertNotNull(mappedNodeTemplate.get().getValue()); + + ServiceTemplate mainServiceTemplate = toscaServiceModel.getServiceTemplates() + .get(toscaServiceModel.getEntryDefinitionServiceTemplate()); + mappedNodeTemplate = toscaAnalyzerService.getSubstitutionMappedNodeTemplateByExposedReq( + toscaServiceModel.getEntryDefinitionServiceTemplate(), mainServiceTemplate, + "local_storage_server_cmaui"); + assertEquals(false, mappedNodeTemplate.isPresent()); + } + + @Test + public void invalidSubstitutableMapping() throws Exception { + thrown.expect(CoreException.class); + thrown.expectMessage( + "Invalid Substitution Service Template invalidMappingServiceTemplate.yaml, missing mandatory file 'Node type' in substitution mapping."); + ServiceTemplate invalidMappingServiceTemplate = new ServiceTemplate(); + invalidMappingServiceTemplate.setTopology_template(new TopologyTemplate()); + invalidMappingServiceTemplate.getTopology_template() + .setSubstitution_mappings(new SubstitutionMapping()); + toscaAnalyzerService + .getSubstitutionMappedNodeTemplateByExposedReq("invalidMappingServiceTemplate.yaml", + invalidMappingServiceTemplate, "local_storage_server_cmaui"); + } + + @Test + public void substitutableMappingWithNoReqMap() throws Exception { + ServiceTemplate mainServiceTemplate = toscaServiceModel.getServiceTemplates() + .get(toscaServiceModel.getEntryDefinitionServiceTemplate()); + ServiceTemplate emptyReqMapping = new ServiceTemplate(); + emptyReqMapping.setTopology_template(new TopologyTemplate()); + emptyReqMapping.getTopology_template().setSubstitution_mappings(new SubstitutionMapping()); + emptyReqMapping.getTopology_template().getSubstitution_mappings().setNode_type("temp"); + Optional<Map.Entry<String, NodeTemplate>> mappedNodeTemplate = toscaAnalyzerService + .getSubstitutionMappedNodeTemplateByExposedReq( + toscaServiceModel.getEntryDefinitionServiceTemplate(), mainServiceTemplate, + "local_storage_server_cmaui"); + assertEquals(false, mappedNodeTemplate.isPresent()); + } + + @Test + public void testGetSubstitutionMappedNodeTemplateByExposedReqInvalid() throws Exception { + thrown.expect(CoreException.class); + thrown.expectMessage( + "Invalid Tosca model data, missing 'Node Template' entry for 'Node Template' id cmaui_port_9"); + ToscaExtensionYamlUtil toscaExtensionYamlUtil = new ToscaExtensionYamlUtil(); + InputStream yamlFile = toscaExtensionYamlUtil + .loadYamlFileIs("/mock/analyzerService/NestedServiceTemplateReqTest.yaml"); + ServiceTemplate nestedServiceTemplateFromYaml = + toscaExtensionYamlUtil.yamlToObject(yamlFile, ServiceTemplate.class); + + toscaAnalyzerService + .getSubstitutionMappedNodeTemplateByExposedReq("NestedServiceTemplateSubstituteTest.yaml", + nestedServiceTemplateFromYaml, "link_cmaui_port_invalid"); + } + + @Test + public void testIsDesiredRequirementAssignmentMatch() throws Exception { + + RequirementAssignment requirementAssignment = new RequirementAssignment(); + String capability = "Test.Capability"; + String node = "Test.node"; + String relationship = "Test.relationship"; + requirementAssignment.setCapability(capability); + requirementAssignment.setNode(node); + requirementAssignment.setRelationship(relationship); + + assertEquals(true, toscaAnalyzerService + .isDesiredRequirementAssignment(requirementAssignment, capability, node, relationship)); + assertEquals(true, toscaAnalyzerService + .isDesiredRequirementAssignment(requirementAssignment, null, node, relationship)); + assertEquals(true, toscaAnalyzerService + .isDesiredRequirementAssignment(requirementAssignment, capability, null, relationship)); + assertEquals(true, toscaAnalyzerService + .isDesiredRequirementAssignment(requirementAssignment, capability, node, null)); + assertEquals(true, toscaAnalyzerService + .isDesiredRequirementAssignment(requirementAssignment, null, null, relationship)); + assertEquals(true, toscaAnalyzerService + .isDesiredRequirementAssignment(requirementAssignment, capability, null, null)); + assertEquals(true, toscaAnalyzerService + .isDesiredRequirementAssignment(requirementAssignment, null, node, null)); + + } + + @Test + public void testIsDesiredRequirementAssignmentNoMatch() throws Exception { + + RequirementAssignment requirementAssignment = new RequirementAssignment(); + String capability = "Test.Capability"; + String node = "Test.node"; + String relationship = "Test.relationship"; + requirementAssignment.setCapability(capability); + requirementAssignment.setNode(node); + requirementAssignment.setRelationship(relationship); + + assertEquals(false, toscaAnalyzerService + .isDesiredRequirementAssignment(requirementAssignment, "no", node, relationship)); + assertEquals(false, toscaAnalyzerService + .isDesiredRequirementAssignment(requirementAssignment, "no", "no", relationship)); + assertEquals(false, toscaAnalyzerService + .isDesiredRequirementAssignment(requirementAssignment, "no", "no", "no")); + assertEquals(false, toscaAnalyzerService + .isDesiredRequirementAssignment(requirementAssignment, capability, "no", relationship)); + assertEquals(false, toscaAnalyzerService + .isDesiredRequirementAssignment(requirementAssignment, capability, node, "no")); + assertEquals(false, toscaAnalyzerService + .isDesiredRequirementAssignment(requirementAssignment, capability, "no", "no")); + assertEquals(false, toscaAnalyzerService + .isDesiredRequirementAssignment(requirementAssignment, "no", null, null)); + assertEquals(false, toscaAnalyzerService + .isDesiredRequirementAssignment(requirementAssignment, null, null, null)); + + + } + + @Test + public void shouldReturnFalseIfNdTmpIsNull() { + assertFalse(toscaAnalyzerService + .isTypeOf(null, ToscaNodeType.NETWORK.getDisplayName(), new ServiceTemplate(), + toscaServiceModelMock)); + } + + @Test + public void shouldReturnTrueIfNdTmpTypeIsOfRequestedType() { + NodeTemplate nodeTemplate = new NodeTemplate(); + ToscaNodeType nodeTypeToSearch = ToscaNodeType.BLOCK_STORAGE; + nodeTemplate.setType(nodeTypeToSearch.getDisplayName()); + assertTrue(toscaAnalyzerService + .isTypeOf(nodeTemplate, nodeTypeToSearch.getDisplayName(), new ServiceTemplate(), + toscaServiceModelMock)); + } + + @Test + public void shouldReturnTrueIfNdTmpTypeIsFoundInSrvTmpNdTyAndNdTyDerivedFromRequestedType() { + String typeToMatch = ToscaNodeType.CINDER_VOLUME.getDisplayName(); + when(nodeTemplateMock.getType()).thenReturn(typeToMatch); + Map<String, NodeType> stNodeTypes = new HashMap<>(); + addNodeType(stNodeTypes, ToscaNodeType.COMPUTE.getDisplayName(), new NodeType()); + NodeType nodeType = createNodeType(ToscaNodeType.BLOCK_STORAGE.getDisplayName()); + addNodeType(stNodeTypes, typeToMatch, nodeType); + ServiceTemplate serviceTemplate = new ServiceTemplate(); + serviceTemplate.setNode_types(stNodeTypes); + assertTrue(toscaAnalyzerService + .isTypeOf(nodeTemplateMock, ToscaNodeType.BLOCK_STORAGE.getDisplayName(), serviceTemplate, + toscaServiceModelMock)); + + } + + @Test + public void shouldThrowCoreExceptionForInvalidNodeType() { + thrown.expect(CoreException.class); + thrown.expectMessage( + "NodeType 'AAA' or one of its derivedFrom node type hierarchy, is not defined in tosca service model"); + when(nodeTemplateMock.getType()).thenReturn("AAA"); + Map<String, NodeType> stNodeTypes = new HashMap<>(); + addNodeType(stNodeTypes, "notImportant", new NodeType()); + ServiceTemplate serviceTemplate = new ServiceTemplate(); + serviceTemplate.setNode_types(stNodeTypes); + toscaAnalyzerService + .isTypeOf(nodeTemplateMock, ToscaNodeType.COMPUTE.getDisplayName(), serviceTemplate, + toscaServiceModelMock); + } + + @Test + public void shouldThrowCoreExceptionForInvalidNodeType2Level() { + thrown.expect(CoreException.class); + thrown.expectMessage( + "NodeType 'A' or one of its derivedFrom node type hierarchy, is not defined in tosca service model"); + String typeToMatch = "A"; + when(nodeTemplateMock.getType()).thenReturn(typeToMatch); + Map<String, NodeType> stNodeTypes = new HashMap<>(); + addNodeType(stNodeTypes, "notImportant", new NodeType()); + addNodeType(stNodeTypes, "A", createNodeType("ADerivedFromB")); + addNodeType(stNodeTypes, "ADerivedFromB'", createNodeType("BDerivedFromC")); + ServiceTemplate serviceTemplate = new ServiceTemplate(); + serviceTemplate.setNode_types(stNodeTypes); + assertTrue(toscaAnalyzerService + .isTypeOf(nodeTemplateMock, "BDerivedFromC", serviceTemplate, toscaServiceModelMock)); + } + + @Test + public void shouldReturnTrueIfNdTmpTypeIsFoundInSrvTmpNdTyAndNotDerivedFromRequestedTypeBut2ndLevelDerivedFromMatch() { + String typeToMatch = "A"; + when(nodeTemplateMock.getType()).thenReturn(typeToMatch); + Map<String, NodeType> stNodeTypes = new HashMap<>(); + addNodeType(stNodeTypes, "notImportant", new NodeType()); + addNodeType(stNodeTypes, "A", createNodeType("ADerivedFromB")); + addNodeType(stNodeTypes, "ADerivedFromB", createNodeType("BDerivedFromC")); + ServiceTemplate serviceTemplate = new ServiceTemplate(); + serviceTemplate.setNode_types(stNodeTypes); + assertTrue(toscaAnalyzerService + .isTypeOf(nodeTemplateMock, "BDerivedFromC", serviceTemplate, toscaServiceModelMock)); + } + + private NodeType createNodeType(String derivedFrom) { + NodeType nodeType = new NodeType(); + nodeType.setDerived_from(derivedFrom); + return nodeType; + } + + private void addNodeType(Map<String, NodeType> stNodeTypes, String key, NodeType nodeType) { + stNodeTypes.put(key, nodeType); + } + + @Test + public void shouldReturnTrueIfNdTmpTypeIsFoundInSrvTmpNdTyButRequestedTypeNotMatchButFoundIn1stLevelImports() { + String typeToMatch = ToscaNodeType.CINDER_VOLUME.getDisplayName(); + when(nodeTemplateMock.getType()).thenReturn(typeToMatch); + ServiceTemplate mainST = new ServiceTemplate(); + Map<String, Import> imports = new HashMap<>(); + Import anImport = new Import(); + anImport.setFile("mainImport"); + imports.put("bla bla", anImport); + mainST.setImports(imports); + + //create searchable service template + Map<String, NodeType> stNodeTypes = new HashMap<>(); + addNodeType(stNodeTypes, ToscaNodeType.COMPUTE.getDisplayName(), new NodeType()); + NodeType nodeType = createNodeType(ToscaNodeType.BLOCK_STORAGE.getDisplayName()); + addNodeType(stNodeTypes, typeToMatch, nodeType); + ServiceTemplate serviceTemplate = new ServiceTemplate(); + serviceTemplate.setNode_types(stNodeTypes); + + // add service templates to tosca service model + Map<String, ServiceTemplate> serviceTemplates = toscaServiceModelMock.getServiceTemplates(); + serviceTemplates.put("testMainServiceTemplate", mainST); + serviceTemplates.put("mainImport", serviceTemplate); + when(toscaServiceModelMock.getServiceTemplates()).thenReturn(serviceTemplates); + + assertTrue(toscaAnalyzerService + .isTypeOf(nodeTemplateMock, ToscaNodeType.BLOCK_STORAGE.getDisplayName(), mainST, + toscaServiceModelMock)); + } + + @Test + public void shouldReturnTrueIfNdTmpTypeIsFoundInSrvTmpNdTyButRequestedTypeNotMatchButFoundIn2ndLevelImports() { + String typeToMatch = ToscaNodeType.CINDER_VOLUME.getDisplayName(); + when(nodeTemplateMock.getType()).thenReturn(typeToMatch); + ServiceTemplate mainST = new ServiceTemplate(); + Map<String, Import> imports = new HashMap<>(); + Import anImport = new Import(); + anImport.setFile("refToMainImport"); + imports.put("bla bla", anImport); + mainST.setImports(imports); + + //create searchable service template + Map<String, NodeType> stNodeTypes = new HashMap<>(); + addNodeType(stNodeTypes, ToscaNodeType.COMPUTE.getDisplayName(), new NodeType()); + NodeType nodeType = createNodeType(ToscaNodeType.BLOCK_STORAGE.getDisplayName()); + addNodeType(stNodeTypes, typeToMatch, nodeType); + ServiceTemplate serviceTemplate = new ServiceTemplate(); + serviceTemplate.setNode_types(stNodeTypes); + + // create 1st level service template with import only + ServiceTemplate firstLevelST = new ServiceTemplate(); + Map<String, Import> firstLevelImports = new HashMap<>(); + Import firstLevelImport = new Import(); + firstLevelImport.setFile("mainImport"); + firstLevelImports.put("bla bla 2", firstLevelImport); + + firstLevelST.setImports(firstLevelImports); + + // add service templates to tosca service model + Map<String, ServiceTemplate> serviceTemplates = toscaServiceModelMock.getServiceTemplates(); + serviceTemplates.put("testMainServiceTemplate", mainST); + serviceTemplates.put("refToMainImport", firstLevelST); + serviceTemplates.put("mainImport", serviceTemplate); + when(toscaServiceModelMock.getServiceTemplates()).thenReturn(serviceTemplates); + + assertTrue(toscaAnalyzerService + .isTypeOf(nodeTemplateMock, ToscaNodeType.BLOCK_STORAGE.getDisplayName(), mainST, + toscaServiceModelMock)); + } + + // not found at all should throw core exception + + +}
\ No newline at end of file diff --git a/openecomp-be/lib/openecomp-tosca-lib/src/test/java/org/openecomp/sdc/tosca/services/impl/ToscaFileOutputServiceCsarImplTest.java b/openecomp-be/lib/openecomp-tosca-lib/src/test/java/org/openecomp/sdc/tosca/services/impl/ToscaFileOutputServiceCsarImplTest.java new file mode 100644 index 0000000000..963b8a6f57 --- /dev/null +++ b/openecomp-be/lib/openecomp-tosca-lib/src/test/java/org/openecomp/sdc/tosca/services/impl/ToscaFileOutputServiceCsarImplTest.java @@ -0,0 +1,150 @@ +package org.openecomp.sdc.tosca.services.impl; + +import org.openecomp.sdc.tosca.datatypes.ToscaServiceModel; +import org.openecomp.sdc.tosca.datatypes.model.Metadata; +import org.openecomp.sdc.tosca.datatypes.model.ServiceTemplate; +import org.openecomp.sdc.tosca.services.ToscaUtil; +import org.openecomp.core.utilities.file.FileContentHandler; +import org.junit.Assert; +import org.junit.Test; + +import java.io.File; +import java.io.FileOutputStream; +import java.io.IOException; +import java.nio.file.Files; +import java.nio.file.Paths; +import java.util.Enumeration; +import java.util.HashMap; +import java.util.Map; +import java.util.zip.ZipEntry; +import java.util.zip.ZipFile; + +public class ToscaFileOutputServiceCsarImplTest { + + private ToscaFileOutputServiceCsarImpl toscaFileOutputServiceCsarImpl = + new ToscaFileOutputServiceCsarImpl(); + + @Test + public void testCreationMetaFile() { + String createdMeta = toscaFileOutputServiceCsarImpl.createMetaFile("entryFile.yaml"); + String expectedMeta = + "TOSCA-Meta-File-Version: 1.0\n" + + "CSAR-Version: 1.1\n" + + "Created-By: ASDC Onboarding portal\n" + + "Entry-Definitions: Definitions" + File.separator + "entryFile.yaml"; + Assert.assertEquals(createdMeta.replaceAll("\\s+", ""), expectedMeta.replaceAll("\\s+", "")); + } + + @Test + public void testCSARFileCreationWithExternalArtifacts() throws IOException { + ServiceTemplate mainServiceTemplate = new ServiceTemplate(); + Metadata metadata1 = new Metadata(); + metadata1.setTemplate_author("OPENECOMP"); + metadata1.setTemplate_name("ST1"); + metadata1.setTemplate_version("1.0.0"); + mainServiceTemplate.setTosca_definitions_version("tosca_simple_yaml_1_0_0"); + mainServiceTemplate.setDescription("testing desc tosca service template"); + mainServiceTemplate.setMetadata(metadata1); + + ServiceTemplate additionalServiceTemplate = new ServiceTemplate(); + Metadata metadata2 = new Metadata(); + metadata2.setTemplate_author("OPENECOMP"); + metadata2.setTemplate_name("ST2"); + metadata2.setTemplate_version("1.0.0"); + additionalServiceTemplate.setTosca_definitions_version("tosca_simple_yaml_1_0_0"); + additionalServiceTemplate.setDescription("testing desc tosca service template"); + additionalServiceTemplate.setMetadata(metadata2); + + Map<String, ServiceTemplate> definitionsInput = new HashMap<>(); + definitionsInput + .put(ToscaUtil.getServiceTemplateFileName(mainServiceTemplate), mainServiceTemplate); + definitionsInput.put(ToscaUtil.getServiceTemplateFileName(additionalServiceTemplate), + additionalServiceTemplate); + + + Map<String, byte[]> dummyHeatArtifacts = new HashMap<>(); + String file1Content = "this is file number 1"; + String file2Content = "this is file number 2"; + String file1 = "file1.xml"; + dummyHeatArtifacts.put(file1, file1Content.getBytes()); + String file2 = "file2.yml"; + dummyHeatArtifacts.put(file2, file2Content.getBytes()); + + + FileContentHandler heatFiles = new FileContentHandler(); + heatFiles.putAll(dummyHeatArtifacts); + Map<String, byte[]> licenseArtifacts = new HashMap<>(); + + FileContentHandler licenseArtifactsFiles = new FileContentHandler(); + + licenseArtifacts.put( + ToscaFileOutputServiceCsarImpl.EXTERNAL_ARTIFACTS_FOLDER_NAME + File.separator + + "license-file-1.xml", file1Content.getBytes()); + licenseArtifacts.put( + ToscaFileOutputServiceCsarImpl.EXTERNAL_ARTIFACTS_FOLDER_NAME + File.separator + + "license-file-2.xml", file1Content.getBytes()); + + licenseArtifactsFiles.putAll(licenseArtifacts); + + byte[] csarFile = toscaFileOutputServiceCsarImpl.createOutputFile( + new ToscaServiceModel(heatFiles, definitionsInput, + ToscaUtil.getServiceTemplateFileName(mainServiceTemplate)), licenseArtifactsFiles); + + String resultFileName = "resultFile.zip"; + File file = new File(resultFileName); + FileOutputStream fos = new FileOutputStream(file); + fos.write(csarFile); + fos.close(); + + ZipFile zipFile = new ZipFile(resultFileName); + + Enumeration<? extends ZipEntry> entries = zipFile.entries(); + + int count = 0; + while (entries.hasMoreElements()) { + count++; + entries.nextElement(); + } + Assert.assertEquals(7, count); + zipFile.close(); + Files.delete(Paths.get(file.getPath())); + } + + @Test + public void testCSARFileCreation_noArtifacts() throws IOException { + ServiceTemplate serviceTemplate = new ServiceTemplate(); + Metadata metadata = new Metadata(); + metadata.setTemplate_author("OPENECOMP"); + metadata.setTemplate_name("Test"); + metadata.setTemplate_version("1.0.0"); + serviceTemplate.setTosca_definitions_version("tosca_simple_yaml_1_0_0"); + serviceTemplate.setDescription("testing desc tosca service template"); + serviceTemplate.setMetadata(metadata); + Map<String, ServiceTemplate> definitionsInput = new HashMap<>(); + String serviceTemplateFileName = ToscaUtil.getServiceTemplateFileName(serviceTemplate); + definitionsInput.put(serviceTemplateFileName, serviceTemplate); + byte[] csarFile = toscaFileOutputServiceCsarImpl + .createOutputFile(new ToscaServiceModel(null, definitionsInput, serviceTemplateFileName), + null); + + + String resultFileName = "resultFile.zip"; + File file = new File(resultFileName); + FileOutputStream fos = new FileOutputStream(file); + fos.write(csarFile); + fos.close(); + + ZipFile zipFile = new ZipFile(resultFileName); + + Enumeration<? extends ZipEntry> entries = zipFile.entries(); + + int count = 0; + while (entries.hasMoreElements()) { + count++; + entries.nextElement(); + } + Assert.assertEquals(2, count); + zipFile.close(); + Files.delete(Paths.get(file.getPath())); + } +}
\ No newline at end of file |