Move Widget attribute data to the mappings JSON
[aai/babel.git] / src / test / java / org / onap / aai / babel / parser / TestArtifactGeneratorToscaParser.java
1 /**
2  * ============LICENSE_START=======================================================
3  * org.onap.aai
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
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.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;
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      */
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();
65     }
66
67     /**
68      * Initialize the Generator with an invalid mappings file path.
69      * 
70      * @throws IOException
71      */
72     @Test(expected = IllegalArgumentException.class)
73     public void testMissingMappingsFile() throws IOException {
74         ArtifactGeneratorToscaParser.initToscaMappingsConfiguration("non-existent.file");
75     }
76
77     /**
78      * Initialize the Generator with no Widget Mappings content.
79      * 
80      * @throws IOException
81      */
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);
86     }
87
88     /**
89      * Initialize the Generator with invalid Widget Mappings content.
90      * 
91      * @throws IOException
92      */
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);
97     }
98
99     /**
100      * Process an Allotted Resource that does not have a Providing Service.
101      */
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),
106                 nodeTemplateList);
107     }
108
109     /**
110      * Add a CR (a type of Resource which is not a Providing Service) to a Resource Model.
111      */
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);
118     }
119
120     /**
121      * Initialise the Artifact Generator Widget Mapping config with incomplete data.
122      */
123     @Test(expected = IllegalArgumentException.class)
124     public void testToscaMappingWithoutType() {
125         WidgetMapping invalidMapping = new WidgetMapping();
126         invalidMapping.setType(null);
127         WidgetConfigurationUtil.setWidgetMappings(Collections.singletonList(invalidMapping));
128     }
129
130     /**
131      * Initialise the Artifact Generator Widget Mapping config with incomplete data.
132      */
133     @Test(expected = IllegalArgumentException.class)
134     public void testToscaMappingWithoutWidget() {
135         WidgetMapping invalidMapping = new WidgetMapping();
136         invalidMapping.setWidget(null);
137         WidgetConfigurationUtil.setWidgetMappings(Collections.singletonList(invalidMapping));
138     }
139
140     /**
141      * Process a dummy Group object for a Service Resource.
142      * 
143      * @throws XmlArtifactGenerationException
144      */
145     @Test
146     public void testInstanceGroups() throws XmlArtifactGenerationException {
147         final String instanceGroupType = "org.openecomp.groups.ResourceInstanceGroup";
148         WidgetConfigurationUtil.setSupportedInstanceGroups(Collections.singletonList(instanceGroupType));
149
150         ISdcCsarHelper helper = Mockito.mock(ISdcCsarHelper.class);
151         SubstitutionMappings sm = Mockito.mock(SubstitutionMappings.class);
152
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);
157
158         ArrayList<Group> groups = new ArrayList<>();
159         groups.add(buildGroup("group", instanceGroupType));
160         Mockito.when(helper.getGroupsOfOriginOfNodeTemplate(serviceNodeTemplate)).thenReturn(groups);
161
162         ArtifactGeneratorToscaParser parser = new ArtifactGeneratorToscaParser(helper);
163         List<Resource> resources =
164                 parser.processInstanceGroups(new Resource(Type.INSTANCE_GROUP, true), serviceNodeTemplate);
165
166         assertThat(resources.size(), is(1));
167         Resource resource = resources.get(0);
168         assertThat(resource.getModelNameVersionId(), is(equalTo(TEST_UUID)));
169     }
170
171     /**
172      * Create a NodeTemplate for unit testing purposes. In production code this object would only be created by the
173      * sdc-tosca parser.
174      *
175      * @param name
176      *            name of the NodeTemplate
177      * @param type
178      *            type of the NodeTemplate
179      * @return a new NodeTemplate object
180      */
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);
186     }
187
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);
194         return customDefs;
195     }
196
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);
205     }
206
207     private LinkedHashMap<String, Object> buildGroupCustomDefs() {
208         LinkedHashMap<String, Object> customDefs = buildCustomDefs();
209         customDefs.put("members", null);
210         return customDefs;
211     }
212
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")));
221         return customDefs;
222     }
223
224     private LinkedHashMap<String, Object> buildMap(String key, Object value) {
225         LinkedHashMap<String, Object> map = new LinkedHashMap<>();
226         map.put(key, value);
227         return map;
228     }
229 }