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;
39 * Direct tests of the VFMODULE Resource and Widget functionality to improve code coverage.
41 public class TestVfModule {
44 * Load the Widget mappings configuration.
47 * if the mappings configuration cannot be loaded
50 public static void setup() throws IOException {
51 new ArtifactTestUtils().loadWidgetMappings();
55 * Call hashCode() method for code coverage.
58 public void testHashCode() {
59 Resource vfModule = createNewVfModule();
60 populateIdentInfo(vfModule);
61 assertThat(vfModule.hashCode(), is(notNullValue()));
65 * Call equals() method for code coverage.
68 public void testEquals() {
69 Resource vfModuleA = createNewVfModule();
70 populateIdentInfo(vfModuleA);
72 // equals() is reflexive
73 assertThat(vfModuleA.equals(vfModuleA), is(true));
75 // equals() is symmetric
76 Resource vfModuleB = createNewVfModule();
77 populateIdentInfo(vfModuleB);
78 assertThat(vfModuleA.equals(vfModuleB), is(true));
79 assertThat(vfModuleB.equals(vfModuleA), is(true));
81 assertThat(vfModuleA.equals(null), is(false));
85 public void testAddVServerWidgetToVf() throws XmlArtifactGenerationException {
86 assertAddWidget(createNewVfModule(), WidgetType.valueOf("VSERVER"));
90 public void testAddServiceWidgetToVf() throws XmlArtifactGenerationException {
91 assertAddWidget(createNewVfModule(), WidgetType.valueOf("SERVICE"));
95 * Add a new Widget to a VF Module, where the Widget is NOT set as a member. N.B. For the current VF Module
96 * implementation the actual Widget type is not important.
98 * @throws XmlArtifactGenerationException
99 * if the Widget mapping configuration is missing
102 public void testNonMemberWidgetToVf() throws XmlArtifactGenerationException {
103 Resource vfModule = createNewVfModule();
104 assertThat(vfModule.addWidget(Widget.createWidget("SERVICE")), is(false));
105 assertNumberOfWidgets(vfModule, 0);
109 * OAM Network is specifically excluded from a VF Module.
111 * @throws XmlArtifactGenerationException
112 * if the Widget mapping configuration is missing
115 public void testAddOamNetworkWidgetToVf() throws XmlArtifactGenerationException {
116 Resource vfModule = createNewVfModule();
117 assertThat(createNewWidgetForModule(vfModule, WidgetType.valueOf("OAM_NETWORK")), is(false));
118 assertNumberOfWidgets(vfModule, 0);
122 * Add a Volume Widget to a VF Module via a vserver Widget.
124 * <li>Create a VF Module</li>
125 * <li>Add a Volume Widget</li>
126 * <li>Add a vserver Widget</li>
127 * <li>Check that the Volume Widget appears under the vserver</li>
129 * @throws XmlArtifactGenerationException
130 * if the Widget mapping configuration is missing
133 public void testAddVolumeWidgetToVf() throws XmlArtifactGenerationException {
134 Resource vfModule = createNewVfModule();
136 // Adding a Volume widget has no effect until a vserver widget is added.
137 assertAddWidget(vfModule, WidgetType.valueOf("VOLUME"));
138 assertNumberOfWidgets(vfModule, 0);
140 final int vserverBaseWidgetCount = createVserverForVf(vfModule);
142 // The vserver now has Volume as well.
143 assertNumberOfWidgets(vfModule.vserver, vserverBaseWidgetCount + 1);
145 // Adding another instance of a vserver widget fails.
146 assertFailToAddWidget(vfModule, WidgetType.valueOf("VSERVER"));
147 assertNumberOfWidgets(vfModule, 1);
149 // Adding another Volume widget is always treated as successful.
150 assertAddWidget(vfModule, WidgetType.valueOf("VOLUME"));
151 // Assert that no additional Widgets are actually present.
152 assertNumberOfWidgets(vfModule, 1);
153 assertNumberOfWidgets(vfModule.vserver, vserverBaseWidgetCount + 1);
157 * Add an L-Interface Widget to a VF Module via a vserver Widget.
159 * <li>Create a VF Module</li>
160 * <li>Add an L-Interface Widget</li>
161 * <li>Add a vserver Widget</li>
162 * <li>Check that the L-Interface Widget appears under the vserver</li>
164 * @throws XmlArtifactGenerationException
165 * if the Widget mapping configuration is missing
168 public void testAddLinterfaceWidgetToVf() throws XmlArtifactGenerationException {
169 Resource vfModule = createNewVfModule();
171 // Adding an L-Interface widget has no effect until a vserver widget is added.
172 assertFailToAddWidget(vfModule, WidgetType.valueOf("LINT"));
173 assertNumberOfWidgets(vfModule, 0);
175 final int vserverBaseWidgetCount = createVserverForVf(vfModule);
177 // The vserver now has an L-Interface as well.
178 assertNumberOfWidgets(vfModule.vserver, vserverBaseWidgetCount + 1);
180 // Adding another instance of a vserver widget fails.
181 assertFailToAddWidget(vfModule, WidgetType.valueOf("VSERVER"));
182 assertNumberOfWidgets(vfModule, 1);
184 // Adding an L-Interface widget is always treated as successful when a vserver exists.
185 assertAddWidget(vfModule, WidgetType.valueOf("LINT"));
186 // Assert that no additional Widgets are actually present.
187 assertNumberOfWidgets(vfModule, 1);
188 assertNumberOfWidgets(vfModule.vserver, vserverBaseWidgetCount + 1);
192 * Add a Volume and an L-Interface Widget to a VF Module via a vserver Widget.
194 * <li>Create a VF Module</li>
195 * <li>Add a Volume Widget</li>
196 * <li>Add an L-Interface Widget</li>
197 * <li>Add a vserver Widget</li>
198 * <li>Check that both Widgets appear under the vserver</li>
200 * @throws XmlArtifactGenerationException
201 * if the Widget mapping configuration is missing
204 public void testAddVolumeAndLinterfaceWidgetToVf() throws XmlArtifactGenerationException {
205 Resource vfModule = createNewVfModule();
207 // Adding a Volume widget has no effect until a vserver widget is added.
208 assertAddWidget(vfModule, WidgetType.valueOf("VOLUME"));
209 assertNumberOfWidgets(vfModule, 0);
211 // Adding an L-Interface widget has no effect until a vserver widget is added.
212 assertFailToAddWidget(vfModule, WidgetType.valueOf("LINT"));
213 assertNumberOfWidgets(vfModule, 0);
215 final int vserverBaseWidgetCount = createVserverForVf(vfModule);
217 // The vserver now has both Volume and L-Interface.
218 assertNumberOfWidgets(vfModule.vserver, vserverBaseWidgetCount + 2);
220 // Adding another instance of a vserver widget fails.
221 assertFailToAddWidget(vfModule, WidgetType.valueOf("VSERVER"));
222 assertNumberOfWidgets(vfModule, 1);
224 // Add new instances (with no effect).
225 assertAddWidget(vfModule, WidgetType.valueOf("VOLUME"));
226 assertAddWidget(vfModule, WidgetType.valueOf("LINT"));
227 // Assert that no additional Widgets are in fact present.
228 assertNumberOfWidgets(vfModule, 1);
229 assertNumberOfWidgets(vfModule.vserver, vserverBaseWidgetCount + 2);
232 private void assertNumberOfWidgets(Model model, int numberOfWidgets) {
233 assertThat(model.getWidgets(), hasSize(numberOfWidgets));
237 * Create a new VF Module that contains zero widgets and has no members.
239 * @return new VF Module resource
241 private Resource createNewVfModule() {
242 Resource vfModule = new Resource(WidgetType.valueOf("VFMODULE"), true);
243 assertNumberOfWidgets(vfModule, 0);
248 * Set up some dummy Model Identification properties.
253 private void populateIdentInfo(Resource vfModule) {
254 Map<String, String> modelIdentInfo = new HashMap<>();
255 modelIdentInfo.put("UUID", "dummy_uuid");
256 vfModule.populateModelIdentificationInformation(modelIdentInfo);
260 * Create a new Widget and assert that it is successfully added to the VF Module.
263 * the VF Module to update
265 * the type of Widget to create and add
266 * @throws XmlArtifactGenerationException
267 * if the Widget mapping configuration is missing
269 private void assertAddWidget(Resource vfModule, WidgetType widgetType) throws XmlArtifactGenerationException {
270 assertThat(createNewWidgetForModule(vfModule, widgetType), is(true));
274 * Create a new Widget and assert that it cannot be added to the VF Module.
279 * the type of Widget to create and attempt to add
280 * @throws XmlArtifactGenerationException
281 * if the Widget mapping configuration is missing
283 private void assertFailToAddWidget(Resource vfModule, WidgetType widgetType) throws XmlArtifactGenerationException {
284 assertThat(createNewWidgetForModule(vfModule, widgetType), is(false));
288 * Create a new widget, make it a member of the VF Module, then try to add it.
291 * the VF Module to update
293 * the type of Widget to create and attempt to add
294 * @return whether or not the Widget was added to the module
295 * @throws XmlArtifactGenerationException
296 * if the Widget mapping configuration is missing
298 private boolean createNewWidgetForModule(Resource vfModule, WidgetType widgetType)
299 throws XmlArtifactGenerationException {
300 Widget widget = Widget.createWidget(widgetType);
301 setWidgetAsMember(vfModule, widget);
302 return vfModule.addWidget(widget);
306 * Make the specified Widget the sole member of the VF Module. This is achieved by first adding the Widget's own ID
307 * to its set of keys, and by then setting the VF Module's members to a Singleton List comprised of this ID. These
308 * updates allow the Widget to be successfully added to the VF Module. (Non-member Widgets cannot be added.)
311 * the module for which members are overwritten
313 * the widget to be set as the member
315 private void setWidgetAsMember(Resource vfModule, Widget widget) {
316 String id = widget.getId();
318 vfModule.setMembers(Collections.singletonList(id));
322 * Create a vserver widget and add it to the specified VF Module.
325 * the VF Module to update
326 * @return the number of Widgets present in the vserver on creation
327 * @throws XmlArtifactGenerationException
328 * if the Widget mapping configuration is missing
330 private int createVserverForVf(Resource vfModule) throws XmlArtifactGenerationException {
331 Widget vserverWidget = Widget.createWidget("VSERVER");
332 assertNumberOfWidgets(vfModule, 0);
333 final int initialWidgetCount = addVserverToVf(vfModule, vserverWidget);
334 assertNumberOfWidgets(vfModule, 1);
335 return initialWidgetCount;
339 * Add the specified vserver to the specified VF Module.
342 * the VF Module to update
343 * @param vserverWidget
345 * @return initial widget count for the vserver Widget
346 * @throws XmlArtifactGenerationException
347 * if the Widget mapping configuration is missing
349 private int addVserverToVf(Resource vfModule, Widget vserverWidget) throws XmlArtifactGenerationException {
350 // A vserver (initially) has Flavor, Image, Tenant and Vfc.
351 final int initialWidgetCount = 4;
352 assertNumberOfWidgets(vserverWidget, initialWidgetCount);
354 // Add the vserver to the VF Module.
355 setWidgetAsMember(vfModule, vserverWidget);
356 assertThat(vfModule.addWidget(vserverWidget), is(true));
358 return initialWidgetCount;