2 * ============LICENSE_START=======================================================
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
11 * http://www.apache.org/licenses/LICENSE-2.0
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=========================================================
21 package org.openecomp.sdc.be.tosca;
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;
35 import java.util.ArrayList;
36 import java.util.Collections;
37 import java.util.HashMap;
38 import java.util.Iterator;
39 import java.util.List;
42 import static org.assertj.core.api.Assertions.assertThat;
43 import static org.junit.Assert.assertEquals;
44 import static org.junit.Assert.assertNotNull;
45 import static org.junit.Assert.assertNull;
46 import static org.junit.Assert.assertTrue;
48 public class PropertyConvertorTest {
49 private PropertyDefinition property;
50 Map<String, DataTypeDefinition> dataTypes;
54 property = new PropertyDefinition();
55 property.setName("myProperty");
56 property.setType(ToscaPropertyType.INTEGER.getType());
57 dataTypes = new HashMap();
58 dataTypes.put(property.getName(), new DataTypeDefinition());
62 public void testConvertProperty() {
63 SchemaDefinition schema = new SchemaDefinition();
64 schema.setProperty(property);
66 property.setSchema(schema);
68 PropertyConvertor.getInstance().convertProperty(dataTypes, property, PropertyConvertor.PropertyType.PROPERTY);
72 public void convertPropertyWhenValueAndDefaultNull() {
73 ToscaProperty prop = PropertyConvertor.getInstance().convertProperty(dataTypes, property, PropertyConvertor.PropertyType.PROPERTY);
75 assertNull(prop.getDefaultp());
79 public void convertPropertyWhenValueNullAndDefaultNotEmpty() {
80 final String def = "1";
81 property.setDefaultValue(def);
82 ToscaProperty result = PropertyConvertor.getInstance().convertProperty(dataTypes, property, PropertyConvertor.PropertyType.PROPERTY);
83 assertNotNull(result);
84 assertEquals(Integer.valueOf(def), result.getDefaultp());
88 public void convertPropertiesWhenValueAndDefaultNullInOne() {
89 PropertyDefinition property1 = new PropertyDefinition();
90 property1.setName("otherProperty");
91 property1.setType(ToscaPropertyType.INTEGER.getType());
92 property1.setDefaultValue("2");
93 dataTypes.put(property1.getName(), new DataTypeDefinition());
94 Resource resource = new Resource();
95 List<PropertyDefinition> properties = new ArrayList();
96 properties.add(property);
97 properties.add(property1);
98 resource.setProperties(properties);
99 Either<ToscaNodeType, ToscaError> result = PropertyConvertor.getInstance().convertProperties(resource, new ToscaNodeType(), dataTypes);
100 assertTrue(result.isLeft());
101 assertEquals(2, result.left().value().getProperties().size());
103 for (Iterator<ToscaProperty> it = result.left().value().getProperties().values().iterator(); it.hasNext(); ) {
104 ToscaProperty prop = it.next();
105 if (prop.getDefaultp() == null) {
109 assertEquals(1, cnt);
113 public void convertPropertiesWhenValueAndDefaultExist() {
114 PropertyDefinition property1 = new PropertyDefinition();
115 property1.setName("otherProperty");
116 property1.setType(ToscaPropertyType.INTEGER.getType());
117 property1.setDefaultValue("2");
118 property.setDefaultValue("1");
119 dataTypes.put(property1.getName(), new DataTypeDefinition());
120 Resource resource = new Resource();
121 List<PropertyDefinition> properties = new ArrayList();
122 properties.add(property);
123 properties.add(property1);
124 resource.setProperties(properties);
125 Either<ToscaNodeType, ToscaError> result = PropertyConvertor.getInstance().convertProperties(resource, new ToscaNodeType(), dataTypes);
126 assertTrue(result.isLeft());
127 assertEquals(2, result.left().value().getProperties().size());
128 for (Iterator<ToscaProperty> it = result.left().value().getProperties().values().iterator(); it.hasNext(); ) {
129 ToscaProperty prop = it.next();
130 assertNotNull(prop.getDefaultp());
135 public void convertPropertiesWhenValueAndDefaultNullInAll() {
136 PropertyDefinition property1 = new PropertyDefinition();
137 property1.setName("otherProperty");
138 property1.setType(ToscaPropertyType.INTEGER.getType());
139 dataTypes.put(property1.getName(), new DataTypeDefinition());
140 Resource resource = new Resource();
141 List<PropertyDefinition> properties = new ArrayList();
142 properties.add(property);
143 properties.add(property1);
144 resource.setProperties(properties);
145 Either<ToscaNodeType, ToscaError> result = PropertyConvertor.getInstance().convertProperties(resource, new ToscaNodeType(), dataTypes);
146 assertTrue(result.isLeft());
147 assertEquals(2, result.left().value().getProperties().size());
148 for (Iterator<ToscaProperty> it = result.left().value().getProperties().values().iterator(); it.hasNext(); ) {
149 ToscaProperty prop = it.next();
150 assertNull(prop.getDefaultp());
155 public void convertPropertyWhichStartsWithSemiColon() throws Exception {
156 PropertyDefinition property1 = new PropertyDataDefinitionBuilder()
157 .setDefaultValue("::")
158 .setType(ToscaPropertyType.STRING.getType())
160 ToscaProperty toscaProperty = PropertyConvertor.getInstance().convertProperty(Collections.emptyMap(), property1, PropertyConvertor.PropertyType.PROPERTY);
161 assertThat(toscaProperty.getDefaultp()).isEqualTo("::");
165 public void convertPropertyWhichStartsWithSlash() throws Exception {
166 PropertyDefinition property1 = new PropertyDataDefinitionBuilder()
167 .setDefaultValue("/")
168 .setType(ToscaPropertyType.STRING.getType())
170 ToscaProperty toscaProperty = PropertyConvertor.getInstance().convertProperty(Collections.emptyMap(), property1, PropertyConvertor.PropertyType.PROPERTY);
171 assertThat(toscaProperty.getDefaultp()).isEqualTo("/");