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.ChannelFuture;
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.mockito.invocation.InvocationOnMock;
31 import org.mockito.stubbing.Answer;
32 import org.opendaylight.netconf.api.NetconfMessage;
33 import org.opendaylight.netconf.api.messages.NetconfHelloMessage;
35 public class SimpleNetconfClientSessionListenerTest {
37 private Channel channel;
38 private ChannelFuture channelFuture;
40 private NetconfHelloMessage helloMessage;
41 private NetconfMessage message;
42 private NetconfClientSessionListener sessionListener;
43 private NetconfClientSession clientSession;
46 public void setUp() throws Exception {
47 channel = mock(Channel.class);
48 channelFuture = mock(ChannelFuture.class);
50 doReturn(channelFuture).when(channel).writeAndFlush(anyObject());
51 doReturn(channelFuture).when(channelFuture).addListener(any(GenericFutureListener.class));
52 caps = Sets.newSet("a", "b");
53 helloMessage = NetconfHelloMessage.createServerHello(caps, 10);
54 message = new NetconfMessage(helloMessage.getDocument());
55 sessionListener = mock(NetconfClientSessionListener.class);
56 clientSession = new NetconfClientSession(sessionListener, channel, 20L, caps);
59 private void mockEventLoop() {
60 final EventLoop eventLoop = mock(EventLoop.class);
61 doReturn(eventLoop).when(channel).eventLoop();
62 doAnswer(new Answer<Void>() {
64 public Void answer(InvocationOnMock invocation) throws Throwable {
65 final Object[] args = invocation.getArguments();
66 final Runnable runnable = (Runnable) args[0];
70 }).when(eventLoop).execute(any(Runnable.class));
74 public void testSessionDown() throws Exception {
75 SimpleNetconfClientSessionListener simpleListener = new SimpleNetconfClientSessionListener();
76 final Future<NetconfMessage> promise = simpleListener.sendRequest(message);
77 simpleListener.onSessionUp(clientSession);
78 verify(channel, times(1)).writeAndFlush(anyObject());
80 simpleListener.onSessionDown(clientSession, new Exception());
81 assertFalse(promise.isSuccess());
85 public void testSendRequest() throws Exception {
86 SimpleNetconfClientSessionListener simpleListener = new SimpleNetconfClientSessionListener();
87 final Future<NetconfMessage> promise = simpleListener.sendRequest(message);
88 simpleListener.onSessionUp(clientSession);
89 verify(channel, times(1)).writeAndFlush(anyObject());
91 simpleListener.sendRequest(message);
92 assertFalse(promise.isSuccess());
96 public void testOnMessage() throws Exception {
97 SimpleNetconfClientSessionListener simpleListener = new SimpleNetconfClientSessionListener();
98 final Future<NetconfMessage> promise = simpleListener.sendRequest(message);
99 simpleListener.onSessionUp(clientSession);
100 verify(channel, times(1)).writeAndFlush(anyObject());
102 simpleListener.onMessage(clientSession, message);
103 assertTrue(promise.isSuccess());