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