78902ec7fc2808f9318836b4c59846dd314bd671
[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.assertNotEquals;
25
26 import java.util.Date;
27 import lombok.Getter;
28 import lombok.Setter;
29 import org.junit.Before;
30 import org.junit.Test;
31
32 public class DroolsPdpObjectTest {
33     private static final String PDP_ID = "my-id";
34     private static final String PDP_ID2 = "my-id2";
35     private static final String SITE = "my-site";
36     private static final String SITE2 = "my-site2";
37     private static final int PRIORITY = 11;
38     private static final int PRIORITY2 = 12;
39
40     private MyPdp pdp;
41
42     @Before
43     public void setUp() {
44         pdp = makePdp(PDP_ID, SITE, PRIORITY);
45     }
46
47     @Test
48     public void testEqualsObject() {
49         // self
50         assertEquals(pdp, pdp);
51
52         // same id
53         MyPdp pdp2 = new MyPdp();
54         pdp2.setPdpId(PDP_ID);
55         assertEquals(pdp, pdp2);
56
57         // different id
58         pdp2.setPdpId(PDP_ID2);
59         assertNotEquals(pdp, pdp2);
60
61         // different type of object
62         assertNotEquals(pdp, "");
63     }
64
65     @Test
66     public void testHashCode() {
67         int hc = pdp.hashCode();
68
69         // same data should yield same hash code
70         assertEquals(hc, pdp.hashCode());
71         assertEquals(hc, makePdp(PDP_ID, SITE, PRIORITY).hashCode());
72
73         // different data should yield different hash code
74         assertNotEquals(hc, makePdp(PDP_ID2, SITE, PRIORITY).hashCode());
75
76         // these fields have no impact on hash code
77         assertEquals(hc, makePdp(PDP_ID, SITE, PRIORITY2).hashCode());
78         assertEquals(hc, makePdp(PDP_ID, SITE2, PRIORITY).hashCode());
79
80         // should not throw an exception
81         new MyPdp().hashCode();
82     }
83
84     @Test
85     public void testNullSafeCompare() {
86         // self, when null
87         pdp.setSite(null);
88         assertEquals(0, pdp.comparePriority(pdp));
89
90         // both null
91         MyPdp pdp2 = makePdp(PDP_ID, null, PRIORITY);
92         assertEquals(0, pdp.comparePriority(pdp2));
93
94         // left null
95         pdp2 = makePdp(PDP_ID, SITE, PRIORITY);
96         assertEquals(-1, pdp.comparePriority(pdp2));
97
98         // right null - note: args are reversed
99         pdp2 = makePdp(PDP_ID, SITE, PRIORITY);
100         assertEquals(1, pdp2.comparePriority(pdp));
101     }
102
103     @Test
104     public void testComparePriorityDroolsPdp() {
105         // self
106         assertEquals(0, pdp.comparePriority(pdp));
107
108         // same
109         MyPdp pdp2 = makePdp(PDP_ID, SITE, PRIORITY);
110         assertEquals(0, pdp.comparePriority(pdp2));
111
112         // different site
113         pdp2 = makePdp(PDP_ID, SITE2, PRIORITY);
114         assertEquals(SITE.compareTo(SITE2), pdp.comparePriority(pdp2));
115
116         // different priority
117         pdp2 = makePdp(PDP_ID, SITE, PRIORITY2);
118         assertEquals(PRIORITY - PRIORITY2, pdp.comparePriority(pdp2));
119
120         // different id
121         pdp2 = makePdp(PDP_ID2, SITE, PRIORITY);
122         assertEquals(PDP_ID.compareTo(PDP_ID2), pdp.comparePriority(pdp2));
123     }
124
125     @Test
126     public void testComparePriorityDroolsPdpString() {
127         final int result = 1000;
128
129         // override other comparison method so we know if it's called
130         MyPdp pdp2 = new MyPdp() {
131             @Override
132             public int comparePriority(DroolsPdp other) {
133                 return result;
134             }
135         };
136
137         pdp2.setPdpId(PDP_ID);
138         pdp2.setSite(SITE2);
139         pdp2.setPriority(PRIORITY);
140
141         // should use overridden comparison method
142         assertEquals(result, pdp2.comparePriority(pdp, null));
143         assertEquals(result, pdp2.comparePriority(pdp, ""));
144
145         // should use normal comparison method
146         assertEquals(SITE2.compareTo(SITE), pdp2.comparePriority(pdp, SITE));
147     }
148
149     private MyPdp makePdp(String id, String site, int priority) {
150         MyPdp pdp2 = new MyPdp();
151
152         pdp2.setSite(site);
153         pdp2.setPdpId(id);
154         pdp2.setPriority(priority);
155
156         return pdp2;
157     }
158
159     @Getter
160     @Setter
161     private class MyPdp extends DroolsPdpObject {
162         private String pdpId;
163         private boolean designated;
164         private int priority;
165         private Date updatedDate;
166         private String site;
167         private Date designatedDate;
168     }
169 }