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.be.tosca.utils;
19 import org.junit.Assert;
20 import org.junit.BeforeClass;
21 import org.junit.Test;
22 import org.openecomp.sdc.be.DummyConfigurationManager;
23 import org.openecomp.sdc.be.datatypes.elements.ArtifactDataDefinition;
24 import org.openecomp.sdc.be.datatypes.elements.ListDataDefinition;
25 import org.openecomp.sdc.be.datatypes.elements.OperationDataDefinition;
26 import org.openecomp.sdc.be.datatypes.elements.OperationInputDefinition;
27 import org.openecomp.sdc.be.model.Component;
28 import org.openecomp.sdc.be.model.InterfaceDefinition;
29 import org.openecomp.sdc.be.model.Resource;
30 import org.openecomp.sdc.be.tosca.ToscaExportHandler;
31 import org.openecomp.sdc.be.tosca.ToscaRepresentation;
32 import org.openecomp.sdc.be.tosca.model.ToscaNodeType;
33 import org.openecomp.sdc.be.tosca.model.ToscaTemplate;
35 import java.util.HashMap;
38 public class InterfacesOperationsToscaUtilTest {
41 public static void setUp() {
42 new DummyConfigurationManager();
46 public void addInterfaceTypeElement() {
47 Component component = new Resource();
48 component.setNormalizedName("normalizedComponentName");
49 InterfaceDefinition addedInterface = new InterfaceDefinition();
50 addedInterface.setToscaResourceName("interface.types.test_resource_name");
51 addOperationsToInterface(addedInterface, 5, 3, true);
52 final String interfaceType = "normalizedComponentName-interface";
53 ((Resource) component).setInterfaces(new HashMap<>());
54 ((Resource) component).getInterfaces().put(interfaceType, addedInterface);
55 final Map<String, Object> interfaceTypeElement =
56 InterfacesOperationsToscaUtil.addInterfaceTypeElement(component);
58 ToscaExportHandler handler = new ToscaExportHandler(null,null,null,null,null,null);
59 ToscaTemplate template = new ToscaTemplate("test");
60 template.setInterface_types(interfaceTypeElement);
61 final ToscaRepresentation toscaRepresentation = handler.createToscaRepresentation(template);
63 Assert.assertFalse(toscaRepresentation.getMainYaml().contains("operations"));
64 Assert.assertTrue(toscaRepresentation.getMainYaml().contains("interface.types.test_resource_name"));
68 public void addInterfaceDefinitionElement() {
69 Component component = new Resource();
70 component.setNormalizedName("normalizedComponentName");
71 InterfaceDefinition addedInterface = new InterfaceDefinition();
72 addedInterface.setToscaResourceName("com.some.resource.or.other.resourceName");
74 addOperationsToInterface(addedInterface, 3, 2, true);
75 final String interfaceType = "normalizedComponentName-interface";
76 ((Resource) component).setInterfaces(new HashMap<>());
77 ((Resource) component).getInterfaces().put(interfaceType, addedInterface);
78 ToscaNodeType nodeType = new ToscaNodeType();
79 InterfacesOperationsToscaUtil.addInterfaceDefinitionElement(component, nodeType);
81 ToscaExportHandler handler = new ToscaExportHandler(null,null,null,null,null,null);
82 ToscaTemplate template = new ToscaTemplate("test");
83 Map<String, ToscaNodeType> nodeTypes = new HashMap<>();
84 nodeTypes.put("test", nodeType);
85 template.setNode_types(nodeTypes);
86 final ToscaRepresentation toscaRepresentation = handler.createToscaRepresentation(template);
88 Assert.assertFalse(toscaRepresentation.getMainYaml().contains("operations"));
89 Assert.assertTrue(toscaRepresentation.getMainYaml().contains("resourceName:"));
90 Assert.assertTrue(toscaRepresentation.getMainYaml().contains("inputs:"));
91 Assert.assertFalse(toscaRepresentation.getMainYaml().contains("defaultp"));
92 Assert.assertTrue(toscaRepresentation.getMainYaml().contains("has description"));
93 Assert.assertTrue(toscaRepresentation.getMainYaml().contains("naming_function_"));
94 Assert.assertTrue(toscaRepresentation.getMainYaml().contains("com.some.resource.or.other.resourceName"));
98 public void addInterfaceDefinitionElement_noInputs() {
99 Component component = new Resource();
100 component.setNormalizedName("normalizedComponentName");
101 InterfaceDefinition addedInterface = new InterfaceDefinition();
102 addedInterface.setToscaResourceName("com.some.resource.or.other.resourceNameNoInputs");
104 addOperationsToInterface(addedInterface, 3, 3, false);
105 final String interfaceType = "normalizedComponentName-interface";
106 ((Resource) component).setInterfaces(new HashMap<>());
107 ((Resource) component).getInterfaces().put(interfaceType, addedInterface);
108 ToscaNodeType nodeType = new ToscaNodeType();
109 InterfacesOperationsToscaUtil.addInterfaceDefinitionElement(component, nodeType);
111 ToscaExportHandler handler = new ToscaExportHandler(null,null,null,null,null,null);
112 ToscaTemplate template = new ToscaTemplate("test");
113 Map<String, ToscaNodeType> nodeTypes = new HashMap<>();
114 nodeTypes.put("test", nodeType);
115 template.setNode_types(nodeTypes);
116 final ToscaRepresentation toscaRepresentation = handler.createToscaRepresentation(template);
118 Assert.assertFalse(toscaRepresentation.getMainYaml().contains("operations"));
119 Assert.assertFalse(toscaRepresentation.getMainYaml().contains("input_"));
120 Assert.assertFalse(toscaRepresentation.getMainYaml().contains("defaultp"));
121 Assert.assertTrue(toscaRepresentation.getMainYaml().contains("resourceNameNoInputs:"));
122 Assert.assertTrue(toscaRepresentation.getMainYaml().contains("has description"));
123 Assert.assertTrue(toscaRepresentation.getMainYaml().contains("com.some.resource.or.other.resourceName"));
127 private void addOperationsToInterface(InterfaceDefinition addedInterface, int numOfOps, int numOfInputsPerOp,
130 addedInterface.setOperations(new HashMap<>());
131 for (int i = 0; i < numOfOps; i++) {
132 final OperationDataDefinition operation = new OperationDataDefinition();
133 operation.setName("name_for_op_" + i);
134 operation.setDescription( "op "+i+" has description");
135 final ArtifactDataDefinition implementation = new ArtifactDataDefinition();
136 implementation.setArtifactName(i + "_createBPMN.bpmn");
137 operation.setImplementation(implementation);
139 operation.setInputs(createInputs(numOfInputsPerOp));
141 addedInterface.getOperations().put(operation.getName(), operation);
146 private ListDataDefinition<OperationInputDefinition> createInputs(int numOfInputs) {
147 ListDataDefinition<OperationInputDefinition> operationInputDefinitionList = new ListDataDefinition<>();
148 for (int i = 0; i < numOfInputs; i++) {
149 operationInputDefinitionList.add(createMockOperationInputDefinition("input_" + i,
150 java.util.UUID.randomUUID().toString() + "." + "naming_function_" + i));
152 return operationInputDefinitionList;
156 private OperationInputDefinition createMockOperationInputDefinition(String name, String id) {
157 OperationInputDefinition operationInputDefinition = new OperationInputDefinition();
158 operationInputDefinition.setName(name);
159 operationInputDefinition.setInputId(id);
160 return operationInputDefinition;