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