Modernize AbstractRequestTest
[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 org.hamcrest.CoreMatchers.containsString;
11 import static org.hamcrest.MatcherAssert.assertThat;
12 import static org.junit.Assert.assertEquals;
13
14 import akka.actor.ActorRef;
15 import akka.actor.ActorSystem;
16 import akka.actor.ExtendedActorSystem;
17 import akka.serialization.JavaSerializer;
18 import akka.testkit.TestProbe;
19 import com.google.common.base.MoreObjects;
20 import org.apache.commons.lang.SerializationUtils;
21 import org.junit.Before;
22 import org.junit.Test;
23
24 public abstract class AbstractRequestTest<T extends Request<?, T>> {
25     private static final ActorSystem SYSTEM = ActorSystem.create("test");
26     protected static final ActorRef ACTOR_REF = TestProbe.apply(SYSTEM).ref();
27
28     protected abstract T object();
29
30     @Before
31     public void setUp() {
32         JavaSerializer.currentSystem().value_$eq((ExtendedActorSystem) SYSTEM);
33     }
34
35     @Test
36     public void getReplyToTest() {
37         assertEquals(ACTOR_REF, object().getReplyTo());
38     }
39
40     @Test
41     public void addToStringAttributesCommonTest() {
42         final var result = object().addToStringAttributes(MoreObjects.toStringHelper(object()));
43         assertThat(result.toString(), containsString("replyTo=" + ACTOR_REF));
44     }
45
46     @SuppressWarnings("unchecked")
47     @Test
48     public void serializationTest() {
49         final Object deserialize = SerializationUtils.clone(object());
50
51         assertEquals(object().getTarget(), ((T) deserialize).getTarget());
52         assertEquals(object().getVersion(), ((T) deserialize).getVersion());
53         assertEquals(object().getSequence(), ((T) deserialize).getSequence());
54         doAdditionalAssertions(deserialize);
55     }
56
57     protected abstract void doAdditionalAssertions(Object deserialize);
58 }