2 * Copyright (c) 2017 Pantheon Technologies s.r.o. and others. All rights reserved.
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
8 package org.opendaylight.controller.cluster.access.concepts;
10 import static java.util.Objects.requireNonNull;
11 import static org.hamcrest.CoreMatchers.containsString;
12 import static org.hamcrest.MatcherAssert.assertThat;
13 import static org.junit.Assert.assertEquals;
15 import akka.actor.ActorRef;
16 import akka.actor.ActorSystem;
17 import akka.actor.ExtendedActorSystem;
18 import akka.serialization.JavaSerializer;
19 import akka.testkit.TestProbe;
20 import com.google.common.base.MoreObjects;
21 import org.apache.commons.lang.SerializationUtils;
22 import org.junit.Before;
23 import org.junit.Test;
24 import org.opendaylight.controller.cluster.access.ABIVersion;
26 public abstract class AbstractRequestTest<T extends Request<?, T>> {
27 private static final ActorSystem SYSTEM = ActorSystem.create("test");
28 protected static final ActorRef ACTOR_REF = TestProbe.apply(SYSTEM).ref();
29 private static final int ACTOR_REF_SIZE = ACTOR_REF.path().toSerializationFormat().length();
31 private final T object;
32 private final int expectedSize;
33 private final int legacySize;
35 protected AbstractRequestTest(final T object, final int baseSize, final int legacySize) {
36 this.object = requireNonNull(object);
37 this.expectedSize = baseSize + ACTOR_REF_SIZE;
38 this.legacySize = legacySize + ACTOR_REF_SIZE;
41 protected final T object() {
47 JavaSerializer.currentSystem().value_$eq((ExtendedActorSystem) SYSTEM);
51 public void getReplyToTest() {
52 assertEquals(ACTOR_REF, object.getReplyTo());
56 public void addToStringAttributesCommonTest() {
57 final var result = object.addToStringAttributes(MoreObjects.toStringHelper(object));
58 assertThat(result.toString(), containsString("replyTo=" + ACTOR_REF));
62 public void serializationTest() {
63 assertEquals(legacySize, SerializationUtils.serialize(object.cloneAsVersion(ABIVersion.MAGNESIUM)).length);
65 final byte[] bytes = SerializationUtils.serialize(object);
66 assertEquals(expectedSize, bytes.length);
67 @SuppressWarnings("unchecked")
68 final T deserialize = (T) SerializationUtils.deserialize(bytes);
70 assertEquals(object.getTarget(), deserialize.getTarget());
71 assertEquals(object.getVersion(), deserialize.getVersion());
72 assertEquals(object.getSequence(), deserialize.getSequence());
73 doAdditionalAssertions(deserialize);
76 protected abstract void doAdditionalAssertions(T deserialize);