2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2019 AT&T Intellectual Property. All rights reserved.
6 * Modifications Copyright (C) 2019 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() {
125 PdpGroup group = new PdpGroup();
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());
134 PdpSubGroup subgroup2 = new PdpSubGroup(subgroup1);
135 subgroup2.setPdpType(PDP_TYPE2);
137 PdpSubGroup subgroup3 = new PdpSubGroup(subgroup1);
138 subgroup3.setPdpType(PDP_TYPE3);
140 group.setPdpSubgroups(Arrays.asList(subgroup1, subgroup2, subgroup3));
143 ValidationResult result = group.validatePapRest();
144 assertNotNull(result);
145 assertTrue(result.isValid());
146 assertNull(result.getResult());
149 PdpGroup group2 = new PdpGroup(group);
150 group2.setName(null);
151 assertInvalid(group2);
153 // null subgroup list
154 group2 = new PdpGroup(group);
155 group2.setPdpSubgroups(null);
156 assertInvalid(group2);
158 // empty subgroup list
159 group2 = new PdpGroup(group);
160 group2.setPdpSubgroups(Collections.emptyList());
161 assertInvalid(group2);
164 group2 = new PdpGroup(group);
165 group2.setPdpSubgroups(Arrays.asList(subgroup1, null));
166 assertInvalid(group2);
169 group2 = new PdpGroup(group);
170 PdpSubGroup subgroupX = new PdpSubGroup(subgroup1);
171 subgroupX.setPdpType(null);
172 group2.setPdpSubgroups(Arrays.asList(subgroupX));
173 assertInvalid(group2);
175 // duplicate PDP type
176 group2 = new PdpGroup(group);
177 group2.setPdpSubgroups(Arrays.asList(subgroup1, subgroup2, subgroup1));
178 assertInvalid(group2);
181 private void assertInvalid(PdpGroup group) {
182 ValidationResult result = group.validatePapRest();
183 assertNotNull(result);
184 assertFalse(result.isValid());
185 assertNotNull(result.getResult());