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.assertNotNull;
13 import static org.junit.Assert.assertNull;
14 import static org.junit.Assert.assertTrue;
16 import akka.actor.ActorRef;
17 import akka.actor.ActorSystem;
18 import akka.testkit.javadsl.TestKit;
20 import java.util.Collection;
21 import org.junit.AfterClass;
22 import org.junit.Before;
23 import org.junit.BeforeClass;
24 import org.mockito.Mock;
25 import org.mockito.MockitoAnnotations;
26 import org.opendaylight.mdsal.dom.api.DOMRpcIdentifier;
27 import org.opendaylight.mdsal.dom.api.DOMRpcResult;
28 import org.opendaylight.mdsal.dom.api.DOMRpcService;
29 import org.opendaylight.yangtools.yang.common.QName;
30 import org.opendaylight.yangtools.yang.common.RpcError;
31 import org.opendaylight.yangtools.yang.common.RpcError.ErrorSeverity;
32 import org.opendaylight.yangtools.yang.common.RpcError.ErrorType;
33 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
34 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.NodeIdentifier;
35 import org.opendaylight.yangtools.yang.data.api.schema.ContainerNode;
36 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
37 import org.opendaylight.yangtools.yang.data.impl.schema.Builders;
38 import org.opendaylight.yangtools.yang.data.impl.schema.ImmutableNodes;
39 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
40 import org.opendaylight.yangtools.yang.model.api.SchemaPath;
41 import org.opendaylight.yangtools.yang.test.util.YangParserTestUtils;
44 * Base class for RPC tests.
46 * @author Thomas Pantelis
48 public class AbstractRpcTest {
49 static final String TEST_REV = "2014-08-28";
50 static final String TEST_NS = "urn:test";
51 static final URI TEST_URI = URI.create(TEST_NS);
52 static final QName TEST_RPC = QName.create(TEST_NS, TEST_REV, "test-rpc");
53 static final QName TEST_RPC_INPUT = QName.create(TEST_NS, TEST_REV, "input");
54 static final QName TEST_RPC_INPUT_DATA = QName.create(TEST_NS, TEST_REV, "input-data");
55 static final QName TEST_RPC_OUTPUT = QName.create(TEST_NS, TEST_REV, "output");
56 static final QName TEST_RPC_OUTPUT_DATA = QName.create(TEST_URI, "output-data");
59 static final SchemaPath TEST_RPC_TYPE = SchemaPath.create(true, TEST_RPC);
60 static final YangInstanceIdentifier TEST_PATH = YangInstanceIdentifier.create(
61 new YangInstanceIdentifier.NodeIdentifier(TEST_RPC));
62 public static final DOMRpcIdentifier TEST_RPC_ID = DOMRpcIdentifier.create(TEST_RPC_TYPE, TEST_PATH);
64 static ActorSystem node1;
65 static ActorSystem node2;
66 static RemoteRpcProviderConfig config1;
67 static RemoteRpcProviderConfig config2;
69 protected ActorRef rpcInvoker1;
70 protected TestKit rpcRegistry1Probe;
71 protected ActorRef rpcInvoker2;
72 protected TestKit rpcRegistry2Probe;
73 protected SchemaContext schemaContext;
74 protected RemoteRpcImplementation remoteRpcImpl1;
75 protected RemoteRpcImplementation remoteRpcImpl2;
78 protected DOMRpcService domRpcService1;
80 protected DOMRpcService domRpcService2;
83 public static void setup() {
84 config1 = new RemoteRpcProviderConfig.Builder("memberA").build();
85 config2 = new RemoteRpcProviderConfig.Builder("memberB").build();
86 node1 = ActorSystem.create("opendaylight-rpc", config1.get());
87 node2 = ActorSystem.create("opendaylight-rpc", config2.get());
91 public static void teardown() {
92 TestKit.shutdownActorSystem(node1);
93 TestKit.shutdownActorSystem(node2);
100 schemaContext = YangParserTestUtils.parseYangResources(AbstractRpcTest.class, "/test-rpc.yang");
102 MockitoAnnotations.initMocks(this);
104 rpcRegistry1Probe = new TestKit(node1);
105 rpcInvoker1 = node1.actorOf(RpcInvoker.props(domRpcService1));
106 rpcRegistry2Probe = new TestKit(node2);
107 rpcInvoker2 = node2.actorOf(RpcInvoker.props(domRpcService2));
108 remoteRpcImpl1 = new RemoteRpcImplementation(rpcInvoker2, config1);
109 remoteRpcImpl2 = new RemoteRpcImplementation(rpcInvoker1, config2);
112 static void assertRpcErrorEquals(final RpcError rpcError, final ErrorSeverity severity,
113 final ErrorType errorType, final String tag, final String message, final String applicationTag,
114 final String info, final String causeMsg) {
115 assertEquals("getSeverity", severity, rpcError.getSeverity());
116 assertEquals("getErrorType", errorType, rpcError.getErrorType());
117 assertEquals("getTag", tag, rpcError.getTag());
118 assertTrue("getMessage contains " + message, rpcError.getMessage().contains(message));
119 assertEquals("getApplicationTag", applicationTag, rpcError.getApplicationTag());
120 assertEquals("getInfo", info, rpcError.getInfo());
122 if (causeMsg == null) {
123 assertNull("Unexpected cause " + rpcError.getCause(), rpcError.getCause());
125 assertEquals("Cause message", causeMsg, rpcError.getCause().getMessage());
129 static void assertCompositeNodeEquals(final NormalizedNode<? , ?> exp, final NormalizedNode<? , ?> actual) {
130 assertEquals(exp, actual);
133 public static ContainerNode makeRPCInput(final String data) {
134 return Builders.containerBuilder().withNodeIdentifier(new NodeIdentifier(TEST_RPC_INPUT))
135 .withChild(ImmutableNodes.leafNode(TEST_RPC_INPUT_DATA, data)).build();
139 public static ContainerNode makeRPCOutput(final String data) {
140 return Builders.containerBuilder().withNodeIdentifier(new NodeIdentifier(TEST_RPC_OUTPUT))
141 .withChild(ImmutableNodes.leafNode(TEST_RPC_OUTPUT, data)).build();
144 static void assertFailedRpcResult(final DOMRpcResult rpcResult, final ErrorSeverity severity,
145 final ErrorType errorType, final String tag, final String message, final String applicationTag,
146 final String info, final String causeMsg) {
147 assertNotNull("RpcResult was null", rpcResult);
148 final Collection<? extends RpcError> rpcErrors = rpcResult.getErrors();
149 assertEquals("RpcErrors count", 1, rpcErrors.size());
150 assertRpcErrorEquals(rpcErrors.iterator().next(), severity, errorType, tag, message,
151 applicationTag, info, causeMsg);
154 static void assertSuccessfulRpcResult(final DOMRpcResult rpcResult,
155 final NormalizedNode<? , ?> expOutput) {
156 assertNotNull("RpcResult was null", rpcResult);
157 assertCompositeNodeEquals(expOutput, rpcResult.getResult());
160 static class TestException extends Exception {
161 private static final long serialVersionUID = 1L;
163 static final String MESSAGE = "mock error";