2 * ============LICENSE_START=======================================================
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
12 * http://www.apache.org/licenses/LICENSE-2.0
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=========================================================
22 package org.onap.aai.babel.xml.generator.model;
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;
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;
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;
42 * Direct tests of the VfModule Model class to improve code coverage.
44 public class TestVfModule {
47 System.setProperty("APP_HOME", ".");
51 * Load the Widget to UUID mappings from the Artifact Generator properties.
53 * @throws FileNotFoundException if the properties file is missing
54 * @throws IOException if the properties file is not loaded
57 public static void setup() throws FileNotFoundException, IOException {
58 InputStream in = TestVfModule.class.getClassLoader().getResourceAsStream("artifact-generator.properties");
59 Properties properties = new Properties();
62 WidgetConfigurationUtil.setConfig(properties);
66 * Call equals() and hashCode() methods for code coverage.
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
80 public void testAddVServerWidgetToVf() {
81 assertAddWidget(createNewVfModule(), Type.VSERVER);
85 public void testAddServiceWidgetToVf() {
86 assertAddWidget(createNewVfModule(), Type.SERVICE);
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.
94 public void testNonMemberWidgetToVf() {
95 VfModule vfModule = createNewVfModule();
96 assertThat(vfModule.addWidget(createNewWidget(Type.SERVICE)), is(false));
97 assertNumberOfWidgets(vfModule, 0);
101 * OAM Network is specifically excluded from a VF Module.
104 public void testAddOamNetworkWidgetToVf() {
105 VfModule vfModule = createNewVfModule();
106 assertThat(createNewWidgetForModule(vfModule, Type.OAM_NETWORK), is(false));
107 assertNumberOfWidgets(vfModule, 0);
111 * Add a Volume Widget to a VF Module via a vserver Widget.
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>
119 public void testAddVolumeWidgetToVf() {
120 VfModule vfModule = createNewVfModule();
122 // Adding a Volume widget has no effect until a vserver widget is added.
123 assertAddWidget(vfModule, Type.VOLUME);
124 assertNumberOfWidgets(vfModule, 0);
126 final int vserverBaseWidgetCount = createVserverForVf(vfModule);
128 // The vserver now has Volume as well.
129 assertNumberOfWidgets(vfModule.vserver, vserverBaseWidgetCount + 1);
131 // Adding another instance of a vserver widget fails.
132 assertFailToAddWidget(vfModule, Type.VSERVER);
133 assertNumberOfWidgets(vfModule, 1);
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);
143 * Add an L-Interface Widget to a VF Module via a vserver Widget.
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>
151 public void testAddLinterfaceWidgetToVf() {
152 VfModule vfModule = createNewVfModule();
154 // Adding an L-Interface widget has no effect until a vserver widget is added.
155 assertFailToAddWidget(vfModule, Type.LINT);
156 assertNumberOfWidgets(vfModule, 0);
158 final int vserverBaseWidgetCount = createVserverForVf(vfModule);
160 // The vserver now has an L-Interface as well.
161 assertNumberOfWidgets(vfModule.vserver, vserverBaseWidgetCount + 1);
163 // Adding another instance of a vserver widget fails.
164 assertFailToAddWidget(vfModule, Type.VSERVER);
165 assertNumberOfWidgets(vfModule, 1);
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);
175 * Add a Volume and an L-Interface Widget to a VF Module via a vserver Widget.
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>
184 public void testAddVolumeAndLinterfaceWidgetToVf() {
185 VfModule vfModule = createNewVfModule();
187 // Adding a Volume widget has no effect until a vserver widget is added.
188 assertAddWidget(vfModule, Type.VOLUME);
189 assertNumberOfWidgets(vfModule, 0);
191 // Adding an L-Interface widget has no effect until a vserver widget is added.
192 assertFailToAddWidget(vfModule, Type.LINT);
193 assertNumberOfWidgets(vfModule, 0);
195 final int vserverBaseWidgetCount = createVserverForVf(vfModule);
197 // The vserver now has both Volume and L-Interface.
198 assertNumberOfWidgets(vfModule.vserver, vserverBaseWidgetCount + 2);
200 // Adding another instance of a vserver widget fails.
201 assertFailToAddWidget(vfModule, Type.VSERVER);
202 assertNumberOfWidgets(vfModule, 1);
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);
212 private void assertNumberOfWidgets(Model model, int numberOfWidgets) {
213 assertThat(model.getWidgets(), hasSize(numberOfWidgets));
217 * Use the static Factory method to create a new Widget.
219 * @param widgetType type of Widget to create
220 * @return a new Widget
222 private Widget createNewWidget(Type widgetType) {
223 return Widget.getWidget(widgetType);
227 * Create a new VF Module that contains zero widgets and has no members.
229 * @return new VF Module resource
231 private VfModule createNewVfModule() {
232 VfModule vfModule = new VfModule();
233 assertNumberOfWidgets(vfModule, 0);
238 * Set up some dummy Model Identification properties.
240 * @param vfModule to be populated
242 private void populateIdentInfo(VfModule vfModule) {
243 Map<String, String> modelIdentInfo = new HashMap<>();
244 modelIdentInfo.put("UUID", "dummy_uuid");
245 vfModule.populateModelIdentificationInformation(modelIdentInfo);
249 * Create a new Widget and assert that it is successfully added to the VF Module.
251 * @param vfModule the VF Module to update
252 * @param widgetType the type of Widget to create and add
254 private void assertAddWidget(VfModule vfModule, Type widgetType) {
255 assertThat(createNewWidgetForModule(vfModule, widgetType), is(true));
259 * Create a new Widget and assert that it cannot be added to the VF Module.
261 * @param vfModule the VF Module
262 * @param widgetType the type of Widget to create and attempt to add
264 private void assertFailToAddWidget(VfModule vfModule, Type widgetType) {
265 assertThat(createNewWidgetForModule(vfModule, widgetType), is(false));
269 * Create a new widget, make it a member of the VF Module, then try to add it.
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
275 private boolean createNewWidgetForModule(VfModule vfModule, Type widgetType) {
276 Widget widget = createNewWidget(widgetType);
277 setWidgetAsMember(vfModule, widget);
278 return vfModule.addWidget(widget);
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.)
286 * @param vfModule the module for which members are overwritten
287 * @param widget the widget to be set as the member
289 private void setWidgetAsMember(VfModule vfModule, Widget widget) {
290 String id = widget.getId();
292 vfModule.setMembers(Collections.singletonList(id));
296 * Create a vserver widget and add it to the specified VF Module.
298 * @param vfModule the VF Module to update
299 * @return the number of Widgets present in the vserver on creation
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;
310 * Add the specified vserver to the specified VF Module.
312 * @param vfModule the VF Module to update
313 * @param vserverWidget the Widget to add
314 * @return initial widget count for the vserver Widget
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);
321 // Add the vserver to the VF Module.
322 setWidgetAsMember(vfModule, vserverWidget);
323 assertThat(vfModule.addWidget(vserverWidget), is(true));
325 return initialWidgetCount;