Refactor DOMDataBrokerImpl
[controller.git] / opendaylight / netconf / netconf-util / src / test / java / org / opendaylight / controller / netconf / util / messages / SendErrorExceptionUtilTest.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.util.messages;
10
11 import io.netty.channel.Channel;
12 import io.netty.channel.ChannelFuture;
13 import io.netty.util.concurrent.GenericFutureListener;
14 import org.junit.Before;
15 import org.junit.Test;
16 import org.opendaylight.controller.netconf.api.NetconfDocumentedException;
17 import org.opendaylight.controller.netconf.api.NetconfMessage;
18 import org.opendaylight.controller.netconf.api.NetconfSession;
19 import org.opendaylight.controller.netconf.util.test.XmlFileLoader;
20 import org.w3c.dom.Document;
21
22 import static org.mockito.Matchers.any;
23 import static org.mockito.Mockito.*;
24
25 public class SendErrorExceptionUtilTest {
26
27     NetconfSession netconfSession;
28     ChannelFuture channelFuture;
29     Channel channel;
30     private NetconfDocumentedException exception;
31
32     @Before
33     public void setUp() throws Exception {
34         netconfSession = mock(NetconfSession.class);
35         channelFuture = mock(ChannelFuture.class);
36         channel = mock(Channel.class);
37         doReturn(channelFuture).when(netconfSession).sendMessage(any(NetconfMessage.class));
38         doReturn(channelFuture).when(channelFuture).addListener(any(GenericFutureListener.class));
39         doReturn(channelFuture).when(channel).writeAndFlush(any(NetconfMessage.class));
40         exception = new NetconfDocumentedException("err");
41     }
42
43     @Test
44     public void testSendErrorMessage1() throws Exception {
45         SendErrorExceptionUtil.sendErrorMessage(netconfSession, exception);
46         verify(channelFuture, times(1)).addListener(any(GenericFutureListener.class));
47         verify(netconfSession, times(1)).sendMessage(any(NetconfMessage.class));
48     }
49
50     @Test
51     public void testSendErrorMessage2() throws Exception {
52         SendErrorExceptionUtil.sendErrorMessage(channel, exception);
53         verify(channelFuture, times(1)).addListener(any(GenericFutureListener.class));
54     }
55
56     @Test
57     public void testSendErrorMessage3() throws Exception {
58         Document helloMessage = XmlFileLoader.xmlFileToDocument("netconfMessages/rpc.xml");
59         SendErrorExceptionUtil.sendErrorMessage(netconfSession, exception, new NetconfMessage(helloMessage));
60         verify(channelFuture, times(1)).addListener(any(GenericFutureListener.class));
61     }
62 }