aboutsummaryrefslogtreecommitdiffstats
path: root/feature-active-standby-management/src/test/java/org/onap/policy/drools/activestandby/DroolsPdpObjectTest.java
blob: 3f4a31b266151984f0760f49eb334c0f6fe2dd3a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
/*
 * ============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.assertNotEquals;

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
        assertEquals(pdp,pdp);

        // same id
        MyPdp pdp2 = new MyPdp();
        pdp2.setPdpId(PDP_ID);
        assertEquals(pdp,pdp2);

        // different id
        pdp2.setPdpId(PDP_ID2);
        assertNotEquals(pdp,pdp2);

        // different type of object
        assertNotEquals(pdp,"");
    }

    @Test
    public void testHashCode() {
        int hc = pdp.hashCode();

        // same data should yield same hash code
        assertEquals(hc, pdp.hashCode());
        assertEquals(hc, makePdp(PDP_ID, SITE, PRIORITY).hashCode());

        // different data should yield different hash code
        assertNotEquals(hc, makePdp(PDP_ID2, SITE, PRIORITY).hashCode());

        // these fields have no impact on hash code
        assertEquals(hc, makePdp(PDP_ID, SITE, PRIORITY2).hashCode());
        assertEquals(hc, makePdp(PDP_ID, SITE2, PRIORITY).hashCode());

        // should not throw an exception
        new MyPdp().hashCode();
    }

    @Test
    public void testNullSafeCompare() {
        // self, when null
        pdp.setSite(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.setSite(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.setSite(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 site;
        private Date designatedDate;
    }
}