Service import issues
[sdc.git] / catalog-be / src / test / java / org / openecomp / sdc / be / components / csar / YamlTemplateParsingHandlerTest.java
index a2931a5..218e026 100644 (file)
 
 package org.openecomp.sdc.be.components.csar;
 
-import static org.assertj.core.api.Assertions.assertThat;
-import static org.junit.jupiter.api.Assertions.assertEquals;
-import static org.junit.jupiter.api.Assertions.assertNotNull;
-import static org.junit.jupiter.api.Assertions.assertTrue;
-import static org.mockito.ArgumentMatchers.any;
-import static org.mockito.ArgumentMatchers.eq;
-import static org.mockito.Mockito.when;
-import static org.openecomp.sdc.be.utils.TypeUtils.ToscaTagNamesEnum.ARTIFACTS;
-
-import java.io.File;
-import java.net.URISyntaxException;
-import java.util.Collections;
-import java.util.HashMap;
-import java.util.List;
-import java.util.Map;
-import java.util.Optional;
-import java.util.stream.Collectors;
 import mockit.Deencapsulation;
 import org.apache.commons.collections.MapUtils;
+import org.apache.commons.lang3.StringUtils;
 import org.assertj.core.util.Lists;
 import org.junit.jupiter.api.BeforeAll;
 import org.junit.jupiter.api.BeforeEach;
@@ -50,6 +34,7 @@ import org.mockito.junit.jupiter.MockitoExtension;
 import org.openecomp.sdc.be.components.impl.AnnotationBusinessLogic;
 import org.openecomp.sdc.be.components.impl.GroupTypeBusinessLogic;
 import org.openecomp.sdc.be.components.impl.PolicyTypeBusinessLogic;
+import org.openecomp.sdc.be.components.impl.ServiceBusinessLogic;
 import org.openecomp.sdc.be.components.validation.AnnotationValidator;
 import org.openecomp.sdc.be.dao.janusgraph.JanusGraphDao;
 import org.openecomp.sdc.be.datatypes.elements.PropertyDataDefinition;
@@ -64,12 +49,33 @@ import org.openecomp.sdc.be.model.Resource;
 import org.openecomp.sdc.be.model.Service;
 import org.openecomp.sdc.be.model.UploadArtifactInfo;
 import org.openecomp.sdc.be.model.UploadComponentInstanceInfo;
+import org.openecomp.sdc.be.model.UploadReqInfo;
 import org.openecomp.sdc.be.model.User;
 import org.openecomp.sdc.be.model.operations.impl.AnnotationTypeOperations;
+import org.openecomp.sdc.be.ui.model.OperationUi;
 import org.openecomp.sdc.common.zip.ZipUtils;
 import org.openecomp.sdc.common.zip.exception.ZipException;
 import org.springframework.test.util.ReflectionTestUtils;
 
+import java.io.File;
+import java.net.URISyntaxException;
+import java.util.ArrayList;
+import java.util.Collections;
+import java.util.HashMap;
+import java.util.List;
+import java.util.Map;
+import java.util.Optional;
+import java.util.stream.Collectors;
+
+import static org.assertj.core.api.Assertions.assertThat;
+import static org.junit.jupiter.api.Assertions.assertEquals;
+import static org.junit.jupiter.api.Assertions.assertNotNull;
+import static org.junit.jupiter.api.Assertions.assertTrue;
+import static org.mockito.ArgumentMatchers.any;
+import static org.mockito.ArgumentMatchers.eq;
+import static org.mockito.Mockito.when;
+import static org.openecomp.sdc.be.utils.TypeUtils.ToscaTagNamesEnum.ARTIFACTS;
+
 @ExtendWith(MockitoExtension.class)
 public class YamlTemplateParsingHandlerTest {
 
@@ -85,6 +91,10 @@ public class YamlTemplateParsingHandlerTest {
     private User user;
     @Mock
     private PolicyTypeBusinessLogic policyTypeBusinessLogic;
+    @Mock
+    private ServiceBusinessLogic serviceBusinessLogic;
+    @Mock
+    private ToscaFunctionYamlParsingHandler toscaFunctionYamlParsingHandler;
 
     private YamlTemplateParsingHandler handler;
 
@@ -130,10 +140,9 @@ public class YamlTemplateParsingHandlerTest {
 
     @BeforeEach
     public void setup() {
-
-        AnnotationBusinessLogic annotationBusinessLogic = new AnnotationBusinessLogic(annotationTypeOperations,
-            annotationValidator);
-        handler = new YamlTemplateParsingHandler(janusGraphDao, groupTypeBusinessLogic, annotationBusinessLogic, policyTypeBusinessLogic);
+        final var annotationBusinessLogic = new AnnotationBusinessLogic(annotationTypeOperations, annotationValidator);
+        handler = new YamlTemplateParsingHandler(janusGraphDao, groupTypeBusinessLogic, annotationBusinessLogic, policyTypeBusinessLogic,
+            serviceBusinessLogic, toscaFunctionYamlParsingHandler);
         ReflectionTestUtils.setField(handler, "policyTypeBusinessLogic", policyTypeBusinessLogic);
     }
 
@@ -142,7 +151,7 @@ public class YamlTemplateParsingHandlerTest {
         when(groupTypeBusinessLogic.getLatestGroupTypeByType(eq(HEAT_GROUP_TYPE), any())).thenReturn(heatGroupType);
 
         String main_template_content = new String(csar.get(MAIN_TEMPLATE_NAME));
-        CsarInfo csarInfo = new CsarInfo(user, CSAR_UUID, csar, RESOURCE_NAME,
+        CsarInfo csarInfo = new OnboardedCsarInfo(user, CSAR_UUID, csar, RESOURCE_NAME,
             MAIN_TEMPLATE_NAME, main_template_content, true);
 
         Resource resource = new Resource();
@@ -157,7 +166,7 @@ public class YamlTemplateParsingHandlerTest {
     void parseServicePropertiesInfoFromYamlTest() {
         when(groupTypeBusinessLogic.getLatestGroupTypeByType(eq(HEAT_GROUP_TYPE), any())).thenReturn(heatGroupType);
         String main_template_content = new String(csar.get(MAIN_TEMPLATE_NAME));
-        CsarInfo csarInfo = new CsarInfo(user, CSAR_UUID, csar, RESOURCE_NAME,
+        CsarInfo csarInfo = new OnboardedCsarInfo(user, CSAR_UUID, csar, RESOURCE_NAME,
             MAIN_TEMPLATE_NAME, main_template_content, true);
 
         Service service = new Service();
@@ -173,6 +182,31 @@ public class YamlTemplateParsingHandlerTest {
         assertTrue(parsedYaml.getProperties().containsKey("default_software_version"));
     }
 
+    @Test
+    void parseRelationshipTemplateInfoFromYamlTest() {
+        when(groupTypeBusinessLogic.getLatestGroupTypeByType(eq(HEAT_GROUP_TYPE), any())).thenReturn(heatGroupType);
+        String main_template_content = new String(csar.get(MAIN_TEMPLATE_NAME));
+        CsarInfo csarInfo = new OnboardedCsarInfo(user, CSAR_UUID, csar, RESOURCE_NAME,
+            MAIN_TEMPLATE_NAME, main_template_content, true);
+
+        Service service = new Service();
+        ParsedToscaYamlInfo parsedYaml = handler.parseResourceInfoFromYAML(FILE_NAME, resourceYml, new HashMap<>(),
+            csarInfo.extractTypesInfo(), NODE_NAME, service, getInterfaceTemplateYaml(csarInfo).get());
+
+        assertThat(parsedYaml.getInstances()).isNotNull();
+        final Map<String, List<OperationUi>> operations = new HashMap<>();
+        for (UploadComponentInstanceInfo instance : parsedYaml.getInstances().values()) {
+            final Map<String, List<UploadReqInfo>> requirements = instance.getRequirements();
+            if (MapUtils.isNotEmpty(requirements)) {
+                requirements.values()
+                    .forEach(requirementInfoList -> requirementInfoList.stream()
+                        .filter(requirement -> StringUtils.isNotEmpty(requirement.getRelationshipTemplate()))
+                        .forEach(requirement -> operations.putAll(instance.getOperations())));
+            }
+        }
+        assertEquals(1, operations.size());
+    }
+
     @Test
     void parseResourceInfoFromYAMLTest() {
         stubGetGroupType();
@@ -260,6 +294,42 @@ public class YamlTemplateParsingHandlerTest {
         validateParsedYamlWithPolicies(parsedYaml);
     }
 
+    @Test
+    void parseResourceInstanceWithAttributesTest() {
+        stubGetGroupType();
+        stubGetPolicyType();
+        Resource resource = new Resource();
+        ParsedToscaYamlInfo parsedYaml = handler.parseResourceInfoFromYAML(FILE_NAME, resourceYml, new HashMap<>(),
+                new HashMap<>(), "", resource, null);
+        validateParsedYamlWithAttributes(parsedYaml);
+    }
+
+    private void validateParsedYamlWithAttributes(ParsedToscaYamlInfo parsedYaml) {
+        ArrayList<String> expectedSubnetsShowList = new ArrayList<>();
+        expectedSubnetsShowList.add("val1");
+        expectedSubnetsShowList.add("val2");
+
+        HashMap<String, String> expectedSubnetsNameMap = new HashMap<>();
+        expectedSubnetsNameMap.put("name1", "name_val1");
+        expectedSubnetsNameMap.put("name2", "name_val2");
+
+
+        assertThat(parsedYaml.getInstances().get("resource_instance_with_attributes")).isNotNull();
+        UploadComponentInstanceInfo resourceInstanceWithAttributes = parsedYaml.getInstances().get("resource_instance_with_attributes");
+        assertEquals(5, resourceInstanceWithAttributes.getAttributes().size());
+
+        assertTrue(resourceInstanceWithAttributes.getAttributes().containsKey("fq_name"));
+        assertEquals("fq_name_value", resourceInstanceWithAttributes.getAttributes().get("fq_name").getValue());
+        assertTrue(resourceInstanceWithAttributes.getAttributes().containsKey("tosca_name"));
+        assertEquals("tosca_name_value", resourceInstanceWithAttributes.getAttributes().get("tosca_name").getValue());
+        assertTrue(resourceInstanceWithAttributes.getAttributes().containsKey("subnets_show"));
+        assertEquals(expectedSubnetsShowList, resourceInstanceWithAttributes.getAttributes().get("subnets_show").getValue());
+        assertTrue(resourceInstanceWithAttributes.getAttributes().containsKey("subnets_name"));
+        assertEquals(expectedSubnetsNameMap, resourceInstanceWithAttributes.getAttributes().get("subnets_name").getValue());
+        assertTrue(resourceInstanceWithAttributes.getAttributes().containsKey("new_attribute"));
+        assertEquals("new_attribute_value", resourceInstanceWithAttributes.getAttributes().get("new_attribute").getValue());
+    }
+
     private void validateParsedYaml(ParsedToscaYamlInfo parsedYaml, String group, List<String> expectedProp) {
         assertThat(parsedYaml).isNotNull();
         assertThat(parsedYaml.getGroups()).isNotNull().containsKey(group);