diff options
Diffstat (limited to 'models-pdp/src/test')
3 files changed, 435 insertions, 0 deletions
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); + } +} |