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;
37 import static org.assertj.core.api.Assertions.assertThat;
38 import static org.junit.Assert.*;
40 public class PropertyConvertorTest {
41 private PropertyDefinition property;
42 Map<String, DataTypeDefinition> dataTypes;
46 property = new PropertyDefinition();
47 property.setName("myProperty");
48 property.setType(ToscaPropertyType.INTEGER.getType());
49 dataTypes = new HashMap();
50 dataTypes.put(property.getName(), new DataTypeDefinition());
54 public void testConvertProperty() {
55 SchemaDefinition schema = new SchemaDefinition();
56 schema.setProperty(property);
58 property.setSchema(schema);
60 PropertyConvertor.getInstance().convertProperty(dataTypes, property, PropertyConvertor.PropertyType.PROPERTY);
64 public void convertPropertyWhenValueAndDefaultNull() {
65 ToscaProperty prop = PropertyConvertor.getInstance().convertProperty(dataTypes, property, PropertyConvertor.PropertyType.PROPERTY);
67 assertNull(prop.getDefaultp());
71 public void convertPropertyWhenValueNullAndDefaultNotEmpty() {
72 final String def = "1";
73 property.setDefaultValue(def);
74 ToscaProperty result = PropertyConvertor.getInstance().convertProperty(dataTypes, property, PropertyConvertor.PropertyType.PROPERTY);
75 assertNotNull(result);
76 assertEquals(Integer.valueOf(def), result.getDefaultp());
80 public void convertPropertiesWhenValueAndDefaultNullInOne() {
81 PropertyDefinition property1 = new PropertyDefinition();
82 property1.setName("otherProperty");
83 property1.setType(ToscaPropertyType.INTEGER.getType());
84 property1.setDefaultValue("2");
85 dataTypes.put(property1.getName(), new DataTypeDefinition());
86 Resource resource = new Resource();
87 List<PropertyDefinition> properties = new ArrayList();
88 properties.add(property);
89 properties.add(property1);
90 resource.setProperties(properties);
91 Either<ToscaNodeType, ToscaError> result = PropertyConvertor.getInstance().convertProperties(resource, new ToscaNodeType(), dataTypes);
92 assertTrue(result.isLeft());
93 assertEquals(2, result.left().value().getProperties().size());
95 for (Iterator<ToscaProperty> it = result.left().value().getProperties().values().iterator(); it.hasNext(); ) {
96 ToscaProperty prop = it.next();
97 if (prop.getDefaultp() == null) {
101 assertEquals(1, cnt);
105 public void convertPropertiesWhenValueAndDefaultExist() {
106 PropertyDefinition property1 = new PropertyDefinition();
107 property1.setName("otherProperty");
108 property1.setType(ToscaPropertyType.INTEGER.getType());
109 property1.setDefaultValue("2");
110 property.setDefaultValue("1");
111 dataTypes.put(property1.getName(), new DataTypeDefinition());
112 Resource resource = new Resource();
113 List<PropertyDefinition> properties = new ArrayList();
114 properties.add(property);
115 properties.add(property1);
116 resource.setProperties(properties);
117 Either<ToscaNodeType, ToscaError> result = PropertyConvertor.getInstance().convertProperties(resource, new ToscaNodeType(), dataTypes);
118 assertTrue(result.isLeft());
119 assertEquals(2, result.left().value().getProperties().size());
120 for (Iterator<ToscaProperty> it = result.left().value().getProperties().values().iterator(); it.hasNext(); ) {
121 ToscaProperty prop = it.next();
122 assertNotNull(prop.getDefaultp());
127 public void convertPropertiesWhenValueAndDefaultNullInAll() {
128 PropertyDefinition property1 = new PropertyDefinition();
129 property1.setName("otherProperty");
130 property1.setType(ToscaPropertyType.INTEGER.getType());
131 dataTypes.put(property1.getName(), new DataTypeDefinition());
132 Resource resource = new Resource();
133 List<PropertyDefinition> properties = new ArrayList();
134 properties.add(property);
135 properties.add(property1);
136 resource.setProperties(properties);
137 Either<ToscaNodeType, ToscaError> result = PropertyConvertor.getInstance().convertProperties(resource, new ToscaNodeType(), dataTypes);
138 assertTrue(result.isLeft());
139 assertEquals(2, result.left().value().getProperties().size());
140 for (Iterator<ToscaProperty> it = result.left().value().getProperties().values().iterator(); it.hasNext(); ) {
141 ToscaProperty prop = it.next();
142 assertNull(prop.getDefaultp());
147 public void convertPropertyWhichStartsWithSemiColon() throws Exception {
148 PropertyDefinition property1 = new PropertyDataDefinitionBuilder()
149 .setDefaultValue("::")
150 .setType(ToscaPropertyType.STRING.getType())
152 ToscaProperty toscaProperty = PropertyConvertor.getInstance().convertProperty(Collections.emptyMap(), property1, PropertyConvertor.PropertyType.PROPERTY);
153 assertThat(toscaProperty.getDefaultp()).isEqualTo("::");
157 public void convertPropertyWhichStartsWithSlash() throws Exception {
158 PropertyDefinition property1 = new PropertyDataDefinitionBuilder()
159 .setDefaultValue("/")
160 .setType(ToscaPropertyType.STRING.getType())
162 ToscaProperty toscaProperty = PropertyConvertor.getInstance().convertProperty(Collections.emptyMap(), property1, PropertyConvertor.PropertyType.PROPERTY);
163 assertThat(toscaProperty.getDefaultp()).isEqualTo("/");
167 public void testConvertToToscaObject() {
168 dataTypes.put(ToscaPropertyType.Root.getType(), new DataTypeDefinition());
170 PropertyConvertor.getInstance().convertToToscaObject(ToscaPropertyType.Root.getType(), "", "innerType", dataTypes,true);
174 public void testConvertToToscaObjectWhenPropertyTypeAndInnerTypeNull() {
175 dataTypes.put(ToscaPropertyType.Root.getType(), new DataTypeDefinition());
177 PropertyConvertor.getInstance().convertToToscaObject(null, "value", null, dataTypes,true);
181 public void testConvertToToscaObjectWhenIsScalarTypeIsNotNull() {
182 DataTypeDefinition def = new DataTypeDefinition();
183 def.setName("integer");
184 dataTypes.put("type", def);
186 PropertyConvertor.getInstance().convertToToscaObject("type", "value", "innerType", dataTypes,true);