diff options
author | a.sreekumar <ajith.sreekumar@est.tech> | 2020-01-21 17:07:50 +0000 |
---|---|---|
committer | a.sreekumar <ajith.sreekumar@est.tech> | 2020-01-22 15:11:27 +0000 |
commit | 2a7cbf33a8b53bb83d43bf1adca7e6a8658cb262 (patch) | |
tree | 73d5216fa7a752ace2b26769251eaf7032293e04 | |
parent | af4d20eb737dc7670e811d5272416eec06da1960 (diff) |
Handling supported policy type during PdpGroup Update
Change-Id: I469125c232af9d78a55c3dfa71cb701cb3864015
Issue-ID: POLICY-2023
Signed-off-by: a.sreekumar <ajith.sreekumar@est.tech>
5 files changed, 109 insertions, 25 deletions
diff --git a/models-pdp/src/main/java/org/onap/policy/models/pdp/concepts/PdpGroup.java b/models-pdp/src/main/java/org/onap/policy/models/pdp/concepts/PdpGroup.java index 6d5f804f8..b6886bef1 100644 --- a/models-pdp/src/main/java/org/onap/policy/models/pdp/concepts/PdpGroup.java +++ b/models-pdp/src/main/java/org/onap/policy/models/pdp/concepts/PdpGroup.java @@ -1,6 +1,6 @@ /*- * ============LICENSE_START======================================================= - * Copyright (C) 2019 Nordix Foundation. + * Copyright (C) 2019-2020 Nordix Foundation. * Modifications Copyright (C) 2019 AT&T Intellectual Property. * ================================================================================ * Licensed under the Apache License, Version 2.0 (the "License"); @@ -81,9 +81,10 @@ public class PdpGroup implements PfNameVersion, Comparable<PdpGroup> { /** * Validates that appropriate fields are populated for an incoming call to the PAP REST API. * + * @param updateGroupFlow if the operation is pdp group update * @return the validation result */ - public ValidationResult validatePapRest() { + public ValidationResult validatePapRest(boolean updateGroupFlow) { BeanValidationResult result = new BeanValidationResult("group", this); /* @@ -91,7 +92,8 @@ public class PdpGroup implements PfNameVersion, Comparable<PdpGroup> { */ result.validateNotNull("name", name); - result.validateNotNullList(SUBGROUP_FIELD, pdpSubgroups, PdpSubGroup::validatePapRest); + result.validateNotNullList(SUBGROUP_FIELD, pdpSubgroups, + (PdpSubGroup pdpSubGroup) -> pdpSubGroup.validatePapRest(updateGroupFlow)); if (pdpSubgroups != null && pdpSubgroups.isEmpty()) { result.addResult(new ObjectValidationResult(SUBGROUP_FIELD, pdpSubgroups, ValidationStatus.INVALID, diff --git a/models-pdp/src/main/java/org/onap/policy/models/pdp/concepts/PdpGroups.java b/models-pdp/src/main/java/org/onap/policy/models/pdp/concepts/PdpGroups.java index 06194eaba..269130b07 100644 --- a/models-pdp/src/main/java/org/onap/policy/models/pdp/concepts/PdpGroups.java +++ b/models-pdp/src/main/java/org/onap/policy/models/pdp/concepts/PdpGroups.java @@ -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,28 @@ 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 + return checkForDuplicateGroups(result); + } + + /** + * 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) { List<String> names = groups.stream().map(PdpGroup::getName).collect(Collectors.toList()); - if (groups.size() == new HashSet<>(names).size()) { - return result; + if (groups.size() != new HashSet<>(names).size()) { + result = new ObjectValidationResult(GROUPS_FIELD, names, ValidationStatus.INVALID, "duplicate group names"); } - - // different sizes implies duplicates - return new ObjectValidationResult(GROUPS_FIELD, names, ValidationStatus.INVALID, "duplicate group names"); + return result; } } diff --git a/models-pdp/src/main/java/org/onap/policy/models/pdp/concepts/PdpSubGroup.java b/models-pdp/src/main/java/org/onap/policy/models/pdp/concepts/PdpSubGroup.java index 236cc858a..e3e33b454 100644 --- a/models-pdp/src/main/java/org/onap/policy/models/pdp/concepts/PdpSubGroup.java +++ b/models-pdp/src/main/java/org/onap/policy/models/pdp/concepts/PdpSubGroup.java @@ -1,6 +1,6 @@ /*- * ============LICENSE_START======================================================= - * Copyright (C) 2019 Nordix Foundation. + * Copyright (C) 2019-2020 Nordix Foundation. * Modifications Copyright (C) 2019 AT&T Intellectual Property. All rights reserved. * ================================================================================ * Licensed under the Apache License, Version 2.0 (the "License"); @@ -78,19 +78,23 @@ public class PdpSubGroup { * Validates that appropriate fields are populated for an incoming call to the PAP * REST API. * + * @param updateGroupFlow if the operation is pdp group update * @return the validation result */ - public ValidationResult validatePapRest() { + public ValidationResult validatePapRest(boolean updateGroupFlow) { BeanValidationResult result = new BeanValidationResult("group", this); result.validateNotNull("pdpType", pdpType); - result.validateNotNullList("supportedPolicyTypes", supportedPolicyTypes, - ToscaPolicyTypeIdentifier::validatePapRest); - result.validateNotNullList("policies", policies, ToscaPolicyIdentifier::validatePapRest); + // When doing PdpGroup Update operation, supported policy types and policies doesn't have to be validated. + if (!updateGroupFlow) { + result.validateNotNullList("policies", policies, ToscaPolicyIdentifier::validatePapRest); + result.validateNotNullList("supportedPolicyTypes", supportedPolicyTypes, + ToscaPolicyTypeIdentifier::validatePapRest); - if (supportedPolicyTypes != null && supportedPolicyTypes.isEmpty()) { - result.addResult(new ObjectValidationResult("supportedPolicyTypes", supportedPolicyTypes, - ValidationStatus.INVALID, "empty list")); + if (supportedPolicyTypes != null && supportedPolicyTypes.isEmpty()) { + result.addResult(new ObjectValidationResult("supportedPolicyTypes", supportedPolicyTypes, + ValidationStatus.INVALID, "empty list")); + } } if (desiredInstanceCount <= 0) { diff --git a/models-pdp/src/test/java/org/onap/policy/models/pdp/concepts/PdpGroupTest.java b/models-pdp/src/test/java/org/onap/policy/models/pdp/concepts/PdpGroupTest.java index a282b7dba..4f11079c2 100644 --- a/models-pdp/src/test/java/org/onap/policy/models/pdp/concepts/PdpGroupTest.java +++ b/models-pdp/src/test/java/org/onap/policy/models/pdp/concepts/PdpGroupTest.java @@ -3,7 +3,7 @@ * ONAP Policy Models * ================================================================================ * Copyright (C) 2019 AT&T Intellectual Property. All rights reserved. - * Modifications Copyright (C) 2019 Nordix Foundation. + * Modifications Copyright (C) 2019-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. @@ -121,6 +121,42 @@ public class PdpGroupTest { } @Test + public void testValidatePapRest_GroupUpdateFlow() { + PdpGroup group = new PdpGroup(); + group.setName(NAME); + // with supported policy type and policies + PdpSubGroup subgroup1 = new PdpSubGroup(); + subgroup1.setDesiredInstanceCount(1); + subgroup1.setPdpType(PDP_TYPE1); + subgroup1.setSupportedPolicyTypes(Arrays.asList(new ToscaPolicyTypeIdentifier("a-type-name", "3.2.1"))); + subgroup1.setPolicies(Collections.emptyList()); + group.setPdpSubgroups(Arrays.asList(subgroup1)); + + ValidationResult result = group.validatePapRest(true); + assertNotNull(result); + assertTrue(result.isValid()); + assertNull(result.getResult()); + + // without supported policy type and policies + PdpSubGroup subgroup2 = new PdpSubGroup(); + subgroup2.setDesiredInstanceCount(1); + subgroup2.setPdpType(PDP_TYPE1); + group.setPdpSubgroups(Arrays.asList(subgroup2)); + + // valid + result = group.validatePapRest(true); + assertNotNull(result); + assertTrue(result.isValid()); + assertNull(result.getResult()); + + // invalid + result = group.validatePapRest(false); + assertNotNull(result); + assertFalse(result.isValid()); + assertNotNull(result.getResult()); + } + + @Test public void testValidatePapRest() { PdpGroup group = new PdpGroup(); group.setName(NAME); @@ -140,7 +176,7 @@ public class PdpGroupTest { group.setPdpSubgroups(Arrays.asList(subgroup1, subgroup2, subgroup3)); // valid - ValidationResult result = group.validatePapRest(); + ValidationResult result = group.validatePapRest(false); assertNotNull(result); assertTrue(result.isValid()); assertNull(result.getResult()); @@ -179,7 +215,7 @@ public class PdpGroupTest { } private void assertInvalid(PdpGroup group) { - ValidationResult result = group.validatePapRest(); + ValidationResult result = group.validatePapRest(false); assertNotNull(result); assertFalse(result.isValid()); assertNotNull(result.getResult()); diff --git a/models-pdp/src/test/java/org/onap/policy/models/pdp/concepts/PdpSubGroupTest.java b/models-pdp/src/test/java/org/onap/policy/models/pdp/concepts/PdpSubGroupTest.java index e24e8e2d6..ba55426c7 100644 --- a/models-pdp/src/test/java/org/onap/policy/models/pdp/concepts/PdpSubGroupTest.java +++ b/models-pdp/src/test/java/org/onap/policy/models/pdp/concepts/PdpSubGroupTest.java @@ -3,7 +3,7 @@ * ONAP Policy Models * ================================================================================ * Copyright (C) 2019 AT&T Intellectual Property. All rights reserved. - * Modifications Copyright (C) 2019 Nordix Foundation. + * Modifications Copyright (C) 2019-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. @@ -91,6 +91,39 @@ public class PdpSubGroupTest { } @Test + public void testValidatePapRest_GroupUpdateFlow() throws Exception { + PdpSubGroup subgrp = new PdpSubGroup(); + // with supported policy type and policies + subgrp.setDesiredInstanceCount(1); + subgrp.setPdpType("pdp-type"); + subgrp.setSupportedPolicyTypes( + Arrays.asList(makeIdent("type-X", VERSION_300, ToscaPolicyTypeIdentifier.class))); + subgrp.setPolicies(Arrays.asList(makeIdent("policy-X", "4.0.0", ToscaPolicyIdentifier.class))); + + ValidationResult result = subgrp.validatePapRest(false); + assertNotNull(result); + assertTrue(result.isValid()); + assertNull(result.getResult()); + + // without supported policy type and policies + PdpSubGroup subgrp2 = new PdpSubGroup(); + subgrp2.setDesiredInstanceCount(1); + subgrp2.setPdpType("pdp-type"); + + // valid + result = subgrp2.validatePapRest(true); + assertNotNull(result); + assertTrue(result.isValid()); + assertNull(result.getResult()); + + // invalid + result = subgrp2.validatePapRest(false); + assertNotNull(result); + assertFalse(result.isValid()); + assertNotNull(result.getResult()); + } + + @Test public void testValidatePapRest() throws Exception { PdpSubGroup subgrp = new PdpSubGroup(); @@ -101,7 +134,7 @@ public class PdpSubGroupTest { subgrp.setPolicies(Arrays.asList(makeIdent("policy-X", "4.0.0", ToscaPolicyIdentifier.class))); // valid - ValidationResult result = subgrp.validatePapRest(); + ValidationResult result = subgrp.validatePapRest(false); assertNotNull(result); assertTrue(result.isValid()); assertNull(result.getResult()); @@ -158,7 +191,7 @@ public class PdpSubGroupTest { } private void assertInvalid(PdpSubGroup sub2) { - ValidationResult result = sub2.validatePapRest(); + ValidationResult result = sub2.validatePapRest(false); assertNotNull(result); assertFalse(result.isValid()); assertNotNull(result.getResult()); |