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 java.util.Collection;
20 import org.junit.AfterClass;
21 import org.junit.Before;
22 import org.junit.BeforeClass;
23 import org.mockito.Mock;
24 import org.mockito.MockitoAnnotations;
25 import org.opendaylight.mdsal.common.api.LogicalDatastoreType;
26 import org.opendaylight.mdsal.dom.api.DOMActionService;
27 import org.opendaylight.mdsal.dom.api.DOMDataTreeIdentifier;
28 import org.opendaylight.mdsal.dom.api.DOMRpcIdentifier;
29 import org.opendaylight.mdsal.dom.api.DOMRpcResult;
30 import org.opendaylight.mdsal.dom.api.DOMRpcService;
31 import org.opendaylight.yangtools.yang.common.QName;
32 import org.opendaylight.yangtools.yang.common.RpcError;
33 import org.opendaylight.yangtools.yang.common.RpcError.ErrorSeverity;
34 import org.opendaylight.yangtools.yang.common.RpcError.ErrorType;
35 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
36 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.NodeIdentifier;
37 import org.opendaylight.yangtools.yang.data.api.schema.ContainerNode;
38 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
39 import org.opendaylight.yangtools.yang.data.impl.schema.Builders;
40 import org.opendaylight.yangtools.yang.data.impl.schema.ImmutableNodes;
41 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
42 import org.opendaylight.yangtools.yang.model.api.SchemaPath;
43 import org.opendaylight.yangtools.yang.test.util.YangParserTestUtils;
46 * Base class for RPC tests.
48 * @author Thomas Pantelis
50 public class AbstractOpsTest {
51 static final String TEST_REV = "2014-08-28";
52 static final String TEST_NS = "urn:test";
53 static final URI TEST_URI = URI.create(TEST_NS);
54 static final QName TEST_RPC = QName.create(TEST_NS, TEST_REV, "test-something");
55 static final QName TEST_RPC_INPUT = QName.create(TEST_NS, TEST_REV, "input");
56 static final QName TEST_RPC_INPUT_DATA = QName.create(TEST_NS, TEST_REV, "input-data");
57 static final QName TEST_RPC_OUTPUT = QName.create(TEST_NS, TEST_REV, "output");
60 static final SchemaPath TEST_RPC_TYPE = SchemaPath.create(true, TEST_RPC);
61 static final YangInstanceIdentifier TEST_PATH = YangInstanceIdentifier.create(
62 new YangInstanceIdentifier.NodeIdentifier(TEST_RPC));
63 public static final DOMRpcIdentifier TEST_RPC_ID = DOMRpcIdentifier.create(TEST_RPC_TYPE, TEST_PATH);
64 public static final DOMDataTreeIdentifier TEST_DATA_TREE_ID = new DOMDataTreeIdentifier(
65 LogicalDatastoreType.OPERATIONAL, TEST_PATH);
67 static ActorSystem node1;
68 static ActorSystem node2;
69 static RemoteOpsProviderConfig config1;
70 static RemoteOpsProviderConfig config2;
72 protected ActorRef rpcInvoker1;
73 protected TestKit rpcRegistry1Probe;
74 protected ActorRef rpcInvoker2;
75 protected TestKit rpcRegistry2Probe;
76 protected SchemaContext schemaContext;
77 protected RemoteRpcImplementation remoteRpcImpl1;
78 protected RemoteRpcImplementation remoteRpcImpl2;
79 protected RemoteActionImplementation remoteActionImpl1;
80 protected RemoteActionImplementation remoteActionImpl2;
83 protected DOMRpcService domRpcService1;
85 protected DOMActionService domActionService1;
87 protected DOMRpcService domRpcService2;
89 protected DOMActionService domActionService2;
92 public static void setup() {
93 config1 = new RemoteOpsProviderConfig.Builder("memberA").build();
94 config2 = new RemoteOpsProviderConfig.Builder("memberB").build();
95 node1 = ActorSystem.create("opendaylight-rpc", config1.get());
96 node2 = ActorSystem.create("opendaylight-rpc", config2.get());
100 public static void teardown() {
101 TestKit.shutdownActorSystem(node1);
102 TestKit.shutdownActorSystem(node2);
108 public void setUp() {
109 schemaContext = YangParserTestUtils.parseYangResources(AbstractOpsTest.class, "/test-rpc.yang");
111 MockitoAnnotations.initMocks(this);
113 rpcRegistry1Probe = new TestKit(node1);
114 rpcInvoker1 = node1.actorOf(OpsInvoker.props(domRpcService1, domActionService1));
115 rpcRegistry2Probe = new TestKit(node2);
116 rpcInvoker2 = node2.actorOf(OpsInvoker.props(domRpcService2, domActionService2));
117 remoteRpcImpl1 = new RemoteRpcImplementation(rpcInvoker2, config1);
118 remoteRpcImpl2 = new RemoteRpcImplementation(rpcInvoker1, config2);
119 remoteActionImpl1 = new RemoteActionImplementation(rpcInvoker2, config1);
120 remoteActionImpl2 = new RemoteActionImplementation(rpcInvoker1, config2);
123 static void assertRpcErrorEquals(final RpcError rpcError, final ErrorSeverity severity,
124 final ErrorType errorType, final String tag, final String message,
125 final String applicationTag, final String info, final String causeMsg) {
126 assertEquals("getSeverity", severity, rpcError.getSeverity());
127 assertEquals("getErrorType", errorType, rpcError.getErrorType());
128 assertEquals("getTag", tag, rpcError.getTag());
129 assertTrue("getMessage contains " + message, rpcError.getMessage().contains(message));
130 assertEquals("getApplicationTag", applicationTag, rpcError.getApplicationTag());
131 assertEquals("getInfo", info, rpcError.getInfo());
133 if (causeMsg == null) {
134 assertNull("Unexpected cause " + rpcError.getCause(), rpcError.getCause());
136 assertEquals("Cause message", causeMsg, rpcError.getCause().getMessage());
140 static void assertCompositeNodeEquals(final NormalizedNode<? , ?> exp, final NormalizedNode<? , ?> actual) {
141 assertEquals(exp, actual);
144 public static ContainerNode makeRPCInput(final String data) {
145 return Builders.containerBuilder().withNodeIdentifier(new NodeIdentifier(TEST_RPC_INPUT))
146 .withChild(ImmutableNodes.leafNode(TEST_RPC_INPUT_DATA, data)).build();
150 public static ContainerNode makeRPCOutput(final String data) {
151 return Builders.containerBuilder().withNodeIdentifier(new NodeIdentifier(TEST_RPC_OUTPUT))
152 .withChild(ImmutableNodes.leafNode(TEST_RPC_OUTPUT, data)).build();
155 static void assertFailedRpcResult(final DOMRpcResult rpcResult, final ErrorSeverity severity,
156 final ErrorType errorType, final String tag, final String message,
157 final String applicationTag, final String info, final String causeMsg) {
158 assertNotNull("RpcResult was null", rpcResult);
159 final Collection<? extends RpcError> rpcErrors = rpcResult.getErrors();
160 assertEquals("RpcErrors count", 1, rpcErrors.size());
161 assertRpcErrorEquals(rpcErrors.iterator().next(), severity, errorType, tag, message,
162 applicationTag, info, causeMsg);
165 static void assertSuccessfulRpcResult(final DOMRpcResult rpcResult,
166 final NormalizedNode<? , ?> expOutput) {
167 assertNotNull("RpcResult was null", rpcResult);
168 assertCompositeNodeEquals(expOutput, rpcResult.getResult());
171 static class TestException extends Exception {
172 private static final long serialVersionUID = 1L;
174 static final String MESSAGE = "mock error";