Add new unit and refactor tests for YamlTemplateParsingHandler. 51/89151/2
authork.kedron <k.kedron@partner.samsung.com>
Mon, 3 Jun 2019 14:29:50 +0000 (16:29 +0200)
committerOren Kleks <orenkle@amdocs.com>
Wed, 5 Jun 2019 11:15:23 +0000 (11:15 +0000)
Issue-ID: SDC-2327
Signed-off-by: Krystian Kedron <k.kedron@partner.samsung.com>
Change-Id: Id3c8b6df65beddedf822748f517bd48585014df9

catalog-be/src/test/java/org/openecomp/sdc/ZipUtil.java
catalog-be/src/test/java/org/openecomp/sdc/be/components/impl/utils/YamlTemplateParsingHandlerTest.java

index cb68189..87e351b 100644 (file)
@@ -4,6 +4,8 @@
  * ================================================================================
  * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved.
  * ================================================================================
+ * Modifications Copyright (c) 2019 Samsung
+ * ================================================================================
  * 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
@@ -25,6 +27,7 @@ import org.apache.commons.io.output.ByteArrayOutputStream;
 
 import java.io.ByteArrayInputStream;
 import java.io.IOException;
+import java.net.URISyntaxException;
 import java.nio.file.Files;
 import java.nio.file.Path;
 import java.nio.file.Paths;
@@ -126,4 +129,15 @@ public class ZipUtil {
 
     }
 
+    private static byte[] loadResource(String resourceDir) throws IOException, URISyntaxException {
+
+        Path path = Paths.get(ZipUtil.class.getResource(resourceDir).toURI());
+        return Files.readAllBytes(path);
+    }
+
+    public static Map<String, byte[]> readData(String resourceDir) throws IOException, URISyntaxException {
+
+        byte[] data = loadResource(resourceDir);
+        return readZip(data);
+    }
 }
index 055276b..151303f 100644 (file)
@@ -1,37 +1,73 @@
+/*-
+ * ============LICENSE_START===============================================
+ * ONAP SDC
+ * ========================================================================
+ * Modifications Copyright (c) 2019 Samsung
+ * ========================================================================
+ * 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.components.impl.utils;
 
+import static org.assertj.core.api.Assertions.assertThat;
+import static org.junit.Assert.assertNotNull;
+import static org.mockito.ArgumentMatchers.eq;
+import static org.mockito.Mockito.when;
+
+import java.io.IOException;
+import java.net.URISyntaxException;
+import java.util.*;
+import java.util.stream.Collectors;
+
 import org.assertj.core.util.Lists;
 import org.junit.Before;
+import org.junit.BeforeClass;
 import org.junit.Test;
 import org.junit.runner.RunWith;
 import org.mockito.Mock;
 import org.mockito.junit.MockitoJUnitRunner;
+
+import org.openecomp.sdc.ZipUtil;
+import org.openecomp.sdc.be.components.csar.CsarInfo;
 import org.openecomp.sdc.be.components.csar.YamlTemplateParsingHandler;
 import org.openecomp.sdc.be.components.impl.AnnotationBusinessLogic;
 import org.openecomp.sdc.be.components.impl.GroupTypeBusinessLogic;
 import org.openecomp.sdc.be.components.validation.AnnotationValidator;
 import org.openecomp.sdc.be.dao.jsongraph.TitanDao;
 import org.openecomp.sdc.be.datatypes.elements.PropertyDataDefinition;
-import org.openecomp.sdc.be.impl.ComponentsUtils;
 import org.openecomp.sdc.be.model.*;
 import org.openecomp.sdc.be.model.operations.impl.AnnotationTypeOperations;
-import org.openecomp.sdc.common.util.ZipUtil;
 
-import java.io.File;
-import java.io.IOException;
-import java.io.StringReader;
-import java.nio.file.Files;
-import java.nio.file.Path;
-import java.nio.file.Paths;
-import java.util.*;
-import java.util.stream.Collectors;
-
-import static org.assertj.core.api.Assertions.assertThat;
-import static org.mockito.ArgumentMatchers.eq;
-import static org.mockito.Mockito.when;
 @RunWith(MockitoJUnitRunner.class)
 public class YamlTemplateParsingHandlerTest {
 
+    @Mock
+    private GroupTypeBusinessLogic groupTypeBusinessLogic;
+    @Mock
+    private AnnotationTypeOperations annotationTypeOperations;
+    @Mock
+    private AnnotationValidator annotationValidator;
+    @Mock
+    private TitanDao titanDao;
+    @Mock
+    private User user;
+
+    private YamlTemplateParsingHandler handler;
+
+    private static Map<String, byte[]> csar;
+    private static String resourceYml;
+
     private final static String VFC_GROUP_TYPE = "org.openecomp.groups.VfcInstanceGroup";
     private final static String HEAT_GROUP_TYPE = "org.openecomp.groups.heat.HeatStack";
     private final static String ROOT_GROUP_TYPE = "tosca.groups.Root";
@@ -40,140 +76,127 @@ public class YamlTemplateParsingHandlerTest {
     private final static GroupTypeDefinition rootGroupType = buildRootGroupType();
     private final static String CAPABILITY_TYPE = "org.openecomp.capabilities.VLANAssignment";
     private final static String CAPABILITY_NAME = "vlan_assignment";
-    public static final String csarsFilePath = System.getProperty("user.dir") + File.separator + "src" + File.separator + "test" + File.separator + "resources" + File.separator + "csars" ;
+    private static final String CSAR_FILE_PATH = "/csars/with_groups.csar";
 
-    private YamlTemplateParsingHandler handler;
-    private AnnotationBusinessLogic annotationBusinessLogic;
-    @Mock
-    private ComponentsUtils componentsUtils;
-    @Mock
-    private GroupTypeBusinessLogic groupTypeBusinessLogic;
-    @Mock
-    private AnnotationTypeOperations annotationTypeOperations;
-    @Mock
-    private AnnotationValidator annotationValidator;
-    @Mock
-    private TitanDao titanDao;
+    private static final String FILE_NAME = "MainServiceTemplate.yaml";
+
+    private static final String CSAR_UUID = "csarUUID";
+    private static final String RESOURCE_NAME = "resourceName";
+    private static final String MAIN_TEMPLATE_NAME = "Definitions/MainServiceTemplate.yaml";
+    private static final String NODE_NAME = "org.openecomp.resource.abstract.nodes.heat.mg";
+    private static final String MAIN_GROUP_NAME = "x_group";
+    private static final String NESTED_GROUP_NAME = "nested_mg_vepdg_group";
+
+    @BeforeClass()
+    public static void prepareData() throws IOException, URISyntaxException {
+        csar = ZipUtil.readData(CSAR_FILE_PATH);
+
+        Optional<String> keyOp = csar.keySet().stream().filter(k -> k.endsWith(FILE_NAME)).findAny();
+        byte[] mainTemplateService = keyOp.map(csar::get).orElse(null);
+        assertNotNull(mainTemplateService);
+
+        resourceYml = new String(mainTemplateService);
+    }
 
     @Before
-    public void init(){
-        annotationBusinessLogic = new AnnotationBusinessLogic(annotationTypeOperations, annotationValidator);
+    public void setup() {
+
+        AnnotationBusinessLogic annotationBusinessLogic = new AnnotationBusinessLogic(annotationTypeOperations,
+                annotationValidator);
         handler = new YamlTemplateParsingHandler(titanDao, groupTypeBusinessLogic, annotationBusinessLogic);
+        stubGetGroupType();
     }
 
     @Test
-    public void parseResourceInfoFromYAMLTest(){
-        Path path = Paths.get(csarsFilePath + File.separator + "with_groups.csar");
-        try {
-            Map<String, byte[]> csar = ZipUtil.readZip(Files.readAllBytes(path));
-            String fileName = "MainServiceTemplate.yaml";
-            Optional<String> keyOp = csar.keySet().stream().filter(k -> k.endsWith(fileName)).findAny();
-            byte[] mainTemplateService = csar.get(keyOp.get());
-            Properties props = new Properties();
-            String resourceYml = new String(mainTemplateService);
-            props.load(new StringReader(resourceYml.replace("\\","\\\\")));
-            Resource resource = new Resource();
-            
-            stubGetGroupType();
-            
-            ParsedToscaYamlInfo parsedYaml = handler.parseResourceInfoFromYAML(fileName, resourceYml, new HashMap<>(), new HashMap<>(), "");
-            
-            validateParsedYaml(parsedYaml);
-            
-        } catch (IOException e) {
-            e.printStackTrace();
-        }
+    public void parseResourceInfoFromOneNodeTest() {
+
+        String main_template_content = new String(csar.get(MAIN_TEMPLATE_NAME));
+        CsarInfo csarInfo = new CsarInfo(user, CSAR_UUID, csar, RESOURCE_NAME,
+                MAIN_TEMPLATE_NAME, main_template_content, true);
+
+        ParsedToscaYamlInfo parsedYaml = handler.parseResourceInfoFromYAML(FILE_NAME, resourceYml, new HashMap<>(),
+                csarInfo.extractNodeTypesInfo(), NODE_NAME);
+
+        validateParsedYaml(parsedYaml, NESTED_GROUP_NAME,
+                Lists.newArrayList("heat_file", "description"));
     }
 
+    @Test
+    public void parseResourceInfoFromYAMLTest() {
+
+        ParsedToscaYamlInfo parsedYaml = handler.parseResourceInfoFromYAML(FILE_NAME, resourceYml, new HashMap<>(),
+                new HashMap<>(), "");
+        validateParsedYamlWithCapability(parsedYaml);
+    }
+
+    private void validateParsedYaml(ParsedToscaYamlInfo parsedYaml, String group, List<String> expectedProp) {
+        assertThat(parsedYaml).isNotNull();
+        assertThat(parsedYaml.getGroups()).isNotNull().containsKey(group);
+        assertThat(parsedYaml.getGroups().get(group)).isNotNull();
+
+        assertThat(parsedYaml.getGroups().get(group).getProperties()).isNotNull();
+        assertThat(parsedYaml.getGroups().get(group).getProperties()
+                .stream()
+                .map(PropertyDataDefinition::getName)
+                .collect(Collectors.toList()))
+                .containsAll(expectedProp);
 
+        assertThat(parsedYaml.getGroups().get(group).getMembers()).isNotNull();
+    }
+
+       private void validateParsedYamlWithCapability(ParsedToscaYamlInfo parsedYaml) {
+
+        final List<String> expectedProp = Lists.newArrayList("vfc_parent_port_role",
+                "network_collection_function", "vfc_instance_group_function", "subinterface_role");
 
-       private void validateParsedYaml(ParsedToscaYamlInfo parsedYaml) {
-               assertThat(parsedYaml).isNotNull();
-               assertThat(parsedYaml.getGroups()).isNotNull().containsKey("x_group");
-               assertThat(parsedYaml.getGroups().get("x_group")).isNotNull();
-               assertThat(parsedYaml.getGroups().get("x_group").getProperties()).isNotNull();
-               assertThat(parsedYaml.getGroups().get("x_group").getProperties()
-                               .stream()
-                               .map(PropertyDataDefinition::getName)
-                               .collect(Collectors.toList()))
-               .containsAll(Lists.newArrayList("vfc_parent_port_role", "network_collection_function", "vfc_instance_group_function", "subinterface_role"));
-               assertThat(parsedYaml.getGroups().get("x_group").getCapabilities()
-                               .get(CAPABILITY_TYPE)
-                               .get(0).getProperties().get(0).getValue()).isEqualTo("success");
-               assertThat(parsedYaml.getGroups().get("x_group").getProperties()
-                               .stream()
-                               .map(PropertyDataDefinition::getName)
-                               .collect(Collectors.toList()))
-               .containsAll(Lists.newArrayList("vfc_parent_port_role", "network_collection_function", "vfc_instance_group_function", "subinterface_role"));
-               assertThat(parsedYaml.getGroups().get("x_group").getCapabilities()).isNotNull();
-               assertThat(parsedYaml.getGroups().get("x_group").getMembers()).isNotNull();
+        validateParsedYaml(parsedYaml, MAIN_GROUP_NAME, expectedProp);
+
+        assertThat(parsedYaml.getGroups().get(MAIN_GROUP_NAME).getCapabilities()
+                .get(CAPABILITY_TYPE)
+                .get(0).getProperties().get(0).getValue()).isEqualTo("success");
+        assertThat(parsedYaml.getGroups().get(MAIN_GROUP_NAME).getCapabilities()).isNotNull();
        }
 
        private void stubGetGroupType() {
                when(groupTypeBusinessLogic.getLatestGroupTypeByType(eq(VFC_GROUP_TYPE))).thenReturn(VfcInstanceGroupType);
                when(groupTypeBusinessLogic.getLatestGroupTypeByType(eq(HEAT_GROUP_TYPE))).thenReturn(heatGroupType);
         when(groupTypeBusinessLogic.getLatestGroupTypeByType(eq(ROOT_GROUP_TYPE))).thenReturn(rootGroupType);
-//        when(annotationBusinessLogic.validateAndMergeAnnotationsAndAssignToInput(any(Map.class))).thenReturn(null);
     }
 
     private static GroupTypeDefinition buildRootGroupType() {
-        GroupTypeDefinition groupType = new GroupTypeDefinition();
-        groupType.setType(ROOT_GROUP_TYPE);
-        groupType.setDescription("The TOSCA Group Type all other TOSCA Group Types derive from");
-        return groupType;
+        return createGroupTypeDefinition(ROOT_GROUP_TYPE, null,
+                "The TOSCA Group Type all other TOSCA Group Types derive from");
        }
 
        private static GroupTypeDefinition buildHeatStackGroupType() {
-        GroupTypeDefinition groupType = new GroupTypeDefinition();
-        groupType.setType(HEAT_GROUP_TYPE);
-        groupType.setDerivedFrom("tosca.groups.Root");
-        groupType.setDescription("Grouped all heat resources which are in the same heat stack");
-                     
-        GroupProperty property1 = new GroupProperty();
-        property1.setName("heat_file");
-        property1.setType("string");
-        property1.setRequired(true);
-        property1.setDescription("Heat file which associate to this group/heat stack");
-        property1.setStatus("SUPPORTED");
-
-        GroupProperty property2 = new GroupProperty();
-        property2.setName("description");
-        property2.setType("string");
-        property2.setRequired(true);
-        property2.setDescription("group description");
-        property2.setStatus("SUPPORTED");
+        GroupTypeDefinition groupType = createGroupTypeDefinition(HEAT_GROUP_TYPE, "tosca.groups.Root",
+                "Grouped all heat resources which are in the same heat stack");
+
+        GroupProperty property1 = createGroupProperty("heat_file",
+                "Heat file which associate to this group/heat stack", "SUPPORTED");
+
+        GroupProperty property2 = createGroupProperty("description",
+                "Group description", "SUPPORTED");
+
         groupType.setProperties(Lists.newArrayList(property1, property2));
         return groupType;
        }
 
        private static GroupTypeDefinition buildVfcInstanceGroupType() {
-        GroupTypeDefinition groupType = new GroupTypeDefinition();
-        groupType.setType(VFC_GROUP_TYPE);
-        groupType.setDerivedFrom("tosca.groups.Root");
-        groupType.setDescription("groups VFCs with same parent port role");
-        GroupProperty property1 = new GroupProperty();
-        property1.setName("vfc_instance_group_function");
-        property1.setType("string");
-        property1.setRequired(true);
-        property1.setDescription("function of this VFC group");
-
-        GroupProperty property2 = new GroupProperty();
-        property2.setName("vfc_parent_port_role");
-        property2.setType("string");
-        property2.setRequired(true);
-        property2.setDescription("common role of parent ports of VFCs in this group");
-
-        GroupProperty property3 = new GroupProperty();
-        property3.setName("network_collection_function");
-        property3.setType("string");
-        property3.setRequired(true);
-        property3.setDescription("network collection function assigned to this group");
-
-        GroupProperty property4 = new GroupProperty();
-        property4.setName("subinterface_role");
-        property4.setType("string");
-        property4.setRequired(true);
-        property4.setDescription("common role of subinterfaces of VFCs in this group, criteria the group is created");
+        GroupTypeDefinition groupType = createGroupTypeDefinition(VFC_GROUP_TYPE, "tosca.groups.Root",
+                "Groups of VFCs with same parent port role");
+
+        GroupProperty property1 = createGroupProperty("vfc_instance_group_function",
+                "Function of this VFC group", null);
+
+        GroupProperty property2 = createGroupProperty("vfc_parent_port_role",
+                "Common role of parent ports of VFCs in this group", null);
+
+        GroupProperty property3 = createGroupProperty("network_collection_function",
+                "Network collection function assigned to this group", null);
+
+        GroupProperty property4 = createGroupProperty("subinterface_role",
+                "Common role of subinterfaces of VFCs in this group, criteria the group is created", null);
 
         groupType.setProperties(Lists.newArrayList(property1, property2, property3, property4));
 
@@ -183,7 +206,7 @@ public class YamlTemplateParsingHandlerTest {
         ComponentInstanceProperty capabilityProperty = new ComponentInstanceProperty();
         capabilityProperty.setName("vfc_instance_group_reference");
         capabilityProperty.setType("string");
-        capability.setProperties(Arrays.asList(capabilityProperty));
+        capability.setProperties(Collections.singletonList(capabilityProperty));
 
         Map<String, CapabilityDefinition> capabilityMap = new HashMap<>();
         capabilityMap.put(CAPABILITY_NAME, capability);
@@ -191,4 +214,39 @@ public class YamlTemplateParsingHandlerTest {
         return groupType;
     }
 
+    private static GroupTypeDefinition createGroupTypeDefinition(String type, String derivedFrom, String description){
+        GroupTypeDefinition property = new GroupTypeDefinition();
+
+        if (type != null)
+            property.setType(type);
+
+        if (derivedFrom != null) {
+            property.setDerivedFrom(derivedFrom);
+        }
+
+        if (description != null) {
+            property.setDescription(description);
+        }
+
+        return property;
+    }
+    private static GroupProperty createGroupProperty(String name, String description,
+                                                     String status){
+        GroupProperty property = new GroupProperty();
+        if (name != null)
+            property.setName(name);
+
+        if (description != null) {
+            property.setDescription(description);
+        }
+
+        if (status != null) {
+            property.setStatus(status);
+        }
+
+        property.setType("string");
+        property.setRequired(true);
+
+        return property;
+    }
 }