Improved unit test coverage (covered some untested branches)
[openflowjava.git] / openflow-protocol-impl / src / test / java / org / opendaylight / openflowjava / protocol / impl / connection / SimpleRpcListenerTest.java
1 /*\r
2  * Copyright (c) 2014 Pantheon Technologies s.r.o. and others. All rights reserved.\r
3  *\r
4  * This program and the accompanying materials are made available under the\r
5  * terms of the Eclipse Public License v1.0 which accompanies this distribution,\r
6  * and is available at http://www.eclipse.org/legal/epl-v10.html\r
7  */\r
8 \r
9 package org.opendaylight.openflowjava.protocol.impl.connection;\r
10 \r
11 import static org.junit.Assert.fail;\r
12 import static org.mockito.Mockito.times;\r
13 import static org.mockito.Mockito.verify;\r
14 import static org.mockito.Mockito.when;\r
15 import io.netty.util.concurrent.Future;\r
16 \r
17 import java.util.Collections;\r
18 import java.util.concurrent.ExecutionException;\r
19 \r
20 import org.junit.Assert;\r
21 import org.junit.Before;\r
22 import org.junit.Test;\r
23 import org.mockito.Mock;\r
24 import org.mockito.MockitoAnnotations;\r
25 import org.opendaylight.controller.sal.common.util.Rpcs;\r
26 import org.opendaylight.yangtools.yang.common.RpcError;\r
27 import org.opendaylight.yangtools.yang.common.RpcResult;\r
28 \r
29 import com.google.common.util.concurrent.SettableFuture;\r
30 \r
31 /**\r
32  * @author michal.polkorab\r
33  *\r
34  */\r
35 public class SimpleRpcListenerTest {\r
36 \r
37     @Mock Future<Void> future;\r
38 \r
39     /**\r
40      * Initializes mocks\r
41      */\r
42     @Before\r
43     public void startUp() {\r
44         MockitoAnnotations.initMocks(this);\r
45     }\r
46 \r
47     /**\r
48      * Test SimpleRpcListener creation\r
49      */\r
50     @Test\r
51     public void test() {\r
52         SimpleRpcListener listener = new SimpleRpcListener("MESSAGE", "Failed to send message");\r
53         Assert.assertEquals("Wrong message", "MESSAGE", listener.takeMessage());\r
54         Assert.assertEquals("Wrong message", listener, listener.takeListener());\r
55     }\r
56 \r
57     /**\r
58      * Test rpc success\r
59      */\r
60     @Test\r
61     public void testSuccessfulRpc() {\r
62         SimpleRpcListener listener = new SimpleRpcListener("MESSAGE", "Failed to send message");\r
63         listener.operationSuccessful();\r
64         SettableFuture<RpcResult<?>> result = SettableFuture.create();\r
65         result.set(Rpcs.getRpcResult(true, null, Collections.<RpcError>emptyList()));\r
66         try {\r
67             Assert.assertEquals("Wrong result", result.get().getErrors(), listener.getResult().get().getErrors());\r
68             Assert.assertEquals("Wrong result", result.get().getResult(), listener.getResult().get().getResult());\r
69             Assert.assertEquals("Wrong result", result.get().isSuccessful(), listener.getResult().get().isSuccessful());\r
70         } catch (InterruptedException | ExecutionException e) {\r
71             fail("Problem accessing result");\r
72         }\r
73     }\r
74 \r
75     /**\r
76      * Test rpc success\r
77      */\r
78     @Test\r
79     public void testOperationComplete() {\r
80         when(future.isSuccess()).thenReturn(false);\r
81         SimpleRpcListener listener = new SimpleRpcListener("MESSAGE", "Failed to send message");\r
82         listener.operationComplete(future);\r
83         verify(future, times(1)).cause();\r
84         try {\r
85             Assert.assertEquals("Wrong result", 1, listener.getResult().get().getErrors().size());\r
86         } catch (InterruptedException | ExecutionException e) {\r
87             Assert.fail();\r
88         }\r
89     }\r
90 \r
91     /**\r
92      * Test rpc success\r
93      */\r
94     @Test\r
95     public void testOperationComplete2() {\r
96         when(future.isSuccess()).thenReturn(true);\r
97         SimpleRpcListener listener = new SimpleRpcListener("MESSAGE", "Failed to send message");\r
98         listener.operationComplete(future);\r
99         verify(future, times(0)).cause();\r
100         try {\r
101             Assert.assertEquals("Wrong result", 0, listener.getResult().get().getErrors().size());\r
102             Assert.assertEquals("Wrong result", true, listener.getResult().get().isSuccessful());\r
103         } catch (InterruptedException | ExecutionException e) {\r
104             Assert.fail();\r
105         }\r
106     }\r
107 }