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