blob: 105c94ec89931f5f06c0ec42e6077f9bd15811de [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
20import org.junit.Test;
21
22import com.google.common.collect.ImmutableSet;
23import com.google.common.testing.EqualsTester;
Jordan Halterman07f052b2017-10-08 14:22:41 -070024import org.onosproject.core.Version;
Ray Milkeycc6310f2016-02-12 19:02:10 -080025
26import static org.hamcrest.MatcherAssert.assertThat;
27import static org.hamcrest.Matchers.contains;
28import static org.hamcrest.Matchers.hasSize;
29import static org.hamcrest.Matchers.is;
30import static org.hamcrest.Matchers.notNullValue;
31import static org.hamcrest.Matchers.nullValue;
32import static org.onlab.junit.ImmutableClassChecker.assertThatClassIsImmutableBaseClass;
33
34/**
35 * Unit tests for the default partition implementation.
36 */
37public class DefaultPartitionTest {
38
39 NodeId id1 = new NodeId("1");
40 NodeId id2 = new NodeId("2");
41 NodeId id3 = new NodeId("3");
42
43 PartitionId pid1 = new PartitionId(1);
44 PartitionId pid2 = new PartitionId(2);
45 PartitionId pid3 = new PartitionId(3);
46
Jordan Halterman07f052b2017-10-08 14:22:41 -070047 DefaultPartition partition1 = new DefaultPartition(pid1, Version.version("1.0.0"), ImmutableSet.of(id1));
48 DefaultPartition sameAsPartition1 = new DefaultPartition(pid1, Version.version("1.0.0"), ImmutableSet.of(id1));
Ray Milkeycc6310f2016-02-12 19:02:10 -080049
Jordan Halterman07f052b2017-10-08 14:22:41 -070050 DefaultPartition partition2 = new DefaultPartition(pid2, Version.version("1.0.0"), ImmutableSet.of(id2));
Ray Milkeycc6310f2016-02-12 19:02:10 -080051 DefaultPartition copyOfPartition2 = new DefaultPartition(partition2);
52
Jordan Halterman07f052b2017-10-08 14:22:41 -070053 DefaultPartition partition3 = new DefaultPartition(pid3, Version.version("1.0.0"), ImmutableSet.of(id1, id2, id3));
Ray Milkeycc6310f2016-02-12 19:02:10 -080054
55 /**
56 * Checks that the default partition implementation is an immutable
57 * base class.
58 */
59 @Test
60 public void checkImmutability() {
61 assertThatClassIsImmutableBaseClass(DefaultPartition.class);
62 }
63
64 /**
65 * Tests operation of the equals(), hashCode(), and toString() methods.
66 */
67 @Test
68 public void testEquals() {
69 new EqualsTester()
70 .addEqualityGroup(partition1, sameAsPartition1)
71 .addEqualityGroup(partition2, copyOfPartition2)
72 .addEqualityGroup(partition3)
73 .testEquals();
74 }
75
76 /**
77 * Tests that default partition objects are properly constructed.
78 */
79 @Test
80 public void testConstruction() {
81 Collection<NodeId> members = partition3.getMembers();
82 assertThat(members, notNullValue());
83 assertThat(members, hasSize(3));
84 assertThat(members, contains(id1, id2, id3));
85 assertThat(partition3.getId(), is(pid3));
86 }
87
88 /**
89 * Tests the empty defaut partition constructor.
90 */
91 @Test
92 public void testEmptyConstructor() {
93 DefaultPartition empty = new DefaultPartition();
94 assertThat(empty.getId(), nullValue());
95 assertThat(empty.getMembers(), nullValue());
96 }
97}