0736fae6d7eb825da2f769d5d42bebef986a9a19
[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.javadsl.TestKit;
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.mdsal.dom.api.DOMRpcIdentifier;
27 import org.opendaylight.mdsal.dom.api.DOMRpcResult;
28 import org.opendaylight.mdsal.dom.api.DOMRpcService;
29 import org.opendaylight.yangtools.yang.common.QName;
30 import org.opendaylight.yangtools.yang.common.RpcError;
31 import org.opendaylight.yangtools.yang.common.RpcError.ErrorSeverity;
32 import org.opendaylight.yangtools.yang.common.RpcError.ErrorType;
33 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
34 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.NodeIdentifier;
35 import org.opendaylight.yangtools.yang.data.api.schema.ContainerNode;
36 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
37 import org.opendaylight.yangtools.yang.data.impl.schema.Builders;
38 import org.opendaylight.yangtools.yang.data.impl.schema.ImmutableNodes;
39 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
40 import org.opendaylight.yangtools.yang.model.api.SchemaPath;
41 import org.opendaylight.yangtools.yang.test.util.YangParserTestUtils;
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 = QName.create(TEST_URI, "output-data");
57
58
59     static final SchemaPath TEST_RPC_TYPE = SchemaPath.create(true, TEST_RPC);
60     static final YangInstanceIdentifier TEST_PATH = YangInstanceIdentifier.create(
61             new YangInstanceIdentifier.NodeIdentifier(TEST_RPC));
62     public static final DOMRpcIdentifier TEST_RPC_ID = DOMRpcIdentifier.create(TEST_RPC_TYPE, TEST_PATH);
63
64     static ActorSystem node1;
65     static ActorSystem node2;
66     static RemoteRpcProviderConfig config1;
67     static RemoteRpcProviderConfig config2;
68
69     protected ActorRef rpcInvoker1;
70     protected TestKit rpcRegistry1Probe;
71     protected ActorRef rpcInvoker2;
72     protected TestKit rpcRegistry2Probe;
73     protected SchemaContext schemaContext;
74     protected RemoteRpcImplementation remoteRpcImpl1;
75     protected RemoteRpcImplementation remoteRpcImpl2;
76
77     @Mock
78     protected DOMRpcService domRpcService1;
79     @Mock
80     protected DOMRpcService domRpcService2;
81
82     @BeforeClass
83     public static void setup() {
84         config1 = new RemoteRpcProviderConfig.Builder("memberA").build();
85         config2 = new RemoteRpcProviderConfig.Builder("memberB").build();
86         node1 = ActorSystem.create("opendaylight-rpc", config1.get());
87         node2 = ActorSystem.create("opendaylight-rpc", config2.get());
88     }
89
90     @AfterClass
91     public static void teardown() {
92         TestKit.shutdownActorSystem(node1);
93         TestKit.shutdownActorSystem(node2);
94         node1 = null;
95         node2 = null;
96     }
97
98     @Before
99     public void setUp() {
100         schemaContext = YangParserTestUtils.parseYangResources(AbstractRpcTest.class, "/test-rpc.yang");
101
102         MockitoAnnotations.initMocks(this);
103
104         rpcRegistry1Probe = new TestKit(node1);
105         rpcInvoker1 = node1.actorOf(RpcInvoker.props(domRpcService1));
106         rpcRegistry2Probe = new TestKit(node2);
107         rpcInvoker2 = node2.actorOf(RpcInvoker.props(domRpcService2));
108         remoteRpcImpl1 = new RemoteRpcImplementation(rpcInvoker2, config1);
109         remoteRpcImpl2 = new RemoteRpcImplementation(rpcInvoker1, config2);
110     }
111
112     static void assertRpcErrorEquals(final RpcError rpcError, final ErrorSeverity severity,
113             final ErrorType errorType, final String tag, final String message, final String applicationTag,
114             final String info, final String causeMsg) {
115         assertEquals("getSeverity", severity, rpcError.getSeverity());
116         assertEquals("getErrorType", errorType, rpcError.getErrorType());
117         assertEquals("getTag", tag, rpcError.getTag());
118         assertTrue("getMessage contains " + message, rpcError.getMessage().contains(message));
119         assertEquals("getApplicationTag", applicationTag, rpcError.getApplicationTag());
120         assertEquals("getInfo", info, rpcError.getInfo());
121
122         if (causeMsg == null) {
123             assertNull("Unexpected cause " + rpcError.getCause(), rpcError.getCause());
124         } else {
125             assertEquals("Cause message", causeMsg, rpcError.getCause().getMessage());
126         }
127     }
128
129     static void assertCompositeNodeEquals(final NormalizedNode<? , ?> exp, final NormalizedNode<? , ?> actual) {
130         assertEquals(exp, actual);
131     }
132
133     public static ContainerNode makeRPCInput(final String data) {
134         return Builders.containerBuilder().withNodeIdentifier(new NodeIdentifier(TEST_RPC_INPUT))
135             .withChild(ImmutableNodes.leafNode(TEST_RPC_INPUT_DATA, data)).build();
136
137     }
138
139     public static ContainerNode makeRPCOutput(final String data) {
140         return Builders.containerBuilder().withNodeIdentifier(new NodeIdentifier(TEST_RPC_OUTPUT))
141                 .withChild(ImmutableNodes.leafNode(TEST_RPC_OUTPUT, data)).build();
142     }
143
144     static void assertFailedRpcResult(final DOMRpcResult rpcResult, final ErrorSeverity severity,
145             final ErrorType errorType, final String tag, final String message, final String applicationTag,
146             final String info, final String causeMsg) {
147         assertNotNull("RpcResult was null", rpcResult);
148         final Collection<? extends RpcError> rpcErrors = rpcResult.getErrors();
149         assertEquals("RpcErrors count", 1, rpcErrors.size());
150         assertRpcErrorEquals(rpcErrors.iterator().next(), severity, errorType, tag, message,
151                 applicationTag, info, causeMsg);
152     }
153
154     static void assertSuccessfulRpcResult(final DOMRpcResult rpcResult,
155             final NormalizedNode<? , ?> expOutput) {
156         assertNotNull("RpcResult was null", rpcResult);
157         assertCompositeNodeEquals(expOutput, rpcResult.getResult());
158     }
159
160     static class TestException extends Exception {
161         private static final long serialVersionUID = 1L;
162
163         static final String MESSAGE = "mock error";
164
165         TestException() {
166             super(MESSAGE);
167         }
168     }
169 }

©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.