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.controller.netconf.client;
10 import io.netty.channel.*;
11 import io.netty.util.concurrent.Future;
12 import io.netty.util.concurrent.Promise;
13 import org.junit.Before;
14 import org.junit.Test;
15 import org.mockito.internal.util.collections.Sets;
16 import org.opendaylight.controller.netconf.api.NetconfMessage;
17 import org.opendaylight.controller.netconf.util.messages.NetconfHelloMessage;
21 import static org.junit.Assert.*;
22 import static org.mockito.Matchers.anyObject;
23 import static org.mockito.Mockito.*;
25 public class SimpleNetconfClientSessionListenerTest {
27 private Channel channel;
28 private ChannelFuture channelFuture;
30 private NetconfHelloMessage helloMessage;
31 private NetconfMessage message;
32 private NetconfClientSessionListener sessionListener;
33 private NetconfClientSession clientSession;
36 public void setUp() throws Exception {
37 channel = mock(Channel.class);
38 channelFuture = mock(ChannelFuture.class);
39 doReturn(channelFuture).when(channel).writeAndFlush(anyObject());
40 caps = Sets.newSet("a", "b");
41 helloMessage = NetconfHelloMessage.createServerHello(caps, 10);
42 message = new NetconfMessage(helloMessage.getDocument());
43 sessionListener = mock(NetconfClientSessionListener.class);
44 clientSession = new NetconfClientSession(sessionListener, channel, 20L, caps);
48 public void testSessionDown() throws Exception {
49 SimpleNetconfClientSessionListener simpleListener = new SimpleNetconfClientSessionListener();
50 Future<NetconfMessage> promise = simpleListener.sendRequest(message);
51 simpleListener.onSessionUp(clientSession);
52 verify(channel, times(1)).writeAndFlush(anyObject());
54 simpleListener.onSessionDown(clientSession, new Exception());
55 assertFalse(promise.isSuccess());
59 public void testSendRequest() throws Exception {
60 SimpleNetconfClientSessionListener simpleListener = new SimpleNetconfClientSessionListener();
61 Future<NetconfMessage> promise = simpleListener.sendRequest(message);
62 simpleListener.onSessionUp(clientSession);
63 verify(channel, times(1)).writeAndFlush(anyObject());
65 simpleListener.sendRequest(message);
66 assertFalse(promise.isSuccess());
70 public void testOnMessage() throws Exception {
71 SimpleNetconfClientSessionListener simpleListener = new SimpleNetconfClientSessionListener();
72 Future<NetconfMessage> promise = simpleListener.sendRequest(message);
73 simpleListener.onSessionUp(clientSession);
74 verify(channel, times(1)).writeAndFlush(anyObject());
76 simpleListener.onMessage(clientSession, message);
77 assertTrue(promise.isSuccess());