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