e938f5d36f1d87259870413bb733a9819a8e54c3
[netconf.git] / netconf / netconf-util / src / test / java / org / opendaylight / 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.netconf.util.messages;
10
11 import static org.junit.Assert.assertEquals;
12 import static org.mockito.Matchers.any;
13 import static org.mockito.Mockito.doReturn;
14 import static org.mockito.Mockito.mock;
15 import static org.mockito.Mockito.times;
16 import static org.mockito.Mockito.verify;
17
18 import io.netty.channel.Channel;
19 import io.netty.channel.ChannelFuture;
20 import io.netty.util.concurrent.GenericFutureListener;
21 import org.junit.Before;
22 import org.junit.Test;
23 import org.mockito.ArgumentCaptor;
24 import org.opendaylight.netconf.api.DocumentedException;
25 import org.opendaylight.netconf.api.NetconfMessage;
26 import org.opendaylight.netconf.api.NetconfSession;
27 import org.opendaylight.netconf.util.test.XmlFileLoader;
28 import org.w3c.dom.Document;
29 import org.w3c.dom.Element;
30
31 public class SendErrorExceptionUtilTest {
32
33     NetconfSession netconfSession;
34     ChannelFuture channelFuture;
35     Channel channel;
36     private DocumentedException exception;
37
38     @Before
39     public void setUp() throws Exception {
40         netconfSession = mock(NetconfSession.class);
41         channelFuture = mock(ChannelFuture.class);
42         channel = mock(Channel.class);
43         doReturn(channelFuture).when(netconfSession).sendMessage(any(NetconfMessage.class));
44         doReturn(channelFuture).when(channelFuture).addListener(any(GenericFutureListener.class));
45         doReturn(channelFuture).when(channel).writeAndFlush(any(NetconfMessage.class));
46         exception = new DocumentedException("err");
47     }
48
49     @Test
50     public void testSendErrorMessage1() throws Exception {
51         SendErrorExceptionUtil.sendErrorMessage(netconfSession, exception);
52         verify(channelFuture, times(1)).addListener(any(GenericFutureListener.class));
53         verify(netconfSession, times(1)).sendMessage(any(NetconfMessage.class));
54     }
55
56     @Test
57     public void testSendErrorMessage2() throws Exception {
58         SendErrorExceptionUtil.sendErrorMessage(channel, exception);
59         verify(channelFuture, times(1)).addListener(any(GenericFutureListener.class));
60     }
61
62     @Test
63     public void testSendErrorMessage3() throws Exception {
64         Document helloMessage = XmlFileLoader.xmlFileToDocument("netconfMessages/rpc.xml");
65         SendErrorExceptionUtil.sendErrorMessage(netconfSession, exception, new NetconfMessage(helloMessage));
66         verify(channelFuture, times(1)).addListener(any(GenericFutureListener.class));
67     }
68
69     @Test
70     public void testSendErrorMessage4() throws Exception {
71         Document helloMessage = XmlFileLoader.xmlFileToDocument("netconfMessages/rpc_ns.xml");
72         SendErrorExceptionUtil.sendErrorMessage(netconfSession, exception, new NetconfMessage(helloMessage));
73         final ArgumentCaptor<NetconfMessage> messageCaptor = ArgumentCaptor.forClass(NetconfMessage.class);
74         verify(netconfSession, times(1)).sendMessage(messageCaptor.capture());
75         final Element rpcReply = messageCaptor.getValue().getDocument().getDocumentElement();
76         assertEquals("Invalid value of message-id attribute in the reply message", "a",
77             rpcReply.getAttribute("message-id"));
78     }
79 }