Revert - Workflow Operation output tosca
[sdc.git] / catalog-be / src / test / java / org / openecomp / sdc / be / tosca / utils / InterfacesOperationsToscaUtilTest.java
1 /*
2  * Copyright © 2016-2018 European Support Limited
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16
17 package org.openecomp.sdc.be.tosca.utils;
18
19 import java.util.HashMap;
20 import java.util.Map;
21
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;
37
38 /**
39  * @author KATYR
40  * @since April 12, 2018
41  */
42
43 public class InterfacesOperationsToscaUtilTest {
44
45     @BeforeClass
46     public static void setUp() {
47         new DummyConfigurationManager();
48     }
49
50
51     @Test
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);
63
64         ToscaExportHandler handler = new ToscaExportHandler();
65         ToscaTemplate template = new ToscaTemplate("test");
66         template.setInterface_types(interfaceTypeElement);
67         final ToscaRepresentation toscaRepresentation = handler.createToscaRepresentation(template);
68
69         Assert.assertFalse(toscaRepresentation.getMainYaml().contains("operations"));
70         Assert.assertTrue(toscaRepresentation.getMainYaml().contains("interface.types.test_resource_name"));
71     }
72
73     @Test
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");
79
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);
86
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);
93
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"));
101     }
102
103     @Test
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");
109
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);
116
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);
123
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"));
130     }
131
132
133     private void addOperationsToInterface(InterfaceDefinition addedInterface, int numOfOps, int numOfInputsPerOp,
134             boolean hasInputs) {
135
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);
144             if (hasInputs) {
145                 operation.setInputs(createInputs(numOfInputsPerOp));
146             }
147             addedInterface.getOperations().put(operation.getName(), operation);
148         }
149     }
150
151
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));
157         }
158         return operationInputDefinitionList;
159     }
160
161
162     private OperationInputDefinition createMockOperationInputDefinition(String name, String id) {
163         OperationInputDefinition operationInputDefinition = new OperationInputDefinition();
164         operationInputDefinition.setName(name);
165         operationInputDefinition.setInputId(id);
166         return operationInputDefinition;
167     }
168 }