Merge "Add Service Template TOSCA handling"
[policy/models.git] / models-pdp / src / main / java / org / onap / policy / models / pdp / concepts / PdpGroups.java
index 06194ea..6729d7e 100644 (file)
@@ -3,6 +3,7 @@
  * ONAP Policy Models
  * ================================================================================
  * Copyright (C) 2019 AT&T Intellectual Property. All rights reserved.
+ * Modifications Copyright (C) 2020 Nordix Foundation.
  * ================================================================================
  * Licensed under the Apache License, Version 2.0 (the "License");
  * you may not use this file except in compliance with the License.
@@ -66,20 +67,33 @@ public class PdpGroups {
      * @return the validation result
      */
     public ValidationResult validatePapRest() {
-        BeanValidationResult result = new BeanValidationResult(GROUPS_FIELD, this);
-
-        result.validateNotNullList(GROUPS_FIELD, groups, PdpGroup::validatePapRest);
+        ValidationResult result = new BeanValidationResult(GROUPS_FIELD, this);
+        ((BeanValidationResult) result).validateNotNullList(GROUPS_FIELD, groups,
+            (PdpGroup pdpGroup) -> pdpGroup.validatePapRest(false));
         if (!result.isValid()) {
             return result;
         }
 
         // verify that the same group doesn't appear more than once
-        List<String> names = groups.stream().map(PdpGroup::getName).collect(Collectors.toList());
-        if (groups.size() == new HashSet<>(names).size()) {
-            return result;
-        }
+        return checkForDuplicateGroups(result);
+    }
 
-        // different sizes implies duplicates
-        return new ObjectValidationResult(GROUPS_FIELD, names, ValidationStatus.INVALID, "duplicate group names");
+    /**
+     * Validates that there are no duplicate PdpGroups with the same name.
+     *
+     * @param result the validation result
+     * @return the validation result
+     */
+    public ValidationResult checkForDuplicateGroups(ValidationResult result) {
+        if (null == groups) {
+            result.setResult(ValidationStatus.INVALID, "is null");
+        } else {
+            List<String> names = groups.stream().map(PdpGroup::getName).collect(Collectors.toList());
+            if (groups.size() != new HashSet<>(names).size()) {
+                result =
+                    new ObjectValidationResult(GROUPS_FIELD, names, ValidationStatus.INVALID, "duplicate group names");
+            }
+        }
+        return result;
     }
 }