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.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;
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;
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;
51 @RunWith(MockitoJUnitRunner.class)
52 public class PropertyConvertorTest {
53 private PropertyDefinition property;
54 private Map<String, DataTypeDefinition> dataTypes;
57 private PropertyConvertor propertyConvertor;
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());
69 public void testConvertProperty() {
70 SchemaDefinition schema = new SchemaDefinition();
71 schema.setProperty(property);
73 property.setSchema(schema);
75 propertyConvertor.convertProperty(dataTypes, property, PropertyConvertor.PropertyType.PROPERTY);
79 public void convertPropertyWhenValueAndDefaultNull() {
80 ToscaProperty prop = propertyConvertor.convertProperty(dataTypes, property, PropertyConvertor.PropertyType.PROPERTY);
82 assertNull(prop.getDefaultp());
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());
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());
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());
120 for (ToscaProperty prop : result.left().value().getProperties().values()) {
121 if (prop.getDefaultp() == null) {
125 assertEquals(1, cnt);
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());
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());
169 public void convertPropertyWhichStartsWithSemiColon() throws Exception {
170 PropertyDefinition property1 = new PropertyDataDefinitionBuilder()
171 .setDefaultValue("::")
172 .setType(ToscaPropertyType.STRING.getType())
174 ToscaProperty toscaProperty = propertyConvertor.convertProperty(Collections.emptyMap(), property1, PropertyConvertor.PropertyType.PROPERTY);
175 assertThat(toscaProperty.getDefaultp()).isEqualTo("::");
179 public void convertPropertyWhichStartsWithSlash() throws Exception {
180 PropertyDefinition property1 = new PropertyDataDefinitionBuilder()
181 .setDefaultValue("/")
182 .setType(ToscaPropertyType.STRING.getType())
184 ToscaProperty toscaProperty = propertyConvertor.convertProperty(Collections.emptyMap(), property1, PropertyConvertor.PropertyType.PROPERTY);
185 assertThat(toscaProperty.getDefaultp()).isEqualTo("/");