blob: be0321bf85b091236dbfdc87e05feede45eb6607 [file] [log] [blame]
Ray Milkey50510402015-06-11 14:36:01 -07001/*
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 */
16package org.onosproject.cluster;
17
18import org.junit.Test;
19
20import com.google.common.testing.EqualsTester;
21
22import static org.hamcrest.Matchers.is;
23import static org.junit.Assert.assertThat;
24
25/**
26 * Unit tests for the leadership event test.
27 */
28public 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}