Add more serialization assertions 22/84622/3
authorRobert Varga <robert.varga@pantheon.tech>
Mon, 23 Sep 2019 06:23:29 +0000 (08:23 +0200)
committerRobert Varga <nite@hq.sk>
Tue, 24 Sep 2019 02:22:07 +0000 (02:22 +0000)
This adds a few more assertions to ensure our serialization format
does not move without us knowing.

Change-Id: Ieed326e9e57fb15ea46cd7a088d713222963e2e5
Signed-off-by: Robert Varga <robert.varga@pantheon.tech>
opendaylight/md-sal/cds-access-api/src/test/java/org/opendaylight/controller/cluster/access/concepts/AbstractIdentifierTest.java
opendaylight/md-sal/cds-access-api/src/test/java/org/opendaylight/controller/cluster/access/concepts/ClientIdentifierTest.java
opendaylight/md-sal/cds-access-api/src/test/java/org/opendaylight/controller/cluster/access/concepts/FrontendIdentifierTest.java
opendaylight/md-sal/cds-access-api/src/test/java/org/opendaylight/controller/cluster/access/concepts/FrontendTypeTest.java
opendaylight/md-sal/cds-access-api/src/test/java/org/opendaylight/controller/cluster/access/concepts/LocalHistoryIdentifierTest.java
opendaylight/md-sal/cds-access-api/src/test/java/org/opendaylight/controller/cluster/access/concepts/MemberNameTest.java
opendaylight/md-sal/cds-access-api/src/test/java/org/opendaylight/controller/cluster/access/concepts/TransactionIdentifierTest.java
opendaylight/md-sal/sal-distributed-datastore/src/test/java/org/opendaylight/controller/cluster/datastore/persisted/FrontendShardDataTreeSnapshotMetadataTest.java
opendaylight/md-sal/sal-distributed-datastore/src/test/java/org/opendaylight/controller/cluster/datastore/persisted/ShardDataTreeSnapshotTest.java

index 080e1e4..8fcc9fa 100644 (file)
@@ -26,6 +26,8 @@ public abstract class AbstractIdentifierTest<T extends Identifier> {
 
     abstract T equalObject();
 
+    abstract int expectedSize();
+
     @Test
     public final void testEquals() {
         assertTrue(object().equals(object()));
@@ -40,22 +42,26 @@ public abstract class AbstractIdentifierTest<T extends Identifier> {
         assertEquals(object().hashCode(), equalObject().hashCode());
     }
 
+
+    @Test
+    public final void testSerialization() throws Exception {
+        assertTrue(object().equals(copy(object())));
+        assertTrue(object().equals(copy(equalObject())));
+        assertFalse(differentObject().equals(copy(object())));
+    }
+
     @SuppressWarnings("unchecked")
-    private static <T> T copy(T obj) throws IOException, ClassNotFoundException {
+    private T copy(final T obj) throws IOException, ClassNotFoundException {
         final ByteArrayOutputStream bos = new ByteArrayOutputStream();
         try (ObjectOutputStream oos = new ObjectOutputStream(bos)) {
             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();
         }
     }
-
-    @Test
-    public final void testSerialization() throws Exception {
-        assertTrue(object().equals(copy(object())));
-        assertTrue(object().equals(copy(equalObject())));
-        assertFalse(differentObject().equals(copy(object())));
-    }
 }
index e14bf76..0cfd887 100644 (file)
@@ -7,6 +7,8 @@
  */
 package org.opendaylight.controller.cluster.access.concepts;
 
+import static org.junit.Assert.assertEquals;
+
 import java.io.ByteArrayInputStream;
 import java.io.ByteArrayOutputStream;
 import java.io.DataInputStream;
@@ -31,14 +33,21 @@ public class FrontendTypeTest extends AbstractIdentifierTest<FrontendType> {
         return FrontendType.forName("type-1");
     }
 
+    @Override
+    int expectedSize() {
+        return 104;
+    }
+
     @Test
     public void testWriteToReadFrom() throws Exception {
         final FrontendType type = FrontendType.forName("type");
         final ByteArrayOutputStream baos = new ByteArrayOutputStream();
         final DataOutputStream dos = new DataOutputStream(baos);
         type.writeTo(dos);
-        final FrontendType read =
-                FrontendType.readFrom(new DataInputStream(new ByteArrayInputStream(baos.toByteArray())));
+
+        final byte[] bytes = baos.toByteArray();
+        assertEquals(8, bytes.length);
+        final FrontendType read = FrontendType.readFrom(new DataInputStream(new ByteArrayInputStream(bytes)));
         Assert.assertEquals(type, read);
     }
 
index 2c377e5..136aa5a 100644 (file)
@@ -32,6 +32,10 @@ public class MemberNameTest extends AbstractIdentifierTest<MemberName> {
         return EQUAL_OBJECT;
     }
 
+    @Override
+    int expectedSize() {
+        return 101;
+    }
 
     @Test
     public void testCompareTo() {
index f873485..db52111 100644 (file)
@@ -44,21 +44,21 @@ public class FrontendShardDataTreeSnapshotMetadataTest {
     @Test
     public final 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 {
         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 {
         final FrontendShardDataTreeSnapshotMetadata origSnapshot = createMetadataSnapshot(5);
-        final FrontendShardDataTreeSnapshotMetadata copySnapshot = copy(origSnapshot);
+        final FrontendShardDataTreeSnapshotMetadata copySnapshot = copy(origSnapshot, 314);
         testMetadataSnapshotEqual(origSnapshot, copySnapshot);
     }
 
@@ -124,13 +124,16 @@ public class FrontendShardDataTreeSnapshotMetadataTest {
     }
 
     @SuppressWarnings("unchecked")
-    private static <T> T copy(final T obj) throws IOException, ClassNotFoundException {
+    private static <T> T copy(final T obj, final int expectedSize) throws IOException, ClassNotFoundException {
         final ByteArrayOutputStream bos = new ByteArrayOutputStream();
         try (ObjectOutputStream oos = new ObjectOutputStream(bos)) {
             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();
         }
     }
index 9e05cf4..9327f62 100644 (file)
@@ -47,8 +47,11 @@ public class ShardDataTreeSnapshotTest {
             snapshot.serialize(out);
         }
 
+        final byte[] bytes = bos.toByteArray();
+        assertEquals(242, bytes.length);
+
         ShardDataTreeSnapshot deserialized;
-        try (ObjectInputStream in = new ObjectInputStream(new ByteArrayInputStream(bos.toByteArray()))) {
+        try (ObjectInputStream in = new ObjectInputStream(new ByteArrayInputStream(bytes))) {
             deserialized = ShardDataTreeSnapshot.deserialize(in).getSnapshot();
         }
 
@@ -73,8 +76,11 @@ public class ShardDataTreeSnapshotTest {
             snapshot.serialize(out);
         }
 
+        final byte[] bytes = bos.toByteArray();
+        assertEquals(390, bytes.length);
+
         ShardDataTreeSnapshot deserialized;
-        try (ObjectInputStream in = new ObjectInputStream(new ByteArrayInputStream(bos.toByteArray()))) {
+        try (ObjectInputStream in = new ObjectInputStream(new ByteArrayInputStream(bytes))) {
             deserialized = ShardDataTreeSnapshot.deserialize(in).getSnapshot();
         }
 

©2013 OpenDaylight, A Linux Foundation Collaborative Project. All Rights Reserved.
OpenDaylight is a registered trademark of The OpenDaylight Project, Inc.
Linux Foundation and OpenDaylight are registered trademarks of the Linux Foundation.
Linux is a registered trademark of Linus Torvalds.