From 12fce55a66848bcc7f71430324b3a9051b8ce0d4 Mon Sep 17 00:00:00 2001 From: Jim Hahn Date: Fri, 18 Dec 2020 19:01:52 -0500 Subject: Use annotations to do validation Modified models to use annotations instead of function calls for most field validations. Created a few new validation annotations for use within models. Per review comments: - renamed Key to VerifyKey - enhanced VerifyKey to imply "@Valid", unless disabled Issue-ID: POLICY-2648 Change-Id: I2b53f218b0a2ab1ed1f5e278816a3509f1408972 Signed-off-by: Jim Hahn --- .../org/onap/policy/models/base/PfKeyImplTest.java | 6 +++++- .../org/onap/policy/models/base/ValidatedTest.java | 6 ++++-- .../models/base/testconcepts/DummyPfConcept.java | 19 +++++++------------ .../policy/models/base/testconcepts/DummyPfKey.java | 4 ++-- 4 files changed, 18 insertions(+), 17 deletions(-) (limited to 'models-base/src/test/java') diff --git a/models-base/src/test/java/org/onap/policy/models/base/PfKeyImplTest.java b/models-base/src/test/java/org/onap/policy/models/base/PfKeyImplTest.java index 8d6b1b9f4..dc69e2e67 100644 --- a/models-base/src/test/java/org/onap/policy/models/base/PfKeyImplTest.java +++ b/models-base/src/test/java/org/onap/policy/models/base/PfKeyImplTest.java @@ -37,6 +37,7 @@ import lombok.NoArgsConstructor; import lombok.Setter; import org.junit.Test; import org.onap.policy.common.parameters.ValidationResult; +import org.onap.policy.common.parameters.annotations.Pattern; import org.onap.policy.models.base.PfKey.Compatibility; import org.onap.policy.models.base.testconcepts.DummyPfKey; @@ -268,10 +269,13 @@ public class PfKeyImplTest { @Setter @EqualsAndHashCode(callSuper = false) @NoArgsConstructor - private static class MyKey extends PfKeyImpl { + public static class MyKey extends PfKeyImpl { private static final long serialVersionUID = 1L; + @Pattern(regexp = NAME_REGEXP) private String name; + + @Pattern(regexp = VERSION_REGEXP) private String version; public MyKey(String name, String version) { diff --git a/models-base/src/test/java/org/onap/policy/models/base/ValidatedTest.java b/models-base/src/test/java/org/onap/policy/models/base/ValidatedTest.java index 8534d82a6..dab28f636 100644 --- a/models-base/src/test/java/org/onap/policy/models/base/ValidatedTest.java +++ b/models-base/src/test/java/org/onap/policy/models/base/ValidatedTest.java @@ -434,12 +434,14 @@ public class ValidatedTest { private final String text; @Override - public ValidationResult validate(String fieldName) { + public BeanValidationResult validate(String fieldName) { if (TEXT.equals(text)) { return null; } - return new ObjectValidationResult(fieldName, text, ValidationStatus.INVALID, NOT_SAME); + BeanValidationResult result = new BeanValidationResult(fieldName, this); + result.addResult(new ObjectValidationResult(fieldName, text, ValidationStatus.INVALID, NOT_SAME)); + return result; } } } diff --git a/models-base/src/test/java/org/onap/policy/models/base/testconcepts/DummyPfConcept.java b/models-base/src/test/java/org/onap/policy/models/base/testconcepts/DummyPfConcept.java index 67a450702..548c26252 100644 --- a/models-base/src/test/java/org/onap/policy/models/base/testconcepts/DummyPfConcept.java +++ b/models-base/src/test/java/org/onap/policy/models/base/testconcepts/DummyPfConcept.java @@ -27,20 +27,25 @@ import lombok.Data; import lombok.EqualsAndHashCode; import lombok.NonNull; import org.apache.commons.lang3.ObjectUtils; -import org.onap.policy.common.parameters.BeanValidationResult; -import org.onap.policy.common.parameters.ValidationResult; +import org.onap.policy.common.parameters.annotations.NotBlank; +import org.onap.policy.common.parameters.annotations.NotNull; import org.onap.policy.models.base.PfAuthorative; import org.onap.policy.models.base.PfConcept; import org.onap.policy.models.base.PfConceptKey; import org.onap.policy.models.base.PfKey; +import org.onap.policy.models.base.validation.annotations.VerifyKey; @Data @EqualsAndHashCode(callSuper = false) public class DummyPfConcept extends PfConcept implements PfAuthorative { private static final long serialVersionUID = 1L; + @EmbeddedId + @VerifyKey + @NotNull private PfConceptKey key; + @NotBlank private String description; @@ -100,16 +105,6 @@ public class DummyPfConcept extends PfConcept implements PfAuthorative