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