Additional JUnit tests for Model classes
[aai/babel.git] / src / test / java / org / onap / aai / babel / parser / TestArtifactGeneratorToscaParser.java
1 /**
2  * ============LICENSE_START=======================================================
3  * org.onap.aai
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
11  *
12  *       http://www.apache.org/licenses/LICENSE-2.0
13  *
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=========================================================
20  */
21
22 package org.onap.aai.babel.parser;
23
24 import static org.hamcrest.CoreMatchers.equalTo;
25 import static org.hamcrest.MatcherAssert.assertThat;
26 import static org.hamcrest.Matchers.is;
27
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;
49
50 /**
51  * Direct tests of the TOSCA parser-based Artifact Generator {@link ArtifactGeneratorToscaParser}., to cover exceptional
52  * cases.
53  */
54
55 public class TestArtifactGeneratorToscaParser {
56
57     private static final String TEST_UUID = "1234";
58
59     /**
60      * Initialize the Generator with an invalid artifact generator properties file path.
61      *
62      * @throws IOException
63      *             if an error occurs reading the configuration properties
64      */
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();
69     }
70
71     /**
72      * Initialize the Generator with an invalid mappings file path.
73      *
74      * @throws IOException
75      *             if the file content could not be read successfully
76      */
77     @Test(expected = IllegalArgumentException.class)
78     public void testMissingMappingsFile() throws IOException {
79         ArtifactGeneratorToscaParser.initToscaMappingsConfiguration("non-existent.file");
80     }
81
82     /**
83      * Initialize the Generator with no Widget Mappings content.
84      *
85      * @throws IOException
86      *             if the file content could not be read successfully
87      */
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);
92     }
93
94     /**
95      * Initialize the Generator with invalid Widget Mappings content.
96      *
97      * @throws IOException
98      *             if the file content could not be read successfully
99      */
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);
104     }
105
106     /**
107      * Process an Allotted Resource that does not have a Providing Service.
108      */
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);
114     }
115
116     /**
117      * Add a CR (a type of Resource which is not a Providing Service) to a Resource Model.
118      */
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);
125     }
126
127     /**
128      * Initialize the Artifact Generator Widget Mapping config with incomplete data (no type).
129      *
130      * @throws IOException
131      *             if a WidgetMapping is invalid
132      */
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));
138     }
139
140     /**
141      * Initialize the Artifact Generator Widget Mapping config with invalid data (type value).
142      *
143      * @throws IOException
144      *             if a WidgetMapping is invalid
145      */
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));
151     }
152
153     /**
154      * Initialize the Artifact Generator Widget Mapping config with incomplete data (no widget name).
155      *
156      * @throws IOException
157      *             if a WidgetMapping is invalid
158      */
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));
164     }
165
166     /**
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.
169      *
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
174      */
175     @Test
176     public void testAddWidgetToService() throws IOException, XmlArtifactGenerationException {
177         ArtifactTestUtils testUtils = new ArtifactTestUtils();
178         testUtils.loadWidgetMappings();
179         testUtils.loadWidgetToUuidMappings();
180
181         Model serviceModel = new Service();
182         Resource resourceModel = new Resource(WidgetType.valueOf("VF"), false);
183         resourceModel.setModelType(ModelType.WIDGET);
184
185         ISdcCsarHelper helper = Mockito.mock(ISdcCsarHelper.class);
186         ArtifactGeneratorToscaParser parser = new ArtifactGeneratorToscaParser(helper);
187         parser.addRelatedModel(serviceModel, resourceModel);
188     }
189
190     /**
191      * Process a dummy Group object for a Service Resource.
192      *
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
197      */
198     @Test
199     public void testInstanceGroups() throws XmlArtifactGenerationException, IOException {
200         new ArtifactTestUtils().loadWidgetMappings();
201
202         final String instanceGroupType = "org.openecomp.groups.ResourceInstanceGroup";
203         WidgetConfigurationUtil.setSupportedInstanceGroups(Collections.singletonList(instanceGroupType));
204
205         ISdcCsarHelper helper = Mockito.mock(ISdcCsarHelper.class);
206         SubstitutionMappings sm = Mockito.mock(SubstitutionMappings.class);
207
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);
212
213         ArrayList<Group> groups = new ArrayList<>();
214         groups.add(buildGroup("group", instanceGroupType));
215         Mockito.when(helper.getGroupsOfOriginOfNodeTemplate(serviceNodeTemplate)).thenReturn(groups);
216
217         ArtifactGeneratorToscaParser parser = new ArtifactGeneratorToscaParser(helper);
218         Resource groupResource = new Resource(WidgetType.valueOf("INSTANCE_GROUP"), true);
219         List<Resource> resources = parser.processInstanceGroups(groupResource, serviceNodeTemplate);
220
221         assertThat(resources.size(), is(1));
222         Resource resource = resources.get(0);
223         assertThat(resource.getModelNameVersionId(), is(equalTo(TEST_UUID)));
224     }
225
226     /**
227      * Create a NodeTemplate for unit testing purposes. In production code this object would only be created by the
228      * sdc-tosca parser.
229      *
230      * @param name
231      *            name of the NodeTemplate
232      * @param type
233      *            type of the NodeTemplate
234      * @return a new NodeTemplate object
235      */
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);
241     }
242
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);
249         return customDefs;
250     }
251
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);
260     }
261
262     private LinkedHashMap<String, Object> buildGroupCustomDefs() {
263         LinkedHashMap<String, Object> customDefs = buildCustomDefs();
264         customDefs.put("members", null);
265         return customDefs;
266     }
267
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")));
276         return customDefs;
277     }
278
279     private LinkedHashMap<String, Object> buildMap(String key, Object value) {
280         LinkedHashMap<String, Object> map = new LinkedHashMap<>();
281         map.put(key, value);
282         return map;
283     }
284 }