55a2083e7cea47c0fab1c103168a891b7ea854d7
[sdc.git] / catalog-be / src / test / java / org / openecomp / sdc / be / tosca / ToscaExportUtilsTest.java
1 /*
2  * Copyright © 2016-2019 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;
18
19 import org.junit.Assert;
20 import org.junit.Test;
21 import org.openecomp.sdc.be.datatypes.elements.ArtifactDataDefinition;
22 import org.openecomp.sdc.be.datatypes.elements.OperationDataDefinition;
23 import org.openecomp.sdc.be.model.Component;
24 import org.openecomp.sdc.be.model.DataTypeDefinition;
25 import org.openecomp.sdc.be.model.InputDefinition;
26 import org.openecomp.sdc.be.model.InterfaceDefinition;
27 import org.openecomp.sdc.be.model.PropertyDefinition;
28 import org.openecomp.sdc.be.model.Service;
29 import org.openecomp.sdc.be.tosca.model.ToscaProperty;
30 import org.openecomp.sdc.be.tosca.utils.ToscaExportUtils;
31
32 import java.util.ArrayList;
33 import java.util.Arrays;
34 import java.util.HashMap;
35 import java.util.Map;
36 import java.util.Optional;
37
38 public class ToscaExportUtilsTest {
39
40     private static final Map<String, DataTypeDefinition> dataTypes = new HashMap<>();
41
42     @Test
43     public void testGetProxyNodeTypeInterfacesNoInterfaces() {
44         Component service = new Service();
45         Optional<Map<String, Object>> proxyNodeTypeInterfaces =
46                 ToscaExportUtils.getProxyNodeTypeInterfaces(service, dataTypes);
47         Assert.assertFalse(proxyNodeTypeInterfaces.isPresent());
48     }
49
50     @Test
51     public void testGetProxyNodeTypeInterfaces() {
52         Component service = getTestComponent();
53         Optional<Map<String, Object>> proxyNodeTypeInterfaces =
54                 ToscaExportUtils.getProxyNodeTypeInterfaces(service, dataTypes);
55         Assert.assertTrue(proxyNodeTypeInterfaces.isPresent());
56         Map<String, Object> componentInterfaces = proxyNodeTypeInterfaces.get();
57         Assert.assertNotNull(componentInterfaces);
58         Assert.assertEquals(1, componentInterfaces.size());
59     }
60
61
62     @Test
63     public void testGetProxyNodeTypePropertiesComponentNull() {
64         Optional<Map<String, ToscaProperty>> proxyNodeTypeProperties =
65                 ToscaExportUtils.getProxyNodeTypeProperties(null, dataTypes);
66         Assert.assertFalse(proxyNodeTypeProperties.isPresent());
67     }
68
69     @Test
70     public void testGetProxyNodeTypePropertiesNoProperties() {
71         Component service = new Service();
72         Optional<Map<String, ToscaProperty>> proxyNodeTypeProperties =
73                 ToscaExportUtils.getProxyNodeTypeProperties(service, dataTypes);
74         Assert.assertFalse(proxyNodeTypeProperties.isPresent());
75     }
76
77     @Test
78     public void testGetProxyNodeTypeProperties() {
79         Component service = getTestComponent();
80         service.setProperties(Arrays.asList(createMockProperty("componentPropStr", "Default String Prop"),
81                 createMockProperty("componentPropInt", null)));
82         Optional<Map<String, ToscaProperty>> proxyNodeTypeProperties =
83                 ToscaExportUtils.getProxyNodeTypeProperties(service, dataTypes);
84         Assert.assertTrue(proxyNodeTypeProperties.isPresent());
85         Map<String, ToscaProperty> componentProperties = proxyNodeTypeProperties.get();
86         Assert.assertNotNull(componentProperties);
87         Assert.assertEquals(2, componentProperties.size());
88     }
89
90     @Test
91     public void testAddInputsToPropertiesNoInputs() {
92         Component service = getTestComponent();
93         service.setProperties(Arrays.asList(createMockProperty("componentPropStr", "Default String Prop"),
94                 createMockProperty("componentPropInt", null)));
95         Optional<Map<String, ToscaProperty>> proxyNodeTypePropertiesResult =
96                 ToscaExportUtils.getProxyNodeTypeProperties(service, dataTypes);
97
98         Assert.assertTrue(proxyNodeTypePropertiesResult.isPresent());
99         Map<String, ToscaProperty> proxyNodeTypeProperties = proxyNodeTypePropertiesResult.get();
100         ToscaExportUtils.addInputsToProperties(dataTypes, null, proxyNodeTypeProperties);
101         Assert.assertNotNull(proxyNodeTypeProperties);
102         Assert.assertEquals(2, proxyNodeTypeProperties.size());
103         ToscaExportUtils.addInputsToProperties(dataTypes, new ArrayList<>(), proxyNodeTypeProperties);
104         Assert.assertEquals(2, proxyNodeTypeProperties.size());
105     }
106
107     @Test
108     public void testAddInputsToPropertiesWithInputs() {
109         Component service = getTestComponent();
110         service.setProperties(Arrays.asList(createMockProperty("componentPropStr", "Default String Prop"),
111                 createMockProperty("componentPropInt", null)));
112         service.setInputs(Arrays.asList(createMockInput("componentInputStr1",
113                 "Default String Input1"), createMockInput("componentInputStr2", "Default String Input2")));
114         Optional<Map<String, ToscaProperty>> proxyNodeTypePropertiesResult =
115                 ToscaExportUtils.getProxyNodeTypeProperties(service, dataTypes);
116
117         Assert.assertTrue(proxyNodeTypePropertiesResult.isPresent());
118         Map<String, ToscaProperty> proxyNodeTypeProperties = proxyNodeTypePropertiesResult.get();
119         ToscaExportUtils.addInputsToProperties(dataTypes, service.getInputs(), proxyNodeTypeProperties);
120         Assert.assertNotNull(proxyNodeTypeProperties);
121         Assert.assertEquals(4, proxyNodeTypeProperties.size());
122         Assert.assertNotNull(proxyNodeTypeProperties.get("componentInputStr1"));
123         Assert.assertNotNull(proxyNodeTypeProperties.get("componentInputStr2"));
124     }
125
126     @Test
127     public void testAddInputsToPropertiesOnlyInputs() {
128         Component service = getTestComponent();
129         service.setInputs(Arrays.asList(createMockInput("componentInputStr1",
130                 "Default String Input1"), createMockInput("componentInputStr2", "Default String Input2")));
131         Optional<Map<String, ToscaProperty>> proxyNodeTypePropertiesResult =
132                 ToscaExportUtils.getProxyNodeTypeProperties(service, dataTypes);
133
134         Assert.assertTrue(proxyNodeTypePropertiesResult.isPresent());
135         Map<String, ToscaProperty> proxyNodeTypeProperties = proxyNodeTypePropertiesResult.get();
136         ToscaExportUtils.addInputsToProperties(dataTypes, service.getInputs(), proxyNodeTypeProperties);
137         Assert.assertNotNull(proxyNodeTypeProperties);
138         Assert.assertEquals(2, proxyNodeTypeProperties.size());
139         Assert.assertNotNull(proxyNodeTypeProperties.get("componentInputStr1"));
140         Assert.assertNotNull(proxyNodeTypeProperties.get("componentInputStr2"));
141     }
142
143     @Test
144     public void testOperationImplementationInProxyNodeTypeNotPresent() {
145         Component service = getTestComponent();
146         InterfaceDefinition interfaceDefinition =
147                 service.getInterfaces().get("normalizedServiceComponentName-interface");
148         interfaceDefinition.setOperations(new HashMap<>());
149         final OperationDataDefinition operation = new OperationDataDefinition();
150         operation.setName("start");
151         operation.setDescription("op description");
152         final ArtifactDataDefinition implementation = new ArtifactDataDefinition();
153         implementation.setArtifactName("createBPMN.bpmn");
154         operation.setImplementation(implementation);
155         interfaceDefinition.getOperations().put(operation.getName(), operation);
156         service.getInterfaces().put("normalizedServiceComponentName-interface", interfaceDefinition);
157         service.setInputs(Arrays.asList(createMockInput("componentInputStr1",
158                 "Default String Input1"), createMockInput("componentInputStr2", "Default String Input2")));
159         Optional<Map<String, Object>> proxyNodeTypeInterfaces =
160                 ToscaExportUtils.getProxyNodeTypeInterfaces(service, dataTypes);
161         Assert.assertTrue(proxyNodeTypeInterfaces.isPresent());
162         Map<String, Object> componentInterfaces = proxyNodeTypeInterfaces.get();
163         Assert.assertNotNull(componentInterfaces);
164         Assert.assertEquals(1, componentInterfaces.size());
165         Map<String, Object> proxyInterfaceDefinition =
166                 (Map<String, Object>) componentInterfaces.get("serviceName");
167         Map<String, Object> startOperationDefinition = (Map<String, Object>) proxyInterfaceDefinition.get("start");
168         Assert.assertNotNull(startOperationDefinition);
169         Assert.assertNull(startOperationDefinition.get("implementation"));
170     }
171
172     private Component getTestComponent() {
173         Component component = new Service();
174         component.setNormalizedName("normalizedServiceComponentName");
175         InterfaceDefinition addedInterface = new InterfaceDefinition();
176         addedInterface.setType("com.some.service.or.other.serviceName");
177         final String interfaceType = "normalizedServiceComponentName-interface";
178         component.setInterfaces(new HashMap<>());
179         component.getInterfaces().put(interfaceType, addedInterface);
180         return component;
181     }
182
183     private PropertyDefinition createMockProperty(String propertyName, String defaultValue){
184         PropertyDefinition propertyDefinition = new PropertyDefinition();
185         propertyDefinition.setName(propertyName);
186         propertyDefinition.setType("string");
187         propertyDefinition.setDefaultValue(defaultValue);
188         return propertyDefinition;
189     }
190
191     private InputDefinition createMockInput(String inputName, String defaultValue){
192         InputDefinition inputDefinition = new InputDefinition();
193         inputDefinition.setName(inputName);
194         inputDefinition.setType("string");
195         inputDefinition.setDefaultValue(defaultValue);
196         return inputDefinition;
197     }
198 }