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