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