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