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