re base code
[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 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;
34
35 import java.util.HashMap;
36 import java.util.Map;
37
38 public class InterfacesOperationsToscaUtilTest {
39
40     @BeforeClass
41     public static void setUp() {
42         new DummyConfigurationManager();
43     }
44
45     @Test()
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);
57
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);
62
63         Assert.assertFalse(toscaRepresentation.getMainYaml().contains("operations"));
64         Assert.assertTrue(toscaRepresentation.getMainYaml().contains("interface.types.test_resource_name"));
65     }
66
67     @Test
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");
73
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);
80
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);
87
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"));
95     }
96
97     @Test
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");
103
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);
110
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);
117
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"));
124     }
125
126
127     private void addOperationsToInterface(InterfaceDefinition addedInterface, int numOfOps, int numOfInputsPerOp,
128             boolean hasInputs) {
129
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);
138             if (hasInputs) {
139                 operation.setInputs(createInputs(numOfInputsPerOp));
140             }
141             addedInterface.getOperations().put(operation.getName(), operation);
142         }
143     }
144
145
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));
151         }
152         return operationInputDefinitionList;
153     }
154
155
156     private OperationInputDefinition createMockOperationInputDefinition(String name, String id) {
157         OperationInputDefinition operationInputDefinition = new OperationInputDefinition();
158         operationInputDefinition.setName(name);
159         operationInputDefinition.setInputId(id);
160         return operationInputDefinition;
161     }
162 }