Ray Milkey | 5051040 | 2015-06-11 14:36:01 -0700 | [diff] [blame] | 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 | } |