Move NetconfMessage into netconf.api.messages
[netconf.git] / protocol / netconf-server / src / test / java / org / opendaylight / netconf / server / mapping / operations / DefaultCloseSessionTest.java
1 /*
2  * Copyright (c) 2014 Cisco Systems, Inc. and others.  All rights reserved.
3  *
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
7  */
8 package org.opendaylight.netconf.server.mapping.operations;
9
10 import static org.junit.Assert.assertEquals;
11 import static org.junit.Assert.assertSame;
12 import static org.junit.Assert.assertThrows;
13 import static org.mockito.ArgumentMatchers.any;
14 import static org.mockito.Mockito.doAnswer;
15 import static org.mockito.Mockito.doNothing;
16 import static org.mockito.Mockito.doReturn;
17 import static org.mockito.Mockito.doThrow;
18 import static org.mockito.Mockito.mock;
19 import static org.mockito.Mockito.verify;
20
21 import io.netty.channel.Channel;
22 import io.netty.channel.ChannelFuture;
23 import io.netty.channel.ChannelPromise;
24 import io.netty.channel.EventLoop;
25 import io.netty.util.concurrent.GenericFutureListener;
26 import org.junit.Test;
27 import org.opendaylight.netconf.api.DocumentedException;
28 import org.opendaylight.netconf.api.NetconfDocumentedException;
29 import org.opendaylight.netconf.api.NetconfTerminationReason;
30 import org.opendaylight.netconf.api.messages.NetconfHelloMessageAdditionalHeader;
31 import org.opendaylight.netconf.api.messages.NetconfMessage;
32 import org.opendaylight.netconf.api.xml.XmlElement;
33 import org.opendaylight.netconf.api.xml.XmlUtil;
34 import org.opendaylight.netconf.server.NetconfServerSession;
35 import org.opendaylight.netconf.server.NetconfServerSessionListener;
36 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.netconf.base._1._0.rev110601.SessionIdType;
37 import org.opendaylight.yangtools.yang.common.Uint32;
38 import org.w3c.dom.Document;
39
40 public class DefaultCloseSessionTest {
41
42     private static void mockEventLoop(final Channel channel) {
43         final EventLoop eventLoop = mock(EventLoop.class);
44         doReturn(eventLoop).when(channel).eventLoop();
45         doAnswer(invocation -> {
46             invocation.getArgument(0, Runnable.class).run();
47             return null;
48         }).when(eventLoop).execute(any(Runnable.class));
49         doReturn(true).when(eventLoop).inEventLoop();
50     }
51
52     @Test
53     public void testDefaultCloseSession() throws Exception {
54         AutoCloseable res = mock(AutoCloseable.class);
55         doNothing().when(res).close();
56         DefaultCloseSession close = new DefaultCloseSession(new SessionIdType(Uint32.TEN), res);
57         final Document doc = XmlUtil.newDocument();
58         final XmlElement elem = XmlElement.fromDomElement(XmlUtil.readXmlToElement("<elem/>"));
59         final Channel channel = mock(Channel.class);
60         doReturn("channel").when(channel).toString();
61         mockEventLoop(channel);
62         final ChannelFuture channelFuture = mock(ChannelFuture.class);
63         doReturn(channelFuture).when(channel).close();
64         doReturn(channelFuture).when(channelFuture).addListener(any(GenericFutureListener.class));
65
66         final ChannelPromise sendFuture = mock(ChannelPromise.class);
67         doAnswer(invocation -> {
68             invocation.getArgument(0, GenericFutureListener.class).operationComplete(sendFuture);
69             return null;
70         }).when(sendFuture).addListener(any(GenericFutureListener.class));
71         doReturn(sendFuture).when(channel).newPromise();
72         doReturn(sendFuture).when(channel).writeAndFlush(any(), any());
73         doReturn(true).when(sendFuture).isSuccess();
74         final NetconfServerSessionListener listener = mock(NetconfServerSessionListener.class);
75         doNothing().when(listener).onSessionTerminated(any(NetconfServerSession.class),
76                 any(NetconfTerminationReason.class));
77         final NetconfServerSession session =
78                 new NetconfServerSession(listener, channel, new SessionIdType(Uint32.ONE),
79                         NetconfHelloMessageAdditionalHeader.fromString("[netconf;10.12.0.102:48528;ssh;;;;;;]"));
80         close.setNetconfSession(session);
81         close.handleWithNoSubsequentOperations(doc, elem);
82         // Fake close response to trigger delayed close
83         session.sendMessage(new NetconfMessage(XmlUtil.readXmlToDocument("""
84             <rpc-reply message-id="101" xmlns="urn:ietf:params:xml:ns:netconf:base:1.0">
85               <ok/>
86             </rpc-reply>""")));
87         verify(channel).close();
88         verify(listener).onSessionTerminated(any(NetconfServerSession.class), any(NetconfTerminationReason.class));
89     }
90
91     @Test
92     public void testDefaultCloseSession2() throws Exception {
93         final NetconfDocumentedException expectedCause = new NetconfDocumentedException("testMessage");
94         final AutoCloseable res = mock(AutoCloseable.class);
95         doThrow(expectedCause).when(res).close();
96         final DefaultCloseSession session = new DefaultCloseSession(new SessionIdType(Uint32.TEN), res);
97         XmlElement elem = XmlElement.fromDomElement(XmlUtil.readXmlToElement("<elem/>"));
98
99         final DocumentedException ex = assertThrows(DocumentedException.class,
100             () -> session.handleWithNoSubsequentOperations(XmlUtil.newDocument(), elem));
101         assertEquals("Unable to properly close session 10", ex.getMessage());
102         assertSame(expectedCause, ex.getCause());
103     }
104 }