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