Removed uncessary calls to RpcBroker to find routes.
[controller.git] / opendaylight / md-sal / sal-remoterpc-connector / src / test / java / org / opendaylight / controller / remote / rpc / AbstractRpcTest.java
1 /*
2  * Copyright (c) 2014 Brocade Communications Systems, Inc. and others.  All rights reserved.
3  *
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
7  */
8
9 package org.opendaylight.controller.remote.rpc;
10
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;
15
16 import akka.actor.ActorRef;
17 import akka.actor.ActorSystem;
18 import akka.testkit.JavaTestKit;
19 import java.io.File;
20 import java.net.URI;
21 import java.util.Arrays;
22 import java.util.Collection;
23 import org.junit.AfterClass;
24 import org.junit.Before;
25 import org.junit.BeforeClass;
26 import org.mockito.Mockito;
27 import org.opendaylight.controller.md.sal.dom.api.DOMRpcIdentifier;
28 import org.opendaylight.controller.md.sal.dom.api.DOMRpcResult;
29 import org.opendaylight.controller.md.sal.dom.api.DOMRpcService;
30 import org.opendaylight.controller.sal.core.api.Broker;
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.parser.impl.YangParserImpl;
44
45 /**
46  * Base class for RPC tests.
47  *
48  * @author Thomas Pantelis
49  */
50 public class AbstractRpcTest {
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-rpc");
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");
58     static final QName TEST_RPC_OUTPUT_DATA = new QName(TEST_URI, "output-data");
59
60
61     static final SchemaPath TEST_RPC_TYPE = SchemaPath.create(true, TEST_RPC);
62     static final YangInstanceIdentifier TEST_PATH = YangInstanceIdentifier.create(new YangInstanceIdentifier.NodeIdentifier(TEST_RPC));
63     static final DOMRpcIdentifier TEST_RPC_ID = DOMRpcIdentifier.create(TEST_RPC_TYPE, TEST_PATH);
64
65     static ActorSystem node1;
66     static ActorSystem node2;
67     static RemoteRpcProviderConfig config1;
68     static RemoteRpcProviderConfig config2;
69
70     protected ActorRef rpcBroker1;
71     protected JavaTestKit rpcRegistry1Probe;
72     protected ActorRef rpcBroker2;
73     protected JavaTestKit rpcRegistry2Probe;
74     protected Broker.ProviderSession brokerSession;
75     protected SchemaContext schemaContext;
76     protected RemoteRpcImplementation remoteRpcImpl1;
77     protected RemoteRpcImplementation remoteRpcImpl2;
78     protected DOMRpcService domRpcService1;
79     protected DOMRpcService domRpcService2;
80
81     @BeforeClass
82     public static void setup() throws InterruptedException {
83         config1 = new RemoteRpcProviderConfig.Builder("memberA").build();
84         config2 = new RemoteRpcProviderConfig.Builder("memberB").build();
85         node1 = ActorSystem.create("opendaylight-rpc", config1.get());
86         node2 = ActorSystem.create("opendaylight-rpc", config2.get());
87     }
88
89     @AfterClass
90     public static void teardown() {
91         JavaTestKit.shutdownActorSystem(node1);
92         JavaTestKit.shutdownActorSystem(node2);
93         node1 = null;
94         node2 = null;
95     }
96
97     @Before
98     public void setUp() {
99         schemaContext = new YangParserImpl().parseFiles(Arrays.asList(
100                 new File(RpcBrokerTest.class.getResource("/test-rpc.yang").getPath())));
101
102         domRpcService1 = Mockito.mock(DOMRpcService.class);
103         domRpcService2 = Mockito.mock(DOMRpcService.class);
104         rpcRegistry1Probe = new JavaTestKit(node1);
105         rpcBroker1 = node1.actorOf(RpcBroker.props(domRpcService1));
106         rpcRegistry2Probe = new JavaTestKit(node2);
107         rpcBroker2 = node2.actorOf(RpcBroker.props(domRpcService2));
108         remoteRpcImpl1 = new RemoteRpcImplementation(rpcRegistry1Probe.getRef(), config1);
109         remoteRpcImpl2 = new RemoteRpcImplementation(rpcRegistry2Probe.getRef(), config2);
110
111
112     }
113
114     static void assertRpcErrorEquals(final RpcError rpcError, final ErrorSeverity severity,
115             final ErrorType errorType, final String tag, final String message, final String applicationTag, final String info,
116             final String causeMsg) {
117         assertEquals("getSeverity", severity, rpcError.getSeverity());
118         assertEquals("getErrorType", errorType, rpcError.getErrorType());
119         assertEquals("getTag", tag, rpcError.getTag());
120         assertTrue("getMessage contains " + message, rpcError.getMessage().contains(message));
121         assertEquals("getApplicationTag", applicationTag, rpcError.getApplicationTag());
122         assertEquals("getInfo", info, rpcError.getInfo());
123
124         if(causeMsg == null) {
125             assertNull("Unexpected cause " + rpcError.getCause(), rpcError.getCause());
126         } else {
127             assertEquals("Cause message", causeMsg, rpcError.getCause().getMessage());
128         }
129     }
130
131     static void assertCompositeNodeEquals(final NormalizedNode<? , ?> exp, final NormalizedNode<? , ? > actual) {
132         assertEquals(exp, actual);
133     }
134
135     static ContainerNode makeRPCInput(final String data) {
136         return Builders.containerBuilder().withNodeIdentifier(new NodeIdentifier(TEST_RPC_INPUT))
137             .withChild(ImmutableNodes.leafNode(TEST_RPC_INPUT_DATA, data)).build();
138
139     }
140
141     static ContainerNode makeRPCOutput(final String data) {
142         return Builders.containerBuilder().withNodeIdentifier(new NodeIdentifier(TEST_RPC_OUTPUT))
143                 .withChild(ImmutableNodes.leafNode(TEST_RPC_OUTPUT, data)).build();
144     }
145
146     static void assertFailedRpcResult(final DOMRpcResult rpcResult, final ErrorSeverity severity,
147             final ErrorType errorType, final String tag, final String message, final String applicationTag, final String info,
148             final String causeMsg) {
149
150         assertNotNull("RpcResult was null", rpcResult);
151         final Collection<RpcError> rpcErrors = rpcResult.getErrors();
152         assertEquals("RpcErrors count", 1, rpcErrors.size());
153         assertRpcErrorEquals(rpcErrors.iterator().next(), severity, errorType, tag, message,
154                 applicationTag, info, causeMsg);
155     }
156
157     static void assertSuccessfulRpcResult(final DOMRpcResult rpcResult,
158             final NormalizedNode<? , ?> expOutput) {
159         assertNotNull("RpcResult was null", rpcResult);
160         assertCompositeNodeEquals(expOutput, rpcResult.getResult());
161     }
162
163     static class TestException extends Exception {
164         private static final long serialVersionUID = 1L;
165
166         static final String MESSAGE = "mock error";
167
168         TestException() {
169             super(MESSAGE);
170         }
171     }
172 }