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