Ray Milkey
Committed by Brian O'Connor

Unit tests for Leadership and LeadershipEvent classes

Change-Id: I0bd902348607f27976ad39f18ec4b3fc6f1b1d1a
...@@ -132,7 +132,7 @@ public class Leadership { ...@@ -132,7 +132,7 @@ public class Leadership {
132 132
133 @Override 133 @Override
134 public int hashCode() { 134 public int hashCode() {
135 - return Objects.hash(topic, leader, candidates, epoch); 135 + return Objects.hash(topic, leader, candidates, epoch, electedTime);
136 } 136 }
137 137
138 @Override 138 @Override
...@@ -143,7 +143,10 @@ public class Leadership { ...@@ -143,7 +143,10 @@ public class Leadership {
143 if (obj instanceof Leadership) { 143 if (obj instanceof Leadership) {
144 final Leadership other = (Leadership) obj; 144 final Leadership other = (Leadership) obj;
145 return Objects.equals(this.topic, other.topic) && 145 return Objects.equals(this.topic, other.topic) &&
146 - Objects.equals(this.epoch, other.epoch); 146 + Objects.equals(this.leader, other.leader) &&
147 + Objects.equals(this.candidates, other.candidates) &&
148 + Objects.equals(this.epoch, other.epoch) &&
149 + Objects.equals(this.electedTime, other.electedTime);
147 } 150 }
148 return false; 151 return false;
149 } 152 }
......
1 +/*
2 + * Copyright 2015 Open Networking Laboratory
3 + *
4 + * Licensed under the Apache License, Version 2.0 (the "License");
5 + * you may not use this file except in compliance with the License.
6 + * You may obtain a copy of the License at
7 + *
8 + * http://www.apache.org/licenses/LICENSE-2.0
9 + *
10 + * Unless required by applicable law or agreed to in writing, software
11 + * distributed under the License is distributed on an "AS IS" BASIS,
12 + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 + * See the License for the specific language governing permissions and
14 + * limitations under the License.
15 + */
16 +package org.onosproject.cluster;
17 +
18 +import org.junit.Test;
19 +
20 +import com.google.common.testing.EqualsTester;
21 +
22 +import static org.hamcrest.Matchers.is;
23 +import static org.junit.Assert.assertThat;
24 +
25 +/**
26 + * Unit tests for the leadership event test.
27 + */
28 +public class LeadershipEventTest {
29 + private final NodeId node1 = new NodeId("1");
30 + private final NodeId node2 = new NodeId("2");
31 + private final Leadership lead1 = new Leadership("topic1", node1, 1L, 2L);
32 + private final Leadership lead2 = new Leadership("topic1", node2, 1L, 2L);
33 + private final LeadershipEvent event1 =
34 + new LeadershipEvent(LeadershipEvent.Type.LEADER_ELECTED, lead1);
35 + private final long time = System.currentTimeMillis();
36 + private final LeadershipEvent event2 =
37 + new LeadershipEvent(LeadershipEvent.Type.CANDIDATES_CHANGED,
38 + lead2, time);
39 + private final LeadershipEvent sameAsEvent2 =
40 + new LeadershipEvent(LeadershipEvent.Type.CANDIDATES_CHANGED,
41 + lead2, time);
42 + private final LeadershipEvent event3 =
43 + new LeadershipEvent(LeadershipEvent.Type.LEADER_BOOTED, lead1);
44 + private final LeadershipEvent event4 =
45 + new LeadershipEvent(LeadershipEvent.Type.LEADER_REELECTED, lead1);
46 + private final LeadershipEvent event5 =
47 + new LeadershipEvent(LeadershipEvent.Type.LEADER_REELECTED, lead2);
48 +
49 + /**
50 + * Tests for proper operation of equals(), hashCode() and toString() methods.
51 + */
52 + @Test
53 + public void checkEquals() {
54 + new EqualsTester()
55 + .addEqualityGroup(event1)
56 + .addEqualityGroup(event2, sameAsEvent2)
57 + .addEqualityGroup(event3)
58 + .addEqualityGroup(event4)
59 + .addEqualityGroup(event5)
60 + .testEquals();
61 + }
62 +
63 + /**
64 + * Tests that objects are created properly.
65 + */
66 + @Test
67 + public void checkConstruction() {
68 + assertThat(event1.type(), is(LeadershipEvent.Type.LEADER_ELECTED));
69 + assertThat(event1.subject(), is(lead1));
70 +
71 + assertThat(event2.time(), is(time));
72 + assertThat(event2.type(), is(LeadershipEvent.Type.CANDIDATES_CHANGED));
73 + assertThat(event2.subject(), is(lead2));
74 + }
75 +
76 +}
1 +/*
2 + * Copyright 2015 Open Networking Laboratory
3 + *
4 + * Licensed under the Apache License, Version 2.0 (the "License");
5 + * you may not use this file except in compliance with the License.
6 + * You may obtain a copy of the License at
7 + *
8 + * http://www.apache.org/licenses/LICENSE-2.0
9 + *
10 + * Unless required by applicable law or agreed to in writing, software
11 + * distributed under the License is distributed on an "AS IS" BASIS,
12 + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 + * See the License for the specific language governing permissions and
14 + * limitations under the License.
15 + */
16 +package org.onosproject.cluster;
17 +
18 +import org.junit.Test;
19 +
20 +import com.google.common.collect.ImmutableList;
21 +import com.google.common.testing.EqualsTester;
22 +
23 +import static org.hamcrest.Matchers.contains;
24 +import static org.hamcrest.Matchers.hasSize;
25 +import static org.hamcrest.Matchers.is;
26 +import static org.junit.Assert.assertThat;
27 +
28 +/**
29 + * Unit tests for the Leadership class.
30 + */
31 +public class LeadershipTest {
32 + private final NodeId node1 = new NodeId("1");
33 + private final NodeId node2 = new NodeId("2");
34 + private final Leadership lead1 = new Leadership("topic1", node1, 1L, 2L);
35 + private final Leadership sameAsLead1 = new Leadership("topic1", node1, 1L, 2L);
36 + private final Leadership lead2 = new Leadership("topic2", node1, 1L, 2L);
37 + private final Leadership lead3 = new Leadership("topic1", node1, 2L, 2L);
38 + private final Leadership lead4 = new Leadership("topic1", node1, 3L, 2L);
39 + private final Leadership lead5 = new Leadership("topic1", node1, 3L, 3L);
40 + private final Leadership lead6 = new Leadership("topic1", node1,
41 + ImmutableList.of(node2), 1L, 2L);
42 + private final Leadership lead7 = new Leadership("topic1",
43 + ImmutableList.of(node2), 1L, 2L);
44 +
45 + /**
46 + * Tests for proper operation of equals(), hashCode() and toString() methods.
47 + */
48 + @Test
49 + public void checkEquals() {
50 + new EqualsTester()
51 + .addEqualityGroup(lead1, sameAsLead1)
52 + .addEqualityGroup(lead2)
53 + .addEqualityGroup(lead3)
54 + .addEqualityGroup(lead4)
55 + .addEqualityGroup(lead5)
56 + .addEqualityGroup(lead6)
57 + .addEqualityGroup(lead7)
58 + .testEquals();
59 + }
60 +
61 + /**
62 + * Tests that objects are created properly and accessor methods return
63 + * the correct vsalues.
64 + */
65 + @Test
66 + public void checkConstruction() {
67 + assertThat(lead6.electedTime(), is(2L));
68 + assertThat(lead6.epoch(), is(1L));
69 + assertThat(lead6.leader(), is(node1));
70 + assertThat(lead6.topic(), is("topic1"));
71 + assertThat(lead6.candidates(), hasSize(1));
72 + assertThat(lead6.candidates(), contains(node2));
73 + }
74 +
75 +}