diff --git a/core/api/src/main/java/org/onosproject/cluster/DefaultPartition.java b/core/api/src/main/java/org/onosproject/cluster/DefaultPartition.java index 07a5d71ea5..d5cf068700 100644 --- a/core/api/src/main/java/org/onosproject/cluster/DefaultPartition.java +++ b/core/api/src/main/java/org/onosproject/cluster/DefaultPartition.java @@ -32,16 +32,30 @@ public class DefaultPartition implements Partition { private final PartitionId id; private final Collection members; - private DefaultPartition() { + /** + * Constructs an empty partition for the serializer. + */ + protected DefaultPartition() { id = null; members = null; } + /** + * Constructs a partition. + * + * @param id partition identifier + * @param members partition member nodes + */ public DefaultPartition(PartitionId id, Collection members) { this.id = checkNotNull(id); this.members = ImmutableSet.copyOf(members); } + /** + * Constructs a partition that is a copy of another. + * + * @param other partition to copy + */ public DefaultPartition(Partition other) { this.id = checkNotNull(other.getId()); this.members = ImmutableSet.copyOf(other.getMembers()); @@ -79,4 +93,4 @@ public class DefaultPartition implements Partition { return this.getId().equals(that.getId()) && Sets.symmetricDifference(Sets.newHashSet(this.members), Sets.newHashSet(that.members)).isEmpty(); } -} \ No newline at end of file +} diff --git a/core/api/src/test/java/org/onosproject/cluster/DefaultPartitionTest.java b/core/api/src/test/java/org/onosproject/cluster/DefaultPartitionTest.java new file mode 100644 index 0000000000..dbf2761866 --- /dev/null +++ b/core/api/src/test/java/org/onosproject/cluster/DefaultPartitionTest.java @@ -0,0 +1,96 @@ +/* + * Copyright 2016 Open Networking Laboratory + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.onosproject.cluster; + +import java.util.Collection; + +import org.junit.Test; + +import com.google.common.collect.ImmutableSet; +import com.google.common.testing.EqualsTester; + +import static org.hamcrest.MatcherAssert.assertThat; +import static org.hamcrest.Matchers.contains; +import static org.hamcrest.Matchers.hasSize; +import static org.hamcrest.Matchers.is; +import static org.hamcrest.Matchers.notNullValue; +import static org.hamcrest.Matchers.nullValue; +import static org.onlab.junit.ImmutableClassChecker.assertThatClassIsImmutableBaseClass; + +/** + * Unit tests for the default partition implementation. + */ +public class DefaultPartitionTest { + + NodeId id1 = new NodeId("1"); + NodeId id2 = new NodeId("2"); + NodeId id3 = new NodeId("3"); + + PartitionId pid1 = new PartitionId(1); + PartitionId pid2 = new PartitionId(2); + PartitionId pid3 = new PartitionId(3); + + DefaultPartition partition1 = new DefaultPartition(pid1, ImmutableSet.of(id1)); + DefaultPartition sameAsPartition1 = new DefaultPartition(pid1, ImmutableSet.of(id1)); + + DefaultPartition partition2 = new DefaultPartition(pid2, ImmutableSet.of(id2)); + DefaultPartition copyOfPartition2 = new DefaultPartition(partition2); + + DefaultPartition partition3 = new DefaultPartition(pid3, ImmutableSet.of(id1, id2, id3)); + + /** + * Checks that the default partition implementation is an immutable + * base class. + */ + @Test + public void checkImmutability() { + assertThatClassIsImmutableBaseClass(DefaultPartition.class); + } + + /** + * Tests operation of the equals(), hashCode(), and toString() methods. + */ + @Test + public void testEquals() { + new EqualsTester() + .addEqualityGroup(partition1, sameAsPartition1) + .addEqualityGroup(partition2, copyOfPartition2) + .addEqualityGroup(partition3) + .testEquals(); + } + + /** + * Tests that default partition objects are properly constructed. + */ + @Test + public void testConstruction() { + Collection members = partition3.getMembers(); + assertThat(members, notNullValue()); + assertThat(members, hasSize(3)); + assertThat(members, contains(id1, id2, id3)); + assertThat(partition3.getId(), is(pid3)); + } + + /** + * Tests the empty defaut partition constructor. + */ + @Test + public void testEmptyConstructor() { + DefaultPartition empty = new DefaultPartition(); + assertThat(empty.getId(), nullValue()); + assertThat(empty.getMembers(), nullValue()); + } +}