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