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;
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;
28 * UNit tests for SimpleRpcListener.
30 * @author michal.polkorab
32 public class SimpleRpcListenerTest {
34 @Mock Future<Void> future;
40 public void startUp() {
41 MockitoAnnotations.initMocks(this);
45 * Test SimpleRpcListener creation.
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());
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());
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");
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();
82 Assert.assertEquals("Wrong result", 1, listener.getResult().get().getErrors().size());
83 } catch (InterruptedException | ExecutionException e) {
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();
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) {