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.anyObject;
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.ChannelFuture;
21 import io.netty.util.concurrent.Future;
23 import org.junit.Before;
24 import org.junit.Test;
25 import org.mockito.internal.util.collections.Sets;
26 import org.opendaylight.netconf.util.messages.NetconfHelloMessage;
27 import org.opendaylight.netconf.api.NetconfMessage;
29 public class SimpleNetconfClientSessionListenerTest {
31 private Channel channel;
32 private ChannelFuture channelFuture;
34 private NetconfHelloMessage helloMessage;
35 private NetconfMessage message;
36 private NetconfClientSessionListener sessionListener;
37 private NetconfClientSession clientSession;
40 public void setUp() throws Exception {
41 channel = mock(Channel.class);
42 channelFuture = mock(ChannelFuture.class);
43 doReturn(channelFuture).when(channel).writeAndFlush(anyObject());
44 caps = Sets.newSet("a", "b");
45 helloMessage = NetconfHelloMessage.createServerHello(caps, 10);
46 message = new NetconfMessage(helloMessage.getDocument());
47 sessionListener = mock(NetconfClientSessionListener.class);
48 clientSession = new NetconfClientSession(sessionListener, channel, 20L, caps);
52 public void testSessionDown() throws Exception {
53 SimpleNetconfClientSessionListener simpleListener = new SimpleNetconfClientSessionListener();
54 Future<NetconfMessage> promise = simpleListener.sendRequest(message);
55 simpleListener.onSessionUp(clientSession);
56 verify(channel, times(1)).writeAndFlush(anyObject());
58 simpleListener.onSessionDown(clientSession, new Exception());
59 assertFalse(promise.isSuccess());
63 public void testSendRequest() throws Exception {
64 SimpleNetconfClientSessionListener simpleListener = new SimpleNetconfClientSessionListener();
65 Future<NetconfMessage> promise = simpleListener.sendRequest(message);
66 simpleListener.onSessionUp(clientSession);
67 verify(channel, times(1)).writeAndFlush(anyObject());
69 simpleListener.sendRequest(message);
70 assertFalse(promise.isSuccess());
74 public void testOnMessage() throws Exception {
75 SimpleNetconfClientSessionListener simpleListener = new SimpleNetconfClientSessionListener();
76 Future<NetconfMessage> promise = simpleListener.sendRequest(message);
77 simpleListener.onSessionUp(clientSession);
78 verify(channel, times(1)).writeAndFlush(anyObject());
80 simpleListener.onMessage(clientSession, message);
81 assertTrue(promise.isSuccess());