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