Catalog alignment
[sdc.git] / catalog-be / src / test / java / org / openecomp / sdc / be / tosca / PropertyConvertorTest.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * SDC
4  * ================================================================================
5  * Copyright (C) 2019 AT&T Intellectual Property. All rights reserved.
6  * ================================================================================
7  * Licensed under the Apache License, Version 2.0 (the "License");
8  * you may not use this file except in compliance with the License.
9  * You may obtain a copy of the License at
10  * 
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  * 
13  * Unless required by applicable law or agreed to in writing, software
14  * distributed under the License is distributed on an "AS IS" BASIS,
15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  * See the License for the specific language governing permissions and
17  * limitations under the License.
18  * ============LICENSE_END=========================================================
19  */
20
21 package org.openecomp.sdc.be.tosca;
22
23 import fj.data.Either;
24 import org.junit.Before;
25 import org.junit.Test;
26 import org.openecomp.sdc.be.components.utils.PropertyDataDefinitionBuilder;
27 import org.openecomp.sdc.be.datatypes.elements.SchemaDefinition;
28 import org.openecomp.sdc.be.model.DataTypeDefinition;
29 import org.openecomp.sdc.be.model.PropertyDefinition;
30 import org.openecomp.sdc.be.model.Resource;
31 import org.openecomp.sdc.be.model.tosca.ToscaPropertyType;
32 import org.openecomp.sdc.be.tosca.model.ToscaNodeType;
33 import org.openecomp.sdc.be.tosca.model.ToscaProperty;
34
35 import java.util.ArrayList;
36 import java.util.Collections;
37 import java.util.HashMap;
38 import java.util.Iterator;
39 import java.util.List;
40 import java.util.Map;
41
42 import static org.assertj.core.api.Assertions.assertThat;
43 import static org.junit.Assert.assertEquals;
44 import static org.junit.Assert.assertNotNull;
45 import static org.junit.Assert.assertNull;
46 import static org.junit.Assert.assertTrue;
47
48 public class PropertyConvertorTest {
49     private PropertyDefinition property;
50     Map<String, DataTypeDefinition> dataTypes;
51
52     @Before
53     public void setUp(){
54         property = new PropertyDefinition();
55         property.setName("myProperty");
56         property.setType(ToscaPropertyType.INTEGER.getType());
57         dataTypes = new HashMap();
58         dataTypes.put(property.getName(), new DataTypeDefinition());
59     }
60
61     @Test
62     public void testConvertProperty() {
63         SchemaDefinition schema = new SchemaDefinition();
64         schema.setProperty(property);
65         
66         property.setSchema(schema);
67         
68         PropertyConvertor.getInstance().convertProperty(dataTypes, property, PropertyConvertor.PropertyType.PROPERTY);
69     }
70
71     @Test
72     public void convertPropertyWhenValueAndDefaultNull() {
73         ToscaProperty prop = PropertyConvertor.getInstance().convertProperty(dataTypes, property, PropertyConvertor.PropertyType.PROPERTY);
74         assertNotNull(prop);
75         assertNull(prop.getDefaultp());
76     }
77
78     @Test
79     public void convertPropertyWhenValueNullAndDefaultNotEmpty() {
80         final String def = "1";
81         property.setDefaultValue(def);
82         ToscaProperty result = PropertyConvertor.getInstance().convertProperty(dataTypes, property, PropertyConvertor.PropertyType.PROPERTY);
83         assertNotNull(result);
84         assertEquals(Integer.valueOf(def), result.getDefaultp());
85     }
86
87     @Test
88     public void convertPropertiesWhenValueAndDefaultNullInOne() {
89         PropertyDefinition property1 = new PropertyDefinition();
90         property1.setName("otherProperty");
91         property1.setType(ToscaPropertyType.INTEGER.getType());
92         property1.setDefaultValue("2");
93         dataTypes.put(property1.getName(), new DataTypeDefinition());
94         Resource resource = new Resource();
95         List<PropertyDefinition> properties = new ArrayList();
96         properties.add(property);
97         properties.add(property1);
98         resource.setProperties(properties);
99         Either<ToscaNodeType, ToscaError> result = PropertyConvertor.getInstance().convertProperties(resource, new ToscaNodeType(), dataTypes);
100         assertTrue(result.isLeft());
101         assertEquals(2, result.left().value().getProperties().size());
102         int cnt = 0;
103         for (Iterator<ToscaProperty> it = result.left().value().getProperties().values().iterator(); it.hasNext(); ) {
104             ToscaProperty prop = it.next();
105             if (prop.getDefaultp() == null) {
106                 cnt++;
107             }
108         }
109         assertEquals(1, cnt);
110     }
111
112     @Test
113     public void convertPropertiesWhenValueAndDefaultExist() {
114         PropertyDefinition property1 = new PropertyDefinition();
115         property1.setName("otherProperty");
116         property1.setType(ToscaPropertyType.INTEGER.getType());
117         property1.setDefaultValue("2");
118         property.setDefaultValue("1");
119         dataTypes.put(property1.getName(), new DataTypeDefinition());
120         Resource resource = new Resource();
121         List<PropertyDefinition> properties = new ArrayList();
122         properties.add(property);
123         properties.add(property1);
124         resource.setProperties(properties);
125         Either<ToscaNodeType, ToscaError> result = PropertyConvertor.getInstance().convertProperties(resource, new ToscaNodeType(), dataTypes);
126         assertTrue(result.isLeft());
127         assertEquals(2, result.left().value().getProperties().size());
128         for (Iterator<ToscaProperty> it = result.left().value().getProperties().values().iterator(); it.hasNext(); ) {
129             ToscaProperty prop = it.next();
130             assertNotNull(prop.getDefaultp());
131         }
132     }
133
134     @Test
135     public void convertPropertiesWhenValueAndDefaultNullInAll() {
136         PropertyDefinition property1 = new PropertyDefinition();
137         property1.setName("otherProperty");
138         property1.setType(ToscaPropertyType.INTEGER.getType());
139         dataTypes.put(property1.getName(), new DataTypeDefinition());
140         Resource resource = new Resource();
141         List<PropertyDefinition> properties = new ArrayList();
142         properties.add(property);
143         properties.add(property1);
144         resource.setProperties(properties);
145         Either<ToscaNodeType, ToscaError> result = PropertyConvertor.getInstance().convertProperties(resource, new ToscaNodeType(), dataTypes);
146         assertTrue(result.isLeft());
147         assertEquals(2, result.left().value().getProperties().size());
148         for (Iterator<ToscaProperty> it = result.left().value().getProperties().values().iterator(); it.hasNext(); ) {
149             ToscaProperty prop = it.next();
150             assertNull(prop.getDefaultp());
151         }
152      }
153
154     @Test
155     public void convertPropertyWhichStartsWithSemiColon() throws Exception {
156         PropertyDefinition property1 = new PropertyDataDefinitionBuilder()
157                 .setDefaultValue("::")
158                 .setType(ToscaPropertyType.STRING.getType())
159                 .build();
160         ToscaProperty toscaProperty = PropertyConvertor.getInstance().convertProperty(Collections.emptyMap(), property1, PropertyConvertor.PropertyType.PROPERTY);
161         assertThat(toscaProperty.getDefaultp()).isEqualTo("::");
162     }
163
164     @Test
165     public void convertPropertyWhichStartsWithSlash() throws Exception {
166         PropertyDefinition property1 = new PropertyDataDefinitionBuilder()
167                 .setDefaultValue("/")
168                 .setType(ToscaPropertyType.STRING.getType())
169                 .build();
170         ToscaProperty toscaProperty = PropertyConvertor.getInstance().convertProperty(Collections.emptyMap(), property1, PropertyConvertor.PropertyType.PROPERTY);
171         assertThat(toscaProperty.getDefaultp()).isEqualTo("/");
172     }
173
174 }