2 * Copyright (c) 2017 Pantheon Technologies s.r.o. and others. All rights reserved.
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
8 package org.opendaylight.controller.cluster.access.concepts;
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;
24 public abstract class AbstractRequestProxyTest<T extends AbstractRequestProxy> {
25 public abstract T object();
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);
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);
36 private static final ActorSystem SYSTEM = ActorSystem.create("test");
37 protected static final ActorRef ACTOR_REF = TestProbe.apply(SYSTEM).ref();
41 JavaSerializer.currentSystem().value_$eq((ExtendedActorSystem) SYSTEM);
45 public void externalizableTest() throws Exception {
46 final T copy = copy();
47 Assert.assertTrue(copy != null);
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);
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);
67 public void readTargetTest() throws Exception {
68 final ByteArrayOutputStream bos = new ByteArrayOutputStream();
69 try (ObjectOutputStream oos = new ObjectOutputStream(bos)) {
70 object().writeExternal(oos);
73 final ObjectInputStream ois = new ObjectInputStream(new ByteArrayInputStream(bos.toByteArray()));
74 Assert.assertNotNull(object().readTarget(ois));