Add specific serializer for SimpleReplicatedLogEntry
[controller.git] / opendaylight / md-sal / sal-akka-raft / src / test / java / org / opendaylight / controller / cluster / raft / persisted / SimpleReplicatedLogEntrySerializerTest.java
1 /*
2  * Copyright (c) 2018 Red Hat, Inc. and others. All rights reserved.
3  *
4  * This program and the accompanying materials are made available under the
5  * terms of the Eclipse Public License v1.0 which accompanies this distribution,
6  * and is available at http://www.eclipse.org/legal/epl-v10.html
7  */
8 package org.opendaylight.controller.cluster.raft.persisted;
9
10 import static org.junit.Assert.assertEquals;
11 import static org.junit.Assert.assertNotNull;
12
13 import akka.actor.ExtendedActorSystem;
14 import akka.testkit.javadsl.TestKit;
15 import java.io.NotSerializableException;
16 import org.junit.Test;
17 import org.opendaylight.controller.cluster.raft.MockRaftActorContext;
18
19 /**
20  * Unit tests for SimpleReplicatedLogEntrySerializer.
21  *
22  * @author Thomas Pantelis
23  */
24 public class SimpleReplicatedLogEntrySerializerTest {
25
26     @Test
27     public void testToAndFromBinary() throws NotSerializableException {
28         SimpleReplicatedLogEntry expected = new SimpleReplicatedLogEntry(0, 1,
29                 new MockRaftActorContext.MockPayload("A"));
30
31         final ExtendedActorSystem system = (ExtendedActorSystem) ExtendedActorSystem.create("test");
32         final Object deserialized;
33         try {
34             final SimpleReplicatedLogEntrySerializer serializer = new SimpleReplicatedLogEntrySerializer(system);
35             final byte[] bytes = serializer.toBinary(expected);
36             deserialized = serializer.fromBinary(bytes, SimpleReplicatedLogEntry.class);
37         } finally {
38             TestKit.shutdownActorSystem(system);
39         }
40
41         assertNotNull("fromBinary returned null", deserialized);
42         assertEquals("fromBinary return type", SimpleReplicatedLogEntry.class, deserialized.getClass());
43
44         SimpleReplicatedLogEntry actual = (SimpleReplicatedLogEntry)deserialized;
45         assertEquals("getTerm", expected.getTerm(), actual.getTerm());
46         assertEquals("getIndex", expected.getIndex(), actual.getIndex());
47         assertEquals("getData", expected.getData(), actual.getData());
48     }
49 }