Add method to register listener for unknown msg
[openflowjava.git] / openflow-protocol-impl / src / test / java / org / opendaylight / openflowjava / protocol / impl / core / connection / SimpleRpcListenerTest.java
1 /*
2  * Copyright (c) 2014 Pantheon Technologies s.r.o. 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
9 package org.opendaylight.openflowjava.protocol.impl.core.connection;
10
11 import static org.junit.Assert.fail;
12 import static org.mockito.Mockito.times;
13 import static org.mockito.Mockito.verify;
14 import static org.mockito.Mockito.when;
15 import com.google.common.util.concurrent.SettableFuture;
16 import io.netty.util.concurrent.Future;
17 import java.util.concurrent.ExecutionException;
18 import org.junit.Assert;
19 import org.junit.Before;
20 import org.junit.Test;
21 import org.mockito.Mock;
22 import org.mockito.MockitoAnnotations;
23 import org.opendaylight.yangtools.yang.common.RpcResult;
24 import org.opendaylight.yangtools.yang.common.RpcResultBuilder;
25
26 /**
27  * @author michal.polkorab
28  *
29  */
30 public class SimpleRpcListenerTest {
31
32     @Mock Future<Void> future;
33
34     /**
35      * Initializes mocks
36      */
37     @Before
38     public void startUp() {
39         MockitoAnnotations.initMocks(this);
40     }
41
42     /**
43      * Test SimpleRpcListener creation
44      */
45     @Test
46     public void test() {
47         SimpleRpcListener listener = new SimpleRpcListener("MESSAGE", "Failed to send message");
48         Assert.assertEquals("Wrong message", "MESSAGE", listener.takeMessage());
49         Assert.assertEquals("Wrong message", listener, listener.takeListener());
50     }
51
52     /**
53      * Test rpc success
54      */
55     @Test
56     public void testSuccessfulRpc() {
57         SimpleRpcListener listener = new SimpleRpcListener("MESSAGE", "Failed to send message");
58         listener.operationSuccessful();
59         SettableFuture<RpcResult<?>> result = SettableFuture.create();
60         result.set(RpcResultBuilder.success((Void)null).build());
61         try {
62             Assert.assertEquals("Wrong result", result.get().getErrors(), listener.getResult().get().getErrors());
63             Assert.assertEquals("Wrong result", result.get().getResult(), listener.getResult().get().getResult());
64             Assert.assertEquals("Wrong result", result.get().isSuccessful(), listener.getResult().get().isSuccessful());
65         } catch (InterruptedException | ExecutionException e) {
66             fail("Problem accessing result");
67         }
68     }
69
70     /**
71      * Test rpc success
72      */
73     @Test
74     public void testOperationComplete() {
75         when(future.isSuccess()).thenReturn(false);
76         SimpleRpcListener listener = new SimpleRpcListener("MESSAGE", "Failed to send message");
77         listener.operationComplete(future);
78         verify(future, times(1)).cause();
79         try {
80             Assert.assertEquals("Wrong result", 1, listener.getResult().get().getErrors().size());
81         } catch (InterruptedException | ExecutionException e) {
82             Assert.fail();
83         }
84     }
85
86     /**
87      * Test rpc success
88      */
89     @Test
90     public void testOperationComplete2() {
91         when(future.isSuccess()).thenReturn(true);
92         SimpleRpcListener listener = new SimpleRpcListener("MESSAGE", "Failed to send message");
93         listener.operationComplete(future);
94         verify(future, times(0)).cause();
95         try {
96             Assert.assertEquals("Wrong result", 0, listener.getResult().get().getErrors().size());
97             Assert.assertEquals("Wrong result", true, listener.getResult().get().isSuccessful());
98         } catch (InterruptedException | ExecutionException e) {
99             Assert.fail();
100         }
101     }
102 }