46406fd4feebad58c7546ec5df12e5bb1f2f8137
[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 akka.actor.ActorRef;
12 import akka.actor.ActorSystem;
13 import akka.testkit.JavaTestKit;
14 import com.google.common.collect.ImmutableList;
15 import org.junit.AfterClass;
16 import org.junit.Before;
17 import org.junit.BeforeClass;
18 import org.mockito.Mockito;
19 import org.opendaylight.controller.sal.core.api.Broker;
20 import org.opendaylight.yangtools.yang.common.QName;
21 import org.opendaylight.yangtools.yang.common.RpcError;
22 import org.opendaylight.yangtools.yang.common.RpcError.ErrorSeverity;
23 import org.opendaylight.yangtools.yang.common.RpcError.ErrorType;
24 import org.opendaylight.yangtools.yang.common.RpcResult;
25 import org.opendaylight.yangtools.yang.data.api.CompositeNode;
26 import org.opendaylight.yangtools.yang.data.api.Node;
27 import org.opendaylight.yangtools.yang.data.impl.ImmutableCompositeNode;
28 import org.opendaylight.yangtools.yang.data.impl.util.CompositeNodeBuilder;
29 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
30 import org.opendaylight.yangtools.yang.parser.impl.YangParserImpl;
31
32 import java.io.File;
33 import java.net.URI;
34 import java.util.Arrays;
35 import java.util.Collection;
36 import java.util.List;
37
38 import static org.junit.Assert.assertEquals;
39 import static org.junit.Assert.assertNotNull;
40 import static org.junit.Assert.assertNull;
41 import static org.junit.Assert.assertTrue;
42
43 /**
44  * Base class for RPC tests.
45  *
46  * @author Thomas Pantelis
47  */
48 public class AbstractRpcTest {
49     static final String TEST_REV = "2014-08-28";
50     static final String TEST_NS = "urn:test";
51     static final URI TEST_URI = URI.create(TEST_NS);
52     static final QName TEST_RPC = QName.create(TEST_NS, TEST_REV, "test-rpc");
53     static final QName TEST_RPC_INPUT = QName.create(TEST_NS, TEST_REV, "input");
54     static final QName TEST_RPC_INPUT_DATA = QName.create(TEST_NS, TEST_REV, "input-data");
55     static final QName TEST_RPC_OUTPUT = QName.create(TEST_NS, TEST_REV, "output");
56     static final QName TEST_RPC_OUTPUT_DATA = new QName(TEST_URI, "output-data");
57
58     static ActorSystem node1;
59     static ActorSystem node2;
60
61     protected ActorRef rpcBroker1;
62     protected JavaTestKit probeReg1;
63     protected ActorRef rpcBroker2;
64     protected JavaTestKit probeReg2;
65     protected Broker.ProviderSession brokerSession;
66     protected SchemaContext schemaContext;
67
68     @BeforeClass
69     public static void setup() throws InterruptedException {
70         RemoteRpcProviderConfig config1 = new RemoteRpcProviderConfig.Builder("memberA").build();
71         RemoteRpcProviderConfig config2 = new RemoteRpcProviderConfig.Builder("memberB").build();
72         node1 = ActorSystem.create("opendaylight-rpc", config1.get());
73         node2 = ActorSystem.create("opendaylight-rpc", config2.get());
74     }
75
76     @AfterClass
77     public static void teardown() {
78         JavaTestKit.shutdownActorSystem(node1);
79         JavaTestKit.shutdownActorSystem(node2);
80         node1 = null;
81         node2 = null;
82     }
83
84     @Before
85     public void setUp() {
86         schemaContext = new YangParserImpl().parseFiles(Arrays.asList(
87                 new File(RpcBrokerTest.class.getResource("/test-rpc.yang").getPath())));
88
89         brokerSession = Mockito.mock(Broker.ProviderSession.class);
90         probeReg1 = new JavaTestKit(node1);
91         rpcBroker1 = node1.actorOf(RpcBroker.props(brokerSession, probeReg1.getRef(), schemaContext));
92         probeReg2 = new JavaTestKit(node2);
93         rpcBroker2 = node2.actorOf(RpcBroker.props(brokerSession, probeReg2.getRef(), schemaContext));
94
95     }
96
97     static void assertRpcErrorEquals(RpcError rpcError, ErrorSeverity severity,
98             ErrorType errorType, String tag, String message, String applicationTag, String info,
99             String causeMsg) {
100         assertEquals("getSeverity", severity, rpcError.getSeverity());
101         assertEquals("getErrorType", errorType, rpcError.getErrorType());
102         assertEquals("getTag", tag, rpcError.getTag());
103         assertTrue("getMessage contains " + message, rpcError.getMessage().contains(message));
104         assertEquals("getApplicationTag", applicationTag, rpcError.getApplicationTag());
105         assertEquals("getInfo", info, rpcError.getInfo());
106
107         if(causeMsg == null) {
108             assertNull("Unexpected cause " + rpcError.getCause(), rpcError.getCause());
109         } else {
110             assertEquals("Cause message", causeMsg, rpcError.getCause().getMessage());
111         }
112     }
113
114     static void assertCompositeNodeEquals(CompositeNode exp, CompositeNode actual) {
115         assertEquals("NodeType getNamespace", exp.getNodeType().getNamespace(),
116                 actual.getNodeType().getNamespace());
117         assertEquals("NodeType getLocalName", exp.getNodeType().getLocalName(),
118                 actual.getNodeType().getLocalName());
119         for(Node<?> child: exp.getValue()) {
120             List<Node<?>> c = actual.get(child.getNodeType());
121             assertNotNull("Missing expected child " + child.getNodeType(), c);
122             if(child instanceof CompositeNode) {
123                 assertCompositeNodeEquals((CompositeNode) child, (CompositeNode)c.get(0));
124             } else {
125                 assertEquals("Value for Node " + child.getNodeType(), child.getValue(),
126                         c.get(0).getValue());
127             }
128         }
129     }
130
131     static CompositeNode makeRPCInput(String data) {
132         CompositeNodeBuilder<ImmutableCompositeNode> builder = ImmutableCompositeNode.builder()
133                 .setQName(TEST_RPC_INPUT).addLeaf(TEST_RPC_INPUT_DATA, data);
134         return ImmutableCompositeNode.create(
135                 TEST_RPC, ImmutableList.<Node<?>>of(builder.toInstance()));
136     }
137
138     static CompositeNode makeRPCOutput(String data) {
139         CompositeNodeBuilder<ImmutableCompositeNode> builder = ImmutableCompositeNode.builder()
140                 .setQName(TEST_RPC_OUTPUT).addLeaf(TEST_RPC_OUTPUT_DATA, data);
141         return ImmutableCompositeNode.create(
142                 TEST_RPC, ImmutableList.<Node<?>>of(builder.toInstance()));
143     }
144
145     static void assertFailedRpcResult(RpcResult<CompositeNode> rpcResult, ErrorSeverity severity,
146             ErrorType errorType, String tag, String message, String applicationTag, String info,
147             String causeMsg) {
148
149         assertNotNull("RpcResult was null", rpcResult);
150         assertEquals("isSuccessful", false, rpcResult.isSuccessful());
151         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(RpcResult<CompositeNode> rpcResult,
158             CompositeNode expOutput) {
159
160         assertNotNull("RpcResult was null", rpcResult);
161         assertEquals("isSuccessful", true, rpcResult.isSuccessful());
162         assertCompositeNodeEquals(expOutput, rpcResult.getResult());
163     }
164
165     static class TestException extends Exception {
166         private static final long serialVersionUID = 1L;
167
168         static final String MESSAGE = "mock error";
169
170         TestException() {
171             super(MESSAGE);
172         }
173     }
174 }