/*
- * Copyright (c) 2014 Cisco Systems, Inc. and others. All rights reserved.
+ * Copyright (c) 2014, 2017 Cisco Systems, Inc. and others. All rights reserved.
*
* This program and the accompanying materials are made available under the
* terms of the Eclipse Public License v1.0 which accompanies this distribution,
package org.opendaylight.controller.remote.rpc;
+import static org.junit.Assert.assertEquals;
+import static org.mockito.Matchers.eq;
+import static org.mockito.Mockito.when;
-import akka.actor.ActorRef;
-import akka.actor.ActorSystem;
-import akka.japi.Pair;
-import akka.testkit.JavaTestKit;
-import com.google.common.util.concurrent.Futures;
-import com.typesafe.config.ConfigFactory;
-import junit.framework.Assert;
-import org.junit.AfterClass;
-import org.junit.Before;
-import org.junit.BeforeClass;
+import akka.actor.Status.Failure;
+import akka.testkit.javadsl.TestKit;
+import org.junit.Assert;
import org.junit.Test;
import org.mockito.Mockito;
-import org.opendaylight.controller.remote.rpc.messages.ErrorResponse;
-import org.opendaylight.controller.remote.rpc.messages.InvokeRpc;
+import org.opendaylight.controller.remote.rpc.messages.ExecuteRpc;
import org.opendaylight.controller.remote.rpc.messages.RpcResponse;
-import org.opendaylight.controller.remote.rpc.registry.RpcRegistry;
-import org.opendaylight.controller.sal.common.util.Rpcs;
-import org.opendaylight.controller.sal.core.api.Broker;
-import org.opendaylight.yangtools.yang.common.QName;
-import org.opendaylight.yangtools.yang.common.RpcError;
-import org.opendaylight.yangtools.yang.common.RpcResult;
-import org.opendaylight.yangtools.yang.data.api.CompositeNode;
-import org.opendaylight.yangtools.yang.data.api.ModifyAction;
-import org.opendaylight.yangtools.yang.data.api.Node;
-import org.opendaylight.yangtools.yang.data.impl.ImmutableCompositeNode;
-import org.opendaylight.yangtools.yang.model.api.SchemaContext;
-
-import java.net.URI;
-import java.net.URISyntaxException;
-import java.util.ArrayList;
-import java.util.Collection;
-import java.util.List;
-import java.util.concurrent.Future;
-
-import static org.mockito.Mockito.mock;
-import static org.mockito.Mockito.when;
-
-public class RpcBrokerTest {
-
- static ActorSystem node1;
- static ActorSystem node2;
- private ActorRef rpcBroker1;
- private JavaTestKit probeReg1;
- private ActorRef rpcBroker2;
- private JavaTestKit probeReg2;
- private Broker.ProviderSession brokerSession;
-
-
- @BeforeClass
- public static void setup() throws InterruptedException {
- node1 = ActorSystem.create("opendaylight-rpc", ConfigFactory.load().getConfig("memberA"));
- node2 = ActorSystem.create("opendaylight-rpc", ConfigFactory.load().getConfig("memberB"));
- }
-
- @AfterClass
- public static void teardown() {
- JavaTestKit.shutdownActorSystem(node1);
- JavaTestKit.shutdownActorSystem(node2);
- node1 = null;
- node2 = null;
- }
-
- @Before
- public void createActor() {
- brokerSession = Mockito.mock(Broker.ProviderSession.class);
- SchemaContext schemaContext = mock(SchemaContext.class);
- probeReg1 = new JavaTestKit(node1);
- rpcBroker1 = node1.actorOf(RpcBroker.props(brokerSession, probeReg1.getRef(), schemaContext));
- probeReg2 = new JavaTestKit(node2);
- rpcBroker2 = node2.actorOf(RpcBroker.props(brokerSession, probeReg2.getRef(), schemaContext));
-
- }
- @Test
- public void testInvokeRpcError() throws Exception {
- new JavaTestKit(node1) {{
- QName rpc = new QName(new URI("noactor1"), "noactor1");
- CompositeNode input = new ImmutableCompositeNode(QName.create("ns", "2013-12-09", "no child"), new ArrayList<Node<?>>(), ModifyAction.REPLACE);
-
-
- InvokeRpc invokeMsg = new InvokeRpc(rpc, null, input);
- rpcBroker1.tell(invokeMsg, getRef());
- probeReg1.expectMsgClass(RpcRegistry.Messages.FindRouters.class);
- probeReg1.reply(new RpcRegistry.Messages.FindRoutersReply(new ArrayList<Pair<ActorRef, Long>>()));
+import org.opendaylight.mdsal.dom.api.DOMRpcException;
+import org.opendaylight.mdsal.dom.api.DOMRpcImplementationNotAvailableException;
+import org.opendaylight.mdsal.dom.api.DOMRpcResult;
+import org.opendaylight.mdsal.dom.spi.DefaultDOMRpcResult;
+import org.opendaylight.yangtools.util.concurrent.FluentFutures;
+import org.opendaylight.yangtools.yang.data.api.schema.ContainerNode;
+import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
- Boolean getMsg = new ExpectMsg<Boolean>("ErrorResponse") {
- protected Boolean match(Object in) {
- if (in instanceof ErrorResponse) {
- ErrorResponse reply = (ErrorResponse)in;
- return reply.getException().getMessage().contains("No remote actor found for rpc execution of :");
- } else {
- throw noMatch();
- }
- }
- }.get(); // this extracts the received message
+public class RpcBrokerTest extends AbstractRpcTest {
- Assert.assertTrue(getMsg);
+ @Test
+ public void testExecuteRpc() {
+ new TestKit(node1) {
+ {
- }};
- }
+ final ContainerNode invokeRpcResult = makeRPCOutput("bar");
+ final DOMRpcResult rpcResult = new DefaultDOMRpcResult(invokeRpcResult);
+ when(domRpcService1.invokeRpc(eq(TEST_RPC_TYPE), Mockito.<NormalizedNode<?, ?>>any())).thenReturn(
+ FluentFutures.immediateFluentFuture(rpcResult));
+ final ExecuteRpc executeMsg = ExecuteRpc.from(TEST_RPC_ID, null);
- /**
- * This test method invokes and executes the remote rpc
- */
+ rpcInvoker1.tell(executeMsg, getRef());
- @Test
- public void testInvokeRpc() throws URISyntaxException {
- new JavaTestKit(node1) {{
- QName rpc = new QName(new URI("noactor1"), "noactor1");
- // invoke rpc
- CompositeNode input = new ImmutableCompositeNode(QName.create("ns", "2013-12-09", "child1"), new ArrayList<Node<?>>(), ModifyAction.REPLACE);
- InvokeRpc invokeMsg = new InvokeRpc(rpc, null, input);
- rpcBroker1.tell(invokeMsg, getRef());
+ final RpcResponse rpcResponse = expectMsgClass(duration("5 seconds"), RpcResponse.class);
- probeReg1.expectMsgClass(RpcRegistry.Messages.FindRouters.class);
- List<Pair<ActorRef, Long>> routerList = new ArrayList<Pair<ActorRef, Long>>();
+ assertEquals(rpcResult.getResult(), rpcResponse.getResultNormalizedNode());
+ }
+ };
+ }
- routerList.add(new Pair<ActorRef, Long>(rpcBroker2, 200L));
+ @Test
+ public void testExecuteRpcFailureWithException() {
+ new TestKit(node1) {
+ {
+ when(domRpcService1.invokeRpc(eq(TEST_RPC_TYPE), Mockito.<NormalizedNode<?, ?>>any()))
+ .thenReturn(FluentFutures.immediateFailedFluentFuture(
+ new DOMRpcImplementationNotAvailableException("NOT FOUND")));
- probeReg1.reply(new RpcRegistry.Messages.FindRoutersReply(routerList));
+ final ExecuteRpc executeMsg = ExecuteRpc.from(TEST_RPC_ID, null);
- CompositeNode invokeRpcResult = mock(CompositeNode.class);
- Collection<RpcError> errors = new ArrayList<>();
- RpcResult<CompositeNode> result = Rpcs.getRpcResult(true, invokeRpcResult, errors);
- Future<RpcResult<CompositeNode>> rpcResult = Futures.immediateFuture(result);
- when(brokerSession.rpc(rpc, input)).thenReturn(rpcResult);
+ rpcInvoker1.tell(executeMsg, getRef());
- //verify response msg
- Boolean getMsg = new ExpectMsg<Boolean>("RpcResponse") {
- protected Boolean match(Object in) {
- if (in instanceof RpcResponse) {
- return true;
- } else {
- throw noMatch();
- }
- }
- }.get(); // this extracts the received message
+ final Failure rpcResponse = expectMsgClass(duration("5 seconds"), akka.actor.Status.Failure.class);
- Assert.assertTrue(getMsg);
- }};
- }
+ Assert.assertTrue(rpcResponse.cause() instanceof DOMRpcException);
+ }
+ };
+ }
}