2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (c) 2017-2019 AT&T Intellectual Property. All rights reserved.
6 * Copyright (c) 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 Resource and Widget functionality to improve code coverage.
42 public class TestVfModule {
45 * Load the Widget Configuration, including the type mappings and the UUID mappings.
48 * if the mappings configuration cannot be loaded
51 public static void setup() throws IOException {
52 ArtifactTestUtils util = new ArtifactTestUtils();
53 util.loadWidgetToUuidMappings();
54 util.loadWidgetMappings();
58 * Call hashCode() method for code coverage.
61 public void testHashCode() {
62 Resource vfModule = createNewVfModule();
63 populateIdentInfo(vfModule);
64 assertThat(vfModule.hashCode(), is(notNullValue()));
68 * Call equals() method for code coverage.
71 public void testEquals() {
72 Resource vfModuleA = createNewVfModule();
73 populateIdentInfo(vfModuleA);
75 // equals() is reflexive
76 assertThat(vfModuleA.equals(vfModuleA), is(true));
78 // equals() is symmetric
79 Resource vfModuleB = createNewVfModule();
80 populateIdentInfo(vfModuleB);
81 assertThat(vfModuleA.equals(vfModuleB), is(true));
82 assertThat(vfModuleB.equals(vfModuleA), is(true));
84 assertThat(vfModuleA.equals(null), is(false));
88 public void testAddVServerWidgetToVf() throws XmlArtifactGenerationException {
89 assertAddWidget(createNewVfModule(), Type.VSERVER);
93 public void testAddServiceWidgetToVf() throws XmlArtifactGenerationException {
94 assertAddWidget(createNewVfModule(), Type.SERVICE);
98 * Add a new Widget to a VF Module, where the Widget is NOT set as a member. N.B. For the current VF Module
99 * implementation the actual Widget type is not important.
101 * @throws XmlArtifactGenerationException
102 * if the Widget mapping configuration is missing
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.
114 * @throws XmlArtifactGenerationException
115 * if the Widget mapping configuration is missing
118 public void testAddOamNetworkWidgetToVf() throws XmlArtifactGenerationException {
119 Resource vfModule = createNewVfModule();
120 assertThat(createNewWidgetForModule(vfModule, Type.OAM_NETWORK), is(false));
121 assertNumberOfWidgets(vfModule, 0);
125 * Add a Volume Widget to a VF Module via a vserver Widget.
127 * <li>Create a VF Module</li>
128 * <li>Add a Volume Widget</li>
129 * <li>Add a vserver Widget</li>
130 * <li>Check that the Volume Widget appears under the vserver</li>
132 * @throws XmlArtifactGenerationException
133 * if the Widget mapping configuration is missing
136 public void testAddVolumeWidgetToVf() throws XmlArtifactGenerationException {
137 Resource vfModule = createNewVfModule();
139 // Adding a Volume widget has no effect until a vserver widget is added.
140 assertAddWidget(vfModule, Type.VOLUME);
141 assertNumberOfWidgets(vfModule, 0);
143 final int vserverBaseWidgetCount = createVserverForVf(vfModule);
145 // The vserver now has Volume as well.
146 assertNumberOfWidgets(vfModule.vserver, vserverBaseWidgetCount + 1);
148 // Adding another instance of a vserver widget fails.
149 assertFailToAddWidget(vfModule, Type.VSERVER);
150 assertNumberOfWidgets(vfModule, 1);
152 // Adding another Volume widget is always treated as successful.
153 assertAddWidget(vfModule, Type.VOLUME);
154 // Assert that no additional Widgets are actually present.
155 assertNumberOfWidgets(vfModule, 1);
156 assertNumberOfWidgets(vfModule.vserver, vserverBaseWidgetCount + 1);
160 * Add an L-Interface Widget to a VF Module via a vserver Widget.
162 * <li>Create a VF Module</li>
163 * <li>Add an L-Interface Widget</li>
164 * <li>Add a vserver Widget</li>
165 * <li>Check that the L-Interface Widget appears under the vserver</li>
167 * @throws XmlArtifactGenerationException
168 * if the Widget mapping configuration is missing
171 public void testAddLinterfaceWidgetToVf() throws XmlArtifactGenerationException {
172 Resource vfModule = createNewVfModule();
174 // Adding an L-Interface widget has no effect until a vserver widget is added.
175 assertFailToAddWidget(vfModule, Type.LINT);
176 assertNumberOfWidgets(vfModule, 0);
178 final int vserverBaseWidgetCount = createVserverForVf(vfModule);
180 // The vserver now has an L-Interface as well.
181 assertNumberOfWidgets(vfModule.vserver, vserverBaseWidgetCount + 1);
183 // Adding another instance of a vserver widget fails.
184 assertFailToAddWidget(vfModule, Type.VSERVER);
185 assertNumberOfWidgets(vfModule, 1);
187 // Adding an L-Interface widget is always treated as successful when a vserver exists.
188 assertAddWidget(vfModule, Type.LINT);
189 // Assert that no additional Widgets are actually present.
190 assertNumberOfWidgets(vfModule, 1);
191 assertNumberOfWidgets(vfModule.vserver, vserverBaseWidgetCount + 1);
195 * Add a Volume and an L-Interface Widget to a VF Module via a vserver Widget.
197 * <li>Create a VF Module</li>
198 * <li>Add a Volume Widget</li>
199 * <li>Add an L-Interface Widget</li>
200 * <li>Add a vserver Widget</li>
201 * <li>Check that both Widgets appear under the vserver</li>
203 * @throws XmlArtifactGenerationException
204 * if the Widget mapping configuration is missing
207 public void testAddVolumeAndLinterfaceWidgetToVf() throws XmlArtifactGenerationException {
208 Resource vfModule = createNewVfModule();
210 // Adding a Volume widget has no effect until a vserver widget is added.
211 assertAddWidget(vfModule, Type.VOLUME);
212 assertNumberOfWidgets(vfModule, 0);
214 // Adding an L-Interface widget has no effect until a vserver widget is added.
215 assertFailToAddWidget(vfModule, Type.LINT);
216 assertNumberOfWidgets(vfModule, 0);
218 final int vserverBaseWidgetCount = createVserverForVf(vfModule);
220 // The vserver now has both Volume and L-Interface.
221 assertNumberOfWidgets(vfModule.vserver, vserverBaseWidgetCount + 2);
223 // Adding another instance of a vserver widget fails.
224 assertFailToAddWidget(vfModule, Type.VSERVER);
225 assertNumberOfWidgets(vfModule, 1);
227 // Add new instances (with no effect).
228 assertAddWidget(vfModule, Type.VOLUME);
229 assertAddWidget(vfModule, Type.LINT);
230 // Assert that no additional Widgets are in fact present.
231 assertNumberOfWidgets(vfModule, 1);
232 assertNumberOfWidgets(vfModule.vserver, vserverBaseWidgetCount + 2);
235 private void assertNumberOfWidgets(Model model, int numberOfWidgets) {
236 assertThat(model.getWidgets(), hasSize(numberOfWidgets));
240 * Use the static Factory method to create a new Widget.
243 * type of Widget to create
244 * @return a new Widget
245 * @throws XmlArtifactGenerationException
246 * if the Widget mapping configuration is missing
248 private Widget createNewWidget(Type widgetType) throws XmlArtifactGenerationException {
249 return Widget.getWidget(widgetType);
253 * Create a new VF Module that contains zero widgets and has no members.
255 * @return new VF Module resource
257 private Resource createNewVfModule() {
258 Resource vfModule = new Resource(Type.VFMODULE, true);
259 assertNumberOfWidgets(vfModule, 0);
264 * Set up some dummy Model Identification properties.
269 private void populateIdentInfo(Resource vfModule) {
270 Map<String, String> modelIdentInfo = new HashMap<>();
271 modelIdentInfo.put("UUID", "dummy_uuid");
272 vfModule.populateModelIdentificationInformation(modelIdentInfo);
276 * Create a new Widget and assert that it is successfully added to the VF Module.
279 * the VF Module to update
281 * the type of Widget to create and add
282 * @throws XmlArtifactGenerationException
283 * if the Widget mapping configuration is missing
285 private void assertAddWidget(Resource vfModule, Type widgetType) throws XmlArtifactGenerationException {
286 assertThat(createNewWidgetForModule(vfModule, widgetType), is(true));
290 * Create a new Widget and assert that it cannot be added to the VF Module.
295 * the type of Widget to create and attempt to add
296 * @throws XmlArtifactGenerationException
297 * if the Widget mapping configuration is missing
299 private void assertFailToAddWidget(Resource vfModule, Type widgetType) throws XmlArtifactGenerationException {
300 assertThat(createNewWidgetForModule(vfModule, widgetType), is(false));
304 * Create a new widget, make it a member of the VF Module, then try to add it.
307 * the VF Module to update
309 * the type of Widget to create and attempt to add
310 * @return whether or not the Widget was added to the module
311 * @throws XmlArtifactGenerationException
312 * if the Widget mapping configuration is missing
314 private boolean createNewWidgetForModule(Resource vfModule, Type widgetType) throws XmlArtifactGenerationException {
315 Widget widget = createNewWidget(widgetType);
316 setWidgetAsMember(vfModule, widget);
317 return vfModule.addWidget(widget);
321 * Make the specified Widget the sole member of the VF Module. This is achieved by first adding the Widget's own ID
322 * to its set of keys, and by then setting the VF Module's members to a Singleton List comprised of this ID. These
323 * updates allow the Widget to be successfully added to the VF Module. (Non-member Widgets cannot be added.)
326 * the module for which members are overwritten
328 * the widget to be set as the member
330 private void setWidgetAsMember(Resource vfModule, Widget widget) {
331 String id = widget.getId();
333 vfModule.setMembers(Collections.singletonList(id));
337 * Create a vserver widget and add it to the specified VF Module.
340 * the VF Module to update
341 * @return the number of Widgets present in the vserver on creation
342 * @throws XmlArtifactGenerationException
343 * if the Widget mapping configuration is missing
345 private int createVserverForVf(Resource vfModule) throws XmlArtifactGenerationException {
346 Widget vserverWidget = createNewWidget(Type.VSERVER);
347 assertNumberOfWidgets(vfModule, 0);
348 final int initialWidgetCount = addVserverToVf(vfModule, vserverWidget);
349 assertNumberOfWidgets(vfModule, 1);
350 return initialWidgetCount;
354 * Add the specified vserver to the specified VF Module.
357 * the VF Module to update
358 * @param vserverWidget
360 * @return initial widget count for the vserver Widget
361 * @throws XmlArtifactGenerationException
362 * if the Widget mapping configuration is missing
364 private int addVserverToVf(Resource vfModule, Widget vserverWidget) throws XmlArtifactGenerationException {
365 // A vserver (initially) has Flavor, Image, Tenant and Vfc.
366 final int initialWidgetCount = 4;
367 assertNumberOfWidgets(vserverWidget, initialWidgetCount);
369 // Add the vserver to the VF Module.
370 setWidgetAsMember(vfModule, vserverWidget);
371 assertThat(vfModule.addWidget(vserverWidget), is(true));
373 return initialWidgetCount;