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