431c4a86edef40173a4a4bb1a4fc22541ae1f87c
[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(new YangInstanceIdentifier.NodeIdentifier(TEST_RPC));
68     public static final DOMRpcIdentifier TEST_RPC_ID = DOMRpcIdentifier.create(TEST_RPC_TYPE, TEST_PATH);
69
70     static ActorSystem node1;
71     static ActorSystem node2;
72     static RemoteRpcProviderConfig config1;
73     static RemoteRpcProviderConfig config2;
74
75     protected ActorRef rpcBroker1;
76     protected JavaTestKit rpcRegistry1Probe;
77     protected ActorRef rpcBroker2;
78     protected JavaTestKit rpcRegistry2Probe;
79     protected Broker.ProviderSession brokerSession;
80     protected SchemaContext schemaContext;
81     protected RemoteRpcImplementation remoteRpcImpl1;
82     protected RemoteRpcImplementation remoteRpcImpl2;
83     protected DOMRpcService domRpcService1;
84     protected DOMRpcService domRpcService2;
85
86     @BeforeClass
87     public static void setup() throws InterruptedException {
88         config1 = new RemoteRpcProviderConfig.Builder("memberA").build();
89         config2 = new RemoteRpcProviderConfig.Builder("memberB").build();
90         node1 = ActorSystem.create("opendaylight-rpc", config1.get());
91         node2 = ActorSystem.create("opendaylight-rpc", config2.get());
92     }
93
94     @AfterClass
95     public static void teardown() {
96         JavaTestKit.shutdownActorSystem(node1);
97         JavaTestKit.shutdownActorSystem(node2);
98         node1 = null;
99         node2 = null;
100     }
101
102     @Before
103     public void setUp() throws Exception {
104         final ByteSource byteSource = new ByteSource() {
105             @Override
106             public InputStream openStream() throws IOException {
107                 return AbstractRpcTest.this.getClass().getResourceAsStream("/test-rpc.yang");
108             }
109         };
110
111         final CrossSourceStatementReactor.BuildAction reactor = YangInferencePipeline.RFC6020_REACTOR.newBuild();
112         final ArrayList<ByteSource> sources = Lists.newArrayList(byteSource);
113
114         try {
115             schemaContext = reactor.buildEffective(sources);
116         } catch (ReactorException e) {
117             throw new RuntimeException("Unable to build schema context from " + sources, e);
118         }
119
120         domRpcService1 = Mockito.mock(DOMRpcService.class);
121         domRpcService2 = Mockito.mock(DOMRpcService.class);
122         rpcRegistry1Probe = new JavaTestKit(node1);
123         rpcBroker1 = node1.actorOf(RpcBroker.props(domRpcService1));
124         rpcRegistry2Probe = new JavaTestKit(node2);
125         rpcBroker2 = node2.actorOf(RpcBroker.props(domRpcService2));
126         remoteRpcImpl1 = new RemoteRpcImplementation(rpcRegistry1Probe.getRef(), config1);
127         remoteRpcImpl2 = new RemoteRpcImplementation(rpcRegistry2Probe.getRef(), config2);
128
129     }
130
131     static void assertRpcErrorEquals(final RpcError rpcError, final ErrorSeverity severity,
132             final ErrorType errorType, final String tag, final String message, final String applicationTag, final String info,
133             final String causeMsg) {
134         assertEquals("getSeverity", severity, rpcError.getSeverity());
135         assertEquals("getErrorType", errorType, rpcError.getErrorType());
136         assertEquals("getTag", tag, rpcError.getTag());
137         assertTrue("getMessage contains " + message, rpcError.getMessage().contains(message));
138         assertEquals("getApplicationTag", applicationTag, rpcError.getApplicationTag());
139         assertEquals("getInfo", info, rpcError.getInfo());
140
141         if(causeMsg == null) {
142             assertNull("Unexpected cause " + rpcError.getCause(), rpcError.getCause());
143         } else {
144             assertEquals("Cause message", causeMsg, rpcError.getCause().getMessage());
145         }
146     }
147
148     static void assertCompositeNodeEquals(final NormalizedNode<? , ?> exp, final NormalizedNode<? , ? > actual) {
149         assertEquals(exp, actual);
150     }
151
152     public static ContainerNode makeRPCInput(final String data) {
153         return Builders.containerBuilder().withNodeIdentifier(new NodeIdentifier(TEST_RPC_INPUT))
154             .withChild(ImmutableNodes.leafNode(TEST_RPC_INPUT_DATA, data)).build();
155
156     }
157
158     public static ContainerNode makeRPCOutput(final String data) {
159         return Builders.containerBuilder().withNodeIdentifier(new NodeIdentifier(TEST_RPC_OUTPUT))
160                 .withChild(ImmutableNodes.leafNode(TEST_RPC_OUTPUT, data)).build();
161     }
162
163     static void assertFailedRpcResult(final DOMRpcResult rpcResult, final ErrorSeverity severity,
164             final ErrorType errorType, final String tag, final String message, final String applicationTag, final String info,
165             final String causeMsg) {
166
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 }