Remove reliance on org.opendaylight.controller.sal.core.api.Broker
[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.net.URI;
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.controller.md.sal.dom.api.DOMRpcIdentifier;
27 import org.opendaylight.controller.md.sal.dom.api.DOMRpcResult;
28 import org.opendaylight.controller.md.sal.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.parser.spi.meta.ReactorException;
42 import org.opendaylight.yangtools.yang.test.util.YangParserTestUtils;
43
44 /**
45  * Base class for RPC tests.
46  *
47  * @author Thomas Pantelis
48  */
49 public class AbstractRpcTest {
50     static final String TEST_REV = "2014-08-28";
51     static final String TEST_NS = "urn:test";
52     static final URI TEST_URI = URI.create(TEST_NS);
53     static final QName TEST_RPC = QName.create(TEST_NS, TEST_REV, "test-rpc");
54     static final QName TEST_RPC_INPUT = QName.create(TEST_NS, TEST_REV, "input");
55     static final QName TEST_RPC_INPUT_DATA = QName.create(TEST_NS, TEST_REV, "input-data");
56     static final QName TEST_RPC_OUTPUT = QName.create(TEST_NS, TEST_REV, "output");
57     static final QName TEST_RPC_OUTPUT_DATA = new QName(TEST_URI, "output-data");
58
59
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
65     static ActorSystem node1;
66     static ActorSystem node2;
67     static RemoteRpcProviderConfig config1;
68     static RemoteRpcProviderConfig config2;
69
70     protected ActorRef rpcInvoker1;
71     protected JavaTestKit rpcRegistry1Probe;
72     protected ActorRef rpcInvoker2;
73     protected JavaTestKit rpcRegistry2Probe;
74     protected SchemaContext schemaContext;
75     protected RemoteRpcImplementation remoteRpcImpl1;
76     protected RemoteRpcImplementation remoteRpcImpl2;
77
78     @Mock
79     protected DOMRpcService domRpcService1;
80     @Mock
81     protected DOMRpcService domRpcService2;
82
83     @BeforeClass
84     public static void setup() throws InterruptedException {
85         config1 = new RemoteRpcProviderConfig.Builder("memberA").build();
86         config2 = new RemoteRpcProviderConfig.Builder("memberB").build();
87         node1 = ActorSystem.create("opendaylight-rpc", config1.get());
88         node2 = ActorSystem.create("opendaylight-rpc", config2.get());
89     }
90
91     @AfterClass
92     public static void teardown() {
93         JavaTestKit.shutdownActorSystem(node1);
94         JavaTestKit.shutdownActorSystem(node2);
95         node1 = null;
96         node2 = null;
97     }
98
99     @Before
100     public void setUp() throws ReactorException {
101         schemaContext = YangParserTestUtils.parseYangResources(AbstractRpcTest.class, "/test-rpc.yang");
102
103         MockitoAnnotations.initMocks(this);
104
105         rpcRegistry1Probe = new JavaTestKit(node1);
106         rpcInvoker1 = node1.actorOf(RpcInvoker.props(domRpcService1));
107         rpcRegistry2Probe = new JavaTestKit(node2);
108         rpcInvoker2 = node2.actorOf(RpcInvoker.props(domRpcService2));
109         remoteRpcImpl1 = new RemoteRpcImplementation(rpcInvoker2, config1);
110         remoteRpcImpl2 = new RemoteRpcImplementation(rpcInvoker1, config2);
111     }
112
113     static void assertRpcErrorEquals(final RpcError rpcError, final ErrorSeverity severity,
114             final ErrorType errorType, final String tag, final String message, final String applicationTag,
115             final String info, final String causeMsg) {
116         assertEquals("getSeverity", severity, rpcError.getSeverity());
117         assertEquals("getErrorType", errorType, rpcError.getErrorType());
118         assertEquals("getTag", tag, rpcError.getTag());
119         assertTrue("getMessage contains " + message, rpcError.getMessage().contains(message));
120         assertEquals("getApplicationTag", applicationTag, rpcError.getApplicationTag());
121         assertEquals("getInfo", info, rpcError.getInfo());
122
123         if (causeMsg == null) {
124             assertNull("Unexpected cause " + rpcError.getCause(), rpcError.getCause());
125         } else {
126             assertEquals("Cause message", causeMsg, rpcError.getCause().getMessage());
127         }
128     }
129
130     static void assertCompositeNodeEquals(final NormalizedNode<? , ?> exp, final NormalizedNode<? , ?> actual) {
131         assertEquals(exp, actual);
132     }
133
134     public static ContainerNode makeRPCInput(final String data) {
135         return Builders.containerBuilder().withNodeIdentifier(new NodeIdentifier(TEST_RPC_INPUT))
136             .withChild(ImmutableNodes.leafNode(TEST_RPC_INPUT_DATA, data)).build();
137
138     }
139
140     public static ContainerNode makeRPCOutput(final String data) {
141         return Builders.containerBuilder().withNodeIdentifier(new NodeIdentifier(TEST_RPC_OUTPUT))
142                 .withChild(ImmutableNodes.leafNode(TEST_RPC_OUTPUT, data)).build();
143     }
144
145     static void assertFailedRpcResult(final DOMRpcResult rpcResult, final ErrorSeverity severity,
146             final ErrorType errorType, final String tag, final String message, final String applicationTag,
147             final String info, final String causeMsg) {
148         assertNotNull("RpcResult was null", rpcResult);
149         final Collection<RpcError> rpcErrors = rpcResult.getErrors();
150         assertEquals("RpcErrors count", 1, rpcErrors.size());
151         assertRpcErrorEquals(rpcErrors.iterator().next(), severity, errorType, tag, message,
152                 applicationTag, info, causeMsg);
153     }
154
155     static void assertSuccessfulRpcResult(final DOMRpcResult rpcResult,
156             final NormalizedNode<? , ?> expOutput) {
157         assertNotNull("RpcResult was null", rpcResult);
158         assertCompositeNodeEquals(expOutput, rpcResult.getResult());
159     }
160
161     static class TestException extends Exception {
162         private static final long serialVersionUID = 1L;
163
164         static final String MESSAGE = "mock error";
165
166         TestException() {
167             super(MESSAGE);
168         }
169     }
170 }