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 artifact generator properties file path.
63 * if an error occurs reading the configuration properties
65 @Test(expected = IllegalArgumentException.class)
66 public void testMissingPropertiesFile() throws IOException {
67 System.setProperty(ArtifactGeneratorToscaParser.PROPERTY_ARTIFACT_GENERATOR_CONFIG_FILE, "non-existent.file");
68 ArtifactGeneratorToscaParser.initWidgetConfiguration();
72 * Initialize the Generator with an invalid mappings file path.
75 * if the file content could not be read successfully
77 @Test(expected = IllegalArgumentException.class)
78 public void testMissingMappingsFile() throws IOException {
79 ArtifactGeneratorToscaParser.initToscaMappingsConfiguration("non-existent.file");
83 * Initialize the Generator with no Widget Mappings content.
86 * if the file content could not be read successfully
88 @Test(expected = IOException.class)
89 public void testMissingMappingsContent() throws IOException {
90 String invalidJson = new ArtifactTestUtils().getResourcePath(Resources.EMPTY_TOSCA_MAPPING_CONFIG);
91 ArtifactGeneratorToscaParser.initToscaMappingsConfiguration(invalidJson);
95 * Initialize the Generator with invalid Widget Mappings content.
98 * if the file content could not be read successfully
100 @Test(expected = IOException.class)
101 public void testInvalidMappingsContent() throws IOException {
102 String invalidJson = new ArtifactTestUtils().getResourcePath(Resources.INVALID_TOSCA_MAPPING_CONFIG);
103 ArtifactGeneratorToscaParser.initToscaMappingsConfiguration(invalidJson);
107 * Process an Allotted Resource that does not have a Providing Service.
109 @Test(expected = IllegalArgumentException.class)
110 public void testMissingProvidingService() {
111 List<NodeTemplate> nodeTemplateList = Collections.singletonList(buildNodeTemplate("name", "BlockStorage"));
112 new ArtifactGeneratorToscaParser(null)
113 .processResourceModels(new Resource(WidgetType.valueOf("ALLOTTED_RESOURCE"), true), nodeTemplateList);
117 * Add a CR (a type of Resource which is not a Providing Service) to a Resource Model.
119 @Test(expected = IllegalArgumentException.class)
120 public void testAddResourceNotProvidingService() {
121 List<NodeTemplate> nodeTemplateList = Collections.singletonList(buildNodeTemplate("testCR", "CR"));
122 // Create any Resource to which the CR can be added
123 final Resource dummyResource = new Resource(WidgetType.valueOf("ALLOTTED_RESOURCE"), true);
124 new ArtifactGeneratorToscaParser(null).processResourceModels(dummyResource, nodeTemplateList);
128 * Initialize the Artifact Generator Widget Mapping config with incomplete data (no type).
130 * @throws IOException
131 * if a WidgetMapping is invalid
133 @Test(expected = IOException.class)
134 public void testToscaMappingWithoutType() throws IOException {
135 WidgetMapping invalidMapping = new WidgetMapping();
136 invalidMapping.setType(null);
137 WidgetConfigurationUtil.setWidgetMappings(Collections.singletonList(invalidMapping));
141 * Initialize the Artifact Generator Widget Mapping config with invalid data (type value).
143 * @throws IOException
144 * if a WidgetMapping is invalid
146 @Test(expected = IOException.class)
147 public void testToscaMappingWithInvalidType() throws IOException {
148 WidgetMapping invalidMapping = new WidgetMapping();
149 invalidMapping.setType("invalid");
150 WidgetConfigurationUtil.setWidgetMappings(Collections.singletonList(invalidMapping));
154 * Initialize the Artifact Generator Widget Mapping config with incomplete data (no widget name).
156 * @throws IOException
157 * if a WidgetMapping is invalid
159 @Test(expected = IOException.class)
160 public void testToscaMappingWithoutWidget() throws IOException {
161 WidgetMapping invalidMapping = new WidgetMapping();
162 invalidMapping.setWidget(null);
163 WidgetConfigurationUtil.setWidgetMappings(Collections.singletonList(invalidMapping));
167 * Create a Resource with a Widget model type and add this to a Service. Note that there are no test CSAR files
168 * which require this functionality, but the code path exists to support it.
170 * @throws IOException
171 * if the widget mappings are not loaded
172 * @throws XmlArtifactGenerationException
173 * if there is no configuration defined for the test resource's widget type
176 public void testAddWidgetToService() throws IOException, XmlArtifactGenerationException {
177 ArtifactTestUtils testUtils = new ArtifactTestUtils();
178 testUtils.loadWidgetMappings();
179 testUtils.loadWidgetToUuidMappings();
181 Model serviceModel = new Service();
182 Resource resourceModel = new Resource(WidgetType.valueOf("VF"), false);
183 resourceModel.setModelType(ModelType.WIDGET);
185 ISdcCsarHelper helper = Mockito.mock(ISdcCsarHelper.class);
186 ArtifactGeneratorToscaParser parser = new ArtifactGeneratorToscaParser(helper);
187 parser.addRelatedModel(serviceModel, resourceModel);
191 * Process a dummy Group object for a Service Resource.
193 * @throws XmlArtifactGenerationException
194 * if there is no configuration defined for a member Widget of an instance group
195 * @throws IOException
196 * if the widget mappings cannot be loaded
199 public void testInstanceGroups() throws XmlArtifactGenerationException, IOException {
200 new ArtifactTestUtils().loadWidgetMappings();
202 final String instanceGroupType = "org.openecomp.groups.ResourceInstanceGroup";
203 WidgetConfigurationUtil.setSupportedInstanceGroups(Collections.singletonList(instanceGroupType));
205 ISdcCsarHelper helper = Mockito.mock(ISdcCsarHelper.class);
206 SubstitutionMappings sm = Mockito.mock(SubstitutionMappings.class);
208 NodeTemplate serviceNodeTemplate =
209 buildNodeTemplate("service", "org.openecomp.resource.cr.a-collection-resource");
210 serviceNodeTemplate.setSubMappingToscaTemplate(sm);
211 Mockito.when(helper.getNodeTemplateByName(serviceNodeTemplate.getName())).thenReturn(serviceNodeTemplate);
213 ArrayList<Group> groups = new ArrayList<>();
214 groups.add(buildGroup("group", instanceGroupType));
215 Mockito.when(helper.getGroupsOfOriginOfNodeTemplate(serviceNodeTemplate)).thenReturn(groups);
217 ArtifactGeneratorToscaParser parser = new ArtifactGeneratorToscaParser(helper);
218 Resource groupResource = new Resource(WidgetType.valueOf("INSTANCE_GROUP"), true);
219 List<Resource> resources = parser.processInstanceGroups(groupResource, serviceNodeTemplate);
221 assertThat(resources.size(), is(1));
222 Resource resource = resources.get(0);
223 assertThat(resource.getModelNameVersionId(), is(equalTo(TEST_UUID)));
227 * Create a NodeTemplate for unit testing purposes. In production code this object would only be created by the
231 * name of the NodeTemplate
233 * type of the NodeTemplate
234 * @return a new NodeTemplate object
236 private NodeTemplate buildNodeTemplate(String name, String type) {
237 LinkedHashMap<String, Object> nodeTemplateMap = new LinkedHashMap<>();
238 nodeTemplateMap.put(name, buildMap("type", type));
239 nodeTemplateMap.put(type, buildNodeTemplateCustomDefs());
240 return new NodeTemplate(name, nodeTemplateMap, nodeTemplateMap, null, null);
243 private LinkedHashMap<String, Object> buildNodeTemplateCustomDefs() {
244 LinkedHashMap<String, Object> customDefs = buildCustomDefs();
245 customDefs.put("attributes", null);
246 customDefs.put("requirements", null);
247 customDefs.put("capabilities", null);
248 customDefs.put("artifacts", null);
252 private Group buildGroup(String name, String type) {
253 LinkedHashMap<String, Object> template = new LinkedHashMap<>();
254 template.put("type", type);
255 template.put("metadata", new LinkedHashMap<>());
256 template.put("properties", buildMap("UUID", TEST_UUID));
257 LinkedHashMap<String, Object> customDefMap = buildMap(name, template);
258 customDefMap.put(type, buildGroupCustomDefs());
259 return new Group(name, template, null, customDefMap);
262 private LinkedHashMap<String, Object> buildGroupCustomDefs() {
263 LinkedHashMap<String, Object> customDefs = buildCustomDefs();
264 customDefs.put("members", null);
268 private LinkedHashMap<String, Object> buildCustomDefs() {
269 LinkedHashMap<String, Object> customDefs = new LinkedHashMap<>();
270 customDefs.put("derived_from", null);
271 customDefs.put("metadata", null);
272 customDefs.put("version", null);
273 customDefs.put("description", null);
274 customDefs.put("interfaces", null);
275 customDefs.put("properties", buildMap("UUID", buildMap("type", "java.lang.String")));
279 private LinkedHashMap<String, Object> buildMap(String key, Object value) {
280 LinkedHashMap<String, Object> map = new LinkedHashMap<>();