From 92d9b661cc32b8dcc90e813aa220e26ef6f83b17 Mon Sep 17 00:00:00 2001 From: Jim Hahn Date: Thu, 11 Apr 2019 21:58:12 -0400 Subject: Add validation methods for PAP REST API Also made the identifier classes comparable. Stupid tabs. Change-Id: I54c0595c6a2c61a1b72b58fe1d667657f9d5d71e Issue-ID: POLICY-1542 Signed-off-by: Jim Hahn --- models-tosca/pom.xml | 6 +++ .../concepts/ToscaPolicyIdentifier.java | 38 +++++++++++++++++- .../concepts/ToscaPolicyIdentifierOptVersion.java | 21 +++++++++- .../concepts/ToscaPolicyTypeIdentifier.java | 38 +++++++++++++++++- .../concepts/ToscaIdentifierTestBase.java | 45 ++++++++++++++++++++-- .../ToscaPolicyIdentifierOptVersionTest.java | 9 +++-- .../concepts/ToscaPolicyIdentifierTest.java | 37 ++++++++++++++++-- .../concepts/ToscaPolicyTypeIdentifierTest.java | 36 +++++++++++++++-- 8 files changed, 212 insertions(+), 18 deletions(-) (limited to 'models-tosca') diff --git a/models-tosca/pom.xml b/models-tosca/pom.xml index 36ee69b9c..18b1e1ef1 100644 --- a/models-tosca/pom.xml +++ b/models-tosca/pom.xml @@ -57,6 +57,12 @@ ${policy.common.version} + + org.onap.policy.common + common-parameters + ${policy.common.version} + + com.h2database h2 diff --git a/models-tosca/src/main/java/org/onap/policy/models/tosca/authorative/concepts/ToscaPolicyIdentifier.java b/models-tosca/src/main/java/org/onap/policy/models/tosca/authorative/concepts/ToscaPolicyIdentifier.java index e55c6bd4d..f98a238ff 100644 --- a/models-tosca/src/main/java/org/onap/policy/models/tosca/authorative/concepts/ToscaPolicyIdentifier.java +++ b/models-tosca/src/main/java/org/onap/policy/models/tosca/authorative/concepts/ToscaPolicyIdentifier.java @@ -23,13 +23,16 @@ package org.onap.policy.models.tosca.authorative.concepts; import lombok.Data; import lombok.NoArgsConstructor; import lombok.NonNull; +import org.apache.commons.lang3.ObjectUtils; +import org.onap.policy.common.parameters.BeanValidationResult; +import org.onap.policy.common.parameters.ValidationResult; /** * Identifies a policy. Both the name and version must be non-null. */ @Data @NoArgsConstructor -public class ToscaPolicyIdentifier { +public class ToscaPolicyIdentifier implements Comparable { @NonNull private String name; @@ -47,4 +50,37 @@ public class ToscaPolicyIdentifier { this.name = source.name; this.version = source.version; } + + /** + * Validates that appropriate fields are populated for an incoming call to the PAP + * REST API. + * + * @return the validation result + */ + public ValidationResult validatePapRest() { + BeanValidationResult result = new BeanValidationResult("group", this); + + result.validateNotNull("name", name); + result.validateNotNull("version", version); + + return result; + } + + @Override + public int compareTo(ToscaPolicyIdentifier other) { + if (this == other) { + return 0; + } + + if (other == null) { + return 1; + } + + int result = ObjectUtils.compare(getName(), other.getName()); + if (result != 0) { + return result; + } + + return ObjectUtils.compare(getVersion(), other.getVersion()); + } } diff --git a/models-tosca/src/main/java/org/onap/policy/models/tosca/authorative/concepts/ToscaPolicyIdentifierOptVersion.java b/models-tosca/src/main/java/org/onap/policy/models/tosca/authorative/concepts/ToscaPolicyIdentifierOptVersion.java index 9296780d5..d5ddb0522 100644 --- a/models-tosca/src/main/java/org/onap/policy/models/tosca/authorative/concepts/ToscaPolicyIdentifierOptVersion.java +++ b/models-tosca/src/main/java/org/onap/policy/models/tosca/authorative/concepts/ToscaPolicyIdentifierOptVersion.java @@ -24,13 +24,14 @@ import com.google.gson.annotations.SerializedName; import lombok.Data; import lombok.NoArgsConstructor; import lombok.NonNull; +import org.apache.commons.lang3.ObjectUtils; /** * Policy identifier with an optional version; only the "name" is required. */ @Data @NoArgsConstructor -public class ToscaPolicyIdentifierOptVersion { +public class ToscaPolicyIdentifierOptVersion implements Comparable { @NonNull @SerializedName("policy-id") @@ -58,4 +59,22 @@ public class ToscaPolicyIdentifierOptVersion { public boolean isNullVersion() { return (version == null); } + + @Override + public int compareTo(ToscaPolicyIdentifierOptVersion other) { + if (this == other) { + return 0; + } + + if (other == null) { + return 1; + } + + int result = ObjectUtils.compare(getName(), other.getName()); + if (result != 0) { + return result; + } + + return ObjectUtils.compare(getVersion(), other.getVersion()); + } } diff --git a/models-tosca/src/main/java/org/onap/policy/models/tosca/authorative/concepts/ToscaPolicyTypeIdentifier.java b/models-tosca/src/main/java/org/onap/policy/models/tosca/authorative/concepts/ToscaPolicyTypeIdentifier.java index a10c3eb9c..4cd1764de 100644 --- a/models-tosca/src/main/java/org/onap/policy/models/tosca/authorative/concepts/ToscaPolicyTypeIdentifier.java +++ b/models-tosca/src/main/java/org/onap/policy/models/tosca/authorative/concepts/ToscaPolicyTypeIdentifier.java @@ -23,13 +23,16 @@ package org.onap.policy.models.tosca.authorative.concepts; import lombok.Data; import lombok.NoArgsConstructor; import lombok.NonNull; +import org.apache.commons.lang3.ObjectUtils; +import org.onap.policy.common.parameters.BeanValidationResult; +import org.onap.policy.common.parameters.ValidationResult; /** * Identifies a policy type. Both the name and version must be non-null. */ @Data @NoArgsConstructor -public class ToscaPolicyTypeIdentifier { +public class ToscaPolicyTypeIdentifier implements Comparable { @NonNull private String name; @@ -47,4 +50,37 @@ public class ToscaPolicyTypeIdentifier { this.name = source.name; this.version = source.version; } + + /** + * Validates that appropriate fields are populated for an incoming call to the PAP + * REST API. + * + * @return the validation result + */ + public ValidationResult validatePapRest() { + BeanValidationResult result = new BeanValidationResult("group", this); + + result.validateNotNull("name", name); + result.validateNotNull("version", version); + + return result; + } + + @Override + public int compareTo(ToscaPolicyTypeIdentifier other) { + if (this == other) { + return 0; + } + + if (other == null) { + return 1; + } + + int result = ObjectUtils.compare(getName(), other.getName()); + if (result != 0) { + return result; + } + + return ObjectUtils.compare(getVersion(), other.getVersion()); + } } diff --git a/models-tosca/src/test/java/org/onap/policy/models/tosca/authorative/concepts/ToscaIdentifierTestBase.java b/models-tosca/src/test/java/org/onap/policy/models/tosca/authorative/concepts/ToscaIdentifierTestBase.java index 5c935394b..d7bca2808 100644 --- a/models-tosca/src/test/java/org/onap/policy/models/tosca/authorative/concepts/ToscaIdentifierTestBase.java +++ b/models-tosca/src/test/java/org/onap/policy/models/tosca/authorative/concepts/ToscaIdentifierTestBase.java @@ -20,6 +20,9 @@ package org.onap.policy.models.tosca.authorative.concepts; +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertTrue; + import org.onap.policy.common.utils.coder.Coder; import org.onap.policy.common.utils.coder.CoderException; import org.onap.policy.common.utils.coder.StandardCoder; @@ -29,19 +32,49 @@ import org.onap.policy.common.utils.coder.StandardCoder; * * @param type of key being tested */ -public class ToscaIdentifierTestBase { +public class ToscaIdentifierTestBase> { + public static final String NAME = "my-name"; + public static final String VERSION = "1.2.3"; private static final Coder coder = new StandardCoder(); private final Class clazz; + private final String nameField; + private final String versionField; /** * Constructs the object. + * * @param clazz the type of class being tested + * @param nameField name of the field containing the "name" + * @param versionField name of the field containing the "version" */ - public ToscaIdentifierTestBase(Class clazz) { + public ToscaIdentifierTestBase(Class clazz, String nameField, String versionField) { this.clazz = clazz; + this.nameField = nameField; + this.versionField = versionField; + } + + /** + * Tests the compareTo() method. + * + * @throws Exception if an error occurs + */ + public void testCompareTo() throws Exception { + T ident = makeIdent(NAME, VERSION); + assertEquals(0, ident.compareTo(ident)); + + assertTrue(ident.compareTo(null) > 0); + + assertTrue(ident.compareTo(makeIdent(NAME, VERSION)) == 0); + assertTrue(ident.compareTo(makeIdent(NAME, null)) > 0); + assertTrue(ident.compareTo(makeIdent(null, VERSION)) > 0); + assertTrue(ident.compareTo(makeIdent(NAME, VERSION + "a")) < 0); + assertTrue(ident.compareTo(makeIdent(NAME + "a", VERSION)) < 0); + + // name takes precedence over version + assertTrue(makeIdent(NAME, VERSION + "a").compareTo(makeIdent(NAME + "a", VERSION)) < 0); } /** @@ -57,7 +90,9 @@ public class ToscaIdentifierTestBase { bldr.append("{"); if (name != null) { - bldr.append("'name':'"); + bldr.append("'"); + bldr.append(nameField); + bldr.append("':'"); bldr.append(name); bldr.append("'"); } @@ -67,7 +102,9 @@ public class ToscaIdentifierTestBase { bldr.append(','); } - bldr.append("'version':'"); + bldr.append("'"); + bldr.append(versionField); + bldr.append("':'"); bldr.append(version); bldr.append("'"); } diff --git a/models-tosca/src/test/java/org/onap/policy/models/tosca/authorative/concepts/ToscaPolicyIdentifierOptVersionTest.java b/models-tosca/src/test/java/org/onap/policy/models/tosca/authorative/concepts/ToscaPolicyIdentifierOptVersionTest.java index 561b4fb21..3f0a7b09c 100644 --- a/models-tosca/src/test/java/org/onap/policy/models/tosca/authorative/concepts/ToscaPolicyIdentifierOptVersionTest.java +++ b/models-tosca/src/test/java/org/onap/policy/models/tosca/authorative/concepts/ToscaPolicyIdentifierOptVersionTest.java @@ -31,11 +31,9 @@ import org.junit.Test; * Test the other constructors, as {@link PojosTest} tests the other methods. */ public class ToscaPolicyIdentifierOptVersionTest extends ToscaIdentifierTestBase { - private static final String NAME = "my-name"; - private static final String VERSION = "1.2.3"; public ToscaPolicyIdentifierOptVersionTest() { - super(ToscaPolicyIdentifierOptVersion.class); + super(ToscaPolicyIdentifierOptVersion.class, "policy-id", "policy-version"); } @Test @@ -68,4 +66,9 @@ public class ToscaPolicyIdentifierOptVersionTest extends ToscaIdentifierTestBase orig = makeIdent(NAME, VERSION); assertEquals(orig.toString(), new ToscaPolicyIdentifierOptVersion(orig).toString()); } + + @Test + public void testCompareTo() throws Exception { + super.testCompareTo(); + } } diff --git a/models-tosca/src/test/java/org/onap/policy/models/tosca/authorative/concepts/ToscaPolicyIdentifierTest.java b/models-tosca/src/test/java/org/onap/policy/models/tosca/authorative/concepts/ToscaPolicyIdentifierTest.java index a53af7b1f..f31abf837 100644 --- a/models-tosca/src/test/java/org/onap/policy/models/tosca/authorative/concepts/ToscaPolicyIdentifierTest.java +++ b/models-tosca/src/test/java/org/onap/policy/models/tosca/authorative/concepts/ToscaPolicyIdentifierTest.java @@ -22,18 +22,21 @@ package org.onap.policy.models.tosca.authorative.concepts; import static org.assertj.core.api.Assertions.assertThatThrownBy; import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertFalse; +import static org.junit.Assert.assertNotNull; +import static org.junit.Assert.assertNull; +import static org.junit.Assert.assertTrue; import org.junit.Test; +import org.onap.policy.common.parameters.ValidationResult; /** - * Test the other constructors, as {@link PojosTest} tests the other methods. + * Test methods not tested by {@link PojosTest}. */ public class ToscaPolicyIdentifierTest extends ToscaIdentifierTestBase { - private static final String NAME = "my-name"; - private static final String VERSION = "1.2.3"; public ToscaPolicyIdentifierTest() { - super(ToscaPolicyIdentifier.class); + super(ToscaPolicyIdentifier.class, "name", "version"); } @Test @@ -59,4 +62,30 @@ public class ToscaPolicyIdentifierTest extends ToscaIdentifierTestBase { - private static final String NAME = "my-name"; - private static final String VERSION = "1.2.3"; public ToscaPolicyTypeIdentifierTest() { - super(ToscaPolicyTypeIdentifier.class); + super(ToscaPolicyTypeIdentifier.class, "name", "version"); } @Test @@ -60,4 +63,29 @@ public class ToscaPolicyTypeIdentifierTest extends ToscaIdentifierTestBase