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.JavaTestKit;
19 import com.google.common.collect.Lists;
20 import com.google.common.io.ByteSource;
21 import java.io.IOException;
22 import java.io.InputStream;
24 import java.util.ArrayList;
25 import java.util.Collection;
26 import org.junit.AfterClass;
27 import org.junit.Before;
28 import org.junit.BeforeClass;
29 import org.mockito.Mockito;
30 import org.opendaylight.controller.md.sal.dom.api.DOMRpcIdentifier;
31 import org.opendaylight.controller.md.sal.dom.api.DOMRpcResult;
32 import org.opendaylight.controller.md.sal.dom.api.DOMRpcService;
33 import org.opendaylight.controller.sal.core.api.Broker;
34 import org.opendaylight.yangtools.yang.common.QName;
35 import org.opendaylight.yangtools.yang.common.RpcError;
36 import org.opendaylight.yangtools.yang.common.RpcError.ErrorSeverity;
37 import org.opendaylight.yangtools.yang.common.RpcError.ErrorType;
38 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
39 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.NodeIdentifier;
40 import org.opendaylight.yangtools.yang.data.api.schema.ContainerNode;
41 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
42 import org.opendaylight.yangtools.yang.data.impl.schema.Builders;
43 import org.opendaylight.yangtools.yang.data.impl.schema.ImmutableNodes;
44 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
45 import org.opendaylight.yangtools.yang.model.api.SchemaPath;
46 import org.opendaylight.yangtools.yang.parser.spi.meta.ReactorException;
47 import org.opendaylight.yangtools.yang.parser.stmt.reactor.CrossSourceStatementReactor;
48 import org.opendaylight.yangtools.yang.parser.stmt.rfc6020.YangInferencePipeline;
51 * Base class for RPC tests.
53 * @author Thomas Pantelis
55 public class AbstractRpcTest {
56 static final String TEST_REV = "2014-08-28";
57 static final String TEST_NS = "urn:test";
58 static final URI TEST_URI = URI.create(TEST_NS);
59 static final QName TEST_RPC = QName.create(TEST_NS, TEST_REV, "test-rpc");
60 static final QName TEST_RPC_INPUT = QName.create(TEST_NS, TEST_REV, "input");
61 static final QName TEST_RPC_INPUT_DATA = QName.create(TEST_NS, TEST_REV, "input-data");
62 static final QName TEST_RPC_OUTPUT = QName.create(TEST_NS, TEST_REV, "output");
63 static final QName TEST_RPC_OUTPUT_DATA = new QName(TEST_URI, "output-data");
66 static final SchemaPath TEST_RPC_TYPE = SchemaPath.create(true, TEST_RPC);
67 static final YangInstanceIdentifier TEST_PATH = YangInstanceIdentifier.create(new YangInstanceIdentifier.NodeIdentifier(TEST_RPC));
68 static final DOMRpcIdentifier TEST_RPC_ID = DOMRpcIdentifier.create(TEST_RPC_TYPE, TEST_PATH);
70 static ActorSystem node1;
71 static ActorSystem node2;
72 static RemoteRpcProviderConfig config1;
73 static RemoteRpcProviderConfig config2;
75 protected ActorRef rpcBroker1;
76 protected JavaTestKit rpcRegistry1Probe;
77 protected ActorRef rpcBroker2;
78 protected JavaTestKit rpcRegistry2Probe;
79 protected Broker.ProviderSession brokerSession;
80 protected SchemaContext schemaContext;
81 protected RemoteRpcImplementation remoteRpcImpl1;
82 protected RemoteRpcImplementation remoteRpcImpl2;
83 protected DOMRpcService domRpcService1;
84 protected DOMRpcService domRpcService2;
87 public static void setup() throws InterruptedException {
88 config1 = new RemoteRpcProviderConfig.Builder("memberA").build();
89 config2 = new RemoteRpcProviderConfig.Builder("memberB").build();
90 node1 = ActorSystem.create("opendaylight-rpc", config1.get());
91 node2 = ActorSystem.create("opendaylight-rpc", config2.get());
95 public static void teardown() {
96 JavaTestKit.shutdownActorSystem(node1);
97 JavaTestKit.shutdownActorSystem(node2);
103 public void setUp() throws Exception {
104 final ByteSource byteSource = new ByteSource() {
106 public InputStream openStream() throws IOException {
107 return AbstractRpcTest.this.getClass().getResourceAsStream("/test-rpc.yang");
111 final CrossSourceStatementReactor.BuildAction reactor = YangInferencePipeline.RFC6020_REACTOR.newBuild();
112 final ArrayList<ByteSource> sources = Lists.newArrayList(byteSource);
115 schemaContext = reactor.buildEffective(sources);
116 } catch (ReactorException e) {
117 throw new RuntimeException("Unable to build schema context from " + sources, e);
120 domRpcService1 = Mockito.mock(DOMRpcService.class);
121 domRpcService2 = Mockito.mock(DOMRpcService.class);
122 rpcRegistry1Probe = new JavaTestKit(node1);
123 rpcBroker1 = node1.actorOf(RpcBroker.props(domRpcService1));
124 rpcRegistry2Probe = new JavaTestKit(node2);
125 rpcBroker2 = node2.actorOf(RpcBroker.props(domRpcService2));
126 remoteRpcImpl1 = new RemoteRpcImplementation(rpcRegistry1Probe.getRef(), config1);
127 remoteRpcImpl2 = new RemoteRpcImplementation(rpcRegistry2Probe.getRef(), config2);
131 static void assertRpcErrorEquals(final RpcError rpcError, final ErrorSeverity severity,
132 final ErrorType errorType, final String tag, final String message, final String applicationTag, final String info,
133 final String causeMsg) {
134 assertEquals("getSeverity", severity, rpcError.getSeverity());
135 assertEquals("getErrorType", errorType, rpcError.getErrorType());
136 assertEquals("getTag", tag, rpcError.getTag());
137 assertTrue("getMessage contains " + message, rpcError.getMessage().contains(message));
138 assertEquals("getApplicationTag", applicationTag, rpcError.getApplicationTag());
139 assertEquals("getInfo", info, rpcError.getInfo());
141 if(causeMsg == null) {
142 assertNull("Unexpected cause " + rpcError.getCause(), rpcError.getCause());
144 assertEquals("Cause message", causeMsg, rpcError.getCause().getMessage());
148 static void assertCompositeNodeEquals(final NormalizedNode<? , ?> exp, final NormalizedNode<? , ? > actual) {
149 assertEquals(exp, actual);
152 static ContainerNode makeRPCInput(final String data) {
153 return Builders.containerBuilder().withNodeIdentifier(new NodeIdentifier(TEST_RPC_INPUT))
154 .withChild(ImmutableNodes.leafNode(TEST_RPC_INPUT_DATA, data)).build();
158 static ContainerNode makeRPCOutput(final String data) {
159 return Builders.containerBuilder().withNodeIdentifier(new NodeIdentifier(TEST_RPC_OUTPUT))
160 .withChild(ImmutableNodes.leafNode(TEST_RPC_OUTPUT, data)).build();
163 static void assertFailedRpcResult(final DOMRpcResult rpcResult, final ErrorSeverity severity,
164 final ErrorType errorType, final String tag, final String message, final String applicationTag, final String info,
165 final String causeMsg) {
167 assertNotNull("RpcResult was null", rpcResult);
168 final Collection<RpcError> rpcErrors = rpcResult.getErrors();
169 assertEquals("RpcErrors count", 1, rpcErrors.size());
170 assertRpcErrorEquals(rpcErrors.iterator().next(), severity, errorType, tag, message,
171 applicationTag, info, causeMsg);
174 static void assertSuccessfulRpcResult(final DOMRpcResult rpcResult,
175 final NormalizedNode<? , ?> expOutput) {
176 assertNotNull("RpcResult was null", rpcResult);
177 assertCompositeNodeEquals(expOutput, rpcResult.getResult());
180 static class TestException extends Exception {
181 private static final long serialVersionUID = 1L;
183 static final String MESSAGE = "mock error";