2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2019-2020 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.assertNotEquals;
27 import static org.junit.Assert.assertNotNull;
28 import static org.junit.Assert.assertNull;
29 import static org.junit.Assert.assertTrue;
31 import java.util.Arrays;
32 import java.util.Collections;
33 import org.junit.Test;
34 import org.onap.policy.common.parameters.ValidationResult;
35 import org.onap.policy.models.pdp.concepts.DeploymentSubGroup.Action;
38 * Test methods not tested by {@link ModelsTest}.
40 public class DeploymentGroupTest {
41 private static final String NAME = "my-name";
42 private static final String PDP_TYPE1 = "type-1";
43 private static final String PDP_TYPE2 = "type-2";
44 private static final String PDP_TYPE3 = "type-3";
47 public void testCopyConstructor() {
48 assertThatThrownBy(() -> new DeploymentGroup(null)).isInstanceOf(NullPointerException.class);
50 DeploymentGroup orig = new DeploymentGroup();
52 // verify with null values
53 assertEquals("DeploymentGroup(name=null, deploymentSubgroups=[])", new DeploymentGroup(orig).toString());
55 // verify with all values
58 DeploymentSubGroup sub1 = new DeploymentSubGroup();
59 DeploymentSubGroup sub2 = new DeploymentSubGroup();
60 orig.setDeploymentSubgroups(Arrays.asList(sub1, sub2));
62 assertEquals("DeploymentGroup(name=my-name, "
63 + "deploymentSubgroups=[DeploymentSubGroup(pdpType=null, action=null, policies=[]), "
64 + "DeploymentSubGroup(pdpType=null, action=null, policies=[])])",
65 new DeploymentGroup(orig).toString());
69 public void testHashCode() {
70 DeploymentGroup group = new DeploymentGroup();
72 int hash = group.hashCode();
74 assertEquals(hash, group.hashCode());
77 assertNotEquals(hash, group.hashCode());
81 public void testValidatePapRest() {
82 DeploymentGroup group = new DeploymentGroup();
85 DeploymentSubGroup subgroup1 = new DeploymentSubGroup();
86 subgroup1.setPdpType(PDP_TYPE1);
87 subgroup1.setAction(Action.PATCH);
88 subgroup1.setPolicies(Collections.emptyList());
90 DeploymentSubGroup subgroup2 = new DeploymentSubGroup(subgroup1);
91 subgroup2.setPdpType(PDP_TYPE2);
93 DeploymentSubGroup subgroup3 = new DeploymentSubGroup(subgroup1);
94 subgroup3.setPdpType(PDP_TYPE3);
96 group.setDeploymentSubgroups(Arrays.asList(subgroup1, subgroup2, subgroup3));
102 DeploymentGroup group2 = new DeploymentGroup(group);
103 group2.setName(null);
104 assertInvalid(group2);
106 // null subgroup list
107 group2 = new DeploymentGroup(group);
108 group2.setDeploymentSubgroups(null);
109 assertInvalid(group2);
111 // empty subgroup list
112 group2 = new DeploymentGroup(group);
113 group2.setDeploymentSubgroups(Collections.emptyList());
114 assertInvalid(group2);
117 group2 = new DeploymentGroup(group);
118 group2.setDeploymentSubgroups(Arrays.asList(subgroup1, null));
119 assertInvalid(group2);
122 group2 = new DeploymentGroup(group);
123 DeploymentSubGroup subgroupX = new DeploymentSubGroup(subgroup1);
124 subgroupX.setPdpType(null);
125 group2.setDeploymentSubgroups(Arrays.asList(subgroupX));
126 assertInvalid(group2);
130 public void testCheckDuplicateSubgroups() {
131 DeploymentGroup group = new DeploymentGroup();
134 DeploymentSubGroup subgroup1 = new DeploymentSubGroup();
135 subgroup1.setPdpType(PDP_TYPE1);
136 subgroup1.setAction(Action.POST);
137 subgroup1.setPolicies(Collections.emptyList());
139 DeploymentSubGroup subgroup2 = new DeploymentSubGroup(subgroup1);
140 subgroup2.setPdpType(PDP_TYPE2);
141 subgroup2.setAction(Action.PATCH);
143 DeploymentSubGroup subgroup3 = new DeploymentSubGroup(subgroup1);
144 subgroup3.setPdpType(PDP_TYPE3);
145 subgroup3.setAction(Action.DELETE);
147 group.setDeploymentSubgroups(Arrays.asList(subgroup1, subgroup2, subgroup3));
155 DeploymentSubGroup subgroup1b = new DeploymentSubGroup(subgroup1);
156 subgroup1b.setAction(Action.POST);
158 DeploymentSubGroup subgroup1c = new DeploymentSubGroup(subgroup1);
159 subgroup1c.setAction(Action.DELETE);
161 DeploymentSubGroup subgroup1d = new DeploymentSubGroup(subgroup1);
162 subgroup1d.setAction(Action.DELETE);
164 group.setDeploymentSubgroups(
165 Arrays.asList(subgroup1, subgroup2, subgroup3, subgroup1b, subgroup1c, subgroup1d));
173 DeploymentSubGroup subgroup1e = new DeploymentSubGroup(subgroup1);
174 subgroup1e.setAction(Action.PATCH);
176 group.setDeploymentSubgroups(
177 Arrays.asList(subgroup1, subgroup2, subgroup3, subgroup1b, subgroup1c, subgroup1d, subgroup1e));
179 assertInvalid(group);
182 private void assertValid(DeploymentGroup group) {
183 ValidationResult result = group.validatePapRest();
184 assertNotNull(result);
185 assertNull(result.getResult());
186 assertTrue(result.isValid());
189 private void assertInvalid(DeploymentGroup group) {
190 ValidationResult result = group.validatePapRest();
191 assertNotNull(result);
192 assertFalse(result.isValid());
193 assertNotNull(result.getResult());