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.mockito.invocation.InvocationOnMock;
26 import org.mockito.stubbing.Answer;
27 import org.opendaylight.controller.config.util.xml.DocumentedException;
28 import org.opendaylight.controller.config.util.xml.XmlElement;
29 import org.opendaylight.controller.config.util.xml.XmlUtil;
30 import org.opendaylight.netconf.util.messages.NetconfHelloMessageAdditionalHeader;
31 import org.opendaylight.netconf.api.NetconfDocumentedException;
32 import org.opendaylight.netconf.api.NetconfMessage;
33 import org.opendaylight.netconf.api.NetconfTerminationReason;
34 import org.opendaylight.netconf.impl.NetconfServerSession;
35 import org.opendaylight.netconf.impl.NetconfServerSessionListener;
36 import org.w3c.dom.Document;
38 public class DefaultCloseSessionTest {
40 private void mockEventLoop(final Channel channel) {
41 final EventLoop eventLoop = mock(EventLoop.class);
42 doReturn(eventLoop).when(channel).eventLoop();
43 doAnswer(new Answer<Void>() {
45 public Void answer(InvocationOnMock invocation) throws Throwable {
46 final Object[] args = invocation.getArguments();
47 final Runnable runnable = (Runnable) args[0];
51 }).when(eventLoop).execute(any(Runnable.class));
52 doReturn(true).when(eventLoop).inEventLoop();
56 public void testDefaultCloseSession() throws Exception {
57 AutoCloseable res = mock(AutoCloseable.class);
58 doNothing().when(res).close();
59 DefaultCloseSession close = new DefaultCloseSession("", res);
60 Document doc = XmlUtil.newDocument();
61 XmlElement elem = XmlElement.fromDomElement(XmlUtil.readXmlToElement("<elem/>"));
62 final Channel channel = mock(Channel.class);
63 doReturn("channel").when(channel).toString();
64 mockEventLoop(channel);
65 final ChannelFuture channelFuture = mock(ChannelFuture.class);
66 doReturn(channelFuture).when(channel).close();
67 doReturn(channelFuture).when(channelFuture).addListener(any(GenericFutureListener.class));
69 final ChannelFuture sendFuture = mock(ChannelFuture.class);
70 doAnswer(new Answer<Object>() {
72 public Object answer(final InvocationOnMock invocation) throws Throwable {
73 ((GenericFutureListener) invocation.getArguments()[0]).operationComplete(sendFuture);
76 }).when(sendFuture).addListener(any(GenericFutureListener.class));
77 doReturn(sendFuture).when(channel).writeAndFlush(anyObject());
78 doReturn(true).when(sendFuture).isSuccess();
79 final NetconfServerSessionListener listener = mock(NetconfServerSessionListener.class);
80 doNothing().when(listener).onSessionTerminated(any(NetconfServerSession.class), any(NetconfTerminationReason.class));
81 final NetconfServerSession session =
82 new NetconfServerSession(listener, channel, 1L,
83 NetconfHelloMessageAdditionalHeader.fromString("[netconf;10.12.0.102:48528;ssh;;;;;;]"));
84 close.setNetconfSession(session);
85 close.handleWithNoSubsequentOperations(doc, elem);
86 // Fake close response to trigger delayed close
87 session.sendMessage(new NetconfMessage(XmlUtil.readXmlToDocument("<rpc-reply message-id=\"101\"\n" +
88 "xmlns=\"urn:ietf:params:xml:ns:netconf:base:1.0\">\n" +
91 verify(channel).close();
92 verify(listener).onSessionTerminated(any(NetconfServerSession.class), any(NetconfTerminationReason.class));
95 @Test(expected = DocumentedException.class)
96 public void testDefaultCloseSession2() throws Exception {
97 AutoCloseable res = mock(AutoCloseable.class);
98 doThrow(NetconfDocumentedException.class).when(res).close();
99 DefaultCloseSession session = new DefaultCloseSession("", res);
100 Document doc = XmlUtil.newDocument();
101 XmlElement elem = XmlElement.fromDomElement(XmlUtil.readXmlToElement("<elem/>"));
102 session.handleWithNoSubsequentOperations(doc, elem);