Ray Milkey | 5051040 | 2015-06-11 14:36:01 -0700 | [diff] [blame] | 1 | /* |
Brian O'Connor | 5ab426f | 2016-04-09 01:19:45 -0700 | [diff] [blame] | 2 | * Copyright 2015-present Open Networking Laboratory |
Ray Milkey | 5051040 | 2015-06-11 14:36:01 -0700 | [diff] [blame] | 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 | |
Madan Jampani | 620f70d | 2016-01-30 22:22:47 -0800 | [diff] [blame] | 18 | import java.util.Arrays; |
| 19 | |
Ray Milkey | 5051040 | 2015-06-11 14:36:01 -0700 | [diff] [blame] | 20 | import org.junit.Test; |
| 21 | |
| 22 | import com.google.common.testing.EqualsTester; |
| 23 | |
| 24 | import static org.hamcrest.Matchers.is; |
| 25 | import static org.junit.Assert.assertThat; |
| 26 | |
| 27 | /** |
| 28 | * Unit tests for the leadership event test. |
| 29 | */ |
| 30 | public class LeadershipEventTest { |
| 31 | private final NodeId node1 = new NodeId("1"); |
| 32 | private final NodeId node2 = new NodeId("2"); |
Madan Jampani | 620f70d | 2016-01-30 22:22:47 -0800 | [diff] [blame] | 33 | 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 Milkey | 5051040 | 2015-06-11 14:36:01 -0700 | [diff] [blame] | 36 | private final LeadershipEvent event1 = |
Madan Jampani | 620f70d | 2016-01-30 22:22:47 -0800 | [diff] [blame] | 37 | new LeadershipEvent(LeadershipEvent.Type.LEADER_CHANGED, lead1); |
Ray Milkey | 5051040 | 2015-06-11 14:36:01 -0700 | [diff] [blame] | 38 | 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 Jampani | 620f70d | 2016-01-30 22:22:47 -0800 | [diff] [blame] | 46 | new LeadershipEvent(LeadershipEvent.Type.LEADER_CHANGED, lead2); |
Ray Milkey | 5051040 | 2015-06-11 14:36:01 -0700 | [diff] [blame] | 47 | private final LeadershipEvent event4 = |
Madan Jampani | 620f70d | 2016-01-30 22:22:47 -0800 | [diff] [blame] | 48 | new LeadershipEvent(LeadershipEvent.Type.LEADER_AND_CANDIDATES_CHANGED, lead3); |
Ray Milkey | 5051040 | 2015-06-11 14:36:01 -0700 | [diff] [blame] | 49 | |
| 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 Milkey | 5051040 | 2015-06-11 14:36:01 -0700 | [diff] [blame] | 60 | .testEquals(); |
| 61 | } |
| 62 | |
| 63 | /** |
| 64 | * Tests that objects are created properly. |
| 65 | */ |
| 66 | @Test |
| 67 | public void checkConstruction() { |
Madan Jampani | 620f70d | 2016-01-30 22:22:47 -0800 | [diff] [blame] | 68 | assertThat(event1.type(), is(LeadershipEvent.Type.LEADER_CHANGED)); |
Ray Milkey | 5051040 | 2015-06-11 14:36:01 -0700 | [diff] [blame] | 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 | } |