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.datatypes.elements.SchemaDefinition;
8 import org.openecomp.sdc.be.model.DataTypeDefinition;
9 import org.openecomp.sdc.be.model.PropertyDefinition;
10 import org.openecomp.sdc.be.model.Resource;
11 import org.openecomp.sdc.be.model.tosca.ToscaPropertyType;
12 import org.openecomp.sdc.be.tosca.model.ToscaNodeType;
13 import org.openecomp.sdc.be.tosca.model.ToscaProperty;
17 import static org.assertj.core.api.Assertions.assertThat;
18 import static org.junit.Assert.*;
20 public class PropertyConvertorTest {
21 private PropertyDefinition property;
22 Map<String, DataTypeDefinition> dataTypes;
26 property = new PropertyDefinition();
27 property.setName("myProperty");
28 property.setType(ToscaPropertyType.INTEGER.getType());
29 dataTypes = new HashMap();
30 dataTypes.put(property.getName(), new DataTypeDefinition());
34 public void testConvertProperty() {
35 SchemaDefinition schema = new SchemaDefinition();
36 schema.setProperty(property);
38 property.setSchema(schema);
40 PropertyConvertor.getInstance().convertProperty(dataTypes, property, true);
44 public void convertPropertyWhenValueAndDefaultNull() {
45 ToscaProperty prop = PropertyConvertor.getInstance().convertProperty(dataTypes, property, false);
47 assertNull(prop.getDefaultp());
51 public void convertPropertyWhenValueNullAndDefaultNotEmpty() {
52 final String def = "1";
53 property.setDefaultValue(def);
54 ToscaProperty result = PropertyConvertor.getInstance().convertProperty(dataTypes, property, false);
55 assertNotNull(result);
56 assertEquals(Integer.valueOf(def), result.getDefaultp());
60 public void convertPropertiesWhenValueAndDefaultNullInOne() {
61 PropertyDefinition property1 = new PropertyDefinition();
62 property1.setName("otherProperty");
63 property1.setType(ToscaPropertyType.INTEGER.getType());
64 property1.setDefaultValue("2");
65 dataTypes.put(property1.getName(), new DataTypeDefinition());
66 Resource resource = new Resource();
67 List<PropertyDefinition> properties = new ArrayList();
68 properties.add(property);
69 properties.add(property1);
70 resource.setProperties(properties);
71 Either<ToscaNodeType, ToscaError> result = PropertyConvertor.getInstance().convertProperties(resource, new ToscaNodeType(), dataTypes);
72 assertTrue(result.isLeft());
73 assertEquals(2, result.left().value().getProperties().size());
75 for (Iterator<ToscaProperty> it = result.left().value().getProperties().values().iterator(); it.hasNext(); ) {
76 ToscaProperty prop = it.next();
77 if (prop.getDefaultp() == null) {
85 public void convertPropertiesWhenValueAndDefaultExist() {
86 PropertyDefinition property1 = new PropertyDefinition();
87 property1.setName("otherProperty");
88 property1.setType(ToscaPropertyType.INTEGER.getType());
89 property1.setDefaultValue("2");
90 property.setDefaultValue("1");
91 dataTypes.put(property1.getName(), new DataTypeDefinition());
92 Resource resource = new Resource();
93 List<PropertyDefinition> properties = new ArrayList();
94 properties.add(property);
95 properties.add(property1);
96 resource.setProperties(properties);
97 Either<ToscaNodeType, ToscaError> result = PropertyConvertor.getInstance().convertProperties(resource, new ToscaNodeType(), dataTypes);
98 assertTrue(result.isLeft());
99 assertEquals(2, result.left().value().getProperties().size());
100 for (Iterator<ToscaProperty> it = result.left().value().getProperties().values().iterator(); it.hasNext(); ) {
101 ToscaProperty prop = it.next();
102 assertNotNull(prop.getDefaultp());
107 public void convertPropertiesWhenValueAndDefaultNullInAll() {
108 PropertyDefinition property1 = new PropertyDefinition();
109 property1.setName("otherProperty");
110 property1.setType(ToscaPropertyType.INTEGER.getType());
111 dataTypes.put(property1.getName(), new DataTypeDefinition());
112 Resource resource = new Resource();
113 List<PropertyDefinition> properties = new ArrayList();
114 properties.add(property);
115 properties.add(property1);
116 resource.setProperties(properties);
117 Either<ToscaNodeType, ToscaError> result = PropertyConvertor.getInstance().convertProperties(resource, new ToscaNodeType(), dataTypes);
118 assertTrue(result.isLeft());
119 assertEquals(2, result.left().value().getProperties().size());
120 for (Iterator<ToscaProperty> it = result.left().value().getProperties().values().iterator(); it.hasNext(); ) {
121 ToscaProperty prop = it.next();
122 assertNull(prop.getDefaultp());
127 public void convertPropertyWhichStartsWithSemiColon() throws Exception {
128 PropertyDefinition property1 = new PropertyDataDefinitionBuilder()
129 .setDefaultValue("::")
130 .setType(ToscaPropertyType.STRING.getType())
132 ToscaProperty toscaProperty = PropertyConvertor.getInstance().convertProperty(Collections.emptyMap(), property1, false);
133 assertThat(toscaProperty.getDefaultp()).isEqualTo("::");
137 public void convertPropertyWhichStartsWithSlash() throws Exception {
138 PropertyDefinition property1 = new PropertyDataDefinitionBuilder()
139 .setDefaultValue("/")
140 .setType(ToscaPropertyType.STRING.getType())
142 ToscaProperty toscaProperty = PropertyConvertor.getInstance().convertProperty(Collections.emptyMap(), property1, false);
143 assertThat(toscaProperty.getDefaultp()).isEqualTo("/");
147 public void testConvertToToscaObject() {
148 dataTypes.put(ToscaPropertyType.Root.getType(), new DataTypeDefinition());
150 PropertyConvertor.getInstance().convertToToscaObject(ToscaPropertyType.Root.getType(), "", "innerType", dataTypes,true);
154 public void testConvertToToscaObjectWhenPropertyTypeAndInnerTypeNull() {
155 dataTypes.put(ToscaPropertyType.Root.getType(), new DataTypeDefinition());
157 PropertyConvertor.getInstance().convertToToscaObject(null, "value", null, dataTypes,true);
161 public void testConvertToToscaObjectWhenIsScalarTypeIsNotNull() {
162 DataTypeDefinition def = new DataTypeDefinition();
163 def.setName("integer");
164 dataTypes.put("type", def);
166 PropertyConvertor.getInstance().convertToToscaObject("type", "value", "innerType", dataTypes,true);