Fix service proxy node type
[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 java.util.ArrayList;
20 import java.util.Arrays;
21 import java.util.Collections;
22 import java.util.HashMap;
23 import java.util.List;
24 import java.util.Map;
25 import java.util.Optional;
26
27 import org.junit.Assert;
28 import org.junit.Test;
29 import org.openecomp.sdc.be.model.Component;
30 import org.openecomp.sdc.be.model.DataTypeDefinition;
31 import org.openecomp.sdc.be.model.InputDefinition;
32 import org.openecomp.sdc.be.model.InterfaceDefinition;
33 import org.openecomp.sdc.be.model.PropertyDefinition;
34 import org.openecomp.sdc.be.model.Service;
35 import org.openecomp.sdc.be.tosca.model.ToscaProperty;
36 import org.openecomp.sdc.be.tosca.utils.ToscaExportUtils;
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 testResolvePropertyDefaultValueFromInputNoInputs() {
92         Component service = getTestComponent();
93         service.setProperties(Collections.singletonList(createMockProperty("componentPropStr", null)));
94         Optional<Map<String, ToscaProperty>> properties = ToscaExportUtils.getProxyNodeTypeProperties(service,
95                 dataTypes);
96         Assert.assertTrue(properties.isPresent());
97         Map<String, ToscaProperty> nodeTypeProperties = properties.get();
98         ToscaExportUtils.resolvePropertyDefaultValueFromInput(null, nodeTypeProperties, dataTypes);
99         nodeTypeProperties.values().forEach(val -> Assert.assertNull(val.getDefaultp()));
100     }
101
102     @Test
103     public void testResolvePropertyDefaultValueFromInput() {
104         Component service = getTestComponent();
105         service.setProperties(Arrays.asList(createMockProperty("componentPropStr1", "{get_input: componentInputStr1}"),
106                 createMockProperty("componentPropStr2", "Default prop value"),
107                 createMockProperty("componentPropStr3", null)));
108         Optional<Map<String, ToscaProperty>> properties = ToscaExportUtils.getProxyNodeTypeProperties(service,
109                 dataTypes);
110         Assert.assertTrue(properties.isPresent());
111         Map<String, ToscaProperty> nodeTypeProperties = properties.get();
112         List<InputDefinition> componentInputs = Arrays.asList(createMockInput("componentInputStr1",
113                 "Default String Input1"), createMockInput("componentInputStr2", "Default String Input2"));
114         ToscaExportUtils.resolvePropertyDefaultValueFromInput(componentInputs, nodeTypeProperties, dataTypes);
115         nodeTypeProperties.entrySet().stream()
116                 .filter(entry -> entry.getKey().equals("componentPropStr1"))
117                 .forEach(entry -> Assert.assertEquals("Default String Input1",
118                         entry.getValue().getDefaultp().toString()));
119
120         nodeTypeProperties.entrySet().stream()
121                 .filter(entry -> entry.getKey().equals("componentPropStr2"))
122                 .forEach(entry -> Assert.assertEquals("Default prop value",
123                         entry.getValue().getDefaultp().toString()));
124
125         nodeTypeProperties.entrySet().stream()
126                 .filter(entry -> entry.getKey().equals("componentPropStr3"))
127                 .forEach(entry -> Assert.assertNull(entry.getValue().getDefaultp()));
128     }
129
130     @Test
131     public void testAddInputsToPropertiesNoInputs() {
132         Component service = getTestComponent();
133         service.setProperties(Arrays.asList(createMockProperty("componentPropStr", "Default String Prop"),
134                 createMockProperty("componentPropInt", null)));
135         Optional<Map<String, ToscaProperty>> proxyNodeTypePropertiesResult =
136                 ToscaExportUtils.getProxyNodeTypeProperties(service, dataTypes);
137
138         Assert.assertTrue(proxyNodeTypePropertiesResult.isPresent());
139         Map<String, ToscaProperty> proxyNodeTypeProperties = proxyNodeTypePropertiesResult.get();
140         ToscaExportUtils.addInputsToProperties(dataTypes, null, proxyNodeTypeProperties);
141         Assert.assertNotNull(proxyNodeTypeProperties);
142         Assert.assertEquals(2, proxyNodeTypeProperties.size());
143         ToscaExportUtils.addInputsToProperties(dataTypes, new ArrayList<>(), proxyNodeTypeProperties);
144         Assert.assertEquals(2, proxyNodeTypeProperties.size());
145     }
146
147     @Test
148     public void testAddInputsToPropertiesWithInputs() {
149         Component service = getTestComponent();
150         service.setProperties(Arrays.asList(createMockProperty("componentPropStr", "Default String Prop"),
151                 createMockProperty("componentPropInt", null)));
152         service.setInputs(Arrays.asList(createMockInput("componentInputStr1",
153                 "Default String Input1"), createMockInput("componentInputStr2", "Default String Input2")));
154         Optional<Map<String, ToscaProperty>> proxyNodeTypePropertiesResult =
155                 ToscaExportUtils.getProxyNodeTypeProperties(service, dataTypes);
156
157         Assert.assertTrue(proxyNodeTypePropertiesResult.isPresent());
158         Map<String, ToscaProperty> proxyNodeTypeProperties = proxyNodeTypePropertiesResult.get();
159         ToscaExportUtils.addInputsToProperties(dataTypes, service.getInputs(), proxyNodeTypeProperties);
160         Assert.assertNotNull(proxyNodeTypeProperties);
161         Assert.assertEquals(4, proxyNodeTypeProperties.size());
162         Assert.assertNotNull(proxyNodeTypeProperties.get("componentInputStr1"));
163         Assert.assertNotNull(proxyNodeTypeProperties.get("componentInputStr2"));
164     }
165
166     @Test
167     public void testAddInputsToPropertiesOnlyInputs() {
168         Component service = getTestComponent();
169         service.setInputs(Arrays.asList(createMockInput("componentInputStr1",
170                 "Default String Input1"), createMockInput("componentInputStr2", "Default String Input2")));
171         Optional<Map<String, ToscaProperty>> proxyNodeTypePropertiesResult =
172                 ToscaExportUtils.getProxyNodeTypeProperties(service, dataTypes);
173
174         Assert.assertTrue(proxyNodeTypePropertiesResult.isPresent());
175         Map<String, ToscaProperty> proxyNodeTypeProperties = proxyNodeTypePropertiesResult.get();
176         ToscaExportUtils.addInputsToProperties(dataTypes, service.getInputs(), proxyNodeTypeProperties);
177         Assert.assertNotNull(proxyNodeTypeProperties);
178         Assert.assertEquals(2, proxyNodeTypeProperties.size());
179         Assert.assertNotNull(proxyNodeTypeProperties.get("componentInputStr1"));
180         Assert.assertNotNull(proxyNodeTypeProperties.get("componentInputStr2"));
181     }
182
183     private Component getTestComponent() {
184         Component component = new Service();
185         component.setNormalizedName("normalizedServiceComponentName");
186         InterfaceDefinition addedInterface = new InterfaceDefinition();
187         addedInterface.setType("com.some.service.or.other.serviceName");
188         final String interfaceType = "normalizedServiceComponentName-interface";
189         component.setInterfaces(new HashMap<>());
190         component.getInterfaces().put(interfaceType, addedInterface);
191         return component;
192     }
193
194     private PropertyDefinition createMockProperty(String propertyName, String defaultValue){
195         PropertyDefinition propertyDefinition = new PropertyDefinition();
196         propertyDefinition.setName(propertyName);
197         propertyDefinition.setType("string");
198         propertyDefinition.setDefaultValue(defaultValue);
199         return propertyDefinition;
200     }
201
202     private InputDefinition createMockInput(String inputName, String defaultValue){
203         InputDefinition inputDefinition = new InputDefinition();
204         inputDefinition.setName(inputName);
205         inputDefinition.setType("string");
206         inputDefinition.setDefaultValue(defaultValue);
207         return inputDefinition;
208     }
209 }