X-Git-Url: https://git.opendaylight.org/gerrit/gitweb?a=blobdiff_plain;f=opendaylight%2Fmd-sal%2Fsal-distributed-datastore%2Fsrc%2Ftest%2Fjava%2Forg%2Fopendaylight%2Fcontroller%2Fcluster%2Fdatastore%2Fpersisted%2FFrontendShardDataTreeSnapshotMetadataTest.java;h=25b5128dbaf08b0ab29c940b333b58190ac8cdbe;hb=546cd1fd100dbaa36908b22c2f422320dbd8c4b2;hp=f99cf6fc41bb9af30314e7ccb614c067e0805c8b;hpb=edd61d79da614388134b0e0a618010c91e9c91bd;p=controller.git diff --git a/opendaylight/md-sal/sal-distributed-datastore/src/test/java/org/opendaylight/controller/cluster/datastore/persisted/FrontendShardDataTreeSnapshotMetadataTest.java b/opendaylight/md-sal/sal-distributed-datastore/src/test/java/org/opendaylight/controller/cluster/datastore/persisted/FrontendShardDataTreeSnapshotMetadataTest.java index f99cf6fc41..25b5128dba 100644 --- a/opendaylight/md-sal/sal-distributed-datastore/src/test/java/org/opendaylight/controller/cluster/datastore/persisted/FrontendShardDataTreeSnapshotMetadataTest.java +++ b/opendaylight/md-sal/sal-distributed-datastore/src/test/java/org/opendaylight/controller/cluster/datastore/persisted/FrontendShardDataTreeSnapshotMetadataTest.java @@ -10,7 +10,10 @@ package org.opendaylight.controller.cluster.datastore.persisted; import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertFalse; +import static org.junit.Assert.assertThrows; import static org.junit.Assert.assertTrue; + +import com.google.common.collect.ImmutableMap; import com.google.common.collect.Range; import com.google.common.collect.RangeSet; import com.google.common.collect.TreeRangeSet; @@ -21,11 +24,10 @@ import java.io.IOException; import java.io.ObjectInputStream; import java.io.ObjectOutputStream; import java.util.ArrayList; -import java.util.Collection; -import java.util.Collections; import java.util.HashMap; import java.util.List; import java.util.Map; +import java.util.Set; import org.junit.Test; import org.opendaylight.controller.cluster.access.concepts.ClientIdentifier; import org.opendaylight.controller.cluster.access.concepts.FrontendIdentifier; @@ -34,29 +36,29 @@ import org.opendaylight.controller.cluster.access.concepts.MemberName; public class FrontendShardDataTreeSnapshotMetadataTest { - @Test(expected = NullPointerException.class) - public final void testCreateMetadataSnapshotNullInput() { - new FrontendShardDataTreeSnapshotMetadata(null); + @Test + public void testCreateMetadataSnapshotNullInput() { + assertThrows(NullPointerException.class, () -> new FrontendShardDataTreeSnapshotMetadata(null)); } @Test - public final void testCreateMetadataSnapshotEmptyInput() throws Exception { + public void testCreateMetadataSnapshotEmptyInput() throws Exception { final FrontendShardDataTreeSnapshotMetadata emptyOrigSnapshot = createEmptyMetadataSnapshot(); - final FrontendShardDataTreeSnapshotMetadata emptyCopySnapshot = copy(emptyOrigSnapshot); + final FrontendShardDataTreeSnapshotMetadata emptyCopySnapshot = copy(emptyOrigSnapshot, 127); testMetadataSnapshotEqual(emptyOrigSnapshot, emptyCopySnapshot); } @Test - public final void testSerializeMetadataSnapshotWithOneClient() throws Exception { + public void testSerializeMetadataSnapshotWithOneClient() throws Exception { final FrontendShardDataTreeSnapshotMetadata origSnapshot = createMetadataSnapshot(1); - final FrontendShardDataTreeSnapshotMetadata copySnapshot = copy(origSnapshot); + final FrontendShardDataTreeSnapshotMetadata copySnapshot = copy(origSnapshot, 162); testMetadataSnapshotEqual(origSnapshot, copySnapshot); } @Test - public final void testSerializeMetadataSnapshotWithMoreClients() throws Exception { + public void testSerializeMetadataSnapshotWithMoreClients() throws Exception { final FrontendShardDataTreeSnapshotMetadata origSnapshot = createMetadataSnapshot(5); - final FrontendShardDataTreeSnapshotMetadata copySnapshot = copy(origSnapshot); + final FrontendShardDataTreeSnapshotMetadata copySnapshot = copy(origSnapshot, 314); testMetadataSnapshotEqual(origSnapshot, copySnapshot); } @@ -66,7 +68,7 @@ public class FrontendShardDataTreeSnapshotMetadataTest { final List origClientList = origSnapshot.getClients(); final List copyClientList = copySnapshot.getClients(); - assertTrue(origClientList.size() == copyClientList.size()); + assertEquals(origClientList.size(), copyClientList.size()); final Map origIdent = new HashMap<>(); final Map copyIdent = new HashMap<>(); @@ -79,40 +81,41 @@ public class FrontendShardDataTreeSnapshotMetadataTest { origIdent.values().forEach(client -> { final FrontendClientMetadata copyClient = copyIdent.get(client.getIdentifier()); testObject(client.getIdentifier(), copyClient.getIdentifier()); - assertTrue(client.getPurgedHistories().equals(copyClient.getPurgedHistories())); - assertTrue(client.getCurrentHistories().equals(copyClient.getCurrentHistories())); + assertEquals(client.getPurgedHistories(), copyClient.getPurgedHistories()); + assertEquals(client.getCurrentHistories(), copyClient.getCurrentHistories()); }); } private static FrontendShardDataTreeSnapshotMetadata createEmptyMetadataSnapshot() { - return new FrontendShardDataTreeSnapshotMetadata(Collections. emptyList()); + return new FrontendShardDataTreeSnapshotMetadata(List.of()); } private static FrontendShardDataTreeSnapshotMetadata createMetadataSnapshot(final int size) { - final List clients = new ArrayList<>(); + final List clients = new ArrayList<>(size); for (long i = 0; i < size; i++) { clients.add(createFrontedClientMetadata(i)); } return new FrontendShardDataTreeSnapshotMetadata(clients); } - private static FrontendClientMetadata createFrontedClientMetadata(final long i) { - final String index = String.valueOf(i); + private static FrontendClientMetadata createFrontedClientMetadata(final long num) { + final String index = String.valueOf(num); final String indexName = "test_" + index; final FrontendIdentifier frontendIdentifier = FrontendIdentifier.create(MemberName.forName(indexName), FrontendType.forName(index)); - final ClientIdentifier clientIdentifier = ClientIdentifier.create(frontendIdentifier, i); + final ClientIdentifier clientIdentifier = ClientIdentifier.create(frontendIdentifier, num); final RangeSet purgedHistories = TreeRangeSet.create(); - purgedHistories.add(Range.closed(UnsignedLong.ZERO, UnsignedLong.ONE)); + purgedHistories.add(Range.closedOpen(UnsignedLong.ZERO, UnsignedLong.ONE)); - final Collection currentHistories = Collections - .singleton(new FrontendHistoryMetadata(i, i, i, true)); + final Set currentHistories = Set.of( + new FrontendHistoryMetadata(num, num, true, ImmutableMap.of(UnsignedLong.ZERO, Boolean.TRUE), + purgedHistories)); return new FrontendClientMetadata(clientIdentifier, purgedHistories, currentHistories); } - private static final void testObject(final T object, final T equalObject) { + private static void testObject(final T object, final T equalObject) { assertEquals(object.hashCode(), equalObject.hashCode()); assertTrue(object.equals(object)); assertTrue(object.equals(equalObject)); @@ -121,13 +124,16 @@ public class FrontendShardDataTreeSnapshotMetadataTest { } @SuppressWarnings("unchecked") - private static T copy(final T o) throws IOException, ClassNotFoundException { + private static T copy(final T obj, final int expectedSize) throws IOException, ClassNotFoundException { final ByteArrayOutputStream bos = new ByteArrayOutputStream(); try (ObjectOutputStream oos = new ObjectOutputStream(bos)) { - oos.writeObject(o); + oos.writeObject(obj); } - try (ObjectInputStream ois = new ObjectInputStream(new ByteArrayInputStream(bos.toByteArray()))) { + final byte[] bytes = bos.toByteArray(); + assertEquals(expectedSize, bytes.length); + + try (ObjectInputStream ois = new ObjectInputStream(new ByteArrayInputStream(bytes))) { return (T) ois.readObject(); } }