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 Configuration, including the type mappings and the UUID mappings.
47 * if the mappings configuration cannot be loaded
50 public static void setup() throws IOException {
51 ArtifactTestUtils util = new ArtifactTestUtils();
52 util.loadWidgetToUuidMappings();
53 util.loadWidgetMappings();
57 * Call hashCode() method for code coverage.
60 public void testHashCode() {
61 Resource vfModule = createNewVfModule();
62 populateIdentInfo(vfModule);
63 assertThat(vfModule.hashCode(), is(notNullValue()));
67 * Call equals() method for code coverage.
70 public void testEquals() {
71 Resource vfModuleA = createNewVfModule();
72 populateIdentInfo(vfModuleA);
74 // equals() is reflexive
75 assertThat(vfModuleA.equals(vfModuleA), is(true));
77 // equals() is symmetric
78 Resource vfModuleB = createNewVfModule();
79 populateIdentInfo(vfModuleB);
80 assertThat(vfModuleA.equals(vfModuleB), is(true));
81 assertThat(vfModuleB.equals(vfModuleA), is(true));
83 assertThat(vfModuleA.equals(null), is(false));
87 public void testAddVServerWidgetToVf() throws XmlArtifactGenerationException {
88 assertAddWidget(createNewVfModule(), WidgetType.valueOf("VSERVER"));
92 public void testAddServiceWidgetToVf() throws XmlArtifactGenerationException {
93 assertAddWidget(createNewVfModule(), WidgetType.valueOf("SERVICE"));
97 * Add a new Widget to a VF Module, where the Widget is NOT set as a member. N.B. For the current VF Module
98 * implementation the actual Widget type is not important.
100 * @throws XmlArtifactGenerationException
101 * if the Widget mapping configuration is missing
104 public void testNonMemberWidgetToVf() throws XmlArtifactGenerationException {
105 Resource vfModule = createNewVfModule();
106 assertThat(vfModule.addWidget(createNewWidget(WidgetType.valueOf("SERVICE"))), is(false));
107 assertNumberOfWidgets(vfModule, 0);
111 * OAM Network is specifically excluded from a VF Module.
113 * @throws XmlArtifactGenerationException
114 * if the Widget mapping configuration is missing
117 public void testAddOamNetworkWidgetToVf() throws XmlArtifactGenerationException {
118 Resource vfModule = createNewVfModule();
119 assertThat(createNewWidgetForModule(vfModule, WidgetType.valueOf("OAM_NETWORK")), is(false));
120 assertNumberOfWidgets(vfModule, 0);
124 * Add a Volume Widget to a VF Module via a vserver Widget.
126 * <li>Create a VF Module</li>
127 * <li>Add a Volume Widget</li>
128 * <li>Add a vserver Widget</li>
129 * <li>Check that the Volume Widget appears under the vserver</li>
131 * @throws XmlArtifactGenerationException
132 * if the Widget mapping configuration is missing
135 public void testAddVolumeWidgetToVf() throws XmlArtifactGenerationException {
136 Resource vfModule = createNewVfModule();
138 // Adding a Volume widget has no effect until a vserver widget is added.
139 assertAddWidget(vfModule, WidgetType.valueOf("VOLUME"));
140 assertNumberOfWidgets(vfModule, 0);
142 final int vserverBaseWidgetCount = createVserverForVf(vfModule);
144 // The vserver now has Volume as well.
145 assertNumberOfWidgets(vfModule.vserver, vserverBaseWidgetCount + 1);
147 // Adding another instance of a vserver widget fails.
148 assertFailToAddWidget(vfModule, WidgetType.valueOf("VSERVER"));
149 assertNumberOfWidgets(vfModule, 1);
151 // Adding another Volume widget is always treated as successful.
152 assertAddWidget(vfModule, WidgetType.valueOf("VOLUME"));
153 // Assert that no additional Widgets are actually present.
154 assertNumberOfWidgets(vfModule, 1);
155 assertNumberOfWidgets(vfModule.vserver, vserverBaseWidgetCount + 1);
159 * Add an L-Interface Widget to a VF Module via a vserver Widget.
161 * <li>Create a VF Module</li>
162 * <li>Add an L-Interface Widget</li>
163 * <li>Add a vserver Widget</li>
164 * <li>Check that the L-Interface Widget appears under the vserver</li>
166 * @throws XmlArtifactGenerationException
167 * if the Widget mapping configuration is missing
170 public void testAddLinterfaceWidgetToVf() throws XmlArtifactGenerationException {
171 Resource vfModule = createNewVfModule();
173 // Adding an L-Interface widget has no effect until a vserver widget is added.
174 assertFailToAddWidget(vfModule, WidgetType.valueOf("LINT"));
175 assertNumberOfWidgets(vfModule, 0);
177 final int vserverBaseWidgetCount = createVserverForVf(vfModule);
179 // The vserver now has an L-Interface as well.
180 assertNumberOfWidgets(vfModule.vserver, vserverBaseWidgetCount + 1);
182 // Adding another instance of a vserver widget fails.
183 assertFailToAddWidget(vfModule, WidgetType.valueOf("VSERVER"));
184 assertNumberOfWidgets(vfModule, 1);
186 // Adding an L-Interface widget is always treated as successful when a vserver exists.
187 assertAddWidget(vfModule, WidgetType.valueOf("LINT"));
188 // Assert that no additional Widgets are actually present.
189 assertNumberOfWidgets(vfModule, 1);
190 assertNumberOfWidgets(vfModule.vserver, vserverBaseWidgetCount + 1);
194 * Add a Volume and an L-Interface Widget to a VF Module via a vserver Widget.
196 * <li>Create a VF Module</li>
197 * <li>Add a Volume Widget</li>
198 * <li>Add an L-Interface Widget</li>
199 * <li>Add a vserver Widget</li>
200 * <li>Check that both Widgets appear under the vserver</li>
202 * @throws XmlArtifactGenerationException
203 * if the Widget mapping configuration is missing
206 public void testAddVolumeAndLinterfaceWidgetToVf() throws XmlArtifactGenerationException {
207 Resource vfModule = createNewVfModule();
209 // Adding a Volume widget has no effect until a vserver widget is added.
210 assertAddWidget(vfModule, WidgetType.valueOf("VOLUME"));
211 assertNumberOfWidgets(vfModule, 0);
213 // Adding an L-Interface widget has no effect until a vserver widget is added.
214 assertFailToAddWidget(vfModule, WidgetType.valueOf("LINT"));
215 assertNumberOfWidgets(vfModule, 0);
217 final int vserverBaseWidgetCount = createVserverForVf(vfModule);
219 // The vserver now has both Volume and L-Interface.
220 assertNumberOfWidgets(vfModule.vserver, vserverBaseWidgetCount + 2);
222 // Adding another instance of a vserver widget fails.
223 assertFailToAddWidget(vfModule, WidgetType.valueOf("VSERVER"));
224 assertNumberOfWidgets(vfModule, 1);
226 // Add new instances (with no effect).
227 assertAddWidget(vfModule, WidgetType.valueOf("VOLUME"));
228 assertAddWidget(vfModule, WidgetType.valueOf("LINT"));
229 // Assert that no additional Widgets are in fact present.
230 assertNumberOfWidgets(vfModule, 1);
231 assertNumberOfWidgets(vfModule.vserver, vserverBaseWidgetCount + 2);
234 private void assertNumberOfWidgets(Model model, int numberOfWidgets) {
235 assertThat(model.getWidgets(), hasSize(numberOfWidgets));
239 * Use the static Factory method to create a new Widget.
242 * type of Widget to create
243 * @return a new Widget
244 * @throws XmlArtifactGenerationException
245 * if the Widget mapping configuration is missing
247 private Widget createNewWidget(WidgetType widgetType) throws XmlArtifactGenerationException {
248 return Widget.getWidget(widgetType);
252 * Create a new VF Module that contains zero widgets and has no members.
254 * @return new VF Module resource
256 private Resource createNewVfModule() {
257 Resource vfModule = new Resource(WidgetType.valueOf("VFMODULE"), true);
258 assertNumberOfWidgets(vfModule, 0);
263 * Set up some dummy Model Identification properties.
268 private void populateIdentInfo(Resource vfModule) {
269 Map<String, String> modelIdentInfo = new HashMap<>();
270 modelIdentInfo.put("UUID", "dummy_uuid");
271 vfModule.populateModelIdentificationInformation(modelIdentInfo);
275 * Create a new Widget and assert that it is successfully added to the VF Module.
278 * the VF Module to update
280 * the type of Widget to create and add
281 * @throws XmlArtifactGenerationException
282 * if the Widget mapping configuration is missing
284 private void assertAddWidget(Resource vfModule, WidgetType widgetType) throws XmlArtifactGenerationException {
285 assertThat(createNewWidgetForModule(vfModule, widgetType), is(true));
289 * Create a new Widget and assert that it cannot be added to the VF Module.
294 * the type of Widget to create and attempt to add
295 * @throws XmlArtifactGenerationException
296 * if the Widget mapping configuration is missing
298 private void assertFailToAddWidget(Resource vfModule, WidgetType widgetType) throws XmlArtifactGenerationException {
299 assertThat(createNewWidgetForModule(vfModule, widgetType), is(false));
303 * Create a new widget, make it a member of the VF Module, then try to add it.
306 * the VF Module to update
308 * the type of Widget to create and attempt to add
309 * @return whether or not the Widget was added to the module
310 * @throws XmlArtifactGenerationException
311 * if the Widget mapping configuration is missing
313 private boolean createNewWidgetForModule(Resource vfModule, WidgetType widgetType)
314 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(WidgetType.valueOf("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;