2 * Copyright (c) 2014 Brocade Communications Systems, Inc. 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
9 package org.opendaylight.controller.remote.rpc;
11 import static org.junit.Assert.assertEquals;
12 import static org.junit.Assert.assertNull;
13 import static org.junit.Assert.assertTrue;
14 import akka.actor.ActorRef;
15 import akka.actor.ActorSystem;
16 import akka.testkit.JavaTestKit;
19 import java.util.Arrays;
20 import org.junit.AfterClass;
21 import org.junit.Before;
22 import org.junit.BeforeClass;
23 import org.mockito.Mockito;
24 import org.opendaylight.controller.md.sal.dom.api.DOMRpcService;
25 import org.opendaylight.controller.sal.core.api.Broker;
26 import org.opendaylight.yangtools.yang.common.QName;
27 import org.opendaylight.yangtools.yang.common.RpcError;
28 import org.opendaylight.yangtools.yang.common.RpcError.ErrorSeverity;
29 import org.opendaylight.yangtools.yang.common.RpcError.ErrorType;
30 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
31 import org.opendaylight.yangtools.yang.parser.impl.YangParserImpl;
34 * Base class for RPC tests.
36 * @author Thomas Pantelis
38 public class AbstractRpcTest {
39 static final String TEST_REV = "2014-08-28";
40 static final String TEST_NS = "urn:test";
41 static final URI TEST_URI = URI.create(TEST_NS);
42 static final QName TEST_RPC = QName.create(TEST_NS, TEST_REV, "test-rpc");
43 static final QName TEST_RPC_INPUT = QName.create(TEST_NS, TEST_REV, "input");
44 static final QName TEST_RPC_INPUT_DATA = QName.create(TEST_NS, TEST_REV, "input-data");
45 static final QName TEST_RPC_OUTPUT = QName.create(TEST_NS, TEST_REV, "output");
46 static final QName TEST_RPC_OUTPUT_DATA = new QName(TEST_URI, "output-data");
48 static ActorSystem node1;
49 static ActorSystem node2;
51 protected ActorRef rpcBroker1;
52 protected JavaTestKit probeReg1;
53 protected ActorRef rpcBroker2;
54 protected JavaTestKit probeReg2;
55 protected Broker.ProviderSession brokerSession;
56 protected SchemaContext schemaContext;
57 protected DOMRpcService rpcService;
60 public static void setup() throws InterruptedException {
61 final RemoteRpcProviderConfig config1 = new RemoteRpcProviderConfig.Builder("memberA").build();
62 final RemoteRpcProviderConfig config2 = new RemoteRpcProviderConfig.Builder("memberB").build();
63 node1 = ActorSystem.create("opendaylight-rpc", config1.get());
64 node2 = ActorSystem.create("opendaylight-rpc", config2.get());
68 public static void teardown() {
69 JavaTestKit.shutdownActorSystem(node1);
70 JavaTestKit.shutdownActorSystem(node2);
77 schemaContext = new YangParserImpl().parseFiles(Arrays.asList(
78 new File(RpcBrokerTest.class.getResource("/test-rpc.yang").getPath())));
80 brokerSession = Mockito.mock(Broker.ProviderSession.class);
81 rpcService = Mockito.mock(DOMRpcService.class);
83 probeReg1 = new JavaTestKit(node1);
84 rpcBroker1 = node1.actorOf(RpcBroker.props(rpcService, probeReg1.getRef()));
85 probeReg2 = new JavaTestKit(node2);
86 rpcBroker2 = node2.actorOf(RpcBroker.props(rpcService, probeReg2.getRef()));
90 static void assertRpcErrorEquals(final RpcError rpcError, final ErrorSeverity severity,
91 final ErrorType errorType, final String tag, final String message, final String applicationTag, final String info,
92 final String causeMsg) {
93 assertEquals("getSeverity", severity, rpcError.getSeverity());
94 assertEquals("getErrorType", errorType, rpcError.getErrorType());
95 assertEquals("getTag", tag, rpcError.getTag());
96 assertTrue("getMessage contains " + message, rpcError.getMessage().contains(message));
97 assertEquals("getApplicationTag", applicationTag, rpcError.getApplicationTag());
98 assertEquals("getInfo", info, rpcError.getInfo());
100 if(causeMsg == null) {
101 assertNull("Unexpected cause " + rpcError.getCause(), rpcError.getCause());
103 assertEquals("Cause message", causeMsg, rpcError.getCause().getMessage());
107 static class TestException extends Exception {
108 private static final long serialVersionUID = 1L;
110 static final String MESSAGE = "mock error";