2 * Copyright (c) 2014 Cisco Systems, Inc. and others. All rights reserved.
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
8 package org.opendaylight.controller.sal.connector.remoterpc;
10 import com.google.common.base.Optional;
11 import junit.framework.Assert;
12 import org.junit.After;
13 import org.junit.Before;
14 import org.junit.Test;
15 import org.opendaylight.controller.sal.common.util.Rpcs;
16 import org.opendaylight.controller.sal.connector.api.RpcRouter;
17 import org.opendaylight.controller.sal.connector.remoterpc.api.RouteChangeListener;
18 import org.opendaylight.controller.sal.connector.remoterpc.api.RoutingTable;
19 import org.opendaylight.controller.sal.connector.remoterpc.api.RoutingTableException;
20 import org.opendaylight.controller.sal.connector.remoterpc.api.SystemException;
21 import org.opendaylight.controller.sal.connector.remoterpc.dto.Message;
22 import org.opendaylight.controller.sal.connector.remoterpc.utils.MessagingUtil;
23 import org.opendaylight.yangtools.yang.common.RpcError;
24 import org.opendaylight.yangtools.yang.common.RpcResult;
25 import org.opendaylight.yangtools.yang.data.api.CompositeNode;
27 import java.io.IOException;
30 import static org.mockito.Matchers.any;
31 import static org.mockito.Mockito.mock;
32 import static org.mockito.Mockito.when;
37 public class ClientImplTest {
38 RoutingTableProvider routingTableProvider;
40 ClientRequestHandler mockHandler;
43 public void setUp() throws Exception {
46 routingTableProvider = mock(RoutingTableProvider.class);
47 RoutingTable<RpcRouter.RouteIdentifier, String> mockRoutingTable = new MockRoutingTable<String, String>();
48 Optional<RoutingTable<RpcRouter.RouteIdentifier, String>> optionalRoutingTable = Optional.fromNullable(mockRoutingTable);
49 when(routingTableProvider.getRoutingTable()).thenReturn(optionalRoutingTable);
51 //mock ClientRequestHandler
52 mockHandler = mock(ClientRequestHandler.class);
54 client = new ClientImpl(mockHandler);
55 client.setRoutingTableProvider(routingTableProvider);
60 public void tearDown() throws Exception {
65 public void getRoutingTableProvider_Call_ShouldReturnMockProvider() throws Exception {
66 Assert.assertEquals(routingTableProvider, client.getRoutingTableProvider());
71 public void testStart() throws Exception {
76 public void testStop() throws Exception {
81 public void testClose() throws Exception {
86 public void invokeRpc_NormalCall_ShouldReturnSuccess() throws Exception {
88 when(mockHandler.handle(any(Message.class))).
89 thenReturn(MessagingUtil.createEmptyMessage());
91 RpcResult<CompositeNode> result = client.invokeRpc(null, null);
93 Assert.assertTrue(result.isSuccessful());
94 Assert.assertTrue(result.getErrors().isEmpty());
95 Assert.assertNull(result.getResult());
99 public void invokeRpc_HandlerThrowsException_ShouldReturnError() throws Exception {
101 when(mockHandler.handle(any(Message.class))).
102 thenThrow(new IOException());
104 RpcResult<CompositeNode> result = client.invokeRpc(null, null);
106 Assert.assertFalse(result.isSuccessful());
107 Assert.assertFalse(result.getErrors().isEmpty());
108 Assert.assertNull(result.getResult());