Merge "Exception for URI /restconf/operations/module_name:rpc ended with slash"
[controller.git] / opendaylight / md-sal / sal-remoterpc-connector / implementation / src / test / java / org / opendaylight / controller / sal / connector / remoterpc / ClientImplTest.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 package org.opendaylight.controller.sal.connector.remoterpc;
9
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;
26
27 import java.io.IOException;
28 import java.util.*;
29
30 import static org.mockito.Matchers.any;
31 import static org.mockito.Mockito.mock;
32 import static org.mockito.Mockito.when;
33
34 /**
35  *
36  */
37 public class ClientImplTest {
38   RoutingTableProvider routingTableProvider;
39   ClientImpl client;
40   ClientRequestHandler mockHandler;
41
42   @Before
43   public void setUp() throws Exception {
44
45     //mock routing table
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);
50
51     //mock ClientRequestHandler
52     mockHandler = mock(ClientRequestHandler.class);
53
54     client = new ClientImpl(mockHandler);
55     client.setRoutingTableProvider(routingTableProvider);
56
57   }
58
59   @After
60   public void tearDown() throws Exception {
61
62   }
63
64   @Test
65   public void getRoutingTableProvider_Call_ShouldReturnMockProvider() throws Exception {
66     Assert.assertEquals(routingTableProvider, client.getRoutingTableProvider());
67
68   }
69
70   @Test
71   public void testStart() throws Exception {
72
73   }
74
75   @Test
76   public void testStop() throws Exception {
77
78   }
79
80   @Test
81   public void testClose() throws Exception {
82
83   }
84
85   //@Test
86   public void invokeRpc_NormalCall_ShouldReturnSuccess() throws Exception {
87
88     when(mockHandler.handle(any(Message.class))).
89             thenReturn(MessagingUtil.createEmptyMessage());
90
91     RpcResult<CompositeNode> result = client.invokeRpc(null, null);
92
93     Assert.assertTrue(result.isSuccessful());
94     Assert.assertTrue(result.getErrors().isEmpty());
95     Assert.assertNull(result.getResult());
96   }
97
98   //@Test
99   public void invokeRpc_HandlerThrowsException_ShouldReturnError() throws Exception {
100
101     when(mockHandler.handle(any(Message.class))).
102             thenThrow(new IOException());
103
104     RpcResult<CompositeNode> result = client.invokeRpc(null, null);
105
106     Assert.assertFalse(result.isSuccessful());
107     Assert.assertFalse(result.getErrors().isEmpty());
108     Assert.assertNull(result.getResult());
109   }
110
111 }