2  * Copyright © 2016-2018 European Support Limited
 
   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
 
   8  *      http://www.apache.org/licenses/LICENSE-2.0
 
  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.
 
  17 package org.openecomp.sdc.tosca.services.impl;
 
  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.DataType;
 
  38 import org.openecomp.sdc.tosca.datatypes.model.Import;
 
  39 import org.openecomp.sdc.tosca.datatypes.model.InterfaceDefinitionType;
 
  40 import org.openecomp.sdc.tosca.datatypes.model.InterfaceType;
 
  41 import org.openecomp.sdc.tosca.datatypes.model.NodeTemplate;
 
  42 import org.openecomp.sdc.tosca.datatypes.model.NodeType;
 
  43 import org.openecomp.sdc.tosca.datatypes.model.ParameterDefinition;
 
  44 import org.openecomp.sdc.tosca.datatypes.model.PropertyDefinition;
 
  45 import org.openecomp.sdc.tosca.datatypes.model.RequirementAssignment;
 
  46 import org.openecomp.sdc.tosca.datatypes.model.RequirementDefinition;
 
  47 import org.openecomp.sdc.tosca.datatypes.model.ServiceTemplate;
 
  48 import org.openecomp.sdc.tosca.datatypes.model.SubstitutionMapping;
 
  49 import org.openecomp.sdc.tosca.datatypes.model.TopologyTemplate;
 
  50 import org.openecomp.sdc.tosca.services.ToscaAnalyzerService;
 
  51 import org.openecomp.sdc.tosca.services.ToscaConstants;
 
  52 import org.openecomp.sdc.tosca.services.ToscaExtensionYamlUtil;
 
  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;
 
  60 import java.util.Optional;
 
  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;
 
  71  * @since July 14, 2016
 
  73 @RunWith(MockitoJUnitRunner.class)
 
  74 public class ToscaAnalyzerServiceImplTest {
 
  77     SrvTmp: ServiceTemplate
 
  82   private static ToscaAnalyzerService toscaAnalyzerService;
 
  83   private static ToscaServiceModel toscaServiceModel;
 
  85   public ExpectedException thrown = ExpectedException.none();
 
  88   private NodeTemplate nodeTemplateMock;
 
  90   private ParameterDefinition parameterDefinitionMock;
 
  92   private PropertyDefinition propertyDefinitionMock;
 
  94   private InterfaceDefinitionType interfaceDefinitionMock;
 
  96   private ToscaServiceModel toscaServiceModelMock;
 
  99   public static void onlyOnceSetUp() throws IOException {
 
 100     toscaAnalyzerService = new ToscaAnalyzerServiceImpl();
 
 101     toscaServiceModel = TestUtil.loadToscaServiceModel("/mock/analyzerService/toscasubstitution/",
 
 102         "/mock/globalServiceTemplates/", null);
 
 106   public void init() throws IOException {
 
 107     MockitoAnnotations.initMocks(this);
 
 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")) {
 
 120           serviceTemplateFromYaml =
 
 121           toscaExtensionYamlUtil.yamlToObject(yamlFile, ServiceTemplate.class);
 
 124           .getFlatEntity(ToscaElementTypes.NODE_TYPE, "org.openecomp.resource.vfc.notFound",
 
 125               serviceTemplateFromYaml, toscaServiceModel);
 
 130   public void testGetFlatEntityNodeType() throws Exception {
 
 131     ToscaExtensionYamlUtil toscaExtensionYamlUtil = new ToscaExtensionYamlUtil();
 
 132     try (InputStream yamlFile = toscaExtensionYamlUtil
 
 133         .loadYamlFileIs("/mock/analyzerService/NestedServiceTemplateReqTest.yaml")) {
 
 136           serviceTemplateFromYaml =
 
 137           toscaExtensionYamlUtil.yamlToObject(yamlFile, ServiceTemplate.class);
 
 139       final NodeType flatEntity = (NodeType) toscaAnalyzerService
 
 140           .getFlatEntity(ToscaElementTypes.NODE_TYPE, "org.openecomp.resource.vfc.nodes.heat" +
 
 141               ".cmaui_image", serviceTemplateFromYaml, toscaServiceModel);
 
 143       Assert.assertNotNull(flatEntity);
 
 144       Assert.assertEquals("org.openecomp.resource.vfc.nodes.heat.nova.Server", flatEntity
 
 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"));
 
 156   public void testGetFlatEntityDataType() throws Exception {
 
 157     ToscaExtensionYamlUtil toscaExtensionYamlUtil = new ToscaExtensionYamlUtil();
 
 158     try (InputStream yamlFile = toscaExtensionYamlUtil
 
 159         .loadYamlFileIs("/mock/analyzerService/NestedServiceTemplateReqTest.yaml")) {
 
 162           serviceTemplateFromYaml =
 
 163           toscaExtensionYamlUtil.yamlToObject(yamlFile, ServiceTemplate.class);
 
 165       final DataType flatEntity = (DataType) toscaAnalyzerService
 
 166           .getFlatEntity(ToscaElementTypes.DATA_TYPE,
 
 167               "org.openecomp.datatypes.heat.network.MyNewAddressPair", serviceTemplateFromYaml,
 
 170       Assert.assertNotNull(flatEntity);
 
 171       Assert.assertEquals("org.openecomp.datatypes.heat.network.MyAddressPair", flatEntity
 
 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"));
 
 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);
 
 192     RequirementDefinition rd1 = new RequirementDefinition();
 
 193     rd.setCapability("tosca.capabilities.network.Bindable");
 
 195     rd.setRelationship("tosca.relationships.network.BindsTo");
 
 196     Object[] occurences1 = new Object[]{1, 1};
 
 197     rd1.setOccurrences(occurences1);
 
 199     nodeTypeRequirementDefinition.put("binding", rd1);
 
 200     nodeTypeRequirementDefinition.put("dependency", rd);
 
 202     Map<String, Map<String, RequirementAssignment>> fullFilledRequirementsDefinition =
 
 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);
 
 211     List<Map<String, RequirementDefinition>> nodeTypeRequirementsDefinition = new ArrayList<>();
 
 212     nodeTypeRequirementsDefinition.add(nodeTypeRequirementDefinition);
 
 214     List<Map<String, RequirementDefinition>> exposedRequirements = toscaAnalyzerService
 
 215         .calculateExposedRequirements(nodeTypeRequirementsDefinition,
 
 216             nodeTemplateRequirementsAssignment);
 
 217     Assert.assertEquals(1, exposedRequirements.size());
 
 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);
 
 230     RequirementDefinition rd1 = new RequirementDefinition();
 
 231     rd.setCapability("tosca.capabilities.network.Bindable");
 
 233     rd.setRelationship("tosca.relationships.network.BindsTo");
 
 234     Object[] occurences1 = new Object[]{1, 1};
 
 235     rd1.setOccurrences(occurences1);
 
 237     nodeTypeRequirementDefinition.put("binding", rd1);
 
 238     nodeTypeRequirementDefinition.put("dependency", rd);
 
 240     Map<String, Map<String, RequirementAssignment>> fullFilledRequirementsDefinition =
 
 242     Map<String, RequirementAssignment> nodeTemplateRequirementsAssignment = new HashMap<>();
 
 243     RequirementAssignment ra = new RequirementAssignment();
 
 244     ra.setCapability("tosca.capabilities.network.Bindable");
 
 246     ra.setRelationship("tosca.relationships.network.BindsTo");
 
 247     nodeTemplateRequirementsAssignment.put("binding", ra);
 
 249     List<Map<String, RequirementDefinition>> nodeTypeRequirementsDefinition = new ArrayList<>();
 
 250     nodeTypeRequirementsDefinition.add(nodeTypeRequirementDefinition);
 
 252     List<Map<String, RequirementDefinition>> exposedRequirements = toscaAnalyzerService
 
 253         .calculateExposedRequirements(nodeTypeRequirementsDefinition,
 
 254             nodeTemplateRequirementsAssignment);
 
 255     Assert.assertEquals(1, exposedRequirements.size());
 
 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 =
 
 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());
 
 280   public void testIsRequirementExistsWithInvalidReqId() throws Exception {
 
 281     ToscaExtensionYamlUtil toscaExtensionYamlUtil = new ToscaExtensionYamlUtil();
 
 282     try (InputStream yamlFile = toscaExtensionYamlUtil
 
 283         .loadYamlFileIs("/mock/analyzerService/NestedServiceTemplateReqTest.yaml")) {
 
 286           serviceTemplateFromYaml =
 
 287           toscaExtensionYamlUtil.yamlToObject(yamlFile, ServiceTemplate.class);
 
 289       TestUtil.createConcreteRequirementObjectsInServiceTemplate(serviceTemplateFromYaml,
 
 290           toscaExtensionYamlUtil);
 
 292       NodeTemplate port_0 =
 
 293           serviceTemplateFromYaml.getTopology_template().getNode_templates().get("cmaui_port_0");
 
 295       RequirementAssignment ra = new RequirementAssignment();
 
 296       ra.setCapability("tosca.capabilities.network.Bindable");
 
 297       ra.setNode("server_cmaui");
 
 298       ra.setRelationship("tosca.relationships.network.BindsTo");
 
 300       //Test With Empty requirementId
 
 301       Assert.assertEquals(false,
 
 302           toscaAnalyzerService.isRequirementExistInNodeTemplate(port_0, "", ra));
 
 304       //Test With valid requirementId
 
 305       Assert.assertEquals(true,
 
 306           toscaAnalyzerService.isRequirementExistInNodeTemplate(port_0, "binding", ra));
 
 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));
 
 319   public void testGetRequirements() throws Exception {
 
 320     ToscaExtensionYamlUtil toscaExtensionYamlUtil = new ToscaExtensionYamlUtil();
 
 321     try (InputStream yamlFile = toscaExtensionYamlUtil
 
 322         .loadYamlFileIs("/mock/analyzerService/NestedServiceTemplateReqTest.yaml")) {
 
 325           serviceTemplateFromYaml =
 
 326           toscaExtensionYamlUtil.yamlToObject(yamlFile, ServiceTemplate.class);
 
 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());
 
 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());
 
 341       reqList = toscaAnalyzerService.getRequirements(port_0, ToscaConstants.LINK_REQUIREMENT_ID);
 
 342       assertEquals(0, reqList.size());
 
 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());
 
 353     ServiceTemplate mainServiceTemplate = toscaServiceModel.getServiceTemplates()
 
 354         .get(toscaServiceModel.getEntryDefinitionServiceTemplate());
 
 355     nodeTemplate = toscaAnalyzerService.getNodeTemplateById(mainServiceTemplate, "test_net");
 
 356     assertEquals(true, nodeTemplate.isPresent());
 
 358     nodeTemplate = toscaAnalyzerService.getNodeTemplateById(mainServiceTemplate, "test_net222");
 
 359     assertEquals(false, nodeTemplate.isPresent());
 
 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.");
 
 368     Optional<String> substituteServiceTemplateName;
 
 370     ServiceTemplate mainServiceTemplate = toscaServiceModel.getServiceTemplates()
 
 371         .get(toscaServiceModel.getEntryDefinitionServiceTemplate());
 
 372     Optional<NodeTemplate> notSubstitutableNodeTemplate =
 
 373         toscaAnalyzerService.getNodeTemplateById(mainServiceTemplate, "test_net");
 
 374     assertEquals(true, notSubstitutableNodeTemplate.isPresent());
 
 376     if (notSubstitutableNodeTemplate.isPresent()) {
 
 377       substituteServiceTemplateName = toscaAnalyzerService
 
 378           .getSubstituteServiceTemplateName("test_net", notSubstitutableNodeTemplate.get());
 
 379       assertEquals(false, substituteServiceTemplateName.isPresent());
 
 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());
 
 392     NodeTemplate invalidSubstitutableNodeTemplate1 = new NodeTemplate();
 
 393     substituteServiceTemplateName = toscaAnalyzerService
 
 394         .getSubstituteServiceTemplateName("invalid1", invalidSubstitutableNodeTemplate1);
 
 395     assertEquals(false, substituteServiceTemplateName.isPresent());
 
 397     substitutableNodeTemplate.ifPresent(nodeTemplate -> {
 
 398       Object serviceTemplateFilter = nodeTemplate.getProperties()
 
 399           .get(ToscaConstants.SERVICE_TEMPLATE_FILTER_PROPERTY_NAME);
 
 400       ((Map) serviceTemplateFilter).clear();
 
 402           .getSubstituteServiceTemplateName("invalid2", nodeTemplate);
 
 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);
 
 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"));
 
 422       ServiceTemplate emptyServiceTemplate = new ServiceTemplate();
 
 423       emptyServiceTemplate.setTopology_template(new TopologyTemplate());
 
 424       substitutableNodeTemplates =
 
 425           toscaAnalyzerService.getSubstitutableNodeTemplates(emptyServiceTemplate);
 
 426       assertEquals(0, substitutableNodeTemplates.size());
 
 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());
 
 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);
 
 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());
 
 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());
 
 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());
 
 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());
 
 487         .getSubstitutionMappedNodeTemplateByExposedReq("invalidMappingServiceTemplate.yaml",
 
 488             invalidMappingServiceTemplate, "local_storage_server_cmaui");
 
 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());
 
 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);
 
 518           .getSubstitutionMappedNodeTemplateByExposedReq("NestedServiceTemplateSubstituteTest.yaml",
 
 519               nestedServiceTemplateFromYaml, "link_cmaui_port_invalid");
 
 524   public void testIsDesiredRequirementAssignmentMatch() throws Exception {
 
 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);
 
 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));
 
 552   public void testIsDesiredRequirementAssignmentNoMatch() throws Exception {
 
 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);
 
 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));
 
 583   public void shouldReturnFalseIfNdTmpIsNull() {
 
 584     NodeTemplate nodeTemplate = null;
 
 585     assertFalse(toscaAnalyzerService
 
 586         .isTypeOf(nodeTemplate, ToscaNodeType.NATIVE_NETWORK, new ServiceTemplate(),
 
 587             toscaServiceModelMock));
 
 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));
 
 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));
 
 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));
 
 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));
 
 637   public void dataTypeParameterExistInHierarchy() {
 
 638     String testedDataTypeKey = "test.dataType.B";
 
 639     when(parameterDefinitionMock.getType()).thenReturn(testedDataTypeKey);
 
 640     dataTypeExistInHierarchy(testedDataTypeKey, parameterDefinitionMock);
 
 645   public void dataTypePropertyExistInHierarchy() {
 
 646     String testedDataTypeKey = "test.dataType.B";
 
 647     when(propertyDefinitionMock.getType()).thenReturn(testedDataTypeKey);
 
 648     dataTypeExistInHierarchy(testedDataTypeKey, propertyDefinitionMock);
 
 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));
 
 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));
 
 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);
 
 692         .isTypeOf(nodeTemplateMock, ToscaNodeType.NATIVE_COMPUTE, serviceTemplate,
 
 693             toscaServiceModelMock);
 
 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 " +
 
 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));
 
 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));
 
 728   private NodeType createNodeType(String derivedFrom) {
 
 729     NodeType nodeType = new NodeType();
 
 730     nodeType.setDerived_from(derivedFrom);
 
 734   private DataType createDataType(String derivedFrom) {
 
 735     DataType dataType = new DataType();
 
 736     dataType.setDerived_from(derivedFrom);
 
 740   private InterfaceType createInterfaceType(String derivedFrom) {
 
 741     InterfaceType interfaceType = new InterfaceType();
 
 742     interfaceType.setDerived_from(derivedFrom);
 
 743     return interfaceType;
 
 746   private void addNodeType(Map<String, NodeType> stNodeTypes, String key, NodeType nodeType) {
 
 747     stNodeTypes.put(key, nodeType);
 
 750   private void addDataType(Map<String, DataType> stDataTypes, String key, DataType dataType) {
 
 751     stDataTypes.put(key, dataType);
 
 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);
 
 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);
 
 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);
 
 781     assertTrue(toscaAnalyzerService
 
 782         .isTypeOf(nodeTemplateMock, ToscaNodeType.NATIVE_BLOCK_STORAGE, mainST,
 
 783             toscaServiceModelMock));
 
 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);
 
 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);
 
 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);
 
 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);
 
 824     assertTrue(toscaAnalyzerService
 
 825         .isTypeOf(nodeTemplateMock, ToscaNodeType.NATIVE_BLOCK_STORAGE, mainST,
 
 826             toscaServiceModelMock));
 
 829   // not found at all should throw core exception