Add support for ResourceInstanceGroup

Refactor existing code and add a Junit test for the relevant method.

Change-Id: I66703f1d2e93173df4676070faf66e289360609a
Issue-ID: AAI-1884
Signed-off-by: mark.j.leonard <mark.j.leonard@gmail.com>
diff --git a/src/main/java/org/onap/aai/babel/parser/ArtifactGeneratorToscaParser.java b/src/main/java/org/onap/aai/babel/parser/ArtifactGeneratorToscaParser.java
index e5141a5..80b75b0 100644
--- a/src/main/java/org/onap/aai/babel/parser/ArtifactGeneratorToscaParser.java
+++ b/src/main/java/org/onap/aai/babel/parser/ArtifactGeneratorToscaParser.java
@@ -31,7 +31,6 @@
 import java.util.Optional;
 import java.util.Properties;
 import java.util.stream.Collectors;
-
 import org.onap.aai.babel.logging.ApplicationMsgs;
 import org.onap.aai.babel.logging.LogHelper;
 import org.onap.aai.babel.xml.generator.data.WidgetConfigurationUtil;
@@ -234,13 +233,14 @@
      * @param serviceNode
      * @return resources for which XML Models should be generated
      */
-    private List<Resource> processInstanceGroups(Model resourceModel, NodeTemplate serviceNode) {
+    List<Resource> processInstanceGroups(Model resourceModel, NodeTemplate serviceNode) {
         List<Resource> resources = new ArrayList<>();
         if (csarHelper.getNodeTemplateByName(serviceNode.getName()).getSubMappingToscaTemplate() != null) {
             List<Group> serviceGroups = csarHelper.getGroupsOfOriginOfNodeTemplate(serviceNode);
             for (Group group : serviceGroups) {
                 if (WidgetConfigurationUtil.isSupportedInstanceGroup(group.getType())) {
-                    resources.addAll(processInstanceGroup(resourceModel, group));
+                    resources.addAll(processInstanceGroup(resourceModel, group.getMemberNodes(),
+                            group.getMetadata().getAllProperties(), group.getProperties()));
                 }
             }
         }
@@ -248,43 +248,57 @@
     }
 
     /**
-     * Create an Instance Group Model for the supplied Service Group and relate this to the supplied resource Model.
+     * Create an Instance Group Model and populate it with the supplied data.
      *
      * @param resourceModel the Resource node template Model
-     * @param group the Service Group
+     * @param memberNodes the Resources and Widgets belonging to the Group
+     * @param metaProperties the metadata of the Group
+     * @param properties the properties of the Group
      * @return the Instance Group and Member resource models
      */
-    private List<Resource> processInstanceGroup(Model resourceModel, Group group) {
+    private List<Resource> processInstanceGroup(Model resourceModel, ArrayList<NodeTemplate> memberNodes,
+            Map<String, String> metaProperties, Map<String, Property> properties) {
         List<Resource> resources = new ArrayList<>();
 
         Resource groupModel = new InstanceGroup();
-        groupModel.populateModelIdentificationInformation(group.getMetadata().getAllProperties());
-        groupModel.populateModelIdentificationInformation(populateStringProperties(group.getProperties()));
+        groupModel.populateModelIdentificationInformation(metaProperties);
+        groupModel.populateModelIdentificationInformation(populateStringProperties(properties));
 
         resourceModel.addResource(groupModel);
         resources.add(groupModel);
 
-        List<NodeTemplate> members = group.getMemberNodes();
-        if (members != null && !members.isEmpty()) {
-            for (NodeTemplate nodeTemplate : members) {
-                String nodeTypeName = normaliseNodeTypeName(nodeTemplate);
-                Model memberModel = Model.getModelFor(nodeTypeName, nodeTemplate.getMetaData().getValue("type"));
-                memberModel.populateModelIdentificationInformation(nodeTemplate.getMetaData().getAllProperties());
-                if (memberModel instanceof Resource) {
-                    log.debug("Generating grouped Resource " + nodeTypeName);
-                    groupModel.addResource((Resource) memberModel);
-                    resources.add((Resource) memberModel);
-                } else {
-                    log.debug("Generating grouped Widget " + nodeTypeName);
-                    groupModel.addWidget((Widget) memberModel);
-                }
-            }
+        if (memberNodes != null && !memberNodes.isEmpty()) {
+            resources.addAll(generateResourcesAndWidgets(memberNodes, groupModel));
         }
 
         return resources;
     }
 
     /**
+     * @param memberNodes
+     * @param groupModel
+     * @return
+     */
+    private List<Resource> generateResourcesAndWidgets(final ArrayList<NodeTemplate> memberNodes,
+            final Resource groupModel) {
+        List<Resource> resources = new ArrayList<>();
+        for (NodeTemplate nodeTemplate : memberNodes) {
+            String nodeTypeName = normaliseNodeTypeName(nodeTemplate);
+            Model memberModel = Model.getModelFor(nodeTypeName, nodeTemplate.getMetaData().getValue("type"));
+            memberModel.populateModelIdentificationInformation(nodeTemplate.getMetaData().getAllProperties());
+
+            log.debug(String.format("Generating grouped %s (%s) from TOSCA type %s",
+                    memberModel.getClass().getSuperclass().getSimpleName(), memberModel.getClass(), nodeTypeName));
+
+            addRelatedModel(groupModel, memberModel);
+            if (memberModel instanceof Resource) {
+                resources.add((Resource) memberModel);
+            }
+        }
+        return resources;
+    }
+
+    /**
      * Add the supplied Node Template to the Service, provided that it is a valid Resource or Widget. If the Node
      * Template is a Resource type, this is also recorded in the supplied nodesById Map.
      *
@@ -300,16 +314,26 @@
                 model.populateModelIdentificationInformation(nodeTemplate.getMetaData().getAllProperties());
             }
 
+            addRelatedModel(service, model);
             if (model instanceof Resource) {
                 nodesById.put(model.getModelNameVersionId(), nodeTypeName);
-                service.addResource((Resource) model);
-            } else {
-                service.addWidget((Widget) model);
             }
         }
     }
 
     /**
+     * @param model
+     * @param relation
+     */
+    private void addRelatedModel(final Model model, final Model relation) {
+        if (relation instanceof Resource) {
+            model.addResource((Resource) relation);
+        } else {
+            model.addWidget((Widget) relation);
+        }
+    }
+
+    /**
      * Process TOSCA Group information for VF Modules.
      *
      * @param resources
diff --git a/src/test/java/org/onap/aai/babel/parser/TestArtifactGeneratorToscaParser.java b/src/test/java/org/onap/aai/babel/parser/TestArtifactGeneratorToscaParser.java
index a74efaf..822cda5 100644
--- a/src/test/java/org/onap/aai/babel/parser/TestArtifactGeneratorToscaParser.java
+++ b/src/test/java/org/onap/aai/babel/parser/TestArtifactGeneratorToscaParser.java
@@ -21,11 +21,25 @@
 
 package org.onap.aai.babel.parser;
 
+import static org.hamcrest.CoreMatchers.equalTo;
+import static org.hamcrest.MatcherAssert.assertThat;
+import static org.hamcrest.Matchers.is;
+
+import java.util.ArrayList;
 import java.util.Collections;
 import java.util.LinkedHashMap;
 import java.util.List;
+import java.util.Properties;
 import org.junit.Test;
+import org.mockito.Mockito;
+import org.onap.aai.babel.xml.generator.data.WidgetConfigurationUtil;
+import org.onap.aai.babel.xml.generator.model.InstanceGroup;
+import org.onap.aai.babel.xml.generator.model.Model;
+import org.onap.aai.babel.xml.generator.model.Resource;
+import org.onap.sdc.tosca.parser.api.ISdcCsarHelper;
+import org.onap.sdc.toscaparser.api.Group;
 import org.onap.sdc.toscaparser.api.NodeTemplate;
+import org.onap.sdc.toscaparser.api.SubstitutionMappings;
 
 /**
  * Direct tests of the TOSCA parser-based Artifact Generator, to cover exceptional cases.
@@ -33,7 +47,7 @@
 
 public class TestArtifactGeneratorToscaParser {
 
-    ArtifactGeneratorToscaParser parser = new ArtifactGeneratorToscaParser(null);
+    private static final String TEST_UUID = "1234";
 
     /**
      * Process a dummy Node Template object for a Service. A WARNING should be logged for the missing metadata.
@@ -41,16 +55,89 @@
     @Test
     public void testMissingServiceData() {
         List<NodeTemplate> nodeTemplateList = Collections.singletonList(buildNodeTemplate("name", "BlockStorage"));
+        ArtifactGeneratorToscaParser parser = new ArtifactGeneratorToscaParser(null);
         parser.processServiceTosca(null, Collections.emptyMap(), nodeTemplateList);
         parser.processResourceToscas(nodeTemplateList, null);
     }
 
-    private NodeTemplate buildNodeTemplate(String name, String type) {
-        LinkedHashMap<String, Object> nodeTemplateMap = new LinkedHashMap<>();
-        LinkedHashMap<String, Object> template = new LinkedHashMap<>();
-        template.put("type", type);
-        nodeTemplateMap.put(name, template);
-        return new NodeTemplate(name, nodeTemplateMap, null, null, null);
+    /**
+     * Process a dummy Group object for a Service Resource.
+     */
+    @Test
+    public void testInstanceGroups() {
+        final String instanceGroupType = "org.openecomp.groups.ResourceInstanceGroup";
+        Properties props = new Properties();
+        props.put("AAI.instance-group-types", instanceGroupType);
+        WidgetConfigurationUtil.setFilterConfig(props);
+
+        ISdcCsarHelper helper = Mockito.mock(ISdcCsarHelper.class);
+        NodeTemplate serviceNodeTemplate = Mockito.mock(NodeTemplate.class);
+        SubstitutionMappings sm = Mockito.mock(SubstitutionMappings.class);
+
+        Mockito.when(serviceNodeTemplate.getSubMappingToscaTemplate()).thenReturn(sm);
+
+        NodeTemplate serviceNode = buildNodeTemplate("service", "org.openecomp.resource.cr.a-collection-resource");
+        Mockito.when(helper.getNodeTemplateByName(serviceNode.getName())).thenReturn(serviceNodeTemplate);
+
+        ArrayList<Group> groups = new ArrayList<>();
+        groups.add(buildGroup("group", instanceGroupType));
+        Mockito.when(helper.getGroupsOfOriginOfNodeTemplate(serviceNode)).thenReturn(groups);
+
+        ArtifactGeneratorToscaParser parser = new ArtifactGeneratorToscaParser(helper);
+        Model resourceModel = new InstanceGroup();
+        List<Resource> resources = parser.processInstanceGroups(resourceModel, serviceNode);
+
+        assertThat(resources.size(), is(1));
+        Resource resource = resources.get(0);
+        assertThat(resource.getModelNameVersionId(), is(equalTo(TEST_UUID)));
     }
 
+    private NodeTemplate buildNodeTemplate(String name, String type) {
+        LinkedHashMap<String, Object> nodeTemplateMap = new LinkedHashMap<>();
+        nodeTemplateMap.put(name, buildMap("type", type));
+        nodeTemplateMap.put(type, buildNodeTemplateCustomDefs());
+        return new NodeTemplate(name, nodeTemplateMap, nodeTemplateMap, null, null);
+    }
+
+    private LinkedHashMap<String, Object> buildNodeTemplateCustomDefs() {
+        LinkedHashMap<String, Object> customDefs = buildCustomDefs();
+        customDefs.put("attributes", null);
+        customDefs.put("requirements", null);
+        customDefs.put("capabilities", null);
+        customDefs.put("artifacts", null);
+        return customDefs;
+    }
+
+    private Group buildGroup(String name, String type) {
+        LinkedHashMap<String, Object> template = new LinkedHashMap<>();
+        template.put("type", type);
+        template.put("metadata", new LinkedHashMap<>());
+        template.put("properties", buildMap("UUID", TEST_UUID));
+        LinkedHashMap<String, Object> customDefMap = buildMap(name, template);
+        customDefMap.put(type, buildGroupCustomDefs());
+        return new Group(name, template, null, customDefMap);
+    }
+
+    private LinkedHashMap<String, Object> buildGroupCustomDefs() {
+        LinkedHashMap<String, Object> customDefs = buildCustomDefs();
+        customDefs.put("members", null);
+        return customDefs;
+    }
+
+    private LinkedHashMap<String, Object> buildCustomDefs() {
+        LinkedHashMap<String, Object> customDefs = new LinkedHashMap<>();
+        customDefs.put("derived_from", null);
+        customDefs.put("metadata", null);
+        customDefs.put("version", null);
+        customDefs.put("description", null);
+        customDefs.put("interfaces", null);
+        customDefs.put("properties", buildMap("UUID", buildMap("type", "java.lang.String")));
+        return customDefs;
+    }
+
+    private LinkedHashMap<String, Object> buildMap(String key, Object value) {
+        LinkedHashMap<String, Object> map = new LinkedHashMap<>();
+        map.put(key, value);
+        return map;
+    }
 }
diff --git a/src/test/resources/filter-types.properties b/src/test/resources/filter-types.properties
index fcf139f..8577841 100644
--- a/src/test/resources/filter-types.properties
+++ b/src/test/resources/filter-types.properties
@@ -1 +1 @@
-AAI.instance-group-types=org.openecomp.groups.NetworkCollection,org.openecomp.groups.VfcInstanceGroup
+AAI.instance-group-types=org.openecomp.groups.NetworkCollection,org.openecomp.groups.VfcInstanceGroup,org.openecomp.groups.ResourceInstanceGroup