1 package org.opendaylight.controller.sal.connector.remoterpc;
3 import com.google.common.base.Optional;
4 import junit.framework.Assert;
5 import org.junit.After;
6 import org.junit.Before;
8 import org.opendaylight.controller.sal.common.util.Rpcs;
9 import org.opendaylight.controller.sal.connector.remoterpc.api.RouteChangeListener;
10 import org.opendaylight.controller.sal.connector.remoterpc.api.RoutingTable;
11 import org.opendaylight.controller.sal.connector.remoterpc.api.RoutingTableException;
12 import org.opendaylight.controller.sal.connector.remoterpc.api.SystemException;
13 import org.opendaylight.controller.sal.connector.remoterpc.dto.Message;
14 import org.opendaylight.controller.sal.connector.remoterpc.utils.MessagingUtil;
15 import org.opendaylight.yangtools.yang.common.RpcError;
16 import org.opendaylight.yangtools.yang.common.RpcResult;
17 import org.opendaylight.yangtools.yang.data.api.CompositeNode;
19 import java.io.IOException;
22 import static org.mockito.Matchers.any;
23 import static org.mockito.Mockito.mock;
24 import static org.mockito.Mockito.when;
29 public class ClientImplTest {
30 RoutingTableProvider routingTableProvider;
32 ClientRequestHandler mockHandler;
35 public void setUp() throws Exception {
38 routingTableProvider = mock(RoutingTableProvider.class);
39 RoutingTable<String, String> mockRoutingTable = new MockRoutingTable<String, String>();
40 Optional<RoutingTable<String, String>> optionalRoutingTable = Optional.fromNullable(mockRoutingTable);
41 when(routingTableProvider.getRoutingTable()).thenReturn(optionalRoutingTable);
43 //mock ClientRequestHandler
44 mockHandler = mock(ClientRequestHandler.class);
46 client = new ClientImpl(mockHandler);
47 client.setRoutingTableProvider(routingTableProvider);
52 public void tearDown() throws Exception {
57 public void getRoutingTableProvider_Call_ShouldReturnMockProvider() throws Exception {
58 Assert.assertEquals(routingTableProvider, client.getRoutingTableProvider());
63 public void testStart() throws Exception {
68 public void testStop() throws Exception {
73 public void testClose() throws Exception {
78 public void invokeRpc_NormalCall_ShouldReturnSuccess() throws Exception {
80 when(mockHandler.handle(any(Message.class))).
81 thenReturn(MessagingUtil.createEmptyMessage());
83 RpcResult<CompositeNode> result = client.invokeRpc(null, null);
85 Assert.assertTrue(result.isSuccessful());
86 Assert.assertTrue(result.getErrors().isEmpty());
87 Assert.assertNull(result.getResult());
91 public void invokeRpc_HandlerThrowsException_ShouldReturnError() throws Exception {
93 when(mockHandler.handle(any(Message.class))).
94 thenThrow(new IOException());
96 RpcResult<CompositeNode> result = client.invokeRpc(null, null);
98 Assert.assertFalse(result.isSuccessful());
99 Assert.assertFalse(result.getErrors().isEmpty());
100 Assert.assertNull(result.getResult());