2 * Copyright (c) 2014 Cisco Systems, Inc. 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
8 package org.opendaylight.netconf.client;
10 import static org.junit.Assert.assertFalse;
11 import static org.junit.Assert.assertTrue;
12 import static org.mockito.ArgumentMatchers.any;
13 import static org.mockito.Mockito.doAnswer;
14 import static org.mockito.Mockito.doReturn;
15 import static org.mockito.Mockito.mock;
16 import static org.mockito.Mockito.times;
17 import static org.mockito.Mockito.verify;
19 import io.netty.channel.Channel;
20 import io.netty.channel.ChannelPromise;
21 import io.netty.channel.EventLoop;
22 import io.netty.util.concurrent.Future;
23 import io.netty.util.concurrent.GenericFutureListener;
25 import org.junit.Before;
26 import org.junit.Test;
27 import org.mockito.internal.util.collections.Sets;
28 import org.opendaylight.netconf.api.NetconfDocumentedException;
29 import org.opendaylight.netconf.api.NetconfMessage;
30 import org.opendaylight.netconf.api.messages.NetconfHelloMessage;
32 public class SimpleNetconfClientSessionListenerTest {
34 private Channel channel;
35 private ChannelPromise channelFuture;
37 private NetconfHelloMessage helloMessage;
38 private NetconfMessage message;
39 private NetconfClientSessionListener sessionListener;
40 private NetconfClientSession clientSession;
43 public void setUp() throws NetconfDocumentedException {
44 channel = mock(Channel.class);
45 channelFuture = mock(ChannelPromise.class);
47 doReturn(channelFuture).when(channel).newPromise();
48 doReturn(channelFuture).when(channel).writeAndFlush(any());
49 doReturn(channelFuture).when(channel).writeAndFlush(any(), any(ChannelPromise.class));
50 doReturn(channelFuture).when(channelFuture).addListener(any(GenericFutureListener.class));
51 caps = Sets.newSet("a", "b");
52 helloMessage = NetconfHelloMessage.createServerHello(caps, 10);
53 message = new NetconfMessage(helloMessage.getDocument());
54 sessionListener = mock(NetconfClientSessionListener.class);
55 clientSession = new NetconfClientSession(sessionListener, channel, 20L, caps);
58 private void mockEventLoop() {
59 final EventLoop eventLoop = mock(EventLoop.class);
60 doReturn(eventLoop).when(channel).eventLoop();
61 doAnswer(invocation -> {
62 invocation.<Runnable>getArgument(0).run();
64 }).when(eventLoop).execute(any(Runnable.class));
68 public void testSessionDown() throws Exception {
69 SimpleNetconfClientSessionListener simpleListener = new SimpleNetconfClientSessionListener();
70 final Future<NetconfMessage> promise = simpleListener.sendRequest(message);
71 simpleListener.onSessionUp(clientSession);
72 verify(channel, times(1)).writeAndFlush(any(), any());
74 simpleListener.onSessionDown(clientSession, new Exception());
75 assertFalse(promise.isSuccess());
79 public void testSendRequest() {
80 SimpleNetconfClientSessionListener simpleListener = new SimpleNetconfClientSessionListener();
81 final Future<NetconfMessage> promise = simpleListener.sendRequest(message);
82 simpleListener.onSessionUp(clientSession);
83 verify(channel, times(1)).writeAndFlush(any(), any());
85 simpleListener.sendRequest(message);
86 assertFalse(promise.isSuccess());
90 public void testOnMessage() {
91 SimpleNetconfClientSessionListener simpleListener = new SimpleNetconfClientSessionListener();
92 final Future<NetconfMessage> promise = simpleListener.sendRequest(message);
93 simpleListener.onSessionUp(clientSession);
94 verify(channel, times(1)).writeAndFlush(any(), any());
96 simpleListener.onMessage(clientSession, message);
97 assertTrue(promise.isSuccess());