1 package org.openecomp.sdc.be.tosca;
3 import static org.junit.Assert.assertEquals;
4 import static org.junit.Assert.assertNotNull;
5 import static org.junit.Assert.assertNull;
6 import static org.junit.Assert.assertTrue;
8 import java.util.ArrayList;
9 import java.util.HashMap;
10 import java.util.Iterator;
11 import java.util.List;
14 import org.junit.Before;
15 import org.junit.Test;
16 import org.openecomp.sdc.be.model.DataTypeDefinition;
17 import org.openecomp.sdc.be.model.PropertyDefinition;
18 import org.openecomp.sdc.be.model.Resource;
19 import org.openecomp.sdc.be.model.tosca.ToscaPropertyType;
20 import org.openecomp.sdc.be.tosca.model.ToscaNodeType;
21 import org.openecomp.sdc.be.tosca.model.ToscaProperty;
23 import fj.data.Either;
25 public class PropertyConvertorTest {
26 private PropertyDefinition property;
27 Map<String, DataTypeDefinition> dataTypes;
31 property = new PropertyDefinition();
32 property.setName("myProperty");
33 property.setType(ToscaPropertyType.INTEGER.getType());
34 dataTypes = new HashMap<String, DataTypeDefinition>();
35 dataTypes.put(property.getName(), new DataTypeDefinition());
40 public void convertPropertyWhenValueAndDefaultNull() {
41 ToscaProperty prop = PropertyConvertor.getInstance().convertProperty(dataTypes, property, false);
43 assertNull(prop.getDefaultp());
47 public void convertPropertyWhenValueNullAndDefaultNotEmpty() {
48 final String def = "1";
49 property.setDefaultValue(def);
50 ToscaProperty result = PropertyConvertor.getInstance().convertProperty(dataTypes, property, false);
51 assertNotNull(result);
52 assertEquals(Integer.valueOf(def).intValue(), result.getDefaultp());
56 public void convertPropertiesWhenValueAndDefaultNullInOne() {
57 PropertyDefinition property1 = new PropertyDefinition();
58 property1.setName("otherProperty");
59 property1.setType(ToscaPropertyType.INTEGER.getType());
60 property1.setDefaultValue("2");
61 dataTypes.put(property1.getName(), new DataTypeDefinition());
62 Resource resource = new Resource();
63 List<PropertyDefinition> properties = new ArrayList<PropertyDefinition>();
64 properties.add(property);
65 properties.add(property1);
66 resource.setProperties(properties);
67 Either<ToscaNodeType, ToscaError> result = PropertyConvertor.getInstance().convertProperties(resource, new ToscaNodeType(), dataTypes);
68 assertTrue(result.isLeft());
69 assertEquals(2, result.left().value().getProperties().size());
71 for (Iterator<ToscaProperty> it = result.left().value().getProperties().values().iterator(); it.hasNext(); ) {
72 ToscaProperty prop = it.next();
73 if (prop.getDefaultp() == null) {
81 public void convertPropertiesWhenValueAndDefaultExist() {
82 PropertyDefinition property1 = new PropertyDefinition();
83 property1.setName("otherProperty");
84 property1.setType(ToscaPropertyType.INTEGER.getType());
85 property1.setDefaultValue("2");
86 property.setDefaultValue("1");
87 dataTypes.put(property1.getName(), new DataTypeDefinition());
88 Resource resource = new Resource();
89 List<PropertyDefinition> properties = new ArrayList<PropertyDefinition>();
90 properties.add(property);
91 properties.add(property1);
92 resource.setProperties(properties);
93 Either<ToscaNodeType, ToscaError> result = PropertyConvertor.getInstance().convertProperties(resource, new ToscaNodeType(), dataTypes);
94 assertTrue(result.isLeft());
95 assertEquals(2, result.left().value().getProperties().size());
96 for (Iterator<ToscaProperty> it = result.left().value().getProperties().values().iterator(); it.hasNext(); ) {
97 ToscaProperty prop = it.next();
98 assertNotNull(prop.getDefaultp());
103 public void convertPropertiesWhenValueAndDefaultNullInAll() {
104 PropertyDefinition property1 = new PropertyDefinition();
105 property1.setName("otherProperty");
106 property1.setType(ToscaPropertyType.INTEGER.getType());
107 dataTypes.put(property1.getName(), new DataTypeDefinition());
108 Resource resource = new Resource();
109 List<PropertyDefinition> properties = new ArrayList<PropertyDefinition>();
110 properties.add(property);
111 properties.add(property1);
112 resource.setProperties(properties);
113 Either<ToscaNodeType, ToscaError> result = PropertyConvertor.getInstance().convertProperties(resource, new ToscaNodeType(), dataTypes);
114 assertTrue(result.isLeft());
115 assertEquals(2, result.left().value().getProperties().size());
116 for (Iterator<ToscaProperty> it = result.left().value().getProperties().values().iterator(); it.hasNext(); ) {
117 ToscaProperty prop = it.next();
118 assertNull(prop.getDefaultp());