diff options
author | Jim Hahn <jrh3@att.com> | 2020-12-18 19:01:52 -0500 |
---|---|---|
committer | Jim Hahn <jrh3@att.com> | 2020-12-21 10:38:00 -0500 |
commit | 12fce55a66848bcc7f71430324b3a9051b8ce0d4 (patch) | |
tree | f8fdcb848f5b3bcfc07cc44f728bc51da620c6b3 /models-pdp/src/main | |
parent | 6345dce40405740dc09176c45dae03baeb939b8e (diff) |
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 <jrh3@att.com>
Diffstat (limited to 'models-pdp/src/main')
4 files changed, 48 insertions, 56 deletions
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<Pdp>, 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<Pdp>, 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<PdpGroup> { 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<String, String> properties; // @formatter:off @@ -90,6 +100,8 @@ public class JpaPdpGroup extends PfConcept implements PfAuthorative<PdpGroup> { @JoinColumn(name = "pdpGroupLocalName", referencedColumnName = "localName") }) // @formatter:on + @NotNull + @Items(notNull = {@NotNull}, valid = {@Valid}) private List<JpaPdpSubGroup> pdpSubGroups; /** @@ -215,22 +227,6 @@ public class JpaPdpGroup extends PfConcept implements PfAuthorative<PdpGroup> { } @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) { return -1; 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<PdpStat private static final String NULL_NAME = "NULL"; @EmbeddedId + @VerifyKey + @NotNull private PfTimestampKey key; @Column(length = 120) @@ -202,15 +205,6 @@ public class JpaPdpStatistics extends PfConcept implements PfAuthorative<PdpStat } @Override - public BeanValidationResult validate(@NonNull String fieldName) { - BeanValidationResult result = new BeanValidationResult(fieldName, this); - - result.addResult(validateKeyNotNull("key", key)); - - return result; - } - - @Override public void clean() { key.clean(); pdpGroupName = pdpGroupName.trim(); diff --git a/models-pdp/src/main/java/org/onap/policy/models/pdp/persistence/concepts/JpaPdpSubGroup.java b/models-pdp/src/main/java/org/onap/policy/models/pdp/persistence/concepts/JpaPdpSubGroup.java index c3d172a85..3474fccc1 100644 --- a/models-pdp/src/main/java/org/onap/policy/models/pdp/persistence/concepts/JpaPdpSubGroup.java +++ b/models-pdp/src/main/java/org/onap/policy/models/pdp/persistence/concepts/JpaPdpSubGroup.java @@ -44,6 +44,12 @@ import lombok.Data; import lombok.EqualsAndHashCode; import lombok.NonNull; 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.Min; +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; @@ -52,7 +58,7 @@ import org.onap.policy.models.base.PfKeyUse; import org.onap.policy.models.base.PfReferenceKey; import org.onap.policy.models.base.PfSearchableKey; 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.Pdp; import org.onap.policy.models.pdp.concepts.PdpSubGroup; import org.onap.policy.models.tosca.authorative.concepts.ToscaPolicyIdentifier; @@ -72,21 +78,31 @@ public class JpaPdpSubGroup extends PfConcept implements PfAuthorative<PdpSubGro private static final long serialVersionUID = -357224425637789775L; @EmbeddedId + @VerifyKey + @NotNull private PfReferenceKey key; @ElementCollection + @NotNull + @Items(notNull = {@NotNull}, valid = {@Valid}) private List<PfSearchableKey> supportedPolicyTypes; @ElementCollection + @NotNull + @Items(notNull = {@NotNull}, valid = {@Valid}) private List<PfConceptKey> 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<String, String> properties; // @formatter:off @@ -100,6 +116,8 @@ public class JpaPdpSubGroup extends PfConcept implements PfAuthorative<PdpSubGro } ) // formatter:on + @NotNull + @Items(notNull = {@NotNull}, valid = {@Valid}) private List<JpaPdp> pdpInstances; /** @@ -278,33 +296,14 @@ public class JpaPdpSubGroup extends PfConcept implements PfAuthorative<PdpSubGro @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 (currentInstanceCount < 0) { - addResult(result, "currentInstanceCount", currentInstanceCount, "is negative"); - } - - if (desiredInstanceCount < 0) { - addResult(result, "desiredInstanceCount", desiredInstanceCount, "is negative"); - } - - validateMap(result, "properties", properties, Validated::validateEntryNotBlankNotBlank); - - if (supportedPolicyTypes == null || supportedPolicyTypes.isEmpty()) { + if (supportedPolicyTypes != null && supportedPolicyTypes.isEmpty()) { addResult(result, "supportedPolicyTypes", supportedPolicyTypes, "is empty"); - } else { - validateList(result, "supportedPolicyTypes", supportedPolicyTypes, Validated::validateNotNull); } - result.validateNotNull("policies", policies); - validateList(result, "policies", policies, Validated::validateNotNull); - - result.validateNotNull("pdpInstances", pdpInstances); - validateList(result, "pdpInstances", pdpInstances, Validated::validateNotNull); - return result; } |