*/
package org.opendaylight.controller.cluster.access.concepts;
+import static java.util.Objects.requireNonNull;
+import static org.hamcrest.CoreMatchers.containsString;
+import static org.hamcrest.MatcherAssert.assertThat;
+import static org.junit.Assert.assertEquals;
+
import akka.actor.ActorRef;
import akka.actor.ActorSystem;
+import akka.actor.ExtendedActorSystem;
+import akka.serialization.JavaSerializer;
import akka.testkit.TestProbe;
import com.google.common.base.MoreObjects;
-import org.junit.Assert;
+import org.apache.commons.lang3.SerializationUtils;
+import org.junit.Before;
import org.junit.Test;
-public abstract class AbstractRequestTest<T extends Request> {
+public abstract class AbstractRequestTest<T extends Request<?, T>> {
private static final ActorSystem SYSTEM = ActorSystem.create("test");
protected static final ActorRef ACTOR_REF = TestProbe.apply(SYSTEM).ref();
+ private static final int ACTOR_REF_SIZE = ACTOR_REF.path().toSerializationFormat().length();
+
+ private final T object;
+ private final int expectedSize;
+
+ protected AbstractRequestTest(final T object, final int baseSize) {
+ this.object = requireNonNull(object);
+ this.expectedSize = baseSize + ACTOR_REF_SIZE;
+ }
- protected abstract T object();
+ protected final T object() {
+ return object;
+ }
+
+ @Before
+ public void setUp() {
+ JavaSerializer.currentSystem().value_$eq((ExtendedActorSystem) SYSTEM);
+ }
@Test
- public void getReplyToTest() {}
+ public void getReplyToTest() {
+ assertEquals(ACTOR_REF, object.getReplyTo());
+ }
@Test
public void addToStringAttributesCommonTest() {
- final MoreObjects.ToStringHelper result = object().addToStringAttributes(MoreObjects.toStringHelper(object()));
- Assert.assertTrue(result.toString().contains("replyTo=" + ACTOR_REF));
+ final var result = object.addToStringAttributes(MoreObjects.toStringHelper(object));
+ assertThat(result.toString(), containsString("replyTo=" + ACTOR_REF));
+ }
+
+ @Test
+ public void serializationTest() {
+ final byte[] bytes = SerializationUtils.serialize(object);
+ assertEquals(expectedSize, bytes.length);
+ @SuppressWarnings("unchecked")
+ final T deserialize = (T) SerializationUtils.deserialize(bytes);
+
+ assertEquals(object.getTarget(), deserialize.getTarget());
+ assertEquals(object.getVersion(), deserialize.getVersion());
+ assertEquals(object.getSequence(), deserialize.getSequence());
+ doAdditionalAssertions(deserialize);
}
+
+ protected abstract void doAdditionalAssertions(T deserialize);
}