2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2019 AT&T Intellectual Property. All rights reserved.
6 * ================================================================================
7 * Licensed under the Apache License, Version 2.0 (the "License");
8 * you may not use this file except in compliance with the License.
9 * You may obtain a copy of the License at
11 * http://www.apache.org/licenses/LICENSE-2.0
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS,
15 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
18 * ============LICENSE_END=========================================================
21 package org.onap.policy.models.pdp.concepts;
23 import static org.assertj.core.api.Assertions.assertThatThrownBy;
24 import static org.junit.Assert.assertEquals;
25 import static org.junit.Assert.assertFalse;
26 import static org.junit.Assert.assertNotNull;
27 import static org.junit.Assert.assertNull;
28 import static org.junit.Assert.assertTrue;
30 import java.util.Arrays;
31 import java.util.Collections;
32 import org.junit.Test;
33 import org.onap.policy.common.parameters.ValidationResult;
34 import org.onap.policy.models.pdp.concepts.DeploymentSubGroup.Action;
37 * Test methods not tested by {@link ModelsTest}.
39 public class DeploymentGroupTest {
40 private static final String NAME = "my-name";
41 private static final String PDP_TYPE1 = "type-1";
42 private static final String PDP_TYPE2 = "type-2";
43 private static final String PDP_TYPE3 = "type-3";
46 public void testCopyConstructor() {
47 assertThatThrownBy(() -> new DeploymentGroup(null)).isInstanceOf(NullPointerException.class);
49 DeploymentGroup orig = new DeploymentGroup();
51 // verify with null values
52 assertEquals("DeploymentGroup(name=null, deploymentSubgroups=[])", new DeploymentGroup(orig).toString());
54 // verify with all values
57 DeploymentSubGroup sub1 = new DeploymentSubGroup();
58 DeploymentSubGroup sub2 = new DeploymentSubGroup();
59 orig.setDeploymentSubgroups(Arrays.asList(sub1, sub2));
61 assertEquals("DeploymentGroup(name=my-name, "
62 + "deploymentSubgroups=[DeploymentSubGroup(pdpType=null, action=null, policies=[]), "
63 + "DeploymentSubGroup(pdpType=null, action=null, policies=[])])",
64 new DeploymentGroup(orig).toString());
68 public void testHashCode() {
69 DeploymentGroup group = new DeploymentGroup();
71 int hash = group.hashCode();
73 assertEquals(hash, group.hashCode());
76 assertTrue(hash != group.hashCode());
80 public void testValidatePapRest() {
81 DeploymentGroup group = new DeploymentGroup();
84 DeploymentSubGroup subgroup1 = new DeploymentSubGroup();
85 subgroup1.setPdpType(PDP_TYPE1);
86 subgroup1.setAction(Action.PATCH);
87 subgroup1.setPolicies(Collections.emptyList());
89 DeploymentSubGroup subgroup2 = new DeploymentSubGroup(subgroup1);
90 subgroup2.setPdpType(PDP_TYPE2);
92 DeploymentSubGroup subgroup3 = new DeploymentSubGroup(subgroup1);
93 subgroup3.setPdpType(PDP_TYPE3);
95 group.setDeploymentSubgroups(Arrays.asList(subgroup1, subgroup2, subgroup3));
101 DeploymentGroup group2 = new DeploymentGroup(group);
102 group2.setName(null);
103 assertInvalid(group2);
105 // null subgroup list
106 group2 = new DeploymentGroup(group);
107 group2.setDeploymentSubgroups(null);
108 assertInvalid(group2);
110 // empty subgroup list
111 group2 = new DeploymentGroup(group);
112 group2.setDeploymentSubgroups(Collections.emptyList());
113 assertInvalid(group2);
116 group2 = new DeploymentGroup(group);
117 group2.setDeploymentSubgroups(Arrays.asList(subgroup1, null));
118 assertInvalid(group2);
121 group2 = new DeploymentGroup(group);
122 DeploymentSubGroup subgroupX = new DeploymentSubGroup(subgroup1);
123 subgroupX.setPdpType(null);
124 group2.setDeploymentSubgroups(Arrays.asList(subgroupX));
125 assertInvalid(group2);
129 public void testCheckDuplicateSubgroups() {
130 DeploymentGroup group = new DeploymentGroup();
133 DeploymentSubGroup subgroup1 = new DeploymentSubGroup();
134 subgroup1.setPdpType(PDP_TYPE1);
135 subgroup1.setAction(Action.POST);
136 subgroup1.setPolicies(Collections.emptyList());
138 DeploymentSubGroup subgroup2 = new DeploymentSubGroup(subgroup1);
139 subgroup2.setPdpType(PDP_TYPE2);
140 subgroup2.setAction(Action.PATCH);
142 DeploymentSubGroup subgroup3 = new DeploymentSubGroup(subgroup1);
143 subgroup3.setPdpType(PDP_TYPE3);
144 subgroup3.setAction(Action.DELETE);
146 group.setDeploymentSubgroups(Arrays.asList(subgroup1, subgroup2, subgroup3));
154 DeploymentSubGroup subgroup1b = new DeploymentSubGroup(subgroup1);
155 subgroup1b.setAction(Action.POST);
157 DeploymentSubGroup subgroup1c = new DeploymentSubGroup(subgroup1);
158 subgroup1c.setAction(Action.DELETE);
160 DeploymentSubGroup subgroup1d = new DeploymentSubGroup(subgroup1);
161 subgroup1d.setAction(Action.DELETE);
163 group.setDeploymentSubgroups(
164 Arrays.asList(subgroup1, subgroup2, subgroup3, subgroup1b, subgroup1c, subgroup1d));
172 DeploymentSubGroup subgroup1e = new DeploymentSubGroup(subgroup1);
173 subgroup1e.setAction(Action.PATCH);
175 group.setDeploymentSubgroups(
176 Arrays.asList(subgroup1, subgroup2, subgroup3, subgroup1b, subgroup1c, subgroup1d, subgroup1e));
178 assertInvalid(group);
181 private void assertValid(DeploymentGroup group) {
182 ValidationResult result = group.validatePapRest();
183 assertNotNull(result);
184 assertNull(result.getResult());
185 assertTrue(result.isValid());
188 private void assertInvalid(DeploymentGroup group) {
189 ValidationResult result = group.validatePapRest();
190 assertNotNull(result);
191 assertFalse(result.isValid());
192 assertNotNull(result.getResult());