blob: 292c9a284947a306b69ff8aa72bd5ecac0fa88a6 [file] [log] [blame]
Ray Milkeycc6310f2016-02-12 19:02:10 -08001/*
Brian O'Connora09fe5b2017-08-03 21:12:30 -07002 * Copyright 2016-present Open Networking Foundation
Ray Milkeycc6310f2016-02-12 19:02:10 -08003 *
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
18import java.util.Collection;
19
Ray Milkeycc6310f2016-02-12 19:02:10 -080020import com.google.common.collect.ImmutableSet;
21import com.google.common.testing.EqualsTester;
Jordan Halterman00e92da2018-05-22 23:05:52 -070022import org.junit.Test;
Ray Milkeycc6310f2016-02-12 19:02:10 -080023
24import static org.hamcrest.MatcherAssert.assertThat;
25import static org.hamcrest.Matchers.contains;
26import static org.hamcrest.Matchers.hasSize;
27import static org.hamcrest.Matchers.is;
28import static org.hamcrest.Matchers.notNullValue;
29import static org.hamcrest.Matchers.nullValue;
30import static org.onlab.junit.ImmutableClassChecker.assertThatClassIsImmutableBaseClass;
31
32/**
33 * Unit tests for the default partition implementation.
34 */
35public class DefaultPartitionTest {
36
37 NodeId id1 = new NodeId("1");
38 NodeId id2 = new NodeId("2");
39 NodeId id3 = new NodeId("3");
40
41 PartitionId pid1 = new PartitionId(1);
42 PartitionId pid2 = new PartitionId(2);
43 PartitionId pid3 = new PartitionId(3);
44
Jordan Halterman00e92da2018-05-22 23:05:52 -070045 DefaultPartition partition1 = new DefaultPartition(pid1, ImmutableSet.of(id1));
46 DefaultPartition sameAsPartition1 = new DefaultPartition(pid1, ImmutableSet.of(id1));
Ray Milkeycc6310f2016-02-12 19:02:10 -080047
Jordan Halterman00e92da2018-05-22 23:05:52 -070048 DefaultPartition partition2 = new DefaultPartition(pid2, ImmutableSet.of(id2));
Ray Milkeycc6310f2016-02-12 19:02:10 -080049 DefaultPartition copyOfPartition2 = new DefaultPartition(partition2);
50
Jordan Halterman00e92da2018-05-22 23:05:52 -070051 DefaultPartition partition3 = new DefaultPartition(pid3, ImmutableSet.of(id1, id2, id3));
Ray Milkeycc6310f2016-02-12 19:02:10 -080052
53 /**
54 * Checks that the default partition implementation is an immutable
55 * base class.
56 */
57 @Test
58 public void checkImmutability() {
59 assertThatClassIsImmutableBaseClass(DefaultPartition.class);
60 }
61
62 /**
63 * Tests operation of the equals(), hashCode(), and toString() methods.
64 */
65 @Test
66 public void testEquals() {
67 new EqualsTester()
68 .addEqualityGroup(partition1, sameAsPartition1)
69 .addEqualityGroup(partition2, copyOfPartition2)
70 .addEqualityGroup(partition3)
71 .testEquals();
72 }
73
74 /**
75 * Tests that default partition objects are properly constructed.
76 */
77 @Test
78 public void testConstruction() {
79 Collection<NodeId> members = partition3.getMembers();
80 assertThat(members, notNullValue());
81 assertThat(members, hasSize(3));
82 assertThat(members, contains(id1, id2, id3));
83 assertThat(partition3.getId(), is(pid3));
84 }
85
86 /**
87 * Tests the empty defaut partition constructor.
88 */
89 @Test
90 public void testEmptyConstructor() {
91 DefaultPartition empty = new DefaultPartition();
92 assertThat(empty.getId(), nullValue());
93 assertThat(empty.getMembers(), nullValue());
94 }
95}