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 static org.mockito.Matchers.any;
11 import static org.mockito.Mockito.mock;
12 import static org.mockito.Mockito.when;
14 import java.io.IOException;
16 import junit.framework.Assert;
18 import org.junit.After;
19 import org.junit.Before;
20 import org.junit.Test;
21 import org.opendaylight.controller.sal.connector.api.RpcRouter;
22 import org.opendaylight.controller.sal.connector.remoterpc.api.RoutingTable;
23 import org.opendaylight.controller.sal.connector.remoterpc.dto.Message;
24 import org.opendaylight.controller.sal.connector.remoterpc.utils.MessagingUtil;
25 import org.opendaylight.yangtools.yang.common.RpcResult;
26 import org.opendaylight.yangtools.yang.data.api.CompositeNode;
28 import com.google.common.base.Optional;
33 public class ClientImplTest {
34 RoutingTableProvider routingTableProvider;
36 ClientRequestHandler mockHandler;
39 public void setUp() throws Exception {
42 routingTableProvider = mock(RoutingTableProvider.class);
43 RoutingTable<RpcRouter.RouteIdentifier<?, ?, ?>, String> mockRoutingTable = new MockRoutingTable<String, String>();
44 Optional<RoutingTable<RpcRouter.RouteIdentifier<?, ?, ?>, String>> optionalRoutingTable = Optional.fromNullable(mockRoutingTable);
45 when(routingTableProvider.getRoutingTable()).thenReturn(optionalRoutingTable);
47 //mock ClientRequestHandler
48 mockHandler = mock(ClientRequestHandler.class);
50 client = new ClientImpl(mockHandler);
51 client.setRoutingTableProvider(routingTableProvider);
56 public void tearDown() throws Exception {
61 public void getRoutingTableProvider_Call_ShouldReturnMockProvider() throws Exception {
62 Assert.assertEquals(routingTableProvider, client.getRoutingTableProvider());
67 public void testStart() throws Exception {
72 public void testStop() throws Exception {
77 public void testClose() throws Exception {
82 public void invokeRpc_NormalCall_ShouldReturnSuccess() throws Exception {
84 when(mockHandler.handle(any(Message.class))).
85 thenReturn(MessagingUtil.createEmptyMessage());
87 RpcResult<CompositeNode> result = client.invokeRpc(null, null).get();
89 Assert.assertTrue(result.isSuccessful());
90 Assert.assertTrue(result.getErrors().isEmpty());
91 Assert.assertNull(result.getResult());
95 public void invokeRpc_HandlerThrowsException_ShouldReturnError() throws Exception {
97 when(mockHandler.handle(any(Message.class))).
98 thenThrow(new IOException());
100 RpcResult<CompositeNode> result = client.invokeRpc(null, null).get();
102 Assert.assertFalse(result.isSuccessful());
103 Assert.assertFalse(result.getErrors().isEmpty());
104 Assert.assertNull(result.getResult());