Rename packages from openecomp to onap.
[sdc.git] / openecomp-be / lib / openecomp-tosca-lib / src / test / java / org / openecomp / sdc / tosca / services / impl / ToscaAnalyzerServiceImplTest.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.services.impl;
18
19 import org.junit.Assert;
20 import org.junit.Before;
21 import org.junit.BeforeClass;
22 import org.junit.Rule;
23 import org.junit.Test;
24 import org.junit.rules.ExpectedException;
25 import org.junit.runner.RunWith;
26 import org.mockito.Mock;
27 import org.mockito.MockitoAnnotations;
28 import org.mockito.runners.MockitoJUnitRunner;
29 import org.openecomp.sdc.common.errors.CoreException;
30 import org.openecomp.sdc.tosca.TestUtil;
31 import org.openecomp.sdc.tosca.datatypes.ToscaElementTypes;
32 import org.openecomp.sdc.tosca.datatypes.ToscaNodeType;
33 import org.openecomp.sdc.tosca.datatypes.ToscaServiceModel;
34 import org.onap.sdc.tosca.datatypes.model.CapabilityDefinition;
35 import org.onap.sdc.tosca.datatypes.model.DataType;
36 import org.onap.sdc.tosca.datatypes.model.DefinitionOfDataType;
37 import org.onap.sdc.tosca.datatypes.model.DataType;
38 import org.onap.sdc.tosca.datatypes.model.Import;
39 import org.onap.sdc.tosca.datatypes.model.InterfaceDefinitionType;
40 import org.onap.sdc.tosca.datatypes.model.InterfaceType;
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.ParameterDefinition;
44 import org.onap.sdc.tosca.datatypes.model.PropertyDefinition;
45 import org.onap.sdc.tosca.datatypes.model.RequirementAssignment;
46 import org.onap.sdc.tosca.datatypes.model.RequirementDefinition;
47 import org.onap.sdc.tosca.datatypes.model.ServiceTemplate;
48 import org.onap.sdc.tosca.datatypes.model.SubstitutionMapping;
49 import org.onap.sdc.tosca.datatypes.model.TopologyTemplate;
50 import org.openecomp.sdc.tosca.services.ToscaAnalyzerService;
51 import org.openecomp.sdc.tosca.services.ToscaConstants;
52 import org.onap.sdc.tosca.services.ToscaExtensionYamlUtil;
53
54 import java.io.IOException;
55 import java.io.InputStream;
56 import java.util.ArrayList;
57 import java.util.HashMap;
58 import java.util.List;
59 import java.util.Map;
60 import java.util.Optional;
61
62 import static org.junit.Assert.assertEquals;
63 import static org.junit.Assert.assertFalse;
64 import static org.junit.Assert.assertNotNull;
65 import static org.junit.Assert.assertTrue;
66 import static org.mockito.Mockito.when;
67
68
69 /**
70  * @author Avrahamg
71  * @since July 14, 2016
72  */
73 @RunWith(MockitoJUnitRunner.class)
74 public class ToscaAnalyzerServiceImplTest {
75     /*
76     Dictionary:
77     SrvTmp: ServiceTemplate
78     NdTmp: NodeTemplate
79     NdTy: NodeType
80     */
81
82   private static ToscaAnalyzerService toscaAnalyzerService;
83   private static ToscaServiceModel toscaServiceModel;
84   @Rule
85   public ExpectedException thrown = ExpectedException.none();
86
87   @Mock
88   private NodeTemplate nodeTemplateMock;
89   @Mock
90   private ParameterDefinition parameterDefinitionMock;
91   @Mock
92   private PropertyDefinition propertyDefinitionMock;
93   @Mock
94   private InterfaceDefinitionType interfaceDefinitionMock;
95   @Mock
96   private ToscaServiceModel toscaServiceModelMock;
97
98   @BeforeClass
99   public static void onlyOnceSetUp() throws IOException {
100     toscaAnalyzerService = new ToscaAnalyzerServiceImpl();
101     toscaServiceModel = TestUtil.loadToscaServiceModel("/mock/analyzerService/toscasubstitution/",
102         "/mock/globalServiceTemplates/", null);
103   }
104
105   @Before
106   public void init() throws IOException {
107     MockitoAnnotations.initMocks(this);
108   }
109
110   @Test
111   public void testGetFlatEntityNotFound() throws Exception {
112     thrown.expect(CoreException.class);
113     thrown.expectMessage(
114         "Entity Type 'org.openecomp.resource.vfc.notFound' or one of its derivedFrom type hierarchy, is not defined in tosca service model");
115     ToscaExtensionYamlUtil toscaExtensionYamlUtil = new ToscaExtensionYamlUtil();
116     try (InputStream yamlFile = toscaExtensionYamlUtil
117         .loadYamlFileIs("/mock/analyzerService/NestedServiceTemplateReqTest.yaml")) {
118
119       ServiceTemplate
120           serviceTemplateFromYaml =
121           toscaExtensionYamlUtil.yamlToObject(yamlFile, ServiceTemplate.class);
122
123       toscaAnalyzerService
124           .getFlatEntity(ToscaElementTypes.NODE_TYPE, "org.openecomp.resource.vfc.notFound",
125               serviceTemplateFromYaml, toscaServiceModel);
126     }
127   }
128
129   @Test
130   public void testGetFlatEntityNodeType() throws Exception {
131     ToscaExtensionYamlUtil toscaExtensionYamlUtil = new ToscaExtensionYamlUtil();
132     try (InputStream yamlFile = toscaExtensionYamlUtil
133         .loadYamlFileIs("/mock/analyzerService/NestedServiceTemplateReqTest.yaml")) {
134
135       ServiceTemplate
136           serviceTemplateFromYaml =
137           toscaExtensionYamlUtil.yamlToObject(yamlFile, ServiceTemplate.class);
138
139       final NodeType flatEntity = (NodeType) toscaAnalyzerService
140           .getFlatEntity(ToscaElementTypes.NODE_TYPE, "org.openecomp.resource.vfc.nodes.heat" +
141               ".cmaui_image", serviceTemplateFromYaml, toscaServiceModel);
142
143       Assert.assertNotNull(flatEntity);
144       Assert.assertEquals("org.openecomp.resource.vfc.nodes.heat.nova.Server", flatEntity
145           .getDerived_from());
146       Assert.assertEquals(20, flatEntity.getProperties().size());
147       Assert.assertEquals("overridden default value",
148           flatEntity.getProperties().get("admin_pass").get_default());
149       Assert.assertEquals("REBUILD",
150           flatEntity.getProperties().get("image_update_policy").get_default());
151       Assert.assertNotNull(flatEntity.getProperties().get("new_property"));
152     }
153   }
154
155   @Test
156   public void testGetFlatEntityDataType() throws Exception {
157     ToscaExtensionYamlUtil toscaExtensionYamlUtil = new ToscaExtensionYamlUtil();
158     try (InputStream yamlFile = toscaExtensionYamlUtil
159         .loadYamlFileIs("/mock/analyzerService/NestedServiceTemplateReqTest.yaml")) {
160
161       ServiceTemplate
162           serviceTemplateFromYaml =
163           toscaExtensionYamlUtil.yamlToObject(yamlFile, ServiceTemplate.class);
164
165       final DataType flatEntity = (DataType) toscaAnalyzerService
166           .getFlatEntity(ToscaElementTypes.DATA_TYPE,
167               "org.openecomp.datatypes.heat.network.MyNewAddressPair", serviceTemplateFromYaml,
168               toscaServiceModel);
169
170       Assert.assertNotNull(flatEntity);
171       Assert.assertEquals("org.openecomp.datatypes.heat.network.MyAddressPair", flatEntity
172           .getDerived_from());
173       Assert.assertEquals(3, flatEntity.getProperties().size());
174       Assert.assertEquals("overridden default value",
175           flatEntity.getProperties().get("mac_address").get_default());
176       Assert.assertEquals(true,
177           flatEntity.getProperties().get("mac_address").getRequired());
178       Assert.assertNotNull(flatEntity.getProperties().get("new_property"));
179     }
180   }
181
182   @Test
183   public void testCalculateExposedRequirements() throws Exception {
184     Map<String, RequirementDefinition> nodeTypeRequirementDefinition = new HashMap<>();
185     RequirementDefinition rd = new RequirementDefinition();
186     rd.setCapability("tosca.capabilities.Node");
187     rd.setNode("tosca.nodes.Root");
188     rd.setRelationship("tosca.relationships.DependsOn");
189     Object[] occurences = new Object[]{0, "UNBOUNDED"};
190     rd.setOccurrences(occurences);
191
192     RequirementDefinition rd1 = new RequirementDefinition();
193     rd.setCapability("tosca.capabilities.network.Bindable");
194     rd.setNode(null);
195     rd.setRelationship("tosca.relationships.network.BindsTo");
196     Object[] occurences1 = new Object[]{1, 1};
197     rd1.setOccurrences(occurences1);
198
199     nodeTypeRequirementDefinition.put("binding", rd1);
200     nodeTypeRequirementDefinition.put("dependency", rd);
201
202     Map<String, Map<String, RequirementAssignment>> fullFilledRequirementsDefinition =
203         new HashMap<>();
204     Map<String, RequirementAssignment> nodeTemplateRequirementsAssignment = new HashMap<>();
205     RequirementAssignment ra = new RequirementAssignment();
206     ra.setCapability("tosca.capabilities.network.Bindable");
207     ra.setNode("pd_server");
208     ra.setRelationship("tosca.relationships.network.BindsTo");
209     nodeTemplateRequirementsAssignment.put("binding", ra);
210
211     List<Map<String, RequirementDefinition>> nodeTypeRequirementsDefinition = new ArrayList<>();
212     nodeTypeRequirementsDefinition.add(nodeTypeRequirementDefinition);
213
214     List<Map<String, RequirementDefinition>> exposedRequirements = toscaAnalyzerService
215         .calculateExposedRequirements(nodeTypeRequirementsDefinition,
216             nodeTemplateRequirementsAssignment);
217     Assert.assertEquals(1, exposedRequirements.size());
218   }
219
220   @Test
221   public void testCalExpReqWithNullNodeInReqAssignment() throws Exception {
222     Map<String, RequirementDefinition> nodeTypeRequirementDefinition = new HashMap<>();
223     RequirementDefinition rd = new RequirementDefinition();
224     rd.setCapability("tosca.capabilities.Node");
225     rd.setNode("tosca.nodes.Root");
226     rd.setRelationship("tosca.relationships.DependsOn");
227     Object[] occurences = new Object[]{0, "UNBOUNDED"};
228     rd.setOccurrences(occurences);
229
230     RequirementDefinition rd1 = new RequirementDefinition();
231     rd.setCapability("tosca.capabilities.network.Bindable");
232     rd.setNode(null);
233     rd.setRelationship("tosca.relationships.network.BindsTo");
234     Object[] occurences1 = new Object[]{1, 1};
235     rd1.setOccurrences(occurences1);
236
237     nodeTypeRequirementDefinition.put("binding", rd1);
238     nodeTypeRequirementDefinition.put("dependency", rd);
239
240     Map<String, Map<String, RequirementAssignment>> fullFilledRequirementsDefinition =
241         new HashMap<>();
242     Map<String, RequirementAssignment> nodeTemplateRequirementsAssignment = new HashMap<>();
243     RequirementAssignment ra = new RequirementAssignment();
244     ra.setCapability("tosca.capabilities.network.Bindable");
245     ra.setNode(null);
246     ra.setRelationship("tosca.relationships.network.BindsTo");
247     nodeTemplateRequirementsAssignment.put("binding", ra);
248
249     List<Map<String, RequirementDefinition>> nodeTypeRequirementsDefinition = new ArrayList<>();
250     nodeTypeRequirementsDefinition.add(nodeTypeRequirementDefinition);
251
252     List<Map<String, RequirementDefinition>> exposedRequirements = toscaAnalyzerService
253         .calculateExposedRequirements(nodeTypeRequirementsDefinition,
254             nodeTemplateRequirementsAssignment);
255     Assert.assertEquals(1, exposedRequirements.size());
256   }
257
258   @Test
259   public void testCalculateExposedCapabilities() throws Exception {
260     Map<String, CapabilityDefinition> nodeTypeCapabilitiesDefinition = new HashMap<>();
261     CapabilityDefinition cd = new CapabilityDefinition();
262     cd.setType("tosca.capabilities.Scalable");
263     nodeTypeCapabilitiesDefinition.put("tosca.capabilities.network.Bindable_pd_server", cd);
264     Map<String, Map<String, RequirementAssignment>> fullFilledRequirementsDefinition =
265         new HashMap<>();
266     Map<String, RequirementAssignment> nodeTemplateRequirementsAssignment = new HashMap<>();
267     RequirementAssignment ra = new RequirementAssignment();
268     ra.setCapability("tosca.capabilities.network.Bindable");
269     ra.setNode("pd_server");
270     ra.setRelationship("tosca.relationships.network.BindsTo");
271     nodeTemplateRequirementsAssignment.put("binding", ra);
272     fullFilledRequirementsDefinition.put("pd_server", nodeTemplateRequirementsAssignment);
273     Map<String, CapabilityDefinition> exposedCapabilities =
274         toscaAnalyzerService.calculateExposedCapabilities(nodeTypeCapabilitiesDefinition,
275             fullFilledRequirementsDefinition);
276     Assert.assertEquals(1, exposedCapabilities.size());
277   }
278
279   @Test
280   public void testIsRequirementExistsWithInvalidReqId() throws Exception {
281     ToscaExtensionYamlUtil toscaExtensionYamlUtil = new ToscaExtensionYamlUtil();
282     try (InputStream yamlFile = toscaExtensionYamlUtil
283         .loadYamlFileIs("/mock/analyzerService/NestedServiceTemplateReqTest.yaml")) {
284
285       ServiceTemplate
286           serviceTemplateFromYaml =
287           toscaExtensionYamlUtil.yamlToObject(yamlFile, ServiceTemplate.class);
288
289       TestUtil.createConcreteRequirementObjectsInServiceTemplate(serviceTemplateFromYaml,
290           toscaExtensionYamlUtil);
291
292       NodeTemplate port_0 =
293           serviceTemplateFromYaml.getTopology_template().getNode_templates().get("cmaui_port_0");
294
295       RequirementAssignment ra = new RequirementAssignment();
296       ra.setCapability("tosca.capabilities.network.Bindable");
297       ra.setNode("server_cmaui");
298       ra.setRelationship("tosca.relationships.network.BindsTo");
299
300       //Test With Empty requirementId
301       Assert.assertEquals(false,
302           toscaAnalyzerService.isRequirementExistInNodeTemplate(port_0, "", ra));
303
304       //Test With valid requirementId
305       Assert.assertEquals(true,
306           toscaAnalyzerService.isRequirementExistInNodeTemplate(port_0, "binding", ra));
307
308       //Test With invalid requirement assignment
309       RequirementAssignment ra1 = new RequirementAssignment();
310       ra1.setCapability("tosca.capabilities.network.Bindable1");
311       ra1.setNode("server_cmaui1");
312       ra1.setRelationship("tosca.relationships.network.BindsTo1");
313       Assert.assertEquals(false,
314           toscaAnalyzerService.isRequirementExistInNodeTemplate(port_0, "binding", ra1));
315     }
316   }
317
318   @Test
319   public void testGetRequirements() throws Exception {
320     ToscaExtensionYamlUtil toscaExtensionYamlUtil = new ToscaExtensionYamlUtil();
321     try (InputStream yamlFile = toscaExtensionYamlUtil
322         .loadYamlFileIs("/mock/analyzerService/NestedServiceTemplateReqTest.yaml")) {
323
324       ServiceTemplate
325           serviceTemplateFromYaml =
326           toscaExtensionYamlUtil.yamlToObject(yamlFile, ServiceTemplate.class);
327
328       NodeTemplate port_0 =
329           serviceTemplateFromYaml.getTopology_template().getNode_templates().get("cmaui_port_0");
330       List<RequirementAssignment> reqList =
331           toscaAnalyzerService.getRequirements(port_0, ToscaConstants.BINDING_REQUIREMENT_ID);
332       assertEquals(1, reqList.size());
333
334       reqList.clear();
335       NodeTemplate port_1 =
336           serviceTemplateFromYaml.getTopology_template().getNode_templates().get("cmaui1_port_1");
337       reqList = toscaAnalyzerService.getRequirements(port_1, ToscaConstants.LINK_REQUIREMENT_ID);
338       assertEquals(2, reqList.size());
339
340       reqList.clear();
341       reqList = toscaAnalyzerService.getRequirements(port_0, ToscaConstants.LINK_REQUIREMENT_ID);
342       assertEquals(0, reqList.size());
343     }
344   }
345
346   @Test
347   public void testGetNodeTemplateById() throws Exception {
348     ServiceTemplate emptyServiceTemplate = new ServiceTemplate();
349     Optional<NodeTemplate> nodeTemplate =
350         toscaAnalyzerService.getNodeTemplateById(emptyServiceTemplate, "test_net222");
351     assertEquals(false, nodeTemplate.isPresent());
352
353     ServiceTemplate mainServiceTemplate = toscaServiceModel.getServiceTemplates()
354         .get(toscaServiceModel.getEntryDefinitionServiceTemplate());
355     nodeTemplate = toscaAnalyzerService.getNodeTemplateById(mainServiceTemplate, "test_net");
356     assertEquals(true, nodeTemplate.isPresent());
357
358     nodeTemplate = toscaAnalyzerService.getNodeTemplateById(mainServiceTemplate, "test_net222");
359     assertEquals(false, nodeTemplate.isPresent());
360   }
361
362   @Test
363   public void testGetSubstituteServiceTemplateName() throws Exception {
364     thrown.expect(CoreException.class);
365     thrown.expectMessage(
366         "Invalid Substitute Node Template invalid2, mandatory map property service_template_filter with mandatory key substitute_service_template must be defined.");
367
368     Optional<String> substituteServiceTemplateName;
369
370     ServiceTemplate mainServiceTemplate = toscaServiceModel.getServiceTemplates()
371         .get(toscaServiceModel.getEntryDefinitionServiceTemplate());
372     Optional<NodeTemplate> notSubstitutableNodeTemplate =
373         toscaAnalyzerService.getNodeTemplateById(mainServiceTemplate, "test_net");
374     assertEquals(true, notSubstitutableNodeTemplate.isPresent());
375
376     if (notSubstitutableNodeTemplate.isPresent()) {
377       substituteServiceTemplateName = toscaAnalyzerService
378           .getSubstituteServiceTemplateName("test_net", notSubstitutableNodeTemplate.get());
379       assertEquals(false, substituteServiceTemplateName.isPresent());
380     }
381
382     Optional<NodeTemplate> substitutableNodeTemplate =
383         toscaAnalyzerService.getNodeTemplateById(mainServiceTemplate, "test_nested");
384     assertEquals(true, substitutableNodeTemplate.isPresent());
385     if (substitutableNodeTemplate.isPresent()) {
386       substituteServiceTemplateName = toscaAnalyzerService
387           .getSubstituteServiceTemplateName("test_nested", substitutableNodeTemplate.get());
388       assertEquals(true, substituteServiceTemplateName.isPresent());
389       assertEquals("nestedServiceTemplate.yaml", substituteServiceTemplateName.get());
390     }
391
392     NodeTemplate invalidSubstitutableNodeTemplate1 = new NodeTemplate();
393     substituteServiceTemplateName = toscaAnalyzerService
394         .getSubstituteServiceTemplateName("invalid1", invalidSubstitutableNodeTemplate1);
395     assertEquals(false, substituteServiceTemplateName.isPresent());
396
397     substitutableNodeTemplate.ifPresent(nodeTemplate -> {
398       Object serviceTemplateFilter = nodeTemplate.getProperties()
399           .get(ToscaConstants.SERVICE_TEMPLATE_FILTER_PROPERTY_NAME);
400       ((Map) serviceTemplateFilter).clear();
401       toscaAnalyzerService
402           .getSubstituteServiceTemplateName("invalid2", nodeTemplate);
403
404     });
405   }
406
407
408   @Test
409   public void testGetSubstitutableNodeTemplates() throws Exception {
410     ToscaExtensionYamlUtil toscaExtensionYamlUtil = new ToscaExtensionYamlUtil();
411     try (InputStream yamlFile = toscaExtensionYamlUtil
412         .loadYamlFileIs("/mock/analyzerService/ServiceTemplateSubstituteTest.yaml")) {
413       ServiceTemplate serviceTemplateFromYaml =
414           toscaExtensionYamlUtil.yamlToObject(yamlFile, ServiceTemplate.class);
415
416       Map<String, NodeTemplate> substitutableNodeTemplates =
417           toscaAnalyzerService.getSubstitutableNodeTemplates(serviceTemplateFromYaml);
418       assertEquals(2, substitutableNodeTemplates.size());
419       assertNotNull(substitutableNodeTemplates.get("test_nested1"));
420       assertNotNull(substitutableNodeTemplates.get("test_nested2"));
421
422       ServiceTemplate emptyServiceTemplate = new ServiceTemplate();
423       emptyServiceTemplate.setTopology_template(new TopologyTemplate());
424       substitutableNodeTemplates =
425           toscaAnalyzerService.getSubstitutableNodeTemplates(emptyServiceTemplate);
426       assertEquals(0, substitutableNodeTemplates.size());
427     }
428
429     try (InputStream yamlFile = toscaExtensionYamlUtil
430         .loadYamlFileIs("/mock/analyzerService/NestedServiceTemplateReqTest.yaml")) {
431       ServiceTemplate serviceTemplateFromYaml =
432           toscaExtensionYamlUtil.yamlToObject(yamlFile, ServiceTemplate.class);
433       Map<String, NodeTemplate> substitutableNodeTemplates =
434           toscaAnalyzerService.getSubstitutableNodeTemplates(serviceTemplateFromYaml);
435       assertEquals(0, substitutableNodeTemplates.size());
436     }
437   }
438
439   @Test
440   public void testGetSubstitutionMappedNodeTemplateByExposedReq() throws Exception {
441     thrown.expect(CoreException.class);
442     thrown.expectMessage(
443         "Invalid Tosca model data, missing 'Node Template' entry for 'Node Template' id cmaui_port_9");
444     ToscaExtensionYamlUtil toscaExtensionYamlUtil = new ToscaExtensionYamlUtil();
445     try (InputStream yamlFile = toscaExtensionYamlUtil
446         .loadYamlFileIs("/mock/analyzerService/NestedServiceTemplateReqTest.yaml")) {
447       ServiceTemplate nestedServiceTemplateFromYaml =
448           toscaExtensionYamlUtil.yamlToObject(yamlFile, ServiceTemplate.class);
449
450       Optional<Map.Entry<String, NodeTemplate>> mappedNodeTemplate = toscaAnalyzerService
451           .getSubstitutionMappedNodeTemplateByExposedReq("NestedServiceTemplateSubstituteTest.yaml",
452               nestedServiceTemplateFromYaml, "local_storage_server_cmaui");
453       assertEquals(true, mappedNodeTemplate.isPresent());
454       mappedNodeTemplate.ifPresent(stringNodeTemplateEntry -> {
455         assertEquals("server_cmaui", stringNodeTemplateEntry.getKey());
456         assertNotNull(stringNodeTemplateEntry.getValue());
457       });
458
459       mappedNodeTemplate = toscaAnalyzerService
460           .getSubstitutionMappedNodeTemplateByExposedReq("NestedServiceTemplateSubstituteTest.yaml",
461               nestedServiceTemplateFromYaml, "link_cmaui_port_invalid");
462       assertEquals(true, mappedNodeTemplate.isPresent());
463       mappedNodeTemplate.ifPresent(stringNodeTemplateEntry -> {
464         assertEquals("server_cmaui", stringNodeTemplateEntry.getKey());
465         assertNotNull(stringNodeTemplateEntry.getValue());
466       });
467
468       ServiceTemplate mainServiceTemplate = toscaServiceModel.getServiceTemplates()
469           .get(toscaServiceModel.getEntryDefinitionServiceTemplate());
470       mappedNodeTemplate = toscaAnalyzerService.getSubstitutionMappedNodeTemplateByExposedReq(
471           toscaServiceModel.getEntryDefinitionServiceTemplate(), mainServiceTemplate,
472           "local_storage_server_cmaui");
473       assertEquals(false, mappedNodeTemplate.isPresent());
474     }
475   }
476
477   @Test
478   public void invalidSubstitutableMapping() throws Exception {
479     thrown.expect(CoreException.class);
480     thrown.expectMessage(
481         "Invalid Substitution Service Template invalidMappingServiceTemplate.yaml, missing mandatory file 'Node type' in substitution mapping.");
482     ServiceTemplate invalidMappingServiceTemplate = new ServiceTemplate();
483     invalidMappingServiceTemplate.setTopology_template(new TopologyTemplate());
484     invalidMappingServiceTemplate.getTopology_template()
485         .setSubstitution_mappings(new SubstitutionMapping());
486     toscaAnalyzerService
487         .getSubstitutionMappedNodeTemplateByExposedReq("invalidMappingServiceTemplate.yaml",
488             invalidMappingServiceTemplate, "local_storage_server_cmaui");
489   }
490
491   @Test
492   public void substitutableMappingWithNoReqMap() throws Exception {
493     ServiceTemplate mainServiceTemplate = toscaServiceModel.getServiceTemplates()
494         .get(toscaServiceModel.getEntryDefinitionServiceTemplate());
495     ServiceTemplate emptyReqMapping = new ServiceTemplate();
496     emptyReqMapping.setTopology_template(new TopologyTemplate());
497     emptyReqMapping.getTopology_template().setSubstitution_mappings(new SubstitutionMapping());
498     emptyReqMapping.getTopology_template().getSubstitution_mappings().setNode_type("temp");
499     Optional<Map.Entry<String, NodeTemplate>> mappedNodeTemplate = toscaAnalyzerService
500         .getSubstitutionMappedNodeTemplateByExposedReq(
501             toscaServiceModel.getEntryDefinitionServiceTemplate(), mainServiceTemplate,
502             "local_storage_server_cmaui");
503     assertEquals(false, mappedNodeTemplate.isPresent());
504   }
505
506   @Test
507   public void testGetSubstitutionMappedNodeTemplateByExposedReqInvalid() throws Exception {
508     thrown.expect(CoreException.class);
509     thrown.expectMessage(
510         "Invalid Tosca model data, missing 'Node Template' entry for 'Node Template' id cmaui_port_9");
511     ToscaExtensionYamlUtil toscaExtensionYamlUtil = new ToscaExtensionYamlUtil();
512     try (InputStream yamlFile = toscaExtensionYamlUtil
513         .loadYamlFileIs("/mock/analyzerService/NestedServiceTemplateReqTest.yaml")) {
514       ServiceTemplate nestedServiceTemplateFromYaml =
515           toscaExtensionYamlUtil.yamlToObject(yamlFile, ServiceTemplate.class);
516
517       toscaAnalyzerService
518           .getSubstitutionMappedNodeTemplateByExposedReq("NestedServiceTemplateSubstituteTest.yaml",
519               nestedServiceTemplateFromYaml, "link_cmaui_port_invalid");
520     }
521   }
522
523   @Test
524   public void testIsDesiredRequirementAssignmentMatch() throws Exception {
525
526     RequirementAssignment requirementAssignment = new RequirementAssignment();
527     String capability = "Test.Capability";
528     String node = "Test.node";
529     String relationship = "Test.relationship";
530     requirementAssignment.setCapability(capability);
531     requirementAssignment.setNode(node);
532     requirementAssignment.setRelationship(relationship);
533
534     assertEquals(true, toscaAnalyzerService
535         .isDesiredRequirementAssignment(requirementAssignment, capability, node, relationship));
536     assertEquals(true, toscaAnalyzerService
537         .isDesiredRequirementAssignment(requirementAssignment, null, node, relationship));
538     assertEquals(true, toscaAnalyzerService
539         .isDesiredRequirementAssignment(requirementAssignment, capability, null, relationship));
540     assertEquals(true, toscaAnalyzerService
541         .isDesiredRequirementAssignment(requirementAssignment, capability, node, null));
542     assertEquals(true, toscaAnalyzerService
543         .isDesiredRequirementAssignment(requirementAssignment, null, null, relationship));
544     assertEquals(true, toscaAnalyzerService
545         .isDesiredRequirementAssignment(requirementAssignment, capability, null, null));
546     assertEquals(true, toscaAnalyzerService
547         .isDesiredRequirementAssignment(requirementAssignment, null, node, null));
548
549   }
550
551   @Test
552   public void testIsDesiredRequirementAssignmentNoMatch() throws Exception {
553
554     RequirementAssignment requirementAssignment = new RequirementAssignment();
555     String capability = "Test.Capability";
556     String node = "Test.node";
557     String relationship = "Test.relationship";
558     requirementAssignment.setCapability(capability);
559     requirementAssignment.setNode(node);
560     requirementAssignment.setRelationship(relationship);
561
562     assertEquals(false, toscaAnalyzerService
563         .isDesiredRequirementAssignment(requirementAssignment, "no", node, relationship));
564     assertEquals(false, toscaAnalyzerService
565         .isDesiredRequirementAssignment(requirementAssignment, "no", "no", relationship));
566     assertEquals(false, toscaAnalyzerService
567         .isDesiredRequirementAssignment(requirementAssignment, "no", "no", "no"));
568     assertEquals(false, toscaAnalyzerService
569         .isDesiredRequirementAssignment(requirementAssignment, capability, "no", relationship));
570     assertEquals(false, toscaAnalyzerService
571         .isDesiredRequirementAssignment(requirementAssignment, capability, node, "no"));
572     assertEquals(false, toscaAnalyzerService
573         .isDesiredRequirementAssignment(requirementAssignment, capability, "no", "no"));
574     assertEquals(false, toscaAnalyzerService
575         .isDesiredRequirementAssignment(requirementAssignment, "no", null, null));
576     assertEquals(false, toscaAnalyzerService
577         .isDesiredRequirementAssignment(requirementAssignment, null, null, null));
578
579
580   }
581
582   @Test
583   public void shouldReturnFalseIfNdTmpIsNull() {
584     NodeTemplate nodeTemplate = null;
585     assertFalse(toscaAnalyzerService
586         .isTypeOf(nodeTemplate, ToscaNodeType.NATIVE_NETWORK, new ServiceTemplate(),
587             toscaServiceModelMock));
588   }
589
590   @Test
591   public void shouldReturnTrueIfNdTmpTypeIsOfRequestedType() {
592     NodeTemplate nodeTemplate = new NodeTemplate();
593     String nodeTypeToSearch = ToscaNodeType.NATIVE_BLOCK_STORAGE;
594     nodeTemplate.setType(nodeTypeToSearch);
595     assertTrue(toscaAnalyzerService
596         .isTypeOf(nodeTemplate, nodeTypeToSearch, new ServiceTemplate(),
597             toscaServiceModelMock));
598   }
599
600   @Test
601   public void shouldReturnTrueIfDataTypeIsOfRequestedType() {
602     PropertyDefinition propertyDefinition = new PropertyDefinition();
603     String propertyTypeToSearch = "tosca.datatypes.TimeInterval";
604     propertyDefinition.setType(propertyTypeToSearch);
605     assertTrue(toscaAnalyzerService
606         .isTypeOf(propertyDefinition, propertyTypeToSearch, new ServiceTemplate(),
607             toscaServiceModelMock));
608   }
609
610   @Test
611   public void shouldReturnTrueIfInterfaceTypeIsOfRequestedType() {
612     InterfaceDefinitionType interfaceDefinition = new InterfaceDefinitionType();
613     String interfaceTypeToSearch = "test.interface.A";
614     interfaceDefinition.setType(interfaceTypeToSearch);
615     assertTrue(toscaAnalyzerService
616         .isTypeOf(interfaceDefinition, interfaceTypeToSearch, new ServiceTemplate(),
617             toscaServiceModelMock));
618   }
619
620   @Test
621   public void shouldReturnTrueIfNdTmpTypeIsFoundInSrvTmpNdTyAndNdTyDerivedFromRequestedType() {
622     String typeToMatch = ToscaNodeType.CINDER_VOLUME;
623     when(nodeTemplateMock.getType()).thenReturn(typeToMatch);
624     Map<String, NodeType> stNodeTypes = new HashMap<>();
625     addNodeType(stNodeTypes, ToscaNodeType.NATIVE_COMPUTE, new NodeType());
626     NodeType nodeType = createNodeType(ToscaNodeType.NATIVE_BLOCK_STORAGE);
627     addNodeType(stNodeTypes, typeToMatch, nodeType);
628     ServiceTemplate serviceTemplate = new ServiceTemplate();
629     serviceTemplate.setNode_types(stNodeTypes);
630     assertTrue(toscaAnalyzerService
631         .isTypeOf(nodeTemplateMock, ToscaNodeType.NATIVE_BLOCK_STORAGE,
632             serviceTemplate, toscaServiceModelMock));
633
634   }
635
636   @Test
637   public void dataTypeParameterExistInHierarchy() {
638     String testedDataTypeKey = "test.dataType.B";
639     when(parameterDefinitionMock.getType()).thenReturn(testedDataTypeKey);
640     dataTypeExistInHierarchy(testedDataTypeKey, parameterDefinitionMock);
641
642   }
643
644   @Test
645   public void dataTypePropertyExistInHierarchy() {
646     String testedDataTypeKey = "test.dataType.B";
647     when(propertyDefinitionMock.getType()).thenReturn(testedDataTypeKey);
648     dataTypeExistInHierarchy(testedDataTypeKey, propertyDefinitionMock);
649   }
650
651   private void dataTypeExistInHierarchy(String testedDataTypeKey,
652                                         DefinitionOfDataType testedDefinitionDataType) {
653     String typeToMatch = "test.dataType.A";
654     Map<String, DataType> stDataTypes = new HashMap<>();
655     addDataType(stDataTypes, "tosca.datatypes.network.NetworkInfo", new DataType());
656     DataType testedDataType = createDataType(typeToMatch);
657     addDataType(stDataTypes, testedDataTypeKey, testedDataType);
658     ServiceTemplate serviceTemplate = new ServiceTemplate();
659     serviceTemplate.setData_types(stDataTypes);
660     assertTrue(toscaAnalyzerService
661         .isTypeOf(testedDefinitionDataType, typeToMatch, serviceTemplate, toscaServiceModelMock));
662   }
663
664   @Test
665   public void interfaceTypeExistInHierarchy() {
666     String typeToMatch = "test.interfaceType.A";
667     String testedInterfaceTypeKey = "test.interfaceType.B";
668     when(interfaceDefinitionMock.getType()).thenReturn(testedInterfaceTypeKey);
669     Map<String, Object> stInterfaceTypes = new HashMap<>();
670     stInterfaceTypes.put("tosca.interfaces.network.NetworkInfo", new InterfaceType());
671     InterfaceType testedInterfaceType = createInterfaceType(typeToMatch);
672     stInterfaceTypes.put(testedInterfaceTypeKey, testedInterfaceType);
673     ServiceTemplate serviceTemplate = new ServiceTemplate();
674     serviceTemplate.setInterface_types(stInterfaceTypes);
675     assertTrue(toscaAnalyzerService
676         .isTypeOf(interfaceDefinitionMock, "test.interfaceType.A",
677             serviceTemplate, toscaServiceModelMock));
678   }
679
680   @Test
681   public void shouldThrowCoreExceptionForInvalidNodeType() {
682     thrown.expect(CoreException.class);
683     thrown.expectMessage(
684         "Entity Type 'AAA' or one of its derivedFrom type hierarchy, is not defined in " +
685             "tosca service model");
686     when(nodeTemplateMock.getType()).thenReturn("AAA");
687     Map<String, NodeType> stNodeTypes = new HashMap<>();
688     addNodeType(stNodeTypes, "notImportant", new NodeType());
689     ServiceTemplate serviceTemplate = new ServiceTemplate();
690     serviceTemplate.setNode_types(stNodeTypes);
691     toscaAnalyzerService
692         .isTypeOf(nodeTemplateMock, ToscaNodeType.NATIVE_COMPUTE, serviceTemplate,
693             toscaServiceModelMock);
694   }
695
696   @Test
697   public void shouldThrowCoreExceptionForInvalidNodeType2Level() {
698     thrown.expect(CoreException.class);
699     thrown.expectMessage(
700         "Entity Type 'A' or one of its derivedFrom type hierarchy, is not defined in tosca " +
701             "service model");
702     String typeToMatch = "A";
703     when(nodeTemplateMock.getType()).thenReturn(typeToMatch);
704     Map<String, NodeType> stNodeTypes = new HashMap<>();
705     addNodeType(stNodeTypes, "notImportant", new NodeType());
706     addNodeType(stNodeTypes, "A", createNodeType("ADerivedFromB"));
707     addNodeType(stNodeTypes, "ADerivedFromB'", createNodeType("BDerivedFromC"));
708     ServiceTemplate serviceTemplate = new ServiceTemplate();
709     serviceTemplate.setNode_types(stNodeTypes);
710     assertTrue(toscaAnalyzerService
711         .isTypeOf(nodeTemplateMock, "BDerivedFromC", serviceTemplate, toscaServiceModelMock));
712   }
713
714   @Test
715   public void shouldReturnTrueIfNdTmpTypeIsFoundInSrvTmpNdTyAndNotDerivedFromRequestedTypeBut2ndLevelDerivedFromMatch() {
716     String typeToMatch = "A";
717     when(nodeTemplateMock.getType()).thenReturn(typeToMatch);
718     Map<String, NodeType> stNodeTypes = new HashMap<>();
719     addNodeType(stNodeTypes, "notImportant", new NodeType());
720     addNodeType(stNodeTypes, "A", createNodeType("ADerivedFromB"));
721     addNodeType(stNodeTypes, "ADerivedFromB", createNodeType("BDerivedFromC"));
722     ServiceTemplate serviceTemplate = new ServiceTemplate();
723     serviceTemplate.setNode_types(stNodeTypes);
724     assertTrue(toscaAnalyzerService
725         .isTypeOf(nodeTemplateMock, "BDerivedFromC", serviceTemplate, toscaServiceModelMock));
726   }
727
728   private NodeType createNodeType(String derivedFrom) {
729     NodeType nodeType = new NodeType();
730     nodeType.setDerived_from(derivedFrom);
731     return nodeType;
732   }
733
734   private DataType createDataType(String derivedFrom) {
735     DataType dataType = new DataType();
736     dataType.setDerived_from(derivedFrom);
737     return dataType;
738   }
739
740   private InterfaceType createInterfaceType(String derivedFrom) {
741     InterfaceType interfaceType = new InterfaceType();
742     interfaceType.setDerived_from(derivedFrom);
743     return interfaceType;
744   }
745
746   private void addNodeType(Map<String, NodeType> stNodeTypes, String key, NodeType nodeType) {
747     stNodeTypes.put(key, nodeType);
748   }
749
750   private void addDataType(Map<String, DataType> stDataTypes, String key, DataType dataType) {
751     stDataTypes.put(key, dataType);
752   }
753
754   @Test
755   public void shouldReturnTrueIfNdTmpTypeIsFoundInSrvTmpNdTyButRequestedTypeNotMatchButFoundIn1stLevelImports() {
756     String typeToMatch = ToscaNodeType.CINDER_VOLUME;
757     when(nodeTemplateMock.getType()).thenReturn(typeToMatch);
758     ServiceTemplate mainST = new ServiceTemplate();
759     List<Map<String, Import>> imports = new ArrayList<>();
760     Map<String, Import> importMap = new HashMap<>();
761     Import anImport = new Import();
762     anImport.setFile("mainImport");
763     importMap.put("bla bla", anImport);
764     imports.add(importMap);
765     mainST.setImports(imports);
766
767     //create searchable service template
768     Map<String, NodeType> stNodeTypes = new HashMap<>();
769     addNodeType(stNodeTypes, ToscaNodeType.NATIVE_COMPUTE, new NodeType());
770     NodeType nodeType = createNodeType(ToscaNodeType.NATIVE_BLOCK_STORAGE);
771     addNodeType(stNodeTypes, typeToMatch, nodeType);
772     ServiceTemplate serviceTemplate = new ServiceTemplate();
773     serviceTemplate.setNode_types(stNodeTypes);
774
775     // add service templates to tosca service model
776     Map<String, ServiceTemplate> serviceTemplates = toscaServiceModelMock.getServiceTemplates();
777     serviceTemplates.put("testMainServiceTemplate", mainST);
778     serviceTemplates.put("mainImport", serviceTemplate);
779     when(toscaServiceModelMock.getServiceTemplates()).thenReturn(serviceTemplates);
780
781     assertTrue(toscaAnalyzerService
782         .isTypeOf(nodeTemplateMock, ToscaNodeType.NATIVE_BLOCK_STORAGE, mainST,
783             toscaServiceModelMock));
784   }
785
786   @Test
787   public void shouldReturnTrueIfNdTmpTypeIsFoundInSrvTmpNdTyButRequestedTypeNotMatchButFoundIn2ndLevelImports() {
788     String typeToMatch = ToscaNodeType.CINDER_VOLUME;
789     when(nodeTemplateMock.getType()).thenReturn(typeToMatch);
790     ServiceTemplate mainST = new ServiceTemplate();
791     List<Map<String, Import>> imports = new ArrayList<>();
792     Map<String, Import> importMap = new HashMap<>();
793     Import anImport = new Import();
794     anImport.setFile("refToMainImport");
795     importMap.put("bla bla", anImport);
796     imports.add(importMap);
797     mainST.setImports(imports);
798
799     //create searchable service template
800     Map<String, NodeType> stNodeTypes = new HashMap<>();
801     addNodeType(stNodeTypes, ToscaNodeType.NATIVE_COMPUTE, new NodeType());
802     NodeType nodeType = createNodeType(ToscaNodeType.NATIVE_BLOCK_STORAGE);
803     addNodeType(stNodeTypes, typeToMatch, nodeType);
804     ServiceTemplate serviceTemplate = new ServiceTemplate();
805     serviceTemplate.setNode_types(stNodeTypes);
806
807     // create 1st level service template with import only
808     ServiceTemplate firstLevelST = new ServiceTemplate();
809     List<Map<String, Import>> firstLevelImports = new ArrayList<>();
810     Map<String, Import> firstLevelImportsMap = new HashMap<>();
811     Import firstLevelImport = new Import();
812     firstLevelImport.setFile("mainImport");
813     firstLevelImportsMap.put("bla bla 2", firstLevelImport);
814     firstLevelImports.add(firstLevelImportsMap);
815     firstLevelST.setImports(firstLevelImports);
816
817     // add service templates to tosca service model
818     Map<String, ServiceTemplate> serviceTemplates = toscaServiceModelMock.getServiceTemplates();
819     serviceTemplates.put("testMainServiceTemplate", mainST);
820     serviceTemplates.put("refToMainImport", firstLevelST);
821     serviceTemplates.put("mainImport", serviceTemplate);
822     when(toscaServiceModelMock.getServiceTemplates()).thenReturn(serviceTemplates);
823
824     assertTrue(toscaAnalyzerService
825         .isTypeOf(nodeTemplateMock, ToscaNodeType.NATIVE_BLOCK_STORAGE, mainST,
826             toscaServiceModelMock));
827   }
828
829   // not found at all should throw core exception
830
831
832 }