Changed identifiers to concept identifiers
[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-2020 AT&T Intellectual Property. All rights reserved.
6  * Modifications Copyright (C) 2019-2021 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.assertNotEquals;
28 import static org.junit.Assert.assertNotNull;
29 import static org.junit.Assert.assertNull;
30 import static org.junit.Assert.assertTrue;
31
32 import java.util.ArrayList;
33 import java.util.Arrays;
34 import java.util.Collections;
35 import java.util.List;
36 import java.util.Map;
37 import java.util.TreeMap;
38 import org.junit.Test;
39 import org.onap.policy.common.parameters.ValidationResult;
40 import org.onap.policy.models.pdp.enums.PdpState;
41 import org.onap.policy.models.tosca.authorative.concepts.ToscaConceptIdentifier;
42
43 /**
44  * Test methods not tested by {@link ModelsTest}.
45  */
46 public class PdpGroupTest {
47     private static final String VERSION = "1.2.3";
48     private static final String NAME = "my-name";
49     private static final String PDP_TYPE1 = "type-1";
50     private static final String PDP_TYPE2 = "type-2";
51     private static final String PDP_TYPE3 = "type-3";
52
53     @Test
54     public void testCopyConstructor() {
55         assertThatThrownBy(() -> new PdpGroup(null)).isInstanceOf(NullPointerException.class);
56
57         PdpGroup orig = new PdpGroup();
58
59         // verify with null values
60         assertEquals("PdpGroup(name=null, description=null, pdpGroupState=null, " + "properties=null, pdpSubgroups=[])",
61                         new PdpGroup(orig).toString());
62
63         // verify with all values
64         orig.setDescription("my-descript");
65         orig.setName(NAME);
66         orig.setVersion(VERSION);
67         orig.setDescription("my-description");
68         orig.setPdpGroupState(PdpState.SAFE);
69
70         PdpSubGroup sub1 = new PdpSubGroup();
71         sub1.setCurrentInstanceCount(10);
72         PdpSubGroup sub2 = new PdpSubGroup();
73         sub2.setCurrentInstanceCount(11);
74         orig.setPdpSubgroups(Arrays.asList(sub1, sub2));
75
76         Map<String, String> props = new TreeMap<>();
77         props.put("key-A", "value-A");
78         props.put("key-B", "value-B");
79         orig.setProperties(props);
80
81         assertEquals("PdpGroup(name=my-name, description=my-description, "
82                         + "pdpGroupState=SAFE, properties={key-A=value-A, key-B=value-B}, "
83                         + "pdpSubgroups=[PdpSubGroup(pdpType=null, supportedPolicyTypes=[], policies=[], "
84                         + "currentInstanceCount=10, desiredInstanceCount=0, properties=null, pdpInstances=[]), "
85                         + "PdpSubGroup(pdpType=null, supportedPolicyTypes=[], policies=[], currentInstanceCount=11, "
86                         + "desiredInstanceCount=0, properties=null, pdpInstances=[])])", new PdpGroup(orig).toString());
87     }
88
89     @Test
90     public void testHashCode() {
91         PdpGroup group = new PdpGroup();
92         group.setDescription("A");
93         int hash = group.hashCode();
94
95         assertEquals(hash, group.hashCode());
96
97         group.setDescription("B");
98         assertNotEquals(hash, group.hashCode());
99     }
100
101     @Test
102     public void testCompareTo() {
103         PdpGroup pdpGroup0 = new PdpGroup();
104         pdpGroup0.setName("Name0");
105         pdpGroup0.setVersion(VERSION);
106
107         PdpGroup pdpGroup1 = new PdpGroup();
108         pdpGroup1.setName("Name0");
109         pdpGroup1.setVersion(VERSION);
110
111         assertEquals(0, pdpGroup0.compareTo(pdpGroup1));
112
113         PdpGroups pdpGroups = new PdpGroups();
114         pdpGroups.setGroups(new ArrayList<>());
115         pdpGroups.getGroups().add(pdpGroup0);
116         pdpGroups.getGroups().add(pdpGroup1);
117
118         List<Map<String, PdpGroup>> mapList = pdpGroups.toMapList();
119
120         assertEquals(1, mapList.size());
121         assertEquals(1, mapList.get(0).size());
122     }
123
124     @Test
125     public void testValidatePapRest_GroupUpdateFlow() {
126         PdpGroup group = new PdpGroup();
127         group.setName(NAME);
128         // with supported policy type and policies
129         PdpSubGroup subgroup1 = new PdpSubGroup();
130         subgroup1.setDesiredInstanceCount(1);
131         subgroup1.setPdpType(PDP_TYPE1);
132         subgroup1.setSupportedPolicyTypes(Arrays.asList(new ToscaConceptIdentifier("a-type-name", "3.2.1")));
133         subgroup1.setPolicies(Collections.emptyList());
134         group.setPdpSubgroups(Arrays.asList(subgroup1));
135
136         ValidationResult result = group.validatePapRest(true);
137         assertNotNull(result);
138         assertTrue(result.isValid());
139         assertNull(result.getResult());
140
141         // without supported policy type and policies
142         PdpSubGroup subgroup2 = new PdpSubGroup();
143         subgroup2.setDesiredInstanceCount(1);
144         subgroup2.setPdpType(PDP_TYPE1);
145         group.setPdpSubgroups(Arrays.asList(subgroup2));
146
147         // valid
148         result = group.validatePapRest(true);
149         assertNotNull(result);
150         assertTrue(result.isValid());
151         assertNull(result.getResult());
152
153         // invalid
154         result = group.validatePapRest(false);
155         assertNotNull(result);
156         assertFalse(result.isValid());
157         assertNotNull(result.getResult());
158     }
159
160     @Test
161     public void testValidatePapRest() {
162         PdpGroup group = new PdpGroup();
163         group.setName(NAME);
164
165         PdpSubGroup subgroup1 = new PdpSubGroup();
166         subgroup1.setDesiredInstanceCount(1);
167         subgroup1.setPdpType(PDP_TYPE1);
168         subgroup1.setSupportedPolicyTypes(Arrays.asList(new ToscaConceptIdentifier("a-type-name", "3.2.1")));
169         subgroup1.setPolicies(Collections.emptyList());
170
171         PdpSubGroup subgroup2 = new PdpSubGroup(subgroup1);
172         subgroup2.setPdpType(PDP_TYPE2);
173
174         PdpSubGroup subgroup3 = new PdpSubGroup(subgroup1);
175         subgroup3.setPdpType(PDP_TYPE3);
176
177         group.setPdpSubgroups(Arrays.asList(subgroup1, subgroup2, subgroup3));
178
179         // valid
180         ValidationResult result = group.validatePapRest(false);
181         assertNotNull(result);
182         assertTrue(result.isValid());
183         assertNull(result.getResult());
184
185         // null name
186         PdpGroup group2 = new PdpGroup(group);
187         group2.setName(null);
188         assertInvalid(group2);
189
190         // null subgroup list
191         group2 = new PdpGroup(group);
192         group2.setPdpSubgroups(null);
193         assertInvalid(group2);
194
195         // empty subgroup list
196         group2 = new PdpGroup(group);
197         group2.setPdpSubgroups(Collections.emptyList());
198         assertInvalid(group2);
199
200         // null subgroup
201         group2 = new PdpGroup(group);
202         group2.setPdpSubgroups(Arrays.asList(subgroup1, null));
203         assertInvalid(group2);
204
205         // invalid subgroup
206         group2 = new PdpGroup(group);
207         PdpSubGroup subgroupX = new PdpSubGroup(subgroup1);
208         subgroupX.setPdpType(null);
209         group2.setPdpSubgroups(Arrays.asList(subgroupX));
210         assertInvalid(group2);
211
212         // duplicate PDP type
213         group2 = new PdpGroup(group);
214         group2.setPdpSubgroups(Arrays.asList(subgroup1, subgroup2, subgroup1));
215         assertInvalid(group2);
216     }
217
218     private void assertInvalid(PdpGroup group) {
219         ValidationResult result = group.validatePapRest(false);
220         assertNotNull(result);
221         assertFalse(result.isValid());
222         assertNotNull(result.getResult());
223     }
224 }