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