aboutsummaryrefslogtreecommitdiffstats
path: root/models-pdp
diff options
context:
space:
mode:
authorJim Hahn <jrh3@att.com>2019-12-06 10:22:48 -0500
committerJim Hahn <jrh3@att.com>2019-12-06 17:09:17 -0500
commit9780a369f50d5e1cad9e5a013c4cb353fa09f01d (patch)
tree9a83b1ceefb71fbcd15dd00713a6827f85578094 /models-pdp
parentdb85df62efbf70597d9f7e965f68b77f4ddc1f1f (diff)
Add models to support deltas to policy deployments
Change-Id: Ia5134e2ba4faeefe8bcc1333abe6acc57972c243 Issue-ID: POLICY-2274 Signed-off-by: Jim Hahn <jrh3@att.com>
Diffstat (limited to 'models-pdp')
-rw-r--r--models-pdp/src/main/java/org/onap/policy/models/pdp/concepts/DeploymentGroup.java116
-rw-r--r--models-pdp/src/main/java/org/onap/policy/models/pdp/concepts/DeploymentGroups.java73
-rw-r--r--models-pdp/src/main/java/org/onap/policy/models/pdp/concepts/DeploymentSubGroup.java82
-rw-r--r--models-pdp/src/test/java/org/onap/policy/models/pdp/concepts/DeploymentGroupTest.java194
-rw-r--r--models-pdp/src/test/java/org/onap/policy/models/pdp/concepts/DeploymentGroupsTest.java89
-rw-r--r--models-pdp/src/test/java/org/onap/policy/models/pdp/concepts/DeploymentSubGroupTest.java152
6 files changed, 706 insertions, 0 deletions
diff --git a/models-pdp/src/main/java/org/onap/policy/models/pdp/concepts/DeploymentGroup.java b/models-pdp/src/main/java/org/onap/policy/models/pdp/concepts/DeploymentGroup.java
new file mode 100644
index 000000000..d98f6ed30
--- /dev/null
+++ b/models-pdp/src/main/java/org/onap/policy/models/pdp/concepts/DeploymentGroup.java
@@ -0,0 +1,116 @@
+/*-
+ * ============LICENSE_START=======================================================
+ * Copyright (C) 2019 AT&T Intellectual Property.
+ * ================================================================================
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ *
+ * SPDX-License-Identifier: Apache-2.0
+ * ============LICENSE_END=========================================================
+ */
+
+package org.onap.policy.models.pdp.concepts;
+
+import java.util.ArrayList;
+import java.util.HashMap;
+import java.util.List;
+import java.util.Map;
+import lombok.Data;
+import lombok.NoArgsConstructor;
+import lombok.NonNull;
+import org.onap.policy.common.parameters.BeanValidationResult;
+import org.onap.policy.common.parameters.ObjectValidationResult;
+import org.onap.policy.common.parameters.ValidationResult;
+import org.onap.policy.common.parameters.ValidationStatus;
+import org.onap.policy.models.base.PfUtils;
+import org.onap.policy.models.pdp.concepts.DeploymentSubGroup.Action;
+
+/**
+ * Batch modification of a deployment group, which groups multiple DeploymentSubGroup
+ * entities together for a particular domain.
+ */
+@Data
+@NoArgsConstructor
+public class DeploymentGroup {
+ private static final String SUBGROUP_FIELD = "deploymentSubgroups";
+
+ private String name;
+ private List<DeploymentSubGroup> deploymentSubgroups;
+
+ /**
+ * Constructs the object, making a deep copy from the source.
+ *
+ * @param source source from which to copy fields
+ */
+ public DeploymentGroup(@NonNull DeploymentGroup source) {
+ this.name = source.name;
+ this.deploymentSubgroups =
+ PfUtils.mapList(source.deploymentSubgroups, DeploymentSubGroup::new, new ArrayList<>(0));
+ }
+
+ /**
+ * Validates that appropriate fields are populated for an incoming call to the PAP
+ * REST API.
+ *
+ * @return the validation result
+ */
+ public ValidationResult validatePapRest() {
+ BeanValidationResult result = new BeanValidationResult("group", this);
+
+ result.validateNotNull("name", name);
+ result.validateNotNullList(SUBGROUP_FIELD, deploymentSubgroups, DeploymentSubGroup::validatePapRest);
+
+ if (deploymentSubgroups != null && deploymentSubgroups.isEmpty()) {
+ result.addResult(new ObjectValidationResult(SUBGROUP_FIELD, deploymentSubgroups, ValidationStatus.INVALID,
+ "is empty"));
+ }
+
+ checkDuplicateSubgroups(result);
+
+ return result;
+ }
+
+ /**
+ * Checks for duplicate subgroups.
+ *
+ * @param result where to place validation results
+ */
+ private void checkDuplicateSubgroups(BeanValidationResult result) {
+ if (deploymentSubgroups == null || !result.isValid()) {
+ return;
+ }
+
+ /*
+ * Verify that if a subgroup appears more than once, then the second appearance is
+ * not a PATCH, as that would overwrite anything that has appeared before.
+ */
+ Map<String, Action> pdpType2action = new HashMap<>();
+
+ for (DeploymentSubGroup subgrp : deploymentSubgroups) {
+ Action action = subgrp.getAction();
+
+ pdpType2action.compute(subgrp.getPdpType(), (pdpType, curact) -> {
+
+ if (curact != null && action == Action.PATCH) {
+ BeanValidationResult subResult = new BeanValidationResult(pdpType, pdpType);
+ subResult.addResult(new ObjectValidationResult("action", action, ValidationStatus.INVALID,
+ "incompatible with previous action: " + curact));
+ BeanValidationResult subResult2 = new BeanValidationResult(SUBGROUP_FIELD, subgrp);
+ subResult2.addResult(subResult);
+ result.addResult(subResult2);
+ }
+
+ return action;
+ });
+ }
+ }
+}
diff --git a/models-pdp/src/main/java/org/onap/policy/models/pdp/concepts/DeploymentGroups.java b/models-pdp/src/main/java/org/onap/policy/models/pdp/concepts/DeploymentGroups.java
new file mode 100644
index 000000000..0d810d210
--- /dev/null
+++ b/models-pdp/src/main/java/org/onap/policy/models/pdp/concepts/DeploymentGroups.java
@@ -0,0 +1,73 @@
+/*
+ * ============LICENSE_START=======================================================
+ * ONAP Policy Models
+ * ================================================================================
+ * Copyright (C) 2019 AT&T Intellectual Property. All rights reserved.
+ * ================================================================================
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ * ============LICENSE_END=========================================================
+ */
+
+package org.onap.policy.models.pdp.concepts;
+
+import java.util.HashSet;
+import java.util.List;
+import java.util.Set;
+import lombok.Getter;
+import lombok.Setter;
+import lombok.ToString;
+import org.onap.policy.common.parameters.BeanValidationResult;
+import org.onap.policy.common.parameters.ObjectValidationResult;
+import org.onap.policy.common.parameters.ValidationResult;
+import org.onap.policy.common.parameters.ValidationStatus;
+
+/**
+ * Batch modification of a deployment groups via the PDP Group deployment REST API.
+ */
+@Getter
+@Setter
+@ToString
+public class DeploymentGroups {
+ private static final String GROUPS_FIELD = "groups";
+
+ private List<DeploymentGroup> groups;
+
+ /**
+ * Validates that appropriate fields are populated for an incoming call to the PAP
+ * REST API.
+ *
+ * @return the validation result
+ */
+ public ValidationResult validatePapRest() {
+ BeanValidationResult result = new BeanValidationResult(GROUPS_FIELD, this);
+
+ result.validateNotNullList(GROUPS_FIELD, groups, DeploymentGroup::validatePapRest);
+ if (!result.isValid()) {
+ return result;
+ }
+
+ // verify that the same group doesn't appear more than once
+ Set<String> sawGroup = new HashSet<>();
+ for (DeploymentGroup group : groups) {
+ String name = group.getName();
+ if (sawGroup.contains(name)) {
+ return new ObjectValidationResult(GROUPS_FIELD, name, ValidationStatus.INVALID, "duplicate group name");
+
+ } else {
+ sawGroup.add(name);
+ }
+ }
+
+ return result;
+ }
+}
diff --git a/models-pdp/src/main/java/org/onap/policy/models/pdp/concepts/DeploymentSubGroup.java b/models-pdp/src/main/java/org/onap/policy/models/pdp/concepts/DeploymentSubGroup.java
new file mode 100644
index 000000000..1a1fe228d
--- /dev/null
+++ b/models-pdp/src/main/java/org/onap/policy/models/pdp/concepts/DeploymentSubGroup.java
@@ -0,0 +1,82 @@
+/*-
+ * ============LICENSE_START=======================================================
+ * Copyright (C) 2019 AT&T Intellectual Property. All rights reserved.
+ * ================================================================================
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ *
+ * SPDX-License-Identifier: Apache-2.0
+ * ============LICENSE_END=========================================================
+ */
+
+package org.onap.policy.models.pdp.concepts;
+
+import java.util.ArrayList;
+import java.util.List;
+import lombok.Data;
+import lombok.NonNull;
+import org.onap.policy.common.parameters.BeanValidationResult;
+import org.onap.policy.common.parameters.ValidationResult;
+import org.onap.policy.models.base.PfUtils;
+import org.onap.policy.models.tosca.authorative.concepts.ToscaPolicyIdentifier;
+
+/**
+ * A deployment (i.e., set of policies) for all PDPs of the same pdp type running within a
+ * particular domain.
+ */
+@Data
+public class DeploymentSubGroup {
+
+ public enum Action {
+ POST, // all listed policies are to be added
+ DELETE, // all listed policies are to be deleted
+ PATCH // update the deployment so that the policies match exactly
+ }
+
+ private String pdpType;
+ private Action action;
+ private List<ToscaPolicyIdentifier> policies;
+
+ /**
+ * Constructs the object.
+ */
+ public DeploymentSubGroup() {
+ super();
+ }
+
+ /**
+ * Constructs the object, making a deep copy from the source.
+ *
+ * @param source source from which to copy fields
+ */
+ public DeploymentSubGroup(@NonNull final DeploymentSubGroup source) {
+ this.pdpType = source.pdpType;
+ this.action = source.action;
+ this.policies = PfUtils.mapList(source.policies, ToscaPolicyIdentifier::new, new ArrayList<>(0));
+ }
+
+ /**
+ * Validates that appropriate fields are populated for an incoming call to the PAP
+ * REST API.
+ *
+ * @return the validation result
+ */
+ public ValidationResult validatePapRest() {
+ BeanValidationResult result = new BeanValidationResult("group", this);
+
+ result.validateNotNull("pdpType", pdpType);
+ result.validateNotNull("action", action);
+ result.validateNotNullList("policies", policies, ToscaPolicyIdentifier::validatePapRest);
+
+ return result;
+ }
+}
diff --git a/models-pdp/src/test/java/org/onap/policy/models/pdp/concepts/DeploymentGroupTest.java b/models-pdp/src/test/java/org/onap/policy/models/pdp/concepts/DeploymentGroupTest.java
new file mode 100644
index 000000000..a74029e10
--- /dev/null
+++ b/models-pdp/src/test/java/org/onap/policy/models/pdp/concepts/DeploymentGroupTest.java
@@ -0,0 +1,194 @@
+/*-
+ * ============LICENSE_START=======================================================
+ * ONAP Policy Models
+ * ================================================================================
+ * Copyright (C) 2019 AT&T Intellectual Property. All rights reserved.
+ * ================================================================================
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ * ============LICENSE_END=========================================================
+ */
+
+package org.onap.policy.models.pdp.concepts;
+
+import static org.assertj.core.api.Assertions.assertThatThrownBy;
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertFalse;
+import static org.junit.Assert.assertNotNull;
+import static org.junit.Assert.assertNull;
+import static org.junit.Assert.assertTrue;
+
+import java.util.Arrays;
+import java.util.Collections;
+import org.junit.Test;
+import org.onap.policy.common.parameters.ValidationResult;
+import org.onap.policy.models.pdp.concepts.DeploymentSubGroup.Action;
+
+/**
+ * Test methods not tested by {@link ModelsTest}.
+ */
+public class DeploymentGroupTest {
+ private static final String NAME = "my-name";
+ private static final String PDP_TYPE1 = "type-1";
+ private static final String PDP_TYPE2 = "type-2";
+ private static final String PDP_TYPE3 = "type-3";
+
+ @Test
+ public void testCopyConstructor() {
+ assertThatThrownBy(() -> new DeploymentGroup(null)).isInstanceOf(NullPointerException.class);
+
+ DeploymentGroup orig = new DeploymentGroup();
+
+ // verify with null values
+ assertEquals("DeploymentGroup(name=null, deploymentSubgroups=[])", new DeploymentGroup(orig).toString());
+
+ // verify with all values
+ orig.setName(NAME);
+
+ DeploymentSubGroup sub1 = new DeploymentSubGroup();
+ DeploymentSubGroup sub2 = new DeploymentSubGroup();
+ orig.setDeploymentSubgroups(Arrays.asList(sub1, sub2));
+
+ assertEquals("DeploymentGroup(name=my-name, "
+ + "deploymentSubgroups=[DeploymentSubGroup(pdpType=null, action=null, policies=[]), "
+ + "DeploymentSubGroup(pdpType=null, action=null, policies=[])])",
+ new DeploymentGroup(orig).toString());
+ }
+
+ @Test
+ public void testHashCode() {
+ DeploymentGroup group = new DeploymentGroup();
+ group.setName("A");
+ int hash = group.hashCode();
+
+ assertEquals(hash, group.hashCode());
+
+ group.setName("B");
+ assertTrue(hash != group.hashCode());
+ }
+
+ @Test
+ public void testValidatePapRest() {
+ DeploymentGroup group = new DeploymentGroup();
+ group.setName(NAME);
+
+ DeploymentSubGroup subgroup1 = new DeploymentSubGroup();
+ subgroup1.setPdpType(PDP_TYPE1);
+ subgroup1.setAction(Action.PATCH);
+ subgroup1.setPolicies(Collections.emptyList());
+
+ DeploymentSubGroup subgroup2 = new DeploymentSubGroup(subgroup1);
+ subgroup2.setPdpType(PDP_TYPE2);
+
+ DeploymentSubGroup subgroup3 = new DeploymentSubGroup(subgroup1);
+ subgroup3.setPdpType(PDP_TYPE3);
+
+ group.setDeploymentSubgroups(Arrays.asList(subgroup1, subgroup2, subgroup3));
+
+ // valid
+ assertValid(group);
+
+ // null name
+ DeploymentGroup group2 = new DeploymentGroup(group);
+ group2.setName(null);
+ assertInvalid(group2);
+
+ // null subgroup list
+ group2 = new DeploymentGroup(group);
+ group2.setDeploymentSubgroups(null);
+ assertInvalid(group2);
+
+ // empty subgroup list
+ group2 = new DeploymentGroup(group);
+ group2.setDeploymentSubgroups(Collections.emptyList());
+ assertInvalid(group2);
+
+ // null subgroup
+ group2 = new DeploymentGroup(group);
+ group2.setDeploymentSubgroups(Arrays.asList(subgroup1, null));
+ assertInvalid(group2);
+
+ // invalid subgroup
+ group2 = new DeploymentGroup(group);
+ DeploymentSubGroup subgroupX = new DeploymentSubGroup(subgroup1);
+ subgroupX.setPdpType(null);
+ group2.setDeploymentSubgroups(Arrays.asList(subgroupX));
+ assertInvalid(group2);
+ }
+
+ @Test
+ public void testCheckDuplicateSubgroups() {
+ DeploymentGroup group = new DeploymentGroup();
+ group.setName(NAME);
+
+ DeploymentSubGroup subgroup1 = new DeploymentSubGroup();
+ subgroup1.setPdpType(PDP_TYPE1);
+ subgroup1.setAction(Action.POST);
+ subgroup1.setPolicies(Collections.emptyList());
+
+ DeploymentSubGroup subgroup2 = new DeploymentSubGroup(subgroup1);
+ subgroup2.setPdpType(PDP_TYPE2);
+ subgroup2.setAction(Action.PATCH);
+
+ DeploymentSubGroup subgroup3 = new DeploymentSubGroup(subgroup1);
+ subgroup3.setPdpType(PDP_TYPE3);
+ subgroup3.setAction(Action.DELETE);
+
+ group.setDeploymentSubgroups(Arrays.asList(subgroup1, subgroup2, subgroup3));
+
+ // no duplicates
+ assertValid(group);
+
+ /*
+ * Allowed duplicates
+ */
+ DeploymentSubGroup subgroup1b = new DeploymentSubGroup(subgroup1);
+ subgroup1b.setAction(Action.POST);
+
+ DeploymentSubGroup subgroup1c = new DeploymentSubGroup(subgroup1);
+ subgroup1c.setAction(Action.DELETE);
+
+ DeploymentSubGroup subgroup1d = new DeploymentSubGroup(subgroup1);
+ subgroup1d.setAction(Action.DELETE);
+
+ group.setDeploymentSubgroups(
+ Arrays.asList(subgroup1, subgroup2, subgroup3, subgroup1b, subgroup1c, subgroup1d));
+
+ // still ok
+ assertValid(group);
+
+ /*
+ * Not allowed
+ */
+ DeploymentSubGroup subgroup1e = new DeploymentSubGroup(subgroup1);
+ subgroup1e.setAction(Action.PATCH);
+
+ group.setDeploymentSubgroups(
+ Arrays.asList(subgroup1, subgroup2, subgroup3, subgroup1b, subgroup1c, subgroup1d, subgroup1e));
+
+ assertInvalid(group);
+ }
+
+ private void assertValid(DeploymentGroup group) {
+ ValidationResult result = group.validatePapRest();
+ assertNotNull(result);
+ assertNull(result.getResult());
+ assertTrue(result.isValid());
+ }
+
+ private void assertInvalid(DeploymentGroup group) {
+ ValidationResult result = group.validatePapRest();
+ assertNotNull(result);
+ assertFalse(result.isValid());
+ assertNotNull(result.getResult());
+ }
+}
diff --git a/models-pdp/src/test/java/org/onap/policy/models/pdp/concepts/DeploymentGroupsTest.java b/models-pdp/src/test/java/org/onap/policy/models/pdp/concepts/DeploymentGroupsTest.java
new file mode 100644
index 000000000..18b13759a
--- /dev/null
+++ b/models-pdp/src/test/java/org/onap/policy/models/pdp/concepts/DeploymentGroupsTest.java
@@ -0,0 +1,89 @@
+/*
+ * ============LICENSE_START=======================================================
+ * ONAP Policy Models
+ * ================================================================================
+ * Copyright (C) 2019 AT&T Intellectual Property. All rights reserved.
+ * ================================================================================
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ * ============LICENSE_END=========================================================
+ */
+
+package org.onap.policy.models.pdp.concepts;
+
+import static org.junit.Assert.assertFalse;
+import static org.junit.Assert.assertNotNull;
+import static org.junit.Assert.assertNull;
+import static org.junit.Assert.assertTrue;
+
+import java.util.Arrays;
+import java.util.Collections;
+import org.junit.Test;
+import org.onap.policy.common.parameters.ValidationResult;
+import org.onap.policy.models.pdp.concepts.DeploymentSubGroup.Action;
+
+public class DeploymentGroupsTest {
+
+ @Test
+ public void testValidatePapRest_toMapList() {
+ DeploymentGroup group1 = new DeploymentGroup();
+ group1.setName("group-1");
+
+ DeploymentSubGroup subgrp = new DeploymentSubGroup();
+ subgrp.setPdpType("pdp-type");
+ subgrp.setAction(Action.DELETE);
+ subgrp.setPolicies(Collections.emptyList());
+
+ group1.setDeploymentSubgroups(Arrays.asList(subgrp));
+
+ DeploymentGroup group2 = new DeploymentGroup();
+ group2.setName("group-2");
+ group2.setDeploymentSubgroups(Arrays.asList(subgrp));
+
+ DeploymentGroups groups = new DeploymentGroups();
+ groups.setGroups(Arrays.asList(group1, group2));
+
+ // valid
+ ValidationResult result = groups.validatePapRest();
+ assertNotNull(result);
+ assertNull(result.getResult());
+ assertTrue(result.isValid());
+
+ // null group list
+ groups = new DeploymentGroups();
+ groups.setGroups(null);
+ assertInvalid(groups);
+
+ // null group
+ groups = new DeploymentGroups();
+ groups.setGroups(Arrays.asList(group1, null));
+ assertInvalid(groups);
+
+ // invalid group
+ DeploymentGroup groupX = new DeploymentGroup(group1);
+ groupX.setName(null);
+ groups.setGroups(Arrays.asList(group1, groupX));
+ assertInvalid(groups);
+
+ // duplicate groups
+ groups = new DeploymentGroups();
+ groups.setGroups(Arrays.asList(group1, group2, group1));
+ assertInvalid(groups);
+ }
+
+ private void assertInvalid(DeploymentGroups groups) {
+ ValidationResult result = groups.validatePapRest();
+ assertNotNull(result);
+ assertFalse(result.isValid());
+ assertNotNull(result.getResult());
+ }
+}
diff --git a/models-pdp/src/test/java/org/onap/policy/models/pdp/concepts/DeploymentSubGroupTest.java b/models-pdp/src/test/java/org/onap/policy/models/pdp/concepts/DeploymentSubGroupTest.java
new file mode 100644
index 000000000..511d88f27
--- /dev/null
+++ b/models-pdp/src/test/java/org/onap/policy/models/pdp/concepts/DeploymentSubGroupTest.java
@@ -0,0 +1,152 @@
+/*-
+ * ============LICENSE_START=======================================================
+ * ONAP Policy Models
+ * ================================================================================
+ * Copyright (C) 2019 AT&T Intellectual Property. All rights reserved.
+ * ================================================================================
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ * ============LICENSE_END=========================================================
+ */
+
+package org.onap.policy.models.pdp.concepts;
+
+import static org.assertj.core.api.Assertions.assertThatThrownBy;
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertFalse;
+import static org.junit.Assert.assertNotNull;
+import static org.junit.Assert.assertNull;
+import static org.junit.Assert.assertTrue;
+
+import java.util.Arrays;
+import org.junit.Test;
+import org.onap.policy.common.parameters.ValidationResult;
+import org.onap.policy.common.utils.coder.Coder;
+import org.onap.policy.common.utils.coder.CoderException;
+import org.onap.policy.common.utils.coder.StandardCoder;
+import org.onap.policy.models.pdp.concepts.DeploymentSubGroup.Action;
+import org.onap.policy.models.tosca.authorative.concepts.ToscaPolicyIdentifier;
+
+/**
+ * Test methods not tested by {@link ModelsTest}.
+ */
+public class DeploymentSubGroupTest {
+ private static final String VERSION_300 = "3.0.0";
+ private static final Coder coder = new StandardCoder();
+
+ @Test
+ public void testCopyConstructor() {
+ assertThatThrownBy(() -> new DeploymentSubGroup(null)).isInstanceOf(NullPointerException.class);
+
+ final DeploymentSubGroup orig = new DeploymentSubGroup();
+
+ // verify with null values
+ assertEquals("DeploymentSubGroup(pdpType=null, action=null, policies=[])",
+ new DeploymentSubGroup(orig).toString());
+
+ orig.setPdpType("my-type");
+ orig.setAction(Action.POST);
+
+ final ToscaPolicyIdentifier pol1 = new ToscaPolicyIdentifier();
+ pol1.setName("policy-A");
+ pol1.setVersion("1.0.0");
+ final ToscaPolicyIdentifier pol2 = new ToscaPolicyIdentifier();
+ pol2.setName("policy-B");
+ pol1.setVersion("2.0.0");
+ orig.setPolicies(Arrays.asList(pol1, pol2));
+
+ assertEquals(orig.toString(), new DeploymentSubGroup(orig).toString());
+ }
+
+ @Test
+ public void testValidatePapRest() throws Exception {
+ DeploymentSubGroup subgrp = new DeploymentSubGroup();
+
+ subgrp.setPdpType("pdp-type");
+ subgrp.setAction(Action.PATCH);
+ subgrp.setPolicies(Arrays.asList(makeIdent("policy-X", "4.0.0", ToscaPolicyIdentifier.class)));
+
+ // valid
+ ValidationResult result = subgrp.validatePapRest();
+ assertNotNull(result);
+ assertTrue(result.isValid());
+ assertNull(result.getResult());
+
+ // null pdp type
+ DeploymentSubGroup sub2 = new DeploymentSubGroup(subgrp);
+ sub2.setPdpType(null);
+ assertInvalid(sub2);
+
+ // null action
+ sub2 = new DeploymentSubGroup(subgrp);
+ sub2.setAction(null);
+ assertInvalid(sub2);
+
+ // null policies
+ sub2 = new DeploymentSubGroup(subgrp);
+ sub2.setPolicies(null);
+ assertInvalid(sub2);
+
+ // null policy item
+ sub2 = new DeploymentSubGroup(subgrp);
+ sub2.getPolicies().set(0, null);
+ assertInvalid(sub2);
+
+ // invalid policy item
+ sub2 = new DeploymentSubGroup(subgrp);
+ sub2.getPolicies().set(0, makeIdent(null, VERSION_300, ToscaPolicyIdentifier.class));
+ assertInvalid(sub2);
+ }
+
+ private void assertInvalid(DeploymentSubGroup sub2) {
+ ValidationResult result = sub2.validatePapRest();
+ assertNotNull(result);
+ assertFalse(result.isValid());
+ assertNotNull(result.getResult());
+ }
+
+ /**
+ * Makes an identifier. Uses JSON which does no error checking.
+ *
+ * @param name name to put into the identifier
+ * @param version version to put into the identifier
+ * @param clazz type of identifier to create
+ * @return a new identifier
+ * @throws CoderException if the JSON cannot be decoded
+ */
+ private <T> T makeIdent(String name, String version, Class<T> clazz) throws CoderException {
+ StringBuilder bldr = new StringBuilder();
+ bldr.append("{");
+
+ if (name != null) {
+ bldr.append("'name':'");
+ bldr.append(name);
+ bldr.append("'");
+ }
+
+ if (version != null) {
+ if (name != null) {
+ bldr.append(',');
+ }
+
+ bldr.append("'version':'");
+ bldr.append(version);
+ bldr.append("'");
+ }
+
+ bldr.append("}");
+
+ String json = bldr.toString().replace('\'', '"');
+
+ return coder.decode(json, clazz);
+ }
+}