diff options
Diffstat (limited to 'feature-active-standby-management/src/test/java/org/onap/policy/drools/activestandby/DroolsPdpObjectTest.java')
-rw-r--r-- | feature-active-standby-management/src/test/java/org/onap/policy/drools/activestandby/DroolsPdpObjectTest.java | 153 |
1 files changed, 153 insertions, 0 deletions
diff --git a/feature-active-standby-management/src/test/java/org/onap/policy/drools/activestandby/DroolsPdpObjectTest.java b/feature-active-standby-management/src/test/java/org/onap/policy/drools/activestandby/DroolsPdpObjectTest.java new file mode 100644 index 00000000..310f9110 --- /dev/null +++ b/feature-active-standby-management/src/test/java/org/onap/policy/drools/activestandby/DroolsPdpObjectTest.java @@ -0,0 +1,153 @@ +/* + * ============LICENSE_START======================================================= + * ONAP + * ================================================================================ + * Copyright (C) 2019 AT&T Intellectual Property. All rights reserved. + * ================================================================================ + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * ============LICENSE_END========================================================= + */ + +package org.onap.policy.drools.activestandby; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertFalse; +import static org.junit.Assert.assertTrue; + +import java.util.Date; +import lombok.Getter; +import lombok.Setter; +import org.junit.Before; +import org.junit.Test; +import org.onap.policy.drools.activestandby.DroolsPdp; +import org.onap.policy.drools.activestandby.DroolsPdpObject; + +public class DroolsPdpObjectTest { + private static final String PDP_ID = "my-id"; + private static final String PDP_ID2 = "my-id2"; + private static final String SITE = "my-site"; + private static final String SITE2 = "my-site2"; + private static final int PRIORITY = 11; + private static final int PRIORITY2 = 12; + + private MyPdp pdp; + + @Before + public void setUp() { + pdp = makePdp(PDP_ID, SITE, PRIORITY); + } + + @Test + public void testEqualsObject() { + // self + assertTrue(pdp.equals(pdp)); + + // same id + MyPdp pdp2 = new MyPdp(); + pdp2.setPdpId(PDP_ID); + assertTrue(pdp.equals(pdp2)); + + // different id + pdp2.setPdpId(PDP_ID2); + assertFalse(pdp.equals(pdp2)); + + // different type of object + assertFalse(pdp.equals("")); + } + + @Test + public void testNullSafeCompare() { + // self, when null + pdp.setSiteName(null); + assertEquals(0, pdp.comparePriority(pdp)); + + // both null + MyPdp pdp2 = makePdp(PDP_ID, null, PRIORITY); + assertEquals(0, pdp.comparePriority(pdp2)); + + // left null + pdp2 = makePdp(PDP_ID, SITE, PRIORITY); + assertEquals(-1, pdp.comparePriority(pdp2)); + + // right null - note: args are reversed + pdp2 = makePdp(PDP_ID, SITE, PRIORITY); + assertEquals(1, pdp2.comparePriority(pdp)); + } + + @Test + public void testComparePriorityDroolsPdp() { + // self + assertEquals(0, pdp.comparePriority(pdp)); + + // same + MyPdp pdp2 = makePdp(PDP_ID, SITE, PRIORITY); + assertEquals(0, pdp.comparePriority(pdp2)); + + // different site + pdp2 = makePdp(PDP_ID, SITE2, PRIORITY); + assertEquals(SITE.compareTo(SITE2), pdp.comparePriority(pdp2)); + + // different priority + pdp2 = makePdp(PDP_ID, SITE, PRIORITY2); + assertEquals(PRIORITY - PRIORITY2, pdp.comparePriority(pdp2)); + + // different id + pdp2 = makePdp(PDP_ID2, SITE, PRIORITY); + assertEquals(PDP_ID.compareTo(PDP_ID2), pdp.comparePriority(pdp2)); + } + + @Test + public void testComparePriorityDroolsPdpString() { + final int result = 1000; + + // override other comparison method so we know if it's called + MyPdp pdp2 = new MyPdp() { + @Override + public int comparePriority(DroolsPdp other) { + return result; + } + }; + + pdp2.setPdpId(PDP_ID); + pdp2.setSiteName(SITE2); + pdp2.setPriority(PRIORITY); + + // should use overridden comparison method + assertEquals(result, pdp2.comparePriority(pdp, null)); + assertEquals(result, pdp2.comparePriority(pdp, "")); + + // should use normal comparison method + assertEquals(SITE2.compareTo(SITE), pdp2.comparePriority(pdp, SITE)); + } + + private MyPdp makePdp(String id, String site, int priority) { + MyPdp pdp2 = new MyPdp(); + + pdp2.setSiteName(site); + pdp2.setPdpId(id); + pdp2.setPriority(priority); + + return pdp2; + } + + @Getter + @Setter + private class MyPdp extends DroolsPdpObject { + private String pdpId; + private boolean designated; + private int priority; + private Date updatedDate; + private String siteName; + private Date designatedDate; + } +} |