Move SendErrorExceptionUtil
[netconf.git] / netconf / netconf-impl / src / test / java / org / opendaylight / netconf / impl / SendErrorExceptionUtilTest.java
similarity index 66%
rename from netconf/netconf-util/src/test/java/org/opendaylight/netconf/util/messages/SendErrorExceptionUtilTest.java
rename to netconf/netconf-impl/src/test/java/org/opendaylight/netconf/impl/SendErrorExceptionUtilTest.java
index b5421384d6f41d68f0c979c9e6cbca36c818d164..5dbd480f60d130b2976e7de5e551d78db9c885c9 100644 (file)
@@ -5,75 +5,84 @@
  * terms of the Eclipse Public License v1.0 which accompanies this distribution,
  * and is available at http://www.eclipse.org/legal/epl-v10.html
  */
+package org.opendaylight.netconf.impl;
 
-package org.opendaylight.netconf.util.messages;
-
+import static java.util.Objects.requireNonNull;
 import static org.junit.Assert.assertEquals;
 import static org.mockito.ArgumentMatchers.any;
 import static org.mockito.Mockito.doReturn;
-import static org.mockito.Mockito.mock;
 import static org.mockito.Mockito.times;
 import static org.mockito.Mockito.verify;
 
 import io.netty.channel.Channel;
 import io.netty.channel.ChannelFuture;
 import io.netty.util.concurrent.GenericFutureListener;
+import java.io.IOException;
+import java.io.InputStream;
 import org.junit.Before;
 import org.junit.Test;
+import org.junit.runner.RunWith;
 import org.mockito.ArgumentCaptor;
+import org.mockito.Mock;
+import org.mockito.junit.MockitoJUnitRunner;
 import org.opendaylight.netconf.api.DocumentedException;
 import org.opendaylight.netconf.api.NetconfMessage;
 import org.opendaylight.netconf.api.NetconfSession;
-import org.opendaylight.netconf.util.test.XmlFileLoader;
-import org.w3c.dom.Document;
+import org.opendaylight.netconf.api.xml.XmlUtil;
 import org.w3c.dom.Element;
+import org.xml.sax.SAXException;
 
+@RunWith(MockitoJUnitRunner.StrictStubs.class)
 public class SendErrorExceptionUtilTest {
+    private final DocumentedException exception = new DocumentedException("err");
 
+    @Mock
     NetconfSession netconfSession;
+    @Mock
     ChannelFuture channelFuture;
+    @Mock
     Channel channel;
-    private DocumentedException exception;
 
     @Before
     public void setUp() throws Exception {
-        netconfSession = mock(NetconfSession.class);
-        channelFuture = mock(ChannelFuture.class);
-        channel = mock(Channel.class);
         doReturn(channelFuture).when(netconfSession).sendMessage(any(NetconfMessage.class));
         doReturn(channelFuture).when(channelFuture).addListener(any(GenericFutureListener.class));
         doReturn(channelFuture).when(channel).writeAndFlush(any(NetconfMessage.class));
-        exception = new DocumentedException("err");
     }
 
     @Test
     public void testSendErrorMessage1() throws Exception {
         SendErrorExceptionUtil.sendErrorMessage(netconfSession, exception);
-        verify(channelFuture, times(1)).addListener(any(GenericFutureListener.class));
-        verify(netconfSession, times(1)).sendMessage(any(NetconfMessage.class));
+        verify(channelFuture).addListener(any(GenericFutureListener.class));
+        verify(netconfSession).sendMessage(any(NetconfMessage.class));
     }
 
     @Test
     public void testSendErrorMessage2() throws Exception {
         SendErrorExceptionUtil.sendErrorMessage(channel, exception);
-        verify(channelFuture, times(1)).addListener(any(GenericFutureListener.class));
+        verify(channelFuture).addListener(any(GenericFutureListener.class));
     }
 
     @Test
     public void testSendErrorMessage3() throws Exception {
-        Document helloMessage = XmlFileLoader.xmlFileToDocument("netconfMessages/rpc.xml");
-        SendErrorExceptionUtil.sendErrorMessage(netconfSession, exception, new NetconfMessage(helloMessage));
-        verify(channelFuture, times(1)).addListener(any(GenericFutureListener.class));
+        SendErrorExceptionUtil.sendErrorMessage(netconfSession, exception, readMessage("rpc.xml"));
+        verify(channelFuture).addListener(any(GenericFutureListener.class));
     }
 
     @Test
     public void testSendErrorMessage4() throws Exception {
-        Document helloMessage = XmlFileLoader.xmlFileToDocument("netconfMessages/rpc_ns.xml");
-        SendErrorExceptionUtil.sendErrorMessage(netconfSession, exception, new NetconfMessage(helloMessage));
+        SendErrorExceptionUtil.sendErrorMessage(netconfSession, exception, readMessage("rpc_ns.xml"));
         final ArgumentCaptor<NetconfMessage> messageCaptor = ArgumentCaptor.forClass(NetconfMessage.class);
         verify(netconfSession, times(1)).sendMessage(messageCaptor.capture());
         final Element rpcReply = messageCaptor.getValue().getDocument().getDocumentElement();
         assertEquals("Invalid value of message-id attribute in the reply message", "a",
             rpcReply.getAttribute("message-id"));
     }
+
+    private static NetconfMessage readMessage(final String name) throws IOException, SAXException {
+        try (InputStream resource =
+                requireNonNull(SendErrorExceptionUtilTest.class.getResourceAsStream("/messages/" + name))) {
+            return new NetconfMessage(XmlUtil.readXmlToDocument(resource));
+        }
+    }
 }