2 * ============LICENSE_START=======================================================
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
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.IOException;
30 import java.util.Collections;
31 import java.util.HashMap;
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.XmlArtifactGenerationException;
37 import org.onap.aai.babel.xml.generator.model.Widget.Type;
40 * Direct tests of the VfModule Model class to improve code coverage.
42 public class TestVfModule {
45 System.setProperty("APP_HOME", ".");
50 * if a properties file is not loaded
53 public static void setup() throws IOException {
54 ArtifactTestUtils util = new ArtifactTestUtils();
55 util.loadWidgetToUuidMappings();
56 util.loadWidgetMappings();
60 * Call hashCode() method for code coverage.
63 public void testHashCode() {
64 Resource vfModule = createNewVfModule();
65 populateIdentInfo(vfModule);
66 assertThat(vfModule.hashCode(), is(notNullValue()));
70 * Call equals() method for code coverage.
73 public void testEquals() {
74 Resource vfModuleA = createNewVfModule();
75 populateIdentInfo(vfModuleA);
77 // equals() is reflexive
78 assertThat(vfModuleA.equals(vfModuleA), is(true));
80 // equals() is symmetric
81 Resource vfModuleB = createNewVfModule();
82 populateIdentInfo(vfModuleB);
83 assertThat(vfModuleA.equals(vfModuleB), is(true));
84 assertThat(vfModuleB.equals(vfModuleA), is(true));
86 assertThat(vfModuleA.equals(null), is(false));
90 public void testAddVServerWidgetToVf() throws XmlArtifactGenerationException {
91 assertAddWidget(createNewVfModule(), Type.VSERVER);
95 public void testAddServiceWidgetToVf() throws XmlArtifactGenerationException {
96 assertAddWidget(createNewVfModule(), Type.SERVICE);
100 * Add a new Widget to a VF Module, where the Widget is NOT set as a member. N.B. For the current VF Module
101 * implementation the actual Widget type is not important.
102 * @throws XmlArtifactGenerationException
105 public void testNonMemberWidgetToVf() throws XmlArtifactGenerationException {
106 Resource vfModule = createNewVfModule();
107 assertThat(vfModule.addWidget(createNewWidget(Type.SERVICE)), is(false));
108 assertNumberOfWidgets(vfModule, 0);
112 * OAM Network is specifically excluded from a VF Module.
113 * @throws XmlArtifactGenerationException
116 public void testAddOamNetworkWidgetToVf() throws XmlArtifactGenerationException {
117 Resource vfModule = createNewVfModule();
118 assertThat(createNewWidgetForModule(vfModule, Type.OAM_NETWORK), is(false));
119 assertNumberOfWidgets(vfModule, 0);
123 * Add a Volume Widget to a VF Module via a vserver Widget.
125 * <li>Create a VF Module</li>
126 * <li>Add a Volume Widget</li>
127 * <li>Add a vserver Widget</li>
128 * <li>Check that the Volume Widget appears under the vserver</li>
129 * @throws XmlArtifactGenerationException
132 public void testAddVolumeWidgetToVf() throws XmlArtifactGenerationException {
133 Resource vfModule = createNewVfModule();
135 // Adding a Volume widget has no effect until a vserver widget is added.
136 assertAddWidget(vfModule, Type.VOLUME);
137 assertNumberOfWidgets(vfModule, 0);
139 final int vserverBaseWidgetCount = createVserverForVf(vfModule);
141 // The vserver now has Volume as well.
142 assertNumberOfWidgets(vfModule.vserver, vserverBaseWidgetCount + 1);
144 // Adding another instance of a vserver widget fails.
145 assertFailToAddWidget(vfModule, Type.VSERVER);
146 assertNumberOfWidgets(vfModule, 1);
148 // Adding another Volume widget is always treated as successful.
149 assertAddWidget(vfModule, Type.VOLUME);
150 // Assert that no additional Widgets are actually present.
151 assertNumberOfWidgets(vfModule, 1);
152 assertNumberOfWidgets(vfModule.vserver, vserverBaseWidgetCount + 1);
156 * Add an L-Interface Widget to a VF Module via a vserver Widget.
158 * <li>Create a VF Module</li>
159 * <li>Add an L-Interface Widget</li>
160 * <li>Add a vserver Widget</li>
161 * <li>Check that the L-Interface Widget appears under the vserver</li>
162 * @throws XmlArtifactGenerationException
165 public void testAddLinterfaceWidgetToVf() throws XmlArtifactGenerationException {
166 Resource vfModule = createNewVfModule();
168 // Adding an L-Interface widget has no effect until a vserver widget is added.
169 assertFailToAddWidget(vfModule, Type.LINT);
170 assertNumberOfWidgets(vfModule, 0);
172 final int vserverBaseWidgetCount = createVserverForVf(vfModule);
174 // The vserver now has an L-Interface as well.
175 assertNumberOfWidgets(vfModule.vserver, vserverBaseWidgetCount + 1);
177 // Adding another instance of a vserver widget fails.
178 assertFailToAddWidget(vfModule, Type.VSERVER);
179 assertNumberOfWidgets(vfModule, 1);
181 // Adding an L-Interface widget is always treated as successful when a vserver exists.
182 assertAddWidget(vfModule, Type.LINT);
183 // Assert that no additional Widgets are actually present.
184 assertNumberOfWidgets(vfModule, 1);
185 assertNumberOfWidgets(vfModule.vserver, vserverBaseWidgetCount + 1);
189 * Add a Volume and an L-Interface Widget to a VF Module via a vserver Widget.
191 * <li>Create a VF Module</li>
192 * <li>Add a Volume Widget</li>
193 * <li>Add an L-Interface Widget</li>
194 * <li>Add a vserver Widget</li>
195 * <li>Check that both Widgets appear under the vserver</li>
196 * @throws XmlArtifactGenerationException
199 public void testAddVolumeAndLinterfaceWidgetToVf() throws XmlArtifactGenerationException {
200 Resource vfModule = createNewVfModule();
202 // Adding a Volume widget has no effect until a vserver widget is added.
203 assertAddWidget(vfModule, Type.VOLUME);
204 assertNumberOfWidgets(vfModule, 0);
206 // Adding an L-Interface widget has no effect until a vserver widget is added.
207 assertFailToAddWidget(vfModule, Type.LINT);
208 assertNumberOfWidgets(vfModule, 0);
210 final int vserverBaseWidgetCount = createVserverForVf(vfModule);
212 // The vserver now has both Volume and L-Interface.
213 assertNumberOfWidgets(vfModule.vserver, vserverBaseWidgetCount + 2);
215 // Adding another instance of a vserver widget fails.
216 assertFailToAddWidget(vfModule, Type.VSERVER);
217 assertNumberOfWidgets(vfModule, 1);
219 // Add new instances (with no effect).
220 assertAddWidget(vfModule, Type.VOLUME);
221 assertAddWidget(vfModule, Type.LINT);
222 // Assert that no additional Widgets are in fact present.
223 assertNumberOfWidgets(vfModule, 1);
224 assertNumberOfWidgets(vfModule.vserver, vserverBaseWidgetCount + 2);
227 private void assertNumberOfWidgets(Model model, int numberOfWidgets) {
228 assertThat(model.getWidgets(), hasSize(numberOfWidgets));
232 * Use the static Factory method to create a new Widget.
235 * type of Widget to create
236 * @return a new Widget
237 * @throws XmlArtifactGenerationException
239 private Widget createNewWidget(Type widgetType) throws XmlArtifactGenerationException {
240 return Widget.getWidget(widgetType);
244 * Create a new VF Module that contains zero widgets and has no members.
246 * @return new VF Module resource
248 private Resource createNewVfModule() {
249 Resource vfModule = new Resource(Type.VFMODULE, true);
250 assertNumberOfWidgets(vfModule, 0);
255 * Set up some dummy Model Identification properties.
260 private void populateIdentInfo(Resource vfModule) {
261 Map<String, String> modelIdentInfo = new HashMap<>();
262 modelIdentInfo.put("UUID", "dummy_uuid");
263 vfModule.populateModelIdentificationInformation(modelIdentInfo);
267 * Create a new Widget and assert that it is successfully added to the VF Module.
270 * the VF Module to update
272 * the type of Widget to create and add
273 * @throws XmlArtifactGenerationException
275 private void assertAddWidget(Resource vfModule, Type widgetType) throws XmlArtifactGenerationException {
276 assertThat(createNewWidgetForModule(vfModule, widgetType), is(true));
280 * Create a new Widget and assert that it cannot be added to the VF Module.
285 * the type of Widget to create and attempt to add
286 * @throws XmlArtifactGenerationException
288 private void assertFailToAddWidget(Resource vfModule, Type widgetType) throws XmlArtifactGenerationException {
289 assertThat(createNewWidgetForModule(vfModule, widgetType), is(false));
293 * Create a new widget, make it a member of the VF Module, then try to add it.
296 * the VF Module to update
298 * the type of Widget to create and attempt to add
299 * @return whether or not the Widget was added to the module
300 * @throws XmlArtifactGenerationException
302 private boolean createNewWidgetForModule(Resource vfModule, Type widgetType) throws XmlArtifactGenerationException {
303 Widget widget = createNewWidget(widgetType);
304 setWidgetAsMember(vfModule, widget);
305 return vfModule.addWidget(widget);
309 * Make the specified Widget the sole member of the VF Module. This is achieved by first adding the Widget's own ID
310 * to its set of keys, and by then setting the VF Module's members to a Singleton List comprised of this ID. These
311 * updates allow the Widget to be successfully added to the VF Module. (Non-member Widgets cannot be added.)
314 * the module for which members are overwritten
316 * the widget to be set as the member
318 private void setWidgetAsMember(Resource vfModule, Widget widget) {
319 String id = widget.getId();
321 vfModule.setMembers(Collections.singletonList(id));
325 * Create a vserver widget and add it to the specified VF Module.
328 * the VF Module to update
329 * @return the number of Widgets present in the vserver on creation
330 * @throws XmlArtifactGenerationException
332 private int createVserverForVf(Resource vfModule) throws XmlArtifactGenerationException {
333 Widget vserverWidget = createNewWidget(Type.VSERVER);
334 assertNumberOfWidgets(vfModule, 0);
335 final int initialWidgetCount = addVserverToVf(vfModule, vserverWidget);
336 assertNumberOfWidgets(vfModule, 1);
337 return initialWidgetCount;
341 * Add the specified vserver to the specified VF Module.
344 * the VF Module to update
345 * @param vserverWidget
347 * @return initial widget count for the vserver Widget
348 * @throws XmlArtifactGenerationException
350 private int addVserverToVf(Resource vfModule, Widget vserverWidget) throws XmlArtifactGenerationException {
351 // A vserver (initially) has Flavor, Image, Tenant and Vfc.
352 final int initialWidgetCount = 4;
353 assertNumberOfWidgets(vserverWidget, initialWidgetCount);
355 // Add the vserver to the VF Module.
356 setWidgetAsMember(vfModule, vserverWidget);
357 assertThat(vfModule.addWidget(vserverWidget), is(true));
359 return initialWidgetCount;