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;
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;
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;
42 import java.util.Optional;
44 import static org.junit.Assert.assertEquals;
45 import static org.junit.Assert.assertNull;
46 import static org.mockito.Mockito.when;
48 @RunWith(MockitoJUnitRunner.class)
49 public class PropertiesUtilsTest {
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")));
59 final List<PropertyDefinition> properties = PropertiesUtils.getProperties(service);
60 assertEquals(3, properties.size());
64 public void testProxyServiceNullInputs(){
65 when(service.getProperties()).thenReturn(Arrays.asList(buildPropertyDefinition("a"),buildPropertyDefinition("b")));
66 when(service.getInputs()).thenReturn(null);
68 final List<PropertyDefinition> properties = PropertiesUtils.getProperties(service);
69 assertEquals(2, properties.size());
73 public void testProxyServiceNullProperties(){
74 when(service.getProperties()).thenReturn(null);
75 when(service.getInputs()).thenReturn(Arrays.asList(buildInputDefinition("a"), buildInputDefinition("c")));
77 final List<PropertyDefinition> properties = PropertiesUtils.getProperties(service);
78 assertEquals(2, properties.size());
82 public void testGetCapabilityProperty() {
84 Assert.assertEquals(1, PropertiesUtils.getCapabilityProperty(createProperties(),
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",
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());
109 public void testGetPropertyByInputId() {
110 Resource resource = new ResourceBuilder().setComponentType(ComponentTypeEnum.RESOURCE).setUniqueId("resourceId")
111 .setName("name").build();
112 CapabilityDefinition capabilityDefinition = createCapabilityDefinition();
114 List<ComponentInstanceProperty> properties = new ArrayList<>();
115 ComponentInstanceProperty instanceProperty = createProperties();
117 List<GetInputValueDataDefinition> valueDataDefinitionList = new ArrayList<>();
118 GetInputValueDataDefinition getInputValueDataDefinition = new GetInputValueDataDefinition();
119 getInputValueDataDefinition.setInputId("inputId");
120 getInputValueDataDefinition.setPropName("prop_name");
121 valueDataDefinitionList.add(getInputValueDataDefinition);
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);
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());
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));
147 public void testProxyServiceAllNull(){
148 when(service.getProperties()).thenReturn(null);
149 when(service.getInputs()).thenReturn(null);
151 final List<PropertyDefinition> properties = PropertiesUtils.getProperties(service);
152 assertEquals(0, properties.size());
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());
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());
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());
195 Optional<PropertyDefinition> declaredProperty = properties.stream()
196 .filter(propertyDefinition -> propertyDefinition.getName().equals(declaredPropertyName))
198 Assert.assertTrue(declaredProperty.isPresent());
199 assertEquals(inputValue, declaredProperty.get().getValue());
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());
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());
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);
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());
252 private PropertyDefinition buildPropertyDefinition(String name) {
253 PropertyDefinition retVal = new PropertyDefinition();
254 retVal.setUniqueId("componentUUID." + name);
255 retVal.setName(name);
259 private PropertyDefinition buildPropertyDefinition(String name, String value) {
260 PropertyDefinition retVal = buildPropertyDefinition(name);
261 retVal.setValue(value);
265 private InputDefinition buildInputDefinition(String name){
266 InputDefinition retVal = new InputDefinition();
267 retVal.setName(name);
271 private PropertyDefinition buildPropertyDefinitionForDeclaredProperty(String propertyName, String inputName){
272 String declaredPropertyValue = "{get_input : " + inputName + " }";
273 return buildPropertyDefinition(propertyName, declaredPropertyValue);
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;
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());
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;
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<>();
314 capabilityDefinition.setPath(path);
315 return capabilityDefinition;