Rename the groupfilter.config System Property
[aai/babel.git] / src / test / java / org / onap / aai / babel / parser / TestArtifactGeneratorToscaParser.java
1 /**
2  * ============LICENSE_START=======================================================
3  * org.onap.aai
4  * ================================================================================
5  * Copyright © 2017-2019 AT&T Intellectual Property. All rights reserved.
6  * Copyright © 2017-2019 European Software Marketing Ltd.
7  * ================================================================================
8  * Licensed under the Apache License, Version 2.0 (the "License");
9  * you may not use this file except in compliance with the License.
10  * You may obtain a copy of the License at
11  *
12  *       http://www.apache.org/licenses/LICENSE-2.0
13  *
14  * Unless required by applicable law or agreed to in writing, software
15  * distributed under the License is distributed on an "AS IS" BASIS,
16  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17  * See the License for the specific language governing permissions and
18  * limitations under the License.
19  * ============LICENSE_END=========================================================
20  */
21
22 package org.onap.aai.babel.parser;
23
24 import static org.hamcrest.CoreMatchers.equalTo;
25 import static org.hamcrest.MatcherAssert.assertThat;
26 import static org.hamcrest.Matchers.is;
27
28 import java.util.ArrayList;
29 import java.util.Collections;
30 import java.util.LinkedHashMap;
31 import java.util.List;
32 import org.junit.Test;
33 import org.mockito.Mockito;
34 import org.onap.aai.babel.xml.generator.data.WidgetConfigurationUtil;
35 import org.onap.aai.babel.xml.generator.data.WidgetMapping;
36 import org.onap.aai.babel.xml.generator.model.Resource;
37 import org.onap.aai.babel.xml.generator.model.Widget.Type;
38 import org.onap.sdc.tosca.parser.api.ISdcCsarHelper;
39 import org.onap.sdc.toscaparser.api.Group;
40 import org.onap.sdc.toscaparser.api.NodeTemplate;
41 import org.onap.sdc.toscaparser.api.SubstitutionMappings;
42
43 /**
44  * Direct tests of the TOSCA parser-based Artifact Generator {@link ArtifactGeneratorToscaParser}., to cover exceptional
45  * cases.
46  */
47
48 public class TestArtifactGeneratorToscaParser {
49
50     private static final String TEST_UUID = "1234";
51
52     /**
53      * Process an Allotted Resource that does not have a Providing Service.
54      */
55     @Test(expected = IllegalArgumentException.class)
56     public void testMissingProvidingService() {
57         List<NodeTemplate> nodeTemplateList = Collections.singletonList(buildNodeTemplate("name", "BlockStorage"));
58         new ArtifactGeneratorToscaParser(null).processResourceModels(new Resource(Type.ALLOTTED_RESOURCE, true),
59                 nodeTemplateList);
60     }
61
62     /**
63      * Add a CR (a type of Resource which is not a Providing Service) to a Resource Model.
64      */
65     @Test(expected = IllegalArgumentException.class)
66     public void testAddResourceNotProvidingService() {
67         List<NodeTemplate> nodeTemplateList = Collections.singletonList(buildNodeTemplate("testCR", "CR"));
68         // Create any Resource to which the CR can be added
69         final Resource dummyResource = new Resource(Type.ALLOTTED_RESOURCE, true);
70         new ArtifactGeneratorToscaParser(null).processResourceModels(dummyResource, nodeTemplateList);
71     }
72
73     /**
74      * Initialise the Artifact Generator Widget Mapping config with incomplete data.
75      */
76     @Test(expected = IllegalArgumentException.class)
77     public void testToscaMappingWithoutType() {
78         WidgetMapping invalidMapping = new WidgetMapping();
79         invalidMapping.setType(null);
80         WidgetConfigurationUtil.setWidgetMappings(Collections.singletonList(invalidMapping));
81     }
82
83     /**
84      * Initialise the Artifact Generator Widget Mapping config with incomplete data.
85      */
86     @Test(expected = IllegalArgumentException.class)
87     public void testToscaMappingWithoutWidget() {
88         WidgetMapping invalidMapping = new WidgetMapping();
89         invalidMapping.setWidget(null);
90         WidgetConfigurationUtil.setWidgetMappings(Collections.singletonList(invalidMapping));
91     }
92
93     /**
94      * Process a dummy Group object for a Service Resource.
95      */
96     @Test
97     public void testInstanceGroups() {
98         final String instanceGroupType = "org.openecomp.groups.ResourceInstanceGroup";
99         WidgetConfigurationUtil.setSupportedInstanceGroups(Collections.singletonList(instanceGroupType));
100
101         ISdcCsarHelper helper = Mockito.mock(ISdcCsarHelper.class);
102         SubstitutionMappings sm = Mockito.mock(SubstitutionMappings.class);
103
104         NodeTemplate serviceNodeTemplate =
105                 buildNodeTemplate("service", "org.openecomp.resource.cr.a-collection-resource");
106         serviceNodeTemplate.setSubMappingToscaTemplate(sm);
107         Mockito.when(helper.getNodeTemplateByName(serviceNodeTemplate.getName())).thenReturn(serviceNodeTemplate);
108
109         ArrayList<Group> groups = new ArrayList<>();
110         groups.add(buildGroup("group", instanceGroupType));
111         Mockito.when(helper.getGroupsOfOriginOfNodeTemplate(serviceNodeTemplate)).thenReturn(groups);
112
113         ArtifactGeneratorToscaParser parser = new ArtifactGeneratorToscaParser(helper);
114         List<Resource> resources =
115                 parser.processInstanceGroups(new Resource(Type.INSTANCE_GROUP, true), serviceNodeTemplate);
116
117         assertThat(resources.size(), is(1));
118         Resource resource = resources.get(0);
119         assertThat(resource.getModelNameVersionId(), is(equalTo(TEST_UUID)));
120     }
121
122     /**
123      * Create a NodeTemplate for unit testing purposes. In production code this object would only be created by the
124      * sdc-tosca parser.
125      *
126      * @param name
127      *            name of the NodeTemplate
128      * @param type
129      *            type of the NodeTemplate
130      * @return a new NodeTemplate object
131      */
132     private NodeTemplate buildNodeTemplate(String name, String type) {
133         LinkedHashMap<String, Object> nodeTemplateMap = new LinkedHashMap<>();
134         nodeTemplateMap.put(name, buildMap("type", type));
135         nodeTemplateMap.put(type, buildNodeTemplateCustomDefs());
136         return new NodeTemplate(name, nodeTemplateMap, nodeTemplateMap, null, null);
137     }
138
139     private LinkedHashMap<String, Object> buildNodeTemplateCustomDefs() {
140         LinkedHashMap<String, Object> customDefs = buildCustomDefs();
141         customDefs.put("attributes", null);
142         customDefs.put("requirements", null);
143         customDefs.put("capabilities", null);
144         customDefs.put("artifacts", null);
145         return customDefs;
146     }
147
148     private Group buildGroup(String name, String type) {
149         LinkedHashMap<String, Object> template = new LinkedHashMap<>();
150         template.put("type", type);
151         template.put("metadata", new LinkedHashMap<>());
152         template.put("properties", buildMap("UUID", TEST_UUID));
153         LinkedHashMap<String, Object> customDefMap = buildMap(name, template);
154         customDefMap.put(type, buildGroupCustomDefs());
155         return new Group(name, template, null, customDefMap);
156     }
157
158     private LinkedHashMap<String, Object> buildGroupCustomDefs() {
159         LinkedHashMap<String, Object> customDefs = buildCustomDefs();
160         customDefs.put("members", null);
161         return customDefs;
162     }
163
164     private LinkedHashMap<String, Object> buildCustomDefs() {
165         LinkedHashMap<String, Object> customDefs = new LinkedHashMap<>();
166         customDefs.put("derived_from", null);
167         customDefs.put("metadata", null);
168         customDefs.put("version", null);
169         customDefs.put("description", null);
170         customDefs.put("interfaces", null);
171         customDefs.put("properties", buildMap("UUID", buildMap("type", "java.lang.String")));
172         return customDefs;
173     }
174
175     private LinkedHashMap<String, Object> buildMap(String key, Object value) {
176         LinkedHashMap<String, Object> map = new LinkedHashMap<>();
177         map.put(key, value);
178         return map;
179     }
180 }