Reimplement Widget.Type enum class
[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
38 /**
39  * Direct tests of the VFMODULE Resource and Widget functionality to improve code coverage.
40  */
41 public class TestVfModule {
42
43     /**
44      * Load the Widget Configuration, including the type mappings and the UUID mappings.
45      *
46      * @throws IOException
47      *             if the mappings configuration cannot be loaded
48      */
49     @BeforeClass
50     public static void setup() throws IOException {
51         ArtifactTestUtils util = new ArtifactTestUtils();
52         util.loadWidgetToUuidMappings();
53         util.loadWidgetMappings();
54     }
55
56     /**
57      * Call hashCode() method for code coverage.
58      */
59     @Test
60     public void testHashCode() {
61         Resource vfModule = createNewVfModule();
62         populateIdentInfo(vfModule);
63         assertThat(vfModule.hashCode(), is(notNullValue()));
64     }
65
66     /**
67      * Call equals() method for code coverage.
68      */
69     @Test
70     public void testEquals() {
71         Resource vfModuleA = createNewVfModule();
72         populateIdentInfo(vfModuleA);
73
74         // equals() is reflexive
75         assertThat(vfModuleA.equals(vfModuleA), is(true));
76
77         // equals() is symmetric
78         Resource vfModuleB = createNewVfModule();
79         populateIdentInfo(vfModuleB);
80         assertThat(vfModuleA.equals(vfModuleB), is(true));
81         assertThat(vfModuleB.equals(vfModuleA), is(true));
82
83         assertThat(vfModuleA.equals(null), is(false));
84     }
85
86     @Test
87     public void testAddVServerWidgetToVf() throws XmlArtifactGenerationException {
88         assertAddWidget(createNewVfModule(), WidgetType.valueOf("VSERVER"));
89     }
90
91     @Test
92     public void testAddServiceWidgetToVf() throws XmlArtifactGenerationException {
93         assertAddWidget(createNewVfModule(), WidgetType.valueOf("SERVICE"));
94     }
95
96     /**
97      * Add a new Widget to a VF Module, where the Widget is NOT set as a member. N.B. For the current VF Module
98      * implementation the actual Widget type is not important.
99      * 
100      * @throws XmlArtifactGenerationException
101      *             if the Widget mapping configuration is missing
102      */
103     @Test
104     public void testNonMemberWidgetToVf() throws XmlArtifactGenerationException {
105         Resource vfModule = createNewVfModule();
106         assertThat(vfModule.addWidget(createNewWidget(WidgetType.valueOf("SERVICE"))), is(false));
107         assertNumberOfWidgets(vfModule, 0);
108     }
109
110     /**
111      * OAM Network is specifically excluded from a VF Module.
112      * 
113      * @throws XmlArtifactGenerationException
114      *             if the Widget mapping configuration is missing
115      */
116     @Test
117     public void testAddOamNetworkWidgetToVf() throws XmlArtifactGenerationException {
118         Resource vfModule = createNewVfModule();
119         assertThat(createNewWidgetForModule(vfModule, WidgetType.valueOf("OAM_NETWORK")), is(false));
120         assertNumberOfWidgets(vfModule, 0);
121     }
122
123     /**
124      * Add a Volume Widget to a VF Module via a vserver Widget.
125      * 
126      * <li>Create a VF Module</li>
127      * <li>Add a Volume Widget</li>
128      * <li>Add a vserver Widget</li>
129      * <li>Check that the Volume Widget appears under the vserver</li>
130      * 
131      * @throws XmlArtifactGenerationException
132      *             if the Widget mapping configuration is missing
133      */
134     @Test
135     public void testAddVolumeWidgetToVf() throws XmlArtifactGenerationException {
136         Resource vfModule = createNewVfModule();
137
138         // Adding a Volume widget has no effect until a vserver widget is added.
139         assertAddWidget(vfModule, WidgetType.valueOf("VOLUME"));
140         assertNumberOfWidgets(vfModule, 0);
141
142         final int vserverBaseWidgetCount = createVserverForVf(vfModule);
143
144         // The vserver now has Volume as well.
145         assertNumberOfWidgets(vfModule.vserver, vserverBaseWidgetCount + 1);
146
147         // Adding another instance of a vserver widget fails.
148         assertFailToAddWidget(vfModule, WidgetType.valueOf("VSERVER"));
149         assertNumberOfWidgets(vfModule, 1);
150
151         // Adding another Volume widget is always treated as successful.
152         assertAddWidget(vfModule, WidgetType.valueOf("VOLUME"));
153         // Assert that no additional Widgets are actually present.
154         assertNumberOfWidgets(vfModule, 1);
155         assertNumberOfWidgets(vfModule.vserver, vserverBaseWidgetCount + 1);
156     }
157
158     /**
159      * Add an L-Interface Widget to a VF Module via a vserver Widget.
160      * 
161      * <li>Create a VF Module</li>
162      * <li>Add an L-Interface Widget</li>
163      * <li>Add a vserver Widget</li>
164      * <li>Check that the L-Interface Widget appears under the vserver</li>
165      * 
166      * @throws XmlArtifactGenerationException
167      *             if the Widget mapping configuration is missing
168      */
169     @Test
170     public void testAddLinterfaceWidgetToVf() throws XmlArtifactGenerationException {
171         Resource vfModule = createNewVfModule();
172
173         // Adding an L-Interface widget has no effect until a vserver widget is added.
174         assertFailToAddWidget(vfModule, WidgetType.valueOf("LINT"));
175         assertNumberOfWidgets(vfModule, 0);
176
177         final int vserverBaseWidgetCount = createVserverForVf(vfModule);
178
179         // The vserver now has an L-Interface as well.
180         assertNumberOfWidgets(vfModule.vserver, vserverBaseWidgetCount + 1);
181
182         // Adding another instance of a vserver widget fails.
183         assertFailToAddWidget(vfModule, WidgetType.valueOf("VSERVER"));
184         assertNumberOfWidgets(vfModule, 1);
185
186         // Adding an L-Interface widget is always treated as successful when a vserver exists.
187         assertAddWidget(vfModule, WidgetType.valueOf("LINT"));
188         // Assert that no additional Widgets are actually present.
189         assertNumberOfWidgets(vfModule, 1);
190         assertNumberOfWidgets(vfModule.vserver, vserverBaseWidgetCount + 1);
191     }
192
193     /**
194      * Add a Volume and an L-Interface Widget to a VF Module via a vserver Widget.
195      * 
196      * <li>Create a VF Module</li>
197      * <li>Add a Volume Widget</li>
198      * <li>Add an L-Interface Widget</li>
199      * <li>Add a vserver Widget</li>
200      * <li>Check that both Widgets appear under the vserver</li>
201      * 
202      * @throws XmlArtifactGenerationException
203      *             if the Widget mapping configuration is missing
204      */
205     @Test
206     public void testAddVolumeAndLinterfaceWidgetToVf() throws XmlArtifactGenerationException {
207         Resource vfModule = createNewVfModule();
208
209         // Adding a Volume widget has no effect until a vserver widget is added.
210         assertAddWidget(vfModule, WidgetType.valueOf("VOLUME"));
211         assertNumberOfWidgets(vfModule, 0);
212
213         // Adding an L-Interface widget has no effect until a vserver widget is added.
214         assertFailToAddWidget(vfModule, WidgetType.valueOf("LINT"));
215         assertNumberOfWidgets(vfModule, 0);
216
217         final int vserverBaseWidgetCount = createVserverForVf(vfModule);
218
219         // The vserver now has both Volume and L-Interface.
220         assertNumberOfWidgets(vfModule.vserver, vserverBaseWidgetCount + 2);
221
222         // Adding another instance of a vserver widget fails.
223         assertFailToAddWidget(vfModule, WidgetType.valueOf("VSERVER"));
224         assertNumberOfWidgets(vfModule, 1);
225
226         // Add new instances (with no effect).
227         assertAddWidget(vfModule, WidgetType.valueOf("VOLUME"));
228         assertAddWidget(vfModule, WidgetType.valueOf("LINT"));
229         // Assert that no additional Widgets are in fact present.
230         assertNumberOfWidgets(vfModule, 1);
231         assertNumberOfWidgets(vfModule.vserver, vserverBaseWidgetCount + 2);
232     }
233
234     private void assertNumberOfWidgets(Model model, int numberOfWidgets) {
235         assertThat(model.getWidgets(), hasSize(numberOfWidgets));
236     }
237
238     /**
239      * Use the static Factory method to create a new Widget.
240      *
241      * @param widgetType
242      *            type of Widget to create
243      * @return a new Widget
244      * @throws XmlArtifactGenerationException
245      *             if the Widget mapping configuration is missing
246      */
247     private Widget createNewWidget(WidgetType widgetType) throws XmlArtifactGenerationException {
248         return Widget.getWidget(widgetType);
249     }
250
251     /**
252      * Create a new VF Module that contains zero widgets and has no members.
253      *
254      * @return new VF Module resource
255      */
256     private Resource createNewVfModule() {
257         Resource vfModule = new Resource(WidgetType.valueOf("VFMODULE"), true);
258         assertNumberOfWidgets(vfModule, 0);
259         return vfModule;
260     }
261
262     /**
263      * Set up some dummy Model Identification properties.
264      *
265      * @param vfModule
266      *            to be populated
267      */
268     private void populateIdentInfo(Resource vfModule) {
269         Map<String, String> modelIdentInfo = new HashMap<>();
270         modelIdentInfo.put("UUID", "dummy_uuid");
271         vfModule.populateModelIdentificationInformation(modelIdentInfo);
272     }
273
274     /**
275      * Create a new Widget and assert that it is successfully added to the VF Module.
276      *
277      * @param vfModule
278      *            the VF Module to update
279      * @param widgetType
280      *            the type of Widget to create and add
281      * @throws XmlArtifactGenerationException
282      *             if the Widget mapping configuration is missing
283      */
284     private void assertAddWidget(Resource vfModule, WidgetType widgetType) throws XmlArtifactGenerationException {
285         assertThat(createNewWidgetForModule(vfModule, widgetType), is(true));
286     }
287
288     /**
289      * Create a new Widget and assert that it cannot be added to the VF Module.
290      *
291      * @param vfModule
292      *            the VF Module
293      * @param widgetType
294      *            the type of Widget to create and attempt to add
295      * @throws XmlArtifactGenerationException
296      *             if the Widget mapping configuration is missing
297      */
298     private void assertFailToAddWidget(Resource vfModule, WidgetType widgetType) throws XmlArtifactGenerationException {
299         assertThat(createNewWidgetForModule(vfModule, widgetType), is(false));
300     }
301
302     /**
303      * Create a new widget, make it a member of the VF Module, then try to add it.
304      *
305      * @param vfModule
306      *            the VF Module to update
307      * @param widgetType
308      *            the type of Widget to create and attempt to add
309      * @return whether or not the Widget was added to the module
310      * @throws XmlArtifactGenerationException
311      *             if the Widget mapping configuration is missing
312      */
313     private boolean createNewWidgetForModule(Resource vfModule, WidgetType widgetType)
314             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(WidgetType.valueOf("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 }