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
8 package org.opendaylight.controller.remote.rpc;
10 import static org.junit.Assert.assertEquals;
11 import static org.junit.Assert.assertNotNull;
12 import static org.junit.Assert.assertNull;
13 import static org.junit.Assert.assertTrue;
15 import akka.actor.ActorRef;
16 import akka.actor.ActorSystem;
17 import akka.testkit.javadsl.TestKit;
19 import org.junit.AfterClass;
20 import org.junit.Before;
21 import org.junit.BeforeClass;
22 import org.mockito.Mock;
23 import org.mockito.MockitoAnnotations;
24 import org.opendaylight.mdsal.common.api.LogicalDatastoreType;
25 import org.opendaylight.mdsal.dom.api.DOMActionService;
26 import org.opendaylight.mdsal.dom.api.DOMDataTreeIdentifier;
27 import org.opendaylight.mdsal.dom.api.DOMRpcIdentifier;
28 import org.opendaylight.mdsal.dom.api.DOMRpcResult;
29 import org.opendaylight.mdsal.dom.api.DOMRpcService;
30 import org.opendaylight.yangtools.yang.common.ErrorSeverity;
31 import org.opendaylight.yangtools.yang.common.ErrorType;
32 import org.opendaylight.yangtools.yang.common.QName;
33 import org.opendaylight.yangtools.yang.common.RpcError;
34 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
35 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.NodeIdentifier;
36 import org.opendaylight.yangtools.yang.data.api.schema.ContainerNode;
37 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
38 import org.opendaylight.yangtools.yang.data.spi.node.ImmutableNodes;
39 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
40 import org.opendaylight.yangtools.yang.model.api.stmt.SchemaNodeIdentifier.Absolute;
41 import org.opendaylight.yangtools.yang.test.util.YangParserTestUtils;
44 * Base class for RPC tests.
46 * @author Thomas Pantelis
48 public class AbstractOpsTest {
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-something");
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");
58 static final Absolute TEST_RPC_TYPE = Absolute.of(TEST_RPC);
59 static final YangInstanceIdentifier TEST_PATH = YangInstanceIdentifier.of(TEST_RPC);
60 public static final DOMRpcIdentifier TEST_RPC_ID = DOMRpcIdentifier.create(TEST_RPC, TEST_PATH);
61 public static final DOMDataTreeIdentifier TEST_DATA_TREE_ID =
62 DOMDataTreeIdentifier.of(LogicalDatastoreType.OPERATIONAL, TEST_PATH);
64 static ActorSystem node1;
65 static ActorSystem node2;
66 static RemoteOpsProviderConfig config1;
67 static RemoteOpsProviderConfig 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;
76 protected RemoteActionImplementation remoteActionImpl1;
77 protected RemoteActionImplementation remoteActionImpl2;
80 protected DOMRpcService domRpcService1;
82 protected DOMActionService domActionService1;
84 protected DOMRpcService domRpcService2;
86 protected DOMActionService domActionService2;
89 public static void setup() {
90 config1 = new RemoteOpsProviderConfig.Builder("memberA").build();
91 config2 = new RemoteOpsProviderConfig.Builder("memberB").build();
92 node1 = ActorSystem.create("opendaylight-rpc", config1.get());
93 node2 = ActorSystem.create("opendaylight-rpc", config2.get());
97 public static void teardown() {
98 TestKit.shutdownActorSystem(node1);
99 TestKit.shutdownActorSystem(node2);
105 public void setUp() {
106 schemaContext = YangParserTestUtils.parseYangResources(AbstractOpsTest.class, "/test-rpc.yang");
108 MockitoAnnotations.initMocks(this);
110 rpcRegistry1Probe = new TestKit(node1);
111 rpcInvoker1 = node1.actorOf(OpsInvoker.props(domRpcService1, domActionService1));
112 rpcRegistry2Probe = new TestKit(node2);
113 rpcInvoker2 = node2.actorOf(OpsInvoker.props(domRpcService2, domActionService2));
114 remoteRpcImpl1 = new RemoteRpcImplementation(rpcInvoker2, config1);
115 remoteRpcImpl2 = new RemoteRpcImplementation(rpcInvoker1, config2);
116 remoteActionImpl1 = new RemoteActionImplementation(rpcInvoker2, config1);
117 remoteActionImpl2 = new RemoteActionImplementation(rpcInvoker1, config2);
120 static void assertRpcErrorEquals(final RpcError rpcError, final ErrorSeverity severity,
121 final ErrorType errorType, final String tag, final String message,
122 final String applicationTag, final String info, final String causeMsg) {
123 assertEquals("getSeverity", severity, rpcError.getSeverity());
124 assertEquals("getErrorType", errorType, rpcError.getErrorType());
125 assertEquals("getTag", tag, rpcError.getTag());
126 assertTrue("getMessage contains " + message, rpcError.getMessage().contains(message));
127 assertEquals("getApplicationTag", applicationTag, rpcError.getApplicationTag());
128 assertEquals("getInfo", info, rpcError.getInfo());
130 if (causeMsg == null) {
131 assertNull("Unexpected cause " + rpcError.getCause(), rpcError.getCause());
133 assertEquals("Cause message", causeMsg, rpcError.getCause().getMessage());
137 static void assertCompositeNodeEquals(final NormalizedNode exp, final NormalizedNode actual) {
138 assertEquals(exp, actual);
141 public static ContainerNode makeRPCInput(final String data) {
142 return ImmutableNodes.newContainerBuilder()
143 .withNodeIdentifier(new NodeIdentifier(TEST_RPC_INPUT))
144 .withChild(ImmutableNodes.leafNode(TEST_RPC_INPUT_DATA, data))
149 public static ContainerNode makeRPCOutput(final String data) {
150 return ImmutableNodes.newContainerBuilder()
151 .withNodeIdentifier(new NodeIdentifier(TEST_RPC_OUTPUT))
152 .withChild(ImmutableNodes.leafNode(TEST_RPC_OUTPUT, data))
156 static void assertFailedRpcResult(final DOMRpcResult rpcResult, final ErrorSeverity severity,
157 final ErrorType errorType, final String tag, final String message,
158 final String applicationTag, final String info, final String causeMsg) {
159 assertNotNull("RpcResult was null", rpcResult);
160 final var rpcErrors = rpcResult.errors();
161 assertEquals("RpcErrors count", 1, rpcErrors.size());
162 assertRpcErrorEquals(rpcErrors.iterator().next(), severity, errorType, tag, message,
163 applicationTag, info, causeMsg);
166 static void assertSuccessfulRpcResult(final DOMRpcResult rpcResult, final NormalizedNode expOutput) {
167 assertNotNull("RpcResult was null", rpcResult);
168 assertCompositeNodeEquals(expOutput, rpcResult.value());
171 static class TestException extends Exception {
172 private static final long serialVersionUID = 1L;
174 static final String MESSAGE = "mock error";