Unit tests for AbstractRequestProxy derived classes
[controller.git] / opendaylight / md-sal / cds-access-api / src / test / java / org / opendaylight / controller / cluster / access / concepts / AbstractRequestProxyTest.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 java.io.ByteArrayInputStream;
16 import java.io.ByteArrayOutputStream;
17 import java.io.ObjectInputStream;
18 import java.io.ObjectOutputStream;
19 import java.lang.reflect.Constructor;
20 import org.junit.Assert;
21 import org.junit.Before;
22 import org.junit.Test;
23
24 public abstract class AbstractRequestProxyTest<T extends AbstractRequestProxy> {
25     public abstract T object();
26
27     private static final FrontendIdentifier FRONTEND_IDENTIFIER = FrontendIdentifier.create(
28             MemberName.forName("test"), FrontendType.forName("one"));
29     protected static final ClientIdentifier CLIENT_IDENTIFIER = ClientIdentifier.create(FRONTEND_IDENTIFIER, 0);
30
31     private static final LocalHistoryIdentifier HISTORY_IDENTIFIER = new LocalHistoryIdentifier(
32             CLIENT_IDENTIFIER, 0);
33     protected static final TransactionIdentifier TRANSACTION_IDENTIFIER = new TransactionIdentifier(
34             HISTORY_IDENTIFIER, 0);
35
36     private static final ActorSystem SYSTEM = ActorSystem.create("test");
37     protected static final ActorRef ACTOR_REF = TestProbe.apply(SYSTEM).ref();
38
39     @Before
40     public void setUp() {
41         JavaSerializer.currentSystem().value_$eq((ExtendedActorSystem) SYSTEM);
42     }
43
44     @Test
45     public void externalizableTest() throws Exception {
46         final T copy = copy();
47         Assert.assertTrue(copy != null);
48     }
49
50     @SuppressWarnings("unchecked")
51     private T copy() throws Exception {
52         final ByteArrayOutputStream bos = new ByteArrayOutputStream();
53         try (ObjectOutputStream oos = new ObjectOutputStream(bos)) {
54             object().writeExternal(oos);
55         }
56
57         try (ObjectInputStream ois = new ObjectInputStream(new ByteArrayInputStream(bos.toByteArray()))) {
58             final Constructor constructor = object().getClass().getConstructor();
59             constructor.setAccessible(true);
60             final T result = (T) constructor.newInstance();
61             result.readExternal(ois);
62             return result;
63         }
64     }
65
66     @Test
67     public void readTargetTest() throws Exception {
68         final ByteArrayOutputStream bos = new ByteArrayOutputStream();
69         try (ObjectOutputStream oos = new ObjectOutputStream(bos)) {
70             object().writeExternal(oos);
71         }
72
73         final ObjectInputStream ois = new ObjectInputStream(new ByteArrayInputStream(bos.toByteArray()));
74         Assert.assertNotNull(object().readTarget(ois));
75     }
76 }