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