From e8bb310641941ddbb073df33d92cfbe6f6029029 Mon Sep 17 00:00:00 2001 From: "mark.j.leonard" Date: Mon, 11 Mar 2019 15:17:15 +0000 Subject: [PATCH] Reimplement Widget.Type enum class Replace this with a WidgetType dynamic enumeration, which may be extended at runtime. Load the Widget Types from the mapping configuration (JSON file). Issue-ID: AAI-2229 Change-Id: I5d1bc4291b4446f6d744821bf1d74b1f117b901f Signed-off-by: mark.j.leonard --- .../babel/parser/ArtifactGeneratorToscaParser.java | 16 ++-- .../xml/generator/api/AaiArtifactGenerator.java | 8 +- .../xml/generator/data/GroupConfiguration.java | 8 +- .../generator/data/WidgetConfigurationUtil.java | 17 ++-- .../{WidgetType.java => WidgetTypeConfig.java} | 10 ++- .../onap/aai/babel/xml/generator/model/Model.java | 7 +- .../aai/babel/xml/generator/model/Resource.java | 37 +++++---- .../aai/babel/xml/generator/model/Service.java | 6 +- .../onap/aai/babel/xml/generator/model/Widget.java | 30 ++++--- .../aai/babel/xml/generator/model/WidgetType.java | 73 +++++++++++++++++ .../parser/TestArtifactGeneratorToscaParser.java | 29 ++++--- .../aai/babel/xml/generator/model/TestModel.java | 91 ++++++---------------- .../babel/xml/generator/model/TestVfModule.java | 44 +++++------ .../aai/babel/xml/generator/model/TestWidget.java | 87 ++++++++++++++++----- 14 files changed, 273 insertions(+), 190 deletions(-) rename src/main/java/org/onap/aai/babel/xml/generator/data/{WidgetType.java => WidgetTypeConfig.java} (81%) create mode 100644 src/main/java/org/onap/aai/babel/xml/generator/model/WidgetType.java diff --git a/src/main/java/org/onap/aai/babel/parser/ArtifactGeneratorToscaParser.java b/src/main/java/org/onap/aai/babel/parser/ArtifactGeneratorToscaParser.java index 6e37587..3f2e670 100644 --- a/src/main/java/org/onap/aai/babel/parser/ArtifactGeneratorToscaParser.java +++ b/src/main/java/org/onap/aai/babel/parser/ArtifactGeneratorToscaParser.java @@ -43,7 +43,7 @@ import org.onap.aai.babel.xml.generator.data.WidgetConfigurationUtil; import org.onap.aai.babel.xml.generator.model.Model; import org.onap.aai.babel.xml.generator.model.Resource; import org.onap.aai.babel.xml.generator.model.Widget; -import org.onap.aai.babel.xml.generator.model.Widget.Type; +import org.onap.aai.babel.xml.generator.model.WidgetType; import org.onap.aai.babel.xml.generator.types.ModelType; import org.onap.aai.cl.api.Logger; import org.onap.sdc.tosca.parser.api.ISdcCsarHelper; @@ -191,7 +191,7 @@ public class ArtifactGeneratorToscaParser { } public Resource createInstanceGroupModel(Map properties) { - Resource groupModel = new Resource(Type.INSTANCE_GROUP, true); + Resource groupModel = new Resource(WidgetType.valueOf("INSTANCE_GROUP"), true); groupModel.populateModelIdentificationInformation(properties); return groupModel; } @@ -238,7 +238,7 @@ public class ArtifactGeneratorToscaParser { // Process each VF Group for (Group serviceGroup : serviceGroups) { Model groupModel = Model.getModelFor(serviceGroup.getType()); - if (groupModel.getWidgetType() == Type.VFMODULE) { + if (groupModel.getWidgetType() == WidgetType.valueOf("VFMODULE")) { processVfModule(resources, resourceModel, serviceGroup, serviceNode, (Resource) groupModel); } } @@ -258,8 +258,8 @@ public class ArtifactGeneratorToscaParser { Resource model = Model.getModelFor(nodeTypeName, metaDataType); if (metadata != null && hasAllottedResource(metadata.getAllProperties()) - && model.getWidgetType() == Type.VSERVER) { - model = new Resource(Type.ALLOTTED_RESOURCE, false); + && model.getWidgetType() == WidgetType.valueOf("VSERVER")) { + model = new Resource(WidgetType.valueOf("ALLOTTED_RESOURCE"), false); Map props = new HashMap<>(); props.put("providingService", true); model.setProperties(props); @@ -268,7 +268,7 @@ public class ArtifactGeneratorToscaParser { foundProvidingService |= processModel(resourceModel, metadata, model, resourceNodeTemplate.getProperties()); } - if (resourceModel.getWidgetType() == Type.ALLOTTED_RESOURCE && !foundProvidingService) { + if (resourceModel.getWidgetType() == WidgetType.valueOf("ALLOTTED_RESOURCE") && !foundProvidingService) { final String modelInvariantId = resourceModel.getModelId(); throw new IllegalArgumentException(String.format(GENERATOR_AAI_PROVIDING_SERVICE_MISSING, modelInvariantId == null ? "" : modelInvariantId)); @@ -379,7 +379,7 @@ public class ArtifactGeneratorToscaParser { log.debug(member.getType() + " mapped to " + resource); - if (resource.getWidgetType() == Type.L3_NET) { + if (resource.getWidgetType() == WidgetType.valueOf("L3_NET")) { // An l3-network inside a vf-module is treated as a Widget resource.setModelType(ModelType.WIDGET); } @@ -426,7 +426,7 @@ public class ArtifactGeneratorToscaParser { if (foundProvidingService) { processProvidingService(resourceModel, resourceNode, nodeProperties); } else if (resourceNode != null && resourceNode.getModelType() == ModelType.RESOURCE - && resourceNode.getWidgetType() != Widget.Type.L3_NET) { + && resourceNode.getWidgetType() != WidgetType.valueOf("L3_NET")) { if (metaData != null) { resourceNode.populateModelIdentificationInformation(metaData.getAllProperties()); } diff --git a/src/main/java/org/onap/aai/babel/xml/generator/api/AaiArtifactGenerator.java b/src/main/java/org/onap/aai/babel/xml/generator/api/AaiArtifactGenerator.java index 52d342f..741c194 100644 --- a/src/main/java/org/onap/aai/babel/xml/generator/api/AaiArtifactGenerator.java +++ b/src/main/java/org/onap/aai/babel/xml/generator/api/AaiArtifactGenerator.java @@ -44,8 +44,8 @@ import org.onap.aai.babel.xml.generator.data.WidgetConfigurationUtil; import org.onap.aai.babel.xml.generator.model.Model; import org.onap.aai.babel.xml.generator.model.Resource; import org.onap.aai.babel.xml.generator.model.Service; +import org.onap.aai.babel.xml.generator.model.WidgetType; import org.onap.aai.babel.xml.generator.model.Widget; -import org.onap.aai.babel.xml.generator.model.Widget.Type; import org.onap.aai.babel.xml.generator.types.ModelType; import org.onap.aai.cl.api.Logger; import org.onap.sdc.tosca.parser.api.ISdcCsarHelper; @@ -242,8 +242,8 @@ public class AaiArtifactGenerator implements ArtifactGenerator { if (model != null) { Metadata metadata = nodeTemplate.getMetaData(); if (metadata != null && parser.hasAllottedResource(metadata.getAllProperties()) - && model.getWidgetType() == Type.VF) { - model = new Resource(Type.ALLOTTED_RESOURCE, true); + && model.getWidgetType() == WidgetType.valueOf("VF")) { + model = new Resource(WidgetType.valueOf("ALLOTTED_RESOURCE"), true); } } @@ -268,7 +268,7 @@ public class AaiArtifactGenerator implements ArtifactGenerator { } if (parser.hasSubCategoryTunnelXConnect(serviceMetadata) && parser.hasAllottedResource(serviceMetadata)) { - resourceModel.addWidget(Widget.getWidget(Type.TUNNEL_XCONNECT)); + resourceModel.addWidget(Widget.getWidget(WidgetType.valueOf("TUNNEL_XCONNECT"))); } resources.addAll(parser.processInstanceGroups(resourceModel, nodeTemplate)); diff --git a/src/main/java/org/onap/aai/babel/xml/generator/data/GroupConfiguration.java b/src/main/java/org/onap/aai/babel/xml/generator/data/GroupConfiguration.java index 9d6409b..7ecd3f2 100644 --- a/src/main/java/org/onap/aai/babel/xml/generator/data/GroupConfiguration.java +++ b/src/main/java/org/onap/aai/babel/xml/generator/data/GroupConfiguration.java @@ -2,8 +2,8 @@ * ============LICENSE_START======================================================= * org.onap.aai * ================================================================================ - * Copyright © 2017-2019 AT&T Intellectual Property. All rights reserved. - * Copyright © 2017-2019 European Software Marketing Ltd. + * Copyright (c) 2017-2019 AT&T Intellectual Property. All rights reserved. + * Copyright (c) 2017-2019 European Software Marketing Ltd. * ================================================================================ * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -33,7 +33,7 @@ public class GroupConfiguration { /** * Set of Widget Types. */ - private List widgetTypes; + private List widgetTypes; /** * Mapping from TOSCA type to Widget directly. @@ -44,7 +44,7 @@ public class GroupConfiguration { return instanceGroupTypes; } - public List getWidgetTypes() { + public List getWidgetTypes() { return widgetTypes; } diff --git a/src/main/java/org/onap/aai/babel/xml/generator/data/WidgetConfigurationUtil.java b/src/main/java/org/onap/aai/babel/xml/generator/data/WidgetConfigurationUtil.java index b340cd9..7683f7c 100644 --- a/src/main/java/org/onap/aai/babel/xml/generator/data/WidgetConfigurationUtil.java +++ b/src/main/java/org/onap/aai/babel/xml/generator/data/WidgetConfigurationUtil.java @@ -31,8 +31,8 @@ import java.util.Optional; import java.util.Properties; import org.onap.aai.babel.xml.generator.XmlArtifactGenerationException; import org.onap.aai.babel.xml.generator.model.Resource; +import org.onap.aai.babel.xml.generator.model.WidgetType; import org.onap.aai.babel.xml.generator.model.Widget; -import org.onap.aai.babel.xml.generator.model.Widget.Type; import org.onap.aai.babel.xml.generator.types.ModelType; public class WidgetConfigurationUtil { @@ -69,8 +69,8 @@ public class WidgetConfigurationUtil { return Optional.ofNullable(typeToResource.get(typePrefix)); } - public static Widget createWidgetFromType(Type type) throws XmlArtifactGenerationException { - Optional widget = Optional.ofNullable(typeToWidget.get(type.toString())); + public static Widget createWidgetFromType(String widgetType) throws XmlArtifactGenerationException { + Optional widget = Optional.ofNullable(typeToWidget.get(widgetType)); if (widget.isPresent()) { // Make a copy of the Widget found in the mappings table. return new Widget(widget.get()); @@ -78,25 +78,26 @@ public class WidgetConfigurationUtil { return null; } - public static void setWidgetTypes(List types) { - for (WidgetType type : types) { + public static void setWidgetTypes(List types) { + for (WidgetTypeConfig type : types) { if (type.type == null || type.name == null) { throw new IllegalArgumentException("Incomplete widget type specified: " + type); } - Type widgetType = Widget.Type.valueOf(type.type); + WidgetType widgetType = new WidgetType(type.type); Widget widget = new Widget(widgetType, type.name, type.deleteFlag); typeToWidget.put(type.type, widget); } + WidgetType.validateElements(); } - public static void setWidgetMappings(List mappings) throws IOException { + public static void setWidgetMappings(List mappings) throws IOException { for (WidgetMapping mapping : mappings) { ModelType modelType = Optional.ofNullable(mapping.type).map(String::toUpperCase) .map(s -> Enums.getIfPresent(ModelType.class, s).orNull()).orElse(null); if (mapping.prefix == null || mapping.widget == null || modelType == null) { throw new IOException("Invalid widget mapping specified: " + mapping); } - Resource resource = new Resource(Widget.Type.valueOf(mapping.widget), mapping.deleteFlag); + Resource resource = new Resource(WidgetType.valueOf(mapping.widget), mapping.deleteFlag); resource.setModelType(modelType); typeToResource.put(mapping.prefix, resource); } diff --git a/src/main/java/org/onap/aai/babel/xml/generator/data/WidgetType.java b/src/main/java/org/onap/aai/babel/xml/generator/data/WidgetTypeConfig.java similarity index 81% rename from src/main/java/org/onap/aai/babel/xml/generator/data/WidgetType.java rename to src/main/java/org/onap/aai/babel/xml/generator/data/WidgetTypeConfig.java index 7691f0f..0d0fe6b 100644 --- a/src/main/java/org/onap/aai/babel/xml/generator/data/WidgetType.java +++ b/src/main/java/org/onap/aai/babel/xml/generator/data/WidgetTypeConfig.java @@ -2,8 +2,8 @@ * ============LICENSE_START======================================================= * org.onap.aai * ================================================================================ - * Copyright © 2017-2019 AT&T Intellectual Property. All rights reserved. - * Copyright © 2017-2019 European Software Marketing Ltd. + * Copyright (c) 2017-2019 AT&T Intellectual Property. All rights reserved. + * Copyright (c) 2017-2019 European Software Marketing Ltd. * ================================================================================ * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -21,7 +21,11 @@ package org.onap.aai.babel.xml.generator.data; -public class WidgetType { +/** + * Widget Type as configured in the TOSCA Mappings. + * + */ +public class WidgetTypeConfig { String type; String name; diff --git a/src/main/java/org/onap/aai/babel/xml/generator/model/Model.java b/src/main/java/org/onap/aai/babel/xml/generator/model/Model.java index 12f1ac8..ea1d478 100644 --- a/src/main/java/org/onap/aai/babel/xml/generator/model/Model.java +++ b/src/main/java/org/onap/aai/babel/xml/generator/model/Model.java @@ -30,7 +30,6 @@ import java.util.Optional; import java.util.Set; import org.onap.aai.babel.xml.generator.XmlArtifactGenerationException; import org.onap.aai.babel.xml.generator.data.WidgetConfigurationUtil; -import org.onap.aai.babel.xml.generator.model.Widget.Type; public abstract class Model { @@ -148,9 +147,9 @@ public abstract class Model { */ public static Resource getModelFor(String toscaType, String metaDataType) { if ("Configuration".equals(metaDataType)) { - return new Resource(Type.CONFIGURATION, true); + return new Resource(WidgetType.valueOf("CONFIGURATION"), true); } else if ("CR".equals(metaDataType)) { - return new Resource(Type.CR, true); + return new Resource(WidgetType.valueOf("CR"), true); } else { return getModelFor(toscaType); } @@ -158,7 +157,7 @@ public abstract class Model { public abstract boolean addWidget(Widget resource) throws XmlArtifactGenerationException; - public abstract Widget.Type getWidgetType(); + public abstract WidgetType getWidgetType(); public abstract String getModelTypeName(); diff --git a/src/main/java/org/onap/aai/babel/xml/generator/model/Resource.java b/src/main/java/org/onap/aai/babel/xml/generator/model/Resource.java index 911bf32..f5f7c50 100644 --- a/src/main/java/org/onap/aai/babel/xml/generator/model/Resource.java +++ b/src/main/java/org/onap/aai/babel/xml/generator/model/Resource.java @@ -25,12 +25,11 @@ import java.util.Collections; import java.util.List; import java.util.Map; import org.onap.aai.babel.xml.generator.XmlArtifactGenerationException; -import org.onap.aai.babel.xml.generator.model.Widget.Type; import org.onap.aai.babel.xml.generator.types.ModelType; public class Resource extends Model { - - private Type type; + + private WidgetType type; private boolean deleteFlag; private ModelType modelType = ModelType.RESOURCE; private Map properties = Collections.emptyMap(); @@ -40,7 +39,7 @@ public class Resource extends Model { boolean addvolume = false; List members; - public Resource(Type type, boolean deleteFlag) { + public Resource(WidgetType type, boolean deleteFlag) { this.type = type; this.deleteFlag = deleteFlag; } @@ -71,15 +70,15 @@ public class Resource extends Model { public Map getProperties() { return properties; } - + public void setModelType(ModelType type) { this.modelType = type; } - + public ModelType getModelType() { return modelType; } - + public void setMembers(List members) { this.members = members; } @@ -88,23 +87,23 @@ public class Resource extends Model { * Adds a Widget. * * @param widget - * the widget + * the widget * @return the boolean - * @throws XmlArtifactGenerationException + * @throws XmlArtifactGenerationException */ @Override public boolean addWidget(Widget widget) throws XmlArtifactGenerationException { - if (type == Type.VFMODULE) { + if (type == WidgetType.valueOf("VFMODULE")) { if (widget.memberOf(members)) { - if (vserver == null && widget.getWidgetType() == Type.VSERVER) { + if (vserver == null && widget.getWidgetType() == WidgetType.valueOf("VSERVER")) { addVserverWidget(widget); - } else if (widget.getWidgetType() == Type.LINT) { + } else if (widget.getWidgetType() == WidgetType.valueOf("LINT")) { return addLIntfWidget(widget); - } else if (widget.getWidgetType() == Type.VOLUME) { + } else if (widget.getWidgetType() == WidgetType.valueOf("VOLUME")) { addVolumeWidget(widget); return true; } - if (widget.getWidgetType() != Type.OAM_NETWORK) { + if (widget.getWidgetType() != WidgetType.valueOf("OAM_NETWORK")) { return widgets.add(widget); } } @@ -113,11 +112,11 @@ public class Resource extends Model { return widgets.add(widget); } } - - public Type getWidgetType() { + + public WidgetType getWidgetType() { return type; } - + public String getModelTypeName() { return "resource"; } @@ -154,10 +153,10 @@ public class Resource extends Model { private void addVserverWidget(Widget widget) throws XmlArtifactGenerationException { vserver = widget; if (addlintf) { - vserver.addWidget(Widget.getWidget(Type.LINT)); + vserver.addWidget(Widget.getWidget(WidgetType.valueOf("LINT"))); } if (addvolume) { - vserver.addWidget(Widget.getWidget(Type.VOLUME)); + vserver.addWidget(Widget.getWidget(WidgetType.valueOf("VOLUME"))); } } diff --git a/src/main/java/org/onap/aai/babel/xml/generator/model/Service.java b/src/main/java/org/onap/aai/babel/xml/generator/model/Service.java index 6c96e96..0d20c2d 100644 --- a/src/main/java/org/onap/aai/babel/xml/generator/model/Service.java +++ b/src/main/java/org/onap/aai/babel/xml/generator/model/Service.java @@ -21,8 +21,6 @@ package org.onap.aai.babel.xml.generator.model; -import org.onap.aai.babel.xml.generator.model.Widget.Type; - public class Service extends Model { @Override @@ -31,8 +29,8 @@ public class Service extends Model { } @Override - public Widget.Type getWidgetType() { - return Type.SERVICE; + public WidgetType getWidgetType() { + return WidgetType.valueOf("SERVICE"); } public String getModelTypeName() { diff --git a/src/main/java/org/onap/aai/babel/xml/generator/model/Widget.java b/src/main/java/org/onap/aai/babel/xml/generator/model/Widget.java index 2decc8c..732ec4d 100644 --- a/src/main/java/org/onap/aai/babel/xml/generator/model/Widget.java +++ b/src/main/java/org/onap/aai/babel/xml/generator/model/Widget.java @@ -37,17 +37,13 @@ public class Widget extends Model { public static final String GENERATOR_AAI_CONFIGLPROP_NOT_FOUND = "Cannot generate artifacts. Widget configuration not found for %s"; - public enum Type { - SERVICE, VF, VFC, VSERVER, VOLUME, FLAVOR, TENANT, VOLUME_GROUP, LINT, L3_NET, VFMODULE, IMAGE, OAM_NETWORK, ALLOTTED_RESOURCE, TUNNEL_XCONNECT, CONFIGURATION, CR, INSTANCE_GROUP; - } - private Set keys = new HashSet<>(); protected String name; - protected Type type; + protected WidgetType type; protected boolean deleteFlag = false; - public Widget(Type widgetType, String name, boolean deleteFlag) { + public Widget(WidgetType widgetType, String name, boolean deleteFlag) { type = widgetType; this.name = name; this.deleteFlag = deleteFlag; @@ -61,25 +57,25 @@ public class Widget extends Model { */ public Widget(Widget baseWidget) throws XmlArtifactGenerationException { this(baseWidget.getWidgetType(), baseWidget.getName(), baseWidget.getDeleteFlag()); - if (type == Type.VSERVER) { - widgets.add(getWidget(Type.FLAVOR)); - widgets.add(getWidget(Type.IMAGE)); - widgets.add(getWidget(Type.TENANT)); - widgets.add(getWidget(Type.VFC)); + if (type == WidgetType.valueOf("VSERVER")) { + widgets.add(getWidget(WidgetType.valueOf("FLAVOR"))); + widgets.add(getWidget(WidgetType.valueOf("IMAGE"))); + widgets.add(getWidget(WidgetType.valueOf("TENANT"))); + widgets.add(getWidget(WidgetType.valueOf("VFC"))); } } /** * Gets widget. * - * @param type - * the type + * @param typeString + * * @return a new widget of the specified type * @throws XmlArtifactGenerationException * if there is no configuration defined for the specified type */ - public static Widget getWidget(Type type) throws XmlArtifactGenerationException { - Widget widget = WidgetConfigurationUtil.createWidgetFromType(type); + public static Widget getWidget(WidgetType type) throws XmlArtifactGenerationException { + Widget widget = WidgetConfigurationUtil.createWidgetFromType(type.toString()); if (widget == null) { throw new XmlArtifactGenerationException("No widget type is defined for " + type); } @@ -126,7 +122,7 @@ public class Widget extends Model { } @Override - public Type getWidgetType() { + public WidgetType getWidgetType() { return type; } @@ -175,7 +171,7 @@ public class Widget extends Model { @Override public boolean addWidget(Widget widget) { - if (getWidgetType() == Type.VSERVER) { + if (getWidgetType() == WidgetType.valueOf("VSERVER")) { return widgets.add(widget); } return true; diff --git a/src/main/java/org/onap/aai/babel/xml/generator/model/WidgetType.java b/src/main/java/org/onap/aai/babel/xml/generator/model/WidgetType.java new file mode 100644 index 0000000..2cab6ae --- /dev/null +++ b/src/main/java/org/onap/aai/babel/xml/generator/model/WidgetType.java @@ -0,0 +1,73 @@ +/** + * ============LICENSE_START======================================================= + * org.onap.aai + * ================================================================================ + * Copyright (c) 2017-2019 AT&T Intellectual Property. All rights reserved. + * Copyright (c) 2017-2019 European Software Marketing Ltd. + * ================================================================================ + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * ============LICENSE_END========================================================= + */ + +package org.onap.aai.babel.xml.generator.model; + +import java.util.Arrays; +import java.util.LinkedHashMap; +import java.util.List; +import java.util.Map; + +/** + * Widget Type Enumeration. + * + */ +public class WidgetType { + + /** + * Dynamically created set of Widget Types. + */ + private static Map elements = new LinkedHashMap<>(); + + /** + * Types that must be present for Model generation to function correctly. + */ + private static final List mandatoryElements = Arrays.asList( // + "SERVICE", "VF", "VFC", "VSERVER", "VOLUME", "FLAVOR", "TENANT", "VOLUME_GROUP", "LINT", "L3_NET", + "VFMODULE", "IMAGE", "OAM_NETWORK", "ALLOTTED_RESOURCE", "TUNNEL_XCONNECT", "CONFIGURATION", "CR", + "INSTANCE_GROUP"); + + private final String name; + + public WidgetType(String name) { + this.name = name; + elements.put(name, this); + } + + public static void validateElements() { + mandatoryElements.forEach(WidgetType::valueOf); + } + + public static WidgetType valueOf(String typeName) { + WidgetType type = elements.get(typeName); + if (type == null) { + throw new IllegalArgumentException("Unknown type " + typeName); + } + return type; + } + + @Override + public String toString() { + return name; + } + +} + diff --git a/src/test/java/org/onap/aai/babel/parser/TestArtifactGeneratorToscaParser.java b/src/test/java/org/onap/aai/babel/parser/TestArtifactGeneratorToscaParser.java index acc4a35..50f0aa9 100644 --- a/src/test/java/org/onap/aai/babel/parser/TestArtifactGeneratorToscaParser.java +++ b/src/test/java/org/onap/aai/babel/parser/TestArtifactGeneratorToscaParser.java @@ -2,8 +2,8 @@ * ============LICENSE_START======================================================= * org.onap.aai * ================================================================================ - * Copyright © 2017-2019 AT&T Intellectual Property. All rights reserved. - * Copyright © 2017-2019 European Software Marketing Ltd. + * Copyright (c) 2017-2019 AT&T Intellectual Property. All rights reserved. + * Copyright (c) 2017-2019 European Software Marketing Ltd. * ================================================================================ * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -38,7 +38,7 @@ import org.onap.aai.babel.xml.generator.XmlArtifactGenerationException; import org.onap.aai.babel.xml.generator.data.WidgetConfigurationUtil; import org.onap.aai.babel.xml.generator.data.WidgetMapping; import org.onap.aai.babel.xml.generator.model.Resource; -import org.onap.aai.babel.xml.generator.model.Widget.Type; +import org.onap.aai.babel.xml.generator.model.WidgetType; import org.onap.sdc.tosca.parser.api.ISdcCsarHelper; import org.onap.sdc.toscaparser.api.Group; import org.onap.sdc.toscaparser.api.NodeTemplate; @@ -106,8 +106,8 @@ public class TestArtifactGeneratorToscaParser { @Test(expected = IllegalArgumentException.class) public void testMissingProvidingService() { List nodeTemplateList = Collections.singletonList(buildNodeTemplate("name", "BlockStorage")); - new ArtifactGeneratorToscaParser(null).processResourceModels(new Resource(Type.ALLOTTED_RESOURCE, true), - nodeTemplateList); + new ArtifactGeneratorToscaParser(null) + .processResourceModels(new Resource(WidgetType.valueOf("ALLOTTED_RESOURCE"), true), nodeTemplateList); } /** @@ -117,7 +117,7 @@ public class TestArtifactGeneratorToscaParser { public void testAddResourceNotProvidingService() { List nodeTemplateList = Collections.singletonList(buildNodeTemplate("testCR", "CR")); // Create any Resource to which the CR can be added - final Resource dummyResource = new Resource(Type.ALLOTTED_RESOURCE, true); + final Resource dummyResource = new Resource(WidgetType.valueOf("ALLOTTED_RESOURCE"), true); new ArtifactGeneratorToscaParser(null).processResourceModels(dummyResource, nodeTemplateList); } @@ -125,6 +125,7 @@ public class TestArtifactGeneratorToscaParser { * Initialize the Artifact Generator Widget Mapping config with incomplete data (no type). * * @throws IOException + * if a WidgetMapping is invalid */ @Test(expected = IOException.class) public void testToscaMappingWithoutType() throws IOException { @@ -137,6 +138,7 @@ public class TestArtifactGeneratorToscaParser { * Initialize the Artifact Generator Widget Mapping config with invalid data (type value). * * @throws IOException + * if a WidgetMapping is invalid */ @Test(expected = IOException.class) public void testToscaMappingWithInvalidType() throws IOException { @@ -144,11 +146,12 @@ public class TestArtifactGeneratorToscaParser { invalidMapping.setType("invalid"); WidgetConfigurationUtil.setWidgetMappings(Collections.singletonList(invalidMapping)); } - + /** * Initialize the Artifact Generator Widget Mapping config with incomplete data (no widget name). * * @throws IOException + * if a WidgetMapping is invalid */ @Test(expected = IOException.class) public void testToscaMappingWithoutWidget() throws IOException { @@ -156,15 +159,19 @@ public class TestArtifactGeneratorToscaParser { invalidMapping.setWidget(null); WidgetConfigurationUtil.setWidgetMappings(Collections.singletonList(invalidMapping)); } - + /** * Process a dummy Group object for a Service Resource. * * @throws XmlArtifactGenerationException * if there is no configuration defined for a member Widget of an instance group + * @throws IOException + * if the widget mappings cannot be loaded */ @Test - public void testInstanceGroups() throws XmlArtifactGenerationException { + public void testInstanceGroups() throws XmlArtifactGenerationException, IOException { + new ArtifactTestUtils().loadWidgetMappings(); + final String instanceGroupType = "org.openecomp.groups.ResourceInstanceGroup"; WidgetConfigurationUtil.setSupportedInstanceGroups(Collections.singletonList(instanceGroupType)); @@ -181,8 +188,8 @@ public class TestArtifactGeneratorToscaParser { Mockito.when(helper.getGroupsOfOriginOfNodeTemplate(serviceNodeTemplate)).thenReturn(groups); ArtifactGeneratorToscaParser parser = new ArtifactGeneratorToscaParser(helper); - List resources = - parser.processInstanceGroups(new Resource(Type.INSTANCE_GROUP, true), serviceNodeTemplate); + Resource groupResource = new Resource(WidgetType.valueOf("INSTANCE_GROUP"), true); + List resources = parser.processInstanceGroups(groupResource, serviceNodeTemplate); assertThat(resources.size(), is(1)); Resource resource = resources.get(0); diff --git a/src/test/java/org/onap/aai/babel/xml/generator/model/TestModel.java b/src/test/java/org/onap/aai/babel/xml/generator/model/TestModel.java index f2b78a1..ed4f5c1 100644 --- a/src/test/java/org/onap/aai/babel/xml/generator/model/TestModel.java +++ b/src/test/java/org/onap/aai/babel/xml/generator/model/TestModel.java @@ -26,11 +26,9 @@ import static org.hamcrest.CoreMatchers.nullValue; import static org.junit.Assert.assertThat; import java.io.IOException; -import org.junit.Before; +import org.junit.BeforeClass; import org.junit.Test; -import org.onap.aai.babel.parser.ArtifactGeneratorToscaParser; import org.onap.aai.babel.util.ArtifactTestUtils; -import org.onap.aai.babel.xml.generator.model.Widget.Type; /** * Direct tests of the Model abstract class (to improve code coverage). Not all methods are tested here. Some are @@ -38,47 +36,17 @@ import org.onap.aai.babel.xml.generator.model.Widget.Type; */ public class TestModel { - private Widget widgetModel = new Widget(Type.OAM_NETWORK, "oam-network", true); - private Model anonymousModel; - /** - * Initialize the Artifact Generator with filtering and mapping configuration. Also load the Widget to UUID mappings - * from the Artifact Generator properties. + * Load the Widget Configuration, including the type mappings and the UUID mappings. * * @throws IOException * if the mappings configuration cannot be loaded */ - @Before - public void setup() throws IOException { - ArtifactTestUtils utils = new ArtifactTestUtils(); - utils.setGeneratorSystemProperties(); - - String configLocation = System.getProperty(ArtifactGeneratorToscaParser.PROPERTY_TOSCA_MAPPING_FILE); - if (configLocation == null) { - throw new IllegalArgumentException( - String.format(ArtifactGeneratorToscaParser.GENERATOR_AAI_CONFIGLOCATION_NOT_FOUND, - ArtifactGeneratorToscaParser.PROPERTY_TOSCA_MAPPING_FILE)); - } - - ArtifactGeneratorToscaParser.initToscaMappingsConfiguration(configLocation); - utils.loadWidgetToUuidMappings(); - - anonymousModel = new Model() { - @Override - public boolean addWidget(Widget resource) { - return false; - } - - @Override - public Type getWidgetType() { - return null; - } - - @Override - public String getModelTypeName() { - return null; - } - }; + @BeforeClass + public static void setup() throws IOException { + ArtifactTestUtils util = new ArtifactTestUtils(); + util.loadWidgetToUuidMappings(); + util.loadWidgetMappings(); } @Test @@ -87,21 +55,22 @@ public class TestModel { assertThat(Model.getModelFor(""), is(nullValue())); assertThat(Model.getModelFor("any.unknown.type"), is(nullValue())); - assertMapping("org.openecomp.resource.vfc", Type.VSERVER); - assertMapping("org.openecomp.resource.cp", Type.LINT); - assertMapping("org.openecomp.cp", Type.LINT); - assertMapping("org.openecomp.cp.some.suffix", Type.LINT); - assertMapping("org.openecomp.resource.vl", Type.L3_NET); - assertMapping("org.openecomp.resource.vf", Type.VF); - assertMapping("org.openecomp.groups.vfmodule", Type.VFMODULE); - assertMapping("org.openecomp.groups.VfModule", Type.VFMODULE); - assertMapping("org.openecomp.resource.vfc.nodes.heat.cinder", Type.VOLUME); - assertMapping("org.openecomp.nodes.PortMirroringConfiguration", "Configuration", Type.CONFIGURATION); - assertMapping("any.string", "Configuration", Type.CONFIGURATION); - assertMapping("org.openecomp.resource.cr.Kk1806Cr1", "CR", Type.CR); - assertMapping("any.string", "CR", Type.CR); - - assertMapping("org.openecomp.resource.vfc", "an.unknown.type", Type.VSERVER); + assertMapping("org.openecomp.resource.vfc", WidgetType.valueOf("VSERVER")); + assertMapping("org.openecomp.resource.cp", WidgetType.valueOf("LINT")); + assertMapping("org.openecomp.cp", WidgetType.valueOf("LINT")); + assertMapping("org.openecomp.cp.some.suffix", WidgetType.valueOf("LINT")); + assertMapping("org.openecomp.resource.vl", WidgetType.valueOf("L3_NET")); + assertMapping("org.openecomp.resource.vf", WidgetType.valueOf("VF")); + assertMapping("org.openecomp.groups.vfmodule", WidgetType.valueOf("VFMODULE")); + assertMapping("org.openecomp.groups.VfModule", WidgetType.valueOf("VFMODULE")); + assertMapping("org.openecomp.resource.vfc.nodes.heat.cinder", WidgetType.valueOf("VOLUME")); + assertMapping("org.openecomp.nodes.PortMirroringConfiguration", "Configuration", + WidgetType.valueOf("CONFIGURATION")); + assertMapping("any.string", "Configuration", WidgetType.valueOf("CONFIGURATION")); + assertMapping("org.openecomp.resource.cr.Kk1806Cr1", "CR", WidgetType.valueOf("CR")); + assertMapping("any.string", "CR", WidgetType.valueOf("CR")); + + assertMapping("org.openecomp.resource.vfc", "an.unknown.type", WidgetType.valueOf("VSERVER")); } /** @@ -112,7 +81,7 @@ public class TestModel { * @param widgetType * the type of Widget expected from the mappings */ - private void assertMapping(String toscaType, Type widgetType) { + private void assertMapping(String toscaType, WidgetType widgetType) { assertThat(Model.getModelFor(toscaType).getWidgetType(), is(widgetType)); } @@ -126,18 +95,8 @@ public class TestModel { * @param widgetType * the type of Widget expected from the mappings */ - private void assertMapping(String toscaType, String metadataType, Type widgetType) { + private void assertMapping(String toscaType, String metadataType, WidgetType widgetType) { assertThat(Model.getModelFor(toscaType, metadataType).getWidgetType(), is(widgetType)); } - @Test - public void testGetModelNameVersionId() { - assertThat(anonymousModel.getModelNameVersionId(), is(nullValue())); - } - - @Test(expected = org.onap.aai.babel.xml.generator.error.IllegalAccessException.class) - public void testGetModelNameVersionIdIsUnsupported() { - assertThat(widgetModel.getModelNameVersionId(), is(nullValue())); - } - } diff --git a/src/test/java/org/onap/aai/babel/xml/generator/model/TestVfModule.java b/src/test/java/org/onap/aai/babel/xml/generator/model/TestVfModule.java index f712ba5..14f3798 100644 --- a/src/test/java/org/onap/aai/babel/xml/generator/model/TestVfModule.java +++ b/src/test/java/org/onap/aai/babel/xml/generator/model/TestVfModule.java @@ -34,7 +34,6 @@ import org.junit.BeforeClass; import org.junit.Test; import org.onap.aai.babel.util.ArtifactTestUtils; import org.onap.aai.babel.xml.generator.XmlArtifactGenerationException; -import org.onap.aai.babel.xml.generator.model.Widget.Type; /** * Direct tests of the VFMODULE Resource and Widget functionality to improve code coverage. @@ -86,12 +85,12 @@ public class TestVfModule { @Test public void testAddVServerWidgetToVf() throws XmlArtifactGenerationException { - assertAddWidget(createNewVfModule(), Type.VSERVER); + assertAddWidget(createNewVfModule(), WidgetType.valueOf("VSERVER")); } @Test public void testAddServiceWidgetToVf() throws XmlArtifactGenerationException { - assertAddWidget(createNewVfModule(), Type.SERVICE); + assertAddWidget(createNewVfModule(), WidgetType.valueOf("SERVICE")); } /** @@ -104,7 +103,7 @@ public class TestVfModule { @Test public void testNonMemberWidgetToVf() throws XmlArtifactGenerationException { Resource vfModule = createNewVfModule(); - assertThat(vfModule.addWidget(createNewWidget(Type.SERVICE)), is(false)); + assertThat(vfModule.addWidget(createNewWidget(WidgetType.valueOf("SERVICE"))), is(false)); assertNumberOfWidgets(vfModule, 0); } @@ -117,7 +116,7 @@ public class TestVfModule { @Test public void testAddOamNetworkWidgetToVf() throws XmlArtifactGenerationException { Resource vfModule = createNewVfModule(); - assertThat(createNewWidgetForModule(vfModule, Type.OAM_NETWORK), is(false)); + assertThat(createNewWidgetForModule(vfModule, WidgetType.valueOf("OAM_NETWORK")), is(false)); assertNumberOfWidgets(vfModule, 0); } @@ -137,7 +136,7 @@ public class TestVfModule { Resource vfModule = createNewVfModule(); // Adding a Volume widget has no effect until a vserver widget is added. - assertAddWidget(vfModule, Type.VOLUME); + assertAddWidget(vfModule, WidgetType.valueOf("VOLUME")); assertNumberOfWidgets(vfModule, 0); final int vserverBaseWidgetCount = createVserverForVf(vfModule); @@ -146,11 +145,11 @@ public class TestVfModule { assertNumberOfWidgets(vfModule.vserver, vserverBaseWidgetCount + 1); // Adding another instance of a vserver widget fails. - assertFailToAddWidget(vfModule, Type.VSERVER); + assertFailToAddWidget(vfModule, WidgetType.valueOf("VSERVER")); assertNumberOfWidgets(vfModule, 1); // Adding another Volume widget is always treated as successful. - assertAddWidget(vfModule, Type.VOLUME); + assertAddWidget(vfModule, WidgetType.valueOf("VOLUME")); // Assert that no additional Widgets are actually present. assertNumberOfWidgets(vfModule, 1); assertNumberOfWidgets(vfModule.vserver, vserverBaseWidgetCount + 1); @@ -172,7 +171,7 @@ public class TestVfModule { Resource vfModule = createNewVfModule(); // Adding an L-Interface widget has no effect until a vserver widget is added. - assertFailToAddWidget(vfModule, Type.LINT); + assertFailToAddWidget(vfModule, WidgetType.valueOf("LINT")); assertNumberOfWidgets(vfModule, 0); final int vserverBaseWidgetCount = createVserverForVf(vfModule); @@ -181,11 +180,11 @@ public class TestVfModule { assertNumberOfWidgets(vfModule.vserver, vserverBaseWidgetCount + 1); // Adding another instance of a vserver widget fails. - assertFailToAddWidget(vfModule, Type.VSERVER); + assertFailToAddWidget(vfModule, WidgetType.valueOf("VSERVER")); assertNumberOfWidgets(vfModule, 1); // Adding an L-Interface widget is always treated as successful when a vserver exists. - assertAddWidget(vfModule, Type.LINT); + assertAddWidget(vfModule, WidgetType.valueOf("LINT")); // Assert that no additional Widgets are actually present. assertNumberOfWidgets(vfModule, 1); assertNumberOfWidgets(vfModule.vserver, vserverBaseWidgetCount + 1); @@ -208,11 +207,11 @@ public class TestVfModule { Resource vfModule = createNewVfModule(); // Adding a Volume widget has no effect until a vserver widget is added. - assertAddWidget(vfModule, Type.VOLUME); + assertAddWidget(vfModule, WidgetType.valueOf("VOLUME")); assertNumberOfWidgets(vfModule, 0); // Adding an L-Interface widget has no effect until a vserver widget is added. - assertFailToAddWidget(vfModule, Type.LINT); + assertFailToAddWidget(vfModule, WidgetType.valueOf("LINT")); assertNumberOfWidgets(vfModule, 0); final int vserverBaseWidgetCount = createVserverForVf(vfModule); @@ -221,12 +220,12 @@ public class TestVfModule { assertNumberOfWidgets(vfModule.vserver, vserverBaseWidgetCount + 2); // Adding another instance of a vserver widget fails. - assertFailToAddWidget(vfModule, Type.VSERVER); + assertFailToAddWidget(vfModule, WidgetType.valueOf("VSERVER")); assertNumberOfWidgets(vfModule, 1); // Add new instances (with no effect). - assertAddWidget(vfModule, Type.VOLUME); - assertAddWidget(vfModule, Type.LINT); + assertAddWidget(vfModule, WidgetType.valueOf("VOLUME")); + assertAddWidget(vfModule, WidgetType.valueOf("LINT")); // Assert that no additional Widgets are in fact present. assertNumberOfWidgets(vfModule, 1); assertNumberOfWidgets(vfModule.vserver, vserverBaseWidgetCount + 2); @@ -245,7 +244,7 @@ public class TestVfModule { * @throws XmlArtifactGenerationException * if the Widget mapping configuration is missing */ - private Widget createNewWidget(Type widgetType) throws XmlArtifactGenerationException { + private Widget createNewWidget(WidgetType widgetType) throws XmlArtifactGenerationException { return Widget.getWidget(widgetType); } @@ -255,7 +254,7 @@ public class TestVfModule { * @return new VF Module resource */ private Resource createNewVfModule() { - Resource vfModule = new Resource(Type.VFMODULE, true); + Resource vfModule = new Resource(WidgetType.valueOf("VFMODULE"), true); assertNumberOfWidgets(vfModule, 0); return vfModule; } @@ -282,7 +281,7 @@ public class TestVfModule { * @throws XmlArtifactGenerationException * if the Widget mapping configuration is missing */ - private void assertAddWidget(Resource vfModule, Type widgetType) throws XmlArtifactGenerationException { + private void assertAddWidget(Resource vfModule, WidgetType widgetType) throws XmlArtifactGenerationException { assertThat(createNewWidgetForModule(vfModule, widgetType), is(true)); } @@ -296,7 +295,7 @@ public class TestVfModule { * @throws XmlArtifactGenerationException * if the Widget mapping configuration is missing */ - private void assertFailToAddWidget(Resource vfModule, Type widgetType) throws XmlArtifactGenerationException { + private void assertFailToAddWidget(Resource vfModule, WidgetType widgetType) throws XmlArtifactGenerationException { assertThat(createNewWidgetForModule(vfModule, widgetType), is(false)); } @@ -311,7 +310,8 @@ public class TestVfModule { * @throws XmlArtifactGenerationException * if the Widget mapping configuration is missing */ - private boolean createNewWidgetForModule(Resource vfModule, Type widgetType) throws XmlArtifactGenerationException { + private boolean createNewWidgetForModule(Resource vfModule, WidgetType widgetType) + throws XmlArtifactGenerationException { Widget widget = createNewWidget(widgetType); setWidgetAsMember(vfModule, widget); return vfModule.addWidget(widget); @@ -343,7 +343,7 @@ public class TestVfModule { * if the Widget mapping configuration is missing */ private int createVserverForVf(Resource vfModule) throws XmlArtifactGenerationException { - Widget vserverWidget = createNewWidget(Type.VSERVER); + Widget vserverWidget = createNewWidget(WidgetType.valueOf("VSERVER")); assertNumberOfWidgets(vfModule, 0); final int initialWidgetCount = addVserverToVf(vfModule, vserverWidget); assertNumberOfWidgets(vfModule, 1); diff --git a/src/test/java/org/onap/aai/babel/xml/generator/model/TestWidget.java b/src/test/java/org/onap/aai/babel/xml/generator/model/TestWidget.java index 8c2338c..4f28131 100644 --- a/src/test/java/org/onap/aai/babel/xml/generator/model/TestWidget.java +++ b/src/test/java/org/onap/aai/babel/xml/generator/model/TestWidget.java @@ -22,6 +22,7 @@ package org.onap.aai.babel.xml.generator.model; import static org.hamcrest.CoreMatchers.is; +import static org.hamcrest.CoreMatchers.nullValue; import static org.junit.Assert.assertThat; import java.io.IOException; @@ -30,7 +31,6 @@ import org.junit.BeforeClass; import org.junit.Test; import org.onap.aai.babel.util.ArtifactTestUtils; import org.onap.aai.babel.xml.generator.XmlArtifactGenerationException; -import org.onap.aai.babel.xml.generator.model.Widget.Type; import org.onap.aai.babel.xml.generator.types.ModelType; /** @@ -53,82 +53,82 @@ public class TestWidget { @Test public void testGetWidgets() throws XmlArtifactGenerationException { - Widget widget = Widget.getWidget(Type.SERVICE); + Widget widget = Widget.getWidget(WidgetType.valueOf("SERVICE")); assertThat(widget.getType(), is(ModelType.WIDGET)); assertThat(widget.getName(), is("service-instance")); assertThat(widget.getDeleteFlag(), is(true)); - widget = Widget.getWidget(Type.VF); + widget = Widget.getWidget(WidgetType.valueOf("VF")); assertThat(widget.getType(), is(ModelType.WIDGET)); assertThat(widget.getName(), is("generic-vnf")); assertThat(widget.getDeleteFlag(), is(false)); - widget = Widget.getWidget(Type.VFC); + widget = Widget.getWidget(WidgetType.valueOf("VFC")); assertThat(widget.getType(), is(ModelType.WIDGET)); assertThat(widget.getName(), is("vnfc")); assertThat(widget.getDeleteFlag(), is(true)); - widget = Widget.getWidget(Type.VSERVER); + widget = Widget.getWidget(WidgetType.valueOf("VSERVER")); assertThat(widget.getType(), is(ModelType.WIDGET)); assertThat(widget.getName(), is("vserver")); assertThat(widget.getDeleteFlag(), is(true)); - widget = Widget.getWidget(Type.VOLUME); + widget = Widget.getWidget(WidgetType.valueOf("VOLUME")); assertThat(widget.getType(), is(ModelType.WIDGET)); assertThat(widget.getName(), is("volume")); assertThat(widget.getDeleteFlag(), is(true)); - widget = Widget.getWidget(Type.FLAVOR); + widget = Widget.getWidget(WidgetType.valueOf("FLAVOR")); assertThat(widget.getType(), is(ModelType.WIDGET)); assertThat(widget.getName(), is("flavor")); assertThat(widget.getDeleteFlag(), is(false)); - widget = Widget.getWidget(Type.TENANT); + widget = Widget.getWidget(WidgetType.valueOf("TENANT")); assertThat(widget.getType(), is(ModelType.WIDGET)); assertThat(widget.getName(), is("tenant")); assertThat(widget.getDeleteFlag(), is(false)); - widget = Widget.getWidget(Type.VOLUME_GROUP); + widget = Widget.getWidget(WidgetType.valueOf("VOLUME_GROUP")); assertThat(widget.getType(), is(ModelType.WIDGET)); assertThat(widget.getName(), is("volume-group")); assertThat(widget.getDeleteFlag(), is(true)); - widget = Widget.getWidget(Type.LINT); + widget = Widget.getWidget(WidgetType.valueOf("LINT")); assertThat(widget.getType(), is(ModelType.WIDGET)); assertThat(widget.getName(), is("l-interface")); assertThat(widget.getDeleteFlag(), is(true)); - widget = Widget.getWidget(Type.L3_NET); + widget = Widget.getWidget(WidgetType.valueOf("L3_NET")); assertThat(widget.getType(), is(ModelType.WIDGET)); assertThat(widget.getName(), is("l3-network")); assertThat(widget.getDeleteFlag(), is(true)); - widget = Widget.getWidget(Type.VFMODULE); + widget = Widget.getWidget(WidgetType.valueOf("VFMODULE")); assertThat(widget.getType(), is(ModelType.WIDGET)); assertThat(widget.getName(), is("vf-module")); assertThat(widget.getDeleteFlag(), is(true)); - widget = Widget.getWidget(Type.IMAGE); + widget = Widget.getWidget(WidgetType.valueOf("IMAGE")); assertThat(widget.getType(), is(ModelType.WIDGET)); assertThat(widget.getName(), is("image")); assertThat(widget.getDeleteFlag(), is(false)); - widget = Widget.getWidget(Type.OAM_NETWORK); + widget = Widget.getWidget(WidgetType.valueOf("OAM_NETWORK")); assertThat(widget.getType(), is(ModelType.WIDGET)); assertThat(widget.getName(), is("oam-network")); assertThat(widget.getDeleteFlag(), is(true)); - widget = Widget.getWidget(Type.ALLOTTED_RESOURCE); + widget = Widget.getWidget(WidgetType.valueOf("ALLOTTED_RESOURCE")); assertThat(widget.getType(), is(ModelType.WIDGET)); assertThat(widget.getName(), is("allotted-resource")); assertThat(widget.getDeleteFlag(), is(true)); - widget = Widget.getWidget(Type.TUNNEL_XCONNECT); + widget = Widget.getWidget(WidgetType.valueOf("TUNNEL_XCONNECT")); assertThat(widget.getType(), is(ModelType.WIDGET)); assertThat(widget.getName(), is("tunnel-xconnect")); assertThat(widget.getDeleteFlag(), is(true)); - widget = Widget.getWidget(Type.CONFIGURATION); + widget = Widget.getWidget(WidgetType.valueOf("CONFIGURATION")); assertThat(widget.getType(), is(ModelType.WIDGET)); assertThat(widget.getName(), is("configuration")); assertThat(widget.getDeleteFlag(), is(true)); @@ -136,16 +136,63 @@ public class TestWidget { @Test public void testWidgetMethods() throws XmlArtifactGenerationException { - Widget widget = new Widget(Type.SERVICE, "service-instance", true); + Widget widget = new Widget(WidgetType.valueOf("SERVICE"), "service-instance", true); assertThat(widget.getType(), is(ModelType.WIDGET)); assertThat(widget.getWidgetId(), is("service-instance-invariant-id")); - assertThat(widget.addWidget(Widget.getWidget(Type.TENANT)), is(true)); + assertThat(widget.addWidget(Widget.getWidget(WidgetType.valueOf("TENANT"))), is(true)); assertThat(widget.memberOf(null), is(false)); assertThat(widget.memberOf(Collections.emptyList()), is(false)); } + @Test(expected = IllegalArgumentException.class) + public void testGetUnknownWidget() throws XmlArtifactGenerationException { + WidgetType.valueOf("invalid-widget-name"); + } + + /** + * Try to get the Widget object for an unsupported (non-configured) type. + * + * @throws XmlArtifactGenerationException + * if there is no configuration defined for the specified Widget type + */ + @Test(expected = XmlArtifactGenerationException.class) + public void testGetDynamicWidget() throws XmlArtifactGenerationException { + Widget.getWidget(new WidgetType(null)); + } + @Test(expected = org.onap.aai.babel.xml.generator.error.IllegalAccessException.class) public void testAddResourceIsUnsupported() throws XmlArtifactGenerationException { - Widget.getWidget(Type.OAM_NETWORK).addResource(null); + Widget.getWidget(WidgetType.valueOf("OAM_NETWORK")).addResource(null); + } + + @Test(expected = IllegalArgumentException.class) + public void testGetVersionIdForUknownWidget() { + new Widget(new WidgetType("test"), null, false).getId(); + } + + @Test(expected = IllegalArgumentException.class) + public void testGetInvariantIdForUknownWidget() { + new Widget(new WidgetType("test"), null, false).getWidgetId(); + } + + // Call Widget methods which are not supported, purely for code coverage. + + @Test(expected = org.onap.aai.babel.xml.generator.error.IllegalAccessException.class) + public void testGetModelNameVersionIdIsUnsupported() { + Widget widgetModel = new Widget(WidgetType.valueOf("OAM_NETWORK"), "oam-network", true); + assertThat(widgetModel.getModelNameVersionId(), is(nullValue())); + } + + @Test(expected = org.onap.aai.babel.xml.generator.error.IllegalAccessException.class) + public void testGetModelTypeNameIsUnsupported() { + Widget widgetModel = new Widget(WidgetType.valueOf("OAM_NETWORK"), "oam-network", true); + assertThat(widgetModel.getModelTypeName(), is(nullValue())); } + + @Test(expected = org.onap.aai.babel.xml.generator.error.IllegalAccessException.class) + public void testGetModelIdIsUnsupported() { + Widget widgetModel = new Widget(WidgetType.valueOf("OAM_NETWORK"), "oam-network", true); + assertThat(widgetModel.getModelId(), is(nullValue())); + } + } -- 2.16.6