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", ".");
49 * Initialize the Widget mappings.
52 * if a properties file is not loaded
55 public static void setup() throws IOException {
56 ArtifactTestUtils util = new ArtifactTestUtils();
57 util.loadWidgetToUuidMappings();
58 util.loadWidgetMappings();
62 * Call hashCode() method for code coverage.
65 public void testHashCode() {
66 Resource vfModule = createNewVfModule();
67 populateIdentInfo(vfModule);
68 assertThat(vfModule.hashCode(), is(notNullValue()));
72 * Call equals() method for code coverage.
75 public void testEquals() {
76 Resource vfModuleA = createNewVfModule();
77 populateIdentInfo(vfModuleA);
79 // equals() is reflexive
80 assertThat(vfModuleA.equals(vfModuleA), is(true));
82 // equals() is symmetric
83 Resource vfModuleB = createNewVfModule();
84 populateIdentInfo(vfModuleB);
85 assertThat(vfModuleA.equals(vfModuleB), is(true));
86 assertThat(vfModuleB.equals(vfModuleA), is(true));
88 assertThat(vfModuleA.equals(null), is(false));
92 public void testAddVServerWidgetToVf() throws XmlArtifactGenerationException {
93 assertAddWidget(createNewVfModule(), Type.VSERVER);
97 public void testAddServiceWidgetToVf() throws XmlArtifactGenerationException {
98 assertAddWidget(createNewVfModule(), Type.SERVICE);
102 * Add a new Widget to a VF Module, where the Widget is NOT set as a member. N.B. For the current VF Module
103 * implementation the actual Widget type is not important.
105 * @throws XmlArtifactGenerationException
106 * if the Widget mapping configuration is missing
109 public void testNonMemberWidgetToVf() throws XmlArtifactGenerationException {
110 Resource vfModule = createNewVfModule();
111 assertThat(vfModule.addWidget(createNewWidget(Type.SERVICE)), is(false));
112 assertNumberOfWidgets(vfModule, 0);
116 * OAM Network is specifically excluded from a VF Module.
118 * @throws XmlArtifactGenerationException
119 * if the Widget mapping configuration is missing
122 public void testAddOamNetworkWidgetToVf() throws XmlArtifactGenerationException {
123 Resource vfModule = createNewVfModule();
124 assertThat(createNewWidgetForModule(vfModule, Type.OAM_NETWORK), is(false));
125 assertNumberOfWidgets(vfModule, 0);
129 * Add a Volume Widget to a VF Module via a vserver Widget.
131 * <li>Create a VF Module</li>
132 * <li>Add a Volume Widget</li>
133 * <li>Add a vserver Widget</li>
134 * <li>Check that the Volume Widget appears under the vserver</li>
136 * @throws XmlArtifactGenerationException
137 * if the Widget mapping configuration is missing
140 public void testAddVolumeWidgetToVf() throws XmlArtifactGenerationException {
141 Resource vfModule = createNewVfModule();
143 // Adding a Volume widget has no effect until a vserver widget is added.
144 assertAddWidget(vfModule, Type.VOLUME);
145 assertNumberOfWidgets(vfModule, 0);
147 final int vserverBaseWidgetCount = createVserverForVf(vfModule);
149 // The vserver now has Volume as well.
150 assertNumberOfWidgets(vfModule.vserver, vserverBaseWidgetCount + 1);
152 // Adding another instance of a vserver widget fails.
153 assertFailToAddWidget(vfModule, Type.VSERVER);
154 assertNumberOfWidgets(vfModule, 1);
156 // Adding another Volume widget is always treated as successful.
157 assertAddWidget(vfModule, Type.VOLUME);
158 // Assert that no additional Widgets are actually present.
159 assertNumberOfWidgets(vfModule, 1);
160 assertNumberOfWidgets(vfModule.vserver, vserverBaseWidgetCount + 1);
164 * Add an L-Interface Widget to a VF Module via a vserver Widget.
166 * <li>Create a VF Module</li>
167 * <li>Add an L-Interface Widget</li>
168 * <li>Add a vserver Widget</li>
169 * <li>Check that the L-Interface Widget appears under the vserver</li>
171 * @throws XmlArtifactGenerationException
172 * if the Widget mapping configuration is missing
175 public void testAddLinterfaceWidgetToVf() throws XmlArtifactGenerationException {
176 Resource vfModule = createNewVfModule();
178 // Adding an L-Interface widget has no effect until a vserver widget is added.
179 assertFailToAddWidget(vfModule, Type.LINT);
180 assertNumberOfWidgets(vfModule, 0);
182 final int vserverBaseWidgetCount = createVserverForVf(vfModule);
184 // The vserver now has an L-Interface as well.
185 assertNumberOfWidgets(vfModule.vserver, vserverBaseWidgetCount + 1);
187 // Adding another instance of a vserver widget fails.
188 assertFailToAddWidget(vfModule, Type.VSERVER);
189 assertNumberOfWidgets(vfModule, 1);
191 // Adding an L-Interface widget is always treated as successful when a vserver exists.
192 assertAddWidget(vfModule, Type.LINT);
193 // Assert that no additional Widgets are actually present.
194 assertNumberOfWidgets(vfModule, 1);
195 assertNumberOfWidgets(vfModule.vserver, vserverBaseWidgetCount + 1);
199 * Add a Volume and an L-Interface Widget to a VF Module via a vserver Widget.
201 * <li>Create a VF Module</li>
202 * <li>Add a Volume Widget</li>
203 * <li>Add an L-Interface Widget</li>
204 * <li>Add a vserver Widget</li>
205 * <li>Check that both Widgets appear under the vserver</li>
207 * @throws XmlArtifactGenerationException
208 * if the Widget mapping configuration is missing
211 public void testAddVolumeAndLinterfaceWidgetToVf() throws XmlArtifactGenerationException {
212 Resource vfModule = createNewVfModule();
214 // Adding a Volume widget has no effect until a vserver widget is added.
215 assertAddWidget(vfModule, Type.VOLUME);
216 assertNumberOfWidgets(vfModule, 0);
218 // Adding an L-Interface widget has no effect until a vserver widget is added.
219 assertFailToAddWidget(vfModule, Type.LINT);
220 assertNumberOfWidgets(vfModule, 0);
222 final int vserverBaseWidgetCount = createVserverForVf(vfModule);
224 // The vserver now has both Volume and L-Interface.
225 assertNumberOfWidgets(vfModule.vserver, vserverBaseWidgetCount + 2);
227 // Adding another instance of a vserver widget fails.
228 assertFailToAddWidget(vfModule, Type.VSERVER);
229 assertNumberOfWidgets(vfModule, 1);
231 // Add new instances (with no effect).
232 assertAddWidget(vfModule, Type.VOLUME);
233 assertAddWidget(vfModule, Type.LINT);
234 // Assert that no additional Widgets are in fact present.
235 assertNumberOfWidgets(vfModule, 1);
236 assertNumberOfWidgets(vfModule.vserver, vserverBaseWidgetCount + 2);
239 private void assertNumberOfWidgets(Model model, int numberOfWidgets) {
240 assertThat(model.getWidgets(), hasSize(numberOfWidgets));
244 * Use the static Factory method to create a new Widget.
247 * type of Widget to create
248 * @return a new Widget
249 * @throws XmlArtifactGenerationException
250 * if the Widget mapping configuration is missing
252 private Widget createNewWidget(Type widgetType) throws XmlArtifactGenerationException {
253 return Widget.getWidget(widgetType);
257 * Create a new VF Module that contains zero widgets and has no members.
259 * @return new VF Module resource
261 private Resource createNewVfModule() {
262 Resource vfModule = new Resource(Type.VFMODULE, true);
263 assertNumberOfWidgets(vfModule, 0);
268 * Set up some dummy Model Identification properties.
273 private void populateIdentInfo(Resource vfModule) {
274 Map<String, String> modelIdentInfo = new HashMap<>();
275 modelIdentInfo.put("UUID", "dummy_uuid");
276 vfModule.populateModelIdentificationInformation(modelIdentInfo);
280 * Create a new Widget and assert that it is successfully added to the VF Module.
283 * the VF Module to update
285 * the type of Widget to create and add
286 * @throws XmlArtifactGenerationException
287 * if the Widget mapping configuration is missing
289 private void assertAddWidget(Resource vfModule, Type widgetType) throws XmlArtifactGenerationException {
290 assertThat(createNewWidgetForModule(vfModule, widgetType), is(true));
294 * Create a new Widget and assert that it cannot be added to the VF Module.
299 * the type of Widget to create and attempt to add
300 * @throws XmlArtifactGenerationException
301 * if the Widget mapping configuration is missing
303 private void assertFailToAddWidget(Resource vfModule, Type widgetType) throws XmlArtifactGenerationException {
304 assertThat(createNewWidgetForModule(vfModule, widgetType), is(false));
308 * Create a new widget, make it a member of the VF Module, then try to add it.
311 * the VF Module to update
313 * the type of Widget to create and attempt to add
314 * @return whether or not the Widget was added to the module
315 * @throws XmlArtifactGenerationException
316 * if the Widget mapping configuration is missing
318 private boolean createNewWidgetForModule(Resource vfModule, Type widgetType) throws XmlArtifactGenerationException {
319 Widget widget = createNewWidget(widgetType);
320 setWidgetAsMember(vfModule, widget);
321 return vfModule.addWidget(widget);
325 * Make the specified Widget the sole member of the VF Module. This is achieved by first adding the Widget's own ID
326 * to its set of keys, and by then setting the VF Module's members to a Singleton List comprised of this ID. These
327 * updates allow the Widget to be successfully added to the VF Module. (Non-member Widgets cannot be added.)
330 * the module for which members are overwritten
332 * the widget to be set as the member
334 private void setWidgetAsMember(Resource vfModule, Widget widget) {
335 String id = widget.getId();
337 vfModule.setMembers(Collections.singletonList(id));
341 * Create a vserver widget and add it to the specified VF Module.
344 * the VF Module to update
345 * @return the number of Widgets present in the vserver on creation
346 * @throws XmlArtifactGenerationException
347 * if the Widget mapping configuration is missing
349 private int createVserverForVf(Resource vfModule) throws XmlArtifactGenerationException {
350 Widget vserverWidget = createNewWidget(Type.VSERVER);
351 assertNumberOfWidgets(vfModule, 0);
352 final int initialWidgetCount = addVserverToVf(vfModule, vserverWidget);
353 assertNumberOfWidgets(vfModule, 1);
354 return initialWidgetCount;
358 * Add the specified vserver to the specified VF Module.
361 * the VF Module to update
362 * @param vserverWidget
364 * @return initial widget count for the vserver Widget
365 * @throws XmlArtifactGenerationException
366 * if the Widget mapping configuration is missing
368 private int addVserverToVf(Resource vfModule, Widget vserverWidget) throws XmlArtifactGenerationException {
369 // A vserver (initially) has Flavor, Image, Tenant and Vfc.
370 final int initialWidgetCount = 4;
371 assertNumberOfWidgets(vserverWidget, initialWidgetCount);
373 // Add the vserver to the VF Module.
374 setWidgetAsMember(vfModule, vserverWidget);
375 assertThat(vfModule.addWidget(vserverWidget), is(true));
377 return initialWidgetCount;