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;
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;
43 * Direct tests of the VfModule Model class to improve code coverage.
45 public class TestVfModule {
48 System.setProperty("APP_HOME", ".");
52 * Load the Widget to UUID mappings from the Artifact Generator properties.
54 * @throws FileNotFoundException if the properties file is missing
55 * @throws IOException if the properties file is not loaded
58 public static void setup() throws FileNotFoundException, IOException {
59 InputStream in = TestVfModule.class.getClassLoader().getResourceAsStream("artifact-generator.properties");
60 Properties properties = new Properties();
63 WidgetConfigurationUtil.setConfig(properties);
67 * Call equals() and hashCode() methods for code coverage.
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
81 public void testAddVServerWidgetToVf() {
82 assertAddWidget(createNewVfModule(), Type.VSERVER);
86 public void testAddServiceWidgetToVf() {
87 assertAddWidget(createNewVfModule(), Type.SERVICE);
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.
95 public void testNonMemberWidgetToVf() {
96 VfModule vfModule = createNewVfModule();
97 assertThat(vfModule.addWidget(createNewWidget(Type.SERVICE)), is(false));
98 assertNumberOfWidgets(vfModule, 0);
102 * OAM Network is specifically excluded from a VF Module.
105 public void testAddOamNetworkWidgetToVf() {
106 VfModule vfModule = createNewVfModule();
107 assertThat(createNewWidgetForModule(vfModule, Type.OAM_NETWORK), is(false));
108 assertNumberOfWidgets(vfModule, 0);
112 * Add a Volume Widget to a VF Module via a vserver Widget.
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>
120 public void testAddVolumeWidgetToVf() {
121 VfModule vfModule = createNewVfModule();
123 // Adding a Volume widget has no effect until a vserver widget is added.
124 assertAddWidget(vfModule, Type.VOLUME);
125 assertNumberOfWidgets(vfModule, 0);
127 final int vserverBaseWidgetCount = createVserverForVf(vfModule);
129 // The vserver now has Volume as well.
130 assertNumberOfWidgets(vfModule.vserver, vserverBaseWidgetCount + 1);
132 // Adding another instance of a vserver widget fails.
133 assertFailToAddWidget(vfModule, Type.VSERVER);
134 assertNumberOfWidgets(vfModule, 1);
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);
144 * Add an L-Interface Widget to a VF Module via a vserver Widget.
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>
152 public void testAddLinterfaceWidgetToVf() {
153 VfModule vfModule = createNewVfModule();
155 // Adding an L-Interface widget has no effect until a vserver widget is added.
156 assertFailToAddWidget(vfModule, Type.LINT);
157 assertNumberOfWidgets(vfModule, 0);
159 final int vserverBaseWidgetCount = createVserverForVf(vfModule);
161 // The vserver now has an L-Interface as well.
162 assertNumberOfWidgets(vfModule.vserver, vserverBaseWidgetCount + 1);
164 // Adding another instance of a vserver widget fails.
165 assertFailToAddWidget(vfModule, Type.VSERVER);
166 assertNumberOfWidgets(vfModule, 1);
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);
176 * Add a Volume and an L-Interface Widget to a VF Module via a vserver Widget.
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>
185 public void testAddVolumeAndLinterfaceWidgetToVf() {
186 VfModule vfModule = createNewVfModule();
188 // Adding a Volume widget has no effect until a vserver widget is added.
189 assertAddWidget(vfModule, Type.VOLUME);
190 assertNumberOfWidgets(vfModule, 0);
192 // Adding an L-Interface widget has no effect until a vserver widget is added.
193 assertFailToAddWidget(vfModule, Type.LINT);
194 assertNumberOfWidgets(vfModule, 0);
196 final int vserverBaseWidgetCount = createVserverForVf(vfModule);
198 // The vserver now has both Volume and L-Interface.
199 assertNumberOfWidgets(vfModule.vserver, vserverBaseWidgetCount + 2);
201 // Adding another instance of a vserver widget fails.
202 assertFailToAddWidget(vfModule, Type.VSERVER);
203 assertNumberOfWidgets(vfModule, 1);
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);
213 private void assertNumberOfWidgets(Model model, int numberOfWidgets) {
214 assertThat(model.getWidgets(), hasSize(numberOfWidgets));
218 * Use the static Factory method to create a new Widget.
220 * @param widgetType type of Widget to create
221 * @return a new Widget
223 private Widget createNewWidget(Type widgetType) {
224 return Widget.getWidget(widgetType);
228 * Create a new VF Module that contains zero widgets and has no members.
230 * @return new VF Module resource
232 private VfModule createNewVfModule() {
233 VfModule vfModule = new VfModule();
234 assertNumberOfWidgets(vfModule, 0);
239 * Set up some dummy Model Identification properties.
241 * @param vfModule to be populated
243 private void populateIdentInfo(VfModule vfModule) {
244 Map<String, String> modelIdentInfo = new HashMap<>();
245 modelIdentInfo.put("UUID", "dummy_uuid");
246 vfModule.populateModelIdentificationInformation(modelIdentInfo);
250 * Create a new Widget and assert that it is successfully added to the VF Module.
252 * @param vfModule the VF Module to update
253 * @param widgetType the type of Widget to create and add
255 private void assertAddWidget(VfModule vfModule, Type widgetType) {
256 assertThat(createNewWidgetForModule(vfModule, widgetType), is(true));
260 * Create a new Widget and assert that it cannot be added to the VF Module.
262 * @param vfModule the VF Module
263 * @param widgetType the type of Widget to create and attempt to add
265 private void assertFailToAddWidget(VfModule vfModule, Type widgetType) {
266 assertThat(createNewWidgetForModule(vfModule, widgetType), is(false));
270 * Create a new widget, make it a member of the VF Module, then try to add it.
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
276 private boolean createNewWidgetForModule(VfModule vfModule, Type widgetType) {
277 Widget widget = createNewWidget(widgetType);
278 setWidgetAsMember(vfModule, widget);
279 return vfModule.addWidget(widget);
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.)
287 * @param vfModule the module for which members are overwritten
288 * @param widget the widget to be set as the member
290 private void setWidgetAsMember(VfModule vfModule, Widget widget) {
291 String id = widget.getId();
293 vfModule.setMembers(Collections.singletonList(id));
297 * Create a vserver widget and add it to the specified VF Module.
299 * @param vfModule the VF Module to update
300 * @return the number of Widgets present in the vserver on creation
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;
311 * Add the specified vserver to the specified VF Module.
313 * @param vfModule the VF Module to update
314 * @param vserverWidget the Widget to add
315 * @return initial widget count for the vserver Widget
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);
322 // Add the vserver to the VF Module.
323 setWidgetAsMember(vfModule, vserverWidget);
324 assertThat(vfModule.addWidget(vserverWidget), is(true));
326 return initialWidgetCount;