1 package org.openecomp.sdc.be.tosca;
4 import org.junit.Before;
6 import org.openecomp.sdc.be.components.utils.PropertyDataDefinitionBuilder;
7 import org.openecomp.sdc.be.model.DataTypeDefinition;
8 import org.openecomp.sdc.be.model.PropertyDefinition;
9 import org.openecomp.sdc.be.model.Resource;
10 import org.openecomp.sdc.be.model.tosca.ToscaPropertyType;
11 import org.openecomp.sdc.be.tosca.model.ToscaNodeType;
12 import org.openecomp.sdc.be.tosca.model.ToscaProperty;
14 import java.util.ArrayList;
15 import java.util.Collections;
16 import java.util.HashMap;
17 import java.util.Iterator;
18 import java.util.List;
21 import static org.assertj.core.api.Assertions.assertThat;
22 import static org.junit.Assert.assertEquals;
23 import static org.junit.Assert.assertNotNull;
24 import static org.junit.Assert.assertNull;
25 import static org.junit.Assert.assertTrue;
27 public class PropertyConvertorTest {
28 private PropertyDefinition property;
29 Map<String, DataTypeDefinition> dataTypes;
33 property = new PropertyDefinition();
34 property.setName("myProperty");
35 property.setType(ToscaPropertyType.INTEGER.getType());
36 dataTypes = new HashMap();
37 dataTypes.put(property.getName(), new DataTypeDefinition());
42 public void convertPropertyWhenValueAndDefaultNull() {
43 ToscaProperty prop = PropertyConvertor.getInstance().convertProperty(dataTypes, property, false);
45 assertNull(prop.getDefaultp());
49 public void convertPropertyWhenValueNullAndDefaultNotEmpty() {
50 final String def = "1";
51 property.setDefaultValue(def);
52 ToscaProperty result = PropertyConvertor.getInstance().convertProperty(dataTypes, property, false);
53 assertNotNull(result);
54 assertEquals(Integer.valueOf(def), result.getDefaultp());
58 public void convertPropertiesWhenValueAndDefaultNullInOne() {
59 PropertyDefinition property1 = new PropertyDefinition();
60 property1.setName("otherProperty");
61 property1.setType(ToscaPropertyType.INTEGER.getType());
62 property1.setDefaultValue("2");
63 dataTypes.put(property1.getName(), new DataTypeDefinition());
64 Resource resource = new Resource();
65 List<PropertyDefinition> properties = new ArrayList();
66 properties.add(property);
67 properties.add(property1);
68 resource.setProperties(properties);
69 Either<ToscaNodeType, ToscaError> result = PropertyConvertor.getInstance().convertProperties(resource, new ToscaNodeType(), dataTypes);
70 assertTrue(result.isLeft());
71 assertEquals(2, result.left().value().getProperties().size());
73 for (Iterator<ToscaProperty> it = result.left().value().getProperties().values().iterator(); it.hasNext(); ) {
74 ToscaProperty prop = it.next();
75 if (prop.getDefaultp() == null) {
83 public void convertPropertiesWhenValueAndDefaultExist() {
84 PropertyDefinition property1 = new PropertyDefinition();
85 property1.setName("otherProperty");
86 property1.setType(ToscaPropertyType.INTEGER.getType());
87 property1.setDefaultValue("2");
88 property.setDefaultValue("1");
89 dataTypes.put(property1.getName(), new DataTypeDefinition());
90 Resource resource = new Resource();
91 List<PropertyDefinition> properties = new ArrayList();
92 properties.add(property);
93 properties.add(property1);
94 resource.setProperties(properties);
95 Either<ToscaNodeType, ToscaError> result = PropertyConvertor.getInstance().convertProperties(resource, new ToscaNodeType(), dataTypes);
96 assertTrue(result.isLeft());
97 assertEquals(2, result.left().value().getProperties().size());
98 for (Iterator<ToscaProperty> it = result.left().value().getProperties().values().iterator(); it.hasNext(); ) {
99 ToscaProperty prop = it.next();
100 assertNotNull(prop.getDefaultp());
105 public void convertPropertiesWhenValueAndDefaultNullInAll() {
106 PropertyDefinition property1 = new PropertyDefinition();
107 property1.setName("otherProperty");
108 property1.setType(ToscaPropertyType.INTEGER.getType());
109 dataTypes.put(property1.getName(), new DataTypeDefinition());
110 Resource resource = new Resource();
111 List<PropertyDefinition> properties = new ArrayList();
112 properties.add(property);
113 properties.add(property1);
114 resource.setProperties(properties);
115 Either<ToscaNodeType, ToscaError> result = PropertyConvertor.getInstance().convertProperties(resource, new ToscaNodeType(), dataTypes);
116 assertTrue(result.isLeft());
117 assertEquals(2, result.left().value().getProperties().size());
118 for (Iterator<ToscaProperty> it = result.left().value().getProperties().values().iterator(); it.hasNext(); ) {
119 ToscaProperty prop = it.next();
120 assertNull(prop.getDefaultp());
125 public void convertPropertyWhichStartsWithSemiColon() throws Exception {
126 PropertyDefinition property1 = new PropertyDataDefinitionBuilder()
127 .setDefaultValue("::")
128 .setType(ToscaPropertyType.STRING.getType())
130 ToscaProperty toscaProperty = PropertyConvertor.getInstance().convertProperty(Collections.emptyMap(), property1, false);
131 assertThat(toscaProperty.getDefaultp()).isEqualTo("::");
135 public void convertPropertyWhichStartsWithSlash() throws Exception {
136 PropertyDefinition property1 = new PropertyDataDefinitionBuilder()
137 .setDefaultValue("/")
138 .setType(ToscaPropertyType.STRING.getType())
140 ToscaProperty toscaProperty = PropertyConvertor.getInstance().convertProperty(Collections.emptyMap(), property1, false);
141 assertThat(toscaProperty.getDefaultp()).isEqualTo("/");