Catalog alignment
[sdc.git] / catalog-be / src / test / java / org / openecomp / sdc / be / components / impl / utils / PropertiesUtilsTest.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 package org.openecomp.sdc.be.components.impl.utils;
17
18 import org.junit.Assert;
19 import org.junit.Test;
20 import org.junit.runner.RunWith;
21 import org.mockito.Mock;
22 import org.mockito.junit.MockitoJUnitRunner;
23 import org.openecomp.sdc.be.components.utils.PropertiesUtils;
24 import org.openecomp.sdc.be.components.utils.ResourceBuilder;
25 import org.openecomp.sdc.be.datatypes.elements.GetInputValueDataDefinition;
26 import org.openecomp.sdc.be.datatypes.elements.SchemaDefinition;
27 import org.openecomp.sdc.be.datatypes.enums.ComponentTypeEnum;
28 import org.openecomp.sdc.be.datatypes.enums.ResourceTypeEnum;
29 import org.openecomp.sdc.be.model.CapabilityDefinition;
30 import org.openecomp.sdc.be.model.ComponentInstanceProperty;
31 import org.openecomp.sdc.be.model.InputDefinition;
32 import org.openecomp.sdc.be.model.PropertyDefinition;
33 import org.openecomp.sdc.be.model.Resource;
34 import org.openecomp.sdc.be.model.Service;
35
36 import java.util.ArrayList;
37 import java.util.Arrays;
38 import java.util.Collections;
39 import java.util.HashMap;
40 import java.util.List;
41 import java.util.Map;
42 import java.util.Optional;
43
44 import static org.junit.Assert.assertEquals;
45 import static org.junit.Assert.assertNull;
46 import static org.mockito.Mockito.when;
47
48 @RunWith(MockitoJUnitRunner.class)
49 public class PropertiesUtilsTest {
50     @Mock
51     Service service;
52
53
54     @Test
55     public void testProxyServiceProperties(){
56         when(service.getProperties()).thenReturn(Arrays.asList(buildPropertyDefinition("a"),buildPropertyDefinition("b")));
57         when(service.getInputs()).thenReturn(Arrays.asList(buildInputDefinition("a"), buildInputDefinition("c")));
58
59         final List<PropertyDefinition> properties = PropertiesUtils.getProperties(service);
60         assertEquals(3, properties.size());
61     }
62
63     @Test
64     public void testProxyServiceNullInputs(){
65         when(service.getProperties()).thenReturn(Arrays.asList(buildPropertyDefinition("a"),buildPropertyDefinition("b")));
66         when(service.getInputs()).thenReturn(null);
67
68         final List<PropertyDefinition> properties = PropertiesUtils.getProperties(service);
69         assertEquals(2, properties.size());
70     }
71
72     @Test
73     public void testProxyServiceNullProperties(){
74         when(service.getProperties()).thenReturn(null);
75         when(service.getInputs()).thenReturn(Arrays.asList(buildInputDefinition("a"), buildInputDefinition("c")));
76
77         final List<PropertyDefinition> properties = PropertiesUtils.getProperties(service);
78         assertEquals(2, properties.size());
79     }
80
81     @Test
82     public void testGetCapabilityProperty() {
83
84         Assert.assertEquals(1, PropertiesUtils.getCapabilityProperty(createProperties(),
85                 "inputId").size());
86     }
87
88     @Test
89     public void testGetPropertyCapabilityOfChildInstance() {
90         CapabilityDefinition capabilityDefinition = createCapabilityDefinition();
91         capabilityDefinition.setPath(Collections.singletonList("path"));
92         Map<String, List<CapabilityDefinition>> capMap = new HashMap<>();
93         capMap.put(capabilityDefinition.getType(), Collections.singletonList(capabilityDefinition));
94         Assert.assertTrue(PropertiesUtils.getPropertyCapabilityOfChildInstance("capUniqueId",
95                 capMap).isPresent());
96     }
97
98     @Test
99     public void testGetPropertyCapabilityFromAllCapProps() {
100         CapabilityDefinition capabilityDefinition = createCapabilityDefinition();
101         Map<String, List<CapabilityDefinition>> capMap = new HashMap<>();
102         capMap.put(capabilityDefinition.getType(), Collections.singletonList(capabilityDefinition));
103         Assert.assertTrue(PropertiesUtils.getPropertyCapabilityOfChildInstance("capUniqueId",
104                 capMap).isPresent());
105     }
106
107     @Test
108
109     public void testGetPropertyByInputId() {
110         Resource resource = new ResourceBuilder().setComponentType(ComponentTypeEnum.RESOURCE).setUniqueId("resourceId")
111                 .setName("name").build();
112         CapabilityDefinition capabilityDefinition = createCapabilityDefinition();
113
114         List<ComponentInstanceProperty> properties = new ArrayList<>();
115         ComponentInstanceProperty instanceProperty = createProperties();
116
117         List<GetInputValueDataDefinition> valueDataDefinitionList = new ArrayList<>();
118         GetInputValueDataDefinition getInputValueDataDefinition = new GetInputValueDataDefinition();
119         getInputValueDataDefinition.setInputId("inputId");
120         getInputValueDataDefinition.setPropName("prop_name");
121         valueDataDefinitionList.add(getInputValueDataDefinition);
122
123         instanceProperty.setGetInputValues(valueDataDefinitionList);
124         properties.add(instanceProperty);
125         capabilityDefinition.setProperties(properties);
126         Map<String, List<CapabilityDefinition>> capabilityMap = new HashMap<>();
127         capabilityMap.put(capabilityDefinition.getType(), Collections.singletonList(capabilityDefinition));
128         resource.setCapabilities(capabilityMap);
129
130         InputDefinition inputDefinition = new InputDefinition();
131         inputDefinition.setUniqueId("inputId");
132         inputDefinition.setInputId("inputId");
133         inputDefinition.setPropertyId("inputId");
134         resource.setInputs(Collections.singletonList(inputDefinition));
135         Assert.assertTrue(PropertiesUtils.getPropertyByInputId(resource, "inputId").isPresent());
136     }
137
138     @Test
139     public void testIsNodeServiceProxy() {
140        Resource resource = new ResourceBuilder().setComponentType(ComponentTypeEnum.RESOURCE).setUniqueId("resourceId")
141                 .setName("name").build();
142        resource.setResourceType(ResourceTypeEnum.ServiceProxy);
143         Assert.assertTrue( PropertiesUtils.isNodeServiceProxy(resource));
144     }
145
146     @Test
147     public void testProxyServiceAllNull(){
148         when(service.getProperties()).thenReturn(null);
149         when(service.getInputs()).thenReturn(null);
150
151         final List<PropertyDefinition> properties = PropertiesUtils.getProperties(service);
152         assertEquals(0, properties.size());
153     }
154
155     @Test
156     public void testProxyInstanceGetPropertiesUndeclaredPropertyWithValue(){
157         String undeclaredPropertyValue = "testPropDefaultValue";
158         List<PropertyDefinition> propertyDefinitions =
159                 Collections.singletonList(buildPropertyDefinition("undeclaredProperty", undeclaredPropertyValue));
160         when(service.getProperties()).thenReturn(propertyDefinitions);
161         when(service.getInputs()).thenReturn(null);
162         final List<PropertyDefinition> properties = PropertiesUtils.getProperties(service);
163         assertEquals(1, properties.size());
164         assertEquals(undeclaredPropertyValue, properties.get(0).getValue());
165     }
166
167     @Test
168     public void testProxyInstanceGetPropertiesUndeclaredPropertyWithoutValue(){
169         List<PropertyDefinition> propertyDefinitions =
170                 Collections.singletonList(buildPropertyDefinition("undeclaredProperty"));
171         when(service.getProperties()).thenReturn(propertyDefinitions);
172         when(service.getInputs()).thenReturn(null);
173         final List<PropertyDefinition> properties = PropertiesUtils.getProperties(service);
174         assertEquals(1, properties.size());
175         assertNull(properties.get(0).getValue());
176     }
177
178     @Test
179     public void testProxyInstanceGetPropertiesResolvePropertyValueFromInput() {
180         String declaredPropertyName = "declaredProperty";
181         String mappedInputName = "mappedInput";
182         //Setting default value in input
183         String inputValue = "testDefaultValue";
184         List<PropertyDefinition> propertyDefinitions =
185                 Collections.singletonList(buildPropertyDefinitionForDeclaredProperty(
186                         declaredPropertyName, mappedInputName));
187         when(service.getProperties()).thenReturn(propertyDefinitions);
188         List<InputDefinition> inputDefinitions =
189                 Collections.singletonList(buildInputDefinitionForMappedProperty(mappedInputName, inputValue,
190                         "componentUUID." + declaredPropertyName));
191         when(service.getInputs()).thenReturn(inputDefinitions);
192         final List<PropertyDefinition> properties = PropertiesUtils.getProperties(service);
193         assertEquals(2, properties.size());
194
195         Optional<PropertyDefinition> declaredProperty = properties.stream()
196                 .filter(propertyDefinition -> propertyDefinition.getName().equals(declaredPropertyName))
197                 .findFirst();
198         Assert.assertTrue(declaredProperty.isPresent());
199         assertEquals(inputValue, declaredProperty.get().getValue());
200     }
201
202
203     @Test
204     public void testResolvePropertyValueFromInput() {
205         String mappedInputValue = "Default String Input Value";
206         PropertyDefinition mappedProperty =
207                 buildPropertyDefinitionForDeclaredProperty("componentPropStr1", "componentInputStr1");
208         List<InputDefinition> componentInputs =
209                 Collections.singletonList(buildInputDefinitionForMappedProperty("componentInputStr1", mappedInputValue,
210                         "componentUUID.componentPropStr1"));
211         PropertyDefinition updatedPropertyDefinition =
212                 PropertiesUtils.resolvePropertyValueFromInput(mappedProperty, componentInputs);
213         Assert.assertNotNull(updatedPropertyDefinition);
214         Assert.assertEquals(mappedInputValue, updatedPropertyDefinition.getValue());
215     }
216
217
218     @Test
219     public void testResolvePropertyValueFromInputNoInputs() {
220         PropertyDefinition mappedProperty =
221                 buildPropertyDefinitionForDeclaredProperty("componentPropStr1", "componentInputStr1");
222         PropertyDefinition updatedPropertyDefinition =
223                 PropertiesUtils.resolvePropertyValueFromInput(mappedProperty, null);
224         Assert.assertNotNull(updatedPropertyDefinition);
225         Assert.assertEquals(mappedProperty.getValue(), updatedPropertyDefinition.getValue());
226     }
227
228     @Test
229     public void testResolvePropertyValueFromInputPropertyDefinitionNull() {
230         List<InputDefinition> componentInputs =
231                 Arrays.asList(buildInputDefinitionForMappedProperty("componentInputStr1", "Default Value",
232                         "componentPropStr1"), buildInputDefinitionForMappedProperty("componentInputStr2",
233                         "Default String Input2", "componentPropStr2"));
234         PropertyDefinition updatedPropertyDefinition =
235                 PropertiesUtils.resolvePropertyValueFromInput(null, componentInputs);
236         Assert.assertNull(updatedPropertyDefinition);
237     }
238
239     @Test
240     public void testResolvePropertyValueFromInputUndeclaredProperty() {
241         String propertyValue = "Default String Property Value";
242         PropertyDefinition undeclaredProperty =
243                 buildPropertyDefinition("componentPropStr1", propertyValue);
244         List<InputDefinition> componentInputs =
245                 Arrays.asList(buildInputDefinition("componentInputStr1"), buildInputDefinition("componentInputStr2"));
246         PropertyDefinition updatedPropertyDefinition =
247                 PropertiesUtils.resolvePropertyValueFromInput(undeclaredProperty, componentInputs);
248         Assert.assertNotNull(updatedPropertyDefinition);
249         Assert.assertEquals(undeclaredProperty.getValue(), updatedPropertyDefinition.getValue());
250     }
251
252     private PropertyDefinition buildPropertyDefinition(String name) {
253         PropertyDefinition retVal = new PropertyDefinition();
254         retVal.setUniqueId("componentUUID." + name);
255         retVal.setName(name);
256         return retVal;
257     }
258
259     private PropertyDefinition buildPropertyDefinition(String name, String value) {
260         PropertyDefinition retVal = buildPropertyDefinition(name);
261         retVal.setValue(value);
262         return retVal;
263     }
264
265     private InputDefinition buildInputDefinition(String name){
266         InputDefinition retVal = new InputDefinition();
267         retVal.setName(name);
268         return retVal;
269     }
270
271     private PropertyDefinition buildPropertyDefinitionForDeclaredProperty(String propertyName, String inputName){
272         String declaredPropertyValue =  "{get_input : " + inputName + " }";
273         return buildPropertyDefinition(propertyName, declaredPropertyValue);
274     }
275
276     private InputDefinition buildInputDefinitionForMappedProperty(String inputName, String inputValue,
277                                                                   String mappedPropertyId){
278         InputDefinition inputDefinition = new InputDefinition();
279         inputDefinition.setName(inputName);
280         inputDefinition.setType("string");
281         inputDefinition.setPropertyId(mappedPropertyId);
282         inputDefinition.setDefaultValue(inputValue);
283         inputDefinition.setValue(inputValue);
284         return inputDefinition;
285     }
286
287     private ComponentInstanceProperty createProperties() {
288         ComponentInstanceProperty instanceProperty = new ComponentInstanceProperty();
289         instanceProperty.setUniqueId("inputId");
290         instanceProperty.setType("Integer");
291         instanceProperty.setName("prop_name");
292         instanceProperty.setDescription("prop_description_prop_desc");
293         instanceProperty.setOwnerId("capUniqueId");
294         instanceProperty.setValue("{\"get_input\":\"extcp20_order\"}");
295         instanceProperty.setSchema(new SchemaDefinition());
296
297         List<GetInputValueDataDefinition> valueDataDefinitionList = new ArrayList<>();
298         GetInputValueDataDefinition getInputValueDataDefinition = new GetInputValueDataDefinition();
299         getInputValueDataDefinition.setInputId("inputId");
300         getInputValueDataDefinition.setPropName("prop_name");
301         valueDataDefinitionList.add(getInputValueDataDefinition);
302         instanceProperty.setGetInputValues(valueDataDefinitionList);
303         return instanceProperty;
304     }
305
306     private CapabilityDefinition createCapabilityDefinition() {
307         CapabilityDefinition capabilityDefinition = new CapabilityDefinition();
308         capabilityDefinition.setName("cap" + Math.random());
309         capabilityDefinition.setType("tosca.capabilities.network.Bindable");
310         capabilityDefinition.setOwnerId("resourceId");
311         capabilityDefinition.setUniqueId("capUniqueId");
312         List<String> path = new ArrayList<>();
313         path.add("path1");
314         capabilityDefinition.setPath(path);
315         return capabilityDefinition;
316     }
317
318 }