Use ValidationResult for models v2.0
[policy/models.git] / models-tosca / src / main / java / org / onap / policy / models / tosca / simple / concepts / JpaToscaPolicyType.java
index 7a5493c..665c79d 100644 (file)
@@ -2,7 +2,7 @@
  * ============LICENSE_START=======================================================
  * ONAP Policy Model
  * ================================================================================
- * Copyright (C) 2019 AT&T Intellectual Property. All rights reserved.
+ * Copyright (C) 2019-2020 AT&T Intellectual Property. All rights reserved.
  * Modifications Copyright (C) 2019-2020 Nordix Foundation.
  * ================================================================================
  * Licensed under the Apache License, Version 2.0 (the "License");
@@ -40,15 +40,14 @@ import lombok.Data;
 import lombok.EqualsAndHashCode;
 import lombok.NonNull;
 import org.apache.commons.collections4.CollectionUtils;
+import org.onap.policy.common.parameters.BeanValidationResult;
 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.PfValidationMessage;
-import org.onap.policy.models.base.PfValidationResult;
-import org.onap.policy.models.base.PfValidationResult.ValidationResult;
+import org.onap.policy.models.base.Validated;
 import org.onap.policy.models.tosca.authorative.concepts.ToscaPolicyType;
 import org.onap.policy.models.tosca.authorative.concepts.ToscaProperty;
 import org.onap.policy.models.tosca.utils.ToscaUtils;
@@ -188,86 +187,15 @@ public class JpaToscaPolicyType extends JpaToscaEntityType<ToscaPolicyType> impl
     }
 
     @Override
-    public PfValidationResult validate(@NonNull final PfValidationResult resultIn) {
-        PfValidationResult result = super.validate(resultIn);
+    public BeanValidationResult validate(String fieldName) {
+        BeanValidationResult result = super.validate(fieldName);
 
-        if (PfKey.NULL_KEY_VERSION.equals(getKey().getVersion())) {
-            result.addValidationMessage(new PfValidationMessage(getKey(), this.getClass(), ValidationResult.INVALID,
-                    "key version is a null version"));
-        }
-
-        if (properties != null) {
-            result = validateProperties(result);
-        }
-
-        if (targets != null) {
-            result = validateTargets(result);
-        }
-
-        if (triggers != null) {
-            result = validateTriggers(result);
-        }
+        result.addResult(validateKeyVersionNotNull("key", getKey()));
 
-        return result;
-    }
+        validateMap(result, "properties", properties, Validated::validateEntryValueNotNull);
+        validateList(result, "targets", targets, Validated::validateNotNull);
+        validateList(result, "triggers", triggers, Validated::validateNotNull);
 
-    /**
-     * Validate the policy properties.
-     *
-     * @param result The result of validations up to now
-     * @return the validation result
-     */
-    private PfValidationResult validateProperties(final PfValidationResult resultIn) {
-        PfValidationResult result = resultIn;
-
-        for (JpaToscaProperty property : properties.values()) {
-            if (property == null) {
-                result.addValidationMessage(new PfValidationMessage(getKey(), this.getClass(), ValidationResult.INVALID,
-                        "policy property may not be null "));
-            } else {
-                result = property.validate(result);
-            }
-        }
-        return result;
-    }
-
-    /**
-     * Validate the policy targets.
-     *
-     * @param result The result of validations up to now
-     * @return the validation result
-     */
-    private PfValidationResult validateTargets(final PfValidationResult resultIn) {
-        PfValidationResult result = resultIn;
-
-        for (PfConceptKey target : targets) {
-            if (target == null) {
-                result.addValidationMessage(new PfValidationMessage(getKey(), this.getClass(), ValidationResult.INVALID,
-                        "policy target may not be null "));
-            } else {
-                result = target.validate(result);
-            }
-        }
-        return result;
-    }
-
-    /**
-     * Validate the policy triggers.
-     *
-     * @param result The result of validations up to now
-     * @return the validation result
-     */
-    private PfValidationResult validateTriggers(final PfValidationResult resultIn) {
-        PfValidationResult result = resultIn;
-
-        for (JpaToscaTrigger trigger : triggers) {
-            if (trigger == null) {
-                result.addValidationMessage(new PfValidationMessage(getKey(), this.getClass(), ValidationResult.INVALID,
-                        "policy trigger may not be null "));
-            } else {
-                result = trigger.validate(result);
-            }
-        }
         return result;
     }