2 * ============LICENSE_START=======================================================
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
12 * http://www.apache.org/licenses/LICENSE-2.0
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=========================================================
22 package org.onap.aai.babel.parser;
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;
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;
54 * Direct tests of the TOSCA parser-based Artifact Generator {@link ArtifactGeneratorToscaParser}., to cover exceptional
58 public class TestArtifactGeneratorToscaParser {
60 private static final String TEST_UUID = "1234";
63 * Initialize the Generator with an invalid mappings file path.
66 * if the file content could not be read successfully
69 public void testMissingMappingsFile() throws IOException {
70 assertThrows(IllegalArgumentException.class, () -> {
71 ArtifactGeneratorToscaParser.initToscaMappingsConfiguration("non-existent.file");
76 * Initialize the Generator with no Widget Mappings content.
79 * if the file content could not be read successfully
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);
90 * Initialize the Generator with invalid Widget Mappings content.
93 * if the file content could not be read successfully
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);
104 * Process an Allotted Resource that does not have a Providing Service.
106 * @throws XmlArtifactGenerationException
107 * because the ALLOTTED_RESOURCE lacks a Providing Service
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);
119 * Add a CR (a type of Resource which is not a Providing Service) to a Resource Model.
121 * @throws XmlArtifactGenerationException
122 * because the ALLOTTED_RESOURCE lacks a Providing Service
123 * @throws IOException
124 * if the test mappings cannot be loaded
127 public void testAddResourceNotProvidingService() throws XmlArtifactGenerationException, IOException {
128 assertThrows(XmlArtifactGenerationException.class, () -> {
129 new ArtifactTestUtils().loadWidgetMappings();
130 List<NodeTemplate> nodeTemplateList = Collections.singletonList(buildNodeTemplate("testCR", "CR"));
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);
139 * Initialize the Artifact Generator Widget Mapping config with incomplete data (no type).
141 * @throws IOException
142 * if a WidgetMapping is invalid
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));
154 * Initialize the Artifact Generator Widget Mapping config with invalid data (type value).
156 * @throws IOException
157 * if a WidgetMapping is invalid
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));
169 * Initialize the Artifact Generator Widget Mapping config with incomplete data (no widget name).
171 * @throws IOException
172 * if a WidgetMapping is invalid
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));
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.
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
193 public void testAddWidgetToService() throws IOException, XmlArtifactGenerationException {
194 assertDoesNotThrow(() -> {
195 ArtifactTestUtils testUtils = new ArtifactTestUtils();
196 testUtils.loadWidgetMappings();
198 Model serviceModel = new Service();
199 Resource resourceModel = new Resource(WidgetType.valueOf("VF"), false);
200 resourceModel.setModelType(ModelType.WIDGET);
202 ISdcCsarHelper helper = Mockito.mock(ISdcCsarHelper.class);
203 ArtifactGeneratorToscaParser parser = new ArtifactGeneratorToscaParser(helper);
204 parser.addRelatedModel(serviceModel, resourceModel);
209 * Process a dummy Group object for a Service Resource.
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
217 public void testInstanceGroups() throws XmlArtifactGenerationException, IOException {
218 new ArtifactTestUtils().loadWidgetMappings();
220 final String instanceGroupType = "org.openecomp.groups.ResourceInstanceGroup";
221 WidgetConfigurationUtil.setSupportedInstanceGroups(Collections.singletonList(instanceGroupType));
223 SubstitutionMappings sm = Mockito.mock(SubstitutionMappings.class);
225 List<Group> groups = Arrays.asList(buildGroup("group", instanceGroupType));
226 Mockito.when(sm.getGroups()).thenReturn(new ArrayList<Group>(groups));
228 NodeTemplate serviceNodeTemplate =
229 buildNodeTemplate("service", "org.openecomp.resource.cr.a-collection-resource");
230 serviceNodeTemplate.setSubMappingToscaTemplate(sm);
232 Resource groupResource = new Resource(WidgetType.valueOf("INSTANCE_GROUP"), true);
233 List<Resource> resources = new ArtifactGeneratorToscaParser(Mockito.mock(ISdcCsarHelper.class))
234 .processInstanceGroups(groupResource, serviceNodeTemplate);
236 assertThat(resources.size(), is(1));
237 Resource resource = resources.get(0);
238 assertThat(resource.getModelNameVersionId(), is(equalTo(TEST_UUID)));
242 * Create a NodeTemplate for unit testing purposes. In production code this object would only be created by the
246 * name of the NodeTemplate
248 * type of the NodeTemplate
249 * @return a new NodeTemplate object
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);
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);
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);
277 private LinkedHashMap<String, Object> buildGroupCustomDefs() {
278 LinkedHashMap<String, Object> customDefs = buildCustomDefs();
279 customDefs.put("members", null);
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")));
294 private LinkedHashMap<String, Object> buildMap(String key, Object value) {
295 LinkedHashMap<String, Object> map = new LinkedHashMap<>();