d5a7499c9306228f7943ef0ffa44a8f1e7ce1e10
[openflowjava.git] / openflow-protocol-impl / src / test / java / org / opendaylight / openflowjava / protocol / impl / core / 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.core.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.openflowjava.protocol.impl.core.connection.SimpleRpcListener;\r
27 import org.opendaylight.yangtools.yang.common.RpcError;\r
28 import org.opendaylight.yangtools.yang.common.RpcResult;\r
29 \r
30 import com.google.common.util.concurrent.SettableFuture;\r
31 \r
32 /**\r
33  * @author michal.polkorab\r
34  *\r
35  */\r
36 public class SimpleRpcListenerTest {\r
37 \r
38     @Mock Future<Void> future;\r
39 \r
40     /**\r
41      * Initializes mocks\r
42      */\r
43     @Before\r
44     public void startUp() {\r
45         MockitoAnnotations.initMocks(this);\r
46     }\r
47 \r
48     /**\r
49      * Test SimpleRpcListener creation\r
50      */\r
51     @Test\r
52     public void test() {\r
53         SimpleRpcListener listener = new SimpleRpcListener("MESSAGE", "Failed to send message");\r
54         Assert.assertEquals("Wrong message", "MESSAGE", listener.takeMessage());\r
55         Assert.assertEquals("Wrong message", listener, listener.takeListener());\r
56     }\r
57 \r
58     /**\r
59      * Test rpc success\r
60      */\r
61     @Test\r
62     public void testSuccessfulRpc() {\r
63         SimpleRpcListener listener = new SimpleRpcListener("MESSAGE", "Failed to send message");\r
64         listener.operationSuccessful();\r
65         SettableFuture<RpcResult<?>> result = SettableFuture.create();\r
66         result.set(Rpcs.getRpcResult(true, null, Collections.<RpcError>emptyList()));\r
67         try {\r
68             Assert.assertEquals("Wrong result", result.get().getErrors(), listener.getResult().get().getErrors());\r
69             Assert.assertEquals("Wrong result", result.get().getResult(), listener.getResult().get().getResult());\r
70             Assert.assertEquals("Wrong result", result.get().isSuccessful(), listener.getResult().get().isSuccessful());\r
71         } catch (InterruptedException | ExecutionException e) {\r
72             fail("Problem accessing result");\r
73         }\r
74     }\r
75 \r
76     /**\r
77      * Test rpc success\r
78      */\r
79     @Test\r
80     public void testOperationComplete() {\r
81         when(future.isSuccess()).thenReturn(false);\r
82         SimpleRpcListener listener = new SimpleRpcListener("MESSAGE", "Failed to send message");\r
83         listener.operationComplete(future);\r
84         verify(future, times(1)).cause();\r
85         try {\r
86             Assert.assertEquals("Wrong result", 1, listener.getResult().get().getErrors().size());\r
87         } catch (InterruptedException | ExecutionException e) {\r
88             Assert.fail();\r
89         }\r
90     }\r
91 \r
92     /**\r
93      * Test rpc success\r
94      */\r
95     @Test\r
96     public void testOperationComplete2() {\r
97         when(future.isSuccess()).thenReturn(true);\r
98         SimpleRpcListener listener = new SimpleRpcListener("MESSAGE", "Failed to send message");\r
99         listener.operationComplete(future);\r
100         verify(future, times(0)).cause();\r
101         try {\r
102             Assert.assertEquals("Wrong result", 0, listener.getResult().get().getErrors().size());\r
103             Assert.assertEquals("Wrong result", true, listener.getResult().get().isSuccessful());\r
104         } catch (InterruptedException | ExecutionException e) {\r
105             Assert.fail();\r
106         }\r
107     }\r
108 }