99e72f456a9b38125e6eff60e5de063348cb6331
[controller.git] / opendaylight / md-sal / cds-access-api / src / test / java / org / opendaylight / controller / cluster / access / concepts / AbstractRequestTest.java
1 /*
2  * Copyright (c) 2017 Pantheon Technologies s.r.o. 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.access.concepts;
9
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;
14
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;
25
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();
30
31     private final T object;
32     private final int expectedSize;
33     private final int legacySize;
34
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;
39     }
40
41     protected final T object() {
42         return object;
43     }
44
45     @Before
46     public void setUp() {
47         JavaSerializer.currentSystem().value_$eq((ExtendedActorSystem) SYSTEM);
48     }
49
50     @Test
51     public void getReplyToTest() {
52         assertEquals(ACTOR_REF, object.getReplyTo());
53     }
54
55     @Test
56     public void addToStringAttributesCommonTest() {
57         final var result = object.addToStringAttributes(MoreObjects.toStringHelper(object));
58         assertThat(result.toString(), containsString("replyTo=" + ACTOR_REF));
59     }
60
61     @Test
62     public void serializationTest() {
63         assertEquals(expectedSize, SerializationUtils.serialize(object.cloneAsVersion(ABIVersion.CHLORINE_SR2)).length);
64
65         final byte[] bytes = SerializationUtils.serialize(object);
66         assertEquals(legacySize, bytes.length);
67         @SuppressWarnings("unchecked")
68         final T deserialize = (T) SerializationUtils.deserialize(bytes);
69
70         assertEquals(object.getTarget(), deserialize.getTarget());
71         assertEquals(object.getVersion(), deserialize.getVersion());
72         assertEquals(object.getSequence(), deserialize.getSequence());
73         doAdditionalAssertions(deserialize);
74     }
75
76     protected abstract void doAdditionalAssertions(T deserialize);
77 }