Fix service proxy node type
[sdc.git] / catalog-be / src / test / java / org / openecomp / sdc / be / tosca / ToscaExportUtilsTest.java
index efc89a9..87cc6e8 100644 (file)
+/*
+ * Copyright © 2016-2019 European Support Limited
+ *
+ * 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.
+ */
+
 package org.openecomp.sdc.be.tosca;
 
-import com.google.gson.Gson;
-import com.google.gson.GsonBuilder;
+import java.util.ArrayList;
+import java.util.Arrays;
+import java.util.Collections;
+import java.util.HashMap;
+import java.util.List;
+import java.util.Map;
+import java.util.Optional;
+
+import org.junit.Assert;
 import org.junit.Test;
-import org.openecomp.sdc.be.components.impl.ComponentInstanceBusinessLogic;
-import org.openecomp.sdc.be.model.operations.impl.ComponentInstanceOperation;
-import org.slf4j.Logger;
-import org.slf4j.LoggerFactory;
-import org.springframework.beans.factory.annotation.Autowired;
+import org.openecomp.sdc.be.model.Component;
+import org.openecomp.sdc.be.model.DataTypeDefinition;
+import org.openecomp.sdc.be.model.InputDefinition;
+import org.openecomp.sdc.be.model.InterfaceDefinition;
+import org.openecomp.sdc.be.model.PropertyDefinition;
+import org.openecomp.sdc.be.model.Service;
+import org.openecomp.sdc.be.tosca.model.ToscaProperty;
+import org.openecomp.sdc.be.tosca.utils.ToscaExportUtils;
 
 public class ToscaExportUtilsTest {
-    private static final Logger log = LoggerFactory.getLogger(ToscaExportUtilsTest.class);
-    @javax.annotation.Resource
-    private ComponentInstanceBusinessLogic componentInstanceBusinessLogic;
-    @Autowired
-    private ToscaExportHandler exportUtils;
-    @Autowired
-    private ComponentInstanceOperation componentInstanceOperation;
 
-    Gson gson = new GsonBuilder().setPrettyPrinting().create();
+    private static final Map<String, DataTypeDefinition> dataTypes = new HashMap<>();
+
+    @Test
+    public void testGetProxyNodeTypeInterfacesNoInterfaces() {
+        Component service = new Service();
+        Optional<Map<String, Object>> proxyNodeTypeInterfaces =
+                ToscaExportUtils.getProxyNodeTypeInterfaces(service, dataTypes);
+        Assert.assertFalse(proxyNodeTypeInterfaces.isPresent());
+    }
+
+    @Test
+    public void testGetProxyNodeTypeInterfaces() {
+        Component service = getTestComponent();
+        Optional<Map<String, Object>> proxyNodeTypeInterfaces =
+                ToscaExportUtils.getProxyNodeTypeInterfaces(service, dataTypes);
+        Assert.assertTrue(proxyNodeTypeInterfaces.isPresent());
+        Map<String, Object> componentInterfaces = proxyNodeTypeInterfaces.get();
+        Assert.assertNotNull(componentInterfaces);
+        Assert.assertEquals(1, componentInterfaces.size());
+    }
+
+
+    @Test
+    public void testGetProxyNodeTypePropertiesComponentNull() {
+        Optional<Map<String, ToscaProperty>> proxyNodeTypeProperties =
+                ToscaExportUtils.getProxyNodeTypeProperties(null, dataTypes);
+        Assert.assertFalse(proxyNodeTypeProperties.isPresent());
+    }
+
+    @Test
+    public void testGetProxyNodeTypePropertiesNoProperties() {
+        Component service = new Service();
+        Optional<Map<String, ToscaProperty>> proxyNodeTypeProperties =
+                ToscaExportUtils.getProxyNodeTypeProperties(service, dataTypes);
+        Assert.assertFalse(proxyNodeTypeProperties.isPresent());
+    }
+
+    @Test
+    public void testGetProxyNodeTypeProperties() {
+        Component service = getTestComponent();
+        service.setProperties(Arrays.asList(createMockProperty("componentPropStr", "Default String Prop"),
+                createMockProperty("componentPropInt", null)));
+        Optional<Map<String, ToscaProperty>> proxyNodeTypeProperties =
+                ToscaExportUtils.getProxyNodeTypeProperties(service, dataTypes);
+        Assert.assertTrue(proxyNodeTypeProperties.isPresent());
+        Map<String, ToscaProperty> componentProperties = proxyNodeTypeProperties.get();
+        Assert.assertNotNull(componentProperties);
+        Assert.assertEquals(2, componentProperties.size());
+    }
 
     @Test
-    public void testExportService() {
-    /*    Resource resource1 = ResourceTestUtils.prepareResource(0);
-        resource1.setResourceType(ResourceTypeEnum.VF);
-        Either<Resource, ResponseFormat> createResource1 = resourceBusinessLogic.createResource(resource1, getAdminUser(), null, null);
-        assertTrue(createResource1.isLeft());
-        Resource certifiedVFC1 = changeResourceStateToCertify(createResource1.left().value());
-
-        Resource resource2 = ResourceTestUtils.prepareResource(1);
-        resource2.setResourceType(ResourceTypeEnum.VF);
-        Either<Resource, ResponseFormat> createResource2 = resourceBusinessLogic.createResource(resource2, getAdminUser(), null, null);
-        assertTrue(createResource2.isLeft());
-        Resource certifiedVFC2 = changeResourceStateToCertify(createResource2.left().value());
-
-        Service service = ResourceTestUtils.prepareService(0);
-        Either<Service, ResponseFormat> createService = serviceBusinessLogic.createService(service, getAdminUser());
-        assertTrue(createService.isLeft());
-
-        // add VFC instance to VF
-        ComponentInstance vfcResourceInstance1 = new ComponentInstance();
-        vfcResourceInstance1.setDescription("VFC instance 1");
-        vfcResourceInstance1.setName(certifiedVFC1.getName());
-        vfcResourceInstance1.setComponentUid(certifiedVFC1.getUniqueId());
-
-        Either<ComponentInstance, ResponseFormat> createResourceVfcInstance1 = serviceInstanceBusinessLogic.createComponentInstance(ComponentTypeEnum.SERVICE_PARAM_NAME, createService.left().value().getUniqueId(), adminUser.getAttuid(),
-                vfcResourceInstance1);
-        assertTrue(createResourceVfcInstance1.isLeft());
-
-        ComponentInstance vfcResourceInstance2 = new ComponentInstance();
-        vfcResourceInstance2.setDescription("VFC instance 2");
-        vfcResourceInstance2.setName(certifiedVFC2.getName());
-        vfcResourceInstance2.setComponentUid(certifiedVFC2.getUniqueId());
-        Either<ComponentInstance, ResponseFormat> createResourceVfcInstance2 = serviceInstanceBusinessLogic.createComponentInstance(ComponentTypeEnum.SERVICE_PARAM_NAME, createService.left().value().getUniqueId(), adminUser.getAttuid(),
-                vfcResourceInstance2);
-        assertTrue(createResourceVfcInstance2.isLeft());
-
-        Either<Service, ResponseFormat> serviceFetch = serviceBusinessLogic.getService(createService.left().value().getUniqueId(), adminUser);
-        assertTrue(serviceFetch.isLeft());
-
-        List<ComponentInstance> componentInstances = serviceFetch.left().value().getComponentInstances();
-        String ciname1 = null;
-        String ciname2 = null;
-
-        for (ComponentInstance ci : componentInstances) {
-            if (ci.getComponentUid().equals(certifiedVFC1.getUniqueId())) {
-                ciname1 = ci.getName();
-            }
-            if (ci.getComponentUid().equals(certifiedVFC2.getUniqueId())) {
-                ciname2 = ci.getName();
-            }
-        }
-
-        Either<ToscaRepresentation, ToscaError> result = exportUtils.exportComponent(serviceFetch.left().value());
-        assertTrue(result.isLeft());
-
-        String mainYaml = result.left().value().getMainYaml();
-        assertNotNull(mainYaml);
-
-        YamlToObjectConverter yamlToObjectConverter = new YamlToObjectConverter();
-        assertTrue(yamlToObjectConverter.isValidYaml(mainYaml.getBytes()));
-        log.debug(mainYaml);
-
-        Yaml yaml = new Yaml();
-
-        InputStream inputStream = new ByteArrayInputStream(mainYaml.getBytes());
-        Map<String, Object> load = (Map<String, Object>) yaml.load(inputStream);
-        Map<String, Object> imports = (Map<String, Object>) load.get("imports");
-        assertNotNull(imports);
-        assertEquals("Validate imports size in yml", 2, imports.size());
-
-        Map<String, Object> metadata = (Map<String, Object>) load.get("metadata");
-        assertNotNull(metadata);
-        validateMetadata(metadata, serviceFetch.left().value(), false);
-
-        Map<String, Object> vf1 = (Map<String, Object>) imports.get(certifiedVFC1.getName());
-        String fileName = (String) vf1.get(ToscaExportHandler.IMPORTS_FILE_KEY);
-        ArtifactDefinition artifactDefinition = certifiedVFC1.getToscaArtifacts().get(ToscaExportHandler.ASSET_TOSCA_TEMPLATE);
-        assertEquals("Validate 1 file name", artifactDefinition.getArtifactName(), fileName);
-
-        Map<String, Object> topology_template = (Map<String, Object>) load.get("topology_template");
-        Map<String, Object> node_templates = (Map<String, Object>) topology_template.get("node_templates");
-        Map<String, Object> inst1 = (Map<String, Object>) node_templates.get(ciname1);
-        Map<String, Object> inst2 = (Map<String, Object>) node_templates.get(ciname2);
-
-        Map<String, Object> inst1MD = (Map<String, Object>) inst1.get("metadata");
-        Map<String, Object> inst2MD = (Map<String, Object>) inst2.get("metadata");
-
-        validateMetadata(inst1MD, certifiedVFC1, true);
-
-        Map<String, Object> vf2 = (Map<String, Object>) imports.get(certifiedVFC2.getName());
-        fileName = (String) vf2.get(ToscaExportHandler.IMPORTS_FILE_KEY);
-        artifactDefinition = certifiedVFC2.getToscaArtifacts().get(ToscaExportHandler.ASSET_TOSCA_TEMPLATE);
-        assertEquals("Validate 2 file name", artifactDefinition.getArtifactName(), fileName);
-
-        validateMetadata(inst2MD, certifiedVFC2, true);*/
+    public void testResolvePropertyDefaultValueFromInputNoInputs() {
+        Component service = getTestComponent();
+        service.setProperties(Collections.singletonList(createMockProperty("componentPropStr", null)));
+        Optional<Map<String, ToscaProperty>> properties = ToscaExportUtils.getProxyNodeTypeProperties(service,
+                dataTypes);
+        Assert.assertTrue(properties.isPresent());
+        Map<String, ToscaProperty> nodeTypeProperties = properties.get();
+        ToscaExportUtils.resolvePropertyDefaultValueFromInput(null, nodeTypeProperties, dataTypes);
+        nodeTypeProperties.values().forEach(val -> Assert.assertNull(val.getDefaultp()));
     }
 
+    @Test
+    public void testResolvePropertyDefaultValueFromInput() {
+        Component service = getTestComponent();
+        service.setProperties(Arrays.asList(createMockProperty("componentPropStr1", "{get_input: componentInputStr1}"),
+                createMockProperty("componentPropStr2", "Default prop value"),
+                createMockProperty("componentPropStr3", null)));
+        Optional<Map<String, ToscaProperty>> properties = ToscaExportUtils.getProxyNodeTypeProperties(service,
+                dataTypes);
+        Assert.assertTrue(properties.isPresent());
+        Map<String, ToscaProperty> nodeTypeProperties = properties.get();
+        List<InputDefinition> componentInputs = Arrays.asList(createMockInput("componentInputStr1",
+                "Default String Input1"), createMockInput("componentInputStr2", "Default String Input2"));
+        ToscaExportUtils.resolvePropertyDefaultValueFromInput(componentInputs, nodeTypeProperties, dataTypes);
+        nodeTypeProperties.entrySet().stream()
+                .filter(entry -> entry.getKey().equals("componentPropStr1"))
+                .forEach(entry -> Assert.assertEquals("Default String Input1",
+                        entry.getValue().getDefaultp().toString()));
+
+        nodeTypeProperties.entrySet().stream()
+                .filter(entry -> entry.getKey().equals("componentPropStr2"))
+                .forEach(entry -> Assert.assertEquals("Default prop value",
+                        entry.getValue().getDefaultp().toString()));
+
+        nodeTypeProperties.entrySet().stream()
+                .filter(entry -> entry.getKey().equals("componentPropStr3"))
+                .forEach(entry -> Assert.assertNull(entry.getValue().getDefaultp()));
+    }
+
+    @Test
+    public void testAddInputsToPropertiesNoInputs() {
+        Component service = getTestComponent();
+        service.setProperties(Arrays.asList(createMockProperty("componentPropStr", "Default String Prop"),
+                createMockProperty("componentPropInt", null)));
+        Optional<Map<String, ToscaProperty>> proxyNodeTypePropertiesResult =
+                ToscaExportUtils.getProxyNodeTypeProperties(service, dataTypes);
+
+        Assert.assertTrue(proxyNodeTypePropertiesResult.isPresent());
+        Map<String, ToscaProperty> proxyNodeTypeProperties = proxyNodeTypePropertiesResult.get();
+        ToscaExportUtils.addInputsToProperties(dataTypes, null, proxyNodeTypeProperties);
+        Assert.assertNotNull(proxyNodeTypeProperties);
+        Assert.assertEquals(2, proxyNodeTypeProperties.size());
+        ToscaExportUtils.addInputsToProperties(dataTypes, new ArrayList<>(), proxyNodeTypeProperties);
+        Assert.assertEquals(2, proxyNodeTypeProperties.size());
+    }
+
+    @Test
+    public void testAddInputsToPropertiesWithInputs() {
+        Component service = getTestComponent();
+        service.setProperties(Arrays.asList(createMockProperty("componentPropStr", "Default String Prop"),
+                createMockProperty("componentPropInt", null)));
+        service.setInputs(Arrays.asList(createMockInput("componentInputStr1",
+                "Default String Input1"), createMockInput("componentInputStr2", "Default String Input2")));
+        Optional<Map<String, ToscaProperty>> proxyNodeTypePropertiesResult =
+                ToscaExportUtils.getProxyNodeTypeProperties(service, dataTypes);
+
+        Assert.assertTrue(proxyNodeTypePropertiesResult.isPresent());
+        Map<String, ToscaProperty> proxyNodeTypeProperties = proxyNodeTypePropertiesResult.get();
+        ToscaExportUtils.addInputsToProperties(dataTypes, service.getInputs(), proxyNodeTypeProperties);
+        Assert.assertNotNull(proxyNodeTypeProperties);
+        Assert.assertEquals(4, proxyNodeTypeProperties.size());
+        Assert.assertNotNull(proxyNodeTypeProperties.get("componentInputStr1"));
+        Assert.assertNotNull(proxyNodeTypeProperties.get("componentInputStr2"));
+    }
+
+    @Test
+    public void testAddInputsToPropertiesOnlyInputs() {
+        Component service = getTestComponent();
+        service.setInputs(Arrays.asList(createMockInput("componentInputStr1",
+                "Default String Input1"), createMockInput("componentInputStr2", "Default String Input2")));
+        Optional<Map<String, ToscaProperty>> proxyNodeTypePropertiesResult =
+                ToscaExportUtils.getProxyNodeTypeProperties(service, dataTypes);
+
+        Assert.assertTrue(proxyNodeTypePropertiesResult.isPresent());
+        Map<String, ToscaProperty> proxyNodeTypeProperties = proxyNodeTypePropertiesResult.get();
+        ToscaExportUtils.addInputsToProperties(dataTypes, service.getInputs(), proxyNodeTypeProperties);
+        Assert.assertNotNull(proxyNodeTypeProperties);
+        Assert.assertEquals(2, proxyNodeTypeProperties.size());
+        Assert.assertNotNull(proxyNodeTypeProperties.get("componentInputStr1"));
+        Assert.assertNotNull(proxyNodeTypeProperties.get("componentInputStr2"));
+    }
+
+    private Component getTestComponent() {
+        Component component = new Service();
+        component.setNormalizedName("normalizedServiceComponentName");
+        InterfaceDefinition addedInterface = new InterfaceDefinition();
+        addedInterface.setType("com.some.service.or.other.serviceName");
+        final String interfaceType = "normalizedServiceComponentName-interface";
+        component.setInterfaces(new HashMap<>());
+        component.getInterfaces().put(interfaceType, addedInterface);
+        return component;
+    }
+
+    private PropertyDefinition createMockProperty(String propertyName, String defaultValue){
+        PropertyDefinition propertyDefinition = new PropertyDefinition();
+        propertyDefinition.setName(propertyName);
+        propertyDefinition.setType("string");
+        propertyDefinition.setDefaultValue(defaultValue);
+        return propertyDefinition;
+    }
+
+    private InputDefinition createMockInput(String inputName, String defaultValue){
+        InputDefinition inputDefinition = new InputDefinition();
+        inputDefinition.setName(inputName);
+        inputDefinition.setType("string");
+        inputDefinition.setDefaultValue(defaultValue);
+        return inputDefinition;
+    }
 }