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.
60 * if an error occurs reading the configuration properties
62 @Test(expected = IllegalArgumentException.class)
63 public void testMissingPropertiesFile() throws IOException {
64 System.setProperty(ArtifactGeneratorToscaParser.PROPERTY_ARTIFACT_GENERATOR_CONFIG_FILE, "non-existent.file");
65 ArtifactGeneratorToscaParser.initWidgetConfiguration();
69 * Initialize the Generator with an invalid mappings file path.
72 * if the file content could not be read successfully
74 @Test(expected = IllegalArgumentException.class)
75 public void testMissingMappingsFile() throws IOException {
76 ArtifactGeneratorToscaParser.initToscaMappingsConfiguration("non-existent.file");
80 * Initialize the Generator with no Widget Mappings content.
83 * if the file content could not be read successfully
85 @Test(expected = IOException.class)
86 public void testMissingMappingsContent() throws IOException {
87 String invalidJson = new ArtifactTestUtils().getResourcePath(Resources.EMPTY_TOSCA_MAPPING_CONFIG);
88 ArtifactGeneratorToscaParser.initToscaMappingsConfiguration(invalidJson);
92 * Initialize the Generator with invalid Widget Mappings content.
95 * if the file content could not be read successfully
97 @Test(expected = IOException.class)
98 public void testInvalidMappingsContent() throws IOException {
99 String invalidJson = new ArtifactTestUtils().getResourcePath(Resources.INVALID_TOSCA_MAPPING_CONFIG);
100 ArtifactGeneratorToscaParser.initToscaMappingsConfiguration(invalidJson);
104 * Process an Allotted Resource that does not have a Providing Service.
106 @Test(expected = IllegalArgumentException.class)
107 public void testMissingProvidingService() {
108 List<NodeTemplate> nodeTemplateList = Collections.singletonList(buildNodeTemplate("name", "BlockStorage"));
109 new ArtifactGeneratorToscaParser(null).processResourceModels(new Resource(Type.ALLOTTED_RESOURCE, true),
114 * Add a CR (a type of Resource which is not a Providing Service) to a Resource Model.
116 @Test(expected = IllegalArgumentException.class)
117 public void testAddResourceNotProvidingService() {
118 List<NodeTemplate> nodeTemplateList = Collections.singletonList(buildNodeTemplate("testCR", "CR"));
119 // Create any Resource to which the CR can be added
120 final Resource dummyResource = new Resource(Type.ALLOTTED_RESOURCE, true);
121 new ArtifactGeneratorToscaParser(null).processResourceModels(dummyResource, nodeTemplateList);
125 * Initialize the Artifact Generator Widget Mapping config with incomplete data (no type).
127 * @throws IOException
129 @Test(expected = IOException.class)
130 public void testToscaMappingWithoutType() throws IOException {
131 WidgetMapping invalidMapping = new WidgetMapping();
132 invalidMapping.setType(null);
133 WidgetConfigurationUtil.setWidgetMappings(Collections.singletonList(invalidMapping));
137 * Initialize the Artifact Generator Widget Mapping config with invalid data (type value).
139 * @throws IOException
141 @Test(expected = IOException.class)
142 public void testToscaMappingWithInvalidType() throws IOException {
143 WidgetMapping invalidMapping = new WidgetMapping();
144 invalidMapping.setType("invalid");
145 WidgetConfigurationUtil.setWidgetMappings(Collections.singletonList(invalidMapping));
149 * Initialize the Artifact Generator Widget Mapping config with incomplete data (no widget name).
151 * @throws IOException
153 @Test(expected = IOException.class)
154 public void testToscaMappingWithoutWidget() throws IOException {
155 WidgetMapping invalidMapping = new WidgetMapping();
156 invalidMapping.setWidget(null);
157 WidgetConfigurationUtil.setWidgetMappings(Collections.singletonList(invalidMapping));
161 * Process a dummy Group object for a Service Resource.
163 * @throws XmlArtifactGenerationException
164 * if there is no configuration defined for a member Widget of an instance group
167 public void testInstanceGroups() throws XmlArtifactGenerationException {
168 final String instanceGroupType = "org.openecomp.groups.ResourceInstanceGroup";
169 WidgetConfigurationUtil.setSupportedInstanceGroups(Collections.singletonList(instanceGroupType));
171 ISdcCsarHelper helper = Mockito.mock(ISdcCsarHelper.class);
172 SubstitutionMappings sm = Mockito.mock(SubstitutionMappings.class);
174 NodeTemplate serviceNodeTemplate =
175 buildNodeTemplate("service", "org.openecomp.resource.cr.a-collection-resource");
176 serviceNodeTemplate.setSubMappingToscaTemplate(sm);
177 Mockito.when(helper.getNodeTemplateByName(serviceNodeTemplate.getName())).thenReturn(serviceNodeTemplate);
179 ArrayList<Group> groups = new ArrayList<>();
180 groups.add(buildGroup("group", instanceGroupType));
181 Mockito.when(helper.getGroupsOfOriginOfNodeTemplate(serviceNodeTemplate)).thenReturn(groups);
183 ArtifactGeneratorToscaParser parser = new ArtifactGeneratorToscaParser(helper);
184 List<Resource> resources =
185 parser.processInstanceGroups(new Resource(Type.INSTANCE_GROUP, true), serviceNodeTemplate);
187 assertThat(resources.size(), is(1));
188 Resource resource = resources.get(0);
189 assertThat(resource.getModelNameVersionId(), is(equalTo(TEST_UUID)));
193 * Create a NodeTemplate for unit testing purposes. In production code this object would only be created by the
197 * name of the NodeTemplate
199 * type of the NodeTemplate
200 * @return a new NodeTemplate object
202 private NodeTemplate buildNodeTemplate(String name, String type) {
203 LinkedHashMap<String, Object> nodeTemplateMap = new LinkedHashMap<>();
204 nodeTemplateMap.put(name, buildMap("type", type));
205 nodeTemplateMap.put(type, buildNodeTemplateCustomDefs());
206 return new NodeTemplate(name, nodeTemplateMap, nodeTemplateMap, null, null);
209 private LinkedHashMap<String, Object> buildNodeTemplateCustomDefs() {
210 LinkedHashMap<String, Object> customDefs = buildCustomDefs();
211 customDefs.put("attributes", null);
212 customDefs.put("requirements", null);
213 customDefs.put("capabilities", null);
214 customDefs.put("artifacts", null);
218 private Group buildGroup(String name, String type) {
219 LinkedHashMap<String, Object> template = new LinkedHashMap<>();
220 template.put("type", type);
221 template.put("metadata", new LinkedHashMap<>());
222 template.put("properties", buildMap("UUID", TEST_UUID));
223 LinkedHashMap<String, Object> customDefMap = buildMap(name, template);
224 customDefMap.put(type, buildGroupCustomDefs());
225 return new Group(name, template, null, customDefMap);
228 private LinkedHashMap<String, Object> buildGroupCustomDefs() {
229 LinkedHashMap<String, Object> customDefs = buildCustomDefs();
230 customDefs.put("members", null);
234 private LinkedHashMap<String, Object> buildCustomDefs() {
235 LinkedHashMap<String, Object> customDefs = new LinkedHashMap<>();
236 customDefs.put("derived_from", null);
237 customDefs.put("metadata", null);
238 customDefs.put("version", null);
239 customDefs.put("description", null);
240 customDefs.put("interfaces", null);
241 customDefs.put("properties", buildMap("UUID", buildMap("type", "java.lang.String")));
245 private LinkedHashMap<String, Object> buildMap(String key, Object value) {
246 LinkedHashMap<String, Object> map = new LinkedHashMap<>();