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.util.concurrent.GenericFutureListener;
23 import org.junit.Test;
24 import org.mockito.invocation.InvocationOnMock;
25 import org.mockito.stubbing.Answer;
26 import org.opendaylight.controller.config.util.xml.DocumentedException;
27 import org.opendaylight.controller.config.util.xml.XmlElement;
28 import org.opendaylight.controller.config.util.xml.XmlUtil;
29 import org.opendaylight.netconf.util.messages.NetconfHelloMessageAdditionalHeader;
30 import org.opendaylight.netconf.api.NetconfDocumentedException;
31 import org.opendaylight.netconf.api.NetconfMessage;
32 import org.opendaylight.netconf.api.NetconfTerminationReason;
33 import org.opendaylight.netconf.impl.NetconfServerSession;
34 import org.opendaylight.netconf.impl.NetconfServerSessionListener;
35 import org.w3c.dom.Document;
37 public class DefaultCloseSessionTest {
40 public void testDefaultCloseSession() throws Exception {
41 AutoCloseable res = mock(AutoCloseable.class);
42 doNothing().when(res).close();
43 DefaultCloseSession close = new DefaultCloseSession("", res);
44 Document doc = XmlUtil.newDocument();
45 XmlElement elem = XmlElement.fromDomElement(XmlUtil.readXmlToElement("<elem/>"));
46 final Channel channel = mock(Channel.class);
47 doReturn("channel").when(channel).toString();
48 doReturn(mock(ChannelFuture.class)).when(channel).close();
50 final ChannelFuture sendFuture = mock(ChannelFuture.class);
51 doAnswer(new Answer<Object>() {
53 public Object answer(final InvocationOnMock invocation) throws Throwable {
54 ((GenericFutureListener) invocation.getArguments()[0]).operationComplete(sendFuture);
57 }).when(sendFuture).addListener(any(GenericFutureListener.class));
58 doReturn(sendFuture).when(channel).writeAndFlush(anyObject());
59 final NetconfServerSessionListener listener = mock(NetconfServerSessionListener.class);
60 doNothing().when(listener).onSessionTerminated(any(NetconfServerSession.class), any(NetconfTerminationReason.class));
61 final NetconfServerSession session =
62 new NetconfServerSession(listener, channel, 1L,
63 NetconfHelloMessageAdditionalHeader.fromString("[netconf;10.12.0.102:48528;ssh;;;;;;]"));
64 close.setNetconfSession(session);
65 close.handleWithNoSubsequentOperations(doc, elem);
66 // Fake close response to trigger delayed close
67 session.sendMessage(new NetconfMessage(XmlUtil.readXmlToDocument("<rpc-reply message-id=\"101\"\n" +
68 "xmlns=\"urn:ietf:params:xml:ns:netconf:base:1.0\">\n" +
71 verify(channel).close();
72 verify(listener).onSessionTerminated(any(NetconfServerSession.class), any(NetconfTerminationReason.class));
75 @Test(expected = DocumentedException.class)
76 public void testDefaultCloseSession2() throws Exception {
77 AutoCloseable res = mock(AutoCloseable.class);
78 doThrow(NetconfDocumentedException.class).when(res).close();
79 DefaultCloseSession session = new DefaultCloseSession("", res);
80 Document doc = XmlUtil.newDocument();
81 XmlElement elem = XmlElement.fromDomElement(XmlUtil.readXmlToElement("<elem/>"));
82 session.handleWithNoSubsequentOperations(doc, elem);