52000fd56032d4e52c28bcb952067c7abb85ed59
[policy/drools-pdp.git] /
1 /*
2  * ============LICENSE_START=======================================================
3  * ONAP
4  * ================================================================================
5  * Copyright (C) 2019 AT&T Intellectual Property. All rights reserved.
6  * ================================================================================
7  * Licensed under the Apache License, Version 2.0 (the "License");
8  * you may not use this file except in compliance with the License.
9  * You may obtain a copy of the License at
10  *
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing, software
14  * distributed under the License is distributed on an "AS IS" BASIS,
15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  * See the License for the specific language governing permissions and
17  * limitations under the License.
18  * ============LICENSE_END=========================================================
19  */
20
21 package org.onap.policy.drools.activestandby;
22
23 import static org.junit.Assert.assertEquals;
24 import static org.junit.Assert.assertFalse;
25 import static org.junit.Assert.assertTrue;
26
27 import java.util.Date;
28 import lombok.Getter;
29 import lombok.Setter;
30 import org.junit.Before;
31 import org.junit.Test;
32 import org.onap.policy.drools.activestandby.DroolsPdp;
33 import org.onap.policy.drools.activestandby.DroolsPdpObject;
34
35 public class DroolsPdpObjectTest {
36     private static final String PDP_ID = "my-id";
37     private static final String PDP_ID2 = "my-id2";
38     private static final String SITE = "my-site";
39     private static final String SITE2 = "my-site2";
40     private static final int PRIORITY = 11;
41     private static final int PRIORITY2 = 12;
42
43     private MyPdp pdp;
44
45     @Before
46     public void setUp() {
47         pdp = makePdp(PDP_ID, SITE, PRIORITY);
48     }
49
50     @Test
51     public void testEqualsObject() {
52         // self
53         assertTrue(pdp.equals(pdp));
54
55         // same id
56         MyPdp pdp2 = new MyPdp();
57         pdp2.setPdpId(PDP_ID);
58         assertTrue(pdp.equals(pdp2));
59
60         // different id
61         pdp2.setPdpId(PDP_ID2);
62         assertFalse(pdp.equals(pdp2));
63
64         // different type of object
65         assertFalse(pdp.equals(""));
66     }
67
68     @Test
69     public void testHashCode() {
70         int hc = pdp.hashCode();
71
72         // same data should yield same hash code
73         assertEquals(hc, pdp.hashCode());
74         assertEquals(hc, makePdp(PDP_ID, SITE, PRIORITY).hashCode());
75
76         // different data should yield different hash code
77         assertTrue(makePdp(PDP_ID2, SITE, PRIORITY).hashCode() != hc);
78
79         // these fields have no impact on hash code
80         assertEquals(hc, makePdp(PDP_ID, SITE, PRIORITY2).hashCode());
81         assertEquals(hc, makePdp(PDP_ID, SITE2, PRIORITY).hashCode());
82
83         // should not throw an exception
84         new MyPdp().hashCode();
85     }
86
87     @Test
88     public void testNullSafeCompare() {
89         // self, when null
90         pdp.setSite(null);
91         assertEquals(0, pdp.comparePriority(pdp));
92
93         // both null
94         MyPdp pdp2 = makePdp(PDP_ID, null, PRIORITY);
95         assertEquals(0, pdp.comparePriority(pdp2));
96
97         // left null
98         pdp2 = makePdp(PDP_ID, SITE, PRIORITY);
99         assertEquals(-1, pdp.comparePriority(pdp2));
100
101         // right null - note: args are reversed
102         pdp2 = makePdp(PDP_ID, SITE, PRIORITY);
103         assertEquals(1, pdp2.comparePriority(pdp));
104     }
105
106     @Test
107     public void testComparePriorityDroolsPdp() {
108         // self
109         assertEquals(0, pdp.comparePriority(pdp));
110
111         // same
112         MyPdp pdp2 = makePdp(PDP_ID, SITE, PRIORITY);
113         assertEquals(0, pdp.comparePriority(pdp2));
114
115         // different site
116         pdp2 = makePdp(PDP_ID, SITE2, PRIORITY);
117         assertEquals(SITE.compareTo(SITE2), pdp.comparePriority(pdp2));
118
119         // different priority
120         pdp2 = makePdp(PDP_ID, SITE, PRIORITY2);
121         assertEquals(PRIORITY - PRIORITY2, pdp.comparePriority(pdp2));
122
123         // different id
124         pdp2 = makePdp(PDP_ID2, SITE, PRIORITY);
125         assertEquals(PDP_ID.compareTo(PDP_ID2), pdp.comparePriority(pdp2));
126     }
127
128     @Test
129     public void testComparePriorityDroolsPdpString() {
130         final int result = 1000;
131
132         // override other comparison method so we know if it's called
133         MyPdp pdp2 = new MyPdp() {
134             @Override
135             public int comparePriority(DroolsPdp other) {
136                 return result;
137             }
138         };
139
140         pdp2.setPdpId(PDP_ID);
141         pdp2.setSite(SITE2);
142         pdp2.setPriority(PRIORITY);
143
144         // should use overridden comparison method
145         assertEquals(result, pdp2.comparePriority(pdp, null));
146         assertEquals(result, pdp2.comparePriority(pdp, ""));
147
148         // should use normal comparison method
149         assertEquals(SITE2.compareTo(SITE), pdp2.comparePriority(pdp, SITE));
150     }
151
152     private MyPdp makePdp(String id, String site, int priority) {
153         MyPdp pdp2 = new MyPdp();
154
155         pdp2.setSite(site);
156         pdp2.setPdpId(id);
157         pdp2.setPriority(priority);
158
159         return pdp2;
160     }
161
162     @Getter
163     @Setter
164     private class MyPdp extends DroolsPdpObject {
165         private String pdpId;
166         private boolean designated;
167         private int priority;
168         private Date updatedDate;
169         private String site;
170         private Date designatedDate;
171     }
172 }