2 * Copyright (c) 2014 Pantheon Technologies s.r.o. and others. All rights reserved.
\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
9 package org.opendaylight.openflowjava.protocol.impl.core.connection;
\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
17 import java.util.Collections;
\r
18 import java.util.concurrent.ExecutionException;
\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
30 import com.google.common.util.concurrent.SettableFuture;
\r
33 * @author michal.polkorab
\r
36 public class SimpleRpcListenerTest {
\r
38 @Mock Future<Void> future;
\r
44 public void startUp() {
\r
45 MockitoAnnotations.initMocks(this);
\r
49 * Test SimpleRpcListener creation
\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
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
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
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
86 Assert.assertEquals("Wrong result", 1, listener.getResult().get().getErrors().size());
\r
87 } catch (InterruptedException | ExecutionException e) {
\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
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