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