2 * Copyright © 2016-2018 European Support Limited
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
16 package org.openecomp.sdc.be.components.impl.utils;
17 import org.junit.Assert;
18 import org.junit.Test;
19 import org.junit.runner.RunWith;
20 import org.mockito.Mock;
21 import org.mockito.junit.MockitoJUnitRunner;
22 import org.openecomp.sdc.be.components.utils.PropertiesUtils;
23 import org.openecomp.sdc.be.components.utils.ResourceBuilder;
24 import org.openecomp.sdc.be.datatypes.elements.GetInputValueDataDefinition;
25 import org.openecomp.sdc.be.datatypes.elements.SchemaDefinition;
26 import org.openecomp.sdc.be.datatypes.enums.ComponentTypeEnum;
27 import org.openecomp.sdc.be.datatypes.enums.ResourceTypeEnum;
28 import org.openecomp.sdc.be.model.CapabilityDefinition;
29 import org.openecomp.sdc.be.model.ComponentInstanceProperty;
30 import org.openecomp.sdc.be.model.InputDefinition;
31 import org.openecomp.sdc.be.model.PropertyDefinition;
32 import org.openecomp.sdc.be.model.Resource;
33 import org.openecomp.sdc.be.model.Service;
35 import java.util.ArrayList;
36 import java.util.Arrays;
37 import java.util.Collections;
38 import java.util.HashMap;
39 import java.util.List;
41 import java.util.Optional;
43 import static org.junit.Assert.assertEquals;
44 import static org.junit.Assert.assertNull;
45 import static org.mockito.Mockito.when;
47 @RunWith(MockitoJUnitRunner.class)
48 public class PropertiesUtilsTest {
54 public void testProxyServiceProperties(){
55 when(service.getProperties()).thenReturn(Arrays.asList(buildPropertyDefinition("a"),buildPropertyDefinition("b")));
56 when(service.getInputs()).thenReturn(Arrays.asList(buildInputDefinition("a"), buildInputDefinition("c")));
58 final List<PropertyDefinition> properties = PropertiesUtils.getProperties(service);
59 assertEquals(3, properties.size());
63 public void testProxyServiceNullInputs(){
64 when(service.getProperties()).thenReturn(Arrays.asList(buildPropertyDefinition("a"),buildPropertyDefinition("b")));
65 when(service.getInputs()).thenReturn(null);
67 final List<PropertyDefinition> properties = PropertiesUtils.getProperties(service);
68 assertEquals(2, properties.size());
72 public void testProxyServiceNullProperties(){
73 when(service.getProperties()).thenReturn(null);
74 when(service.getInputs()).thenReturn(Arrays.asList(buildInputDefinition("a"), buildInputDefinition("c")));
76 final List<PropertyDefinition> properties = PropertiesUtils.getProperties(service);
77 assertEquals(2, properties.size());
81 public void testGetCapabilityProperty() {
83 Assert.assertEquals(1, PropertiesUtils.getCapabilityProperty(createProperties(),
88 public void testGetPropertyCapabilityOfChildInstance() {
89 CapabilityDefinition capabilityDefinition = createCapabilityDefinition();
90 capabilityDefinition.setPath(Collections.singletonList("path"));
91 Map<String, List<CapabilityDefinition>> capMap = new HashMap<>();
92 capMap.put(capabilityDefinition.getType(), Collections.singletonList(capabilityDefinition));
93 Assert.assertTrue(PropertiesUtils.getPropertyCapabilityOfChildInstance("capUniqueId",
98 public void testGetPropertyCapabilityFromAllCapProps() {
99 CapabilityDefinition capabilityDefinition = createCapabilityDefinition();
100 Map<String, List<CapabilityDefinition>> capMap = new HashMap<>();
101 capMap.put(capabilityDefinition.getType(), Collections.singletonList(capabilityDefinition));
102 Assert.assertTrue(PropertiesUtils.getPropertyCapabilityOfChildInstance("capUniqueId",
103 capMap).isPresent());
108 public void testGetPropertyByInputId() {
109 Resource resource = new ResourceBuilder().setComponentType(ComponentTypeEnum.RESOURCE).setUniqueId("resourceId")
110 .setName("name").build();
111 CapabilityDefinition capabilityDefinition = createCapabilityDefinition();
113 List<ComponentInstanceProperty> properties = new ArrayList<>();
114 ComponentInstanceProperty instanceProperty = createProperties();
116 List<GetInputValueDataDefinition> valueDataDefinitionList = new ArrayList<>();
117 GetInputValueDataDefinition getInputValueDataDefinition = new GetInputValueDataDefinition();
118 getInputValueDataDefinition.setInputId("inputId");
119 getInputValueDataDefinition.setPropName("prop_name");
120 valueDataDefinitionList.add(getInputValueDataDefinition);
122 instanceProperty.setGetInputValues(valueDataDefinitionList);
123 properties.add(instanceProperty);
124 capabilityDefinition.setProperties(properties);
125 Map<String, List<CapabilityDefinition>> capabilityMap = new HashMap<>();
126 capabilityMap.put(capabilityDefinition.getType(), Collections.singletonList(capabilityDefinition));
127 resource.setCapabilities(capabilityMap);
129 InputDefinition inputDefinition = new InputDefinition();
130 inputDefinition.setUniqueId("inputId");
131 inputDefinition.setInputId("inputId");
132 inputDefinition.setPropertyId("inputId");
133 resource.setInputs(Collections.singletonList(inputDefinition));
134 Assert.assertTrue(PropertiesUtils.getPropertyByInputId(resource, "inputId").isPresent());
138 public void testIsNodeServiceProxy() {
139 Resource resource = new ResourceBuilder().setComponentType(ComponentTypeEnum.RESOURCE).setUniqueId("resourceId")
140 .setName("name").build();
141 resource.setResourceType(ResourceTypeEnum.ServiceProxy);
142 Assert.assertTrue( PropertiesUtils.isNodeServiceProxy(resource));
146 public void testProxyServiceAllNull(){
147 when(service.getProperties()).thenReturn(null);
148 when(service.getInputs()).thenReturn(null);
150 final List<PropertyDefinition> properties = PropertiesUtils.getProperties(service);
151 assertEquals(0, properties.size());
155 public void testProxyInstanceGetPropertiesUndeclaredPropertyWithValue(){
156 String undeclaredPropertyValue = "testPropDefaultValue";
157 List<PropertyDefinition> propertyDefinitions =
158 Collections.singletonList(buildPropertyDefinition("undeclaredProperty", undeclaredPropertyValue));
159 when(service.getProperties()).thenReturn(propertyDefinitions);
160 when(service.getInputs()).thenReturn(null);
161 final List<PropertyDefinition> properties = PropertiesUtils.getProperties(service);
162 assertEquals(1, properties.size());
163 assertEquals(undeclaredPropertyValue, properties.get(0).getValue());
167 public void testProxyInstanceGetPropertiesUndeclaredPropertyWithoutValue(){
168 List<PropertyDefinition> propertyDefinitions =
169 Collections.singletonList(buildPropertyDefinition("undeclaredProperty"));
170 when(service.getProperties()).thenReturn(propertyDefinitions);
171 when(service.getInputs()).thenReturn(null);
172 final List<PropertyDefinition> properties = PropertiesUtils.getProperties(service);
173 assertEquals(1, properties.size());
174 assertNull(properties.get(0).getValue());
178 public void testProxyInstanceGetPropertiesResolvePropertyValueFromInput() {
179 String declaredPropertyName = "declaredProperty";
180 String mappedInputName = "mappedInput";
181 //Setting default value in input
182 String inputValue = "testDefaultValue";
183 List<PropertyDefinition> propertyDefinitions =
184 Collections.singletonList(buildPropertyDefinitionForDeclaredProperty(
185 declaredPropertyName, mappedInputName));
186 when(service.getProperties()).thenReturn(propertyDefinitions);
187 List<InputDefinition> inputDefinitions =
188 Collections.singletonList(buildInputDefinitionForMappedProperty(mappedInputName, inputValue,
189 "componentUUID." + declaredPropertyName));
190 when(service.getInputs()).thenReturn(inputDefinitions);
191 final List<PropertyDefinition> properties = PropertiesUtils.getProperties(service);
192 assertEquals(2, properties.size());
194 Optional<PropertyDefinition> declaredProperty = properties.stream()
195 .filter(propertyDefinition -> propertyDefinition.getName().equals(declaredPropertyName))
197 Assert.assertTrue(declaredProperty.isPresent());
198 assertEquals(inputValue, declaredProperty.get().getValue());
203 public void testResolvePropertyValueFromInput() {
204 String mappedInputValue = "Default String Input Value";
205 PropertyDefinition mappedProperty =
206 buildPropertyDefinitionForDeclaredProperty("componentPropStr1", "componentInputStr1");
207 List<InputDefinition> componentInputs =
208 Collections.singletonList(buildInputDefinitionForMappedProperty("componentInputStr1", mappedInputValue,
209 "componentUUID.componentPropStr1"));
210 PropertyDefinition updatedPropertyDefinition =
211 PropertiesUtils.resolvePropertyValueFromInput(mappedProperty, componentInputs);
212 Assert.assertNotNull(updatedPropertyDefinition);
213 Assert.assertEquals(mappedInputValue, updatedPropertyDefinition.getValue());
218 public void testResolvePropertyValueFromInputNoInputs() {
219 PropertyDefinition mappedProperty =
220 buildPropertyDefinitionForDeclaredProperty("componentPropStr1", "componentInputStr1");
221 PropertyDefinition updatedPropertyDefinition =
222 PropertiesUtils.resolvePropertyValueFromInput(mappedProperty, null);
223 Assert.assertNotNull(updatedPropertyDefinition);
224 Assert.assertEquals(mappedProperty.getValue(), updatedPropertyDefinition.getValue());
228 public void testResolvePropertyValueFromInputPropertyDefinitionNull() {
229 List<InputDefinition> componentInputs =
230 Arrays.asList(buildInputDefinitionForMappedProperty("componentInputStr1", "Default Value",
231 "componentPropStr1"), buildInputDefinitionForMappedProperty("componentInputStr2",
232 "Default String Input2", "componentPropStr2"));
233 PropertyDefinition updatedPropertyDefinition =
234 PropertiesUtils.resolvePropertyValueFromInput(null, componentInputs);
235 Assert.assertNull(updatedPropertyDefinition);
239 public void testResolvePropertyValueFromInputUndeclaredProperty() {
240 String propertyValue = "Default String Property Value";
241 PropertyDefinition undeclaredProperty =
242 buildPropertyDefinition("componentPropStr1", propertyValue);
243 List<InputDefinition> componentInputs =
244 Arrays.asList(buildInputDefinition("componentInputStr1"), buildInputDefinition("componentInputStr2"));
245 PropertyDefinition updatedPropertyDefinition =
246 PropertiesUtils.resolvePropertyValueFromInput(undeclaredProperty, componentInputs);
247 Assert.assertNotNull(updatedPropertyDefinition);
248 Assert.assertEquals(undeclaredProperty.getValue(), updatedPropertyDefinition.getValue());
251 private PropertyDefinition buildPropertyDefinition(String name) {
252 PropertyDefinition retVal = new PropertyDefinition();
253 retVal.setUniqueId("componentUUID." + name);
254 retVal.setName(name);
258 private PropertyDefinition buildPropertyDefinition(String name, String value) {
259 PropertyDefinition retVal = buildPropertyDefinition(name);
260 retVal.setValue(value);
264 private InputDefinition buildInputDefinition(String name){
265 InputDefinition retVal = new InputDefinition();
266 retVal.setName(name);
270 private PropertyDefinition buildPropertyDefinitionForDeclaredProperty(String propertyName, String inputName){
271 String declaredPropertyValue = "{get_input : " + inputName + " }";
272 return buildPropertyDefinition(propertyName, declaredPropertyValue);
275 private InputDefinition buildInputDefinitionForMappedProperty(String inputName, String inputValue,
276 String mappedPropertyId){
277 InputDefinition inputDefinition = new InputDefinition();
278 inputDefinition.setName(inputName);
279 inputDefinition.setType("string");
280 inputDefinition.setPropertyId(mappedPropertyId);
281 inputDefinition.setDefaultValue(inputValue);
282 inputDefinition.setValue(inputValue);
283 return inputDefinition;
286 private ComponentInstanceProperty createProperties() {
287 ComponentInstanceProperty instanceProperty = new ComponentInstanceProperty();
288 instanceProperty.setUniqueId("inputId");
289 instanceProperty.setType("Integer");
290 instanceProperty.setName("prop_name");
291 instanceProperty.setDescription("prop_description_prop_desc");
292 instanceProperty.setOwnerId("capUniqueId");
293 instanceProperty.setValue("{\"get_input\":\"extcp20_order\"}");
294 instanceProperty.setSchema(new SchemaDefinition());
296 List<GetInputValueDataDefinition> valueDataDefinitionList = new ArrayList<>();
297 GetInputValueDataDefinition getInputValueDataDefinition = new GetInputValueDataDefinition();
298 getInputValueDataDefinition.setInputId("inputId");
299 getInputValueDataDefinition.setPropName("prop_name");
300 valueDataDefinitionList.add(getInputValueDataDefinition);
301 instanceProperty.setGetInputValues(valueDataDefinitionList);
302 return instanceProperty;
305 private CapabilityDefinition createCapabilityDefinition() {
306 CapabilityDefinition capabilityDefinition = new CapabilityDefinition();
307 capabilityDefinition.setName("cap" + Math.random());
308 capabilityDefinition.setType("tosca.capabilities.network.Bindable");
309 capabilityDefinition.setOwnerId("resourceId");
310 capabilityDefinition.setUniqueId("capUniqueId");
311 List<String> path = new ArrayList<>();
313 capabilityDefinition.setPath(path);
314 return capabilityDefinition;