Improve segmented journal actor metrics
[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.lang3.SerializationUtils;
22 import org.junit.Before;
23 import org.junit.Test;
24
25 public abstract class AbstractRequestTest<T extends Request<?, T>> {
26     private static final ActorSystem SYSTEM = ActorSystem.create("test");
27     protected static final ActorRef ACTOR_REF = TestProbe.apply(SYSTEM).ref();
28     private static final int ACTOR_REF_SIZE = ACTOR_REF.path().toSerializationFormat().length();
29
30     private final T object;
31     private final int expectedSize;
32
33     protected AbstractRequestTest(final T object, final int baseSize) {
34         this.object = requireNonNull(object);
35         this.expectedSize = baseSize + ACTOR_REF_SIZE;
36     }
37
38     protected final T object() {
39         return object;
40     }
41
42     @Before
43     public void setUp() {
44         JavaSerializer.currentSystem().value_$eq((ExtendedActorSystem) SYSTEM);
45     }
46
47     @Test
48     public void getReplyToTest() {
49         assertEquals(ACTOR_REF, object.getReplyTo());
50     }
51
52     @Test
53     public void addToStringAttributesCommonTest() {
54         final var result = object.addToStringAttributes(MoreObjects.toStringHelper(object));
55         assertThat(result.toString(), containsString("replyTo=" + ACTOR_REF));
56     }
57
58     @Test
59     public void serializationTest() {
60         final byte[] bytes = SerializationUtils.serialize(object);
61         assertEquals(expectedSize, bytes.length);
62         @SuppressWarnings("unchecked")
63         final T deserialize = (T) SerializationUtils.deserialize(bytes);
64
65         assertEquals(object.getTarget(), deserialize.getTarget());
66         assertEquals(object.getVersion(), deserialize.getVersion());
67         assertEquals(object.getSequence(), deserialize.getSequence());
68         doAdditionalAssertions(deserialize);
69     }
70
71     protected abstract void doAdditionalAssertions(T deserialize);
72 }