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;
20 import org.junit.rules.ExpectedException;
21 import org.junit.runner.RunWith;
22 import org.mockito.Mock;
23 import org.mockito.MockitoAnnotations;
24 import org.mockito.runners.MockitoJUnitRunner;
25 import org.onap.sdc.tosca.datatypes.model.*;
26 import org.onap.sdc.tosca.services.ToscaExtensionYamlUtil;
27 import org.openecomp.sdc.common.errors.CoreException;
28 import org.openecomp.sdc.tosca.TestUtil;
29 import org.openecomp.sdc.tosca.datatypes.ToscaElementTypes;
30 import org.openecomp.sdc.tosca.datatypes.ToscaFlatData;
31 import org.openecomp.sdc.tosca.datatypes.ToscaNodeType;
32 import org.openecomp.sdc.tosca.datatypes.ToscaServiceModel;
33 import org.openecomp.sdc.tosca.services.DataModelUtil;
34 import org.openecomp.sdc.tosca.services.ToscaAnalyzerService;
35 import org.openecomp.sdc.tosca.services.ToscaConstants;
37 import java.io.IOException;
38 import java.io.InputStream;
41 import static org.junit.Assert.*;
42 import static org.mockito.Mockito.when;
45 @RunWith(MockitoJUnitRunner.class)
46 public class ToscaAnalyzerServiceImplTest {
48 public static final String CAPABILITY_TYPE_A = "capabilityTypeA";
49 public static final String CAPABILITY_TYPE_B = "capabilityTypeB";
50 public static final String TOSCA_CAPABILITIES_ROOT = "tosca.capabilities.Root";
54 SrvTmp: ServiceTemplate
59 private static ToscaAnalyzerService toscaAnalyzerService;
60 private static ToscaServiceModel toscaServiceModel;
62 public ExpectedException thrown = ExpectedException.none();
65 private NodeTemplate nodeTemplateMock;
67 private ParameterDefinition parameterDefinitionMock;
69 private PropertyDefinition propertyDefinitionMock;
71 private InterfaceDefinitionType interfaceDefinitionMock;
73 private ToscaServiceModel toscaServiceModelMock;
76 public static void onlyOnceSetUp() throws IOException {
77 toscaAnalyzerService = new ToscaAnalyzerServiceImpl();
78 toscaServiceModel = TestUtil.loadToscaServiceModel("/mock/analyzerService/toscasubstitution/",
79 "/mock/globalServiceTemplates/", null);
83 public void init() throws IOException {
84 MockitoAnnotations.initMocks(this);
88 public void testGetFlatEntityNotFound() throws Exception {
89 thrown.expect(CoreException.class);
91 "Entity Type 'org.openecomp.resource.vfc.notFound' or one of its derivedFrom type hierarchy, is not defined in tosca service model");
92 ToscaExtensionYamlUtil toscaExtensionYamlUtil = new ToscaExtensionYamlUtil();
93 try (InputStream yamlFile = toscaExtensionYamlUtil.loadYamlFileIs(
94 "/mock/analyzerService/NestedServiceTemplateReqTest.yaml")) {
96 ServiceTemplate serviceTemplateFromYaml =
97 toscaExtensionYamlUtil.yamlToObject(yamlFile, ServiceTemplate.class);
99 toscaAnalyzerService.getFlatEntity(ToscaElementTypes.NODE_TYPE, "org.openecomp.resource.vfc.notFound",
100 serviceTemplateFromYaml, toscaServiceModel);
105 public void testGetFlatEntityFileNotFound() throws Exception {
106 thrown.expect(CoreException.class);
107 thrown.expectMessage("Tosca file 'missingFile.yaml' was not found in tosca service model");
108 ToscaExtensionYamlUtil toscaExtensionYamlUtil = new ToscaExtensionYamlUtil();
109 try (InputStream yamlFile = toscaExtensionYamlUtil.loadYamlFileIs(
110 "/mock/analyzerService/ServiceTemplateFileNotFoundTest.yaml")) {
112 ServiceTemplate serviceTemplateFromYaml =
113 toscaExtensionYamlUtil.yamlToObject(yamlFile, ServiceTemplate.class);
116 .getFlatEntity(ToscaElementTypes.NODE_TYPE, "org.openecomp.resource.vfc.nodes.heat.cmaui_image",
117 serviceTemplateFromYaml, toscaServiceModel);
122 public void testGetFlatEntityNodeType() throws Exception {
123 ToscaExtensionYamlUtil toscaExtensionYamlUtil = new ToscaExtensionYamlUtil();
124 try (InputStream yamlFile = toscaExtensionYamlUtil.loadYamlFileIs(
125 "/mock/analyzerService/NestedServiceTemplateReqTest.yaml")) {
127 ServiceTemplate serviceTemplateFromYaml =
128 toscaExtensionYamlUtil.yamlToObject(yamlFile, ServiceTemplate.class);
130 ToscaFlatData flatData = toscaAnalyzerService.getFlatEntity(ToscaElementTypes.NODE_TYPE,
131 "org.openecomp.resource.vfc.nodes.heat" + ".cmaui_image", serviceTemplateFromYaml,
134 Assert.assertNotNull(flatData);
135 checkNodeTypeFlatEntity(flatData);
136 checkNodeTypeInheritanceHierarchy(flatData);
140 private void checkNodeTypeInheritanceHierarchy(ToscaFlatData flatData) {
141 List<String> inheritanceHierarchyType = flatData.getInheritanceHierarchyType();
142 Assert.assertNotNull(inheritanceHierarchyType);
143 Assert.assertEquals(4, inheritanceHierarchyType.size());
144 Assert.assertEquals(true,
145 inheritanceHierarchyType.contains("org.openecomp.resource.vfc.nodes.heat.cmaui_image"));
146 Assert.assertEquals(true,
147 inheritanceHierarchyType.contains("org.openecomp.resource.vfc.nodes.heat.nova.Server"));
148 Assert.assertEquals(true, inheritanceHierarchyType.contains("tosca.nodes.Compute"));
149 Assert.assertEquals(true, inheritanceHierarchyType.contains("tosca.nodes.Root"));
152 private void checkNodeTypeFlatEntity(ToscaFlatData flatData) {
153 Assert.assertNotNull(flatData.getFlatEntity());
154 NodeType flatEntity = (NodeType) flatData.getFlatEntity();
155 Assert.assertEquals("org.openecomp.resource.vfc.nodes.heat.nova.Server", flatEntity.getDerived_from());
156 Assert.assertEquals(20, flatEntity.getProperties().size());
157 Assert.assertEquals("overridden default value", flatEntity.getProperties().get("admin_pass").get_default());
158 Assert.assertEquals("REBUILD", flatEntity.getProperties().get("image_update_policy").get_default());
159 Assert.assertNotNull(flatEntity.getProperties().get("new_property"));
163 public void testGetFlatEntityNodeTypeInterface() throws Exception {
164 ToscaExtensionYamlUtil toscaExtensionYamlUtil = new ToscaExtensionYamlUtil();
165 try (InputStream yamlFile = toscaExtensionYamlUtil.loadYamlFileIs(
166 "/mock/analyzerService/ServiceTemplateInterfaceInheritanceTest.yaml")) {
168 ServiceTemplate serviceTemplateFromYaml =
169 toscaExtensionYamlUtil.yamlToObject(yamlFile, ServiceTemplate.class);
171 ToscaFlatData flatData = toscaAnalyzerService.getFlatEntity(ToscaElementTypes.NODE_TYPE,
172 "org.openecomp.resource.vfc.nodes.heat.cmaui_image_extend", serviceTemplateFromYaml,
175 Assert.assertNotNull(flatData);
176 Assert.assertNotNull(flatData.getFlatEntity());
177 NodeType flatEntity = (NodeType) flatData.getFlatEntity();
178 Assert.assertNotNull(flatEntity.getInterfaces());
179 Object standardInterfaceObj = flatEntity.getInterfaces().get("Standard");
180 Assert.assertNotNull(standardInterfaceObj);
181 Optional<InterfaceDefinitionType> standardInterface = DataModelUtil
182 .convertObjToInterfaceDefinition("Standard",
183 standardInterfaceObj,
184 InterfaceDefinitionType.class);
185 Assert.assertEquals(true, standardInterface.isPresent());
186 Assert.assertEquals(2, standardInterface.get().getInputs().size());
187 Assert.assertEquals(3, standardInterface.get().getOperations().size());
188 OperationDefinitionType createOperation = toscaExtensionYamlUtil.yamlToObject(
189 toscaExtensionYamlUtil.objectToYaml(standardInterface.get().getOperations().get("create")),
190 OperationDefinitionType.class);
191 Assert.assertEquals(2, createOperation.getInputs().size());
193 List<String> inheritanceHierarchyType = flatData.getInheritanceHierarchyType();
194 Assert.assertNotNull(inheritanceHierarchyType);
195 Assert.assertEquals(5, inheritanceHierarchyType.size());
201 public void testGetFlatEntityDataType() throws Exception {
202 ToscaExtensionYamlUtil toscaExtensionYamlUtil = new ToscaExtensionYamlUtil();
203 try (InputStream yamlFile = toscaExtensionYamlUtil.loadYamlFileIs(
204 "/mock/analyzerService/NestedServiceTemplateReqTest.yaml")) {
206 ServiceTemplate serviceTemplateFromYaml =
207 toscaExtensionYamlUtil.yamlToObject(yamlFile, ServiceTemplate.class);
209 ToscaFlatData flatData = toscaAnalyzerService.getFlatEntity(ToscaElementTypes.DATA_TYPE,
210 "org.openecomp.datatypes.heat.network.MyNewAddressPair", serviceTemplateFromYaml,
213 Assert.assertNotNull(flatData);
214 Assert.assertNotNull(flatData.getFlatEntity());
215 DataType flatEntity = (DataType) flatData.getFlatEntity();
216 Assert.assertEquals("org.openecomp.datatypes.heat.network.MyAddressPair", flatEntity.getDerived_from());
217 Assert.assertEquals(3, flatEntity.getProperties().size());
218 Assert.assertEquals("overridden default value",
219 flatEntity.getProperties().get("mac_address").get_default());
220 Assert.assertEquals(true, flatEntity.getProperties().get("mac_address").getRequired());
221 Assert.assertNotNull(flatEntity.getProperties().get("new_property"));
223 List<String> inheritanceHierarchyType = flatData.getInheritanceHierarchyType();
224 Assert.assertNotNull(inheritanceHierarchyType);
225 Assert.assertEquals(4, inheritanceHierarchyType.size());
230 public void testGetFlatEntityDataTypeDerivedFromPrimitive() throws Exception {
231 ToscaExtensionYamlUtil toscaExtensionYamlUtil = new ToscaExtensionYamlUtil();
232 try (InputStream yamlFile = toscaExtensionYamlUtil.loadYamlFileIs(
233 "/mock/analyzerService/ServiceTemplateDatatypeFlatTest.yaml")) {
235 ServiceTemplate serviceTemplateFromYaml =
236 toscaExtensionYamlUtil.yamlToObject(yamlFile, ServiceTemplate.class);
238 ToscaFlatData flatData = toscaAnalyzerService.getFlatEntity(ToscaElementTypes.DATA_TYPE,
239 "org.openecomp.datatypes.heat.network.MyNewString", serviceTemplateFromYaml, toscaServiceModel);
241 Assert.assertNotNull(flatData);
242 Assert.assertNotNull(flatData.getFlatEntity());
243 DataType flatEntity = (DataType) flatData.getFlatEntity();
244 Assert.assertEquals("org.openecomp.datatypes.heat.network.MyString", flatEntity.getDerived_from());
245 Assert.assertEquals(2, flatEntity.getConstraints().size());
246 Assert.assertNotNull(flatEntity.getConstraints().get(0).getValid_values());
247 Assert.assertNotNull(flatEntity.getConstraints().get(1).getMax_length());
249 List<String> inheritanceHierarchyType = flatData.getInheritanceHierarchyType();
250 Assert.assertNotNull(inheritanceHierarchyType);
251 Assert.assertEquals(2, inheritanceHierarchyType.size());
256 public void testCalculateExposedRequirements() throws Exception {
257 RequirementDefinition rd = new RequirementDefinition();
258 rd.setCapability("tosca.capabilities.Node");
259 rd.setNode("tosca.nodes.Root");
260 rd.setRelationship("tosca.relationships.DependsOn");
261 Object[] occurences = new Object[] {0, "UNBOUNDED"};
262 rd.setOccurrences(occurences);
264 RequirementDefinition rd1 = new RequirementDefinition();
265 rd.setCapability("tosca.capabilities.network.Bindable");
267 rd.setRelationship("tosca.relationships.network.BindsTo");
268 Object[] occurences1 = new Object[] {1, 1};
269 rd1.setOccurrences(occurences1);
271 Map<String, RequirementDefinition> nodeTypeRequirementDefinition = new HashMap<>();
272 nodeTypeRequirementDefinition.put("binding", rd1);
273 nodeTypeRequirementDefinition.put("dependency", rd);
275 Map<String, Map<String, RequirementAssignment>> fullFilledRequirementsDefinition = new HashMap<>();
276 RequirementAssignment ra = new RequirementAssignment();
277 ra.setCapability("tosca.capabilities.network.Bindable");
278 ra.setNode("pd_server");
279 ra.setRelationship("tosca.relationships.network.BindsTo");
280 Map<String, RequirementAssignment> nodeTemplateRequirementsAssignment = new HashMap<>();
281 nodeTemplateRequirementsAssignment.put("binding", ra);
283 List<Map<String, RequirementDefinition>> nodeTypeRequirementsDefinition = new ArrayList<>();
284 nodeTypeRequirementsDefinition.add(nodeTypeRequirementDefinition);
286 List<Map<String, RequirementDefinition>> exposedRequirements = toscaAnalyzerService
287 .calculateExposedRequirements(
288 nodeTypeRequirementsDefinition,
289 nodeTemplateRequirementsAssignment);
290 Assert.assertEquals(1, exposedRequirements.size());
294 public void testCalExpReqWithNullNodeInReqAssignment() throws Exception {
295 RequirementDefinition rd = new RequirementDefinition();
296 rd.setCapability("tosca.capabilities.Node");
297 rd.setNode("tosca.nodes.Root");
298 rd.setRelationship("tosca.relationships.DependsOn");
299 Object[] occurences = new Object[] {0, "UNBOUNDED"};
300 rd.setOccurrences(occurences);
302 RequirementDefinition rd1 = new RequirementDefinition();
303 rd.setCapability("tosca.capabilities.network.Bindable");
305 rd.setRelationship("tosca.relationships.network.BindsTo");
306 Object[] occurences1 = new Object[] {1, 1};
307 rd1.setOccurrences(occurences1);
309 Map<String, RequirementDefinition> nodeTypeRequirementDefinition = new HashMap<>();
310 nodeTypeRequirementDefinition.put("binding", rd1);
311 nodeTypeRequirementDefinition.put("dependency", rd);
313 Map<String, Map<String, RequirementAssignment>> fullFilledRequirementsDefinition = new HashMap<>();
314 RequirementAssignment ra = new RequirementAssignment();
315 ra.setCapability("tosca.capabilities.network.Bindable");
317 ra.setRelationship("tosca.relationships.network.BindsTo");
318 Map<String, RequirementAssignment> nodeTemplateRequirementsAssignment = new HashMap<>();
319 nodeTemplateRequirementsAssignment.put("binding", ra);
321 List<Map<String, RequirementDefinition>> nodeTypeRequirementsDefinition = new ArrayList<>();
322 nodeTypeRequirementsDefinition.add(nodeTypeRequirementDefinition);
324 List<Map<String, RequirementDefinition>> exposedRequirements = toscaAnalyzerService
325 .calculateExposedRequirements(
326 nodeTypeRequirementsDefinition,
327 nodeTemplateRequirementsAssignment);
328 Assert.assertEquals(1, exposedRequirements.size());
332 public void testCalculateExposedCapabilities() throws Exception {
333 Map<String, CapabilityDefinition> nodeTypeCapabilitiesDefinition = new HashMap<>();
334 CapabilityDefinition cd = new CapabilityDefinition();
335 cd.setType("tosca.capabilities.Scalable");
336 nodeTypeCapabilitiesDefinition.put("tosca.capabilities.network.Bindable_pd_server", cd);
337 Map<String, RequirementAssignment> nodeTemplateRequirementsAssignment = new HashMap<>();
338 RequirementAssignment ra = new RequirementAssignment();
339 ra.setCapability("tosca.capabilities.network.Bindable");
340 ra.setNode("pd_server");
341 ra.setRelationship("tosca.relationships.network.BindsTo");
342 nodeTemplateRequirementsAssignment.put("binding", ra);
343 Map<String, Map<String, RequirementAssignment>> fullFilledRequirementsDefinition = new HashMap<>();
344 fullFilledRequirementsDefinition.put("pd_server", nodeTemplateRequirementsAssignment);
345 Map<String, CapabilityDefinition> exposedCapabilities = toscaAnalyzerService.calculateExposedCapabilities(
346 nodeTypeCapabilitiesDefinition, fullFilledRequirementsDefinition);
347 Assert.assertEquals(1, exposedCapabilities.size());
351 public void testIsRequirementExistsWithInvalidReqId() throws Exception {
352 ToscaExtensionYamlUtil toscaExtensionYamlUtil = new ToscaExtensionYamlUtil();
353 try (InputStream yamlFile = toscaExtensionYamlUtil.loadYamlFileIs(
354 "/mock/analyzerService/NestedServiceTemplateReqTest.yaml")) {
356 ServiceTemplate serviceTemplateFromYaml =
357 toscaExtensionYamlUtil.yamlToObject(yamlFile, ServiceTemplate.class);
359 TestUtil.createConcreteRequirementObjectsInServiceTemplate(serviceTemplateFromYaml, toscaExtensionYamlUtil);
362 RequirementAssignment ra = new RequirementAssignment();
363 ra.setCapability("tosca.capabilities.network.Bindable");
364 ra.setNode("server_cmaui");
365 ra.setRelationship("tosca.relationships.network.BindsTo");
367 NodeTemplate port0 = serviceTemplateFromYaml.getTopology_template().getNode_templates().get("cmaui_port_0");
368 //Test With Empty requirementId
369 Assert.assertEquals(false, toscaAnalyzerService.isRequirementExistInNodeTemplate(port0, "", ra));
371 //Test With valid requirementId
372 Assert.assertEquals(true, toscaAnalyzerService.isRequirementExistInNodeTemplate(port0, "binding", ra));
374 //Test With invalid requirement assignment
375 RequirementAssignment ra1 = new RequirementAssignment();
376 ra1.setCapability("tosca.capabilities.network.Bindable1");
377 ra1.setNode("server_cmaui1");
378 ra1.setRelationship("tosca.relationships.network.BindsTo1");
379 Assert.assertEquals(false, toscaAnalyzerService.isRequirementExistInNodeTemplate(port0, "binding", ra1));
384 public void testGetRequirements() throws Exception {
385 ToscaExtensionYamlUtil toscaExtensionYamlUtil = new ToscaExtensionYamlUtil();
386 try (InputStream yamlFile = toscaExtensionYamlUtil.loadYamlFileIs(
387 "/mock/analyzerService/NestedServiceTemplateReqTest.yaml")) {
389 ServiceTemplate serviceTemplateFromYaml =
390 toscaExtensionYamlUtil.yamlToObject(yamlFile, ServiceTemplate.class);
392 NodeTemplate port0 = serviceTemplateFromYaml.getTopology_template().getNode_templates().get("cmaui_port_0");
393 List<RequirementAssignment> reqList =
394 toscaAnalyzerService.getRequirements(port0, ToscaConstants.BINDING_REQUIREMENT_ID);
395 assertEquals(1, reqList.size());
399 serviceTemplateFromYaml.getTopology_template().getNode_templates().get("cmaui1_port_1");
400 reqList = toscaAnalyzerService.getRequirements(port1, ToscaConstants.LINK_REQUIREMENT_ID);
401 assertEquals(2, reqList.size());
404 reqList = toscaAnalyzerService.getRequirements(port0, ToscaConstants.LINK_REQUIREMENT_ID);
405 assertEquals(0, reqList.size());
410 public void testGetNodeTemplateById() throws Exception {
411 ServiceTemplate emptyServiceTemplate = new ServiceTemplate();
412 Optional<NodeTemplate> nodeTemplate =
413 toscaAnalyzerService.getNodeTemplateById(emptyServiceTemplate, "test_net222");
414 assertEquals(false, nodeTemplate.isPresent());
416 ServiceTemplate mainServiceTemplate =
417 toscaServiceModel.getServiceTemplates().get(toscaServiceModel.getEntryDefinitionServiceTemplate());
418 nodeTemplate = toscaAnalyzerService.getNodeTemplateById(mainServiceTemplate, "test_net");
419 assertEquals(true, nodeTemplate.isPresent());
421 nodeTemplate = toscaAnalyzerService.getNodeTemplateById(mainServiceTemplate, "test_net222");
422 assertEquals(false, nodeTemplate.isPresent());
426 public void testGetSubstituteServiceTemplateName() throws Exception {
427 thrown.expect(CoreException.class);
428 thrown.expectMessage(
429 "Invalid Substitute Node Template invalid2, mandatory map property service_template_filter with mandatory key substitute_service_template must be defined.");
431 Optional<String> substituteServiceTemplateName;
433 ServiceTemplate mainServiceTemplate =
434 toscaServiceModel.getServiceTemplates().get(toscaServiceModel.getEntryDefinitionServiceTemplate());
435 Optional<NodeTemplate> notSubstitutableNodeTemplate =
436 toscaAnalyzerService.getNodeTemplateById(mainServiceTemplate, "test_net");
437 assertEquals(true, notSubstitutableNodeTemplate.isPresent());
439 if (notSubstitutableNodeTemplate.isPresent()) {
440 substituteServiceTemplateName = toscaAnalyzerService.getSubstituteServiceTemplateName("test_net",
441 notSubstitutableNodeTemplate.get());
442 assertEquals(false, substituteServiceTemplateName.isPresent());
445 Optional<NodeTemplate> substitutableNodeTemplate =
446 toscaAnalyzerService.getNodeTemplateById(mainServiceTemplate, "test_nested");
447 assertEquals(true, substitutableNodeTemplate.isPresent());
448 if (substitutableNodeTemplate.isPresent()) {
449 substituteServiceTemplateName = toscaAnalyzerService.getSubstituteServiceTemplateName("test_nested",
450 substitutableNodeTemplate.get());
451 assertEquals(true, substituteServiceTemplateName.isPresent());
452 assertEquals("nestedServiceTemplate.yaml", substituteServiceTemplateName.get());
455 NodeTemplate invalidSubstitutableNodeTemplate1 = new NodeTemplate();
456 substituteServiceTemplateName =
457 toscaAnalyzerService.getSubstituteServiceTemplateName("invalid1", invalidSubstitutableNodeTemplate1);
458 assertEquals(false, substituteServiceTemplateName.isPresent());
460 substitutableNodeTemplate.ifPresent(nodeTemplate -> {
461 Object serviceTemplateFilter =
462 nodeTemplate.getProperties().get(ToscaConstants.SERVICE_TEMPLATE_FILTER_PROPERTY_NAME);
463 ((Map) serviceTemplateFilter).clear();
464 toscaAnalyzerService.getSubstituteServiceTemplateName("invalid2", nodeTemplate);
471 public void testGetSubstitutableNodeTemplates() throws Exception {
472 ToscaExtensionYamlUtil toscaExtensionYamlUtil = new ToscaExtensionYamlUtil();
473 try (InputStream yamlFile = toscaExtensionYamlUtil.loadYamlFileIs(
474 "/mock/analyzerService/ServiceTemplateSubstituteTest.yaml")) {
475 ServiceTemplate serviceTemplateFromYaml =
476 toscaExtensionYamlUtil.yamlToObject(yamlFile, ServiceTemplate.class);
478 Map<String, NodeTemplate> substitutableNodeTemplates =
479 toscaAnalyzerService.getSubstitutableNodeTemplates(serviceTemplateFromYaml);
480 assertEquals(2, substitutableNodeTemplates.size());
481 assertNotNull(substitutableNodeTemplates.get("test_nested1"));
482 assertNotNull(substitutableNodeTemplates.get("test_nested2"));
484 ServiceTemplate emptyServiceTemplate = new ServiceTemplate();
485 emptyServiceTemplate.setTopology_template(new TopologyTemplate());
486 substitutableNodeTemplates = toscaAnalyzerService.getSubstitutableNodeTemplates(emptyServiceTemplate);
487 assertEquals(0, substitutableNodeTemplates.size());
490 try (InputStream yamlFile = toscaExtensionYamlUtil.loadYamlFileIs(
491 "/mock/analyzerService/NestedServiceTemplateReqTest.yaml")) {
492 ServiceTemplate serviceTemplateFromYaml =
493 toscaExtensionYamlUtil.yamlToObject(yamlFile, ServiceTemplate.class);
494 Map<String, NodeTemplate> substitutableNodeTemplates =
495 toscaAnalyzerService.getSubstitutableNodeTemplates(serviceTemplateFromYaml);
496 assertEquals(0, substitutableNodeTemplates.size());
501 public void testGetSubstitutionMappedNodeTemplateByExposedReq() throws Exception {
502 thrown.expect(CoreException.class);
503 thrown.expectMessage(
504 "Invalid Tosca model data, missing 'Node Template' entry for 'Node Template' id cmaui_port_9");
505 ToscaExtensionYamlUtil toscaExtensionYamlUtil = new ToscaExtensionYamlUtil();
506 try (InputStream yamlFile = toscaExtensionYamlUtil.loadYamlFileIs(
507 "/mock/analyzerService/NestedServiceTemplateReqTest.yaml")) {
508 ServiceTemplate nestedServiceTemplateFromYaml =
509 toscaExtensionYamlUtil.yamlToObject(yamlFile, ServiceTemplate.class);
511 Optional<Map.Entry<String, NodeTemplate>> mappedNodeTemplate = toscaAnalyzerService
512 .getSubstitutionMappedNodeTemplateByExposedReq(
513 "NestedServiceTemplateSubstituteTest.yaml",
514 nestedServiceTemplateFromYaml,
515 "local_storage_server_cmaui");
516 assertEquals(true, mappedNodeTemplate.isPresent());
517 mappedNodeTemplate.ifPresent(stringNodeTemplateEntry -> {
518 assertEquals("server_cmaui", stringNodeTemplateEntry.getKey());
519 assertNotNull(stringNodeTemplateEntry.getValue());
522 mappedNodeTemplate = toscaAnalyzerService.getSubstitutionMappedNodeTemplateByExposedReq(
523 "NestedServiceTemplateSubstituteTest.yaml", nestedServiceTemplateFromYaml,
524 "link_cmaui_port_invalid");
525 assertEquals(true, mappedNodeTemplate.isPresent());
526 mappedNodeTemplate.ifPresent(stringNodeTemplateEntry -> {
527 assertEquals("server_cmaui", stringNodeTemplateEntry.getKey());
528 assertNotNull(stringNodeTemplateEntry.getValue());
531 ServiceTemplate mainServiceTemplate =
532 toscaServiceModel.getServiceTemplates().get(toscaServiceModel.getEntryDefinitionServiceTemplate());
533 mappedNodeTemplate = toscaAnalyzerService.getSubstitutionMappedNodeTemplateByExposedReq(
534 toscaServiceModel.getEntryDefinitionServiceTemplate(), mainServiceTemplate,
535 "local_storage_server_cmaui");
536 assertEquals(false, mappedNodeTemplate.isPresent());
541 public void invalidSubstitutableMapping() throws Exception {
542 thrown.expect(CoreException.class);
543 thrown.expectMessage(
544 "Invalid Substitution Service Template invalidMappingServiceTemplate.yaml, missing mandatory file 'Node type' in substitution mapping.");
545 ServiceTemplate invalidMappingServiceTemplate = new ServiceTemplate();
546 invalidMappingServiceTemplate.setTopology_template(new TopologyTemplate());
547 invalidMappingServiceTemplate.getTopology_template().setSubstitution_mappings(new SubstitutionMapping());
548 toscaAnalyzerService.getSubstitutionMappedNodeTemplateByExposedReq("invalidMappingServiceTemplate.yaml",
549 invalidMappingServiceTemplate, "local_storage_server_cmaui");
553 public void substitutableMappingWithNoReqMap() throws Exception {
554 ServiceTemplate emptyReqMapping = new ServiceTemplate();
555 emptyReqMapping.setTopology_template(new TopologyTemplate());
556 emptyReqMapping.getTopology_template().setSubstitution_mappings(new SubstitutionMapping());
557 emptyReqMapping.getTopology_template().getSubstitution_mappings().setNode_type("temp");
558 ServiceTemplate mainServiceTemplate =
559 toscaServiceModel.getServiceTemplates().get(toscaServiceModel.getEntryDefinitionServiceTemplate());
560 Optional<Map.Entry<String, NodeTemplate>> mappedNodeTemplate = toscaAnalyzerService
561 .getSubstitutionMappedNodeTemplateByExposedReq(
563 .getEntryDefinitionServiceTemplate(),
565 "local_storage_server_cmaui");
566 assertEquals(false, mappedNodeTemplate.isPresent());
570 public void testGetSubstitutionMappedNodeTemplateByExposedReqInvalid() throws Exception {
571 thrown.expect(CoreException.class);
572 thrown.expectMessage(
573 "Invalid Tosca model data, missing 'Node Template' entry for 'Node Template' id cmaui_port_9");
574 ToscaExtensionYamlUtil toscaExtensionYamlUtil = new ToscaExtensionYamlUtil();
575 try (InputStream yamlFile = toscaExtensionYamlUtil.loadYamlFileIs(
576 "/mock/analyzerService/NestedServiceTemplateReqTest.yaml")) {
577 ServiceTemplate nestedServiceTemplateFromYaml =
578 toscaExtensionYamlUtil.yamlToObject(yamlFile, ServiceTemplate.class);
581 .getSubstitutionMappedNodeTemplateByExposedReq("NestedServiceTemplateSubstituteTest.yaml",
582 nestedServiceTemplateFromYaml, "link_cmaui_port_invalid");
587 public void testIsDesiredRequirementAssignmentMatch() throws Exception {
589 RequirementAssignment requirementAssignment = new RequirementAssignment();
590 String capability = "Test.Capability";
591 String node = "Test.node";
592 String relationship = "Test.relationship";
593 requirementAssignment.setCapability(capability);
594 requirementAssignment.setNode(node);
595 requirementAssignment.setRelationship(relationship);
597 assertEquals(true, toscaAnalyzerService.isDesiredRequirementAssignment(requirementAssignment, capability, node,
600 toscaAnalyzerService.isDesiredRequirementAssignment(requirementAssignment, null, node, relationship));
601 assertEquals(true, toscaAnalyzerService.isDesiredRequirementAssignment(requirementAssignment, capability, null,
604 toscaAnalyzerService.isDesiredRequirementAssignment(requirementAssignment, capability, node, null));
606 toscaAnalyzerService.isDesiredRequirementAssignment(requirementAssignment, null, null, relationship));
608 toscaAnalyzerService.isDesiredRequirementAssignment(requirementAssignment, capability, null, null));
610 toscaAnalyzerService.isDesiredRequirementAssignment(requirementAssignment, null, node, null));
615 public void testIsDesiredRequirementAssignmentNoMatch() throws Exception {
617 RequirementAssignment requirementAssignment = new RequirementAssignment();
618 String capability = "Test.Capability";
619 String node = "Test.node";
620 String relationship = "Test.relationship";
621 requirementAssignment.setCapability(capability);
622 requirementAssignment.setNode(node);
623 requirementAssignment.setRelationship(relationship);
626 toscaAnalyzerService.isDesiredRequirementAssignment(requirementAssignment, "no", node, relationship));
628 toscaAnalyzerService.isDesiredRequirementAssignment(requirementAssignment, "no", "no", relationship));
630 toscaAnalyzerService.isDesiredRequirementAssignment(requirementAssignment, "no", "no", "no"));
631 assertEquals(false, toscaAnalyzerService.isDesiredRequirementAssignment(requirementAssignment, capability, "no",
634 toscaAnalyzerService.isDesiredRequirementAssignment(requirementAssignment, capability, node, "no"));
636 toscaAnalyzerService.isDesiredRequirementAssignment(requirementAssignment, capability, "no", "no"));
638 toscaAnalyzerService.isDesiredRequirementAssignment(requirementAssignment, "no", null, null));
640 toscaAnalyzerService.isDesiredRequirementAssignment(requirementAssignment, null, null, null));
646 public void shouldReturnFalseIfNdTmpIsNull() {
647 NodeTemplate nodeTemplate = null;
648 assertFalse(toscaAnalyzerService.isTypeOf(nodeTemplate, ToscaNodeType.NATIVE_NETWORK, new ServiceTemplate(),
649 toscaServiceModelMock));
653 public void shouldReturnTrueIfNdTmpTypeIsOfRequestedType() {
654 NodeTemplate nodeTemplate = new NodeTemplate();
655 String nodeTypeToSearch = ToscaNodeType.NATIVE_BLOCK_STORAGE;
656 nodeTemplate.setType(nodeTypeToSearch);
657 assertTrue(toscaAnalyzerService
658 .isTypeOf(nodeTemplate, nodeTypeToSearch, new ServiceTemplate(), toscaServiceModelMock));
662 public void shouldReturnTrueIfDataTypeIsOfRequestedType() {
663 PropertyDefinition propertyDefinition = new PropertyDefinition();
664 String propertyTypeToSearch = "tosca.datatypes.TimeInterval";
665 propertyDefinition.setType(propertyTypeToSearch);
666 assertTrue(toscaAnalyzerService.isTypeOf(propertyDefinition, propertyTypeToSearch, new ServiceTemplate(),
667 toscaServiceModelMock));
671 public void shouldReturnTrueIfInterfaceTypeIsOfRequestedType() {
672 InterfaceDefinitionType interfaceDefinition = new InterfaceDefinitionType();
673 String interfaceTypeToSearch = "test.interface.A";
674 interfaceDefinition.setType(interfaceTypeToSearch);
675 assertTrue(toscaAnalyzerService.isTypeOf(interfaceDefinition, interfaceTypeToSearch, new ServiceTemplate(),
676 toscaServiceModelMock));
680 public void shouldReturnTrueIfNdTmpTypeIsFoundInSrvTmpNdTyAndNdTyDerivedFromRequestedType() {
681 String typeToMatch = ToscaNodeType.CINDER_VOLUME;
682 when(nodeTemplateMock.getType()).thenReturn(typeToMatch);
683 Map<String, NodeType> stNodeTypes = new HashMap<>();
684 addNodeType(stNodeTypes, ToscaNodeType.NATIVE_COMPUTE, new NodeType());
685 NodeType nodeType = createNodeType(ToscaNodeType.NATIVE_BLOCK_STORAGE);
686 addNodeType(stNodeTypes, typeToMatch, nodeType);
687 ServiceTemplate serviceTemplate = new ServiceTemplate();
688 serviceTemplate.setNode_types(stNodeTypes);
689 assertTrue(toscaAnalyzerService.isTypeOf(nodeTemplateMock, ToscaNodeType.NATIVE_BLOCK_STORAGE, serviceTemplate,
690 toscaServiceModelMock));
695 public void dataTypeParameterExistInHierarchy() {
696 String testedDataTypeKey = "test.dataType.B";
697 when(parameterDefinitionMock.getType()).thenReturn(testedDataTypeKey);
698 dataTypeExistInHierarchy(testedDataTypeKey, parameterDefinitionMock);
703 public void dataTypePropertyExistInHierarchy() {
704 String testedDataTypeKey = "test.dataType.B";
705 when(propertyDefinitionMock.getType()).thenReturn(testedDataTypeKey);
706 dataTypeExistInHierarchy(testedDataTypeKey, propertyDefinitionMock);
709 private void dataTypeExistInHierarchy(String testedDataTypeKey, DefinitionOfDataType testedDefinitionDataType) {
710 String typeToMatch = "test.dataType.A";
711 Map<String, DataType> stDataTypes = new HashMap<>();
712 addDataType(stDataTypes, "tosca.datatypes.network.NetworkInfo", new DataType());
713 DataType testedDataType = createDataType(typeToMatch);
714 addDataType(stDataTypes, testedDataTypeKey, testedDataType);
715 ServiceTemplate serviceTemplate = new ServiceTemplate();
716 serviceTemplate.setData_types(stDataTypes);
717 assertTrue(toscaAnalyzerService
718 .isTypeOf(testedDefinitionDataType, typeToMatch, serviceTemplate, toscaServiceModelMock));
722 public void interfaceTypeExistInHierarchy() {
723 String typeToMatch = "test.interfaceType.A";
724 String testedInterfaceTypeKey = "test.interfaceType.B";
725 when(interfaceDefinitionMock.getType()).thenReturn(testedInterfaceTypeKey);
726 Map<String, Object> stInterfaceTypes = new HashMap<>();
727 stInterfaceTypes.put("tosca.interfaces.network.NetworkInfo", new InterfaceType());
728 InterfaceType testedInterfaceType = createInterfaceType(typeToMatch);
729 stInterfaceTypes.put(testedInterfaceTypeKey, testedInterfaceType);
730 ServiceTemplate serviceTemplate = new ServiceTemplate();
731 serviceTemplate.setInterface_types(stInterfaceTypes);
732 assertTrue(toscaAnalyzerService.isTypeOf(interfaceDefinitionMock, "test.interfaceType.A", serviceTemplate,
733 toscaServiceModelMock));
737 public void shouldThrowCoreExceptionForInvalidNodeType() {
738 thrown.expect(CoreException.class);
739 thrown.expectMessage("Entity Type 'AAA' or one of its derivedFrom type hierarchy, is not defined in "
740 + "tosca service model");
741 when(nodeTemplateMock.getType()).thenReturn("AAA");
742 Map<String, NodeType> stNodeTypes = new HashMap<>();
743 addNodeType(stNodeTypes, "notImportant", new NodeType());
744 ServiceTemplate serviceTemplate = new ServiceTemplate();
745 serviceTemplate.setNode_types(stNodeTypes);
747 .isTypeOf(nodeTemplateMock, ToscaNodeType.NATIVE_COMPUTE, serviceTemplate, toscaServiceModelMock);
751 public void shouldThrowCoreExceptionForInvalidNodeType2Level() {
752 thrown.expect(CoreException.class);
753 thrown.expectMessage(
754 "Entity Type 'A' or one of its derivedFrom type hierarchy, is not defined in tosca " + "service model");
755 String typeToMatch = "A";
756 when(nodeTemplateMock.getType()).thenReturn(typeToMatch);
757 Map<String, NodeType> stNodeTypes = new HashMap<>();
758 addNodeType(stNodeTypes, "notImportant", new NodeType());
759 addNodeType(stNodeTypes, "A", createNodeType("ADerivedFromB"));
760 addNodeType(stNodeTypes, "ADerivedFromB'", createNodeType("BDerivedFromC"));
761 ServiceTemplate serviceTemplate = new ServiceTemplate();
762 serviceTemplate.setNode_types(stNodeTypes);
763 assertTrue(toscaAnalyzerService
764 .isTypeOf(nodeTemplateMock, "BDerivedFromC", serviceTemplate, toscaServiceModelMock));
768 public void shouldReturnTrueIfNdTmpTypeIsFoundInSrvTmpNdTyAndNotDerivedFromRequestedTypeBut2ndLevelDerivedFromMatch() {
769 String typeToMatch = "A";
770 when(nodeTemplateMock.getType()).thenReturn(typeToMatch);
771 Map<String, NodeType> stNodeTypes = new HashMap<>();
772 addNodeType(stNodeTypes, "notImportant", new NodeType());
773 addNodeType(stNodeTypes, "A", createNodeType("ADerivedFromB"));
774 addNodeType(stNodeTypes, "ADerivedFromB", createNodeType("BDerivedFromC"));
775 ServiceTemplate serviceTemplate = new ServiceTemplate();
776 serviceTemplate.setNode_types(stNodeTypes);
777 assertTrue(toscaAnalyzerService
778 .isTypeOf(nodeTemplateMock, "BDerivedFromC", serviceTemplate, toscaServiceModelMock));
781 private NodeType createNodeType(String derivedFrom) {
782 NodeType nodeType = new NodeType();
783 nodeType.setDerived_from(derivedFrom);
787 private DataType createDataType(String derivedFrom) {
788 DataType dataType = new DataType();
789 dataType.setDerived_from(derivedFrom);
793 private InterfaceType createInterfaceType(String derivedFrom) {
794 InterfaceType interfaceType = new InterfaceType();
795 interfaceType.setDerived_from(derivedFrom);
796 return interfaceType;
799 private void addNodeType(Map<String, NodeType> stNodeTypes, String key, NodeType nodeType) {
800 stNodeTypes.put(key, nodeType);
803 private void addDataType(Map<String, DataType> stDataTypes, String key, DataType dataType) {
804 stDataTypes.put(key, dataType);
808 public void shouldReturnTrueIfNdTmpTypeIsFoundInSrvTmpNdTyButRequestedTypeNotMatchButFoundIn1stLevelImports() {
809 String typeToMatch = ToscaNodeType.CINDER_VOLUME;
810 when(nodeTemplateMock.getType()).thenReturn(typeToMatch);
811 List<Map<String, Import>> imports = new ArrayList<>();
812 Map<String, Import> importMap = new HashMap<>();
813 Import anImport = new Import();
814 anImport.setFile("mainImport");
815 importMap.put("bla bla", anImport);
816 imports.add(importMap);
817 ServiceTemplate mainST = new ServiceTemplate();
818 mainST.setImports(imports);
820 //create searchable service template
821 Map<String, NodeType> stNodeTypes = new HashMap<>();
822 addNodeType(stNodeTypes, ToscaNodeType.NATIVE_COMPUTE, new NodeType());
823 NodeType nodeType = createNodeType(ToscaNodeType.NATIVE_BLOCK_STORAGE);
824 addNodeType(stNodeTypes, typeToMatch, nodeType);
825 ServiceTemplate serviceTemplate = new ServiceTemplate();
826 serviceTemplate.setNode_types(stNodeTypes);
828 // add service templates to tosca service model
829 Map<String, ServiceTemplate> serviceTemplates = toscaServiceModelMock.getServiceTemplates();
830 serviceTemplates.put("testMainServiceTemplate", mainST);
831 serviceTemplates.put("mainImport", serviceTemplate);
832 when(toscaServiceModelMock.getServiceTemplates()).thenReturn(serviceTemplates);
834 assertTrue(toscaAnalyzerService.isTypeOf(nodeTemplateMock, ToscaNodeType.NATIVE_BLOCK_STORAGE, mainST,
835 toscaServiceModelMock));
839 public void shouldReturnTrueIfNdTmpTypeIsFoundInSrvTmpNdTyButRequestedTypeNotMatchButFoundIn2ndLevelImports() {
840 String typeToMatch = ToscaNodeType.CINDER_VOLUME;
841 when(nodeTemplateMock.getType()).thenReturn(typeToMatch);
842 List<Map<String, Import>> imports = new ArrayList<>();
843 Map<String, Import> importMap = new HashMap<>();
844 Import anImport = new Import();
845 anImport.setFile("refToMainImport");
846 importMap.put("bla bla", anImport);
847 imports.add(importMap);
848 ServiceTemplate mainST = new ServiceTemplate();
849 mainST.setImports(imports);
851 //create searchable service template
852 Map<String, NodeType> stNodeTypes = new HashMap<>();
853 addNodeType(stNodeTypes, ToscaNodeType.NATIVE_COMPUTE, new NodeType());
854 NodeType nodeType = createNodeType(ToscaNodeType.NATIVE_BLOCK_STORAGE);
855 addNodeType(stNodeTypes, typeToMatch, nodeType);
856 ServiceTemplate serviceTemplate = new ServiceTemplate();
857 serviceTemplate.setNode_types(stNodeTypes);
859 // create 1st level service template with import only
860 List<Map<String, Import>> firstLevelImports = new ArrayList<>();
861 Map<String, Import> firstLevelImportsMap = new HashMap<>();
862 Import firstLevelImport = new Import();
863 firstLevelImport.setFile("mainImport");
864 firstLevelImportsMap.put("bla bla 2", firstLevelImport);
865 firstLevelImports.add(firstLevelImportsMap);
866 ServiceTemplate firstLevelST = new ServiceTemplate();
867 firstLevelST.setImports(firstLevelImports);
869 // add service templates to tosca service model
870 Map<String, ServiceTemplate> serviceTemplates = toscaServiceModelMock.getServiceTemplates();
871 serviceTemplates.put("testMainServiceTemplate", mainST);
872 serviceTemplates.put("refToMainImport", firstLevelST);
873 serviceTemplates.put("mainImport", serviceTemplate);
874 when(toscaServiceModelMock.getServiceTemplates()).thenReturn(serviceTemplates);
876 assertTrue(toscaAnalyzerService.isTypeOf(nodeTemplateMock, ToscaNodeType.NATIVE_BLOCK_STORAGE, mainST,
877 toscaServiceModelMock));
880 // not found at all should throw core exception
884 public void capabilityDefinitionIsTypeOfDirectTypeFound() {
885 CapabilityDefinition capabilityDefinition = new CapabilityDefinition();
886 capabilityDefinition.setType(CAPABILITY_TYPE_A);
887 assertTrue(toscaAnalyzerService.isTypeOf(capabilityDefinition, CAPABILITY_TYPE_A, new ServiceTemplate(),
888 toscaServiceModelMock));
892 public void capabilityDefinitionIsTypeOfReturnNo() {
893 CapabilityDefinition capabilityDefinition = new CapabilityDefinition();
894 capabilityDefinition.setType(CAPABILITY_TYPE_A);
895 ServiceTemplate serviceTemplate = new ServiceTemplate();
896 serviceTemplate.setCapability_types(new HashMap<>());
897 CapabilityType capabilityType = new CapabilityType();
898 capabilityType.setDerived_from(TOSCA_CAPABILITIES_ROOT);
899 serviceTemplate.getCapability_types().put(CAPABILITY_TYPE_A, capabilityType);
900 assertFalse(toscaAnalyzerService
901 .isTypeOf(capabilityDefinition, CAPABILITY_TYPE_B, serviceTemplate, toscaServiceModelMock));
905 public void capabilityDefinitionIsTypeOfInheritanceTypeFound() {
906 CapabilityDefinition capabilityDefinition = new CapabilityDefinition();
907 capabilityDefinition.setType(CAPABILITY_TYPE_A);
908 ServiceTemplate serviceTemplate = new ServiceTemplate();
909 serviceTemplate.setCapability_types(new HashMap<>());
910 CapabilityType capabilityType = new CapabilityType();
911 capabilityType.setDerived_from(CAPABILITY_TYPE_B);
912 serviceTemplate.getCapability_types().put(CAPABILITY_TYPE_A, capabilityType);
913 assertTrue(toscaAnalyzerService
914 .isTypeOf(capabilityDefinition, CAPABILITY_TYPE_B, serviceTemplate, toscaServiceModelMock));