Merge "Created Network Service Functions Features"
[controller.git] / opendaylight / md-sal / sal-remoterpc-connector / src / test / java / org / opendaylight / controller / remote / rpc / RpcBrokerTest.java
1 /*
2  * Copyright (c) 2014 Cisco 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
12 import akka.actor.ActorRef;
13 import akka.actor.ActorSystem;
14 import akka.testkit.JavaTestKit;
15 import com.google.common.util.concurrent.Futures;
16 import junit.framework.Assert;
17 import org.junit.AfterClass;
18 import org.junit.BeforeClass;
19 import org.junit.Test;
20 import org.mockito.Mockito;
21 import org.opendaylight.controller.remote.rpc.messages.AddRoutedRpc;
22 import org.opendaylight.controller.remote.rpc.messages.AddRpc;
23 import org.opendaylight.controller.remote.rpc.messages.ErrorResponse;
24 import org.opendaylight.controller.remote.rpc.messages.InvokeRoutedRpc;
25 import org.opendaylight.controller.remote.rpc.messages.InvokeRpc;
26 import org.opendaylight.controller.remote.rpc.messages.RpcResponse;
27 import org.opendaylight.controller.remote.rpc.registry.ClusterWrapper;
28 import org.opendaylight.controller.remote.rpc.registry.RpcRegistry;
29 import org.opendaylight.controller.sal.common.util.Rpcs;
30 import org.opendaylight.controller.sal.connector.api.RpcRouter;
31 import org.opendaylight.controller.sal.core.api.Broker;
32 import org.opendaylight.yangtools.yang.common.QName;
33 import org.opendaylight.yangtools.yang.common.RpcError;
34 import org.opendaylight.yangtools.yang.common.RpcResult;
35 import org.opendaylight.yangtools.yang.data.api.CompositeNode;
36 import org.opendaylight.yangtools.yang.data.api.ModifyAction;
37 import org.opendaylight.yangtools.yang.data.api.Node;
38 import org.opendaylight.yangtools.yang.data.impl.ImmutableCompositeNode;
39 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
40
41 import java.net.URI;
42 import java.net.URISyntaxException;
43 import java.util.ArrayList;
44 import java.util.Collection;
45 import java.util.HashSet;
46 import java.util.Set;
47 import java.util.concurrent.Future;
48
49 import static org.mockito.Mockito.mock;
50 import static org.mockito.Mockito.when;
51
52 public class RpcBrokerTest {
53
54   static ActorSystem system;
55
56
57   @BeforeClass
58   public static void setup() {
59     system = ActorSystem.create();
60   }
61
62   @AfterClass
63   public static void teardown() {
64     JavaTestKit.shutdownActorSystem(system);
65     system = null;
66   }
67
68   @Test
69   public void testInvokeRpcError() throws URISyntaxException {
70     new JavaTestKit(system) {{
71       ActorRef rpcRegistry = system.actorOf(RpcRegistry.props(Mockito.mock(ClusterWrapper.class)));
72       Broker.ProviderSession brokerSession = Mockito.mock(Broker.ProviderSession.class);
73       SchemaContext schemaContext = mock(SchemaContext.class);
74       ActorRef rpcBroker = system.actorOf(RpcBroker.props(brokerSession, rpcRegistry, schemaContext));
75       QName rpc = new QName(new URI("actor1"), "actor1");
76       InvokeRpc invokeMsg = new InvokeRpc(rpc, null);
77       rpcBroker.tell(invokeMsg, getRef());
78
79       Boolean getMsg = new ExpectMsg<Boolean>("ErrorResponse") {
80         protected Boolean match(Object in) {
81           if (in instanceof ErrorResponse) {
82             ErrorResponse reply = (ErrorResponse)in;
83             return "No remote actor found for rpc execution.".equals(reply.getException().getMessage());
84           } else {
85             throw noMatch();
86           }
87         }
88       }.get(); // this extracts the received message
89
90       Assert.assertTrue(getMsg);
91     }};
92   }
93
94   /**
95    * This test method invokes and executes the remote rpc
96    */
97
98   @Test
99   public void testInvokeRpc() throws URISyntaxException {
100     new JavaTestKit(system) {{
101       ActorRef rpcRegistry = system.actorOf(RpcRegistry.props(mock(ClusterWrapper.class)));
102       Broker.ProviderSession brokerSession = mock(Broker.ProviderSession.class);
103       SchemaContext schemaContext = mock(SchemaContext.class);
104       ActorRef rpcBroker = system.actorOf(RpcBroker.props(brokerSession, rpcRegistry, schemaContext));
105       ActorRef rpcBrokerRemote = system.actorOf(RpcBroker.props(brokerSession, rpcRegistry, schemaContext), "actor1");
106       // Add RPC in table
107       QName rpc = new QName(new URI("actor1"), "actor1");
108       RouteIdentifierImpl routeId = new RouteIdentifierImpl(null, rpc, null);
109       final String route = rpcBrokerRemote.path().toString();
110       AddRpc rpcMsg = new AddRpc(routeId, route);
111       rpcRegistry.tell(rpcMsg, getRef());
112       expectMsgEquals(duration("2 second"), "Success");
113
114       // invoke rpc
115       CompositeNode input = new ImmutableCompositeNode(QName.create("ns", "2013-12-09", "child1"), new ArrayList<Node<?>>(), ModifyAction.REPLACE);
116       CompositeNode invokeRpcResult = mock(CompositeNode.class);
117       Collection<RpcError> errors = new ArrayList<>();
118       RpcResult<CompositeNode> result = Rpcs.getRpcResult(true, invokeRpcResult, errors);
119       Future<RpcResult<CompositeNode>> rpcResult = Futures.immediateFuture(result);
120       when(brokerSession.rpc(rpc, input)).thenReturn(rpcResult);
121       InvokeRpc invokeMsg = new InvokeRpc(rpc, input);
122       rpcBroker.tell(invokeMsg, getRef());
123
124       //verify response msg
125       Boolean getMsg = new ExpectMsg<Boolean>("RpcResponse") {
126         protected Boolean match(Object in) {
127           if (in instanceof RpcResponse) {
128             return true;
129           } else {
130             throw noMatch();
131           }
132         }
133       }.get(); // this extracts the received message
134
135       Assert.assertTrue(getMsg);
136     }};
137   }
138
139   @Test
140   public void testInvokeRoutedRpcError() throws URISyntaxException {
141     new JavaTestKit(system) {{
142       ActorRef rpcRegistry = system.actorOf(RpcRegistry.props(Mockito.mock(ClusterWrapper.class)));
143       Broker.ProviderSession brokerSession = Mockito.mock(Broker.ProviderSession.class);
144       SchemaContext schemaContext = mock(SchemaContext.class);
145       ActorRef rpcBroker = system.actorOf(RpcBroker.props(brokerSession, rpcRegistry, schemaContext));
146       QName rpc = new QName(new URI("actor1"), "actor1");
147       InvokeRoutedRpc invokeMsg = new InvokeRoutedRpc(rpc, null);
148       rpcBroker.tell(invokeMsg, getRef());
149
150       Boolean getMsg = new ExpectMsg<Boolean>("ErrorResponse") {
151         protected Boolean match(Object in) {
152           if (in instanceof ErrorResponse) {
153             ErrorResponse reply = (ErrorResponse)in;
154             return "No remote actor found for rpc execution.".equals(reply.getException().getMessage());
155           } else {
156             throw noMatch();
157           }
158         }
159       }.get(); // this extracts the received message
160
161       Assert.assertTrue(getMsg);
162     }};
163   }
164
165   /**
166    * This test method invokes and executes the remote routed rpc
167    */
168
169   @Test
170   public void testInvokeRoutedRpc() throws URISyntaxException {
171     new JavaTestKit(system) {{
172       ActorRef rpcRegistry = system.actorOf(RpcRegistry.props(mock(ClusterWrapper.class)));
173       Broker.ProviderSession brokerSession = mock(Broker.ProviderSession.class);
174       SchemaContext schemaContext = mock(SchemaContext.class);
175       ActorRef rpcBroker = system.actorOf(RpcBroker.props(brokerSession, rpcRegistry, schemaContext));
176       ActorRef rpcBrokerRemote = system.actorOf(RpcBroker.props(brokerSession, rpcRegistry, schemaContext), "actor2");
177       // Add Routed RPC in table
178       QName rpc = new QName(new URI("actor2"), "actor2");
179       RouteIdentifierImpl routeId = new RouteIdentifierImpl(null, rpc, null);
180       final String route = rpcBrokerRemote.path().toString();
181       Set<RpcRouter.RouteIdentifier<?, ?, ?>> routeIds = new HashSet<>();
182       routeIds.add(routeId);
183
184       AddRoutedRpc rpcMsg = new AddRoutedRpc(routeIds, route);
185       rpcRegistry.tell(rpcMsg, getRef());
186       expectMsgEquals(duration("2 second"), "Success");
187
188       // invoke rpc
189       CompositeNode input = new ImmutableCompositeNode(QName.create("ns", "2013-12-09", "child1"), new ArrayList<Node<?>>(), ModifyAction.REPLACE);
190       CompositeNode invokeRpcResult = mock(CompositeNode.class);
191       Collection<RpcError> errors = new ArrayList<>();
192       RpcResult<CompositeNode> result = Rpcs.getRpcResult(true, invokeRpcResult, errors);
193       Future<RpcResult<CompositeNode>> rpcResult = Futures.immediateFuture(result);
194       when(brokerSession.rpc(rpc, input)).thenReturn(rpcResult);
195       InvokeRoutedRpc invokeMsg = new InvokeRoutedRpc(rpc, input);
196       rpcBroker.tell(invokeMsg, getRef());
197
198       //verify response msg
199       Boolean getMsg = new ExpectMsg<Boolean>("RpcResponse") {
200         protected Boolean match(Object in) {
201           if (in instanceof RpcResponse) {
202             return true;
203           } else {
204             throw noMatch();
205           }
206         }
207       }.get(); // this extracts the received message
208
209       Assert.assertTrue(getMsg);
210     }};
211   }
212
213 }