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