blob: e775273406436d94b5961fc7c08bd46a3f556642 [file] [log] [blame]
Ray Milkey50510402015-06-11 14:36:01 -07001/*
Brian O'Connor5ab426f2016-04-09 01:19:45 -07002 * Copyright 2015-present Open Networking Laboratory
Ray Milkey50510402015-06-11 14:36:01 -07003 *
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
Madan Jampani620f70d2016-01-30 22:22:47 -080018import java.util.Arrays;
19
Ray Milkey50510402015-06-11 14:36:01 -070020import org.junit.Test;
21
22import com.google.common.testing.EqualsTester;
23
24import static org.hamcrest.Matchers.is;
25import static org.junit.Assert.assertThat;
26
27/**
28 * Unit tests for the leadership event test.
29 */
30public class LeadershipEventTest {
31 private final NodeId node1 = new NodeId("1");
32 private final NodeId node2 = new NodeId("2");
Madan Jampani620f70d2016-01-30 22:22:47 -080033 private final Leadership lead1 = new Leadership("topic1", new Leader(node1, 1L, 2L), Arrays.asList(node1));
34 private final Leadership lead2 = new Leadership("topic1", new Leader(node1, 1L, 2L), Arrays.asList(node1, node2));
35 private final Leadership lead3 = new Leadership("topic1", new Leader(node2, 1L, 2L), Arrays.asList(node2));
Ray Milkey50510402015-06-11 14:36:01 -070036 private final LeadershipEvent event1 =
Madan Jampani620f70d2016-01-30 22:22:47 -080037 new LeadershipEvent(LeadershipEvent.Type.LEADER_CHANGED, lead1);
Ray Milkey50510402015-06-11 14:36:01 -070038 private final long time = System.currentTimeMillis();
39 private final LeadershipEvent event2 =
40 new LeadershipEvent(LeadershipEvent.Type.CANDIDATES_CHANGED,
41 lead2, time);
42 private final LeadershipEvent sameAsEvent2 =
43 new LeadershipEvent(LeadershipEvent.Type.CANDIDATES_CHANGED,
44 lead2, time);
45 private final LeadershipEvent event3 =
Madan Jampani620f70d2016-01-30 22:22:47 -080046 new LeadershipEvent(LeadershipEvent.Type.LEADER_CHANGED, lead2);
Ray Milkey50510402015-06-11 14:36:01 -070047 private final LeadershipEvent event4 =
Madan Jampani620f70d2016-01-30 22:22:47 -080048 new LeadershipEvent(LeadershipEvent.Type.LEADER_AND_CANDIDATES_CHANGED, lead3);
Ray Milkey50510402015-06-11 14:36:01 -070049
50 /**
51 * Tests for proper operation of equals(), hashCode() and toString() methods.
52 */
53 @Test
54 public void checkEquals() {
55 new EqualsTester()
56 .addEqualityGroup(event1)
57 .addEqualityGroup(event2, sameAsEvent2)
58 .addEqualityGroup(event3)
59 .addEqualityGroup(event4)
Ray Milkey50510402015-06-11 14:36:01 -070060 .testEquals();
61 }
62
63 /**
64 * Tests that objects are created properly.
65 */
66 @Test
67 public void checkConstruction() {
Madan Jampani620f70d2016-01-30 22:22:47 -080068 assertThat(event1.type(), is(LeadershipEvent.Type.LEADER_CHANGED));
Ray Milkey50510402015-06-11 14:36:01 -070069 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}