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.Collections;
31 import java.util.LinkedHashMap;
32 import java.util.List;
33 import org.junit.Test;
34 import org.mockito.Mockito;
35 import org.onap.aai.babel.util.ArtifactTestUtils;
36 import org.onap.aai.babel.util.Resources;
37 import org.onap.aai.babel.xml.generator.XmlArtifactGenerationException;
38 import org.onap.aai.babel.xml.generator.data.WidgetConfigurationUtil;
39 import org.onap.aai.babel.xml.generator.data.WidgetMapping;
40 import org.onap.aai.babel.xml.generator.model.Model;
41 import org.onap.aai.babel.xml.generator.model.Resource;
42 import org.onap.aai.babel.xml.generator.model.Service;
43 import org.onap.aai.babel.xml.generator.model.WidgetType;
44 import org.onap.aai.babel.xml.generator.types.ModelType;
45 import org.onap.sdc.tosca.parser.api.ISdcCsarHelper;
46 import org.onap.sdc.toscaparser.api.Group;
47 import org.onap.sdc.toscaparser.api.NodeTemplate;
48 import org.onap.sdc.toscaparser.api.SubstitutionMappings;
51 * Direct tests of the TOSCA parser-based Artifact Generator {@link ArtifactGeneratorToscaParser}., to cover exceptional
55 public class TestArtifactGeneratorToscaParser {
57 private static final String TEST_UUID = "1234";
60 * Initialize the Generator with an invalid mappings file path.
63 * if the file content could not be read successfully
65 @Test(expected = IllegalArgumentException.class)
66 public void testMissingMappingsFile() throws IOException {
67 ArtifactGeneratorToscaParser.initToscaMappingsConfiguration("non-existent.file");
71 * Initialize the Generator with no Widget Mappings content.
74 * if the file content could not be read successfully
76 @Test(expected = IOException.class)
77 public void testMissingMappingsContent() throws IOException {
78 String invalidJson = new ArtifactTestUtils().getResourcePath(Resources.EMPTY_TOSCA_MAPPING_CONFIG);
79 ArtifactGeneratorToscaParser.initToscaMappingsConfiguration(invalidJson);
83 * Initialize the Generator with invalid Widget Mappings content.
86 * if the file content could not be read successfully
88 @Test(expected = IOException.class)
89 public void testInvalidMappingsContent() throws IOException {
90 String invalidJson = new ArtifactTestUtils().getResourcePath(Resources.INVALID_TOSCA_MAPPING_CONFIG);
91 ArtifactGeneratorToscaParser.initToscaMappingsConfiguration(invalidJson);
95 * Process an Allotted Resource that does not have a Providing Service.
97 @Test(expected = IllegalArgumentException.class)
98 public void testMissingProvidingService() {
99 List<NodeTemplate> nodeTemplateList = Collections.singletonList(buildNodeTemplate("name", "BlockStorage"));
100 new ArtifactGeneratorToscaParser(null)
101 .processResourceModels(new Resource(WidgetType.valueOf("ALLOTTED_RESOURCE"), true), nodeTemplateList);
105 * Add a CR (a type of Resource which is not a Providing Service) to a Resource Model.
107 @Test(expected = IllegalArgumentException.class)
108 public void testAddResourceNotProvidingService() {
109 List<NodeTemplate> nodeTemplateList = Collections.singletonList(buildNodeTemplate("testCR", "CR"));
110 // Create any Resource to which the CR can be added
111 final Resource dummyResource = new Resource(WidgetType.valueOf("ALLOTTED_RESOURCE"), true);
112 new ArtifactGeneratorToscaParser(null).processResourceModels(dummyResource, nodeTemplateList);
116 * Initialize the Artifact Generator Widget Mapping config with incomplete data (no type).
118 * @throws IOException
119 * if a WidgetMapping is invalid
121 @Test(expected = IOException.class)
122 public void testToscaMappingWithoutType() throws IOException {
123 WidgetMapping invalidMapping = new WidgetMapping();
124 invalidMapping.setType(null);
125 WidgetConfigurationUtil.setWidgetMappings(Collections.singletonList(invalidMapping));
129 * Initialize the Artifact Generator Widget Mapping config with invalid data (type value).
131 * @throws IOException
132 * if a WidgetMapping is invalid
134 @Test(expected = IOException.class)
135 public void testToscaMappingWithInvalidType() throws IOException {
136 WidgetMapping invalidMapping = new WidgetMapping();
137 invalidMapping.setType("invalid");
138 WidgetConfigurationUtil.setWidgetMappings(Collections.singletonList(invalidMapping));
142 * Initialize the Artifact Generator Widget Mapping config with incomplete data (no widget name).
144 * @throws IOException
145 * if a WidgetMapping is invalid
147 @Test(expected = IOException.class)
148 public void testToscaMappingWithoutWidget() throws IOException {
149 WidgetMapping invalidMapping = new WidgetMapping();
150 invalidMapping.setWidget(null);
151 WidgetConfigurationUtil.setWidgetMappings(Collections.singletonList(invalidMapping));
155 * Create a Resource with a Widget model type and add this to a Service. Note that there are no test CSAR files
156 * which require this functionality, but the code path exists to support it.
158 * @throws IOException
159 * if the widget mappings are not loaded
160 * @throws XmlArtifactGenerationException
161 * if there is no configuration defined for the test resource's widget type
164 public void testAddWidgetToService() throws IOException, XmlArtifactGenerationException {
165 ArtifactTestUtils testUtils = new ArtifactTestUtils();
166 testUtils.loadWidgetMappings();
168 Model serviceModel = new Service();
169 Resource resourceModel = new Resource(WidgetType.valueOf("VF"), false);
170 resourceModel.setModelType(ModelType.WIDGET);
172 ISdcCsarHelper helper = Mockito.mock(ISdcCsarHelper.class);
173 ArtifactGeneratorToscaParser parser = new ArtifactGeneratorToscaParser(helper);
174 parser.addRelatedModel(serviceModel, resourceModel);
178 * Process a dummy Group object for a Service Resource.
180 * @throws XmlArtifactGenerationException
181 * if there is no configuration defined for a member Widget of an instance group
182 * @throws IOException
183 * if the widget mappings cannot be loaded
186 public void testInstanceGroups() throws XmlArtifactGenerationException, IOException {
187 new ArtifactTestUtils().loadWidgetMappings();
189 final String instanceGroupType = "org.openecomp.groups.ResourceInstanceGroup";
190 WidgetConfigurationUtil.setSupportedInstanceGroups(Collections.singletonList(instanceGroupType));
192 ISdcCsarHelper helper = Mockito.mock(ISdcCsarHelper.class);
193 SubstitutionMappings sm = Mockito.mock(SubstitutionMappings.class);
195 NodeTemplate serviceNodeTemplate =
196 buildNodeTemplate("service", "org.openecomp.resource.cr.a-collection-resource");
197 serviceNodeTemplate.setSubMappingToscaTemplate(sm);
198 Mockito.when(helper.getNodeTemplateByName(serviceNodeTemplate.getName())).thenReturn(serviceNodeTemplate);
200 ArrayList<Group> groups = new ArrayList<>();
201 groups.add(buildGroup("group", instanceGroupType));
202 Mockito.when(helper.getGroupsOfOriginOfNodeTemplate(serviceNodeTemplate)).thenReturn(groups);
204 ArtifactGeneratorToscaParser parser = new ArtifactGeneratorToscaParser(helper);
205 Resource groupResource = new Resource(WidgetType.valueOf("INSTANCE_GROUP"), true);
206 List<Resource> resources = parser.processInstanceGroups(groupResource, serviceNodeTemplate);
208 assertThat(resources.size(), is(1));
209 Resource resource = resources.get(0);
210 assertThat(resource.getModelNameVersionId(), is(equalTo(TEST_UUID)));
214 * Create a NodeTemplate for unit testing purposes. In production code this object would only be created by the
218 * name of the NodeTemplate
220 * type of the NodeTemplate
221 * @return a new NodeTemplate object
223 private NodeTemplate buildNodeTemplate(String name, String type) {
224 LinkedHashMap<String, Object> nodeTemplateMap = new LinkedHashMap<>();
225 nodeTemplateMap.put(name, buildMap("type", type));
226 nodeTemplateMap.put(type, buildNodeTemplateCustomDefs());
227 return new NodeTemplate(name, nodeTemplateMap, nodeTemplateMap, null, null);
230 private LinkedHashMap<String, Object> buildNodeTemplateCustomDefs() {
231 LinkedHashMap<String, Object> customDefs = buildCustomDefs();
232 customDefs.put("attributes", null);
233 customDefs.put("requirements", null);
234 customDefs.put("capabilities", null);
235 customDefs.put("artifacts", null);
239 private Group buildGroup(String name, String type) {
240 LinkedHashMap<String, Object> template = new LinkedHashMap<>();
241 template.put("type", type);
242 template.put("metadata", new LinkedHashMap<>());
243 template.put("properties", buildMap("UUID", TEST_UUID));
244 LinkedHashMap<String, Object> customDefMap = buildMap(name, template);
245 customDefMap.put(type, buildGroupCustomDefs());
246 return new Group(name, template, null, customDefMap);
249 private LinkedHashMap<String, Object> buildGroupCustomDefs() {
250 LinkedHashMap<String, Object> customDefs = buildCustomDefs();
251 customDefs.put("members", null);
255 private LinkedHashMap<String, Object> buildCustomDefs() {
256 LinkedHashMap<String, Object> customDefs = new LinkedHashMap<>();
257 customDefs.put("derived_from", null);
258 customDefs.put("metadata", null);
259 customDefs.put("version", null);
260 customDefs.put("description", null);
261 customDefs.put("interfaces", null);
262 customDefs.put("properties", buildMap("UUID", buildMap("type", "java.lang.String")));
266 private LinkedHashMap<String, Object> buildMap(String key, Object value) {
267 LinkedHashMap<String, Object> map = new LinkedHashMap<>();