Change RpcImplementation contract to asynchronous
[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 static org.mockito.Matchers.any;
11 import static org.mockito.Mockito.mock;
12 import static org.mockito.Mockito.when;
13
14 import java.io.IOException;
15
16 import junit.framework.Assert;
17
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;
27
28 import com.google.common.base.Optional;
29
30 /**
31  *
32  */
33 public class ClientImplTest {
34   RoutingTableProvider routingTableProvider;
35   ClientImpl client;
36   ClientRequestHandler mockHandler;
37
38   @Before
39   public void setUp() throws Exception {
40
41     //mock routing table
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);
46
47     //mock ClientRequestHandler
48     mockHandler = mock(ClientRequestHandler.class);
49
50     client = new ClientImpl(mockHandler);
51     client.setRoutingTableProvider(routingTableProvider);
52
53   }
54
55   @After
56   public void tearDown() throws Exception {
57
58   }
59
60   @Test
61   public void getRoutingTableProvider_Call_ShouldReturnMockProvider() throws Exception {
62     Assert.assertEquals(routingTableProvider, client.getRoutingTableProvider());
63
64   }
65
66   @Test
67   public void testStart() throws Exception {
68
69   }
70
71   @Test
72   public void testStop() throws Exception {
73
74   }
75
76   @Test
77   public void testClose() throws Exception {
78
79   }
80
81   //@Test
82   public void invokeRpc_NormalCall_ShouldReturnSuccess() throws Exception {
83
84     when(mockHandler.handle(any(Message.class))).
85             thenReturn(MessagingUtil.createEmptyMessage());
86
87     RpcResult<CompositeNode> result = client.invokeRpc(null, null).get();
88
89     Assert.assertTrue(result.isSuccessful());
90     Assert.assertTrue(result.getErrors().isEmpty());
91     Assert.assertNull(result.getResult());
92   }
93
94   //@Test
95   public void invokeRpc_HandlerThrowsException_ShouldReturnError() throws Exception {
96
97     when(mockHandler.handle(any(Message.class))).
98             thenThrow(new IOException());
99
100     RpcResult<CompositeNode> result = client.invokeRpc(null, null).get();
101
102     Assert.assertFalse(result.isSuccessful());
103     Assert.assertFalse(result.getErrors().isEmpty());
104     Assert.assertNull(result.getResult());
105   }
106
107 }