2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright © 2017-2019 AT&T Intellectual Property. All rights reserved.
6 * Copyright © 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.Resource;
41 import org.onap.aai.babel.xml.generator.model.Widget.Type;
42 import org.onap.sdc.tosca.parser.api.ISdcCsarHelper;
43 import org.onap.sdc.toscaparser.api.Group;
44 import org.onap.sdc.toscaparser.api.NodeTemplate;
45 import org.onap.sdc.toscaparser.api.SubstitutionMappings;
48 * Direct tests of the TOSCA parser-based Artifact Generator {@link ArtifactGeneratorToscaParser}., to cover exceptional
52 public class TestArtifactGeneratorToscaParser {
54 private static final String TEST_UUID = "1234";
57 * Initialize the Generator with an invalid artifact generator properties file path.
61 @Test(expected = IllegalArgumentException.class)
62 public void testMissingPropertiesFile() throws IOException {
63 System.setProperty(ArtifactGeneratorToscaParser.PROPERTY_ARTIFACT_GENERATOR_CONFIG_FILE, "non-existent.file");
64 ArtifactGeneratorToscaParser.initWidgetConfiguration();
68 * Initialize the Generator with an invalid mappings file path.
72 @Test(expected = IllegalArgumentException.class)
73 public void testMissingMappingsFile() throws IOException {
74 ArtifactGeneratorToscaParser.initToscaMappingsConfiguration("non-existent.file");
78 * Initialize the Generator with no Widget Mappings content.
82 @Test(expected = IOException.class)
83 public void testMissingMappingsContent() throws IOException {
84 String invalidJson = new ArtifactTestUtils().getResourcePath(Resources.EMPTY_TOSCA_MAPPING_CONFIG);
85 ArtifactGeneratorToscaParser.initToscaMappingsConfiguration(invalidJson);
89 * Initialize the Generator with invalid Widget Mappings content.
93 @Test(expected = IOException.class)
94 public void testInvalidMappingsContent() throws IOException {
95 String invalidJson = new ArtifactTestUtils().getResourcePath(Resources.INVALID_TOSCA_MAPPING_CONFIG);
96 ArtifactGeneratorToscaParser.initToscaMappingsConfiguration(invalidJson);
100 * Process an Allotted Resource that does not have a Providing Service.
102 @Test(expected = IllegalArgumentException.class)
103 public void testMissingProvidingService() {
104 List<NodeTemplate> nodeTemplateList = Collections.singletonList(buildNodeTemplate("name", "BlockStorage"));
105 new ArtifactGeneratorToscaParser(null).processResourceModels(new Resource(Type.ALLOTTED_RESOURCE, true),
110 * Add a CR (a type of Resource which is not a Providing Service) to a Resource Model.
112 @Test(expected = IllegalArgumentException.class)
113 public void testAddResourceNotProvidingService() {
114 List<NodeTemplate> nodeTemplateList = Collections.singletonList(buildNodeTemplate("testCR", "CR"));
115 // Create any Resource to which the CR can be added
116 final Resource dummyResource = new Resource(Type.ALLOTTED_RESOURCE, true);
117 new ArtifactGeneratorToscaParser(null).processResourceModels(dummyResource, nodeTemplateList);
121 * Initialise the Artifact Generator Widget Mapping config with incomplete data.
123 @Test(expected = IllegalArgumentException.class)
124 public void testToscaMappingWithoutType() {
125 WidgetMapping invalidMapping = new WidgetMapping();
126 invalidMapping.setType(null);
127 WidgetConfigurationUtil.setWidgetMappings(Collections.singletonList(invalidMapping));
131 * Initialise the Artifact Generator Widget Mapping config with incomplete data.
133 @Test(expected = IllegalArgumentException.class)
134 public void testToscaMappingWithoutWidget() {
135 WidgetMapping invalidMapping = new WidgetMapping();
136 invalidMapping.setWidget(null);
137 WidgetConfigurationUtil.setWidgetMappings(Collections.singletonList(invalidMapping));
141 * Process a dummy Group object for a Service Resource.
143 * @throws XmlArtifactGenerationException
146 public void testInstanceGroups() throws XmlArtifactGenerationException {
147 final String instanceGroupType = "org.openecomp.groups.ResourceInstanceGroup";
148 WidgetConfigurationUtil.setSupportedInstanceGroups(Collections.singletonList(instanceGroupType));
150 ISdcCsarHelper helper = Mockito.mock(ISdcCsarHelper.class);
151 SubstitutionMappings sm = Mockito.mock(SubstitutionMappings.class);
153 NodeTemplate serviceNodeTemplate =
154 buildNodeTemplate("service", "org.openecomp.resource.cr.a-collection-resource");
155 serviceNodeTemplate.setSubMappingToscaTemplate(sm);
156 Mockito.when(helper.getNodeTemplateByName(serviceNodeTemplate.getName())).thenReturn(serviceNodeTemplate);
158 ArrayList<Group> groups = new ArrayList<>();
159 groups.add(buildGroup("group", instanceGroupType));
160 Mockito.when(helper.getGroupsOfOriginOfNodeTemplate(serviceNodeTemplate)).thenReturn(groups);
162 ArtifactGeneratorToscaParser parser = new ArtifactGeneratorToscaParser(helper);
163 List<Resource> resources =
164 parser.processInstanceGroups(new Resource(Type.INSTANCE_GROUP, true), serviceNodeTemplate);
166 assertThat(resources.size(), is(1));
167 Resource resource = resources.get(0);
168 assertThat(resource.getModelNameVersionId(), is(equalTo(TEST_UUID)));
172 * Create a NodeTemplate for unit testing purposes. In production code this object would only be created by the
176 * name of the NodeTemplate
178 * type of the NodeTemplate
179 * @return a new NodeTemplate object
181 private NodeTemplate buildNodeTemplate(String name, String type) {
182 LinkedHashMap<String, Object> nodeTemplateMap = new LinkedHashMap<>();
183 nodeTemplateMap.put(name, buildMap("type", type));
184 nodeTemplateMap.put(type, buildNodeTemplateCustomDefs());
185 return new NodeTemplate(name, nodeTemplateMap, nodeTemplateMap, null, null);
188 private LinkedHashMap<String, Object> buildNodeTemplateCustomDefs() {
189 LinkedHashMap<String, Object> customDefs = buildCustomDefs();
190 customDefs.put("attributes", null);
191 customDefs.put("requirements", null);
192 customDefs.put("capabilities", null);
193 customDefs.put("artifacts", null);
197 private Group buildGroup(String name, String type) {
198 LinkedHashMap<String, Object> template = new LinkedHashMap<>();
199 template.put("type", type);
200 template.put("metadata", new LinkedHashMap<>());
201 template.put("properties", buildMap("UUID", TEST_UUID));
202 LinkedHashMap<String, Object> customDefMap = buildMap(name, template);
203 customDefMap.put(type, buildGroupCustomDefs());
204 return new Group(name, template, null, customDefMap);
207 private LinkedHashMap<String, Object> buildGroupCustomDefs() {
208 LinkedHashMap<String, Object> customDefs = buildCustomDefs();
209 customDefs.put("members", null);
213 private LinkedHashMap<String, Object> buildCustomDefs() {
214 LinkedHashMap<String, Object> customDefs = new LinkedHashMap<>();
215 customDefs.put("derived_from", null);
216 customDefs.put("metadata", null);
217 customDefs.put("version", null);
218 customDefs.put("description", null);
219 customDefs.put("interfaces", null);
220 customDefs.put("properties", buildMap("UUID", buildMap("type", "java.lang.String")));
224 private LinkedHashMap<String, Object> buildMap(String key, Object value) {
225 LinkedHashMap<String, Object> map = new LinkedHashMap<>();