Added oparent to sdc main
[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.*;
36
37 import static org.assertj.core.api.Assertions.assertThat;
38 import static org.junit.Assert.*;
39
40 public class PropertyConvertorTest {
41     private PropertyDefinition property;
42     Map<String, DataTypeDefinition> dataTypes;
43
44     @Before
45     public void setUp(){
46         property = new PropertyDefinition();
47         property.setName("myProperty");
48         property.setType(ToscaPropertyType.INTEGER.getType());
49         dataTypes = new HashMap();
50         dataTypes.put(property.getName(), new DataTypeDefinition());
51     }
52
53     @Test
54     public void testConvertProperty() {
55         SchemaDefinition schema = new SchemaDefinition();
56         schema.setProperty(property);
57         
58         property.setSchema(schema);
59         
60         PropertyConvertor.getInstance().convertProperty(dataTypes, property, PropertyConvertor.PropertyType.PROPERTY);
61     }
62
63     @Test
64     public void convertPropertyWhenValueAndDefaultNull() {
65         ToscaProperty prop = PropertyConvertor.getInstance().convertProperty(dataTypes, property, PropertyConvertor.PropertyType.PROPERTY);
66         assertNotNull(prop);
67         assertNull(prop.getDefaultp());
68     }
69
70     @Test
71     public void convertPropertyWhenValueNullAndDefaultNotEmpty() {
72         final String def = "1";
73         property.setDefaultValue(def);
74         ToscaProperty result = PropertyConvertor.getInstance().convertProperty(dataTypes, property, PropertyConvertor.PropertyType.PROPERTY);
75         assertNotNull(result);
76         assertEquals(Integer.valueOf(def), result.getDefaultp());
77     }
78
79     @Test
80     public void convertPropertiesWhenValueAndDefaultNullInOne() {
81         PropertyDefinition property1 = new PropertyDefinition();
82         property1.setName("otherProperty");
83         property1.setType(ToscaPropertyType.INTEGER.getType());
84         property1.setDefaultValue("2");
85         dataTypes.put(property1.getName(), new DataTypeDefinition());
86         Resource resource = new Resource();
87         List<PropertyDefinition> properties = new ArrayList();
88         properties.add(property);
89         properties.add(property1);
90         resource.setProperties(properties);
91         Either<ToscaNodeType, ToscaError> result = PropertyConvertor.getInstance().convertProperties(resource, new ToscaNodeType(), dataTypes);
92         assertTrue(result.isLeft());
93         assertEquals(2, result.left().value().getProperties().size());
94         int cnt = 0;
95         for (Iterator<ToscaProperty> it = result.left().value().getProperties().values().iterator(); it.hasNext(); ) {
96             ToscaProperty prop = it.next();
97             if (prop.getDefaultp() == null) {
98                 cnt++;
99             }
100         }
101         assertEquals(1, cnt);
102     }
103
104     @Test
105     public void convertPropertiesWhenValueAndDefaultExist() {
106         PropertyDefinition property1 = new PropertyDefinition();
107         property1.setName("otherProperty");
108         property1.setType(ToscaPropertyType.INTEGER.getType());
109         property1.setDefaultValue("2");
110         property.setDefaultValue("1");
111         dataTypes.put(property1.getName(), new DataTypeDefinition());
112         Resource resource = new Resource();
113         List<PropertyDefinition> properties = new ArrayList();
114         properties.add(property);
115         properties.add(property1);
116         resource.setProperties(properties);
117         Either<ToscaNodeType, ToscaError> result = PropertyConvertor.getInstance().convertProperties(resource, new ToscaNodeType(), dataTypes);
118         assertTrue(result.isLeft());
119         assertEquals(2, result.left().value().getProperties().size());
120         for (Iterator<ToscaProperty> it = result.left().value().getProperties().values().iterator(); it.hasNext(); ) {
121             ToscaProperty prop = it.next();
122             assertNotNull(prop.getDefaultp());
123         }
124     }
125
126     @Test
127     public void convertPropertiesWhenValueAndDefaultNullInAll() {
128         PropertyDefinition property1 = new PropertyDefinition();
129         property1.setName("otherProperty");
130         property1.setType(ToscaPropertyType.INTEGER.getType());
131         dataTypes.put(property1.getName(), new DataTypeDefinition());
132         Resource resource = new Resource();
133         List<PropertyDefinition> properties = new ArrayList();
134         properties.add(property);
135         properties.add(property1);
136         resource.setProperties(properties);
137         Either<ToscaNodeType, ToscaError> result = PropertyConvertor.getInstance().convertProperties(resource, new ToscaNodeType(), dataTypes);
138         assertTrue(result.isLeft());
139         assertEquals(2, result.left().value().getProperties().size());
140         for (Iterator<ToscaProperty> it = result.left().value().getProperties().values().iterator(); it.hasNext(); ) {
141             ToscaProperty prop = it.next();
142             assertNull(prop.getDefaultp());
143         }
144      }
145
146     @Test
147     public void convertPropertyWhichStartsWithSemiColon() throws Exception {
148         PropertyDefinition property1 = new PropertyDataDefinitionBuilder()
149                 .setDefaultValue("::")
150                 .setType(ToscaPropertyType.STRING.getType())
151                 .build();
152         ToscaProperty toscaProperty = PropertyConvertor.getInstance().convertProperty(Collections.emptyMap(), property1, PropertyConvertor.PropertyType.PROPERTY);
153         assertThat(toscaProperty.getDefaultp()).isEqualTo("::");
154     }
155
156     @Test
157     public void convertPropertyWhichStartsWithSlash() throws Exception {
158         PropertyDefinition property1 = new PropertyDataDefinitionBuilder()
159                 .setDefaultValue("/")
160                 .setType(ToscaPropertyType.STRING.getType())
161                 .build();
162         ToscaProperty toscaProperty = PropertyConvertor.getInstance().convertProperty(Collections.emptyMap(), property1, PropertyConvertor.PropertyType.PROPERTY);
163         assertThat(toscaProperty.getDefaultp()).isEqualTo("/");
164     }
165     
166     @Test
167     public void testConvertToToscaObject() {
168                 dataTypes.put(ToscaPropertyType.Root.getType(), new DataTypeDefinition());
169         
170         PropertyConvertor.getInstance().convertToToscaObject(ToscaPropertyType.Root.getType(), "", "innerType", dataTypes,true);
171     }
172     
173     @Test
174     public void testConvertToToscaObjectWhenPropertyTypeAndInnerTypeNull() {
175         dataTypes.put(ToscaPropertyType.Root.getType(), new DataTypeDefinition());
176         
177         PropertyConvertor.getInstance().convertToToscaObject(null, "value", null, dataTypes,true);
178     }
179     
180     @Test
181     public void testConvertToToscaObjectWhenIsScalarTypeIsNotNull() {
182         DataTypeDefinition def = new DataTypeDefinition();
183         def.setName("integer");
184         dataTypes.put("type", def);
185         
186         PropertyConvertor.getInstance().convertToToscaObject("type", "value", "innerType", dataTypes,true);
187     }
188
189 }