Depend on sdc-tosca version 1.5.0
[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 invalidJson = new ArtifactTestUtils().getResourcePath(Resources.EMPTY_TOSCA_MAPPING_CONFIG);
80         ArtifactGeneratorToscaParser.initToscaMappingsConfiguration(invalidJson);
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     @Test(expected = IllegalArgumentException.class)
99     public void testMissingProvidingService() {
100         List<NodeTemplate> nodeTemplateList = Collections.singletonList(buildNodeTemplate("name", "BlockStorage"));
101         new ArtifactGeneratorToscaParser(null)
102                 .processResourceModels(new Resource(WidgetType.valueOf("ALLOTTED_RESOURCE"), true), nodeTemplateList);
103     }
104
105     /**
106      * Add a CR (a type of Resource which is not a Providing Service) to a Resource Model.
107      */
108     @Test(expected = IllegalArgumentException.class)
109     public void testAddResourceNotProvidingService() {
110         List<NodeTemplate> nodeTemplateList = Collections.singletonList(buildNodeTemplate("testCR", "CR"));
111         // Create any Resource to which the CR can be added
112         final Resource dummyResource = new Resource(WidgetType.valueOf("ALLOTTED_RESOURCE"), true);
113         new ArtifactGeneratorToscaParser(null).processResourceModels(dummyResource, nodeTemplateList);
114     }
115
116     /**
117      * Initialize the Artifact Generator Widget Mapping config with incomplete data (no type).
118      *
119      * @throws IOException
120      *             if a WidgetMapping is invalid
121      */
122     @Test(expected = IOException.class)
123     public void testToscaMappingWithoutType() throws IOException {
124         WidgetMapping invalidMapping = new WidgetMapping();
125         invalidMapping.setType(null);
126         WidgetConfigurationUtil.setWidgetMappings(Collections.singletonList(invalidMapping));
127     }
128
129     /**
130      * Initialize the Artifact Generator Widget Mapping config with invalid data (type value).
131      *
132      * @throws IOException
133      *             if a WidgetMapping is invalid
134      */
135     @Test(expected = IOException.class)
136     public void testToscaMappingWithInvalidType() throws IOException {
137         WidgetMapping invalidMapping = new WidgetMapping();
138         invalidMapping.setType("invalid");
139         WidgetConfigurationUtil.setWidgetMappings(Collections.singletonList(invalidMapping));
140     }
141
142     /**
143      * Initialize the Artifact Generator Widget Mapping config with incomplete data (no widget name).
144      *
145      * @throws IOException
146      *             if a WidgetMapping is invalid
147      */
148     @Test(expected = IOException.class)
149     public void testToscaMappingWithoutWidget() throws IOException {
150         WidgetMapping invalidMapping = new WidgetMapping();
151         invalidMapping.setWidget(null);
152         WidgetConfigurationUtil.setWidgetMappings(Collections.singletonList(invalidMapping));
153     }
154
155     /**
156      * Create a Resource with a Widget model type and add this to a Service. Note that there are no test CSAR files
157      * which require this functionality, but the code path exists to support it.
158      *
159      * @throws IOException
160      *             if the widget mappings are not loaded
161      * @throws XmlArtifactGenerationException
162      *             if there is no configuration defined for the test resource's widget type
163      */
164     @Test
165     public void testAddWidgetToService() throws IOException, XmlArtifactGenerationException {
166         ArtifactTestUtils testUtils = new ArtifactTestUtils();
167         testUtils.loadWidgetMappings();
168
169         Model serviceModel = new Service();
170         Resource resourceModel = new Resource(WidgetType.valueOf("VF"), false);
171         resourceModel.setModelType(ModelType.WIDGET);
172
173         ISdcCsarHelper helper = Mockito.mock(ISdcCsarHelper.class);
174         ArtifactGeneratorToscaParser parser = new ArtifactGeneratorToscaParser(helper);
175         parser.addRelatedModel(serviceModel, resourceModel);
176     }
177
178     /**
179      * Process a dummy Group object for a Service Resource.
180      *
181      * @throws XmlArtifactGenerationException
182      *             if there is no configuration defined for a member Widget of an instance group
183      * @throws IOException
184      *             if the widget mappings cannot be loaded
185      */
186     @Test
187     public void testInstanceGroups() throws XmlArtifactGenerationException, IOException {
188         new ArtifactTestUtils().loadWidgetMappings();
189
190         final String instanceGroupType = "org.openecomp.groups.ResourceInstanceGroup";
191         WidgetConfigurationUtil.setSupportedInstanceGroups(Collections.singletonList(instanceGroupType));
192
193         SubstitutionMappings sm = Mockito.mock(SubstitutionMappings.class);
194
195         List<Group> groups = Arrays.asList(buildGroup("group", instanceGroupType));
196         Mockito.when(sm.getGroups()).thenReturn(new ArrayList<Group>(groups));
197
198         NodeTemplate serviceNodeTemplate =
199                 buildNodeTemplate("service", "org.openecomp.resource.cr.a-collection-resource");
200         serviceNodeTemplate.setSubMappingToscaTemplate(sm);
201
202         Resource groupResource = new Resource(WidgetType.valueOf("INSTANCE_GROUP"), true);
203         List<Resource> resources = new ArtifactGeneratorToscaParser(Mockito.mock(ISdcCsarHelper.class))
204                 .processInstanceGroups(groupResource, serviceNodeTemplate);
205
206         assertThat(resources.size(), is(1));
207         Resource resource = resources.get(0);
208         assertThat(resource.getModelNameVersionId(), is(equalTo(TEST_UUID)));
209     }
210
211     /**
212      * Create a NodeTemplate for unit testing purposes. In production code this object would only be created by the
213      * sdc-tosca parser.
214      *
215      * @param name
216      *            name of the NodeTemplate
217      * @param type
218      *            type of the NodeTemplate
219      * @return a new NodeTemplate object
220      */
221     private NodeTemplate buildNodeTemplate(String name, String type) {
222         LinkedHashMap<String, Object> nodeTemplateMap = new LinkedHashMap<>();
223         nodeTemplateMap.put(name, buildMap("type", type));
224         nodeTemplateMap.put(type, buildNodeTemplateCustomDefs());
225         return new NodeTemplate(name, nodeTemplateMap, nodeTemplateMap, null, null);
226     }
227
228     private LinkedHashMap<String, Object> buildNodeTemplateCustomDefs() {
229         LinkedHashMap<String, Object> customDefs = buildCustomDefs();
230         customDefs.put("attributes", null);
231         customDefs.put("requirements", null);
232         customDefs.put("capabilities", null);
233         customDefs.put("artifacts", null);
234         return customDefs;
235     }
236
237     private Group buildGroup(String name, String type) {
238         LinkedHashMap<String, Object> template = new LinkedHashMap<>();
239         template.put("type", type);
240         template.put("metadata", new LinkedHashMap<>());
241         template.put("properties", buildMap("UUID", TEST_UUID));
242         LinkedHashMap<String, Object> customDefMap = buildMap(name, template);
243         customDefMap.put(type, buildGroupCustomDefs());
244         return new Group(name, template, null, customDefMap);
245     }
246
247     private LinkedHashMap<String, Object> buildGroupCustomDefs() {
248         LinkedHashMap<String, Object> customDefs = buildCustomDefs();
249         customDefs.put("members", null);
250         return customDefs;
251     }
252
253     private LinkedHashMap<String, Object> buildCustomDefs() {
254         LinkedHashMap<String, Object> customDefs = new LinkedHashMap<>();
255         customDefs.put("derived_from", null);
256         customDefs.put("metadata", null);
257         customDefs.put("version", null);
258         customDefs.put("description", null);
259         customDefs.put("interfaces", null);
260         customDefs.put("properties", buildMap("UUID", buildMap("type", "java.lang.String")));
261         return customDefs;
262     }
263
264     private LinkedHashMap<String, Object> buildMap(String key, Object value) {
265         LinkedHashMap<String, Object> map = new LinkedHashMap<>();
266         map.put(key, value);
267         return map;
268     }
269 }