2 * ============LICENSE_START=======================================================
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
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.assertNotNull;
28 import static org.junit.Assert.assertNull;
29 import static org.junit.Assert.assertTrue;
31 import java.util.ArrayList;
32 import java.util.Arrays;
33 import java.util.Collections;
34 import java.util.List;
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;
43 * Test methods not tested by {@link ModelsTest}.
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";
53 public void testCopyConstructor() {
54 assertThatThrownBy(() -> new PdpGroup(null)).isInstanceOf(NullPointerException.class);
56 PdpGroup orig = new PdpGroup();
58 // verify with null values
59 assertEquals("PdpGroup(name=null, description=null, pdpGroupState=null, " + "properties=null, pdpSubgroups=[])",
60 new PdpGroup(orig).toString());
62 // verify with all values
63 orig.setDescription("my-descript");
65 orig.setVersion(VERSION);
66 orig.setDescription("my-description");
67 orig.setPdpGroupState(PdpState.SAFE);
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));
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);
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());
89 public void testHashCode() {
90 PdpGroup group = new PdpGroup();
91 group.setDescription("A");
92 int hash = group.hashCode();
94 assertEquals(hash, group.hashCode());
96 group.setDescription("B");
97 assertTrue(hash != group.hashCode());
101 public void testCompareTo() {
102 PdpGroup pdpGroup0 = new PdpGroup();
103 pdpGroup0.setName("Name0");
104 pdpGroup0.setVersion(VERSION);
106 PdpGroup pdpGroup1 = new PdpGroup();
107 pdpGroup1.setName("Name0");
108 pdpGroup1.setVersion(VERSION);
110 assertEquals(0, pdpGroup0.compareTo(pdpGroup1));
112 PdpGroups pdpGroups = new PdpGroups();
113 pdpGroups.setGroups(new ArrayList<>());
114 pdpGroups.getGroups().add(pdpGroup0);
115 pdpGroups.getGroups().add(pdpGroup1);
117 List<Map<String, PdpGroup>> mapList = pdpGroups.toMapList();
119 assertEquals(1, mapList.size());
120 assertEquals(1, mapList.get(0).size());
124 public void testValidatePapRest_GroupUpdateFlow() {
125 PdpGroup group = new PdpGroup();
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));
135 ValidationResult result = group.validatePapRest(true);
136 assertNotNull(result);
137 assertTrue(result.isValid());
138 assertNull(result.getResult());
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));
147 result = group.validatePapRest(true);
148 assertNotNull(result);
149 assertTrue(result.isValid());
150 assertNull(result.getResult());
153 result = group.validatePapRest(false);
154 assertNotNull(result);
155 assertFalse(result.isValid());
156 assertNotNull(result.getResult());
160 public void testValidatePapRest() {
161 PdpGroup group = new PdpGroup();
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());
170 PdpSubGroup subgroup2 = new PdpSubGroup(subgroup1);
171 subgroup2.setPdpType(PDP_TYPE2);
173 PdpSubGroup subgroup3 = new PdpSubGroup(subgroup1);
174 subgroup3.setPdpType(PDP_TYPE3);
176 group.setPdpSubgroups(Arrays.asList(subgroup1, subgroup2, subgroup3));
179 ValidationResult result = group.validatePapRest(false);
180 assertNotNull(result);
181 assertTrue(result.isValid());
182 assertNull(result.getResult());
185 PdpGroup group2 = new PdpGroup(group);
186 group2.setName(null);
187 assertInvalid(group2);
189 // null subgroup list
190 group2 = new PdpGroup(group);
191 group2.setPdpSubgroups(null);
192 assertInvalid(group2);
194 // empty subgroup list
195 group2 = new PdpGroup(group);
196 group2.setPdpSubgroups(Collections.emptyList());
197 assertInvalid(group2);
200 group2 = new PdpGroup(group);
201 group2.setPdpSubgroups(Arrays.asList(subgroup1, null));
202 assertInvalid(group2);
205 group2 = new PdpGroup(group);
206 PdpSubGroup subgroupX = new PdpSubGroup(subgroup1);
207 subgroupX.setPdpType(null);
208 group2.setPdpSubgroups(Arrays.asList(subgroupX));
209 assertInvalid(group2);
211 // duplicate PDP type
212 group2 = new PdpGroup(group);
213 group2.setPdpSubgroups(Arrays.asList(subgroup1, subgroup2, subgroup1));
214 assertInvalid(group2);
217 private void assertInvalid(PdpGroup group) {
218 ValidationResult result = group.validatePapRest(false);
219 assertNotNull(result);
220 assertFalse(result.isValid());
221 assertNotNull(result.getResult());