Use annotations to do validation
[policy/models.git] / models-base / src / main / java / org / onap / policy / models / base / PfConceptContainer.java
index b8f2ed6..4c2cf0c 100644 (file)
@@ -35,7 +35,6 @@ import java.util.TreeSet;
 import java.util.function.Function;
 import javax.persistence.CascadeType;
 import javax.persistence.EmbeddedId;
-import javax.persistence.Entity;
 import javax.persistence.JoinColumn;
 import javax.persistence.JoinTable;
 import javax.persistence.ManyToMany;
@@ -46,7 +45,10 @@ import lombok.Data;
 import lombok.EqualsAndHashCode;
 import lombok.NonNull;
 import org.apache.commons.lang3.StringUtils;
-import org.onap.policy.models.base.PfValidationResult.ValidationResult;
+import org.onap.policy.common.parameters.BeanValidationResult;
+import org.onap.policy.common.parameters.ValidationResult;
+import org.onap.policy.common.parameters.annotations.NotNull;
+import org.onap.policy.models.base.validation.annotations.VerifyKey;
 
 // @formatter:off
 /**
@@ -62,18 +64,19 @@ import org.onap.policy.models.base.PfValidationResult.ValidationResult;
  */
 //@formatter:on
 @MappedSuperclass
-@Entity
 @Table(name = "PfConceptContainer")
 @Data
 @EqualsAndHashCode(callSuper = false)
 
 public class PfConceptContainer<C extends PfConcept, A extends PfNameVersion> extends PfConcept
-    implements PfConceptGetter<C>, PfAuthorative<List<Map<String, A>>> {
+        implements PfConceptGetter<C>, PfAuthorative<List<Map<String, A>>> {
     private static final long serialVersionUID = -324211738823208318L;
 
     private static final Pattern KEY_ID_PATTERN = Pattern.compile(PfKey.KEY_ID_REGEXP);
 
     @EmbeddedId
+    @VerifyKey
+    @NotNull
     private PfConceptKey key;
 
     @ManyToMany(cascade = CascadeType.ALL)
@@ -195,9 +198,9 @@ public class PfConceptContainer<C extends PfConcept, A extends PfNameVersion> ex
                 }
 
                 incomingConceptEntry.getValue().setName(findConceptField(conceptKey, conceptKey.getName(),
-                    incomingConceptEntry.getValue(), PfNameVersion::getName));
+                        incomingConceptEntry.getValue(), PfNameVersion::getName));
                 incomingConceptEntry.getValue().setVersion(findConceptField(conceptKey, conceptKey.getVersion(),
-                    incomingConceptEntry.getValue(), PfNameVersion::getVersion));
+                        incomingConceptEntry.getValue(), PfNameVersion::getVersion));
 
                 C jpaConcept = getConceptNewInstance();
                 // This cast allows us to call the fromAuthorative method
@@ -214,7 +217,7 @@ public class PfConceptContainer<C extends PfConcept, A extends PfNameVersion> ex
 
         if (conceptMap.isEmpty()) {
             throw new PfModelRuntimeException(Response.Status.BAD_REQUEST,
-                "An incoming list of concepts must have at least one entry");
+                    "An incoming list of concepts must have at least one entry");
         }
     }
 
@@ -243,19 +246,9 @@ public class PfConceptContainer<C extends PfConcept, A extends PfNameVersion> ex
     }
 
     @Override
-    public PfValidationResult validate(@NonNull final PfValidationResult resultIn) {
-        PfValidationResult result = resultIn;
-
-        if (key.equals(PfConceptKey.getNullKey())) {
-            result.addValidationMessage(
-                new PfValidationMessage(key, this.getClass(), ValidationResult.INVALID, "key is a null key"));
-        }
-
-        result = key.validate(result);
-
-        if (!conceptMap.isEmpty()) {
-            result = validateConceptMap(result);
-        }
+    public BeanValidationResult validate(@NonNull String fieldName) {
+        BeanValidationResult result = new PfValidator().validateTop(fieldName, this);
+        result.addResult(validateConceptMap());
 
         return result;
     }
@@ -263,29 +256,31 @@ public class PfConceptContainer<C extends PfConcept, A extends PfNameVersion> ex
     /**
      * Validate the concept map of the container.
      *
-     * @param resultIn the incoming validation results so far
-     * @return the validation results with the results of this validation added
+     * @return the validation result
      */
-    private PfValidationResult validateConceptMap(final PfValidationResult resultIn) {
-        PfValidationResult result = resultIn;
+    private ValidationResult validateConceptMap() {
+        BeanValidationResult result = new BeanValidationResult("conceptMap", conceptMap);
 
         for (final Entry<PfConceptKey, C> conceptEntry : conceptMap.entrySet()) {
+            BeanValidationResult result2 = null;
+
             if (conceptEntry.getKey().equals(PfConceptKey.getNullKey())) {
-                result.addValidationMessage(new PfValidationMessage(key, this.getClass(), ValidationResult.INVALID,
-                    "key on concept entry " + conceptEntry.getKey() + " may not be the null key"));
+                addResult(result, "key on concept entry", conceptEntry.getKey(), IS_A_NULL_KEY);
             } else if (conceptEntry.getValue() == null) {
-                result.addValidationMessage(new PfValidationMessage(key, this.getClass(), ValidationResult.INVALID,
-                    "value on concept entry " + conceptEntry.getKey() + " may not be null"));
+                result2 = new BeanValidationResult(conceptEntry.getKey().getId(), conceptEntry.getKey());
+                addResult(result2, "value", conceptEntry.getValue(), IS_NULL);
             } else if (!conceptEntry.getKey().equals(conceptEntry.getValue().getKey())) {
-                result.addValidationMessage(new PfValidationMessage(key, this.getClass(), ValidationResult.INVALID,
-                    "key on concept entry key " + conceptEntry.getKey() + " does not equal concept value key "
-                        + conceptEntry.getValue().getKey()));
-                result = conceptEntry.getValue().validate(result);
+                result2 = new BeanValidationResult(conceptEntry.getKey().getId(), conceptEntry.getKey());
+                addResult(result2, "value", conceptEntry.getValue(), "does not equal concept key");
+                result2.addResult(conceptEntry.getValue().validate("value"));
             } else {
-                result = conceptEntry.getValue().validate(result);
+                result2 = new BeanValidationResult(conceptEntry.getKey().getId(), conceptEntry.getKey());
+                result2.addResult(conceptEntry.getValue().validate("value"));
             }
+
+            result.addResult(result2);
         }
-        return result;
+        return (result.isClean() ? null : result);
     }
 
     @Override
@@ -307,11 +302,7 @@ public class PfConceptContainer<C extends PfConcept, A extends PfNameVersion> ex
             return retVal;
         }
 
-        if (!conceptMap.equals(other.conceptMap)) {
-            return (conceptMap.hashCode() - other.conceptMap.hashCode());
-        }
-
-        return 0;
+        return PfUtils.compareMaps(conceptMap, other.conceptMap);
     }
 
     /**
@@ -351,7 +342,7 @@ public class PfConceptContainer<C extends PfConcept, A extends PfNameVersion> ex
     @Override
     public C get(final String conceptKeyName, final String conceptKeyVersion) {
         return new PfConceptGetterImpl<>((NavigableMap<PfConceptKey, C>) conceptMap).get(conceptKeyName,
-            conceptKeyVersion);
+                conceptKeyVersion);
     }
 
     @Override
@@ -362,7 +353,7 @@ public class PfConceptContainer<C extends PfConcept, A extends PfNameVersion> ex
     @Override
     public Set<C> getAll(final String conceptKeyName, final String conceptKeyVersion) {
         return new PfConceptGetterImpl<>((NavigableMap<PfConceptKey, C>) conceptMap).getAll(conceptKeyName,
-            conceptKeyVersion);
+                conceptKeyVersion);
     }
 
     /**
@@ -374,16 +365,16 @@ public class PfConceptContainer<C extends PfConcept, A extends PfNameVersion> ex
     private C getConceptNewInstance() {
         try {
             String conceptClassName =
-                ((ParameterizedType) getClass().getGenericSuperclass()).getActualTypeArguments()[0].getTypeName();
+                    ((ParameterizedType) getClass().getGenericSuperclass()).getActualTypeArguments()[0].getTypeName();
             return (C) Class.forName(conceptClassName).getDeclaredConstructor().newInstance();
         } catch (Exception ex) {
             throw new PfModelRuntimeException(Response.Status.INTERNAL_SERVER_ERROR,
-                "failed to instantiate instance of container concept class", ex);
+                    "failed to instantiate instance of container concept class", ex);
         }
     }
 
     private String findConceptField(final PfConceptKey conceptKey, final String keyFieldValue,
-        final PfNameVersion concept, final Function<PfNameVersion, String> fieldGetterFunction) {
+            final PfNameVersion concept, final Function<PfNameVersion, String> fieldGetterFunction) {
 
         String conceptField = fieldGetterFunction.apply(concept);
 
@@ -391,7 +382,7 @@ public class PfConceptContainer<C extends PfConcept, A extends PfNameVersion> ex
             return keyFieldValue;
         } else {
             throw new PfModelRuntimeException(Response.Status.BAD_REQUEST, "Key " + conceptKey.getId() + " field "
-                + keyFieldValue + " does not match the value " + conceptField + " in the concept field");
+                    + keyFieldValue + " does not match the value " + conceptField + " in the concept field");
         }
     }
 }