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