Remove all references to artifactgenerator config
[aai/babel.git] / src / test / java / org / onap / aai / babel / parser / TestArtifactGeneratorToscaParser.java
1 /**
2  * ============LICENSE_START=======================================================
3  * org.onap.aai
4  * ================================================================================
5  * Copyright (c) 2017-2019 AT&T Intellectual Property. All rights reserved.
6  * Copyright (c) 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.io.IOException;
29 import java.util.ArrayList;
30 import java.util.Collections;
31 import java.util.LinkedHashMap;
32 import java.util.List;
33 import org.junit.Test;
34 import org.mockito.Mockito;
35 import org.onap.aai.babel.util.ArtifactTestUtils;
36 import org.onap.aai.babel.util.Resources;
37 import org.onap.aai.babel.xml.generator.XmlArtifactGenerationException;
38 import org.onap.aai.babel.xml.generator.data.WidgetConfigurationUtil;
39 import org.onap.aai.babel.xml.generator.data.WidgetMapping;
40 import org.onap.aai.babel.xml.generator.model.Model;
41 import org.onap.aai.babel.xml.generator.model.Resource;
42 import org.onap.aai.babel.xml.generator.model.Service;
43 import org.onap.aai.babel.xml.generator.model.WidgetType;
44 import org.onap.aai.babel.xml.generator.types.ModelType;
45 import org.onap.sdc.tosca.parser.api.ISdcCsarHelper;
46 import org.onap.sdc.toscaparser.api.Group;
47 import org.onap.sdc.toscaparser.api.NodeTemplate;
48 import org.onap.sdc.toscaparser.api.SubstitutionMappings;
49
50 /**
51  * Direct tests of the TOSCA parser-based Artifact Generator {@link ArtifactGeneratorToscaParser}., to cover exceptional
52  * cases.
53  */
54
55 public class TestArtifactGeneratorToscaParser {
56
57     private static final String TEST_UUID = "1234";
58
59     /**
60      * Initialize the Generator with an invalid mappings file path.
61      *
62      * @throws IOException
63      *             if the file content could not be read successfully
64      */
65     @Test(expected = IllegalArgumentException.class)
66     public void testMissingMappingsFile() throws IOException {
67         ArtifactGeneratorToscaParser.initToscaMappingsConfiguration("non-existent.file");
68     }
69
70     /**
71      * Initialize the Generator with no Widget Mappings content.
72      *
73      * @throws IOException
74      *             if the file content could not be read successfully
75      */
76     @Test(expected = IOException.class)
77     public void testMissingMappingsContent() throws IOException {
78         String invalidJson = new ArtifactTestUtils().getResourcePath(Resources.EMPTY_TOSCA_MAPPING_CONFIG);
79         ArtifactGeneratorToscaParser.initToscaMappingsConfiguration(invalidJson);
80     }
81
82     /**
83      * Initialize the Generator with invalid Widget Mappings content.
84      *
85      * @throws IOException
86      *             if the file content could not be read successfully
87      */
88     @Test(expected = IOException.class)
89     public void testInvalidMappingsContent() throws IOException {
90         String invalidJson = new ArtifactTestUtils().getResourcePath(Resources.INVALID_TOSCA_MAPPING_CONFIG);
91         ArtifactGeneratorToscaParser.initToscaMappingsConfiguration(invalidJson);
92     }
93
94     /**
95      * Process an Allotted Resource that does not have a Providing Service.
96      */
97     @Test(expected = IllegalArgumentException.class)
98     public void testMissingProvidingService() {
99         List<NodeTemplate> nodeTemplateList = Collections.singletonList(buildNodeTemplate("name", "BlockStorage"));
100         new ArtifactGeneratorToscaParser(null)
101                 .processResourceModels(new Resource(WidgetType.valueOf("ALLOTTED_RESOURCE"), true), nodeTemplateList);
102     }
103
104     /**
105      * Add a CR (a type of Resource which is not a Providing Service) to a Resource Model.
106      */
107     @Test(expected = IllegalArgumentException.class)
108     public void testAddResourceNotProvidingService() {
109         List<NodeTemplate> nodeTemplateList = Collections.singletonList(buildNodeTemplate("testCR", "CR"));
110         // Create any Resource to which the CR can be added
111         final Resource dummyResource = new Resource(WidgetType.valueOf("ALLOTTED_RESOURCE"), true);
112         new ArtifactGeneratorToscaParser(null).processResourceModels(dummyResource, nodeTemplateList);
113     }
114
115     /**
116      * Initialize the Artifact Generator Widget Mapping config with incomplete data (no type).
117      *
118      * @throws IOException
119      *             if a WidgetMapping is invalid
120      */
121     @Test(expected = IOException.class)
122     public void testToscaMappingWithoutType() throws IOException {
123         WidgetMapping invalidMapping = new WidgetMapping();
124         invalidMapping.setType(null);
125         WidgetConfigurationUtil.setWidgetMappings(Collections.singletonList(invalidMapping));
126     }
127
128     /**
129      * Initialize the Artifact Generator Widget Mapping config with invalid data (type value).
130      *
131      * @throws IOException
132      *             if a WidgetMapping is invalid
133      */
134     @Test(expected = IOException.class)
135     public void testToscaMappingWithInvalidType() throws IOException {
136         WidgetMapping invalidMapping = new WidgetMapping();
137         invalidMapping.setType("invalid");
138         WidgetConfigurationUtil.setWidgetMappings(Collections.singletonList(invalidMapping));
139     }
140
141     /**
142      * Initialize the Artifact Generator Widget Mapping config with incomplete data (no widget name).
143      *
144      * @throws IOException
145      *             if a WidgetMapping is invalid
146      */
147     @Test(expected = IOException.class)
148     public void testToscaMappingWithoutWidget() throws IOException {
149         WidgetMapping invalidMapping = new WidgetMapping();
150         invalidMapping.setWidget(null);
151         WidgetConfigurationUtil.setWidgetMappings(Collections.singletonList(invalidMapping));
152     }
153
154     /**
155      * Create a Resource with a Widget model type and add this to a Service. Note that there are no test CSAR files
156      * which require this functionality, but the code path exists to support it.
157      *
158      * @throws IOException
159      *             if the widget mappings are not loaded
160      * @throws XmlArtifactGenerationException
161      *             if there is no configuration defined for the test resource's widget type
162      */
163     @Test
164     public void testAddWidgetToService() throws IOException, XmlArtifactGenerationException {
165         ArtifactTestUtils testUtils = new ArtifactTestUtils();
166         testUtils.loadWidgetMappings();
167
168         Model serviceModel = new Service();
169         Resource resourceModel = new Resource(WidgetType.valueOf("VF"), false);
170         resourceModel.setModelType(ModelType.WIDGET);
171
172         ISdcCsarHelper helper = Mockito.mock(ISdcCsarHelper.class);
173         ArtifactGeneratorToscaParser parser = new ArtifactGeneratorToscaParser(helper);
174         parser.addRelatedModel(serviceModel, resourceModel);
175     }
176
177     /**
178      * Process a dummy Group object for a Service Resource.
179      *
180      * @throws XmlArtifactGenerationException
181      *             if there is no configuration defined for a member Widget of an instance group
182      * @throws IOException
183      *             if the widget mappings cannot be loaded
184      */
185     @Test
186     public void testInstanceGroups() throws XmlArtifactGenerationException, IOException {
187         new ArtifactTestUtils().loadWidgetMappings();
188
189         final String instanceGroupType = "org.openecomp.groups.ResourceInstanceGroup";
190         WidgetConfigurationUtil.setSupportedInstanceGroups(Collections.singletonList(instanceGroupType));
191
192         ISdcCsarHelper helper = Mockito.mock(ISdcCsarHelper.class);
193         SubstitutionMappings sm = Mockito.mock(SubstitutionMappings.class);
194
195         NodeTemplate serviceNodeTemplate =
196                 buildNodeTemplate("service", "org.openecomp.resource.cr.a-collection-resource");
197         serviceNodeTemplate.setSubMappingToscaTemplate(sm);
198         Mockito.when(helper.getNodeTemplateByName(serviceNodeTemplate.getName())).thenReturn(serviceNodeTemplate);
199
200         ArrayList<Group> groups = new ArrayList<>();
201         groups.add(buildGroup("group", instanceGroupType));
202         Mockito.when(helper.getGroupsOfOriginOfNodeTemplate(serviceNodeTemplate)).thenReturn(groups);
203
204         ArtifactGeneratorToscaParser parser = new ArtifactGeneratorToscaParser(helper);
205         Resource groupResource = new Resource(WidgetType.valueOf("INSTANCE_GROUP"), true);
206         List<Resource> resources = parser.processInstanceGroups(groupResource, serviceNodeTemplate);
207
208         assertThat(resources.size(), is(1));
209         Resource resource = resources.get(0);
210         assertThat(resource.getModelNameVersionId(), is(equalTo(TEST_UUID)));
211     }
212
213     /**
214      * Create a NodeTemplate for unit testing purposes. In production code this object would only be created by the
215      * sdc-tosca parser.
216      *
217      * @param name
218      *            name of the NodeTemplate
219      * @param type
220      *            type of the NodeTemplate
221      * @return a new NodeTemplate object
222      */
223     private NodeTemplate buildNodeTemplate(String name, String type) {
224         LinkedHashMap<String, Object> nodeTemplateMap = new LinkedHashMap<>();
225         nodeTemplateMap.put(name, buildMap("type", type));
226         nodeTemplateMap.put(type, buildNodeTemplateCustomDefs());
227         return new NodeTemplate(name, nodeTemplateMap, nodeTemplateMap, null, null);
228     }
229
230     private LinkedHashMap<String, Object> buildNodeTemplateCustomDefs() {
231         LinkedHashMap<String, Object> customDefs = buildCustomDefs();
232         customDefs.put("attributes", null);
233         customDefs.put("requirements", null);
234         customDefs.put("capabilities", null);
235         customDefs.put("artifacts", null);
236         return customDefs;
237     }
238
239     private Group buildGroup(String name, String type) {
240         LinkedHashMap<String, Object> template = new LinkedHashMap<>();
241         template.put("type", type);
242         template.put("metadata", new LinkedHashMap<>());
243         template.put("properties", buildMap("UUID", TEST_UUID));
244         LinkedHashMap<String, Object> customDefMap = buildMap(name, template);
245         customDefMap.put(type, buildGroupCustomDefs());
246         return new Group(name, template, null, customDefMap);
247     }
248
249     private LinkedHashMap<String, Object> buildGroupCustomDefs() {
250         LinkedHashMap<String, Object> customDefs = buildCustomDefs();
251         customDefs.put("members", null);
252         return customDefs;
253     }
254
255     private LinkedHashMap<String, Object> buildCustomDefs() {
256         LinkedHashMap<String, Object> customDefs = new LinkedHashMap<>();
257         customDefs.put("derived_from", null);
258         customDefs.put("metadata", null);
259         customDefs.put("version", null);
260         customDefs.put("description", null);
261         customDefs.put("interfaces", null);
262         customDefs.put("properties", buildMap("UUID", buildMap("type", "java.lang.String")));
263         return customDefs;
264     }
265
266     private LinkedHashMap<String, Object> buildMap(String key, Object value) {
267         LinkedHashMap<String, Object> map = new LinkedHashMap<>();
268         map.put(key, value);
269         return map;
270     }
271 }