3082bb228917d53099c0e6351a2e4409faf8bd4e
[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 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
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.assertNotNull;
28 import static org.junit.Assert.assertNull;
29 import static org.junit.Assert.assertTrue;
30
31 import java.util.ArrayList;
32 import java.util.Arrays;
33 import java.util.Collections;
34 import java.util.List;
35 import java.util.Map;
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;
41
42 /**
43  * Test methods not tested by {@link ModelsTest}.
44  */
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";
50
51     @Test
52     public void testCopyConstructor() {
53         assertThatThrownBy(() -> new PdpGroup(null)).isInstanceOf(NullPointerException.class);
54
55         PdpGroup orig = new PdpGroup();
56
57         // verify with null values
58         assertEquals("PdpGroup(name=null, version=null, description=null, pdpGroupState=null, "
59                 + "properties=null, pdpSubgroups=[])", new PdpGroup(orig).toString());
60
61         // verify with all values
62         orig.setDescription("my-descript");
63         orig.setName(NAME);
64         orig.setVersion("1.2.3");
65         orig.setDescription("my-description");
66         orig.setPdpGroupState(PdpState.SAFE);
67
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));
73
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);
78
79         assertEquals("PdpGroup(name=my-name, version=1.2.3, 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());
85     }
86
87     @Test
88     public void testHashCode() {
89         PdpGroup group = new PdpGroup();
90         group.setDescription("A");
91         int hash = group.hashCode();
92
93         assertEquals(hash, group.hashCode());
94
95         group.setDescription("B");
96         assertTrue(hash != group.hashCode());
97     }
98
99     @Test
100     public void testCompareTo() {
101         PdpGroup pdpGroup0 = new PdpGroup();
102         pdpGroup0.setName("Name0");
103         pdpGroup0.setVersion("1.2.3");
104
105         PdpGroup pdpGroup1 = new PdpGroup();
106         pdpGroup1.setName("Name0");
107         pdpGroup1.setVersion("1.2.3");
108
109         assertEquals(0, pdpGroup0.compareTo(pdpGroup1));
110
111         PdpGroups pdpGroups = new PdpGroups();
112         pdpGroups.setGroups(new ArrayList<>());
113         pdpGroups.getGroups().add(pdpGroup0);
114         pdpGroups.getGroups().add(pdpGroup1);
115
116         List<Map<String, PdpGroup>> mapList = pdpGroups.toMapList();
117
118         assertEquals(1, mapList.size());
119         assertEquals(1, mapList.get(0).size());
120     }
121
122     @Test
123     public void testValidatePapRest() {
124         PdpGroup group = new PdpGroup();
125         group.setName(NAME);
126
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());
132
133         PdpSubGroup subgroup2 = new PdpSubGroup(subgroup1);
134         subgroup2.setPdpType(PDP_TYPE2);
135
136         PdpSubGroup subgroup3 = new PdpSubGroup(subgroup1);
137         subgroup3.setPdpType(PDP_TYPE3);
138
139         group.setPdpSubgroups(Arrays.asList(subgroup1, subgroup2, subgroup3));
140
141         // valid
142         ValidationResult result = group.validatePapRest();
143         assertNotNull(result);
144         assertTrue(result.isValid());
145         assertNull(result.getResult());
146
147         // null name
148         PdpGroup group2 = new PdpGroup(group);
149         group2.setName(null);
150         assertInvalid(group2);
151
152         // null subgroup list
153         group2 = new PdpGroup(group);
154         group2.setPdpSubgroups(null);
155         assertInvalid(group2);
156
157         // null subgroup
158         group2 = new PdpGroup(group);
159         group2.setPdpSubgroups(Arrays.asList(subgroup1, null));
160         assertInvalid(group2);
161
162         // invalid subgroup
163         group2 = new PdpGroup(group);
164         PdpSubGroup subgroupX = new PdpSubGroup(subgroup1);
165         subgroupX.setPdpType(null);
166         group2.setPdpSubgroups(Arrays.asList(subgroupX));
167         assertInvalid(group2);
168
169         // duplicate PDP type
170         group2 = new PdpGroup(group);
171         group2.setPdpSubgroups(Arrays.asList(subgroup1, subgroup2, subgroup1));
172         assertInvalid(group2);
173     }
174
175     private void assertInvalid(PdpGroup group) {
176         ValidationResult result = group.validatePapRest();
177         assertNotNull(result);
178         assertFalse(result.isValid());
179         assertNotNull(result.getResult());
180     }
181 }