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 java.util.HashMap;
22 import org.junit.Assert;
23 import org.junit.BeforeClass;
24 import org.junit.Test;
25 import org.openecomp.sdc.be.DummyConfigurationManager;
26 import org.openecomp.sdc.be.datatypes.elements.ArtifactDataDefinition;
27 import org.openecomp.sdc.be.datatypes.elements.ListDataDefinition;
28 import org.openecomp.sdc.be.datatypes.elements.OperationDataDefinition;
29 import org.openecomp.sdc.be.datatypes.elements.OperationInputDefinition;
30 import org.openecomp.sdc.be.model.Component;
31 import org.openecomp.sdc.be.model.InterfaceDefinition;
32 import org.openecomp.sdc.be.model.Resource;
33 import org.openecomp.sdc.be.tosca.ToscaExportHandler;
34 import org.openecomp.sdc.be.tosca.ToscaRepresentation;
35 import org.openecomp.sdc.be.tosca.model.ToscaNodeType;
36 import org.openecomp.sdc.be.tosca.model.ToscaTemplate;
40 * @since April 12, 2018
43 public class InterfacesOperationsToscaUtilTest {
46 public static void setUp() {
47 new DummyConfigurationManager();
52 public void addInterfaceTypeElement() {
53 Component component = new Resource();
54 component.setNormalizedName("normalizedComponentName");
55 InterfaceDefinition addedInterface = new InterfaceDefinition();
56 addedInterface.setToscaResourceName("interface.types.test_resource_name");
57 addOperationsToInterface(addedInterface, 5, 3, true);
58 final String interfaceType = "normalizedComponentName-interface";
59 ((Resource) component).setInterfaces(new HashMap<>());
60 ((Resource) component).getInterfaces().put(interfaceType, addedInterface);
61 final Map<String, Object> interfaceTypeElement =
62 InterfacesOperationsToscaUtil.addInterfaceTypeElement(component);
64 ToscaExportHandler handler = new ToscaExportHandler();
65 ToscaTemplate template = new ToscaTemplate("test");
66 template.setInterface_types(interfaceTypeElement);
67 final ToscaRepresentation toscaRepresentation = handler.createToscaRepresentation(template);
69 Assert.assertFalse(toscaRepresentation.getMainYaml().contains("operations"));
70 Assert.assertTrue(toscaRepresentation.getMainYaml().contains("interface.types.test_resource_name"));
74 public void addInterfaceDefinitionElement() {
75 Component component = new Resource();
76 component.setNormalizedName("normalizedComponentName");
77 InterfaceDefinition addedInterface = new InterfaceDefinition();
78 addedInterface.setToscaResourceName("com.some.resource.or.other.resourceName");
80 addOperationsToInterface(addedInterface, 3, 2, true);
81 final String interfaceType = "normalizedComponentName-interface";
82 ((Resource) component).setInterfaces(new HashMap<>());
83 ((Resource) component).getInterfaces().put(interfaceType, addedInterface);
84 ToscaNodeType nodeType = new ToscaNodeType();
85 InterfacesOperationsToscaUtil.addInterfaceDefinitionElement(component, nodeType);
87 ToscaExportHandler handler = new ToscaExportHandler();
88 ToscaTemplate template = new ToscaTemplate("test");
89 Map<String, ToscaNodeType> nodeTypes = new HashMap<>();
90 nodeTypes.put("test", nodeType);
91 template.setNode_types(nodeTypes);
92 final ToscaRepresentation toscaRepresentation = handler.createToscaRepresentation(template);
94 Assert.assertFalse(toscaRepresentation.getMainYaml().contains("operations"));
95 Assert.assertTrue(toscaRepresentation.getMainYaml().contains("resourceName:"));
96 Assert.assertTrue(toscaRepresentation.getMainYaml().contains("inputs:"));
97 Assert.assertFalse(toscaRepresentation.getMainYaml().contains("defaultp"));
98 Assert.assertTrue(toscaRepresentation.getMainYaml().contains("has description"));
99 Assert.assertTrue(toscaRepresentation.getMainYaml().contains("naming_function_"));
100 Assert.assertTrue(toscaRepresentation.getMainYaml().contains("com.some.resource.or.other.resourceName"));
104 public void addInterfaceDefinitionElement_noInputs() {
105 Component component = new Resource();
106 component.setNormalizedName("normalizedComponentName");
107 InterfaceDefinition addedInterface = new InterfaceDefinition();
108 addedInterface.setToscaResourceName("com.some.resource.or.other.resourceNameNoInputs");
110 addOperationsToInterface(addedInterface, 3, 3, false);
111 final String interfaceType = "normalizedComponentName-interface";
112 ((Resource) component).setInterfaces(new HashMap<>());
113 ((Resource) component).getInterfaces().put(interfaceType, addedInterface);
114 ToscaNodeType nodeType = new ToscaNodeType();
115 InterfacesOperationsToscaUtil.addInterfaceDefinitionElement(component, nodeType);
117 ToscaExportHandler handler = new ToscaExportHandler();
118 ToscaTemplate template = new ToscaTemplate("test");
119 Map<String, ToscaNodeType> nodeTypes = new HashMap<>();
120 nodeTypes.put("test", nodeType);
121 template.setNode_types(nodeTypes);
122 final ToscaRepresentation toscaRepresentation = handler.createToscaRepresentation(template);
124 Assert.assertFalse(toscaRepresentation.getMainYaml().contains("operations"));
125 Assert.assertFalse(toscaRepresentation.getMainYaml().contains("input_"));
126 Assert.assertFalse(toscaRepresentation.getMainYaml().contains("defaultp"));
127 Assert.assertTrue(toscaRepresentation.getMainYaml().contains("resourceNameNoInputs:"));
128 Assert.assertTrue(toscaRepresentation.getMainYaml().contains("has description"));
129 Assert.assertTrue(toscaRepresentation.getMainYaml().contains("com.some.resource.or.other.resourceName"));
133 private void addOperationsToInterface(InterfaceDefinition addedInterface, int numOfOps, int numOfInputsPerOp,
136 addedInterface.setOperations(new HashMap<>());
137 for (int i = 0; i < numOfOps; i++) {
138 final OperationDataDefinition operation = new OperationDataDefinition();
139 operation.setName("name_for_op_" + i);
140 operation.setDescription( "op "+i+" has description");
141 final ArtifactDataDefinition implementation = new ArtifactDataDefinition();
142 implementation.setArtifactName(i + "_createBPMN.bpmn");
143 operation.setImplementation(implementation);
145 operation.setInputs(createInputs(numOfInputsPerOp));
147 addedInterface.getOperations().put(operation.getName(), operation);
152 private ListDataDefinition<OperationInputDefinition> createInputs(int numOfInputs) {
153 ListDataDefinition<OperationInputDefinition> operationInputDefinitionList = new ListDataDefinition<>();
154 for (int i = 0; i < numOfInputs; i++) {
155 operationInputDefinitionList.add(createMockOperationInputDefinition("input_" + i,
156 java.util.UUID.randomUUID().toString() + "." + "naming_function_" + i));
158 return operationInputDefinitionList;
162 private OperationInputDefinition createMockOperationInputDefinition(String name, String id) {
163 OperationInputDefinition operationInputDefinition = new OperationInputDefinition();
164 operationInputDefinition.setName(name);
165 operationInputDefinition.setInputId(id);
166 return operationInputDefinition;