From 0096ccec7c45aa0d22cf56250b06517881ca2300 Mon Sep 17 00:00:00 2001 From: Robert Varga Date: Fri, 2 Dec 2022 07:55:33 +0100 Subject: [PATCH] Add sal-akka-raft serialization assertions We need to be in control of serialized payload size. This patch adds the assertions on sizes. Change-Id: Ic73724d0acc51d7d7f28acb392fe569f719762c9 Signed-off-by: Robert Varga --- .../raft/base/messages/TimeoutNowTest.java | 6 ++-- .../behaviors/FollowerIdentifierTest.java | 7 ++-- .../raft/client/messages/ShutdownTest.java | 6 ++-- .../raft/messages/AppendEntriesReplyTest.java | 19 +++++++---- .../raft/messages/AppendEntriesTest.java | 32 +++++++++++-------- .../messages/InstallSnapshotReplyTest.java | 7 ++-- .../raft/messages/InstallSnapshotTest.java | 10 ++++-- .../raft/messages/RequestVoteReplyTest.java | 7 ++-- .../raft/messages/RequestVoteTest.java | 7 ++-- .../persisted/ApplyJournalEntriesTest.java | 7 ++-- .../raft/persisted/DeleteEntriesTest.java | 7 ++-- .../raft/persisted/EmptyStateTest.java | 6 ++-- .../ServerConfigurationPayloadTest.java | 14 ++++---- .../SimpleReplicatedLogEntryTest.java | 8 ++--- .../cluster/raft/persisted/SnapshotTest.java | 20 ++++++------ .../persisted/UpdateElectionTermTest.java | 7 ++-- 16 files changed, 100 insertions(+), 70 deletions(-) diff --git a/opendaylight/md-sal/sal-akka-raft/src/test/java/org/opendaylight/controller/cluster/raft/base/messages/TimeoutNowTest.java b/opendaylight/md-sal/sal-akka-raft/src/test/java/org/opendaylight/controller/cluster/raft/base/messages/TimeoutNowTest.java index 26cdb22d8c..375990e106 100644 --- a/opendaylight/md-sal/sal-akka-raft/src/test/java/org/opendaylight/controller/cluster/raft/base/messages/TimeoutNowTest.java +++ b/opendaylight/md-sal/sal-akka-raft/src/test/java/org/opendaylight/controller/cluster/raft/base/messages/TimeoutNowTest.java @@ -7,6 +7,7 @@ */ package org.opendaylight.controller.cluster.raft.base.messages; +import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertSame; import org.apache.commons.lang.SerializationUtils; @@ -18,10 +19,11 @@ import org.junit.Test; * @author Thomas Pantelis */ public class TimeoutNowTest { - @Test public void test() { - TimeoutNow cloned = (TimeoutNow) SerializationUtils.clone(TimeoutNow.INSTANCE); + final var bytes = SerializationUtils.serialize(TimeoutNow.INSTANCE); + assertEquals(187, bytes.length); + final var cloned = SerializationUtils.deserialize(bytes); assertSame("Cloned instance", TimeoutNow.INSTANCE, cloned); } } diff --git a/opendaylight/md-sal/sal-akka-raft/src/test/java/org/opendaylight/controller/cluster/raft/behaviors/FollowerIdentifierTest.java b/opendaylight/md-sal/sal-akka-raft/src/test/java/org/opendaylight/controller/cluster/raft/behaviors/FollowerIdentifierTest.java index a9305a6862..e0b7d61f0e 100644 --- a/opendaylight/md-sal/sal-akka-raft/src/test/java/org/opendaylight/controller/cluster/raft/behaviors/FollowerIdentifierTest.java +++ b/opendaylight/md-sal/sal-akka-raft/src/test/java/org/opendaylight/controller/cluster/raft/behaviors/FollowerIdentifierTest.java @@ -18,11 +18,12 @@ import org.junit.Test; * @author Thomas Pantelis */ public class FollowerIdentifierTest { - @Test public void testSerialization() { - FollowerIdentifier expected = new FollowerIdentifier("follower1"); - FollowerIdentifier cloned = (FollowerIdentifier) SerializationUtils.clone(expected); + final var expected = new FollowerIdentifier("follower1"); + final var bytes = SerializationUtils.serialize(expected); + assertEquals(109, bytes.length); + final var cloned = (FollowerIdentifier) SerializationUtils.deserialize(bytes); assertEquals("cloned", expected, cloned); } } diff --git a/opendaylight/md-sal/sal-akka-raft/src/test/java/org/opendaylight/controller/cluster/raft/client/messages/ShutdownTest.java b/opendaylight/md-sal/sal-akka-raft/src/test/java/org/opendaylight/controller/cluster/raft/client/messages/ShutdownTest.java index 81b9fbbb86..ea3bd7e472 100644 --- a/opendaylight/md-sal/sal-akka-raft/src/test/java/org/opendaylight/controller/cluster/raft/client/messages/ShutdownTest.java +++ b/opendaylight/md-sal/sal-akka-raft/src/test/java/org/opendaylight/controller/cluster/raft/client/messages/ShutdownTest.java @@ -7,6 +7,7 @@ */ package org.opendaylight.controller.cluster.raft.client.messages; +import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertSame; import org.apache.commons.lang.SerializationUtils; @@ -18,10 +19,11 @@ import org.junit.Test; * @author Thomas Pantelis */ public class ShutdownTest { - @Test public void test() { - Shutdown cloned = (Shutdown) SerializationUtils.clone(Shutdown.INSTANCE); + final var bytes = SerializationUtils.serialize(Shutdown.INSTANCE); + assertEquals(187, bytes.length); + final var cloned = SerializationUtils.deserialize(bytes); assertSame("Cloned instance", Shutdown.INSTANCE, cloned); } } diff --git a/opendaylight/md-sal/sal-akka-raft/src/test/java/org/opendaylight/controller/cluster/raft/messages/AppendEntriesReplyTest.java b/opendaylight/md-sal/sal-akka-raft/src/test/java/org/opendaylight/controller/cluster/raft/messages/AppendEntriesReplyTest.java index 8452a71c24..8c09729e10 100644 --- a/opendaylight/md-sal/sal-akka-raft/src/test/java/org/opendaylight/controller/cluster/raft/messages/AppendEntriesReplyTest.java +++ b/opendaylight/md-sal/sal-akka-raft/src/test/java/org/opendaylight/controller/cluster/raft/messages/AppendEntriesReplyTest.java @@ -19,12 +19,14 @@ import org.opendaylight.controller.cluster.raft.RaftVersions; * @author Thomas Pantelis */ public class AppendEntriesReplyTest { - @Test public void testSerialization() { - AppendEntriesReply expected = new AppendEntriesReply("follower", 5, true, 100, 4, (short)6, true, true, - RaftVersions.CURRENT_VERSION); - AppendEntriesReply cloned = (AppendEntriesReply) SerializationUtils.clone(expected); + final var expected = new AppendEntriesReply("follower", 5, true, 100, 4, (short)6, true, true, + RaftVersions.CURRENT_VERSION); + + final var bytes = SerializationUtils.serialize(expected); + assertEquals(143, bytes.length); + final var cloned = (AppendEntriesReply) SerializationUtils.deserialize(bytes); assertEquals("getTerm", expected.getTerm(), cloned.getTerm()); assertEquals("getFollowerId", expected.getFollowerId(), cloned.getFollowerId()); @@ -39,9 +41,12 @@ public class AppendEntriesReplyTest { @Test @Deprecated public void testPreFluorineSerialization() { - AppendEntriesReply expected = new AppendEntriesReply("follower", 5, true, 100, 4, (short)6, true, true, - RaftVersions.BORON_VERSION); - AppendEntriesReply cloned = (AppendEntriesReply) SerializationUtils.clone(expected); + final var expected = new AppendEntriesReply("follower", 5, true, 100, 4, (short)6, true, true, + RaftVersions.BORON_VERSION); + + final var bytes = SerializationUtils.serialize(expected); + assertEquals(141, bytes.length); + final var cloned = (AppendEntriesReply) SerializationUtils.deserialize(bytes); assertEquals("getTerm", expected.getTerm(), cloned.getTerm()); assertEquals("getFollowerId", expected.getFollowerId(), cloned.getFollowerId()); diff --git a/opendaylight/md-sal/sal-akka-raft/src/test/java/org/opendaylight/controller/cluster/raft/messages/AppendEntriesTest.java b/opendaylight/md-sal/sal-akka-raft/src/test/java/org/opendaylight/controller/cluster/raft/messages/AppendEntriesTest.java index a7c3c8b9d5..bc9c6f1b87 100644 --- a/opendaylight/md-sal/sal-akka-raft/src/test/java/org/opendaylight/controller/cluster/raft/messages/AppendEntriesTest.java +++ b/opendaylight/md-sal/sal-akka-raft/src/test/java/org/opendaylight/controller/cluster/raft/messages/AppendEntriesTest.java @@ -10,8 +10,8 @@ package org.opendaylight.controller.cluster.raft.messages; import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertFalse; -import java.util.Arrays; import java.util.Iterator; +import java.util.List; import org.apache.commons.lang.SerializationUtils; import org.junit.Test; import org.opendaylight.controller.cluster.raft.MockRaftActorContext.MockPayload; @@ -25,7 +25,6 @@ import org.opendaylight.controller.cluster.raft.persisted.SimpleReplicatedLogEnt * @author Thomas Pantelis */ public class AppendEntriesTest { - @Test public void testSerialization() { ReplicatedLogEntry entry1 = new SimpleReplicatedLogEntry(1, 2, new MockPayload("payload1")); @@ -36,19 +35,23 @@ public class AppendEntriesTest { // Without leader address - AppendEntries expected = new AppendEntries(5L, "node1", 7L, 8L, Arrays.asList(entry1, entry2), 10L, - -1, payloadVersion, RaftVersions.CURRENT_VERSION, null); + var expected = new AppendEntries(5L, "node1", 7L, 8L, List.of(entry1, entry2), 10L, -1, payloadVersion, + RaftVersions.CURRENT_VERSION, null); - AppendEntries cloned = (AppendEntries) SerializationUtils.clone(expected); + var bytes = SerializationUtils.serialize(expected); + assertEquals(355, bytes.length); + var cloned = (AppendEntries) SerializationUtils.deserialize(bytes); verifyAppendEntries(expected, cloned, RaftVersions.CURRENT_VERSION); // With leader address - expected = new AppendEntries(5L, "node1", 7L, 8L, Arrays.asList(entry1, entry2), 10L, - -1, payloadVersion, RaftVersions.CURRENT_VERSION, "leader address"); + expected = new AppendEntries(5L, "node1", 7L, 8L, List.of(entry1, entry2), 10L, -1, payloadVersion, + RaftVersions.CURRENT_VERSION, "leader address"); - cloned = (AppendEntries) SerializationUtils.clone(expected); + bytes = SerializationUtils.serialize(expected); + assertEquals(371, bytes.length); + cloned = (AppendEntries) SerializationUtils.deserialize(bytes); verifyAppendEntries(expected, cloned, RaftVersions.CURRENT_VERSION); } @@ -62,15 +65,18 @@ public class AppendEntriesTest { short payloadVersion = 5; - AppendEntries expected = new AppendEntries(5L, "node1", 7L, 8L, Arrays.asList(entry1, entry2), 10L, - -1, payloadVersion, RaftVersions.BORON_VERSION, "leader address"); + final var expected = new AppendEntries(5L, "node1", 7L, 8L, List.of(entry1, entry2), 10L, -1, + payloadVersion, RaftVersions.BORON_VERSION, "leader address"); - AppendEntries cloned = (AppendEntries) SerializationUtils.clone(expected); + final var bytes = SerializationUtils.serialize(expected); + assertEquals(350, bytes.length); + final var cloned = (AppendEntries) SerializationUtils.deserialize(bytes); verifyAppendEntries(expected, cloned, RaftVersions.BORON_VERSION); } - private static void verifyAppendEntries(AppendEntries expected, AppendEntries actual, short recipientRaftVersion) { + private static void verifyAppendEntries(final AppendEntries expected, final AppendEntries actual, + final short recipientRaftVersion) { assertEquals("getLeaderId", expected.getLeaderId(), actual.getLeaderId()); assertEquals("getTerm", expected.getTerm(), actual.getTerm()); assertEquals("getLeaderCommit", expected.getLeaderCommit(), actual.getLeaderCommit()); @@ -94,7 +100,7 @@ public class AppendEntriesTest { } } - private static void verifyReplicatedLogEntry(ReplicatedLogEntry expected, ReplicatedLogEntry actual) { + private static void verifyReplicatedLogEntry(final ReplicatedLogEntry expected, final ReplicatedLogEntry actual) { assertEquals("getIndex", expected.getIndex(), actual.getIndex()); assertEquals("getTerm", expected.getTerm(), actual.getTerm()); assertEquals("getData", expected.getData().toString(), actual.getData().toString()); diff --git a/opendaylight/md-sal/sal-akka-raft/src/test/java/org/opendaylight/controller/cluster/raft/messages/InstallSnapshotReplyTest.java b/opendaylight/md-sal/sal-akka-raft/src/test/java/org/opendaylight/controller/cluster/raft/messages/InstallSnapshotReplyTest.java index 2841d989cf..7ea8fb7200 100644 --- a/opendaylight/md-sal/sal-akka-raft/src/test/java/org/opendaylight/controller/cluster/raft/messages/InstallSnapshotReplyTest.java +++ b/opendaylight/md-sal/sal-akka-raft/src/test/java/org/opendaylight/controller/cluster/raft/messages/InstallSnapshotReplyTest.java @@ -18,11 +18,12 @@ import org.junit.Test; * @author Thomas Pantelis */ public class InstallSnapshotReplyTest { - @Test public void testSerialization() { - InstallSnapshotReply expected = new InstallSnapshotReply(5L, "follower", 1, true); - InstallSnapshotReply cloned = (InstallSnapshotReply) SerializationUtils.clone(expected); + final var expected = new InstallSnapshotReply(5L, "follower", 1, true); + final var bytes = SerializationUtils.serialize(expected); + assertEquals(126, bytes.length); + final var cloned = (InstallSnapshotReply) SerializationUtils.deserialize(bytes); assertEquals("getTerm", expected.getTerm(), cloned.getTerm()); assertEquals("getFollowerId", expected.getFollowerId(), cloned.getFollowerId()); diff --git a/opendaylight/md-sal/sal-akka-raft/src/test/java/org/opendaylight/controller/cluster/raft/messages/InstallSnapshotTest.java b/opendaylight/md-sal/sal-akka-raft/src/test/java/org/opendaylight/controller/cluster/raft/messages/InstallSnapshotTest.java index c7fad2a191..b371ade485 100644 --- a/opendaylight/md-sal/sal-akka-raft/src/test/java/org/opendaylight/controller/cluster/raft/messages/InstallSnapshotTest.java +++ b/opendaylight/md-sal/sal-akka-raft/src/test/java/org/opendaylight/controller/cluster/raft/messages/InstallSnapshotTest.java @@ -45,12 +45,16 @@ public class InstallSnapshotTest { Object serialized = expected.toSerializable(RaftVersions.CURRENT_VERSION); assertEquals("Serialized type", InstallSnapshot.class, serialized.getClass()); - InstallSnapshot actual = (InstallSnapshot) SerializationUtils.clone((Serializable) serialized); + var bytes = SerializationUtils.serialize((Serializable) serialized); + assertEquals(1302, bytes.length); + var actual = (InstallSnapshot) SerializationUtils.deserialize(bytes); + verifyInstallSnapshot(expected, actual); expected = new InstallSnapshot(3L, "leaderId", 11L, 2L, data, 5, 6); - actual = (InstallSnapshot) SerializationUtils.clone((Serializable) expected.toSerializable( - RaftVersions.CURRENT_VERSION)); + bytes = SerializationUtils.serialize((Serializable) expected.toSerializable(RaftVersions.CURRENT_VERSION)); + assertEquals(1165, bytes.length); + actual = (InstallSnapshot) SerializationUtils.deserialize(bytes); verifyInstallSnapshot(expected, actual); } diff --git a/opendaylight/md-sal/sal-akka-raft/src/test/java/org/opendaylight/controller/cluster/raft/messages/RequestVoteReplyTest.java b/opendaylight/md-sal/sal-akka-raft/src/test/java/org/opendaylight/controller/cluster/raft/messages/RequestVoteReplyTest.java index fa1bb5f152..440c35f100 100644 --- a/opendaylight/md-sal/sal-akka-raft/src/test/java/org/opendaylight/controller/cluster/raft/messages/RequestVoteReplyTest.java +++ b/opendaylight/md-sal/sal-akka-raft/src/test/java/org/opendaylight/controller/cluster/raft/messages/RequestVoteReplyTest.java @@ -18,11 +18,12 @@ import org.junit.Test; * @author Thomas Pantelis */ public class RequestVoteReplyTest { - @Test public void testSerialization() { - RequestVoteReply expected = new RequestVoteReply(5, true); - RequestVoteReply cloned = (RequestVoteReply) SerializationUtils.clone(expected); + final var expected = new RequestVoteReply(5, true); + final var bytes = SerializationUtils.serialize(expected); + assertEquals(105, bytes.length); + final var cloned = (RequestVoteReply) SerializationUtils.deserialize(bytes); assertEquals("getTerm", expected.getTerm(), cloned.getTerm()); assertEquals("isVoteGranted", expected.isVoteGranted(), cloned.isVoteGranted()); diff --git a/opendaylight/md-sal/sal-akka-raft/src/test/java/org/opendaylight/controller/cluster/raft/messages/RequestVoteTest.java b/opendaylight/md-sal/sal-akka-raft/src/test/java/org/opendaylight/controller/cluster/raft/messages/RequestVoteTest.java index 6cb9179ded..e710685056 100644 --- a/opendaylight/md-sal/sal-akka-raft/src/test/java/org/opendaylight/controller/cluster/raft/messages/RequestVoteTest.java +++ b/opendaylight/md-sal/sal-akka-raft/src/test/java/org/opendaylight/controller/cluster/raft/messages/RequestVoteTest.java @@ -18,11 +18,12 @@ import org.junit.Test; * @author Thomas Pantelis */ public class RequestVoteTest { - @Test public void testSerialization() { - RequestVote expected = new RequestVote(4, "candidateId", 3, 2); - RequestVote cloned = (RequestVote) SerializationUtils.clone(expected); + final var expected = new RequestVote(4, "candidateId", 3, 2); + final var bytes = SerializationUtils.serialize(expected); + assertEquals(131, bytes.length); + final var cloned = (RequestVote) SerializationUtils.deserialize(bytes); assertEquals("getTerm", expected.getTerm(), cloned.getTerm()); assertEquals("getCandidateId", expected.getCandidateId(), cloned.getCandidateId()); diff --git a/opendaylight/md-sal/sal-akka-raft/src/test/java/org/opendaylight/controller/cluster/raft/persisted/ApplyJournalEntriesTest.java b/opendaylight/md-sal/sal-akka-raft/src/test/java/org/opendaylight/controller/cluster/raft/persisted/ApplyJournalEntriesTest.java index b7f152574b..2d9e7e3391 100644 --- a/opendaylight/md-sal/sal-akka-raft/src/test/java/org/opendaylight/controller/cluster/raft/persisted/ApplyJournalEntriesTest.java +++ b/opendaylight/md-sal/sal-akka-raft/src/test/java/org/opendaylight/controller/cluster/raft/persisted/ApplyJournalEntriesTest.java @@ -18,11 +18,12 @@ import org.junit.Test; * @author Thomas Pantelis */ public class ApplyJournalEntriesTest { - @Test public void testSerialization() { - ApplyJournalEntries expected = new ApplyJournalEntries(5); - ApplyJournalEntries cloned = (ApplyJournalEntries) SerializationUtils.clone(expected); + final var expected = new ApplyJournalEntries(5); + final var bytes = SerializationUtils.serialize(expected); + assertEquals(108, bytes.length); + final var cloned = (ApplyJournalEntries) SerializationUtils.deserialize(bytes); assertEquals("getFromIndex", expected.getToIndex(), cloned.getToIndex()); } diff --git a/opendaylight/md-sal/sal-akka-raft/src/test/java/org/opendaylight/controller/cluster/raft/persisted/DeleteEntriesTest.java b/opendaylight/md-sal/sal-akka-raft/src/test/java/org/opendaylight/controller/cluster/raft/persisted/DeleteEntriesTest.java index 8334296ead..3254c899e6 100644 --- a/opendaylight/md-sal/sal-akka-raft/src/test/java/org/opendaylight/controller/cluster/raft/persisted/DeleteEntriesTest.java +++ b/opendaylight/md-sal/sal-akka-raft/src/test/java/org/opendaylight/controller/cluster/raft/persisted/DeleteEntriesTest.java @@ -18,11 +18,12 @@ import org.junit.Test; * @author Thomas Pantelis */ public class DeleteEntriesTest { - @Test public void testSerialization() { - DeleteEntries expected = new DeleteEntries(5); - DeleteEntries cloned = (DeleteEntries) SerializationUtils.clone(expected); + final var expected = new DeleteEntries(5); + final var bytes = SerializationUtils.serialize(expected); + assertEquals(102, bytes.length); + final var cloned = (DeleteEntries) SerializationUtils.deserialize(bytes); assertEquals("getFromIndex", expected.getFromIndex(), cloned.getFromIndex()); } diff --git a/opendaylight/md-sal/sal-akka-raft/src/test/java/org/opendaylight/controller/cluster/raft/persisted/EmptyStateTest.java b/opendaylight/md-sal/sal-akka-raft/src/test/java/org/opendaylight/controller/cluster/raft/persisted/EmptyStateTest.java index 963580cde4..184b43b3c3 100644 --- a/opendaylight/md-sal/sal-akka-raft/src/test/java/org/opendaylight/controller/cluster/raft/persisted/EmptyStateTest.java +++ b/opendaylight/md-sal/sal-akka-raft/src/test/java/org/opendaylight/controller/cluster/raft/persisted/EmptyStateTest.java @@ -7,6 +7,7 @@ */ package org.opendaylight.controller.cluster.raft.persisted; +import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertSame; import org.apache.commons.lang.SerializationUtils; @@ -19,10 +20,11 @@ import org.junit.Test; * */ public class EmptyStateTest { - @Test public void testSerialization() { - EmptyState cloned = (EmptyState) SerializationUtils.clone(EmptyState.INSTANCE); + final var bytes = SerializationUtils.serialize(EmptyState.INSTANCE); + assertEquals(82, bytes.length); + final var cloned = SerializationUtils.deserialize(bytes); assertSame("cloned", EmptyState.INSTANCE, cloned); } } diff --git a/opendaylight/md-sal/sal-akka-raft/src/test/java/org/opendaylight/controller/cluster/raft/persisted/ServerConfigurationPayloadTest.java b/opendaylight/md-sal/sal-akka-raft/src/test/java/org/opendaylight/controller/cluster/raft/persisted/ServerConfigurationPayloadTest.java index aa2fe90884..82e7f8dbe4 100644 --- a/opendaylight/md-sal/sal-akka-raft/src/test/java/org/opendaylight/controller/cluster/raft/persisted/ServerConfigurationPayloadTest.java +++ b/opendaylight/md-sal/sal-akka-raft/src/test/java/org/opendaylight/controller/cluster/raft/persisted/ServerConfigurationPayloadTest.java @@ -10,7 +10,7 @@ package org.opendaylight.controller.cluster.raft.persisted; import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertTrue; -import java.util.Arrays; +import java.util.List; import org.apache.commons.lang.SerializationUtils; import org.junit.Test; @@ -20,19 +20,21 @@ import org.junit.Test; * @author Thomas Pantelis */ public class ServerConfigurationPayloadTest { - @Test public void testSerialization() { - ServerConfigurationPayload expected = new ServerConfigurationPayload(Arrays.asList(new ServerInfo("1", true), - new ServerInfo("2", false))); - ServerConfigurationPayload cloned = (ServerConfigurationPayload) SerializationUtils.clone(expected); + final var expected = new ServerConfigurationPayload(List.of(new ServerInfo("1", true), + new ServerInfo("2", false))); + + final var bytes = SerializationUtils.serialize(expected); + assertEquals(125, bytes.length); + final var cloned = (ServerConfigurationPayload) SerializationUtils.deserialize(bytes); assertEquals("getServerConfig", expected.getServerConfig(), cloned.getServerConfig()); } @Test public void testSize() { - ServerConfigurationPayload expected = new ServerConfigurationPayload(Arrays.asList(new ServerInfo("1", true))); + final var expected = new ServerConfigurationPayload(List.of(new ServerInfo("1", true))); assertTrue(expected.size() > 0); } } diff --git a/opendaylight/md-sal/sal-akka-raft/src/test/java/org/opendaylight/controller/cluster/raft/persisted/SimpleReplicatedLogEntryTest.java b/opendaylight/md-sal/sal-akka-raft/src/test/java/org/opendaylight/controller/cluster/raft/persisted/SimpleReplicatedLogEntryTest.java index ec4a3689b2..9c0e8907dc 100644 --- a/opendaylight/md-sal/sal-akka-raft/src/test/java/org/opendaylight/controller/cluster/raft/persisted/SimpleReplicatedLogEntryTest.java +++ b/opendaylight/md-sal/sal-akka-raft/src/test/java/org/opendaylight/controller/cluster/raft/persisted/SimpleReplicatedLogEntryTest.java @@ -19,12 +19,12 @@ import org.opendaylight.controller.cluster.raft.MockRaftActorContext; * @author Thomas Pantelis */ public class SimpleReplicatedLogEntryTest { - @Test public void testSerialization() { - SimpleReplicatedLogEntry expected = new SimpleReplicatedLogEntry(0, 1, - new MockRaftActorContext.MockPayload("A")); - SimpleReplicatedLogEntry cloned = (SimpleReplicatedLogEntry) SerializationUtils.clone(expected); + final var expected = new SimpleReplicatedLogEntry(0, 1, new MockRaftActorContext.MockPayload("A")); + final var bytes = SerializationUtils.serialize(expected); + assertEquals(260, bytes.length); + final var cloned = (SimpleReplicatedLogEntry) SerializationUtils.deserialize(bytes); assertEquals("getTerm", expected.getTerm(), cloned.getTerm()); assertEquals("getIndex", expected.getIndex(), cloned.getIndex()); diff --git a/opendaylight/md-sal/sal-akka-raft/src/test/java/org/opendaylight/controller/cluster/raft/persisted/SnapshotTest.java b/opendaylight/md-sal/sal-akka-raft/src/test/java/org/opendaylight/controller/cluster/raft/persisted/SnapshotTest.java index 9f1f924252..55d9d47376 100644 --- a/opendaylight/md-sal/sal-akka-raft/src/test/java/org/opendaylight/controller/cluster/raft/persisted/SnapshotTest.java +++ b/opendaylight/md-sal/sal-akka-raft/src/test/java/org/opendaylight/controller/cluster/raft/persisted/SnapshotTest.java @@ -9,8 +9,6 @@ package org.opendaylight.controller.cluster.raft.persisted; import static org.junit.Assert.assertEquals; -import java.util.Arrays; -import java.util.Collections; import java.util.List; import org.apache.commons.lang.SerializationUtils; import org.junit.Test; @@ -23,27 +21,29 @@ import org.opendaylight.controller.cluster.raft.ReplicatedLogEntry; * @author Thomas Pantelis */ public class SnapshotTest { - @Test public void testSerialization() { - testSerialization(new byte[]{1, 2, 3, 4, 5, 6, 7}, Arrays.asList( - new SimpleReplicatedLogEntry(6, 2, new MockPayload("payload")))); - testSerialization(new byte[]{1, 2, 3, 4, 5, 6, 7, 8, 9}, Collections.emptyList()); + testSerialization(new byte[]{1, 2, 3, 4, 5, 6, 7}, List.of( + new SimpleReplicatedLogEntry(6, 2, new MockPayload("payload"))), 548); + testSerialization(new byte[]{1, 2, 3, 4, 5, 6, 7, 8, 9}, List.of(), 389); } - private static void testSerialization(final byte[] state, final List unapplied) { + private static void testSerialization(final byte[] state, final List unapplied, + final int expectedSize) { long lastIndex = 6; long lastTerm = 2; long lastAppliedIndex = 5; long lastAppliedTerm = 1; long electionTerm = 3; String electionVotedFor = "member-1"; - ServerConfigurationPayload serverConfig = new ServerConfigurationPayload(Arrays.asList( + ServerConfigurationPayload serverConfig = new ServerConfigurationPayload(List.of( new ServerInfo("1", true), new ServerInfo("2", false))); - Snapshot expected = Snapshot.create(ByteState.of(state), unapplied, lastIndex, lastTerm, lastAppliedIndex, + final var expected = Snapshot.create(ByteState.of(state), unapplied, lastIndex, lastTerm, lastAppliedIndex, lastAppliedTerm, electionTerm, electionVotedFor, serverConfig); - Snapshot cloned = (Snapshot) SerializationUtils.clone(expected); + final var bytes = SerializationUtils.serialize(expected); + assertEquals(expectedSize, bytes.length); + final var cloned = (Snapshot) SerializationUtils.deserialize(bytes); assertEquals("lastIndex", expected.getLastIndex(), cloned.getLastIndex()); assertEquals("lastTerm", expected.getLastTerm(), cloned.getLastTerm()); diff --git a/opendaylight/md-sal/sal-akka-raft/src/test/java/org/opendaylight/controller/cluster/raft/persisted/UpdateElectionTermTest.java b/opendaylight/md-sal/sal-akka-raft/src/test/java/org/opendaylight/controller/cluster/raft/persisted/UpdateElectionTermTest.java index de95125966..f783c64be7 100644 --- a/opendaylight/md-sal/sal-akka-raft/src/test/java/org/opendaylight/controller/cluster/raft/persisted/UpdateElectionTermTest.java +++ b/opendaylight/md-sal/sal-akka-raft/src/test/java/org/opendaylight/controller/cluster/raft/persisted/UpdateElectionTermTest.java @@ -18,11 +18,12 @@ import org.junit.Test; * @author Thomas Pantelis */ public class UpdateElectionTermTest { - @Test public void testSerialization() { - UpdateElectionTerm expected = new UpdateElectionTerm(5, "leader"); - UpdateElectionTerm cloned = (UpdateElectionTerm) SerializationUtils.clone(expected); + final var expected = new UpdateElectionTerm(5, "leader"); + final var bytes = SerializationUtils.serialize(expected); + assertEquals(116, bytes.length); + final var cloned = (UpdateElectionTerm) SerializationUtils.deserialize(bytes); assertEquals("getCurrentTerm", expected.getCurrentTerm(), cloned.getCurrentTerm()); assertEquals("getVotedFor", expected.getVotedFor(), cloned.getVotedFor()); -- 2.36.6