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