438c7423f6d3f6985adc624e33714bb8acf399bd
[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.controller.sal.core.api.Broker;
30 import org.opendaylight.yangtools.yang.common.QName;
31 import org.opendaylight.yangtools.yang.common.RpcError;
32 import org.opendaylight.yangtools.yang.common.RpcError.ErrorSeverity;
33 import org.opendaylight.yangtools.yang.common.RpcError.ErrorType;
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.impl.schema.Builders;
39 import org.opendaylight.yangtools.yang.data.impl.schema.ImmutableNodes;
40 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
41 import org.opendaylight.yangtools.yang.model.api.SchemaPath;
42 import org.opendaylight.yangtools.yang.parser.spi.meta.ReactorException;
43 import org.opendaylight.yangtools.yang.test.util.YangParserTestUtils;
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(
63             new YangInstanceIdentifier.NodeIdentifier(TEST_RPC));
64     public static final DOMRpcIdentifier TEST_RPC_ID = DOMRpcIdentifier.create(TEST_RPC_TYPE, TEST_PATH);
65
66     static ActorSystem node1;
67     static ActorSystem node2;
68     static RemoteRpcProviderConfig config1;
69     static RemoteRpcProviderConfig config2;
70
71     protected ActorRef rpcInvoker1;
72     protected JavaTestKit rpcRegistry1Probe;
73     protected ActorRef rpcInvoker2;
74     protected JavaTestKit rpcRegistry2Probe;
75     protected Broker.ProviderSession brokerSession;
76     protected SchemaContext schemaContext;
77     protected RemoteRpcImplementation remoteRpcImpl1;
78     protected RemoteRpcImplementation remoteRpcImpl2;
79
80     @Mock
81     protected DOMRpcService domRpcService1;
82     @Mock
83     protected DOMRpcService domRpcService2;
84
85     @BeforeClass
86     public static void setup() throws InterruptedException {
87         config1 = new RemoteRpcProviderConfig.Builder("memberA").build();
88         config2 = new RemoteRpcProviderConfig.Builder("memberB").build();
89         node1 = ActorSystem.create("opendaylight-rpc", config1.get());
90         node2 = ActorSystem.create("opendaylight-rpc", config2.get());
91     }
92
93     @AfterClass
94     public static void teardown() {
95         JavaTestKit.shutdownActorSystem(node1);
96         JavaTestKit.shutdownActorSystem(node2);
97         node1 = null;
98         node2 = null;
99     }
100
101     @Before
102     public void setUp() throws ReactorException {
103         schemaContext = YangParserTestUtils.parseYangResources(AbstractRpcTest.class, "/test-rpc.yang");
104
105         MockitoAnnotations.initMocks(this);
106
107         rpcRegistry1Probe = new JavaTestKit(node1);
108         rpcInvoker1 = node1.actorOf(RpcInvoker.props(domRpcService1));
109         rpcRegistry2Probe = new JavaTestKit(node2);
110         rpcInvoker2 = node2.actorOf(RpcInvoker.props(domRpcService2));
111         remoteRpcImpl1 = new RemoteRpcImplementation(rpcInvoker2, config1);
112         remoteRpcImpl2 = new RemoteRpcImplementation(rpcInvoker1, config2);
113     }
114
115     static void assertRpcErrorEquals(final RpcError rpcError, final ErrorSeverity severity,
116             final ErrorType errorType, final String tag, final String message, final String applicationTag,
117             final String info, final String causeMsg) {
118         assertEquals("getSeverity", severity, rpcError.getSeverity());
119         assertEquals("getErrorType", errorType, rpcError.getErrorType());
120         assertEquals("getTag", tag, rpcError.getTag());
121         assertTrue("getMessage contains " + message, rpcError.getMessage().contains(message));
122         assertEquals("getApplicationTag", applicationTag, rpcError.getApplicationTag());
123         assertEquals("getInfo", info, rpcError.getInfo());
124
125         if (causeMsg == null) {
126             assertNull("Unexpected cause " + rpcError.getCause(), rpcError.getCause());
127         } else {
128             assertEquals("Cause message", causeMsg, rpcError.getCause().getMessage());
129         }
130     }
131
132     static void assertCompositeNodeEquals(final NormalizedNode<? , ?> exp, final NormalizedNode<? , ?> actual) {
133         assertEquals(exp, actual);
134     }
135
136     public static ContainerNode makeRPCInput(final String data) {
137         return Builders.containerBuilder().withNodeIdentifier(new NodeIdentifier(TEST_RPC_INPUT))
138             .withChild(ImmutableNodes.leafNode(TEST_RPC_INPUT_DATA, data)).build();
139
140     }
141
142     public static ContainerNode makeRPCOutput(final String data) {
143         return Builders.containerBuilder().withNodeIdentifier(new NodeIdentifier(TEST_RPC_OUTPUT))
144                 .withChild(ImmutableNodes.leafNode(TEST_RPC_OUTPUT, data)).build();
145     }
146
147     static void assertFailedRpcResult(final DOMRpcResult rpcResult, final ErrorSeverity severity,
148             final ErrorType errorType, final String tag, final String message, final String applicationTag,
149             final String info, final String causeMsg) {
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 }