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