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