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.remoterpc.api.RouteChangeListener;
17 import org.opendaylight.controller.sal.connector.remoterpc.api.RoutingTable;
18 import org.opendaylight.controller.sal.connector.remoterpc.api.RoutingTableException;
19 import org.opendaylight.controller.sal.connector.remoterpc.api.SystemException;
20 import org.opendaylight.controller.sal.connector.remoterpc.dto.Message;
21 import org.opendaylight.controller.sal.connector.remoterpc.utils.MessagingUtil;
22 import org.opendaylight.yangtools.yang.common.RpcError;
23 import org.opendaylight.yangtools.yang.common.RpcResult;
24 import org.opendaylight.yangtools.yang.data.api.CompositeNode;
26 import java.io.IOException;
29 import static org.mockito.Matchers.any;
30 import static org.mockito.Mockito.mock;
31 import static org.mockito.Mockito.when;
36 public class ClientImplTest {
37 RoutingTableProvider routingTableProvider;
39 ClientRequestHandler mockHandler;
42 public void setUp() throws Exception {
45 routingTableProvider = mock(RoutingTableProvider.class);
46 RoutingTable<String, String> mockRoutingTable = new MockRoutingTable<String, String>();
47 Optional<RoutingTable<String, String>> optionalRoutingTable = Optional.fromNullable(mockRoutingTable);
48 when(routingTableProvider.getRoutingTable()).thenReturn(optionalRoutingTable);
50 //mock ClientRequestHandler
51 mockHandler = mock(ClientRequestHandler.class);
53 client = new ClientImpl(mockHandler);
54 client.setRoutingTableProvider(routingTableProvider);
59 public void tearDown() throws Exception {
64 public void getRoutingTableProvider_Call_ShouldReturnMockProvider() throws Exception {
65 Assert.assertEquals(routingTableProvider, client.getRoutingTableProvider());
70 public void testStart() throws Exception {
75 public void testStop() throws Exception {
80 public void testClose() throws Exception {
85 public void invokeRpc_NormalCall_ShouldReturnSuccess() throws Exception {
87 when(mockHandler.handle(any(Message.class))).
88 thenReturn(MessagingUtil.createEmptyMessage());
90 RpcResult<CompositeNode> result = client.invokeRpc(null, null);
92 Assert.assertTrue(result.isSuccessful());
93 Assert.assertTrue(result.getErrors().isEmpty());
94 Assert.assertNull(result.getResult());
98 public void invokeRpc_HandlerThrowsException_ShouldReturnError() throws Exception {
100 when(mockHandler.handle(any(Message.class))).
101 thenThrow(new IOException());
103 RpcResult<CompositeNode> result = client.invokeRpc(null, null);
105 Assert.assertFalse(result.isSuccessful());
106 Assert.assertFalse(result.getErrors().isEmpty());
107 Assert.assertNull(result.getResult());