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