diff options
author | Jim Hahn <jrh3@att.com> | 2019-04-11 21:58:12 -0400 |
---|---|---|
committer | Jim Hahn <jrh3@att.com> | 2019-04-12 11:37:46 -0400 |
commit | 92d9b661cc32b8dcc90e813aa220e26ef6f83b17 (patch) | |
tree | c1e2afa2a2294377aa5364e8234713c44b62bfeb /models-tosca | |
parent | 8a26f57269caf7a559deb46077050048da92dca8 (diff) |
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 <jrh3@att.com>
Diffstat (limited to 'models-tosca')
8 files changed, 212 insertions, 18 deletions
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 @@ -58,6 +58,12 @@ </dependency> <dependency> + <groupId>org.onap.policy.common</groupId> + <artifactId>common-parameters</artifactId> + <version>${policy.common.version}</version> + </dependency> + + <dependency> <groupId>com.h2database</groupId> <artifactId>h2</artifactId> <scope>test</scope> 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<ToscaPolicyIdentifier> { @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<ToscaPolicyIdentifierOptVersion> { @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<ToscaPolicyTypeIdentifier> { @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 <T> type of key being tested */ -public class ToscaIdentifierTestBase<T> { +public class ToscaIdentifierTestBase<T extends Comparable<T>> { + 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<T> 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<T> clazz) { + public ToscaIdentifierTestBase(Class<T> 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<T> { 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<T> { 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<ToscaPolicyIdentifierOptVersion> { - 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<ToscaPolicyIdentifier> { - 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<ToscaPoli orig = new ToscaPolicyIdentifier(NAME, VERSION); assertEquals(orig.toString(), new ToscaPolicyIdentifier(orig).toString()); } + + @Test + public void testValidatePapRest() throws Exception { + ToscaPolicyIdentifier ident = new ToscaPolicyIdentifier(NAME, VERSION); + ValidationResult result = ident.validatePapRest(); + assertNotNull(result); + assertTrue(result.isValid()); + assertNull(result.getResult()); + + ident = makeIdent(NAME, null); + result = ident.validatePapRest(); + assertNotNull(result); + assertFalse(result.isValid()); + assertNotNull(result.getResult()); + + ident = makeIdent(null, VERSION); + result = ident.validatePapRest(); + assertNotNull(result); + assertFalse(result.isValid()); + assertNotNull(result.getResult()); + } + + @Test + public void testCompareTo() throws Exception { + super.testCompareTo(); + } } diff --git a/models-tosca/src/test/java/org/onap/policy/models/tosca/authorative/concepts/ToscaPolicyTypeIdentifierTest.java b/models-tosca/src/test/java/org/onap/policy/models/tosca/authorative/concepts/ToscaPolicyTypeIdentifierTest.java index 8388f1061..e440dd6da 100644 --- a/models-tosca/src/test/java/org/onap/policy/models/tosca/authorative/concepts/ToscaPolicyTypeIdentifierTest.java +++ b/models-tosca/src/test/java/org/onap/policy/models/tosca/authorative/concepts/ToscaPolicyTypeIdentifierTest.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 ToscaPolicyTypeIdentifierTest extends ToscaIdentifierTestBase<ToscaPolicyTypeIdentifier> { - 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<Tosca assertEquals(orig.toString(), new ToscaPolicyTypeIdentifier(orig).toString()); } + @Test + public void testValidatePapRest() throws Exception { + ToscaPolicyTypeIdentifier ident = new ToscaPolicyTypeIdentifier(NAME, VERSION); + ValidationResult result = ident.validatePapRest(); + assertNotNull(result); + assertTrue(result.isValid()); + assertNull(result.getResult()); + + ident = makeIdent(NAME, null); + result = ident.validatePapRest(); + assertNotNull(result); + assertFalse(result.isValid()); + assertNotNull(result.getResult()); + + ident = makeIdent(null, VERSION); + result = ident.validatePapRest(); + assertNotNull(result); + assertFalse(result.isValid()); + assertNotNull(result.getResult()); + } + + @Test + public void testCompareTo() throws Exception { + super.testCompareTo(); + } } |