align MME
[sdc.git] / catalog-be / src / test / java / org / openecomp / sdc / be / tosca / PropertyConvertorTest.java
1 package org.openecomp.sdc.be.tosca;
2
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;
7
8 import java.util.ArrayList;
9 import java.util.HashMap;
10 import java.util.List;
11 import java.util.Map;
12
13 import org.junit.Before;
14 import org.junit.Test;
15 import org.openecomp.sdc.be.model.DataTypeDefinition;
16 import org.openecomp.sdc.be.model.PropertyDefinition;
17 import org.openecomp.sdc.be.model.Resource;
18 import org.openecomp.sdc.be.model.tosca.ToscaPropertyType;
19 import org.openecomp.sdc.be.tosca.model.ToscaNodeType;
20 import org.openecomp.sdc.be.tosca.model.ToscaProperty;
21
22 import fj.data.Either;
23
24 public class PropertyConvertorTest {
25     private PropertyDefinition property;
26     Map<String, DataTypeDefinition> dataTypes;
27
28     @Before
29     public void setUp(){
30         property = new PropertyDefinition();
31         property.setName("myProperty");
32         property.setType(ToscaPropertyType.INTEGER.getType());
33         dataTypes = new HashMap<String, DataTypeDefinition>();
34         dataTypes.put(property.getName(), new DataTypeDefinition());
35     }
36
37
38     @Test
39     public void convertPropertyWhenValueAndDefaultNull() {
40         assertNull(PropertyConvertor.getInstance().convertProperty(dataTypes, property, false));
41     }
42
43     @Test
44     public void convertPropertyWhenValueNullAndDefaultNotEmpty() {
45         final String def = "1";
46         property.setDefaultValue(def);
47         ToscaProperty result = PropertyConvertor.getInstance().convertProperty(dataTypes, property, false);
48         assertNotNull(result);
49         assertEquals(Integer.valueOf(def).intValue(), result.getDefaultp());
50     }
51
52     @Test
53     public void convertPropertiesWhenValueAndDefaultNullInOne() {
54         PropertyDefinition property1 = new PropertyDefinition();
55         property1.setName("otherProperty");
56         property1.setType(ToscaPropertyType.INTEGER.getType());
57         property1.setDefaultValue("2");
58         dataTypes.put(property1.getName(), new DataTypeDefinition());
59         Resource resource = new Resource();
60         List<PropertyDefinition> properties = new ArrayList<PropertyDefinition>();
61         properties.add(property);
62         properties.add(property1);
63         resource.setProperties(properties);
64         Either<ToscaNodeType, ToscaError> result = PropertyConvertor.getInstance().convertProperties(resource, new ToscaNodeType(), dataTypes);
65         assertTrue(result.isLeft());
66         assertEquals(1, result.left().value().getProperties().size());
67     }
68
69     @Test
70     public void convertPropertiesWhenValueAndDefaultExist() {
71         PropertyDefinition property1 = new PropertyDefinition();
72         property1.setName("otherProperty");
73         property1.setType(ToscaPropertyType.INTEGER.getType());
74         property1.setDefaultValue("2");
75         property.setDefaultValue("1");
76         dataTypes.put(property1.getName(), new DataTypeDefinition());
77         Resource resource = new Resource();
78         List<PropertyDefinition> properties = new ArrayList<PropertyDefinition>();
79         properties.add(property);
80         properties.add(property1);
81         resource.setProperties(properties);
82         Either<ToscaNodeType, ToscaError> result = PropertyConvertor.getInstance().convertProperties(resource, new ToscaNodeType(), dataTypes);
83         assertTrue(result.isLeft());
84         assertEquals(2, result.left().value().getProperties().size());
85     }
86
87     @Test
88     public void convertPropertiesWhenValueAndDefaultNullInAll() {
89         PropertyDefinition property1 = new PropertyDefinition();
90         property1.setName("otherProperty");
91         property1.setType(ToscaPropertyType.INTEGER.getType());
92         dataTypes.put(property1.getName(), new DataTypeDefinition());
93         Resource resource = new Resource();
94         List<PropertyDefinition> properties = new ArrayList<PropertyDefinition>();
95         properties.add(property);
96         properties.add(property1);
97         resource.setProperties(properties);
98         Either<ToscaNodeType, ToscaError> result = PropertyConvertor.getInstance().convertProperties(resource, new ToscaNodeType(), dataTypes);
99         assertTrue(result.isLeft());
100         assertNull(result.left().value().getProperties());
101     }
102 }