Add support of interface in TOSCA datatypes
[sdc.git] / openecomp-be / lib / openecomp-tosca-lib / src / test / java / org / openecomp / sdc / tosca / datatypes / ToscaModelTest.java
1 /*
2  * Copyright © 2016-2018 European Support Limited
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16
17 package org.openecomp.sdc.tosca.datatypes;
18
19 import java.io.IOException;
20 import java.io.InputStream;
21 import java.util.ArrayList;
22 import java.util.HashMap;
23 import java.util.List;
24 import java.util.Map;
25 import java.util.Optional;
26 import org.junit.Assert;
27 import org.junit.Test;
28 import org.onap.sdc.tosca.datatypes.model.ArtifactType;
29 import org.onap.sdc.tosca.datatypes.model.AttributeDefinition;
30 import org.onap.sdc.tosca.datatypes.model.CapabilityAssignment;
31 import org.onap.sdc.tosca.datatypes.model.CapabilityDefinition;
32 import org.onap.sdc.tosca.datatypes.model.CapabilityFilter;
33 import org.onap.sdc.tosca.datatypes.model.Constraint;
34 import org.onap.sdc.tosca.datatypes.model.Directive;
35 import org.onap.sdc.tosca.datatypes.model.Implementation;
36 import org.onap.sdc.tosca.datatypes.model.Import;
37 import org.onap.sdc.tosca.datatypes.model.InterfaceDefinitionTemplate;
38 import org.onap.sdc.tosca.datatypes.model.InterfaceDefinitionType;
39 import org.onap.sdc.tosca.datatypes.model.InterfaceType;
40 import org.onap.sdc.tosca.datatypes.model.NodeFilter;
41 import org.onap.sdc.tosca.datatypes.model.NodeTemplate;
42 import org.onap.sdc.tosca.datatypes.model.NodeType;
43 import org.onap.sdc.tosca.datatypes.model.OperationDefinition;
44 import org.onap.sdc.tosca.datatypes.model.OperationDefinitionTemplate;
45 import org.onap.sdc.tosca.datatypes.model.OperationDefinitionType;
46 import org.onap.sdc.tosca.datatypes.model.ParameterDefinition;
47 import org.onap.sdc.tosca.datatypes.model.PropertyDefinition;
48 import org.onap.sdc.tosca.datatypes.model.PropertyType;
49 import org.onap.sdc.tosca.datatypes.model.RequirementAssignment;
50 import org.onap.sdc.tosca.datatypes.model.RequirementDefinition;
51 import org.onap.sdc.tosca.datatypes.model.ServiceTemplate;
52 import org.onap.sdc.tosca.datatypes.model.SubstitutionMapping;
53 import org.onap.sdc.tosca.datatypes.model.TopologyTemplate;
54 import org.onap.sdc.tosca.datatypes.model.extension.RequirementAssignmentExt;
55 import org.onap.sdc.tosca.datatypes.model.extension.SubstitutionFilter;
56 import org.onap.sdc.tosca.datatypes.model.extension.SubstitutionMappingExt;
57 import org.onap.sdc.tosca.datatypes.model.heatextend.ParameterDefinitionExt;
58 import org.onap.sdc.tosca.services.ToscaExtensionYamlUtil;
59 import org.onap.sdc.tosca.services.YamlUtil;
60 import org.openecomp.sdc.tosca.services.DataModelUtil;
61 import org.openecomp.sdc.tosca.services.ToscaConstants;
62
63
64 public class ToscaModelTest {
65
66     public static final String FIRST_NODE_TEMPLATE = "firstNodeTemplate";
67     public static final String REQ1 = "req1";
68     public static final String REQ2 = "req2";
69     public static final String SERVICE_FILTER_TOSCA_ID = "{get_input=inParam1}";
70     public static final String VMD_NAME = "vmdName";
71     public static final String DIRECTOR = "director";
72     private YamlUtil yamlUtil = new YamlUtil();
73     private ToscaExtensionYamlUtil toscaExtensionYamlUtil = new ToscaExtensionYamlUtil();
74     private static final String INTERFACE_ID = "inter_1";
75     private static final String NODE_TEMPLATE_ID = "firstNodeTemplate";
76     private static final String NODE_TYPE_ID = "compute_node_type";
77     private static final String BASE_DIR = "/mock/model";
78     private static final String ST = "/serviceTemplate.yaml";
79     private static final String ST_WITH_INTERFACE = "/serviceTemplateWithInterface.yaml";
80     private static final String ST_WITH_OPERATIONS = "/serviceTemplateWithInterfaceAndOperation.yaml";
81     private static final String ST_WITH_INTERFACE_DEF = "/serviceTemplateWithNodeTemplateInterface.yaml";
82     private static final String ST_WITH_NODE_INTERFACE_DEF = "/serviceTemplateWithNodeTypeInterface.yaml";
83     private static final String INTERFACE_TYPE_VALUE = "tosca.interfaces.node.lifecycle.Standard";
84     private static final String OPERATION_START = "start";
85     private static final String OPERATION_DESC = "start operation";
86     private static final String IMPLEMENTATION_NAME = "startWorkFlow.json";
87     private static final String PRIMARY_IMPL = "myImpl.yaml";
88     private static final String DEPENDENCY_NAME = "script1.sh";
89     private static final String STRING_TYPE = "string";
90     private static final String ST_WITH_SERVICE_FILTER = "/serviceTemplateWithServiceFilter.yaml";
91     private static final String SUBSTITUTION_MAPPING = "/serviceTemplateWithSubstitutionMapping.yaml";
92     private static final String SUBSTITUTION_MAPPING_EXT = "/substitutionMappingExt.yaml";
93
94     @Test
95     public void testServiceTemplateJavaToYaml() {
96         ServiceTemplate serviceTemplate = new ServiceTemplate();
97         Map<String, String> metadata = new HashMap<>();
98         metadata.put("Template_author", "OPENECOMP");
99         metadata.put(ToscaConstants.ST_METADATA_TEMPLATE_NAME, "Test");
100         metadata.put("Template_version", "1.0.0");
101         serviceTemplate.setMetadata(metadata);
102         serviceTemplate.setTosca_definitions_version("tosca_simple_yaml_1_0_0");
103         serviceTemplate.setDescription("testing desc tosca service template");
104
105         Import fileImport1 = new Import();
106         fileImport1.setFile("path1/path2/file1.yaml");
107         Import fileImport2 = new Import();
108         fileImport2.setFile("path1/path2/file2.yaml");
109         List<Map<String, Import>> imports = new ArrayList<>();
110         Map<String, Import> importsMap = new HashMap<>();
111         importsMap.put("myfile1", fileImport1);
112         imports.add(importsMap);
113         importsMap = new HashMap<>();
114         importsMap.put("myfile2", fileImport2);
115         imports.add(importsMap);
116         serviceTemplate.setImports(imports);
117
118         OperationDefinition operationDefinition = new OperationDefinition();
119         operationDefinition.setDescription("test operation");
120         InterfaceType interfaceType = new InterfaceType();
121         interfaceType.setDerived_from("derived_from");
122         interfaceType.setDescription("desc");
123         interfaceType.addOperation("test", operationDefinition);
124         serviceTemplate.addInterfaceType("test_interface", interfaceType);
125
126         ArtifactType artifact = new ArtifactType();
127         artifact.setMime_type("application/java-archive");
128         ArrayList<String> ext = new ArrayList<>();
129         ext.add("yaml");
130         ext.add("xml");
131         artifact.setFile_ext(ext);
132         Map<String, ArtifactType> artifactTypes = new HashMap<>();
133         artifactTypes.put("one_artifact", artifact);
134         serviceTemplate.setArtifact_types(artifactTypes);
135
136         NodeType nodeType = new NodeType();
137         nodeType.setDerived_from("tosca.nodes.Root");
138         nodeType.setVersion("1.0.0");
139         nodeType.setDescription("tosca compute test");
140
141         PropertyDefinition propDef1 = new PropertyDefinition();
142         propDef1.setType("integer");
143         propDef1.setDescription("Number of CPUs requested for a software node instance");
144         propDef1.setRequired(true);
145         propDef1.set_default(1);
146
147         Constraint graterOrEqual = new Constraint();
148         graterOrEqual.setGreater_or_equal((float) 5.0);
149         Constraint constraintEqual = new Constraint();
150         constraintEqual.setEqual(5);
151         Constraint greater_than = new Constraint();
152         greater_than.setGreater_than(6.02);
153         Constraint inRange = new Constraint();
154         inRange.setIn_range(new Object[2]);
155         inRange.getIn_range()[0] = 0;
156         inRange.getIn_range()[1] = ToscaConstants.UNBOUNDED;
157
158         List<Constraint> constraints = new ArrayList<>();
159         constraints.add(graterOrEqual);
160         constraints.add(constraintEqual);
161         constraints.add(greater_than);
162         constraints.add(inRange);
163         propDef1.setConstraints(constraints);
164
165         Map<String, PropertyDefinition> properties = new HashMap<>();
166         properties.put("cpu_num", propDef1);
167         nodeType.setProperties(properties);
168
169         Map<String, AttributeDefinition> attributesDef = new HashMap<>();
170         AttributeDefinition attDef = new AttributeDefinition();
171         attDef.setType(PropertyType.STRING.getDisplayName());
172         attDef.set_default("hi");
173         attributesDef.put("attDef1", attDef);
174         nodeType.setAttributes(attributesDef);
175
176         Map<String, RequirementDefinition> reqsDef = new HashMap<>();
177         RequirementDefinition reqDef = new RequirementDefinition();
178         reqDef.setCapability("tosca.cap1");
179         reqDef.getOccurrences()[0] = 5;
180         reqsDef.put("re1", reqDef);
181         List<Map<String, RequirementDefinition>> reqList = new ArrayList<>();
182         reqList.add(reqsDef);
183         nodeType.setRequirements(reqList);
184
185
186         CapabilityDefinition capdef = new CapabilityDefinition();
187         capdef.setType("tosca.cap");
188         List<String> vvSource = new ArrayList<>();
189         vvSource.add("node1");
190         vvSource.add("node2");
191         capdef.setValid_source_types(vvSource);
192         Map<String, CapabilityDefinition> capsDef = new HashMap<>();
193         capsDef.put("cap1", capdef);
194         nodeType.setCapabilities(capsDef);
195
196         Map<String, NodeType> nodeTypes = new HashMap<>();
197         nodeTypes.put("compute_node_type", nodeType);
198         serviceTemplate.setNode_types(nodeTypes);
199
200         TopologyTemplate topologyTemplate = new TopologyTemplate();
201         topologyTemplate.setDescription("topologi template descroption");
202         ParameterDefinition paramDef = new ParameterDefinition();
203         paramDef.setType(PropertyType.STRING.getDisplayName());
204         paramDef.setDescription("desc");
205         paramDef.set_default("my default val");
206         paramDef.setRequired(false);
207         paramDef.setEntry_schema(DataModelUtil.createEntrySchema("tosca.myType", null, null));
208         List<Constraint> paramConstraint = new ArrayList<>();
209         Constraint paramConst1 = new Constraint();
210         paramConst1.setGreater_than(6);
211         Constraint paramConst2 = new Constraint();
212         paramConst2.setGreater_or_equal(9);
213         paramConstraint.add(paramConst1);
214         paramConstraint.add(paramConst2);
215         paramDef.setConstraints(paramConstraint);
216         Map<String, ParameterDefinition> inputs = new HashMap<>();
217         inputs.put("inParam1", paramDef);
218         topologyTemplate.setInputs(inputs);
219
220         NodeTemplate nodeTemplate = new NodeTemplate();
221         nodeTemplate.setType("nodeTypeRef");
222         List<String> directives = new ArrayList<>();
223         directives.add(Directive.SELECTABLE.getDisplayName());
224         directives.add(Directive.SUBSTITUTABLE.getDisplayName());
225         nodeTemplate.setDirectives(directives);
226         Map<String, Object> nodeTemplateProperties = new HashMap<>();
227         nodeTemplateProperties.put("prop1", "abcd");
228         nodeTemplateProperties.put("prop2", "{ get_input: my_mysql_rootpw }");
229         nodeTemplate.setProperties(nodeTemplateProperties);
230         Map<String, Object> nodeTemplateAtts = new HashMap<>();
231         nodeTemplateAtts.put("att1", "att1Val");
232         nodeTemplateAtts.put("att2", "{ get_input: my_mysql_rootpw }");
233         Map<String, NodeTemplate> nodeTemplates = new HashMap<>();
234         nodeTemplate.setAttributes(nodeTemplateAtts);
235
236
237         RequirementAssignment reqAssignment1 = new RequirementAssignment();
238         reqAssignment1.setNode("nodeA");
239         reqAssignment1.setCapability("capA");
240         reqAssignment1.setRelationship("relationB");
241         Object[] reqAssOccurrences = new Object[2];
242         reqAssOccurrences[0] = 1;
243         reqAssOccurrences[1] = 2;
244         reqAssignment1.setOccurrences(reqAssOccurrences);
245         List<Constraint> propConstraint1 = new ArrayList<>();
246         Constraint propConst1 = new Constraint();
247         propConst1.setGreater_or_equal(9);
248         propConstraint1.add(propConst1);
249         List<Constraint> propConstraint2 = new ArrayList<>();
250         Constraint propConst2 = new Constraint();
251         propConst2.setMin_length(1);
252         propConstraint2.add(propConst2);
253         Constraint propConst3 = new Constraint();
254         propConst3.setMax_length(2);
255         propConstraint2.add(propConst3);
256         List<Map<String, List<Constraint>>> nodeFilterProp = new ArrayList<>();
257         Map<String, List<Constraint>> propsMap = new HashMap<>();
258         propsMap.put("propName1", propConstraint1);
259         propsMap.put("propName2", propConstraint2);
260         nodeFilterProp.add(propsMap);
261         NodeFilter reqNodeFilter = new NodeFilter();
262         reqNodeFilter.setProperties(nodeFilterProp);
263         reqAssignment1.setNode_filter(reqNodeFilter);
264
265         RequirementAssignment reqAssignment2 = new RequirementAssignment();
266         reqAssignment2.setNode("nodeA");
267         reqAssignment2.setCapability("capA");
268         reqAssignment2.setRelationship("relationB");
269         Map<String, RequirementAssignment> nodeTemplateRequirement1 = new HashMap<>();
270         Map<String, RequirementAssignment> nodeTemplateRequirement2 = new HashMap<>();
271         nodeTemplateRequirement1.put(REQ1, reqAssignment1);
272         nodeTemplateRequirement2.put(REQ2, reqAssignment2);
273         nodeTemplate.setRequirements(new ArrayList<>());
274         nodeTemplate.getRequirements().add(nodeTemplateRequirement1);
275         nodeTemplate.getRequirements().add(nodeTemplateRequirement2);
276
277         CapabilityAssignment capAss = new CapabilityAssignment();
278         Map<String, Object> capProps = new HashMap<>();
279         capProps.put("num_cpus", "{ get_input: cpus }");
280         capAss.setProperties(capProps);
281         Map<String, Object> capAtts = new HashMap<>();
282         capAtts.put("num_cpus", "66");
283         capAss.setAttributes(capAtts);
284         Map<String, CapabilityAssignment> nodeTemplateCapability = new HashMap<>();
285         nodeTemplateCapability.put("cap1", capAss);
286         nodeTemplate.setCapabilities(nodeTemplateCapability);
287
288         List<Map<String, List<Constraint>>> nodeFilterProp2 = new ArrayList<>();
289         Map<String, List<Constraint>> propsMap2 = new HashMap<>();
290         Constraint c1 = new Constraint();
291         c1.setEqual("1 MB");
292         List<Constraint> consList = new ArrayList<>();
293         consList.add(c1);
294         propsMap2.put("test1", consList);
295         nodeFilterProp2.add(propsMap2);
296         NodeFilter nodeTemplateNodeFilter = new NodeFilter();
297         nodeTemplateNodeFilter.setProperties(nodeFilterProp2);
298         nodeTemplate.setNode_filter(nodeTemplateNodeFilter);
299         nodeTemplates.put("firatNodeTemplate", nodeTemplate);
300         topologyTemplate.setNode_templates(nodeTemplates);
301
302         SubstitutionMapping subMap = new SubstitutionMapping();
303         subMap.setNode_type("myNodeType.node");
304         Map<String, List<String>> mapCapabilities = new HashMap<>();
305         List<String> nodeCap = new ArrayList<>();
306         nodeCap.add("database");
307         nodeCap.add("database_endpoint");
308         mapCapabilities.put("database_endpoint", nodeCap);
309         subMap.setCapabilities(mapCapabilities);
310         topologyTemplate.setSubstitution_mappings(subMap);
311         serviceTemplate.setTopology_template(topologyTemplate);
312
313         String yaml = new YamlUtil().objectToYaml(serviceTemplate);
314         ServiceTemplate serviceTemplateFromYaml = new YamlUtil().yamlToObject(yaml, ServiceTemplate.class);
315         Assert.assertNotNull(serviceTemplateFromYaml);
316     }
317
318
319   @Test
320   public void testYamlToServiceTemplateObj() throws IOException {
321     ServiceTemplate serviceTemplateFromYaml = getServiceTemplate(BASE_DIR + ST);
322     Assert.assertNotNull(serviceTemplateFromYaml);
323   }
324
325     @Test
326     public void testYamlWithInterfaceToServiceTemplateObj() throws IOException {
327         ServiceTemplate serviceTemplateWithOperation = getServiceTemplate(BASE_DIR + ST_WITH_OPERATIONS);
328         Assert.assertNotNull(serviceTemplateWithOperation);
329
330         InterfaceType expectedInterfaceType = createInterfaceType();
331
332         Map<String, InterfaceType> interfaceTypes = serviceTemplateWithOperation.getNormalizeInterfaceTypes();
333         Assert.assertEquals(1, interfaceTypes.size());
334         InterfaceType actualInterfaceType = interfaceTypes.get(INTERFACE_ID);
335         Assert.assertEquals(expectedInterfaceType, actualInterfaceType);
336     }
337
338     @Test
339     public void testAddOperationToInterface() throws IOException {
340         YamlUtil yamlUtil = new YamlUtil();
341         ServiceTemplate serviceTemplateWithInterface = getServiceTemplate(BASE_DIR + ST_WITH_INTERFACE);
342         ServiceTemplate serviceTemplateWithOperation = getServiceTemplate(BASE_DIR + ST_WITH_OPERATIONS);
343
344         OperationDefinition operationDefinition = createOperationDefinition();
345         InterfaceType normalizeInterfaceType =
346                 serviceTemplateWithInterface.getNormalizeInterfaceTypes().get(INTERFACE_ID);
347         normalizeInterfaceType.addOperation(OPERATION_START, operationDefinition);
348         serviceTemplateWithInterface.addInterfaceType(INTERFACE_ID, normalizeInterfaceType);
349         String expectedServiceTemplate = yamlUtil.objectToYaml(serviceTemplateWithOperation);
350         String actualServiceTemplate = yamlUtil.objectToYaml(serviceTemplateWithInterface);
351         Assert.assertEquals(expectedServiceTemplate, actualServiceTemplate);
352     }
353
354     @Test
355     public void testInterfaceTypeToObjConversion() throws IOException {
356         ServiceTemplate serviceTemplateWithInterface = getServiceTemplate(BASE_DIR + ST_WITH_INTERFACE);
357         ServiceTemplate serviceTemplateWithOperation = getServiceTemplate(BASE_DIR + ST_WITH_OPERATIONS);
358         InterfaceType interfaceType = createInterfaceType();
359
360         Optional<Object> interfaceAsObj = interfaceType.convertInterfaceTypeToToscaObj();
361         Assert.assertTrue(interfaceAsObj.isPresent());
362
363         Map<String, Object> interfaceTypes = new HashMap<>();
364         interfaceTypes.put(INTERFACE_ID, interfaceAsObj.get());
365         serviceTemplateWithInterface.setInterface_types(interfaceTypes);
366
367         String expectedServiceTemplate = yamlUtil.objectToYaml(serviceTemplateWithOperation);
368         String actualServiceTemplate = yamlUtil.objectToYaml(serviceTemplateWithInterface);
369         Assert.assertEquals(expectedServiceTemplate, actualServiceTemplate);
370     }
371
372     @Test
373     public void testObjToInterfaceTypeConversion() throws IOException, ReflectiveOperationException {
374         ServiceTemplate serviceTemplateWithOperation = getServiceTemplate(BASE_DIR + ST_WITH_OPERATIONS);
375         Map<String, InterfaceType> interfaceTypes = serviceTemplateWithOperation.getNormalizeInterfaceTypes();
376         InterfaceType actualInterfaceType = interfaceTypes.get(INTERFACE_ID);
377         InterfaceType expectedInterfaceType = createInterfaceType();
378         Assert.assertEquals(expectedInterfaceType, actualInterfaceType);
379     }
380
381     @Test
382     public void testObjToInterfaceDefinitionTypeConversion() throws IOException, ReflectiveOperationException {
383         ServiceTemplate serviceTemplateWithInterfaceDef = getServiceTemplate(BASE_DIR + ST_WITH_NODE_INTERFACE_DEF);
384         NodeType nodeTypeWithInterface = DataModelUtil.getNodeType(serviceTemplateWithInterfaceDef, NODE_TYPE_ID);
385         Map<String, Object> interfaces = nodeTypeWithInterface.getInterfaces();
386         Object interfaceObj = interfaces.get(INTERFACE_ID);
387
388         InterfaceDefinitionType actualInterfaceDefinition = new InterfaceDefinitionType(interfaceObj);
389         InterfaceDefinitionType expectedInterfaceDefinitionType = createInterfaceDefinitionType();
390         Assert.assertEquals(expectedInterfaceDefinitionType, actualInterfaceDefinition);
391     }
392
393     @Test
394     public void testObjToInterfaceDefinitionTemplateConversion() throws IOException {
395         ServiceTemplate serviceTemplateWithInterfaceDef = getServiceTemplate(BASE_DIR + ST_WITH_INTERFACE_DEF);
396         NodeTemplate nodeTemplateWithInterface =
397                 DataModelUtil.getNodeTemplate(serviceTemplateWithInterfaceDef, NODE_TEMPLATE_ID);
398         Map<String, Object> interfaces = nodeTemplateWithInterface.getInterfaces();
399         Object interfaceObj = interfaces.get(INTERFACE_ID);
400
401         InterfaceDefinitionTemplate actualInterfaceDefinition = new InterfaceDefinitionTemplate(interfaceObj);
402         InterfaceDefinitionTemplate expectedInterfaceDefinitionTemplate = createInterfaceDefinitionTemplate();
403         Assert.assertEquals(expectedInterfaceDefinitionTemplate, actualInterfaceDefinition);
404     }
405
406     @Test
407     public void testYamlToServiceTemplateIncludingHeatExtend() throws IOException {
408         ToscaExtensionYamlUtil toscaExtensionYamlUtil = new ToscaExtensionYamlUtil();
409         try (InputStream yamlFile = toscaExtensionYamlUtil
410                                             .loadYamlFileIs(BASE_DIR + "/serviceTemplateHeatExtend.yaml")) {
411             ServiceTemplate serviceTemplateFromYaml =
412                     toscaExtensionYamlUtil.yamlToObject(yamlFile, ServiceTemplate.class);
413             ParameterDefinitionExt parameterDefinitionExt =
414                     (ParameterDefinitionExt) serviceTemplateFromYaml.getTopology_template().getInputs().get("inParam1");
415             Assert.assertNotNull(parameterDefinitionExt.getLabel());
416             String backToYamlString = toscaExtensionYamlUtil.objectToYaml(serviceTemplateFromYaml);
417             Assert.assertNotNull(backToYamlString);
418         }
419     }
420
421     @Test
422     public void testServiceFilter() throws IOException {
423         ServiceTemplate serviceTemplateWithServiceFilter = getServiceTemplateExt(BASE_DIR + ST_WITH_SERVICE_FILTER);
424
425         NodeTemplate firstNodeTemplate =
426                 DataModelUtil.getNodeTemplate(serviceTemplateWithServiceFilter, FIRST_NODE_TEMPLATE);
427         Map<String, RequirementAssignment> nodeTemplateRequirements =
428                 DataModelUtil.getNodeTemplateRequirements(firstNodeTemplate);
429
430         Object req1 = nodeTemplateRequirements.get(REQ1);
431         Assert.assertTrue(req1 instanceof RequirementAssignmentExt);
432         Assert.assertNotNull(((RequirementAssignmentExt) req1).getService_filter());
433         List<Map<String, List<Constraint>>> properties =
434                 ((RequirementAssignmentExt) req1).getService_filter().getProperties();
435         Assert.assertNotNull(properties);
436         List<Constraint> vmdNameConstrain = properties.get(0).get(VMD_NAME);
437         Assert.assertNotNull(vmdNameConstrain);
438         Assert.assertNotNull(vmdNameConstrain.get(0).getEqual());
439
440         List<Map<String, CapabilityFilter>> capabilities =
441                 ((RequirementAssignmentExt) req1).getService_filter().getCapabilities();
442         Assert.assertNotNull(capabilities);
443         CapabilityFilter capabilityFilter = capabilities.get(0).get(DIRECTOR);
444         Assert.assertNotNull(capabilityFilter);
445         Assert.assertNotNull(capabilityFilter.getProperties());
446
447
448         Object req2 = nodeTemplateRequirements.get(REQ2);
449         Assert.assertTrue(req2 instanceof RequirementAssignmentExt);
450         Assert.assertNotNull(((RequirementAssignmentExt) req2).getService_filter());
451         Object toscaId = ((RequirementAssignmentExt) req2).getService_filter().getTosca_id();
452         Assert.assertNotNull(toscaId);
453         Assert.assertEquals(SERVICE_FILTER_TOSCA_ID, toscaId.toString());
454
455
456         String serviceTemplateYaml = toscaExtensionYamlUtil.objectToYaml(serviceTemplateWithServiceFilter);
457         Assert.assertNotNull(serviceTemplateYaml);
458
459     }
460
461     @Test
462     public void testSubstitutionMapping() throws IOException {
463         ServiceTemplate serviceTemplate = getServiceTemplateExt(BASE_DIR + SUBSTITUTION_MAPPING);
464
465         SubstitutionMapping substitutionMappings = DataModelUtil.getSubstitutionMappings(serviceTemplate);
466         Assert.assertEquals("myNodeType.node", substitutionMappings.getNode_type());
467         Assert.assertNotNull(substitutionMappings.getCapabilities());
468         Assert.assertEquals(1,substitutionMappings.getCapabilities().size());
469         Assert.assertNotNull(substitutionMappings.getRequirements());
470         Assert.assertEquals(1,substitutionMappings.getRequirements().size());
471         Assert.assertEquals(true, substitutionMappings instanceof SubstitutionMappingExt);
472         Assert.assertNull(((SubstitutionMappingExt)substitutionMappings).getSubstitution_filter());
473
474     }
475
476     @Test
477     public void testSubstitutionMappingExt() throws IOException {
478         ServiceTemplate serviceTemplate = getServiceTemplateExt(BASE_DIR + SUBSTITUTION_MAPPING_EXT);
479
480         SubstitutionMapping substitutionMappings = DataModelUtil.getSubstitutionMappings(serviceTemplate);
481         Assert.assertEquals("myNodeType.node", substitutionMappings.getNode_type());
482         Assert.assertNotNull(substitutionMappings.getCapabilities());
483         Assert.assertEquals(1,substitutionMappings.getCapabilities().size());
484         Assert.assertNotNull(substitutionMappings.getRequirements());
485         Assert.assertEquals(1,substitutionMappings.getRequirements().size());
486         Assert.assertEquals(true, substitutionMappings instanceof SubstitutionMappingExt);
487         SubstitutionFilter substitutionFilter = ((SubstitutionMappingExt) substitutionMappings).getSubstitution_filter();
488         Assert.assertNotNull(substitutionFilter);
489         Assert.assertNotNull(substitutionFilter.getProperties());
490         Assert.assertEquals(2,substitutionFilter.getProperties().size());
491         List<Constraint> vendorFilter = substitutionFilter.getProperties().get(0).get("vendor");
492         Assert.assertNotNull(vendorFilter);
493         Assert.assertNotNull(vendorFilter.get(0).getEqual());
494
495
496
497     }
498
499     private ServiceTemplate getServiceTemplate(String inputPath) throws IOException {
500         try (InputStream yamlFile = yamlUtil.loadYamlFileIs(inputPath)) {
501             return yamlUtil.yamlToObject(yamlFile, ServiceTemplate.class);
502         }
503     }
504
505     private ServiceTemplate getServiceTemplateExt(String inputPath) throws IOException {
506         try (InputStream yamlFile = toscaExtensionYamlUtil.loadYamlFileIs(inputPath)) {
507             return toscaExtensionYamlUtil.yamlToObject(yamlFile, ServiceTemplate.class);
508         }
509     }
510
511     private InterfaceType createInterfaceType() {
512         OperationDefinition operationDefinition = createOperationDefinition();
513         InterfaceType interfaceType = new InterfaceType();
514         interfaceType.setDescription("test interface");
515         interfaceType.addOperation(OPERATION_START, operationDefinition);
516         return interfaceType;
517     }
518
519     private OperationDefinition createOperationDefinition() {
520         OperationDefinition operationDefinition = new OperationDefinition();
521         operationDefinition.setDescription(OPERATION_DESC);
522         return operationDefinition;
523     }
524
525     private InterfaceDefinitionType createInterfaceDefinitionType() {
526         OperationDefinitionType operationDefinitionType = createOperationDefinitionType();
527         InterfaceDefinitionType interfaceDefinitionType = new InterfaceDefinitionType();
528         interfaceDefinitionType.setType(INTERFACE_TYPE_VALUE);
529         interfaceDefinitionType.addOperation(OPERATION_START, operationDefinitionType);
530         return interfaceDefinitionType;
531     }
532
533     private InterfaceDefinitionTemplate createInterfaceDefinitionTemplate() {
534         OperationDefinitionTemplate operationDefinitionTemplate = createOperationDefinitionTemplate();
535         InterfaceDefinitionTemplate interfaceDefinitionTemplate = new InterfaceDefinitionTemplate();
536         interfaceDefinitionTemplate.addOperation(OPERATION_START, operationDefinitionTemplate);
537         return interfaceDefinitionTemplate;
538     }
539
540     private OperationDefinitionTemplate createOperationDefinitionTemplate() {
541         OperationDefinitionTemplate operationDefinitionTemplate = new OperationDefinitionTemplate();
542         operationDefinitionTemplate.setDescription(OPERATION_DESC);
543         Implementation implementation = new Implementation();
544         implementation.setPrimary(PRIMARY_IMPL);
545         List<String> dependencies = new ArrayList<>();
546         dependencies.add(DEPENDENCY_NAME);
547         implementation.setDependencies(dependencies);
548         operationDefinitionTemplate.setImplementation(implementation);
549         return operationDefinitionTemplate;
550     }
551
552     private OperationDefinitionType createOperationDefinitionType() {
553         OperationDefinitionType operationDefinitionType = new OperationDefinitionType();
554         operationDefinitionType.setDescription(OPERATION_DESC);
555         operationDefinitionType.setImplementation(IMPLEMENTATION_NAME);
556         PropertyDefinition propertyDefinition = new PropertyDefinition();
557         propertyDefinition.setType(STRING_TYPE);
558         return operationDefinitionType;
559     }
560 }
561
562
563
564
565
566