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