Interface operation support for service - BE
[sdc.git] / catalog-be / src / test / java / org / openecomp / sdc / be / tosca / utils / InterfacesOperationsToscaUtilTest.java
index bebfbd1..7a88d0a 100644 (file)
 
 package org.openecomp.sdc.be.tosca.utils;
 
+import com.fasterxml.jackson.databind.DeserializationFeature;
+import com.fasterxml.jackson.databind.ObjectMapper;
+
+import java.util.HashMap;
+import java.util.Map;
+
 import org.junit.Assert;
 import org.junit.BeforeClass;
 import org.junit.Test;
@@ -27,31 +33,41 @@ import org.openecomp.sdc.be.datatypes.elements.OperationInputDefinition;
 import org.openecomp.sdc.be.model.Component;
 import org.openecomp.sdc.be.model.InterfaceDefinition;
 import org.openecomp.sdc.be.model.Resource;
+import org.openecomp.sdc.be.model.Service;
 import org.openecomp.sdc.be.tosca.ToscaExportHandler;
 import org.openecomp.sdc.be.tosca.ToscaRepresentation;
+import org.openecomp.sdc.be.tosca.model.ToscaLifecycleOperationDefinition;
 import org.openecomp.sdc.be.tosca.model.ToscaNodeType;
+import org.openecomp.sdc.be.tosca.model.ToscaProperty;
 import org.openecomp.sdc.be.tosca.model.ToscaTemplate;
-
-import java.util.HashMap;
-import java.util.Map;
+import org.openecomp.sdc.common.util.YamlToObjectConverter;
 
 public class InterfacesOperationsToscaUtilTest {
 
+    private static final String MAPPED_PROPERTY_NAME = "mapped_property";
+    private static final String INPUT_NAME_PREFIX = "input_";
+    private static final String NODE_TYPE_NAME = "test";
+    private String[] inputTypes = {"string", "integer", "float", "boolean"};
+    private static ObjectMapper mapper;
+
     @BeforeClass
     public static void setUp() {
         new DummyConfigurationManager();
+        mapper = new ObjectMapper();
+        mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
     }
 
-    @Test()
-    public void addInterfaceTypeElement() {
+
+    @Test
+    public void addInterfaceTypeElementToResource() {
         Component component = new Resource();
         component.setNormalizedName("normalizedComponentName");
         InterfaceDefinition addedInterface = new InterfaceDefinition();
         addedInterface.setToscaResourceName("interface.types.test_resource_name");
         addOperationsToInterface(addedInterface, 5, 3, true);
         final String interfaceType = "normalizedComponentName-interface";
-        ((Resource) component).setInterfaces(new HashMap<>());
-        ((Resource) component).getInterfaces().put(interfaceType, addedInterface);
+        component.setInterfaces(new HashMap<>());
+        component.getInterfaces().put(interfaceType, addedInterface);
         final Map<String, Object> interfaceTypeElement =
                 InterfacesOperationsToscaUtil.addInterfaceTypeElement(component);
 
@@ -65,7 +81,29 @@ public class InterfacesOperationsToscaUtilTest {
     }
 
     @Test
-    public void addInterfaceDefinitionElement() {
+    public void addInterfaceTypeElementToService() {
+        Component component = new Service();
+        component.setNormalizedName("normalizedServiceComponentName");
+        InterfaceDefinition addedInterface = new InterfaceDefinition();
+        addedInterface.setToscaResourceName("interface.types.test_service_name");
+        addOperationsToInterface(addedInterface, 5, 3, true);
+        final String interfaceType = "normalizedServiceComponentName-interface";
+        component.setInterfaces(new HashMap<>());
+        component.getInterfaces().put(interfaceType, addedInterface);
+        final Map<String, Object> interfaceTypeElement =
+                InterfacesOperationsToscaUtil.addInterfaceTypeElement(component);
+
+        ToscaExportHandler handler = new ToscaExportHandler(null,null,null,null,null,null);
+        ToscaTemplate template = new ToscaTemplate("testService");
+        template.setInterface_types(interfaceTypeElement);
+        final ToscaRepresentation toscaRepresentation = handler.createToscaRepresentation(template);
+
+        Assert.assertFalse(toscaRepresentation.getMainYaml().contains("operations"));
+        Assert.assertTrue(toscaRepresentation.getMainYaml().contains("interface.types.test_service_name"));
+    }
+
+    @Test
+    public void addInterfaceDefinitionElementToResource() {
         Component component = new Resource();
         component.setNormalizedName("normalizedComponentName");
         InterfaceDefinition addedInterface = new InterfaceDefinition();
@@ -73,25 +111,58 @@ public class InterfacesOperationsToscaUtilTest {
 
         addOperationsToInterface(addedInterface, 3, 2, true);
         final String interfaceType = "normalizedComponentName-interface";
-        ((Resource) component).setInterfaces(new HashMap<>());
-        ((Resource) component).getInterfaces().put(interfaceType, addedInterface);
+        component.setInterfaces(new HashMap<>());
+        component.getInterfaces().put(interfaceType, addedInterface);
         ToscaNodeType nodeType = new ToscaNodeType();
         InterfacesOperationsToscaUtil.addInterfaceDefinitionElement(component, nodeType);
 
         ToscaExportHandler handler = new ToscaExportHandler(null,null,null,null,null,null);
-        ToscaTemplate template = new ToscaTemplate("test");
+        ToscaTemplate template = new ToscaTemplate(NODE_TYPE_NAME);
         Map<String, ToscaNodeType> nodeTypes = new HashMap<>();
-        nodeTypes.put("test", nodeType);
+        nodeTypes.put(NODE_TYPE_NAME, nodeType);
         template.setNode_types(nodeTypes);
         final ToscaRepresentation toscaRepresentation = handler.createToscaRepresentation(template);
 
-        Assert.assertFalse(toscaRepresentation.getMainYaml().contains("operations"));
-        Assert.assertTrue(toscaRepresentation.getMainYaml().contains("resourceName:"));
-        Assert.assertTrue(toscaRepresentation.getMainYaml().contains("inputs:"));
-        Assert.assertFalse(toscaRepresentation.getMainYaml().contains("defaultp"));
-        Assert.assertTrue(toscaRepresentation.getMainYaml().contains("has description"));
-        Assert.assertTrue(toscaRepresentation.getMainYaml().contains("naming_function_"));
-        Assert.assertTrue(toscaRepresentation.getMainYaml().contains("com.some.resource.or.other.resourceName"));
+        String mainYaml = toscaRepresentation.getMainYaml();
+        Assert.assertFalse(mainYaml.contains("operations"));
+        Assert.assertTrue(mainYaml.contains("resourceName:"));
+        Assert.assertTrue(mainYaml.contains("inputs:"));
+        validateOperationInputs(mainYaml);
+        Assert.assertFalse(mainYaml.contains("defaultp"));
+        Assert.assertTrue(mainYaml.contains("has description"));
+        Assert.assertTrue(mainYaml.contains(MAPPED_PROPERTY_NAME));
+        Assert.assertTrue(mainYaml.contains("com.some.resource.or.other.resourceName"));
+    }
+
+    @Test
+    public void addInterfaceDefinitionElementToService() {
+        Component component = new Service();
+        component.setNormalizedName("normalizedServiceComponentName");
+        InterfaceDefinition addedInterface = new InterfaceDefinition();
+        addedInterface.setToscaResourceName("com.some.service.or.other.serviceName");
+
+        addOperationsToInterface(addedInterface, 3, 2, true);
+        final String interfaceType = "normalizedServiceComponentName-interface";
+        component.setInterfaces(new HashMap<>());
+        component.getInterfaces().put(interfaceType, addedInterface);
+        ToscaNodeType nodeType = new ToscaNodeType();
+        InterfacesOperationsToscaUtil.addInterfaceDefinitionElement(component, nodeType);
+
+        ToscaExportHandler handler = new ToscaExportHandler(null,null,null,null,null,null);
+        ToscaTemplate template = new ToscaTemplate("testService");
+        Map<String, ToscaNodeType> nodeTypes = new HashMap<>();
+        nodeTypes.put(NODE_TYPE_NAME, nodeType);
+        template.setNode_types(nodeTypes);
+        final ToscaRepresentation toscaRepresentation = handler.createToscaRepresentation(template);
+        String mainYaml = toscaRepresentation.getMainYaml();
+        Assert.assertFalse(mainYaml.contains("operations"));
+        Assert.assertTrue(mainYaml.contains("serviceName:"));
+        Assert.assertTrue(mainYaml.contains("inputs:"));
+        validateOperationInputs(mainYaml);
+        Assert.assertFalse(mainYaml.contains("defaultp"));
+        Assert.assertTrue(mainYaml.contains("has description"));
+        Assert.assertTrue(mainYaml.contains(MAPPED_PROPERTY_NAME));
+        Assert.assertTrue(mainYaml.contains("com.some.service.or.other.serviceName"));
     }
 
     @Test
@@ -103,8 +174,8 @@ public class InterfacesOperationsToscaUtilTest {
 
         addOperationsToInterface(addedInterface, 3, 3, false);
         final String interfaceType = "normalizedComponentName-interface";
-        ((Resource) component).setInterfaces(new HashMap<>());
-        ((Resource) component).getInterfaces().put(interfaceType, addedInterface);
+        component.setInterfaces(new HashMap<>());
+        component.getInterfaces().put(interfaceType, addedInterface);
         ToscaNodeType nodeType = new ToscaNodeType();
         InterfacesOperationsToscaUtil.addInterfaceDefinitionElement(component, nodeType);
 
@@ -116,7 +187,7 @@ public class InterfacesOperationsToscaUtilTest {
         final ToscaRepresentation toscaRepresentation = handler.createToscaRepresentation(template);
 
         Assert.assertFalse(toscaRepresentation.getMainYaml().contains("operations"));
-        Assert.assertFalse(toscaRepresentation.getMainYaml().contains("input_"));
+        Assert.assertFalse(toscaRepresentation.getMainYaml().contains(INPUT_NAME_PREFIX));
         Assert.assertFalse(toscaRepresentation.getMainYaml().contains("defaultp"));
         Assert.assertTrue(toscaRepresentation.getMainYaml().contains("resourceNameNoInputs:"));
         Assert.assertTrue(toscaRepresentation.getMainYaml().contains("has description"));
@@ -146,17 +217,48 @@ public class InterfacesOperationsToscaUtilTest {
     private ListDataDefinition<OperationInputDefinition> createInputs(int numOfInputs) {
         ListDataDefinition<OperationInputDefinition> operationInputDefinitionList = new ListDataDefinition<>();
         for (int i = 0; i < numOfInputs; i++) {
-            operationInputDefinitionList.add(createMockOperationInputDefinition("input_" + i,
-                    java.util.UUID.randomUUID().toString() + "." + "naming_function_" + i));
+            operationInputDefinitionList.add(createMockOperationInputDefinition(
+                    INPUT_NAME_PREFIX + inputTypes[i] + "_" + i,
+                    java.util.UUID.randomUUID().toString() + "." + MAPPED_PROPERTY_NAME, i));
         }
         return operationInputDefinitionList;
     }
 
 
-    private OperationInputDefinition createMockOperationInputDefinition(String name, String id) {
+    private OperationInputDefinition createMockOperationInputDefinition(String name, String id, int index) {
         OperationInputDefinition operationInputDefinition = new OperationInputDefinition();
         operationInputDefinition.setName(name);
         operationInputDefinition.setInputId(id);
+        operationInputDefinition.setType(inputTypes[index]);
+        operationInputDefinition.setRequired(index % 2 == 0);
         return operationInputDefinition;
     }
+
+    private void validateOperationInputs(String mainYaml) {
+        String nodeTypeKey = NODE_TYPE_NAME + ":";
+        String nodeTypesRepresentation = mainYaml.substring(mainYaml.indexOf(nodeTypeKey) + nodeTypeKey.length(),
+                mainYaml.lastIndexOf(MAPPED_PROPERTY_NAME) + MAPPED_PROPERTY_NAME.length());
+        YamlToObjectConverter objectConverter = new YamlToObjectConverter();
+        ToscaNodeType toscaNodeType = objectConverter.convert(nodeTypesRepresentation.getBytes(), ToscaNodeType.class);
+        Map<String, Object> interfaces = toscaNodeType.getInterfaces();
+        for (Object interfaceVal : interfaces.values()) {
+            Map<String, Object> interfaceDefinition = mapper.convertValue(interfaceVal, Map.class);
+            for (Object operationVal : interfaceDefinition.values()) {
+                if (operationVal instanceof Map) {
+                    validateOperationInputDefinition(operationVal);
+                }
+            }
+        }
+    }
+
+    private void validateOperationInputDefinition(Object operationVal) {
+        ToscaLifecycleOperationDefinition operation =
+                mapper.convertValue(operationVal, ToscaLifecycleOperationDefinition.class);
+        Map<String, ToscaProperty> inputs = operation.getInputs();
+        for (Map.Entry<String, ToscaProperty> inputEntry : inputs.entrySet()) {
+            Assert.assertEquals(inputEntry.getKey().split("_")[1], inputEntry.getValue().getType());
+            Boolean expectedIsRequired = Integer.parseInt(inputEntry.getKey().split("_")[2]) % 2 == 0;
+            Assert.assertEquals(expectedIsRequired, inputEntry.getValue().getRequired());
+        }
+    }
 }