Reimplement Widget.Type enum class
[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.Resource;
41 import org.onap.aai.babel.xml.generator.model.WidgetType;
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;
46
47 /**
48  * Direct tests of the TOSCA parser-based Artifact Generator {@link ArtifactGeneratorToscaParser}., to cover exceptional
49  * cases.
50  */
51
52 public class TestArtifactGeneratorToscaParser {
53
54     private static final String TEST_UUID = "1234";
55
56     /**
57      * Initialize the Generator with an invalid artifact generator properties file path.
58      * 
59      * @throws IOException
60      *             if an error occurs reading the configuration properties
61      */
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();
66     }
67
68     /**
69      * Initialize the Generator with an invalid mappings file path.
70      * 
71      * @throws IOException
72      *             if the file content could not be read successfully
73      */
74     @Test(expected = IllegalArgumentException.class)
75     public void testMissingMappingsFile() throws IOException {
76         ArtifactGeneratorToscaParser.initToscaMappingsConfiguration("non-existent.file");
77     }
78
79     /**
80      * Initialize the Generator with no Widget Mappings content.
81      * 
82      * @throws IOException
83      *             if the file content could not be read successfully
84      */
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);
89     }
90
91     /**
92      * Initialize the Generator with invalid Widget Mappings content.
93      * 
94      * @throws IOException
95      *             if the file content could not be read successfully
96      */
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);
101     }
102
103     /**
104      * Process an Allotted Resource that does not have a Providing Service.
105      */
106     @Test(expected = IllegalArgumentException.class)
107     public void testMissingProvidingService() {
108         List<NodeTemplate> nodeTemplateList = Collections.singletonList(buildNodeTemplate("name", "BlockStorage"));
109         new ArtifactGeneratorToscaParser(null)
110                 .processResourceModels(new Resource(WidgetType.valueOf("ALLOTTED_RESOURCE"), true), nodeTemplateList);
111     }
112
113     /**
114      * Add a CR (a type of Resource which is not a Providing Service) to a Resource Model.
115      */
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(WidgetType.valueOf("ALLOTTED_RESOURCE"), true);
121         new ArtifactGeneratorToscaParser(null).processResourceModels(dummyResource, nodeTemplateList);
122     }
123
124     /**
125      * Initialize the Artifact Generator Widget Mapping config with incomplete data (no type).
126      * 
127      * @throws IOException
128      *             if a WidgetMapping is invalid
129      */
130     @Test(expected = IOException.class)
131     public void testToscaMappingWithoutType() throws IOException {
132         WidgetMapping invalidMapping = new WidgetMapping();
133         invalidMapping.setType(null);
134         WidgetConfigurationUtil.setWidgetMappings(Collections.singletonList(invalidMapping));
135     }
136
137     /**
138      * Initialize the Artifact Generator Widget Mapping config with invalid data (type value).
139      * 
140      * @throws IOException
141      *             if a WidgetMapping is invalid
142      */
143     @Test(expected = IOException.class)
144     public void testToscaMappingWithInvalidType() throws IOException {
145         WidgetMapping invalidMapping = new WidgetMapping();
146         invalidMapping.setType("invalid");
147         WidgetConfigurationUtil.setWidgetMappings(Collections.singletonList(invalidMapping));
148     }
149
150     /**
151      * Initialize the Artifact Generator Widget Mapping config with incomplete data (no widget name).
152      * 
153      * @throws IOException
154      *             if a WidgetMapping is invalid
155      */
156     @Test(expected = IOException.class)
157     public void testToscaMappingWithoutWidget() throws IOException {
158         WidgetMapping invalidMapping = new WidgetMapping();
159         invalidMapping.setWidget(null);
160         WidgetConfigurationUtil.setWidgetMappings(Collections.singletonList(invalidMapping));
161     }
162
163     /**
164      * Process a dummy Group object for a Service Resource.
165      * 
166      * @throws XmlArtifactGenerationException
167      *             if there is no configuration defined for a member Widget of an instance group
168      * @throws IOException
169      *             if the widget mappings cannot be loaded
170      */
171     @Test
172     public void testInstanceGroups() throws XmlArtifactGenerationException, IOException {
173         new ArtifactTestUtils().loadWidgetMappings();
174
175         final String instanceGroupType = "org.openecomp.groups.ResourceInstanceGroup";
176         WidgetConfigurationUtil.setSupportedInstanceGroups(Collections.singletonList(instanceGroupType));
177
178         ISdcCsarHelper helper = Mockito.mock(ISdcCsarHelper.class);
179         SubstitutionMappings sm = Mockito.mock(SubstitutionMappings.class);
180
181         NodeTemplate serviceNodeTemplate =
182                 buildNodeTemplate("service", "org.openecomp.resource.cr.a-collection-resource");
183         serviceNodeTemplate.setSubMappingToscaTemplate(sm);
184         Mockito.when(helper.getNodeTemplateByName(serviceNodeTemplate.getName())).thenReturn(serviceNodeTemplate);
185
186         ArrayList<Group> groups = new ArrayList<>();
187         groups.add(buildGroup("group", instanceGroupType));
188         Mockito.when(helper.getGroupsOfOriginOfNodeTemplate(serviceNodeTemplate)).thenReturn(groups);
189
190         ArtifactGeneratorToscaParser parser = new ArtifactGeneratorToscaParser(helper);
191         Resource groupResource = new Resource(WidgetType.valueOf("INSTANCE_GROUP"), true);
192         List<Resource> resources = parser.processInstanceGroups(groupResource, serviceNodeTemplate);
193
194         assertThat(resources.size(), is(1));
195         Resource resource = resources.get(0);
196         assertThat(resource.getModelNameVersionId(), is(equalTo(TEST_UUID)));
197     }
198
199     /**
200      * Create a NodeTemplate for unit testing purposes. In production code this object would only be created by the
201      * sdc-tosca parser.
202      *
203      * @param name
204      *            name of the NodeTemplate
205      * @param type
206      *            type of the NodeTemplate
207      * @return a new NodeTemplate object
208      */
209     private NodeTemplate buildNodeTemplate(String name, String type) {
210         LinkedHashMap<String, Object> nodeTemplateMap = new LinkedHashMap<>();
211         nodeTemplateMap.put(name, buildMap("type", type));
212         nodeTemplateMap.put(type, buildNodeTemplateCustomDefs());
213         return new NodeTemplate(name, nodeTemplateMap, nodeTemplateMap, null, null);
214     }
215
216     private LinkedHashMap<String, Object> buildNodeTemplateCustomDefs() {
217         LinkedHashMap<String, Object> customDefs = buildCustomDefs();
218         customDefs.put("attributes", null);
219         customDefs.put("requirements", null);
220         customDefs.put("capabilities", null);
221         customDefs.put("artifacts", null);
222         return customDefs;
223     }
224
225     private Group buildGroup(String name, String type) {
226         LinkedHashMap<String, Object> template = new LinkedHashMap<>();
227         template.put("type", type);
228         template.put("metadata", new LinkedHashMap<>());
229         template.put("properties", buildMap("UUID", TEST_UUID));
230         LinkedHashMap<String, Object> customDefMap = buildMap(name, template);
231         customDefMap.put(type, buildGroupCustomDefs());
232         return new Group(name, template, null, customDefMap);
233     }
234
235     private LinkedHashMap<String, Object> buildGroupCustomDefs() {
236         LinkedHashMap<String, Object> customDefs = buildCustomDefs();
237         customDefs.put("members", null);
238         return customDefs;
239     }
240
241     private LinkedHashMap<String, Object> buildCustomDefs() {
242         LinkedHashMap<String, Object> customDefs = new LinkedHashMap<>();
243         customDefs.put("derived_from", null);
244         customDefs.put("metadata", null);
245         customDefs.put("version", null);
246         customDefs.put("description", null);
247         customDefs.put("interfaces", null);
248         customDefs.put("properties", buildMap("UUID", buildMap("type", "java.lang.String")));
249         return customDefs;
250     }
251
252     private LinkedHashMap<String, Object> buildMap(String key, Object value) {
253         LinkedHashMap<String, Object> map = new LinkedHashMap<>();
254         map.put(key, value);
255         return map;
256     }
257 }