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;
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;
52 * Direct tests of the TOSCA parser-based Artifact Generator {@link ArtifactGeneratorToscaParser}., to cover exceptional
56 public class TestArtifactGeneratorToscaParser {
58 private static final String TEST_UUID = "1234";
61 * Initialize the Generator with an invalid mappings file path.
64 * if the file content could not be read successfully
66 @Test(expected = IllegalArgumentException.class)
67 public void testMissingMappingsFile() throws IOException {
68 ArtifactGeneratorToscaParser.initToscaMappingsConfiguration("non-existent.file");
72 * Initialize the Generator with no Widget Mappings content.
75 * if the file content could not be read successfully
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);
84 * Initialize the Generator with invalid Widget Mappings content.
87 * if the file content could not be read successfully
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);
96 * Process an Allotted Resource that does not have a Providing Service.
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);
106 * Add a CR (a type of Resource which is not a Providing Service) to a Resource Model.
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);
117 * Initialize the Artifact Generator Widget Mapping config with incomplete data (no type).
119 * @throws IOException
120 * if a WidgetMapping is invalid
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));
130 * Initialize the Artifact Generator Widget Mapping config with invalid data (type value).
132 * @throws IOException
133 * if a WidgetMapping is invalid
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));
143 * Initialize the Artifact Generator Widget Mapping config with incomplete data (no widget name).
145 * @throws IOException
146 * if a WidgetMapping is invalid
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));
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.
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
165 public void testAddWidgetToService() throws IOException, XmlArtifactGenerationException {
166 ArtifactTestUtils testUtils = new ArtifactTestUtils();
167 testUtils.loadWidgetMappings();
169 Model serviceModel = new Service();
170 Resource resourceModel = new Resource(WidgetType.valueOf("VF"), false);
171 resourceModel.setModelType(ModelType.WIDGET);
173 ISdcCsarHelper helper = Mockito.mock(ISdcCsarHelper.class);
174 ArtifactGeneratorToscaParser parser = new ArtifactGeneratorToscaParser(helper);
175 parser.addRelatedModel(serviceModel, resourceModel);
179 * Process a dummy Group object for a Service Resource.
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
187 public void testInstanceGroups() throws XmlArtifactGenerationException, IOException {
188 new ArtifactTestUtils().loadWidgetMappings();
190 final String instanceGroupType = "org.openecomp.groups.ResourceInstanceGroup";
191 WidgetConfigurationUtil.setSupportedInstanceGroups(Collections.singletonList(instanceGroupType));
193 SubstitutionMappings sm = Mockito.mock(SubstitutionMappings.class);
195 List<Group> groups = Arrays.asList(buildGroup("group", instanceGroupType));
196 Mockito.when(sm.getGroups()).thenReturn(new ArrayList<Group>(groups));
198 NodeTemplate serviceNodeTemplate =
199 buildNodeTemplate("service", "org.openecomp.resource.cr.a-collection-resource");
200 serviceNodeTemplate.setSubMappingToscaTemplate(sm);
202 Resource groupResource = new Resource(WidgetType.valueOf("INSTANCE_GROUP"), true);
203 List<Resource> resources = new ArtifactGeneratorToscaParser(Mockito.mock(ISdcCsarHelper.class))
204 .processInstanceGroups(groupResource, serviceNodeTemplate);
206 assertThat(resources.size(), is(1));
207 Resource resource = resources.get(0);
208 assertThat(resource.getModelNameVersionId(), is(equalTo(TEST_UUID)));
212 * Create a NodeTemplate for unit testing purposes. In production code this object would only be created by the
216 * name of the NodeTemplate
218 * type of the NodeTemplate
219 * @return a new NodeTemplate object
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);
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);
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);
247 private LinkedHashMap<String, Object> buildGroupCustomDefs() {
248 LinkedHashMap<String, Object> customDefs = buildCustomDefs();
249 customDefs.put("members", null);
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")));
264 private LinkedHashMap<String, Object> buildMap(String key, Object value) {
265 LinkedHashMap<String, Object> map = new LinkedHashMap<>();