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.impl.mapping.operations;
11 import static org.mockito.Matchers.any;
12 import static org.mockito.Matchers.anyObject;
13 import static org.mockito.Mockito.doAnswer;
14 import static org.mockito.Mockito.doNothing;
15 import static org.mockito.Mockito.doReturn;
16 import static org.mockito.Mockito.doThrow;
17 import static org.mockito.Mockito.mock;
18 import static org.mockito.Mockito.verify;
20 import io.netty.channel.Channel;
21 import io.netty.channel.ChannelFuture;
22 import io.netty.channel.EventLoop;
23 import io.netty.util.concurrent.GenericFutureListener;
24 import org.junit.Test;
25 import org.opendaylight.controller.config.util.xml.DocumentedException;
26 import org.opendaylight.controller.config.util.xml.XmlElement;
27 import org.opendaylight.controller.config.util.xml.XmlUtil;
28 import org.opendaylight.netconf.api.NetconfDocumentedException;
29 import org.opendaylight.netconf.api.NetconfMessage;
30 import org.opendaylight.netconf.api.NetconfTerminationReason;
31 import org.opendaylight.netconf.api.messages.NetconfHelloMessageAdditionalHeader;
32 import org.opendaylight.netconf.impl.NetconfServerSession;
33 import org.opendaylight.netconf.impl.NetconfServerSessionListener;
34 import org.w3c.dom.Document;
36 public class DefaultCloseSessionTest {
38 private static void mockEventLoop(final Channel channel) {
39 final EventLoop eventLoop = mock(EventLoop.class);
40 doReturn(eventLoop).when(channel).eventLoop();
41 doAnswer(invocation -> {
42 final Object[] args = invocation.getArguments();
43 final Runnable runnable = (Runnable) args[0];
46 }).when(eventLoop).execute(any(Runnable.class));
47 doReturn(true).when(eventLoop).inEventLoop();
51 public void testDefaultCloseSession() throws Exception {
52 AutoCloseable res = mock(AutoCloseable.class);
53 doNothing().when(res).close();
54 DefaultCloseSession close = new DefaultCloseSession("", res);
55 final Document doc = XmlUtil.newDocument();
56 final XmlElement elem = XmlElement.fromDomElement(XmlUtil.readXmlToElement("<elem/>"));
57 final Channel channel = mock(Channel.class);
58 doReturn("channel").when(channel).toString();
59 mockEventLoop(channel);
60 final ChannelFuture channelFuture = mock(ChannelFuture.class);
61 doReturn(channelFuture).when(channel).close();
62 doReturn(channelFuture).when(channelFuture).addListener(any(GenericFutureListener.class));
64 final ChannelFuture sendFuture = mock(ChannelFuture.class);
65 doAnswer(invocation -> {
66 ((GenericFutureListener) invocation.getArguments()[0]).operationComplete(sendFuture);
68 }).when(sendFuture).addListener(any(GenericFutureListener.class));
69 doReturn(sendFuture).when(channel).writeAndFlush(anyObject());
70 doReturn(true).when(sendFuture).isSuccess();
71 final NetconfServerSessionListener listener = mock(NetconfServerSessionListener.class);
72 doNothing().when(listener).onSessionTerminated(any(NetconfServerSession.class),
73 any(NetconfTerminationReason.class));
74 final NetconfServerSession session =
75 new NetconfServerSession(listener, channel, 1L,
76 NetconfHelloMessageAdditionalHeader.fromString("[netconf;10.12.0.102:48528;ssh;;;;;;]"));
77 close.setNetconfSession(session);
78 close.handleWithNoSubsequentOperations(doc, elem);
79 // Fake close response to trigger delayed close
80 session.sendMessage(new NetconfMessage(XmlUtil.readXmlToDocument("<rpc-reply message-id=\"101\"\n"
81 + "xmlns=\"urn:ietf:params:xml:ns:netconf:base:1.0\">\n"
84 verify(channel).close();
85 verify(listener).onSessionTerminated(any(NetconfServerSession.class), any(NetconfTerminationReason.class));
88 @Test(expected = DocumentedException.class)
89 public void testDefaultCloseSession2() throws Exception {
90 AutoCloseable res = mock(AutoCloseable.class);
91 doThrow(NetconfDocumentedException.class).when(res).close();
92 DefaultCloseSession session = new DefaultCloseSession("", res);
93 Document doc = XmlUtil.newDocument();
94 XmlElement elem = XmlElement.fromDomElement(XmlUtil.readXmlToElement("<elem/>"));
95 session.handleWithNoSubsequentOperations(doc, elem);