Bug 7449: Add message slicing/re-assembly classes
[controller.git] / opendaylight / md-sal / sal-clustering-commons / src / test / java / org / opendaylight / controller / cluster / messaging / MessageSliceReplyTest.java
diff --git a/opendaylight/md-sal/sal-clustering-commons/src/test/java/org/opendaylight/controller/cluster/messaging/MessageSliceReplyTest.java b/opendaylight/md-sal/sal-clustering-commons/src/test/java/org/opendaylight/controller/cluster/messaging/MessageSliceReplyTest.java
new file mode 100644 (file)
index 0000000..fa31fbf
--- /dev/null
@@ -0,0 +1,71 @@
+/*
+ * Copyright (c) 2017 Inocybe Technologies and others.  All rights reserved.
+ *
+ * This program and the accompanying materials are made available under the
+ * terms of the Eclipse Public License v1.0 which accompanies this distribution,
+ * and is available at http://www.eclipse.org/legal/epl-v10.html
+ */
+package org.opendaylight.controller.cluster.messaging;
+
+import static org.junit.Assert.assertEquals;
+
+import akka.actor.ActorSystem;
+import akka.actor.ExtendedActorSystem;
+import akka.serialization.JavaSerializer;
+import akka.testkit.JavaTestKit;
+import akka.testkit.TestProbe;
+import org.apache.commons.lang.SerializationUtils;
+import org.junit.After;
+import org.junit.Before;
+import org.junit.Test;
+
+/**
+ * Unit tests for MessageSliceReply.
+ *
+ * @author Thomas Pantelis
+ */
+public class MessageSliceReplyTest {
+    private final ActorSystem actorSystem = ActorSystem.create("test");
+
+    @Before
+    public void setUp() {
+        JavaSerializer.currentSystem().value_$eq((ExtendedActorSystem) actorSystem);
+    }
+
+    @After
+    public void tearDown() {
+        JavaTestKit.shutdownActorSystem(actorSystem, Boolean.TRUE);
+    }
+
+    @Test
+    public void testSerialization() {
+        testSuccess();
+        testFailure();
+    }
+
+    private void testSuccess() {
+        MessageSliceReply expected = MessageSliceReply.success(new StringIdentifier("test"), 3,
+                TestProbe.apply(actorSystem).ref());
+        MessageSliceReply cloned = (MessageSliceReply) SerializationUtils.clone(expected);
+
+        assertEquals("getIdentifier", expected.getIdentifier(), cloned.getIdentifier());
+        assertEquals("getSliceIndex", expected.getSliceIndex(), cloned.getSliceIndex());
+        assertEquals("getSendTo", expected.getSendTo(), cloned.getSendTo());
+        assertEquals("getFailure present", Boolean.FALSE, cloned.getFailure().isPresent());
+    }
+
+    private void testFailure() {
+        MessageSliceReply expected = MessageSliceReply.failed(new StringIdentifier("test"),
+                new MessageSliceException("mock", true), TestProbe.apply(actorSystem).ref());
+        MessageSliceReply cloned = (MessageSliceReply) SerializationUtils.clone(expected);
+
+        assertEquals("getIdentifier", expected.getIdentifier(), cloned.getIdentifier());
+        assertEquals("getSliceIndex", expected.getSliceIndex(), cloned.getSliceIndex());
+        assertEquals("getSendTo", expected.getSendTo(), cloned.getSendTo());
+        assertEquals("getFailure present", Boolean.TRUE, cloned.getFailure().isPresent());
+        assertEquals("getFailure message", expected.getFailure().get().getMessage(),
+                cloned.getFailure().get().getMessage());
+        assertEquals("getFailure isRetriable", expected.getFailure().get().isRetriable(),
+                cloned.getFailure().get().isRetriable());
+    }
+}