2 * ============LICENSE_START=======================================================
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
11 * http://www.apache.org/licenses/LICENSE-2.0
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=========================================================
21 package org.onap.policy.drools.activestandby;
23 import static org.junit.Assert.assertEquals;
24 import static org.junit.Assert.assertFalse;
25 import static org.junit.Assert.assertTrue;
27 import java.util.Date;
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;
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;
47 pdp = makePdp(PDP_ID, SITE, PRIORITY);
51 public void testEqualsObject() {
53 assertTrue(pdp.equals(pdp));
56 MyPdp pdp2 = new MyPdp();
57 pdp2.setPdpId(PDP_ID);
58 assertTrue(pdp.equals(pdp2));
61 pdp2.setPdpId(PDP_ID2);
62 assertFalse(pdp.equals(pdp2));
64 // different type of object
65 assertFalse(pdp.equals(""));
69 public void testNullSafeCompare() {
71 pdp.setSiteName(null);
72 assertEquals(0, pdp.comparePriority(pdp));
75 MyPdp pdp2 = makePdp(PDP_ID, null, PRIORITY);
76 assertEquals(0, pdp.comparePriority(pdp2));
79 pdp2 = makePdp(PDP_ID, SITE, PRIORITY);
80 assertEquals(-1, pdp.comparePriority(pdp2));
82 // right null - note: args are reversed
83 pdp2 = makePdp(PDP_ID, SITE, PRIORITY);
84 assertEquals(1, pdp2.comparePriority(pdp));
88 public void testComparePriorityDroolsPdp() {
90 assertEquals(0, pdp.comparePriority(pdp));
93 MyPdp pdp2 = makePdp(PDP_ID, SITE, PRIORITY);
94 assertEquals(0, pdp.comparePriority(pdp2));
97 pdp2 = makePdp(PDP_ID, SITE2, PRIORITY);
98 assertEquals(SITE.compareTo(SITE2), pdp.comparePriority(pdp2));
100 // different priority
101 pdp2 = makePdp(PDP_ID, SITE, PRIORITY2);
102 assertEquals(PRIORITY - PRIORITY2, pdp.comparePriority(pdp2));
105 pdp2 = makePdp(PDP_ID2, SITE, PRIORITY);
106 assertEquals(PDP_ID.compareTo(PDP_ID2), pdp.comparePriority(pdp2));
110 public void testComparePriorityDroolsPdpString() {
111 final int result = 1000;
113 // override other comparison method so we know if it's called
114 MyPdp pdp2 = new MyPdp() {
116 public int comparePriority(DroolsPdp other) {
121 pdp2.setPdpId(PDP_ID);
122 pdp2.setSiteName(SITE2);
123 pdp2.setPriority(PRIORITY);
125 // should use overridden comparison method
126 assertEquals(result, pdp2.comparePriority(pdp, null));
127 assertEquals(result, pdp2.comparePriority(pdp, ""));
129 // should use normal comparison method
130 assertEquals(SITE2.compareTo(SITE), pdp2.comparePriority(pdp, SITE));
133 private MyPdp makePdp(String id, String site, int priority) {
134 MyPdp pdp2 = new MyPdp();
136 pdp2.setSiteName(site);
138 pdp2.setPriority(priority);
145 private class MyPdp extends DroolsPdpObject {
146 private String pdpId;
147 private boolean designated;
148 private int priority;
149 private Date updatedDate;
150 private String siteName;
151 private Date designatedDate;