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 emptyJson = new ArtifactTestUtils().getResourcePath(Resources.EMPTY_TOSCA_MAPPING_CONFIG);
80 ArtifactGeneratorToscaParser.initToscaMappingsConfiguration(emptyJson);
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 * @throws XmlArtifactGenerationException
99 * because the ALLOTTED_RESOURCE lacks a Providing Service
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);
109 * Add a CR (a type of Resource which is not a Providing Service) to a Resource Model.
111 * @throws XmlArtifactGenerationException
112 * because the ALLOTTED_RESOURCE lacks a Providing Service
113 * @throws IOException
114 * if the test mappings cannot be loaded
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"));
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);
127 * Initialize the Artifact Generator Widget Mapping config with incomplete data (no type).
129 * @throws IOException
130 * if a WidgetMapping is invalid
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));
140 * Initialize the Artifact Generator Widget Mapping config with invalid data (type value).
142 * @throws IOException
143 * if a WidgetMapping is invalid
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));
153 * Initialize the Artifact Generator Widget Mapping config with incomplete data (no widget name).
155 * @throws IOException
156 * if a WidgetMapping is invalid
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));
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.
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
174 @Test(expected = Test.None.class /* no exception expected */)
175 public void testAddWidgetToService() throws IOException, XmlArtifactGenerationException {
176 ArtifactTestUtils testUtils = new ArtifactTestUtils();
177 testUtils.loadWidgetMappings();
179 Model serviceModel = new Service();
180 Resource resourceModel = new Resource(WidgetType.valueOf("VF"), false);
181 resourceModel.setModelType(ModelType.WIDGET);
183 ISdcCsarHelper helper = Mockito.mock(ISdcCsarHelper.class);
184 ArtifactGeneratorToscaParser parser = new ArtifactGeneratorToscaParser(helper);
185 parser.addRelatedModel(serviceModel, resourceModel);
189 * Process a dummy Group object for a Service Resource.
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
197 public void testInstanceGroups() throws XmlArtifactGenerationException, IOException {
198 new ArtifactTestUtils().loadWidgetMappings();
200 final String instanceGroupType = "org.openecomp.groups.ResourceInstanceGroup";
201 WidgetConfigurationUtil.setSupportedInstanceGroups(Collections.singletonList(instanceGroupType));
203 SubstitutionMappings sm = Mockito.mock(SubstitutionMappings.class);
205 List<Group> groups = Arrays.asList(buildGroup("group", instanceGroupType));
206 Mockito.when(sm.getGroups()).thenReturn(new ArrayList<Group>(groups));
208 NodeTemplate serviceNodeTemplate =
209 buildNodeTemplate("service", "org.openecomp.resource.cr.a-collection-resource");
210 serviceNodeTemplate.setSubMappingToscaTemplate(sm);
212 Resource groupResource = new Resource(WidgetType.valueOf("INSTANCE_GROUP"), true);
213 List<Resource> resources = new ArtifactGeneratorToscaParser(Mockito.mock(ISdcCsarHelper.class))
214 .processInstanceGroups(groupResource, serviceNodeTemplate);
216 assertThat(resources.size(), is(1));
217 Resource resource = resources.get(0);
218 assertThat(resource.getModelNameVersionId(), is(equalTo(TEST_UUID)));
222 * Create a NodeTemplate for unit testing purposes. In production code this object would only be created by the
226 * name of the NodeTemplate
228 * type of the NodeTemplate
229 * @return a new NodeTemplate object
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);
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);
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);
257 private LinkedHashMap<String, Object> buildGroupCustomDefs() {
258 LinkedHashMap<String, Object> customDefs = buildCustomDefs();
259 customDefs.put("members", null);
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")));
274 private LinkedHashMap<String, Object> buildMap(String key, Object value) {
275 LinkedHashMap<String, Object> map = new LinkedHashMap<>();