4f11079c2f8988f151b5cbad08e9bf4c642feb2b
[policy/models.git] / models-pdp / src / test / java / org / onap / policy / models / pdp / concepts / PdpGroupTest.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * ONAP Policy Models
4  * ================================================================================
5  * Copyright (C) 2019 AT&T Intellectual Property. All rights reserved.
6  * Modifications Copyright (C) 2019-2020 Nordix Foundation.
7  * ================================================================================
8  * Licensed under the Apache License, Version 2.0 (the "License");
9  * you may not use this file except in compliance with the License.
10  * You may obtain a copy of the License at
11  *
12  *      http://www.apache.org/licenses/LICENSE-2.0
13  *
14  * Unless required by applicable law or agreed to in writing, software
15  * distributed under the License is distributed on an "AS IS" BASIS,
16  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17  * See the License for the specific language governing permissions and
18  * limitations under the License.
19  * ============LICENSE_END=========================================================
20  */
21
22 package org.onap.policy.models.pdp.concepts;
23
24 import static org.assertj.core.api.Assertions.assertThatThrownBy;
25 import static org.junit.Assert.assertEquals;
26 import static org.junit.Assert.assertFalse;
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.ArrayList;
32 import java.util.Arrays;
33 import java.util.Collections;
34 import java.util.List;
35 import java.util.Map;
36 import java.util.TreeMap;
37 import org.junit.Test;
38 import org.onap.policy.common.parameters.ValidationResult;
39 import org.onap.policy.models.pdp.enums.PdpState;
40 import org.onap.policy.models.tosca.authorative.concepts.ToscaPolicyTypeIdentifier;
41
42 /**
43  * Test methods not tested by {@link ModelsTest}.
44  */
45 public class PdpGroupTest {
46     private static final String VERSION = "1.2.3";
47     private static final String NAME = "my-name";
48     private static final String PDP_TYPE1 = "type-1";
49     private static final String PDP_TYPE2 = "type-2";
50     private static final String PDP_TYPE3 = "type-3";
51
52     @Test
53     public void testCopyConstructor() {
54         assertThatThrownBy(() -> new PdpGroup(null)).isInstanceOf(NullPointerException.class);
55
56         PdpGroup orig = new PdpGroup();
57
58         // verify with null values
59         assertEquals("PdpGroup(name=null, description=null, pdpGroupState=null, " + "properties=null, pdpSubgroups=[])",
60                         new PdpGroup(orig).toString());
61
62         // verify with all values
63         orig.setDescription("my-descript");
64         orig.setName(NAME);
65         orig.setVersion(VERSION);
66         orig.setDescription("my-description");
67         orig.setPdpGroupState(PdpState.SAFE);
68
69         PdpSubGroup sub1 = new PdpSubGroup();
70         sub1.setCurrentInstanceCount(10);
71         PdpSubGroup sub2 = new PdpSubGroup();
72         sub2.setCurrentInstanceCount(11);
73         orig.setPdpSubgroups(Arrays.asList(sub1, sub2));
74
75         Map<String, String> props = new TreeMap<>();
76         props.put("key-A", "value-A");
77         props.put("key-B", "value-B");
78         orig.setProperties(props);
79
80         assertEquals("PdpGroup(name=my-name, description=my-description, "
81                         + "pdpGroupState=SAFE, properties={key-A=value-A, key-B=value-B}, "
82                         + "pdpSubgroups=[PdpSubGroup(pdpType=null, supportedPolicyTypes=[], policies=[], "
83                         + "currentInstanceCount=10, desiredInstanceCount=0, properties=null, pdpInstances=[]), "
84                         + "PdpSubGroup(pdpType=null, supportedPolicyTypes=[], policies=[], currentInstanceCount=11, "
85                         + "desiredInstanceCount=0, properties=null, pdpInstances=[])])", new PdpGroup(orig).toString());
86     }
87
88     @Test
89     public void testHashCode() {
90         PdpGroup group = new PdpGroup();
91         group.setDescription("A");
92         int hash = group.hashCode();
93
94         assertEquals(hash, group.hashCode());
95
96         group.setDescription("B");
97         assertTrue(hash != group.hashCode());
98     }
99
100     @Test
101     public void testCompareTo() {
102         PdpGroup pdpGroup0 = new PdpGroup();
103         pdpGroup0.setName("Name0");
104         pdpGroup0.setVersion(VERSION);
105
106         PdpGroup pdpGroup1 = new PdpGroup();
107         pdpGroup1.setName("Name0");
108         pdpGroup1.setVersion(VERSION);
109
110         assertEquals(0, pdpGroup0.compareTo(pdpGroup1));
111
112         PdpGroups pdpGroups = new PdpGroups();
113         pdpGroups.setGroups(new ArrayList<>());
114         pdpGroups.getGroups().add(pdpGroup0);
115         pdpGroups.getGroups().add(pdpGroup1);
116
117         List<Map<String, PdpGroup>> mapList = pdpGroups.toMapList();
118
119         assertEquals(1, mapList.size());
120         assertEquals(1, mapList.get(0).size());
121     }
122
123     @Test
124     public void testValidatePapRest_GroupUpdateFlow() {
125         PdpGroup group = new PdpGroup();
126         group.setName(NAME);
127         // with supported policy type and policies
128         PdpSubGroup subgroup1 = new PdpSubGroup();
129         subgroup1.setDesiredInstanceCount(1);
130         subgroup1.setPdpType(PDP_TYPE1);
131         subgroup1.setSupportedPolicyTypes(Arrays.asList(new ToscaPolicyTypeIdentifier("a-type-name", "3.2.1")));
132         subgroup1.setPolicies(Collections.emptyList());
133         group.setPdpSubgroups(Arrays.asList(subgroup1));
134
135         ValidationResult result = group.validatePapRest(true);
136         assertNotNull(result);
137         assertTrue(result.isValid());
138         assertNull(result.getResult());
139
140         // without supported policy type and policies
141         PdpSubGroup subgroup2 = new PdpSubGroup();
142         subgroup2.setDesiredInstanceCount(1);
143         subgroup2.setPdpType(PDP_TYPE1);
144         group.setPdpSubgroups(Arrays.asList(subgroup2));
145
146         // valid
147         result = group.validatePapRest(true);
148         assertNotNull(result);
149         assertTrue(result.isValid());
150         assertNull(result.getResult());
151
152         // invalid
153         result = group.validatePapRest(false);
154         assertNotNull(result);
155         assertFalse(result.isValid());
156         assertNotNull(result.getResult());
157     }
158
159     @Test
160     public void testValidatePapRest() {
161         PdpGroup group = new PdpGroup();
162         group.setName(NAME);
163
164         PdpSubGroup subgroup1 = new PdpSubGroup();
165         subgroup1.setDesiredInstanceCount(1);
166         subgroup1.setPdpType(PDP_TYPE1);
167         subgroup1.setSupportedPolicyTypes(Arrays.asList(new ToscaPolicyTypeIdentifier("a-type-name", "3.2.1")));
168         subgroup1.setPolicies(Collections.emptyList());
169
170         PdpSubGroup subgroup2 = new PdpSubGroup(subgroup1);
171         subgroup2.setPdpType(PDP_TYPE2);
172
173         PdpSubGroup subgroup3 = new PdpSubGroup(subgroup1);
174         subgroup3.setPdpType(PDP_TYPE3);
175
176         group.setPdpSubgroups(Arrays.asList(subgroup1, subgroup2, subgroup3));
177
178         // valid
179         ValidationResult result = group.validatePapRest(false);
180         assertNotNull(result);
181         assertTrue(result.isValid());
182         assertNull(result.getResult());
183
184         // null name
185         PdpGroup group2 = new PdpGroup(group);
186         group2.setName(null);
187         assertInvalid(group2);
188
189         // null subgroup list
190         group2 = new PdpGroup(group);
191         group2.setPdpSubgroups(null);
192         assertInvalid(group2);
193
194         // empty subgroup list
195         group2 = new PdpGroup(group);
196         group2.setPdpSubgroups(Collections.emptyList());
197         assertInvalid(group2);
198
199         // null subgroup
200         group2 = new PdpGroup(group);
201         group2.setPdpSubgroups(Arrays.asList(subgroup1, null));
202         assertInvalid(group2);
203
204         // invalid subgroup
205         group2 = new PdpGroup(group);
206         PdpSubGroup subgroupX = new PdpSubGroup(subgroup1);
207         subgroupX.setPdpType(null);
208         group2.setPdpSubgroups(Arrays.asList(subgroupX));
209         assertInvalid(group2);
210
211         // duplicate PDP type
212         group2 = new PdpGroup(group);
213         group2.setPdpSubgroups(Arrays.asList(subgroup1, subgroup2, subgroup1));
214         assertInvalid(group2);
215     }
216
217     private void assertInvalid(PdpGroup group) {
218         ValidationResult result = group.validatePapRest(false);
219         assertNotNull(result);
220         assertFalse(result.isValid());
221         assertNotNull(result.getResult());
222     }
223 }