From 8623ca6174b6d724d3d480f0bf54300f18460350 Mon Sep 17 00:00:00 2001 From: liamfallon Date: Fri, 15 Jun 2018 08:56:44 +0100 Subject: Removing SONAR issues from apex-pdp basic model Removed some code duplication in exceptions Reduced the number of unlogged exception SONAR messages to one. Issue-ID: POLICY-856 Change-Id: I19e60664270b86f77333979eaa412b2e85cb13fa Signed-off-by: liamfallon --- .../model/basicmodel/concepts/ApexException.java | 18 ++- .../basicmodel/concepts/ApexRuntimeException.java | 16 +-- .../model/basicmodel/concepts/AxArtifactKey.java | 36 +++--- .../model/basicmodel/concepts/AxReferenceKey.java | 138 ++++++++++++--------- .../basicmodel/handling/ApexModelCreator.java | 1 + .../model/basicmodel/handling/ApexModelWriter.java | 2 +- .../basicmodel/service/AbstractParameters.java | 2 +- 7 files changed, 119 insertions(+), 94 deletions(-) (limited to 'model/basic-model/src/main/java/org') diff --git a/model/basic-model/src/main/java/org/onap/policy/apex/model/basicmodel/concepts/ApexException.java b/model/basic-model/src/main/java/org/onap/policy/apex/model/basicmodel/concepts/ApexException.java index 896f72a89..913924389 100644 --- a/model/basic-model/src/main/java/org/onap/policy/apex/model/basicmodel/concepts/ApexException.java +++ b/model/basic-model/src/main/java/org/onap/policy/apex/model/basicmodel/concepts/ApexException.java @@ -37,8 +37,7 @@ public class ApexException extends Exception { * @param message the message on the exception */ public ApexException(final String message) { - super(message); - this.object = null; + this(message, null); } /** @@ -59,8 +58,7 @@ public class ApexException extends Exception { * @param e the exception that caused this Apex exception */ public ApexException(final String message, final Exception e) { - super(message, e); - this.object = null; + this(message, e, null); } /** @@ -81,10 +79,18 @@ public class ApexException extends Exception { * @return the cascaded messages from this exception and the exceptions that caused it */ public String getCascadedMessage() { + return buildCascadedMessage(this); + } + + /** + * Build a cascaded message from an exception and all its nested exceptions + * @param throwable the top level exception + */ + public static String buildCascadedMessage(Throwable throwable) { final StringBuilder builder = new StringBuilder(); - builder.append(this.getMessage()); + builder.append(throwable.getMessage()); - for (Throwable t = this; t != null; t = t.getCause()) { + for (Throwable t = throwable; t != null; t = t.getCause()) { builder.append("\ncaused by: "); builder.append(t.getMessage()); } diff --git a/model/basic-model/src/main/java/org/onap/policy/apex/model/basicmodel/concepts/ApexRuntimeException.java b/model/basic-model/src/main/java/org/onap/policy/apex/model/basicmodel/concepts/ApexRuntimeException.java index c3ecd2fd8..5724b487c 100644 --- a/model/basic-model/src/main/java/org/onap/policy/apex/model/basicmodel/concepts/ApexRuntimeException.java +++ b/model/basic-model/src/main/java/org/onap/policy/apex/model/basicmodel/concepts/ApexRuntimeException.java @@ -37,8 +37,7 @@ public class ApexRuntimeException extends RuntimeException { * @param message the message on the exception */ public ApexRuntimeException(final String message) { - super(message); - this.object = null; + this(message, null); } /** @@ -59,8 +58,7 @@ public class ApexRuntimeException extends RuntimeException { * @param e the exception that caused this Apex exception */ public ApexRuntimeException(final String message, final Exception e) { - super(message, e); - this.object = null; + this(message, e, null); } /** @@ -81,15 +79,7 @@ public class ApexRuntimeException extends RuntimeException { * @return the message of this exception and all the exceptions that caused this exception */ public String getCascadedMessage() { - final StringBuilder builder = new StringBuilder(); - builder.append(this.getMessage()); - - for (Throwable t = this; t != null; t = t.getCause()) { - builder.append("\ncaused by: "); - builder.append(t.getMessage()); - } - - return builder.toString(); + return ApexException.buildCascadedMessage(this); } /** diff --git a/model/basic-model/src/main/java/org/onap/policy/apex/model/basicmodel/concepts/AxArtifactKey.java b/model/basic-model/src/main/java/org/onap/policy/apex/model/basicmodel/concepts/AxArtifactKey.java index 382d5024f..e08c3c65f 100644 --- a/model/basic-model/src/main/java/org/onap/policy/apex/model/basicmodel/concepts/AxArtifactKey.java +++ b/model/basic-model/src/main/java/org/onap/policy/apex/model/basicmodel/concepts/AxArtifactKey.java @@ -72,7 +72,8 @@ public class AxArtifactKey extends AxKey { /** * Copy constructor * - * @param copyConcept the concept to copy from + * @param copyConcept + * the concept to copy from */ public AxArtifactKey(final AxArtifactKey copyConcept) { super(copyConcept); @@ -81,8 +82,10 @@ public class AxArtifactKey extends AxKey { /** * Constructor to create a key with the specified name and version. * - * @param name the key name - * @param version the key version + * @param name + * the key name + * @param version + * the key version */ public AxArtifactKey(final String name, final String version) { super(); @@ -93,7 +96,8 @@ public class AxArtifactKey extends AxKey { /** * Constructor to create a key using the key and version from the specified key ID. * - * @param id the key ID in a format that respects the {@link KEY_ID_REGEXP} + * @param id + * the key ID in a format that respects the {@link KEY_ID_REGEXP} */ public AxArtifactKey(final String id) { Assertions.argumentNotNull(id, "id may not be null"); @@ -164,7 +168,8 @@ public class AxArtifactKey extends AxKey { /** * Sets the key name. * - * @param name the key name + * @param name + * the key name */ public void setName(final String name) { this.name = Assertions.validateStringParameter(NAME_TOKEN, name, NAME_REGEXP); @@ -182,7 +187,8 @@ public class AxArtifactKey extends AxKey { /** * Sets the key version. * - * @param version the key version + * @param version + * the key version */ public void setVersion(final String version) { this.version = Assertions.validateStringParameter(VERSION_TOKEN, version, VERSION_REGEXP); @@ -217,7 +223,7 @@ public class AxArtifactKey extends AxKey { } if (thisVersionArray.length >= 2 && otherVersionArray.length >= 2 - && !thisVersionArray[1].equals(otherVersionArray[1])) { + && !thisVersionArray[1].equals(otherVersionArray[1])) { return Compatibility.MINOR; } @@ -252,18 +258,18 @@ public class AxArtifactKey extends AxKey { */ @Override public AxValidationResult validate(final AxValidationResult result) { - try { - Assertions.validateStringParameter(NAME_TOKEN, name, NAME_REGEXP); - } catch (final IllegalArgumentException e) { + final String nameValidationErrorMessage = Assertions.getStringParameterValidationMessage(NAME_TOKEN, name, + NAME_REGEXP); + if (nameValidationErrorMessage != null) { result.addValidationMessage(new AxValidationMessage(this, this.getClass(), ValidationResult.INVALID, - "name invalid-" + e.getMessage())); + "name invalid-" + nameValidationErrorMessage)); } - try { - Assertions.validateStringParameter(VERSION_TOKEN, version, VERSION_REGEXP); - } catch (final IllegalArgumentException e) { + final String versionValidationErrorMessage = Assertions.getStringParameterValidationMessage(VERSION_TOKEN, version, + VERSION_REGEXP); + if (versionValidationErrorMessage != null) { result.addValidationMessage(new AxValidationMessage(this, this.getClass(), ValidationResult.INVALID, - "version invalid-" + e.getMessage())); + "version invalid-" + versionValidationErrorMessage)); } return result; diff --git a/model/basic-model/src/main/java/org/onap/policy/apex/model/basicmodel/concepts/AxReferenceKey.java b/model/basic-model/src/main/java/org/onap/policy/apex/model/basicmodel/concepts/AxReferenceKey.java index 5965dd02b..7cb4dd832 100644 --- a/model/basic-model/src/main/java/org/onap/policy/apex/model/basicmodel/concepts/AxReferenceKey.java +++ b/model/basic-model/src/main/java/org/onap/policy/apex/model/basicmodel/concepts/AxReferenceKey.java @@ -54,8 +54,8 @@ import org.onap.policy.apex.model.utilities.Assertions; @Embeddable @XmlAccessorType(XmlAccessType.FIELD) @XmlRootElement(name = "apexReferenceKey", namespace = "http://www.onap.org/policy/apex-pdp") -@XmlType(name = "AxReferenceKey", namespace = "http://www.onap.org/policy/apex-pdp", - propOrder = { "parentKeyName", "parentKeyVersion", "parentLocalName", "localName" }) +@XmlType(name = "AxReferenceKey", namespace = "http://www.onap.org/policy/apex-pdp", propOrder = { "parentKeyName", + "parentKeyVersion", "parentLocalName", "localName" }) public class AxReferenceKey extends AxKey { private static final String PARENT_KEY_NAME = "parentKeyName"; @@ -69,8 +69,7 @@ public class AxReferenceKey extends AxKey { public static final String LOCAL_NAME_REGEXP = "[A-Za-z0-9\\-_\\.]+|^$"; /** Regular expression to specify the structure of IDs in reference keys. */ - public static final String REFERENCE_KEY_ID_REGEXP = - "[A-Za-z0-9\\-_]+:[0-9].[0-9].[0-9]:[A-Za-z0-9\\-_]+:[A-Za-z0-9\\-_]+"; + public static final String REFERENCE_KEY_ID_REGEXP = "[A-Za-z0-9\\-_]+:[0-9].[0-9].[0-9]:[A-Za-z0-9\\-_]+:[A-Za-z0-9\\-_]+"; private static final int PARENT_NAME_FIELD = 0; private static final int PARENT_VERSION_FIELD = 1; @@ -103,17 +102,19 @@ public class AxReferenceKey extends AxKey { /** * The Copy Constructor creates a key by copying another key. * - * @param referenceKey the reference key to copy from + * @param referenceKey + * the reference key to copy from */ public AxReferenceKey(final AxReferenceKey referenceKey) { this(referenceKey.getParentKeyName(), referenceKey.getParentKeyVersion(), referenceKey.getParentLocalName(), - referenceKey.getLocalName()); + referenceKey.getLocalName()); } /** * Constructor to create a null reference key for the specified parent artifact key. * - * @param axArtifactKey the parent artifact key of this reference key + * @param axArtifactKey + * the parent artifact key of this reference key */ public AxReferenceKey(final AxArtifactKey axArtifactKey) { this(axArtifactKey.getName(), axArtifactKey.getVersion(), NULL_KEY_NAME, NULL_KEY_NAME); @@ -122,8 +123,10 @@ public class AxReferenceKey extends AxKey { /** * Constructor to create a reference key for the given parent artifact key with the given local name. * - * @param axArtifactKey the parent artifact key of this reference key - * @param localName the local name of this reference key + * @param axArtifactKey + * the parent artifact key of this reference key + * @param localName + * the local name of this reference key */ public AxReferenceKey(final AxArtifactKey axArtifactKey, final String localName) { this(axArtifactKey, NULL_KEY_NAME, localName); @@ -132,8 +135,10 @@ public class AxReferenceKey extends AxKey { /** * Constructor to create a reference key for the given parent reference key with the given local name. * - * @param parentReferenceKey the parent reference key of this reference key - * @param localName the local name of this reference key + * @param parentReferenceKey + * the parent reference key of this reference key + * @param localName + * the local name of this reference key */ public AxReferenceKey(final AxReferenceKey parentReferenceKey, final String localName) { this(parentReferenceKey.getParentArtifactKey(), parentReferenceKey.getLocalName(), localName); @@ -143,9 +148,12 @@ public class AxReferenceKey extends AxKey { * Constructor to create a reference key for the given parent reference key (specified by the parent reference key's * artifact key and local name) with the given local name. * - * @param axArtifactKey the artifact key of the parent reference key of this reference key - * @param parentLocalName the local name of the parent reference key of this reference key - * @param localName the local name of this reference key + * @param axArtifactKey + * the artifact key of the parent reference key of this reference key + * @param parentLocalName + * the local name of the parent reference key of this reference key + * @param localName + * the local name of this reference key */ public AxReferenceKey(final AxArtifactKey axArtifactKey, final String parentLocalName, final String localName) { this(axArtifactKey.getName(), axArtifactKey.getVersion(), parentLocalName, localName); @@ -155,9 +163,12 @@ public class AxReferenceKey extends AxKey { * Constructor to create a reference key for the given parent artifact key (specified by the parent artifact key's * name and version) with the given local name. * - * @param parentKeyName the name of the parent artifact key of this reference key - * @param parentKeyVersion the version of the parent artifact key of this reference key - * @param localName the local name of this reference key + * @param parentKeyName + * the name of the parent artifact key of this reference key + * @param parentKeyVersion + * the version of the parent artifact key of this reference key + * @param localName + * the local name of this reference key */ public AxReferenceKey(final String parentKeyName, final String parentKeyVersion, final String localName) { this(parentKeyName, parentKeyVersion, NULL_KEY_NAME, localName); @@ -167,26 +178,31 @@ public class AxReferenceKey extends AxKey { * Constructor to create a reference key for the given parent key (specified by the parent key's name, version nad * local name) with the given local name. * - * @param parentKeyName the parent key name of this reference key - * @param parentKeyVersion the parent key version of this reference key - * @param parentLocalName the parent local name of this reference key - * @param localName the local name of this reference key + * @param parentKeyName + * the parent key name of this reference key + * @param parentKeyVersion + * the parent key version of this reference key + * @param parentLocalName + * the parent local name of this reference key + * @param localName + * the local name of this reference key */ public AxReferenceKey(final String parentKeyName, final String parentKeyVersion, final String parentLocalName, - final String localName) { + final String localName) { super(); this.parentKeyName = Assertions.validateStringParameter(PARENT_KEY_NAME, parentKeyName, NAME_REGEXP); - this.parentKeyVersion = - Assertions.validateStringParameter(PARENT_KEY_VERSION, parentKeyVersion, VERSION_REGEXP); - this.parentLocalName = - Assertions.validateStringParameter(PARENT_LOCAL_NAME, parentLocalName, LOCAL_NAME_REGEXP); + this.parentKeyVersion = Assertions.validateStringParameter(PARENT_KEY_VERSION, parentKeyVersion, + VERSION_REGEXP); + this.parentLocalName = Assertions.validateStringParameter(PARENT_LOCAL_NAME, parentLocalName, + LOCAL_NAME_REGEXP); this.localName = Assertions.validateStringParameter(LOCAL_NAME, localName, LOCAL_NAME_REGEXP); } /** * Constructor to create a key from the specified key ID. * - * @param id the key ID in a format that respects the {@link KEY_ID_REGEXP} + * @param id + * the key ID in a format that respects the {@link KEY_ID_REGEXP} */ public AxReferenceKey(final String id) { final String conditionedId = Assertions.validateStringParameter("id", id, REFERENCE_KEY_ID_REGEXP); @@ -199,13 +215,13 @@ public class AxReferenceKey extends AxKey { // Initiate the new key parentKeyName = Assertions.validateStringParameter(PARENT_KEY_NAME, nameVersionNameArray[PARENT_NAME_FIELD], - NAME_REGEXP); + NAME_REGEXP); parentKeyVersion = Assertions.validateStringParameter(PARENT_KEY_VERSION, - nameVersionNameArray[PARENT_VERSION_FIELD], VERSION_REGEXP); + nameVersionNameArray[PARENT_VERSION_FIELD], VERSION_REGEXP); parentLocalName = Assertions.validateStringParameter(PARENT_LOCAL_NAME, - nameVersionNameArray[PARENT_LOCAL_NAME_FIELD], LOCAL_NAME_REGEXP); + nameVersionNameArray[PARENT_LOCAL_NAME_FIELD], LOCAL_NAME_REGEXP); localName = Assertions.validateStringParameter(LOCAL_NAME, nameVersionNameArray[LOCAL_NAME_FIELD], - LOCAL_NAME_REGEXP); + LOCAL_NAME_REGEXP); } /** @@ -215,7 +231,7 @@ public class AxReferenceKey extends AxKey { */ public static AxReferenceKey getNullKey() { return new AxReferenceKey(AxKey.NULL_KEY_NAME, AxKey.NULL_KEY_VERSION, AxKey.NULL_KEY_NAME, - AxKey.NULL_KEY_NAME); + AxKey.NULL_KEY_NAME); } /* @@ -271,7 +287,8 @@ public class AxReferenceKey extends AxKey { /** * Sets the parent artifact key of this reference key. * - * @param parentKey the parent artifact key of this reference key + * @param parentKey + * the parent artifact key of this reference key */ public void setParentArtifactKey(final AxArtifactKey parentKey) { Assertions.argumentNotNull(parentKey, "parentKey may not be null"); @@ -284,7 +301,8 @@ public class AxReferenceKey extends AxKey { /** * Sets the parent reference key of this reference key. * - * @param parentKey the parent reference key of this reference key + * @param parentKey + * the parent reference key of this reference key */ public void setParentReferenceKey(final AxReferenceKey parentKey) { Assertions.argumentNotNull(parentKey, "parentKey may not be null"); @@ -306,7 +324,8 @@ public class AxReferenceKey extends AxKey { /** * Sets the parent key name of this reference key. * - * @param parentKeyName the parent key name of this reference key + * @param parentKeyName + * the parent key name of this reference key */ public void setParentKeyName(final String parentKeyName) { this.parentKeyName = Assertions.validateStringParameter(PARENT_KEY_NAME, parentKeyName, NAME_REGEXP); @@ -324,11 +343,12 @@ public class AxReferenceKey extends AxKey { /** * Sets the parent key version of this reference key. * - * @param parentKeyVersion the parent key version of this reference key + * @param parentKeyVersion + * the parent key version of this reference key */ public void setParentKeyVersion(final String parentKeyVersion) { - this.parentKeyVersion = - Assertions.validateStringParameter(PARENT_KEY_VERSION, parentKeyVersion, VERSION_REGEXP); + this.parentKeyVersion = Assertions.validateStringParameter(PARENT_KEY_VERSION, parentKeyVersion, + VERSION_REGEXP); } /** @@ -343,11 +363,12 @@ public class AxReferenceKey extends AxKey { /** * Sets the parent local name of this reference key. * - * @param parentLocalName the parent local name of this reference key + * @param parentLocalName + * the parent local name of this reference key */ public void setParentLocalName(final String parentLocalName) { - this.parentLocalName = - Assertions.validateStringParameter(PARENT_LOCAL_NAME, parentLocalName, LOCAL_NAME_REGEXP); + this.parentLocalName = Assertions.validateStringParameter(PARENT_LOCAL_NAME, parentLocalName, + LOCAL_NAME_REGEXP); } /** @@ -362,7 +383,8 @@ public class AxReferenceKey extends AxKey { /** * Sets the local name of this reference key. * - * @param localName the local name of this reference key + * @param localName + * the local name of this reference key */ public void setLocalName(final String localName) { this.localName = Assertions.validateStringParameter(LOCAL_NAME, localName, LOCAL_NAME_REGEXP); @@ -410,32 +432,32 @@ public class AxReferenceKey extends AxKey { */ @Override public AxValidationResult validate(final AxValidationResult result) { - try { - Assertions.validateStringParameter(PARENT_KEY_NAME, parentKeyName, NAME_REGEXP); - } catch (final IllegalArgumentException e) { + final String parentNameValidationErrorMessage = Assertions.getStringParameterValidationMessage(PARENT_KEY_NAME, + parentKeyName, NAME_REGEXP); + if (parentNameValidationErrorMessage != null) { result.addValidationMessage(new AxValidationMessage(this, this.getClass(), ValidationResult.INVALID, - "parentKeyName invalid-" + e.getMessage())); + "parentKeyName invalid-" + parentNameValidationErrorMessage)); } - try { - Assertions.validateStringParameter(PARENT_KEY_VERSION, parentKeyVersion, VERSION_REGEXP); - } catch (final IllegalArgumentException e) { + final String parentKeyVersionValidationErrorMessage = Assertions + .getStringParameterValidationMessage(PARENT_KEY_VERSION, parentKeyVersion, VERSION_REGEXP); + if (parentKeyVersionValidationErrorMessage != null) { result.addValidationMessage(new AxValidationMessage(this, this.getClass(), ValidationResult.INVALID, - "parentKeyVersion invalid-" + e.getMessage())); + "parentKeyVersion invalid-" + parentKeyVersionValidationErrorMessage)); } - try { - Assertions.validateStringParameter(PARENT_LOCAL_NAME, parentLocalName, LOCAL_NAME_REGEXP); - } catch (final IllegalArgumentException e) { + final String parentLocalNameValidationErrorMessage = Assertions + .getStringParameterValidationMessage(PARENT_LOCAL_NAME, parentLocalName, LOCAL_NAME_REGEXP); + if (parentLocalNameValidationErrorMessage != null) { result.addValidationMessage(new AxValidationMessage(this, this.getClass(), ValidationResult.INVALID, - "parentLocalName invalid-" + e.getMessage())); + "parentLocalName invalid-" + parentLocalNameValidationErrorMessage)); } - try { - Assertions.validateStringParameter(LOCAL_NAME, localName, LOCAL_NAME_REGEXP); - } catch (final IllegalArgumentException e) { + final String localNameValidationErrorMessage = Assertions.getStringParameterValidationMessage(LOCAL_NAME, + localName, LOCAL_NAME_REGEXP); + if (localNameValidationErrorMessage != null) { result.addValidationMessage(new AxValidationMessage(this, this.getClass(), ValidationResult.INVALID, - "localName invalid-" + e.getMessage())); + "localName invalid-" + localNameValidationErrorMessage)); } return result; diff --git a/model/basic-model/src/main/java/org/onap/policy/apex/model/basicmodel/handling/ApexModelCreator.java b/model/basic-model/src/main/java/org/onap/policy/apex/model/basicmodel/handling/ApexModelCreator.java index 7b6d830e9..82a808c60 100644 --- a/model/basic-model/src/main/java/org/onap/policy/apex/model/basicmodel/handling/ApexModelCreator.java +++ b/model/basic-model/src/main/java/org/onap/policy/apex/model/basicmodel/handling/ApexModelCreator.java @@ -28,6 +28,7 @@ import org.onap.policy.apex.model.basicmodel.concepts.AxModel; * @author Liam Fallon (liam.fallon@ericsson.com) * @param the type of Apex model to create, must be a sub class of {@link AxModel} */ +@FunctionalInterface public interface ApexModelCreator { /** diff --git a/model/basic-model/src/main/java/org/onap/policy/apex/model/basicmodel/handling/ApexModelWriter.java b/model/basic-model/src/main/java/org/onap/policy/apex/model/basicmodel/handling/ApexModelWriter.java index 7e56b18ae..2bea92cc6 100644 --- a/model/basic-model/src/main/java/org/onap/policy/apex/model/basicmodel/handling/ApexModelWriter.java +++ b/model/basic-model/src/main/java/org/onap/policy/apex/model/basicmodel/handling/ApexModelWriter.java @@ -233,7 +233,7 @@ public class ApexModelWriter { // May fail if not using XALAN XSLT engine. But not in any way vital domTransformer.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "2"); } catch (final Exception ignore) { - LOGGER.trace("Unable to set indent property..."); + LOGGER.trace("Unable to set indent property", ignore); } return domTransformer; } diff --git a/model/basic-model/src/main/java/org/onap/policy/apex/model/basicmodel/service/AbstractParameters.java b/model/basic-model/src/main/java/org/onap/policy/apex/model/basicmodel/service/AbstractParameters.java index 95bc853b8..c7ec2d662 100644 --- a/model/basic-model/src/main/java/org/onap/policy/apex/model/basicmodel/service/AbstractParameters.java +++ b/model/basic-model/src/main/java/org/onap/policy/apex/model/basicmodel/service/AbstractParameters.java @@ -60,7 +60,7 @@ public abstract class AbstractParameters { return (Class) Class.forName(parameterClassName); } catch (final ClassNotFoundException e) { - throw new ApexRuntimeException("class not found for parameter class name \"" + parameterClassName + "\""); + throw new ApexRuntimeException("class not found for parameter class name \"" + parameterClassName + "\"", e); } } -- cgit 1.2.3-korg