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