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