Add lists and tests for PDP filters
[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.assertTrue;
27
28 import java.util.ArrayList;
29 import java.util.Arrays;
30 import java.util.List;
31 import java.util.Map;
32 import java.util.TreeMap;
33 import org.junit.Test;
34 import org.onap.policy.models.pdp.enums.PdpState;
35
36 /**
37  * Test the copy constructor, as {@link ModelsTest} tests the other methods.
38  */
39 public class PdpGroupTest {
40
41     @Test
42     public void testCopyConstructor() {
43         assertThatThrownBy(() -> new PdpGroup(null)).isInstanceOf(NullPointerException.class);
44
45         PdpGroup orig = new PdpGroup();
46
47         // verify with null values
48         assertEquals("PdpGroup(name=null, version=null, description=null, pdpGroupState=null, "
49                 + "properties=null, pdpSubgroups=[])", new PdpGroup(orig).toString());
50
51         // verify with all values
52         orig.setDescription("my-descript");
53         orig.setName("my-name");
54         orig.setVersion("1.2.3");
55         orig.setDescription("my-description");
56         orig.setPdpGroupState(PdpState.SAFE);
57
58         PdpSubGroup sub1 = new PdpSubGroup();
59         sub1.setCurrentInstanceCount(10);
60         PdpSubGroup sub2 = new PdpSubGroup();
61         sub2.setCurrentInstanceCount(11);
62         orig.setPdpSubgroups(Arrays.asList(sub1, sub2));
63
64         Map<String, String> props = new TreeMap<>();
65         props.put("key-A", "value-A");
66         props.put("key-B", "value-B");
67         orig.setProperties(props);
68
69         assertEquals("PdpGroup(name=my-name, version=1.2.3, description=my-description, "
70                 + "pdpGroupState=SAFE, properties={key-A=value-A, key-B=value-B}, "
71                 + "pdpSubgroups=[PdpSubGroup(pdpType=null, supportedPolicyTypes=[], policies=[], "
72                 + "currentInstanceCount=10, desiredInstanceCount=0, properties=null, pdpInstances=[]), "
73                 + "PdpSubGroup(pdpType=null, supportedPolicyTypes=[], policies=[], currentInstanceCount=11, "
74                 + "desiredInstanceCount=0, properties=null, pdpInstances=[])])", new PdpGroup(orig).toString());
75     }
76
77     @Test
78     public void testHashCode() {
79         PdpGroup group = new PdpGroup();
80         group.setDescription("A");
81         int hash = group.hashCode();
82
83         assertEquals(hash, group.hashCode());
84
85         group.setDescription("B");
86         assertTrue(hash != group.hashCode());
87     }
88
89     @Test
90     public void testCompareTo() {
91         PdpGroup pdpGroup0 = new PdpGroup();
92         pdpGroup0.setName("Name0");
93         pdpGroup0.setVersion("1.2.3");
94
95         PdpGroup pdpGroup1 = new PdpGroup();
96         pdpGroup1.setName("Name0");
97         pdpGroup1.setVersion("1.2.3");
98
99         assertEquals(0, pdpGroup0.compareTo(pdpGroup1));
100
101         PdpGroups pdpGroups = new PdpGroups();
102         pdpGroups.setGroups(new ArrayList<>());
103         pdpGroups.getGroups().add(pdpGroup0);
104         pdpGroups.getGroups().add(pdpGroup1);
105
106         List<Map<String, PdpGroup>> mapList = pdpGroups.toMapList();
107
108         assertEquals(1, mapList.size());
109         assertEquals(1, mapList.get(0).size());
110     }
111 }