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