blob: defc6713f94d369ee94d26a16cd2a5646fb0a283 [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
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.collect.ImmutableList;
23import com.google.common.testing.EqualsTester;
24
Ray Milkey50510402015-06-11 14:36:01 -070025import static org.hamcrest.Matchers.hasSize;
26import static org.hamcrest.Matchers.is;
27import static org.junit.Assert.assertThat;
28
29/**
30 * Unit tests for the Leadership class.
31 */
32public class LeadershipTest {
33 private final NodeId node1 = new NodeId("1");
34 private final NodeId node2 = new NodeId("2");
Madan Jampani620f70d2016-01-30 22:22:47 -080035 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 Milkey50510402015-06-11 14:36:01 -070043
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 Jampani620f70d2016-01-30 22:22:47 -080066 assertThat(lead6.leader(), is(new Leader(node2, 1L, 2L)));
Ray Milkey50510402015-06-11 14:36:01 -070067 assertThat(lead6.topic(), is("topic1"));
Madan Jampani620f70d2016-01-30 22:22:47 -080068 assertThat(lead6.candidates(), hasSize(2));
69 assertThat(lead6.candidates().get(1), is(node1));
70 assertThat(lead6.candidates().get(0), is(node2));
Ray Milkey50510402015-06-11 14:36:01 -070071 }
Ray Milkey50510402015-06-11 14:36:01 -070072}