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