Remove CloseableUtil
[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.NetconfMessage;
30 import org.opendaylight.netconf.api.NetconfTerminationReason;
31 import org.opendaylight.netconf.api.messages.NetconfHelloMessageAdditionalHeader;
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.w3c.dom.Document;
37
38 public class DefaultCloseSessionTest {
39
40     private static void mockEventLoop(final Channel channel) {
41         final EventLoop eventLoop = mock(EventLoop.class);
42         doReturn(eventLoop).when(channel).eventLoop();
43         doAnswer(invocation -> {
44             invocation.getArgument(0, Runnable.class).run();
45             return null;
46         }).when(eventLoop).execute(any(Runnable.class));
47         doReturn(true).when(eventLoop).inEventLoop();
48     }
49
50     @Test
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));
63
64         final ChannelPromise sendFuture = mock(ChannelPromise.class);
65         doAnswer(invocation -> {
66             invocation.getArgument(0, GenericFutureListener.class).operationComplete(sendFuture);
67             return null;
68         }).when(sendFuture).addListener(any(GenericFutureListener.class));
69         doReturn(sendFuture).when(channel).newPromise();
70         doReturn(sendFuture).when(channel).writeAndFlush(any(), any());
71         doReturn(true).when(sendFuture).isSuccess();
72         final NetconfServerSessionListener listener = mock(NetconfServerSessionListener.class);
73         doNothing().when(listener).onSessionTerminated(any(NetconfServerSession.class),
74                 any(NetconfTerminationReason.class));
75         final NetconfServerSession session =
76                 new NetconfServerSession(listener, channel, 1L,
77                         NetconfHelloMessageAdditionalHeader.fromString("[netconf;10.12.0.102:48528;ssh;;;;;;]"));
78         close.setNetconfSession(session);
79         close.handleWithNoSubsequentOperations(doc, elem);
80         // Fake close response to trigger delayed close
81         session.sendMessage(new NetconfMessage(XmlUtil.readXmlToDocument("""
82             <rpc-reply message-id="101" xmlns="urn:ietf:params:xml:ns:netconf:base:1.0">
83               <ok/>
84             </rpc-reply>""")));
85         verify(channel).close();
86         verify(listener).onSessionTerminated(any(NetconfServerSession.class), any(NetconfTerminationReason.class));
87     }
88
89     @Test
90     public void testDefaultCloseSession2() throws Exception {
91         final NetconfDocumentedException expectedCause = new NetconfDocumentedException("testMessage");
92         final AutoCloseable res = mock(AutoCloseable.class);
93         doThrow(expectedCause).when(res).close();
94         final DefaultCloseSession session = new DefaultCloseSession("testSession", res);
95         XmlElement elem = XmlElement.fromDomElement(XmlUtil.readXmlToElement("<elem/>"));
96
97         final DocumentedException ex = assertThrows(DocumentedException.class,
98             () -> session.handleWithNoSubsequentOperations(XmlUtil.newDocument(), elem));
99         assertEquals("Unable to properly close session testSession", ex.getMessage());
100         assertSame(expectedCause, ex.getCause());
101     }
102 }