Milestone updates
[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 static org.junit.jupiter.api.Assertions.assertEquals;
24 import static org.junit.jupiter.api.Assertions.assertNotNull;
25 import static org.junit.jupiter.api.Assertions.assertNull;
26 import static org.junit.jupiter.api.Assertions.assertTrue;
27
28 import fj.data.Either;
29 import java.util.ArrayList;
30 import java.util.Arrays;
31 import java.util.Collections;
32 import java.util.HashMap;
33 import java.util.List;
34 import java.util.Map;
35 import org.junit.jupiter.api.BeforeEach;
36 import org.junit.jupiter.api.Test;
37 import org.mockito.InjectMocks;
38 import org.mockito.MockitoAnnotations;
39 import org.openecomp.sdc.be.components.utils.PropertyDataDefinitionBuilder;
40 import org.openecomp.sdc.be.datatypes.elements.SchemaDefinition;
41 import org.openecomp.sdc.be.datatypes.enums.ConstraintType;
42 import org.openecomp.sdc.be.model.DataTypeDefinition;
43 import org.openecomp.sdc.be.model.PropertyConstraint;
44 import org.openecomp.sdc.be.model.PropertyDefinition;
45 import org.openecomp.sdc.be.model.Resource;
46 import org.openecomp.sdc.be.model.tosca.ToscaPropertyType;
47 import org.openecomp.sdc.be.model.tosca.constraints.EqualConstraint;
48 import org.openecomp.sdc.be.model.tosca.constraints.GreaterOrEqualConstraint;
49 import org.openecomp.sdc.be.model.tosca.constraints.GreaterThanConstraint;
50 import org.openecomp.sdc.be.model.tosca.constraints.InRangeConstraint;
51 import org.openecomp.sdc.be.model.tosca.constraints.LengthConstraint;
52 import org.openecomp.sdc.be.model.tosca.constraints.LessOrEqualConstraint;
53 import org.openecomp.sdc.be.model.tosca.constraints.LessThanConstraint;
54 import org.openecomp.sdc.be.model.tosca.constraints.MaxLengthConstraint;
55 import org.openecomp.sdc.be.model.tosca.constraints.MinLengthConstraint;
56 import org.openecomp.sdc.be.model.tosca.constraints.PatternConstraint;
57 import org.openecomp.sdc.be.model.tosca.constraints.ValidValuesConstraint;
58 import org.openecomp.sdc.be.tosca.PropertyConvertor.PropertyType;
59 import org.openecomp.sdc.be.tosca.model.ToscaNodeType;
60 import org.openecomp.sdc.be.tosca.model.ToscaProperty;
61 import org.openecomp.sdc.be.tosca.model.ToscaPropertyConstraint;
62
63 class PropertyConvertorTest {
64
65     private PropertyDefinition property;
66     private Map<String, DataTypeDefinition> dataTypes;
67
68     @InjectMocks
69     private PropertyConvertor propertyConvertor;
70
71     @BeforeEach
72     public void setUp() {
73         MockitoAnnotations.openMocks(this);
74         property = new PropertyDefinition();
75         property.setName("myProperty");
76         property.setType(ToscaPropertyType.INTEGER.getType());
77         dataTypes = new HashMap<>();
78         dataTypes.put(property.getName(), new DataTypeDefinition());
79     }
80
81     @Test
82     void testConvertProperty() {
83         SchemaDefinition schema = new SchemaDefinition();
84         schema.setProperty(property);
85         property.setSchema(schema);
86
87         assertNotNull(propertyConvertor.convertProperty(dataTypes, property, PropertyType.PROPERTY));
88     }
89
90     @Test
91     void testConvertPropertyWithEqualConstraint() {
92         assertTrue(testConstraints(new EqualConstraint(123), ConstraintType.EQUAL, true));
93     }
94
95     @Test
96     void testConvertPropertyWithGreaterOrEqualConstraint() {
97         assertTrue(testConstraints(new GreaterOrEqualConstraint<>(123), ConstraintType.GREATER_OR_EQUAL, true));
98     }
99
100     @Test
101     void testConvertPropertyWithGreaterThanConstraint() {
102         assertTrue(testConstraints(new GreaterThanConstraint<>(123), ConstraintType.GREATER_THAN, true));
103     }
104
105     @Test
106     void testConvertPropertyWithLessOrEqualConstraint() {
107         assertTrue(testConstraints(new LessOrEqualConstraint<>(123), ConstraintType.LESS_OR_EQUAL, true));
108     }
109
110     @Test
111     void testConvertPropertyWithLessThanConstraint() {
112         assertTrue(testConstraints(new LessThanConstraint<>(123), ConstraintType.LESS_THAN, true));
113     }
114
115     @Test
116     void testConvertPropertyWithInRangeConstraint() {
117         assertTrue(testConstraints(new InRangeConstraint(Arrays.asList(123, 345)), ConstraintType.IN_RANGE, false));
118     }
119
120     @Test
121     void testConvertPropertyWithValidValuesConstraint() {
122         assertTrue(testConstraints(new ValidValuesConstraint(Arrays.asList(123, 345)), ConstraintType.VALID_VALUES, false));
123     }
124
125     @Test
126     void testConvertPropertyWithLengthConstraint() {
127         assertTrue(testConstraints(new LengthConstraint(), ConstraintType.LENGTH, false));
128     }
129
130     @Test
131     void testConvertPropertyWithMaxLengthConstraint() {
132         assertTrue(testConstraints(new MaxLengthConstraint(12), ConstraintType.MAX_LENGTH, false));
133     }
134
135     @Test
136     void testConvertPropertyWithMinLengthConstraint() {
137         assertTrue(testConstraints(new MinLengthConstraint(1), ConstraintType.MIN_LENGTH, false));
138     }
139
140     @Test
141     void testConvertPropertyWithPatternConstraint() {
142         assertTrue(testConstraints(new PatternConstraint("[a-z]"), ConstraintType.PATTERN, false));
143     }
144
145     @Test
146     void convertPropertyWhenValueAndDefaultNull() {
147         ToscaProperty prop = propertyConvertor.convertProperty(dataTypes, property, PropertyConvertor.PropertyType.PROPERTY);
148         assertNotNull(prop);
149         assertNull(prop.getDefaultp());
150     }
151
152     @Test
153     void convertPropertyWhenValueNullAndDefaultNotEmpty() {
154         final String def = "1";
155         property.setDefaultValue(def);
156         ToscaProperty result = propertyConvertor.convertProperty(dataTypes, property, PropertyConvertor.PropertyType.PROPERTY);
157         assertNotNull(result);
158         assertEquals(Integer.valueOf(def), result.getDefaultp());
159     }
160
161     @Test
162     void convertPropertyWithMetadata() {
163         Map<String, String> metadata = new HashMap<>();
164         metadata.put("key1", "value");
165         property.setMetadata(metadata);
166         ToscaProperty result = propertyConvertor.convertProperty(dataTypes, property, PropertyConvertor.PropertyType.PROPERTY);
167         assertNotNull(result);
168         assertEquals(metadata, result.getMetadata());
169     }
170
171     @Test
172     void convertPropertiesWhenValueAndDefaultNullInOne() {
173         PropertyDefinition property1 = new PropertyDefinition();
174         property1.setName("otherProperty");
175         property1.setType(ToscaPropertyType.INTEGER.getType());
176         property1.setDefaultValue("2");
177         dataTypes.put(property1.getName(), new DataTypeDefinition());
178         Resource resource = new Resource();
179         List<PropertyDefinition> properties = new ArrayList<>();
180         properties.add(property);
181         properties.add(property1);
182         resource.setProperties(properties);
183         Either<ToscaNodeType, ToscaError> result = propertyConvertor.convertProperties(resource, new ToscaNodeType(), dataTypes);
184         assertTrue(result.isLeft());
185         assertEquals(2, result.left().value().getProperties().size());
186         int cnt = 0;
187         for (ToscaProperty prop : result.left().value().getProperties().values()) {
188             if (prop.getDefaultp() == null) {
189                 cnt++;
190             }
191         }
192         assertEquals(1, cnt);
193     }
194
195     @Test
196     void convertPropertiesWhenValueAndDefaultExist() {
197         PropertyDefinition property1 = new PropertyDefinition();
198         property1.setName("otherProperty");
199         property1.setType(ToscaPropertyType.INTEGER.getType());
200         property1.setDefaultValue("2");
201         property.setDefaultValue("1");
202         dataTypes.put(property1.getName(), new DataTypeDefinition());
203         Resource resource = new Resource();
204         List<PropertyDefinition> properties = new ArrayList<>();
205         properties.add(property);
206         properties.add(property1);
207         resource.setProperties(properties);
208         Either<ToscaNodeType, ToscaError> result = propertyConvertor.convertProperties(resource, new ToscaNodeType(), dataTypes);
209         assertTrue(result.isLeft());
210         assertEquals(2, result.left().value().getProperties().size());
211         for (ToscaProperty prop : result.left().value().getProperties().values()) {
212             assertNotNull(prop.getDefaultp());
213         }
214     }
215
216     @Test
217     void convertPropertiesWhenValueAndDefaultNullInAll() {
218         PropertyDefinition property1 = new PropertyDefinition();
219         property1.setName("otherProperty");
220         property1.setType(ToscaPropertyType.INTEGER.getType());
221         dataTypes.put(property1.getName(), new DataTypeDefinition());
222         Resource resource = new Resource();
223         List<PropertyDefinition> properties = new ArrayList<>();
224         properties.add(property);
225         properties.add(property1);
226         resource.setProperties(properties);
227         Either<ToscaNodeType, ToscaError> result = propertyConvertor.convertProperties(resource, new ToscaNodeType(), dataTypes);
228         assertTrue(result.isLeft());
229         assertEquals(2, result.left().value().getProperties().size());
230         for (ToscaProperty prop : result.left().value().getProperties().values()) {
231             assertNull(prop.getDefaultp());
232         }
233     }
234
235     @Test
236     void convertPropertyWhichStartsWithSemiColon() {
237         final PropertyDefinition property = new PropertyDataDefinitionBuilder()
238             .setDefaultValue("::")
239             .setType(ToscaPropertyType.STRING.getType())
240             .build();
241         final ToscaProperty toscaProperty =
242             propertyConvertor.convertProperty(Collections.emptyMap(), property, PropertyConvertor.PropertyType.PROPERTY);
243         assertEquals("::", toscaProperty.getDefaultp());
244     }
245
246     @Test
247     void convertPropertyWhichStartsWithSlash() {
248         final PropertyDefinition property = new PropertyDataDefinitionBuilder()
249             .setDefaultValue("/")
250             .setType(ToscaPropertyType.STRING.getType())
251             .build();
252         final ToscaProperty toscaProperty =
253             propertyConvertor.convertProperty(Collections.emptyMap(), property, PropertyConvertor.PropertyType.PROPERTY);
254         assertEquals("/", toscaProperty.getDefaultp());
255     }
256
257     @Test
258     void convertPropertyWithYamlValue() {
259         final PropertyDefinition property = new PropertyDataDefinitionBuilder()
260             .setDefaultValue("{concat: [ get_input: service_name, '--', 'WirelessService']}")
261             .setType(ToscaPropertyType.STRING.getType())
262             .build();
263         final ToscaProperty toscaProperty =
264             propertyConvertor.convertProperty(Collections.emptyMap(), property, PropertyConvertor.PropertyType.PROPERTY);
265         assertTrue(toscaProperty.getDefaultp() instanceof Map);
266         assertTrue(((Map) toscaProperty.getDefaultp()).get("concat") instanceof List);
267         assertEquals(3, ((List) ((Map) toscaProperty.getDefaultp()).get("concat")).size());
268     }
269
270     private boolean testConstraints(PropertyConstraint propertyConstraint, ConstraintType constraintType, boolean checkComparable) {
271         property.setConstraints(Collections.singletonList(propertyConstraint));
272
273         ToscaProperty toscaProperty = propertyConvertor.convertProperty(dataTypes, property, PropertyType.PROPERTY);
274         assertNotNull(toscaProperty);
275         List<ToscaPropertyConstraint> constraints = toscaProperty.getConstraints();
276         assertNotNull(constraints);
277         ToscaPropertyConstraint constraint = constraints.get(0);
278         assertNotNull(constraint);
279         ConstraintType actualConstraintType = constraint.getConstraintType();
280         assertEquals(constraintType, actualConstraintType);
281         if (checkComparable) {
282             assertTrue(actualConstraintType.isComparable());
283         }
284         return true;
285     }
286
287 }