Add JUnit test for invalid TOSCA mappings JSON
[aai/babel.git] / src / test / java / org / onap / aai / babel / xml / generator / model / TestVfModule.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.xml.generator.model;
23
24 import static org.hamcrest.CoreMatchers.is;
25 import static org.hamcrest.CoreMatchers.notNullValue;
26 import static org.hamcrest.collection.IsCollectionWithSize.hasSize;
27 import static org.junit.Assert.assertThat;
28
29 import java.io.IOException;
30 import java.util.Collections;
31 import java.util.HashMap;
32 import java.util.Map;
33 import org.junit.BeforeClass;
34 import org.junit.Test;
35 import org.onap.aai.babel.util.ArtifactTestUtils;
36 import org.onap.aai.babel.xml.generator.XmlArtifactGenerationException;
37 import org.onap.aai.babel.xml.generator.model.Widget.Type;
38
39 /**
40  * Direct tests of the VFMODULE Resource and Widget functionality to improve code coverage.
41  */
42 public class TestVfModule {
43
44     /**
45      * Load the Widget Configuration, including the type mappings and the UUID mappings.
46      *
47      * @throws IOException
48      *             if the mappings configuration cannot be loaded
49      */
50     @BeforeClass
51     public static void setup() throws IOException {
52         ArtifactTestUtils util = new ArtifactTestUtils();
53         util.loadWidgetToUuidMappings();
54         util.loadWidgetMappings();
55     }
56
57     /**
58      * Call hashCode() method for code coverage.
59      */
60     @Test
61     public void testHashCode() {
62         Resource vfModule = createNewVfModule();
63         populateIdentInfo(vfModule);
64         assertThat(vfModule.hashCode(), is(notNullValue()));
65     }
66
67     /**
68      * Call equals() method for code coverage.
69      */
70     @Test
71     public void testEquals() {
72         Resource vfModuleA = createNewVfModule();
73         populateIdentInfo(vfModuleA);
74
75         // equals() is reflexive
76         assertThat(vfModuleA.equals(vfModuleA), is(true));
77
78         // equals() is symmetric
79         Resource vfModuleB = createNewVfModule();
80         populateIdentInfo(vfModuleB);
81         assertThat(vfModuleA.equals(vfModuleB), is(true));
82         assertThat(vfModuleB.equals(vfModuleA), is(true));
83
84         assertThat(vfModuleA.equals(null), is(false));
85     }
86
87     @Test
88     public void testAddVServerWidgetToVf() throws XmlArtifactGenerationException {
89         assertAddWidget(createNewVfModule(), Type.VSERVER);
90     }
91
92     @Test
93     public void testAddServiceWidgetToVf() throws XmlArtifactGenerationException {
94         assertAddWidget(createNewVfModule(), Type.SERVICE);
95     }
96
97     /**
98      * Add a new Widget to a VF Module, where the Widget is NOT set as a member. N.B. For the current VF Module
99      * implementation the actual Widget type is not important.
100      * 
101      * @throws XmlArtifactGenerationException
102      *             if the Widget mapping configuration is missing
103      */
104     @Test
105     public void testNonMemberWidgetToVf() throws XmlArtifactGenerationException {
106         Resource vfModule = createNewVfModule();
107         assertThat(vfModule.addWidget(createNewWidget(Type.SERVICE)), is(false));
108         assertNumberOfWidgets(vfModule, 0);
109     }
110
111     /**
112      * OAM Network is specifically excluded from a VF Module.
113      * 
114      * @throws XmlArtifactGenerationException
115      *             if the Widget mapping configuration is missing
116      */
117     @Test
118     public void testAddOamNetworkWidgetToVf() throws XmlArtifactGenerationException {
119         Resource vfModule = createNewVfModule();
120         assertThat(createNewWidgetForModule(vfModule, Type.OAM_NETWORK), is(false));
121         assertNumberOfWidgets(vfModule, 0);
122     }
123
124     /**
125      * Add a Volume Widget to a VF Module via a vserver Widget.
126      * 
127      * <li>Create a VF Module</li>
128      * <li>Add a Volume Widget</li>
129      * <li>Add a vserver Widget</li>
130      * <li>Check that the Volume Widget appears under the vserver</li>
131      * 
132      * @throws XmlArtifactGenerationException
133      *             if the Widget mapping configuration is missing
134      */
135     @Test
136     public void testAddVolumeWidgetToVf() throws XmlArtifactGenerationException {
137         Resource vfModule = createNewVfModule();
138
139         // Adding a Volume widget has no effect until a vserver widget is added.
140         assertAddWidget(vfModule, Type.VOLUME);
141         assertNumberOfWidgets(vfModule, 0);
142
143         final int vserverBaseWidgetCount = createVserverForVf(vfModule);
144
145         // The vserver now has Volume as well.
146         assertNumberOfWidgets(vfModule.vserver, vserverBaseWidgetCount + 1);
147
148         // Adding another instance of a vserver widget fails.
149         assertFailToAddWidget(vfModule, Type.VSERVER);
150         assertNumberOfWidgets(vfModule, 1);
151
152         // Adding another Volume widget is always treated as successful.
153         assertAddWidget(vfModule, Type.VOLUME);
154         // Assert that no additional Widgets are actually present.
155         assertNumberOfWidgets(vfModule, 1);
156         assertNumberOfWidgets(vfModule.vserver, vserverBaseWidgetCount + 1);
157     }
158
159     /**
160      * Add an L-Interface Widget to a VF Module via a vserver Widget.
161      * 
162      * <li>Create a VF Module</li>
163      * <li>Add an L-Interface Widget</li>
164      * <li>Add a vserver Widget</li>
165      * <li>Check that the L-Interface Widget appears under the vserver</li>
166      * 
167      * @throws XmlArtifactGenerationException
168      *             if the Widget mapping configuration is missing
169      */
170     @Test
171     public void testAddLinterfaceWidgetToVf() throws XmlArtifactGenerationException {
172         Resource vfModule = createNewVfModule();
173
174         // Adding an L-Interface widget has no effect until a vserver widget is added.
175         assertFailToAddWidget(vfModule, Type.LINT);
176         assertNumberOfWidgets(vfModule, 0);
177
178         final int vserverBaseWidgetCount = createVserverForVf(vfModule);
179
180         // The vserver now has an L-Interface as well.
181         assertNumberOfWidgets(vfModule.vserver, vserverBaseWidgetCount + 1);
182
183         // Adding another instance of a vserver widget fails.
184         assertFailToAddWidget(vfModule, Type.VSERVER);
185         assertNumberOfWidgets(vfModule, 1);
186
187         // Adding an L-Interface widget is always treated as successful when a vserver exists.
188         assertAddWidget(vfModule, Type.LINT);
189         // Assert that no additional Widgets are actually present.
190         assertNumberOfWidgets(vfModule, 1);
191         assertNumberOfWidgets(vfModule.vserver, vserverBaseWidgetCount + 1);
192     }
193
194     /**
195      * Add a Volume and an L-Interface Widget to a VF Module via a vserver Widget.
196      * 
197      * <li>Create a VF Module</li>
198      * <li>Add a Volume Widget</li>
199      * <li>Add an L-Interface Widget</li>
200      * <li>Add a vserver Widget</li>
201      * <li>Check that both Widgets appear under the vserver</li>
202      * 
203      * @throws XmlArtifactGenerationException
204      *             if the Widget mapping configuration is missing
205      */
206     @Test
207     public void testAddVolumeAndLinterfaceWidgetToVf() throws XmlArtifactGenerationException {
208         Resource vfModule = createNewVfModule();
209
210         // Adding a Volume widget has no effect until a vserver widget is added.
211         assertAddWidget(vfModule, Type.VOLUME);
212         assertNumberOfWidgets(vfModule, 0);
213
214         // Adding an L-Interface widget has no effect until a vserver widget is added.
215         assertFailToAddWidget(vfModule, Type.LINT);
216         assertNumberOfWidgets(vfModule, 0);
217
218         final int vserverBaseWidgetCount = createVserverForVf(vfModule);
219
220         // The vserver now has both Volume and L-Interface.
221         assertNumberOfWidgets(vfModule.vserver, vserverBaseWidgetCount + 2);
222
223         // Adding another instance of a vserver widget fails.
224         assertFailToAddWidget(vfModule, Type.VSERVER);
225         assertNumberOfWidgets(vfModule, 1);
226
227         // Add new instances (with no effect).
228         assertAddWidget(vfModule, Type.VOLUME);
229         assertAddWidget(vfModule, Type.LINT);
230         // Assert that no additional Widgets are in fact present.
231         assertNumberOfWidgets(vfModule, 1);
232         assertNumberOfWidgets(vfModule.vserver, vserverBaseWidgetCount + 2);
233     }
234
235     private void assertNumberOfWidgets(Model model, int numberOfWidgets) {
236         assertThat(model.getWidgets(), hasSize(numberOfWidgets));
237     }
238
239     /**
240      * Use the static Factory method to create a new Widget.
241      *
242      * @param widgetType
243      *            type of Widget to create
244      * @return a new Widget
245      * @throws XmlArtifactGenerationException
246      *             if the Widget mapping configuration is missing
247      */
248     private Widget createNewWidget(Type widgetType) throws XmlArtifactGenerationException {
249         return Widget.getWidget(widgetType);
250     }
251
252     /**
253      * Create a new VF Module that contains zero widgets and has no members.
254      *
255      * @return new VF Module resource
256      */
257     private Resource createNewVfModule() {
258         Resource vfModule = new Resource(Type.VFMODULE, true);
259         assertNumberOfWidgets(vfModule, 0);
260         return vfModule;
261     }
262
263     /**
264      * Set up some dummy Model Identification properties.
265      *
266      * @param vfModule
267      *            to be populated
268      */
269     private void populateIdentInfo(Resource vfModule) {
270         Map<String, String> modelIdentInfo = new HashMap<>();
271         modelIdentInfo.put("UUID", "dummy_uuid");
272         vfModule.populateModelIdentificationInformation(modelIdentInfo);
273     }
274
275     /**
276      * Create a new Widget and assert that it is successfully added to the VF Module.
277      *
278      * @param vfModule
279      *            the VF Module to update
280      * @param widgetType
281      *            the type of Widget to create and add
282      * @throws XmlArtifactGenerationException
283      *             if the Widget mapping configuration is missing
284      */
285     private void assertAddWidget(Resource vfModule, Type widgetType) throws XmlArtifactGenerationException {
286         assertThat(createNewWidgetForModule(vfModule, widgetType), is(true));
287     }
288
289     /**
290      * Create a new Widget and assert that it cannot be added to the VF Module.
291      *
292      * @param vfModule
293      *            the VF Module
294      * @param widgetType
295      *            the type of Widget to create and attempt to add
296      * @throws XmlArtifactGenerationException
297      *             if the Widget mapping configuration is missing
298      */
299     private void assertFailToAddWidget(Resource vfModule, Type widgetType) throws XmlArtifactGenerationException {
300         assertThat(createNewWidgetForModule(vfModule, widgetType), is(false));
301     }
302
303     /**
304      * Create a new widget, make it a member of the VF Module, then try to add it.
305      *
306      * @param vfModule
307      *            the VF Module to update
308      * @param widgetType
309      *            the type of Widget to create and attempt to add
310      * @return whether or not the Widget was added to the module
311      * @throws XmlArtifactGenerationException
312      *             if the Widget mapping configuration is missing
313      */
314     private boolean createNewWidgetForModule(Resource vfModule, Type widgetType) throws XmlArtifactGenerationException {
315         Widget widget = createNewWidget(widgetType);
316         setWidgetAsMember(vfModule, widget);
317         return vfModule.addWidget(widget);
318     }
319
320     /**
321      * Make the specified Widget the sole member of the VF Module. This is achieved by first adding the Widget's own ID
322      * to its set of keys, and by then setting the VF Module's members to a Singleton List comprised of this ID. These
323      * updates allow the Widget to be successfully added to the VF Module. (Non-member Widgets cannot be added.)
324      *
325      * @param vfModule
326      *            the module for which members are overwritten
327      * @param widget
328      *            the widget to be set as the member
329      */
330     private void setWidgetAsMember(Resource vfModule, Widget widget) {
331         String id = widget.getId();
332         widget.addKey(id);
333         vfModule.setMembers(Collections.singletonList(id));
334     }
335
336     /**
337      * Create a vserver widget and add it to the specified VF Module.
338      *
339      * @param vfModule
340      *            the VF Module to update
341      * @return the number of Widgets present in the vserver on creation
342      * @throws XmlArtifactGenerationException
343      *             if the Widget mapping configuration is missing
344      */
345     private int createVserverForVf(Resource vfModule) throws XmlArtifactGenerationException {
346         Widget vserverWidget = createNewWidget(Type.VSERVER);
347         assertNumberOfWidgets(vfModule, 0);
348         final int initialWidgetCount = addVserverToVf(vfModule, vserverWidget);
349         assertNumberOfWidgets(vfModule, 1);
350         return initialWidgetCount;
351     }
352
353     /**
354      * Add the specified vserver to the specified VF Module.
355      * 
356      * @param vfModule
357      *            the VF Module to update
358      * @param vserverWidget
359      *            the Widget to add
360      * @return initial widget count for the vserver Widget
361      * @throws XmlArtifactGenerationException
362      *             if the Widget mapping configuration is missing
363      */
364     private int addVserverToVf(Resource vfModule, Widget vserverWidget) throws XmlArtifactGenerationException {
365         // A vserver (initially) has Flavor, Image, Tenant and Vfc.
366         final int initialWidgetCount = 4;
367         assertNumberOfWidgets(vserverWidget, initialWidgetCount);
368
369         // Add the vserver to the VF Module.
370         setWidgetAsMember(vfModule, vserverWidget);
371         assertThat(vfModule.addWidget(vserverWidget), is(true));
372
373         return initialWidgetCount;
374     }
375 }