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 --- .../models/pdp/persistence/concepts/JpaPdp.java | 15 +++++--- .../pdp/persistence/concepts/JpaPdpGroup.java | 32 +++++++--------- .../pdp/persistence/concepts/JpaPdpStatistics.java | 14 ++----- .../pdp/persistence/concepts/JpaPdpSubGroup.java | 43 +++++++++++----------- .../pdp/persistence/provider/PdpProviderTest.java | 2 +- 5 files changed, 49 insertions(+), 57 deletions(-) (limited to 'models-pdp') diff --git a/models-pdp/src/main/java/org/onap/policy/models/pdp/persistence/concepts/JpaPdp.java b/models-pdp/src/main/java/org/onap/policy/models/pdp/persistence/concepts/JpaPdp.java index d231fe7e4..2e6a2a60a 100644 --- a/models-pdp/src/main/java/org/onap/policy/models/pdp/persistence/concepts/JpaPdp.java +++ b/models-pdp/src/main/java/org/onap/policy/models/pdp/persistence/concepts/JpaPdp.java @@ -36,10 +36,13 @@ 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.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.PfKey; import org.onap.policy.models.base.PfReferenceKey; +import org.onap.policy.models.base.validation.annotations.VerifyKey; import org.onap.policy.models.pdp.concepts.Pdp; import org.onap.policy.models.pdp.enums.PdpHealthStatus; import org.onap.policy.models.pdp.enums.PdpState; @@ -58,15 +61,20 @@ public class JpaPdp extends PfConcept implements PfAuthorative, Serializabl private static final long serialVersionUID = -357224425637789775L; @EmbeddedId + @VerifyKey + @NotNull private PfReferenceKey key; @Column + @NotNull private PdpState pdpState; @Column + @NotNull private PdpHealthStatus healthy; @Column + @NotBlank private String message; /** @@ -161,19 +169,14 @@ public class JpaPdp extends PfConcept implements PfAuthorative, Serializabl @Override public BeanValidationResult validate(@NonNull String fieldName) { - BeanValidationResult result = new BeanValidationResult(fieldName, this); + BeanValidationResult result = super.validate(fieldName); - result.addResult(validateKeyNotNull("key", key)); result.addResult(validateKeyNotNull("parent of key", key.getParentConceptKey())); if (PfKey.NULL_KEY_NAME.equals(key.getParentLocalName())) { addResult(result, "local name of parent of key", key.getParentLocalName(), IS_NULL); } - result.addResult(validateNotNull("pdpState", pdpState)); - result.addResult(validateNotNull("healthy", healthy)); - result.addResult(validateNotBlank("message", message, false)); - return result; } diff --git a/models-pdp/src/main/java/org/onap/policy/models/pdp/persistence/concepts/JpaPdpGroup.java b/models-pdp/src/main/java/org/onap/policy/models/pdp/persistence/concepts/JpaPdpGroup.java index 66d36d8d6..5e078bccb 100644 --- a/models-pdp/src/main/java/org/onap/policy/models/pdp/persistence/concepts/JpaPdpGroup.java +++ b/models-pdp/src/main/java/org/onap/policy/models/pdp/persistence/concepts/JpaPdpGroup.java @@ -44,14 +44,18 @@ 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.annotations.Entries; +import org.onap.policy.common.parameters.annotations.Items; +import org.onap.policy.common.parameters.annotations.NotBlank; +import org.onap.policy.common.parameters.annotations.NotNull; +import org.onap.policy.common.parameters.annotations.Valid; 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.PfReferenceKey; import org.onap.policy.models.base.PfUtils; -import org.onap.policy.models.base.Validated; +import org.onap.policy.models.base.validation.annotations.VerifyKey; import org.onap.policy.models.pdp.concepts.PdpGroup; import org.onap.policy.models.pdp.concepts.PdpSubGroup; import org.onap.policy.models.pdp.enums.PdpState; @@ -70,15 +74,21 @@ public class JpaPdpGroup extends PfConcept implements PfAuthorative { private static final long serialVersionUID = -357224425637789775L; @EmbeddedId + @VerifyKey + @NotNull private PfConceptKey key; @Column + @NotBlank private String description; @Column + @NotNull private PdpState pdpGroupState; @ElementCollection + @Entries(key = @Items(notNull = {@NotNull}, notBlank = {@NotBlank}), + value = @Items(notNull = {@NotNull}, notBlank = {@NotBlank})) private Map properties; // @formatter:off @@ -90,6 +100,8 @@ public class JpaPdpGroup extends PfConcept implements PfAuthorative { @JoinColumn(name = "pdpGroupLocalName", referencedColumnName = "localName") }) // @formatter:on + @NotNull + @Items(notNull = {@NotNull}, valid = {@Valid}) private List pdpSubGroups; /** @@ -214,22 +226,6 @@ public class JpaPdpGroup extends PfConcept implements PfAuthorative { } } - @Override - public BeanValidationResult validate(@NonNull String fieldName) { - BeanValidationResult result = new BeanValidationResult(fieldName, this); - - result.addResult(validateKeyNotNull("key", key)); - result.addResult(validateNotBlank("description", description, false)); - result.addResult(validateNotNull("pdpGroupState", pdpGroupState)); - - validateMap(result, "properties", properties, Validated::validateEntryNotBlankNotBlank); - - result.addResult(validateNotNull("pdpSubGroups", pdpSubGroups)); - validateList(result, "pdpSubGroups", pdpSubGroups, Validated::validateNotNull); - - return result; - } - @Override public int compareTo(final PfConcept otherConcept) { if (otherConcept == null) { diff --git a/models-pdp/src/main/java/org/onap/policy/models/pdp/persistence/concepts/JpaPdpStatistics.java b/models-pdp/src/main/java/org/onap/policy/models/pdp/persistence/concepts/JpaPdpStatistics.java index 49163ee8c..90cf4e468 100644 --- a/models-pdp/src/main/java/org/onap/policy/models/pdp/persistence/concepts/JpaPdpStatistics.java +++ b/models-pdp/src/main/java/org/onap/policy/models/pdp/persistence/concepts/JpaPdpStatistics.java @@ -38,12 +38,13 @@ import lombok.Data; import lombok.EqualsAndHashCode; import lombok.NonNull; import org.apache.commons.lang3.builder.CompareToBuilder; -import org.onap.policy.common.parameters.BeanValidationResult; +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.PfKey; import org.onap.policy.models.base.PfTimestampKey; import org.onap.policy.models.base.PfUtils; +import org.onap.policy.models.base.validation.annotations.VerifyKey; import org.onap.policy.models.pdp.concepts.PdpEngineWorkerStatistics; import org.onap.policy.models.pdp.concepts.PdpStatistics; @@ -63,6 +64,8 @@ public class JpaPdpStatistics extends PfConcept implements PfAuthorative supportedPolicyTypes; @ElementCollection + @NotNull + @Items(notNull = {@NotNull}, valid = {@Valid}) private List policies; @Column + @Min(0) private int currentInstanceCount; @Column + @Min(0) private int desiredInstanceCount; @ElementCollection + @Entries(key = @Items(notNull = {@NotNull}, notBlank = {@NotBlank}), + value = @Items(notNull = {@NotNull}, notBlank = {@NotBlank})) private Map properties; // @formatter:off @@ -100,6 +116,8 @@ public class JpaPdpSubGroup extends PfConcept implements PfAuthorative pdpInstances; /** @@ -278,33 +296,14 @@ public class JpaPdpSubGroup extends PfConcept implements PfAuthorative { new PdpProvider().updatePdpSubGroup(pfDao, PDP_GROUP0, existingSubGroup); }).hasMessageContaining("PDP sub group").hasMessageContaining("desiredInstanceCount") - .hasMessageContaining("is negative"); + .hasMessageContaining("below the minimum value"); existingSubGroup.setDesiredInstanceCount(10); } -- cgit 1.2.3-korg