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 NAME = "my-name";
47 private static final String PDP_TYPE1 = "type-1";
48 private static final String PDP_TYPE2 = "type-2";
49 private static final String PDP_TYPE3 = "type-3";
52 public void testCopyConstructor() {
53 assertThatThrownBy(() -> new PdpGroup(null)).isInstanceOf(NullPointerException.class);
55 PdpGroup orig = new PdpGroup();
57 // verify with null values
58 assertEquals("PdpGroup(name=null, description=null, pdpGroupState=null, " + "properties=null, pdpSubgroups=[])",
59 new PdpGroup(orig).toString());
61 // verify with all values
62 orig.setDescription("my-descript");
64 orig.setVersion("1.2.3");
65 orig.setDescription("my-description");
66 orig.setPdpGroupState(PdpState.SAFE);
68 PdpSubGroup sub1 = new PdpSubGroup();
69 sub1.setCurrentInstanceCount(10);
70 PdpSubGroup sub2 = new PdpSubGroup();
71 sub2.setCurrentInstanceCount(11);
72 orig.setPdpSubgroups(Arrays.asList(sub1, sub2));
74 Map<String, String> props = new TreeMap<>();
75 props.put("key-A", "value-A");
76 props.put("key-B", "value-B");
77 orig.setProperties(props);
79 assertEquals("PdpGroup(name=my-name, description=my-description, "
80 + "pdpGroupState=SAFE, properties={key-A=value-A, key-B=value-B}, "
81 + "pdpSubgroups=[PdpSubGroup(pdpType=null, supportedPolicyTypes=[], policies=[], "
82 + "currentInstanceCount=10, desiredInstanceCount=0, properties=null, pdpInstances=[]), "
83 + "PdpSubGroup(pdpType=null, supportedPolicyTypes=[], policies=[], currentInstanceCount=11, "
84 + "desiredInstanceCount=0, properties=null, pdpInstances=[])])", new PdpGroup(orig).toString());
88 public void testHashCode() {
89 PdpGroup group = new PdpGroup();
90 group.setDescription("A");
91 int hash = group.hashCode();
93 assertEquals(hash, group.hashCode());
95 group.setDescription("B");
96 assertTrue(hash != group.hashCode());
100 public void testCompareTo() {
101 PdpGroup pdpGroup0 = new PdpGroup();
102 pdpGroup0.setName("Name0");
103 pdpGroup0.setVersion("1.2.3");
105 PdpGroup pdpGroup1 = new PdpGroup();
106 pdpGroup1.setName("Name0");
107 pdpGroup1.setVersion("1.2.3");
109 assertEquals(0, pdpGroup0.compareTo(pdpGroup1));
111 PdpGroups pdpGroups = new PdpGroups();
112 pdpGroups.setGroups(new ArrayList<>());
113 pdpGroups.getGroups().add(pdpGroup0);
114 pdpGroups.getGroups().add(pdpGroup1);
116 List<Map<String, PdpGroup>> mapList = pdpGroups.toMapList();
118 assertEquals(1, mapList.size());
119 assertEquals(1, mapList.get(0).size());
123 public void testValidatePapRest() {
124 PdpGroup group = new PdpGroup();
127 PdpSubGroup subgroup1 = new PdpSubGroup();
128 subgroup1.setDesiredInstanceCount(1);
129 subgroup1.setPdpType(PDP_TYPE1);
130 subgroup1.setSupportedPolicyTypes(Arrays.asList(new ToscaPolicyTypeIdentifier("a-type-name", "3.2.1")));
131 subgroup1.setPolicies(Collections.emptyList());
133 PdpSubGroup subgroup2 = new PdpSubGroup(subgroup1);
134 subgroup2.setPdpType(PDP_TYPE2);
136 PdpSubGroup subgroup3 = new PdpSubGroup(subgroup1);
137 subgroup3.setPdpType(PDP_TYPE3);
139 group.setPdpSubgroups(Arrays.asList(subgroup1, subgroup2, subgroup3));
142 ValidationResult result = group.validatePapRest();
143 assertNotNull(result);
144 assertTrue(result.isValid());
145 assertNull(result.getResult());
148 PdpGroup group2 = new PdpGroup(group);
149 group2.setName(null);
150 assertInvalid(group2);
152 // null subgroup list
153 group2 = new PdpGroup(group);
154 group2.setPdpSubgroups(null);
155 assertInvalid(group2);
157 // empty subgroup list
158 group2 = new PdpGroup(group);
159 group2.setPdpSubgroups(Collections.emptyList());
160 assertInvalid(group2);
163 group2 = new PdpGroup(group);
164 group2.setPdpSubgroups(Arrays.asList(subgroup1, null));
165 assertInvalid(group2);
168 group2 = new PdpGroup(group);
169 PdpSubGroup subgroupX = new PdpSubGroup(subgroup1);
170 subgroupX.setPdpType(null);
171 group2.setPdpSubgroups(Arrays.asList(subgroupX));
172 assertInvalid(group2);
174 // duplicate PDP type
175 group2 = new PdpGroup(group);
176 group2.setPdpSubgroups(Arrays.asList(subgroup1, subgroup2, subgroup1));
177 assertInvalid(group2);
180 private void assertInvalid(PdpGroup group) {
181 ValidationResult result = group.validatePapRest();
182 assertNotNull(result);
183 assertFalse(result.isValid());
184 assertNotNull(result.getResult());