Fix PropertyConvertor initialization (use spring)
[sdc.git] / catalog-be / src / test / java / org / openecomp / sdc / be / tosca / PropertyConvertorTest.java
index c77c0f1..d7c53d5 100644 (file)
@@ -1,18 +1,46 @@
+/*-
+ * ============LICENSE_START=======================================================
+ * SDC
+ * ================================================================================
+ * Copyright (C) 2019 AT&T Intellectual Property. All rights reserved.
+ * ================================================================================
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ * 
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ * 
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ * ============LICENSE_END=========================================================
+ */
+
 package org.openecomp.sdc.be.tosca;
 
+import static org.assertj.core.api.Assertions.assertThat;
 import static org.junit.Assert.assertEquals;
 import static org.junit.Assert.assertNotNull;
 import static org.junit.Assert.assertNull;
 import static org.junit.Assert.assertTrue;
 
+import fj.data.Either;
 import java.util.ArrayList;
+import java.util.Collections;
 import java.util.HashMap;
 import java.util.Iterator;
 import java.util.List;
 import java.util.Map;
-
 import org.junit.Before;
 import org.junit.Test;
+import org.junit.runner.RunWith;
+import org.mockito.InjectMocks;
+import org.mockito.Mock;
+import org.mockito.junit.MockitoJUnitRunner;
+import org.openecomp.sdc.be.components.utils.PropertyDataDefinitionBuilder;
+import org.openecomp.sdc.be.datatypes.elements.SchemaDefinition;
 import org.openecomp.sdc.be.model.DataTypeDefinition;
 import org.openecomp.sdc.be.model.PropertyDefinition;
 import org.openecomp.sdc.be.model.Resource;
@@ -20,25 +48,36 @@ import org.openecomp.sdc.be.model.tosca.ToscaPropertyType;
 import org.openecomp.sdc.be.tosca.model.ToscaNodeType;
 import org.openecomp.sdc.be.tosca.model.ToscaProperty;
 
-import fj.data.Either;
-
+@RunWith(MockitoJUnitRunner.class)
 public class PropertyConvertorTest {
     private PropertyDefinition property;
-    Map<String, DataTypeDefinition> dataTypes;
+    private Map<String, DataTypeDefinition> dataTypes;
+
+    @InjectMocks
+    private PropertyConvertor propertyConvertor;
 
     @Before
     public void setUp(){
         property = new PropertyDefinition();
         property.setName("myProperty");
         property.setType(ToscaPropertyType.INTEGER.getType());
-        dataTypes = new HashMap<String, DataTypeDefinition>();
+        dataTypes = new HashMap<>();
         dataTypes.put(property.getName(), new DataTypeDefinition());
     }
 
+    @Test
+    public void testConvertProperty() {
+       SchemaDefinition schema = new SchemaDefinition();
+       schema.setProperty(property);
+       
+       property.setSchema(schema);
+       
+       propertyConvertor.convertProperty(dataTypes, property, PropertyConvertor.PropertyType.PROPERTY);
+    }
 
     @Test
     public void convertPropertyWhenValueAndDefaultNull() {
-        ToscaProperty prop = PropertyConvertor.getInstance().convertProperty(dataTypes, property, false);
+        ToscaProperty prop = propertyConvertor.convertProperty(dataTypes, property, PropertyConvertor.PropertyType.PROPERTY);
         assertNotNull(prop);
         assertNull(prop.getDefaultp());
     }
@@ -47,9 +86,9 @@ public class PropertyConvertorTest {
     public void convertPropertyWhenValueNullAndDefaultNotEmpty() {
         final String def = "1";
         property.setDefaultValue(def);
-        ToscaProperty result = PropertyConvertor.getInstance().convertProperty(dataTypes, property, false);
+        ToscaProperty result = propertyConvertor.convertProperty(dataTypes, property, PropertyConvertor.PropertyType.PROPERTY);
         assertNotNull(result);
-        assertEquals(Integer.valueOf(def).intValue(), result.getDefaultp());
+        assertEquals(Integer.valueOf(def), result.getDefaultp());
     }
 
     @Test
@@ -60,16 +99,15 @@ public class PropertyConvertorTest {
         property1.setDefaultValue("2");
         dataTypes.put(property1.getName(), new DataTypeDefinition());
         Resource resource = new Resource();
-        List<PropertyDefinition> properties = new ArrayList<PropertyDefinition>();
+        List<PropertyDefinition> properties = new ArrayList<>();
         properties.add(property);
         properties.add(property1);
         resource.setProperties(properties);
-        Either<ToscaNodeType, ToscaError> result = PropertyConvertor.getInstance().convertProperties(resource, new ToscaNodeType(), dataTypes);
+        Either<ToscaNodeType, ToscaError> result = propertyConvertor.convertProperties(resource, new ToscaNodeType(), dataTypes);
         assertTrue(result.isLeft());
         assertEquals(2, result.left().value().getProperties().size());
         int cnt = 0;
-        for (Iterator<ToscaProperty> it = result.left().value().getProperties().values().iterator(); it.hasNext(); ) {
-            ToscaProperty prop = it.next();
+        for (ToscaProperty prop : result.left().value().getProperties().values()) {
             if (prop.getDefaultp() == null) {
                 cnt++;
             }
@@ -86,15 +124,14 @@ public class PropertyConvertorTest {
         property.setDefaultValue("1");
         dataTypes.put(property1.getName(), new DataTypeDefinition());
         Resource resource = new Resource();
-        List<PropertyDefinition> properties = new ArrayList<PropertyDefinition>();
+        List<PropertyDefinition> properties = new ArrayList<>();
         properties.add(property);
         properties.add(property1);
         resource.setProperties(properties);
-        Either<ToscaNodeType, ToscaError> result = PropertyConvertor.getInstance().convertProperties(resource, new ToscaNodeType(), dataTypes);
+        Either<ToscaNodeType, ToscaError> result = propertyConvertor.convertProperties(resource, new ToscaNodeType(), dataTypes);
         assertTrue(result.isLeft());
         assertEquals(2, result.left().value().getProperties().size());
-        for (Iterator<ToscaProperty> it = result.left().value().getProperties().values().iterator(); it.hasNext(); ) {
-            ToscaProperty prop = it.next();
+        for (ToscaProperty prop : result.left().value().getProperties().values()) {
             assertNotNull(prop.getDefaultp());
         }
     }
@@ -106,16 +143,36 @@ public class PropertyConvertorTest {
         property1.setType(ToscaPropertyType.INTEGER.getType());
         dataTypes.put(property1.getName(), new DataTypeDefinition());
         Resource resource = new Resource();
-        List<PropertyDefinition> properties = new ArrayList<PropertyDefinition>();
+        List<PropertyDefinition> properties = new ArrayList<>();
         properties.add(property);
         properties.add(property1);
         resource.setProperties(properties);
-        Either<ToscaNodeType, ToscaError> result = PropertyConvertor.getInstance().convertProperties(resource, new ToscaNodeType(), dataTypes);
+        Either<ToscaNodeType, ToscaError> result = propertyConvertor.convertProperties(resource, new ToscaNodeType(), dataTypes);
         assertTrue(result.isLeft());
         assertEquals(2, result.left().value().getProperties().size());
-        for (Iterator<ToscaProperty> it = result.left().value().getProperties().values().iterator(); it.hasNext(); ) {
-            ToscaProperty prop = it.next();
+        for (ToscaProperty prop : result.left().value().getProperties().values()) {
             assertNull(prop.getDefaultp());
         }
      }
+
+    @Test
+    public void convertPropertyWhichStartsWithSemiColon() throws Exception {
+        PropertyDefinition property1 = new PropertyDataDefinitionBuilder()
+                .setDefaultValue("::")
+                .setType(ToscaPropertyType.STRING.getType())
+                .build();
+        ToscaProperty toscaProperty = propertyConvertor.convertProperty(Collections.emptyMap(), property1, PropertyConvertor.PropertyType.PROPERTY);
+        assertThat(toscaProperty.getDefaultp()).isEqualTo("::");
+    }
+
+    @Test
+    public void convertPropertyWhichStartsWithSlash() throws Exception {
+        PropertyDefinition property1 = new PropertyDataDefinitionBuilder()
+                .setDefaultValue("/")
+                .setType(ToscaPropertyType.STRING.getType())
+                .build();
+        ToscaProperty toscaProperty = propertyConvertor.convertProperty(Collections.emptyMap(), property1, PropertyConvertor.PropertyType.PROPERTY);
+        assertThat(toscaProperty.getDefaultp()).isEqualTo("/");
+    }
+
 }