Rename the groupfilter.config System Property
[aai/babel.git] / src / test / java / org / onap / aai / babel / parser / TestArtifactGeneratorToscaParser.java
index 822cda5..a538373 100644 (file)
@@ -2,8 +2,8 @@
  * ============LICENSE_START=======================================================
  * org.onap.aai
  * ================================================================================
- * Copyright © 2017-2018 AT&T Intellectual Property. All rights reserved.
- * Copyright © 2017-2018 European Software Marketing Ltd.
+ * Copyright © 2017-2019 AT&T Intellectual Property. All rights reserved.
+ * Copyright © 2017-2019 European Software Marketing Ltd.
  * ================================================================================
  * Licensed under the Apache License, Version 2.0 (the "License");
  * you may not use this file except in compliance with the License.
@@ -29,20 +29,20 @@ 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.data.WidgetMapping;
 import org.onap.aai.babel.xml.generator.model.Resource;
+import org.onap.aai.babel.xml.generator.model.Widget.Type;
 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.
+ * Direct tests of the TOSCA parser-based Artifact Generator {@link ArtifactGeneratorToscaParser}., to cover exceptional
+ * cases.
  */
 
 public class TestArtifactGeneratorToscaParser {
@@ -50,14 +50,44 @@ public class TestArtifactGeneratorToscaParser {
     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.
+     * Process an Allotted Resource that does not have a Providing Service.
      */
-    @Test
-    public void testMissingServiceData() {
+    @Test(expected = IllegalArgumentException.class)
+    public void testMissingProvidingService() {
         List<NodeTemplate> nodeTemplateList = Collections.singletonList(buildNodeTemplate("name", "BlockStorage"));
-        ArtifactGeneratorToscaParser parser = new ArtifactGeneratorToscaParser(null);
-        parser.processServiceTosca(null, Collections.emptyMap(), nodeTemplateList);
-        parser.processResourceToscas(nodeTemplateList, null);
+        new ArtifactGeneratorToscaParser(null).processResourceModels(new Resource(Type.ALLOTTED_RESOURCE, true),
+                nodeTemplateList);
+    }
+
+    /**
+     * Add a CR (a type of Resource which is not a Providing Service) to a Resource Model.
+     */
+    @Test(expected = IllegalArgumentException.class)
+    public void testAddResourceNotProvidingService() {
+        List<NodeTemplate> nodeTemplateList = Collections.singletonList(buildNodeTemplate("testCR", "CR"));
+        // Create any Resource to which the CR can be added
+        final Resource dummyResource = new Resource(Type.ALLOTTED_RESOURCE, true);
+        new ArtifactGeneratorToscaParser(null).processResourceModels(dummyResource, nodeTemplateList);
+    }
+
+    /**
+     * Initialise the Artifact Generator Widget Mapping config with incomplete data.
+     */
+    @Test(expected = IllegalArgumentException.class)
+    public void testToscaMappingWithoutType() {
+        WidgetMapping invalidMapping = new WidgetMapping();
+        invalidMapping.setType(null);
+        WidgetConfigurationUtil.setWidgetMappings(Collections.singletonList(invalidMapping));
+    }
+
+    /**
+     * Initialise the Artifact Generator Widget Mapping config with incomplete data.
+     */
+    @Test(expected = IllegalArgumentException.class)
+    public void testToscaMappingWithoutWidget() {
+        WidgetMapping invalidMapping = new WidgetMapping();
+        invalidMapping.setWidget(null);
+        WidgetConfigurationUtil.setWidgetMappings(Collections.singletonList(invalidMapping));
     }
 
     /**
@@ -66,32 +96,39 @@ public class TestArtifactGeneratorToscaParser {
     @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);
+        WidgetConfigurationUtil.setSupportedInstanceGroups(Collections.singletonList(instanceGroupType));
 
         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);
+        NodeTemplate serviceNodeTemplate =
+                buildNodeTemplate("service", "org.openecomp.resource.cr.a-collection-resource");
+        serviceNodeTemplate.setSubMappingToscaTemplate(sm);
+        Mockito.when(helper.getNodeTemplateByName(serviceNodeTemplate.getName())).thenReturn(serviceNodeTemplate);
 
         ArrayList<Group> groups = new ArrayList<>();
         groups.add(buildGroup("group", instanceGroupType));
-        Mockito.when(helper.getGroupsOfOriginOfNodeTemplate(serviceNode)).thenReturn(groups);
+        Mockito.when(helper.getGroupsOfOriginOfNodeTemplate(serviceNodeTemplate)).thenReturn(groups);
 
         ArtifactGeneratorToscaParser parser = new ArtifactGeneratorToscaParser(helper);
-        Model resourceModel = new InstanceGroup();
-        List<Resource> resources = parser.processInstanceGroups(resourceModel, serviceNode);
+        List<Resource> resources =
+                parser.processInstanceGroups(new Resource(Type.INSTANCE_GROUP, true), serviceNodeTemplate);
 
         assertThat(resources.size(), is(1));
         Resource resource = resources.get(0);
         assertThat(resource.getModelNameVersionId(), is(equalTo(TEST_UUID)));
     }
 
+    /**
+     * Create a NodeTemplate for unit testing purposes. In production code this object would only be created by the
+     * sdc-tosca parser.
+     *
+     * @param name
+     *            name of the NodeTemplate
+     * @param type
+     *            type of the NodeTemplate
+     * @return a new NodeTemplate object
+     */
     private NodeTemplate buildNodeTemplate(String name, String type) {
         LinkedHashMap<String, Object> nodeTemplateMap = new LinkedHashMap<>();
         nodeTemplateMap.put(name, buildMap("type", type));