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 | |
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.collect.ImmutableList; |
| 23 | import com.google.common.testing.EqualsTester; |
| 24 | |
Ray Milkey | 5051040 | 2015-06-11 14:36:01 -0700 | [diff] [blame] | 25 | import static org.hamcrest.Matchers.hasSize; |
| 26 | import static org.hamcrest.Matchers.is; |
| 27 | import static org.junit.Assert.assertThat; |
| 28 | |
| 29 | /** |
| 30 | * Unit tests for the Leadership class. |
| 31 | */ |
| 32 | public class LeadershipTest { |
| 33 | private final NodeId node1 = new NodeId("1"); |
| 34 | private final NodeId node2 = new NodeId("2"); |
Madan Jampani | 620f70d | 2016-01-30 22:22:47 -0800 | [diff] [blame] | 35 | private final Leadership lead1 = new Leadership("topic1", new Leader(node1, 1L, 2L), Arrays.asList(node1)); |
| 36 | private final Leadership sameAsLead1 = new Leadership("topic1", new Leader(node1, 1L, 2L), Arrays.asList(node1)); |
| 37 | private final Leadership lead2 = new Leadership("topic2", new Leader(node1, 1L, 2L), Arrays.asList(node1)); |
| 38 | private final Leadership lead3 = new Leadership("topic1", new Leader(node1, 2L, 2L), Arrays.asList(node1)); |
| 39 | private final Leadership lead4 = new Leadership("topic1", new Leader(node1, 3L, 2L), Arrays.asList(node1)); |
| 40 | private final Leadership lead5 = new Leadership("topic1", new Leader(node1, 3L, 3L), Arrays.asList(node1)); |
| 41 | private final Leadership lead6 = new Leadership("topic1", new Leader(node2, 1L, 2L), Arrays.asList(node2, node1)); |
| 42 | private final Leadership lead7 = new Leadership("topic1", null, ImmutableList.of()); |
Ray Milkey | 5051040 | 2015-06-11 14:36:01 -0700 | [diff] [blame] | 43 | |
| 44 | /** |
| 45 | * Tests for proper operation of equals(), hashCode() and toString() methods. |
| 46 | */ |
| 47 | @Test |
| 48 | public void checkEquals() { |
| 49 | new EqualsTester() |
| 50 | .addEqualityGroup(lead1, sameAsLead1) |
| 51 | .addEqualityGroup(lead2) |
| 52 | .addEqualityGroup(lead3) |
| 53 | .addEqualityGroup(lead4) |
| 54 | .addEqualityGroup(lead5) |
| 55 | .addEqualityGroup(lead6) |
| 56 | .addEqualityGroup(lead7) |
| 57 | .testEquals(); |
| 58 | } |
| 59 | |
| 60 | /** |
| 61 | * Tests that objects are created properly and accessor methods return |
| 62 | * the correct vsalues. |
| 63 | */ |
| 64 | @Test |
| 65 | public void checkConstruction() { |
Madan Jampani | 620f70d | 2016-01-30 22:22:47 -0800 | [diff] [blame] | 66 | assertThat(lead6.leader(), is(new Leader(node2, 1L, 2L))); |
Ray Milkey | 5051040 | 2015-06-11 14:36:01 -0700 | [diff] [blame] | 67 | assertThat(lead6.topic(), is("topic1")); |
Madan Jampani | 620f70d | 2016-01-30 22:22:47 -0800 | [diff] [blame] | 68 | assertThat(lead6.candidates(), hasSize(2)); |
| 69 | assertThat(lead6.candidates().get(1), is(node1)); |
| 70 | assertThat(lead6.candidates().get(0), is(node2)); |
Ray Milkey | 5051040 | 2015-06-11 14:36:01 -0700 | [diff] [blame] | 71 | } |
Ray Milkey | 5051040 | 2015-06-11 14:36:01 -0700 | [diff] [blame] | 72 | } |