2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2019-2020 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
12 * http://www.apache.org/licenses/LICENSE-2.0
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=========================================================
22 package org.onap.policy.models.pdp.concepts;
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;
32 import java.util.ArrayList;
33 import java.util.Arrays;
34 import java.util.Collections;
35 import java.util.List;
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.ToscaPolicyTypeIdentifier;
44 * Test methods not tested by {@link ModelsTest}.
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";
54 public void testCopyConstructor() {
55 assertThatThrownBy(() -> new PdpGroup(null)).isInstanceOf(NullPointerException.class);
57 PdpGroup orig = new PdpGroup();
59 // verify with null values
60 assertEquals("PdpGroup(name=null, description=null, pdpGroupState=null, " + "properties=null, pdpSubgroups=[])",
61 new PdpGroup(orig).toString());
63 // verify with all values
64 orig.setDescription("my-descript");
66 orig.setVersion(VERSION);
67 orig.setDescription("my-description");
68 orig.setPdpGroupState(PdpState.SAFE);
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));
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);
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());
90 public void testHashCode() {
91 PdpGroup group = new PdpGroup();
92 group.setDescription("A");
93 int hash = group.hashCode();
95 assertEquals(hash, group.hashCode());
97 group.setDescription("B");
98 assertNotEquals(hash, group.hashCode());
102 public void testCompareTo() {
103 PdpGroup pdpGroup0 = new PdpGroup();
104 pdpGroup0.setName("Name0");
105 pdpGroup0.setVersion(VERSION);
107 PdpGroup pdpGroup1 = new PdpGroup();
108 pdpGroup1.setName("Name0");
109 pdpGroup1.setVersion(VERSION);
111 assertEquals(0, pdpGroup0.compareTo(pdpGroup1));
113 PdpGroups pdpGroups = new PdpGroups();
114 pdpGroups.setGroups(new ArrayList<>());
115 pdpGroups.getGroups().add(pdpGroup0);
116 pdpGroups.getGroups().add(pdpGroup1);
118 List<Map<String, PdpGroup>> mapList = pdpGroups.toMapList();
120 assertEquals(1, mapList.size());
121 assertEquals(1, mapList.get(0).size());
125 public void testValidatePapRest_GroupUpdateFlow() {
126 PdpGroup group = new PdpGroup();
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 ToscaPolicyTypeIdentifier("a-type-name", "3.2.1")));
133 subgroup1.setPolicies(Collections.emptyList());
134 group.setPdpSubgroups(Arrays.asList(subgroup1));
136 ValidationResult result = group.validatePapRest(true);
137 assertNotNull(result);
138 assertTrue(result.isValid());
139 assertNull(result.getResult());
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));
148 result = group.validatePapRest(true);
149 assertNotNull(result);
150 assertTrue(result.isValid());
151 assertNull(result.getResult());
154 result = group.validatePapRest(false);
155 assertNotNull(result);
156 assertFalse(result.isValid());
157 assertNotNull(result.getResult());
161 public void testValidatePapRest() {
162 PdpGroup group = new PdpGroup();
165 PdpSubGroup subgroup1 = new PdpSubGroup();
166 subgroup1.setDesiredInstanceCount(1);
167 subgroup1.setPdpType(PDP_TYPE1);
168 subgroup1.setSupportedPolicyTypes(Arrays.asList(new ToscaPolicyTypeIdentifier("a-type-name", "3.2.1")));
169 subgroup1.setPolicies(Collections.emptyList());
171 PdpSubGroup subgroup2 = new PdpSubGroup(subgroup1);
172 subgroup2.setPdpType(PDP_TYPE2);
174 PdpSubGroup subgroup3 = new PdpSubGroup(subgroup1);
175 subgroup3.setPdpType(PDP_TYPE3);
177 group.setPdpSubgroups(Arrays.asList(subgroup1, subgroup2, subgroup3));
180 ValidationResult result = group.validatePapRest(false);
181 assertNotNull(result);
182 assertTrue(result.isValid());
183 assertNull(result.getResult());
186 PdpGroup group2 = new PdpGroup(group);
187 group2.setName(null);
188 assertInvalid(group2);
190 // null subgroup list
191 group2 = new PdpGroup(group);
192 group2.setPdpSubgroups(null);
193 assertInvalid(group2);
195 // empty subgroup list
196 group2 = new PdpGroup(group);
197 group2.setPdpSubgroups(Collections.emptyList());
198 assertInvalid(group2);
201 group2 = new PdpGroup(group);
202 group2.setPdpSubgroups(Arrays.asList(subgroup1, null));
203 assertInvalid(group2);
206 group2 = new PdpGroup(group);
207 PdpSubGroup subgroupX = new PdpSubGroup(subgroup1);
208 subgroupX.setPdpType(null);
209 group2.setPdpSubgroups(Arrays.asList(subgroupX));
210 assertInvalid(group2);
212 // duplicate PDP type
213 group2 = new PdpGroup(group);
214 group2.setPdpSubgroups(Arrays.asList(subgroup1, subgroup2, subgroup1));
215 assertInvalid(group2);
218 private void assertInvalid(PdpGroup group) {
219 ValidationResult result = group.validatePapRest(false);
220 assertNotNull(result);
221 assertFalse(result.isValid());
222 assertNotNull(result.getResult());