2 * Copyright (c) 2014 Cisco Systems, Inc. and others. All rights reserved.
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
9 package org.opendaylight.netconf.util.messages;
11 import static org.junit.Assert.assertEquals;
12 import static org.mockito.ArgumentMatchers.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;
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;
31 public class SendErrorExceptionUtilTest {
33 NetconfSession netconfSession;
34 ChannelFuture channelFuture;
36 private DocumentedException exception;
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");
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));
57 public void testSendErrorMessage2() throws Exception {
58 SendErrorExceptionUtil.sendErrorMessage(channel, exception);
59 verify(channelFuture, times(1)).addListener(any(GenericFutureListener.class));
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));
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"));