Add more serialization assertions 81/84681/1
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:20 +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>
(cherry picked from commit b394690b52324ae91124735c4ec19acc3389d4ec)

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 080e1e46b4854a74615e1ac8d9a08bd3066cb01c..8fcc9fa1f5ffe1f7eabf3326f65982ac56ed6b64 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 4871def180b7a63d529d9b4facb8f4c554dde7cf..0908659487cec6282eab05fcfd217a5bbd0813f2 100644 (file)
@@ -29,4 +29,9 @@ public class ClientIdentifierTest extends AbstractIdentifierTest<ClientIdentifie
     ClientIdentifier equalObject() {
         return EQUAL_OBJECT;
     }
+
+    @Override
+    int expectedSize() {
+        return 114;
+    }
 }
index cc56fc1101b3efecba6dbfa0285bd207b4734761..cc7124483b4e2de4683077df62e44321af6b1ca7 100644 (file)
@@ -30,4 +30,9 @@ public class FrontendIdentifierTest extends AbstractIdentifierTest<FrontendIdent
     FrontendIdentifier equalObject() {
         return EQUAL_OBJECT;
     }
+
+    @Override
+    int expectedSize() {
+        return 115;
+    }
 }
index e14bf766a244add8ff171ebf37c2b3da59ed227d..0cfd887d565dff9bc32f7aa136a795180b9d6499 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 159896b9f4fc7cd09262702adad91de3ef39a834..161370deff8be4b47309b9d5923fca00e697e906 100644 (file)
@@ -30,4 +30,9 @@ public class LocalHistoryIdentifierTest extends AbstractIdentifierTest<LocalHist
     LocalHistoryIdentifier equalObject() {
         return EQUAL_OBJECT;
     }
+
+    @Override
+    int expectedSize() {
+        return 121;
+    }
 }
index 2c377e5efd14d32ad08b038d66854db58adc7963..136aa5a0f31245aaffea0ae892ea798537e95fce 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 e1c281ac2bdf2089482c6774a1553877dcb1cc1c..b33b61c49b2c3e1f52482e95dee63a4acf0c0439 100644 (file)
@@ -31,4 +31,9 @@ public class TransactionIdentifierTest extends AbstractIdentifierTest<Transactio
     TransactionIdentifier equalObject() {
         return EQUAL_OBJECT;
     }
+
+    @Override
+    int expectedSize() {
+        return 121;
+    }
 }
index f8734850a5f4528286d653103f720c2f7eaab1df..db52111ad22199270bac587fd8ce10cbc476f84a 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 9e05cf4860d5e3faaf7d16144e675f5cc207a3d8..9327f6262fea5d000e0692c33c728ac2d2afd23b 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();
         }