05ff278fc0788ea19871d5e09adf661687c85f0f
[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 convertPropertyWithMetadata() {
96         Map<String, String> metadata = new HashMap<>();
97         metadata.put("key1", "value");
98         property.setMetadata(metadata);
99         ToscaProperty result = propertyConvertor.convertProperty(dataTypes, property, PropertyConvertor.PropertyType.PROPERTY);
100         assertNotNull(result);
101         assertEquals(metadata, result.getMetadata());
102     }
103
104     @Test
105     public void convertPropertiesWhenValueAndDefaultNullInOne() {
106         PropertyDefinition property1 = new PropertyDefinition();
107         property1.setName("otherProperty");
108         property1.setType(ToscaPropertyType.INTEGER.getType());
109         property1.setDefaultValue("2");
110         dataTypes.put(property1.getName(), new DataTypeDefinition());
111         Resource resource = new Resource();
112         List<PropertyDefinition> properties = new ArrayList<>();
113         properties.add(property);
114         properties.add(property1);
115         resource.setProperties(properties);
116         Either<ToscaNodeType, ToscaError> result = propertyConvertor.convertProperties(resource, new ToscaNodeType(), dataTypes);
117         assertTrue(result.isLeft());
118         assertEquals(2, result.left().value().getProperties().size());
119         int cnt = 0;
120         for (ToscaProperty prop : result.left().value().getProperties().values()) {
121             if (prop.getDefaultp() == null) {
122                 cnt++;
123             }
124         }
125         assertEquals(1, cnt);
126     }
127
128     @Test
129     public void convertPropertiesWhenValueAndDefaultExist() {
130         PropertyDefinition property1 = new PropertyDefinition();
131         property1.setName("otherProperty");
132         property1.setType(ToscaPropertyType.INTEGER.getType());
133         property1.setDefaultValue("2");
134         property.setDefaultValue("1");
135         dataTypes.put(property1.getName(), new DataTypeDefinition());
136         Resource resource = new Resource();
137         List<PropertyDefinition> properties = new ArrayList<>();
138         properties.add(property);
139         properties.add(property1);
140         resource.setProperties(properties);
141         Either<ToscaNodeType, ToscaError> result = propertyConvertor.convertProperties(resource, new ToscaNodeType(), dataTypes);
142         assertTrue(result.isLeft());
143         assertEquals(2, result.left().value().getProperties().size());
144         for (ToscaProperty prop : result.left().value().getProperties().values()) {
145             assertNotNull(prop.getDefaultp());
146         }
147     }
148
149     @Test
150     public void convertPropertiesWhenValueAndDefaultNullInAll() {
151         PropertyDefinition property1 = new PropertyDefinition();
152         property1.setName("otherProperty");
153         property1.setType(ToscaPropertyType.INTEGER.getType());
154         dataTypes.put(property1.getName(), new DataTypeDefinition());
155         Resource resource = new Resource();
156         List<PropertyDefinition> properties = new ArrayList<>();
157         properties.add(property);
158         properties.add(property1);
159         resource.setProperties(properties);
160         Either<ToscaNodeType, ToscaError> result = propertyConvertor.convertProperties(resource, new ToscaNodeType(), dataTypes);
161         assertTrue(result.isLeft());
162         assertEquals(2, result.left().value().getProperties().size());
163         for (ToscaProperty prop : result.left().value().getProperties().values()) {
164             assertNull(prop.getDefaultp());
165         }
166      }
167
168     @Test
169     public void convertPropertyWhichStartsWithSemiColon() 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     @Test
179     public void convertPropertyWhichStartsWithSlash() throws Exception {
180         PropertyDefinition property1 = new PropertyDataDefinitionBuilder()
181                 .setDefaultValue("/")
182                 .setType(ToscaPropertyType.STRING.getType())
183                 .build();
184         ToscaProperty toscaProperty = propertyConvertor.convertProperty(Collections.emptyMap(), property1, PropertyConvertor.PropertyType.PROPERTY);
185         assertThat(toscaProperty.getDefaultp()).isEqualTo("/");
186     }
187
188 }