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 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;
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;
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;
47 class PropertyConvertorTest {
49 private PropertyDefinition property;
50 private Map<String, DataTypeDefinition> dataTypes;
53 private PropertyConvertor propertyConvertor;
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());
66 void testConvertProperty() {
67 SchemaDefinition schema = new SchemaDefinition();
68 schema.setProperty(property);
70 property.setSchema(schema);
72 propertyConvertor.convertProperty(dataTypes, property, PropertyConvertor.PropertyType.PROPERTY);
76 void convertPropertyWhenValueAndDefaultNull() {
77 ToscaProperty prop = propertyConvertor.convertProperty(dataTypes, property, PropertyConvertor.PropertyType.PROPERTY);
79 assertNull(prop.getDefaultp());
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());
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());
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());
117 for (ToscaProperty prop : result.left().value().getProperties().values()) {
118 if (prop.getDefaultp() == null) {
122 assertEquals(1, cnt);
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());
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());
166 void convertPropertyWhichStartsWithSemiColon() {
167 final PropertyDefinition property = new PropertyDataDefinitionBuilder()
168 .setDefaultValue("::")
169 .setType(ToscaPropertyType.STRING.getType())
171 final ToscaProperty toscaProperty =
172 propertyConvertor.convertProperty(Collections.emptyMap(), property, PropertyConvertor.PropertyType.PROPERTY);
173 assertEquals("::", toscaProperty.getDefaultp());
177 void convertPropertyWhichStartsWithSlash() {
178 final PropertyDefinition property = new PropertyDataDefinitionBuilder()
179 .setDefaultValue("/")
180 .setType(ToscaPropertyType.STRING.getType())
182 final ToscaProperty toscaProperty =
183 propertyConvertor.convertProperty(Collections.emptyMap(), property, PropertyConvertor.PropertyType.PROPERTY);
184 assertEquals("/", toscaProperty.getDefaultp());