BUG-7159: eliminate use of CrossStatementSourceReactor
[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.InputStream;
20 import java.net.URI;
21 import java.util.Collection;
22 import java.util.Collections;
23 import java.util.List;
24 import org.junit.AfterClass;
25 import org.junit.Before;
26 import org.junit.BeforeClass;
27 import org.mockito.Mockito;
28 import org.opendaylight.controller.md.sal.dom.api.DOMRpcIdentifier;
29 import org.opendaylight.controller.md.sal.dom.api.DOMRpcResult;
30 import org.opendaylight.controller.md.sal.dom.api.DOMRpcService;
31 import org.opendaylight.controller.sal.core.api.Broker;
32 import org.opendaylight.yangtools.yang.common.QName;
33 import org.opendaylight.yangtools.yang.common.RpcError;
34 import org.opendaylight.yangtools.yang.common.RpcError.ErrorSeverity;
35 import org.opendaylight.yangtools.yang.common.RpcError.ErrorType;
36 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
37 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.NodeIdentifier;
38 import org.opendaylight.yangtools.yang.data.api.schema.ContainerNode;
39 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
40 import org.opendaylight.yangtools.yang.data.impl.schema.Builders;
41 import org.opendaylight.yangtools.yang.data.impl.schema.ImmutableNodes;
42 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
43 import org.opendaylight.yangtools.yang.model.api.SchemaPath;
44 import org.opendaylight.yangtools.yang.parser.spi.meta.ReactorException;
45 import org.opendaylight.yangtools.yang.test.util.YangParserTestUtils;
46
47 /**
48  * Base class for RPC tests.
49  *
50  * @author Thomas Pantelis
51  */
52 public class AbstractRpcTest {
53     static final String TEST_REV = "2014-08-28";
54     static final String TEST_NS = "urn:test";
55     static final URI TEST_URI = URI.create(TEST_NS);
56     static final QName TEST_RPC = QName.create(TEST_NS, TEST_REV, "test-rpc");
57     static final QName TEST_RPC_INPUT = QName.create(TEST_NS, TEST_REV, "input");
58     static final QName TEST_RPC_INPUT_DATA = QName.create(TEST_NS, TEST_REV, "input-data");
59     static final QName TEST_RPC_OUTPUT = QName.create(TEST_NS, TEST_REV, "output");
60     static final QName TEST_RPC_OUTPUT_DATA = new QName(TEST_URI, "output-data");
61
62
63     static final SchemaPath TEST_RPC_TYPE = SchemaPath.create(true, TEST_RPC);
64     static final YangInstanceIdentifier TEST_PATH = YangInstanceIdentifier.create(
65             new YangInstanceIdentifier.NodeIdentifier(TEST_RPC));
66     public static final DOMRpcIdentifier TEST_RPC_ID = DOMRpcIdentifier.create(TEST_RPC_TYPE, TEST_PATH);
67
68     static ActorSystem node1;
69     static ActorSystem node2;
70     static RemoteRpcProviderConfig config1;
71     static RemoteRpcProviderConfig config2;
72
73     protected ActorRef rpcBroker1;
74     protected JavaTestKit rpcRegistry1Probe;
75     protected ActorRef rpcBroker2;
76     protected JavaTestKit rpcRegistry2Probe;
77     protected Broker.ProviderSession brokerSession;
78     protected SchemaContext schemaContext;
79     protected RemoteRpcImplementation remoteRpcImpl1;
80     protected RemoteRpcImplementation remoteRpcImpl2;
81     protected DOMRpcService domRpcService1;
82     protected DOMRpcService domRpcService2;
83
84     @BeforeClass
85     public static void setup() throws InterruptedException {
86         config1 = new RemoteRpcProviderConfig.Builder("memberA").build();
87         config2 = new RemoteRpcProviderConfig.Builder("memberB").build();
88         node1 = ActorSystem.create("opendaylight-rpc", config1.get());
89         node2 = ActorSystem.create("opendaylight-rpc", config2.get());
90     }
91
92     @AfterClass
93     public static void teardown() {
94         JavaTestKit.shutdownActorSystem(node1);
95         JavaTestKit.shutdownActorSystem(node2);
96         node1 = null;
97         node2 = null;
98     }
99
100     @Before
101     public void setUp() throws Exception {
102         final List<InputStream> sources = Collections.singletonList(
103             AbstractRpcTest.this.getClass().getResourceAsStream("/test-rpc.yang"));
104
105         try {
106             schemaContext = YangParserTestUtils.parseYangStreams(sources);
107         } catch (ReactorException e) {
108             throw new RuntimeException("Unable to build schema context from " + sources, e);
109         }
110
111         domRpcService1 = Mockito.mock(DOMRpcService.class);
112         domRpcService2 = Mockito.mock(DOMRpcService.class);
113         rpcRegistry1Probe = new JavaTestKit(node1);
114         rpcBroker1 = node1.actorOf(RpcBroker.props(domRpcService1));
115         rpcRegistry2Probe = new JavaTestKit(node2);
116         rpcBroker2 = node2.actorOf(RpcBroker.props(domRpcService2));
117         remoteRpcImpl1 = new RemoteRpcImplementation(rpcRegistry1Probe.getRef(), config1);
118         remoteRpcImpl2 = new RemoteRpcImplementation(rpcRegistry2Probe.getRef(), config2);
119
120     }
121
122     static void assertRpcErrorEquals(final RpcError rpcError, final ErrorSeverity severity,
123             final ErrorType errorType, final String tag, final String message, final String applicationTag,
124             final String info, final String causeMsg) {
125         assertEquals("getSeverity", severity, rpcError.getSeverity());
126         assertEquals("getErrorType", errorType, rpcError.getErrorType());
127         assertEquals("getTag", tag, rpcError.getTag());
128         assertTrue("getMessage contains " + message, rpcError.getMessage().contains(message));
129         assertEquals("getApplicationTag", applicationTag, rpcError.getApplicationTag());
130         assertEquals("getInfo", info, rpcError.getInfo());
131
132         if (causeMsg == null) {
133             assertNull("Unexpected cause " + rpcError.getCause(), rpcError.getCause());
134         } else {
135             assertEquals("Cause message", causeMsg, rpcError.getCause().getMessage());
136         }
137     }
138
139     static void assertCompositeNodeEquals(final NormalizedNode<? , ?> exp, final NormalizedNode<? , ?> actual) {
140         assertEquals(exp, actual);
141     }
142
143     public static ContainerNode makeRPCInput(final String data) {
144         return Builders.containerBuilder().withNodeIdentifier(new NodeIdentifier(TEST_RPC_INPUT))
145             .withChild(ImmutableNodes.leafNode(TEST_RPC_INPUT_DATA, data)).build();
146
147     }
148
149     public static ContainerNode makeRPCOutput(final String data) {
150         return Builders.containerBuilder().withNodeIdentifier(new NodeIdentifier(TEST_RPC_OUTPUT))
151                 .withChild(ImmutableNodes.leafNode(TEST_RPC_OUTPUT, data)).build();
152     }
153
154     static void assertFailedRpcResult(final DOMRpcResult rpcResult, final ErrorSeverity severity,
155             final ErrorType errorType, final String tag, final String message, final String applicationTag,
156             final String info, final String causeMsg) {
157         assertNotNull("RpcResult was null", rpcResult);
158         final Collection<RpcError> rpcErrors = rpcResult.getErrors();
159         assertEquals("RpcErrors count", 1, rpcErrors.size());
160         assertRpcErrorEquals(rpcErrors.iterator().next(), severity, errorType, tag, message,
161                 applicationTag, info, causeMsg);
162     }
163
164     static void assertSuccessfulRpcResult(final DOMRpcResult rpcResult,
165             final NormalizedNode<? , ?> expOutput) {
166         assertNotNull("RpcResult was null", rpcResult);
167         assertCompositeNodeEquals(expOutput, rpcResult.getResult());
168     }
169
170     static class TestException extends Exception {
171         private static final long serialVersionUID = 1L;
172
173         static final String MESSAGE = "mock error";
174
175         TestException() {
176             super(MESSAGE);
177         }
178     }
179 }

©2013 OpenDaylight, A Linux Foundation Collaborative Project. All Rights Reserved.
OpenDaylight is a registered trademark of The OpenDaylight Project, Inc.
Linux Foundation and OpenDaylight are registered trademarks of the Linux Foundation.
Linux is a registered trademark of Linus Torvalds.