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.opendaylight.netconf.api.NetconfMessage;
28 import org.opendaylight.netconf.api.messages.NetconfHelloMessage;
30 public class SimpleNetconfClientSessionListenerTest {
31 private Channel channel;
32 private ChannelPromise channelFuture;
33 private NetconfHelloMessage helloMessage;
34 private NetconfMessage message;
35 private NetconfClientSessionListener sessionListener;
36 private NetconfClientSession clientSession;
40 channel = mock(Channel.class);
41 channelFuture = mock(ChannelPromise.class);
43 doReturn(channelFuture).when(channel).newPromise();
44 doReturn(channelFuture).when(channel).writeAndFlush(any());
45 doReturn(channelFuture).when(channel).writeAndFlush(any(), any(ChannelPromise.class));
46 doReturn(channelFuture).when(channelFuture).addListener(any(GenericFutureListener.class));
47 final var caps = Set.of("a", "b");
48 helloMessage = NetconfHelloMessage.createServerHello(caps, 10);
49 message = new NetconfMessage(helloMessage.getDocument());
50 sessionListener = mock(NetconfClientSessionListener.class);
51 clientSession = new NetconfClientSession(sessionListener, channel, 20L, caps);
54 private void mockEventLoop() {
55 final EventLoop eventLoop = mock(EventLoop.class);
56 doReturn(eventLoop).when(channel).eventLoop();
57 doAnswer(invocation -> {
58 invocation.<Runnable>getArgument(0).run();
60 }).when(eventLoop).execute(any(Runnable.class));
64 public void testSessionDown() {
65 SimpleNetconfClientSessionListener simpleListener = new SimpleNetconfClientSessionListener();
66 final Future<NetconfMessage> promise = simpleListener.sendRequest(message);
67 simpleListener.onSessionUp(clientSession);
68 verify(channel, times(1)).writeAndFlush(any(), any());
70 simpleListener.onSessionDown(clientSession, new Exception());
71 assertFalse(promise.isSuccess());
75 public void testSendRequest() {
76 SimpleNetconfClientSessionListener simpleListener = new SimpleNetconfClientSessionListener();
77 final Future<NetconfMessage> promise = simpleListener.sendRequest(message);
78 simpleListener.onSessionUp(clientSession);
79 verify(channel, times(1)).writeAndFlush(any(), any());
81 simpleListener.sendRequest(message);
82 assertFalse(promise.isSuccess());
86 public void testOnMessage() {
87 SimpleNetconfClientSessionListener simpleListener = new SimpleNetconfClientSessionListener();
88 final Future<NetconfMessage> promise = simpleListener.sendRequest(message);
89 simpleListener.onSessionUp(clientSession);
90 verify(channel, times(1)).writeAndFlush(any(), any());
92 simpleListener.onMessage(clientSession, message);
93 assertTrue(promise.isSuccess());