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
8 package org.opendaylight.netconf.impl;
10 import static java.util.Objects.requireNonNull;
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.times;
15 import static org.mockito.Mockito.verify;
17 import io.netty.channel.Channel;
18 import io.netty.channel.ChannelFuture;
19 import io.netty.util.concurrent.GenericFutureListener;
20 import java.io.IOException;
21 import java.io.InputStream;
22 import org.junit.Before;
23 import org.junit.Test;
24 import org.junit.runner.RunWith;
25 import org.mockito.ArgumentCaptor;
26 import org.mockito.Mock;
27 import org.mockito.junit.MockitoJUnitRunner;
28 import org.opendaylight.netconf.api.DocumentedException;
29 import org.opendaylight.netconf.api.NetconfMessage;
30 import org.opendaylight.netconf.api.NetconfSession;
31 import org.opendaylight.netconf.api.xml.XmlUtil;
32 import org.w3c.dom.Element;
33 import org.xml.sax.SAXException;
35 @RunWith(MockitoJUnitRunner.StrictStubs.class)
36 public class SendErrorExceptionUtilTest {
37 private final DocumentedException exception = new DocumentedException("err");
40 NetconfSession netconfSession;
42 ChannelFuture channelFuture;
47 public void setUp() throws Exception {
48 doReturn(channelFuture).when(netconfSession).sendMessage(any(NetconfMessage.class));
49 doReturn(channelFuture).when(channelFuture).addListener(any(GenericFutureListener.class));
50 doReturn(channelFuture).when(channel).writeAndFlush(any(NetconfMessage.class));
54 public void testSendErrorMessage1() throws Exception {
55 SendErrorExceptionUtil.sendErrorMessage(netconfSession, exception);
56 verify(channelFuture).addListener(any(GenericFutureListener.class));
57 verify(netconfSession).sendMessage(any(NetconfMessage.class));
61 public void testSendErrorMessage2() throws Exception {
62 SendErrorExceptionUtil.sendErrorMessage(channel, exception);
63 verify(channelFuture).addListener(any(GenericFutureListener.class));
67 public void testSendErrorMessage3() throws Exception {
68 SendErrorExceptionUtil.sendErrorMessage(netconfSession, exception, readMessage("rpc.xml"));
69 verify(channelFuture).addListener(any(GenericFutureListener.class));
73 public void testSendErrorMessage4() throws Exception {
74 SendErrorExceptionUtil.sendErrorMessage(netconfSession, exception, readMessage("rpc_ns.xml"));
75 final ArgumentCaptor<NetconfMessage> messageCaptor = ArgumentCaptor.forClass(NetconfMessage.class);
76 verify(netconfSession, times(1)).sendMessage(messageCaptor.capture());
77 final Element rpcReply = messageCaptor.getValue().getDocument().getDocumentElement();
78 assertEquals("Invalid value of message-id attribute in the reply message", "a",
79 rpcReply.getAttribute("message-id"));
82 private static NetconfMessage readMessage(final String name) throws IOException, SAXException {
83 try (InputStream resource =
84 requireNonNull(SendErrorExceptionUtilTest.class.getResourceAsStream("/messages/" + name))) {
85 return new NetconfMessage(XmlUtil.readXmlToDocument(resource));