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
9 package org.opendaylight.netconf.client;
11 import static org.junit.Assert.assertFalse;
12 import static org.junit.Assert.assertTrue;
13 import static org.mockito.Matchers.any;
14 import static org.mockito.Matchers.anyObject;
15 import static org.mockito.Mockito.doAnswer;
16 import static org.mockito.Mockito.doReturn;
17 import static org.mockito.Mockito.mock;
18 import static org.mockito.Mockito.times;
19 import static org.mockito.Mockito.verify;
21 import io.netty.channel.Channel;
22 import io.netty.channel.ChannelPromise;
23 import io.netty.channel.EventLoop;
24 import io.netty.util.concurrent.Future;
25 import io.netty.util.concurrent.GenericFutureListener;
27 import org.junit.Before;
28 import org.junit.Test;
29 import org.mockito.internal.util.collections.Sets;
30 import org.opendaylight.netconf.api.NetconfDocumentedException;
31 import org.opendaylight.netconf.api.NetconfMessage;
32 import org.opendaylight.netconf.api.messages.NetconfHelloMessage;
34 public class SimpleNetconfClientSessionListenerTest {
36 private Channel channel;
37 private ChannelPromise channelFuture;
39 private NetconfHelloMessage helloMessage;
40 private NetconfMessage message;
41 private NetconfClientSessionListener sessionListener;
42 private NetconfClientSession clientSession;
45 public void setUp() throws NetconfDocumentedException {
46 channel = mock(Channel.class);
47 channelFuture = mock(ChannelPromise.class);
49 doReturn(channelFuture).when(channel).newPromise();
50 doReturn(channelFuture).when(channel).writeAndFlush(anyObject());
51 doReturn(channelFuture).when(channel).writeAndFlush(anyObject(), any(ChannelPromise.class));
52 doReturn(channelFuture).when(channelFuture).addListener(any(GenericFutureListener.class));
53 caps = Sets.newSet("a", "b");
54 helloMessage = NetconfHelloMessage.createServerHello(caps, 10);
55 message = new NetconfMessage(helloMessage.getDocument());
56 sessionListener = mock(NetconfClientSessionListener.class);
57 clientSession = new NetconfClientSession(sessionListener, channel, 20L, caps);
60 private void mockEventLoop() {
61 final EventLoop eventLoop = mock(EventLoop.class);
62 doReturn(eventLoop).when(channel).eventLoop();
63 doAnswer(invocation -> {
64 invocation.<Runnable>getArgument(0).run();
66 }).when(eventLoop).execute(any(Runnable.class));
70 public void testSessionDown() throws Exception {
71 SimpleNetconfClientSessionListener simpleListener = new SimpleNetconfClientSessionListener();
72 final Future<NetconfMessage> promise = simpleListener.sendRequest(message);
73 simpleListener.onSessionUp(clientSession);
74 verify(channel, times(1)).writeAndFlush(anyObject(), anyObject());
76 simpleListener.onSessionDown(clientSession, new Exception());
77 assertFalse(promise.isSuccess());
81 public void testSendRequest() {
82 SimpleNetconfClientSessionListener simpleListener = new SimpleNetconfClientSessionListener();
83 final Future<NetconfMessage> promise = simpleListener.sendRequest(message);
84 simpleListener.onSessionUp(clientSession);
85 verify(channel, times(1)).writeAndFlush(anyObject(), anyObject());
87 simpleListener.sendRequest(message);
88 assertFalse(promise.isSuccess());
92 public void testOnMessage() {
93 SimpleNetconfClientSessionListener simpleListener = new SimpleNetconfClientSessionListener();
94 final Future<NetconfMessage> promise = simpleListener.sendRequest(message);
95 simpleListener.onSessionUp(clientSession);
96 verify(channel, times(1)).writeAndFlush(anyObject(), anyObject());
98 simpleListener.onMessage(clientSession, message);
99 assertTrue(promise.isSuccess());