a74029e10f0bdcfbf5db56a5b0d70ec0e9297725
[policy/models.git] / models-pdp / src / test / java / org / onap / policy / models / pdp / concepts / DeploymentGroupTest.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * ONAP Policy Models
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
10  *
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  *
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=========================================================
19  */
20
21 package org.onap.policy.models.pdp.concepts;
22
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;
29
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;
35
36 /**
37  * Test methods not tested by {@link ModelsTest}.
38  */
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";
44
45     @Test
46     public void testCopyConstructor() {
47         assertThatThrownBy(() -> new DeploymentGroup(null)).isInstanceOf(NullPointerException.class);
48
49         DeploymentGroup orig = new DeploymentGroup();
50
51         // verify with null values
52         assertEquals("DeploymentGroup(name=null, deploymentSubgroups=[])", new DeploymentGroup(orig).toString());
53
54         // verify with all values
55         orig.setName(NAME);
56
57         DeploymentSubGroup sub1 = new DeploymentSubGroup();
58         DeploymentSubGroup sub2 = new DeploymentSubGroup();
59         orig.setDeploymentSubgroups(Arrays.asList(sub1, sub2));
60
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());
65     }
66
67     @Test
68     public void testHashCode() {
69         DeploymentGroup group = new DeploymentGroup();
70         group.setName("A");
71         int hash = group.hashCode();
72
73         assertEquals(hash, group.hashCode());
74
75         group.setName("B");
76         assertTrue(hash != group.hashCode());
77     }
78
79     @Test
80     public void testValidatePapRest() {
81         DeploymentGroup group = new DeploymentGroup();
82         group.setName(NAME);
83
84         DeploymentSubGroup subgroup1 = new DeploymentSubGroup();
85         subgroup1.setPdpType(PDP_TYPE1);
86         subgroup1.setAction(Action.PATCH);
87         subgroup1.setPolicies(Collections.emptyList());
88
89         DeploymentSubGroup subgroup2 = new DeploymentSubGroup(subgroup1);
90         subgroup2.setPdpType(PDP_TYPE2);
91
92         DeploymentSubGroup subgroup3 = new DeploymentSubGroup(subgroup1);
93         subgroup3.setPdpType(PDP_TYPE3);
94
95         group.setDeploymentSubgroups(Arrays.asList(subgroup1, subgroup2, subgroup3));
96
97         // valid
98         assertValid(group);
99
100         // null name
101         DeploymentGroup group2 = new DeploymentGroup(group);
102         group2.setName(null);
103         assertInvalid(group2);
104
105         // null subgroup list
106         group2 = new DeploymentGroup(group);
107         group2.setDeploymentSubgroups(null);
108         assertInvalid(group2);
109
110         // empty subgroup list
111         group2 = new DeploymentGroup(group);
112         group2.setDeploymentSubgroups(Collections.emptyList());
113         assertInvalid(group2);
114
115         // null subgroup
116         group2 = new DeploymentGroup(group);
117         group2.setDeploymentSubgroups(Arrays.asList(subgroup1, null));
118         assertInvalid(group2);
119
120         // invalid subgroup
121         group2 = new DeploymentGroup(group);
122         DeploymentSubGroup subgroupX = new DeploymentSubGroup(subgroup1);
123         subgroupX.setPdpType(null);
124         group2.setDeploymentSubgroups(Arrays.asList(subgroupX));
125         assertInvalid(group2);
126     }
127
128     @Test
129     public void testCheckDuplicateSubgroups() {
130         DeploymentGroup group = new DeploymentGroup();
131         group.setName(NAME);
132
133         DeploymentSubGroup subgroup1 = new DeploymentSubGroup();
134         subgroup1.setPdpType(PDP_TYPE1);
135         subgroup1.setAction(Action.POST);
136         subgroup1.setPolicies(Collections.emptyList());
137
138         DeploymentSubGroup subgroup2 = new DeploymentSubGroup(subgroup1);
139         subgroup2.setPdpType(PDP_TYPE2);
140         subgroup2.setAction(Action.PATCH);
141
142         DeploymentSubGroup subgroup3 = new DeploymentSubGroup(subgroup1);
143         subgroup3.setPdpType(PDP_TYPE3);
144         subgroup3.setAction(Action.DELETE);
145
146         group.setDeploymentSubgroups(Arrays.asList(subgroup1, subgroup2, subgroup3));
147
148         // no duplicates
149         assertValid(group);
150
151         /*
152          * Allowed duplicates
153          */
154         DeploymentSubGroup subgroup1b = new DeploymentSubGroup(subgroup1);
155         subgroup1b.setAction(Action.POST);
156
157         DeploymentSubGroup subgroup1c = new DeploymentSubGroup(subgroup1);
158         subgroup1c.setAction(Action.DELETE);
159
160         DeploymentSubGroup subgroup1d = new DeploymentSubGroup(subgroup1);
161         subgroup1d.setAction(Action.DELETE);
162
163         group.setDeploymentSubgroups(
164                         Arrays.asList(subgroup1, subgroup2, subgroup3, subgroup1b, subgroup1c, subgroup1d));
165
166         // still ok
167         assertValid(group);
168
169         /*
170          * Not allowed
171          */
172         DeploymentSubGroup subgroup1e = new DeploymentSubGroup(subgroup1);
173         subgroup1e.setAction(Action.PATCH);
174
175         group.setDeploymentSubgroups(
176                         Arrays.asList(subgroup1, subgroup2, subgroup3, subgroup1b, subgroup1c, subgroup1d, subgroup1e));
177
178         assertInvalid(group);
179     }
180
181     private void assertValid(DeploymentGroup group) {
182         ValidationResult result = group.validatePapRest();
183         assertNotNull(result);
184         assertNull(result.getResult());
185         assertTrue(result.isValid());
186     }
187
188     private void assertInvalid(DeploymentGroup group) {
189         ValidationResult result = group.validatePapRest();
190         assertNotNull(result);
191         assertFalse(result.isValid());
192         assertNotNull(result.getResult());
193     }
194 }