Move SendErrorExceptionUtil 04/97304/9
authorRobert Varga <robert.varga@pantheon.tech>
Mon, 23 Aug 2021 23:35:44 +0000 (01:35 +0200)
committerRobert Varga <nite@hq.sk>
Tue, 31 Aug 2021 12:32:28 +0000 (12:32 +0000)
SendErrorExceptionUtil is only used in netconf-impl, move it there
along with its tests. This improves our ability to reason about its
interactions.

Change-Id: I4e93c0d3fcbe87a6689f55d592a085d7aa6b8be3
Signed-off-by: Robert Varga <robert.varga@pantheon.tech>
netconf/netconf-impl/src/main/java/org/opendaylight/netconf/impl/NetconfServerSessionListener.java
netconf/netconf-impl/src/main/java/org/opendaylight/netconf/impl/SendErrorExceptionUtil.java [moved from netconf/netconf-util/src/main/java/org/opendaylight/netconf/util/messages/SendErrorExceptionUtil.java with 99% similarity]
netconf/netconf-impl/src/main/java/org/opendaylight/netconf/impl/util/DeserializerExceptionHandler.java
netconf/netconf-impl/src/test/java/org/opendaylight/netconf/impl/SendErrorExceptionUtilTest.java [moved from netconf/netconf-util/src/test/java/org/opendaylight/netconf/util/messages/SendErrorExceptionUtilTest.java with 66% similarity]
netconf/netconf-impl/src/test/resources/messages/rpc.xml [moved from netconf/netconf-util/src/test/resources/netconfMessages/rpc.xml with 100% similarity]
netconf/netconf-impl/src/test/resources/messages/rpc_ns.xml [moved from netconf/netconf-util/src/test/resources/netconfMessages/rpc_ns.xml with 100% similarity]
netconf/netconf-util/src/test/java/org/opendaylight/netconf/util/mapping/AbstractNetconfOperationTest.java
netconf/netconf-util/src/test/java/org/opendaylight/netconf/util/messages/NetconfMessageUtilTest.java
netconf/netconf-util/src/test/java/org/opendaylight/netconf/util/test/XmlFileLoader.java

index c641d4158dd22ff93dcc2fd100d4ff3ebc51a797..141edb98d36a88f23261c92e56b6d2ca1ee663af 100644 (file)
@@ -20,7 +20,6 @@ import org.opendaylight.netconf.api.xml.XmlNetconfConstants;
 import org.opendaylight.netconf.api.xml.XmlUtil;
 import org.opendaylight.netconf.impl.osgi.NetconfOperationRouter;
 import org.opendaylight.netconf.notifications.NetconfNotification;
-import org.opendaylight.netconf.util.messages.SendErrorExceptionUtil;
 import org.opendaylight.netconf.util.messages.SubtreeFilter;
 import org.opendaylight.yangtools.yang.common.ErrorSeverity;
 import org.opendaylight.yangtools.yang.common.ErrorTag;
similarity index 99%
rename from netconf/netconf-util/src/main/java/org/opendaylight/netconf/util/messages/SendErrorExceptionUtil.java
rename to netconf/netconf-impl/src/main/java/org/opendaylight/netconf/impl/SendErrorExceptionUtil.java
index 4f93674224edd29c5d5465a69ffc9882b6669510..b119c3738bdf72a23df93c051a890e13f2171cf6 100644 (file)
@@ -5,8 +5,7 @@
  * 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.util.messages;
+package org.opendaylight.netconf.impl;
 
 import com.google.common.base.Preconditions;
 import io.netty.channel.Channel;
index 4c7c0b89645326d4f24ce6755ae9744c4920dac9..6deea762f4697514adf7a7740b59398b733c19ea 100644 (file)
@@ -11,7 +11,7 @@ import io.netty.channel.ChannelHandlerContext;
 import io.netty.channel.ChannelInboundHandlerAdapter;
 import java.util.Map;
 import org.opendaylight.netconf.api.DocumentedException;
-import org.opendaylight.netconf.util.messages.SendErrorExceptionUtil;
+import org.opendaylight.netconf.impl.SendErrorExceptionUtil;
 import org.opendaylight.yangtools.yang.common.ErrorSeverity;
 import org.opendaylight.yangtools.yang.common.ErrorTag;
 import org.opendaylight.yangtools.yang.common.ErrorType;
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));
+        }
+    }
 }
index ad3ab9eaa0b2b143a37758f0e212dd2f331274b8..5420432f01f568eaf1cf49f6badbe343b3a88750 100644 (file)
@@ -27,12 +27,10 @@ import org.w3c.dom.Element;
 import org.xml.sax.SAXException;
 
 public class AbstractNetconfOperationTest {
-
-    class NetconfOperationImpl extends AbstractNetconfOperation {
-
+    static class NetconfOperationImpl extends AbstractNetconfOperation {
         public boolean handleRun;
 
-        protected NetconfOperationImpl(String netconfSessionIdForReporting) {
+        protected NetconfOperationImpl(final String netconfSessionIdForReporting) {
             super(netconfSessionIdForReporting);
             this.handleRun = false;
         }
@@ -43,23 +41,22 @@ public class AbstractNetconfOperationTest {
         }
 
         @Override
-        protected Element handle(Document document, XmlElement message,
-                                 NetconfOperationChainedExecution subsequentOperation) throws DocumentedException {
+        protected Element handle(final Document document, final XmlElement message,
+                final NetconfOperationChainedExecution subsequentOperation) throws DocumentedException {
             this.handleRun = true;
             try {
                 return XmlUtil.readXmlToElement("<element/>");
             } catch (SAXException | IOException e) {
-                throw new RuntimeException(e);
+                throw DocumentedException.wrap(e);
             }
         }
     }
 
-    private NetconfOperationImpl netconfOperation;
+    private final NetconfOperationImpl netconfOperation = new NetconfOperationImpl("str");
     private NetconfOperationChainedExecution operation;
 
     @Before
     public void setUp() throws Exception {
-        netconfOperation = new NetconfOperationImpl("str");
         operation = mock(NetconfOperationChainedExecution.class);
     }
 
index 53d2c585ef6f26a06a8b269917cb24b3f3c47789..aa953fe77b63e3f90177d52cab0e01fb4f59662e 100644 (file)
@@ -5,7 +5,6 @@
  * 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.util.messages;
 
 import static org.junit.Assert.assertFalse;
@@ -15,23 +14,22 @@ import java.util.Collection;
 import org.junit.Test;
 import org.opendaylight.netconf.api.NetconfMessage;
 import org.opendaylight.netconf.util.test.XmlFileLoader;
-import org.w3c.dom.Document;
 
 public class NetconfMessageUtilTest {
     @Test
     public void testNetconfMessageUtil() throws Exception {
-        Document okMessage = XmlFileLoader.xmlFileToDocument("netconfMessages/rpc-reply_ok.xml");
-        assertTrue(NetconfMessageUtil.isOKMessage(new NetconfMessage(okMessage)));
-        assertFalse(NetconfMessageUtil.isErrorMessage(new NetconfMessage(okMessage)));
+        final NetconfMessage okMessage = new NetconfMessage(XmlFileLoader.xmlFileToDocument(
+            "netconfMessages/rpc-reply_ok.xml"));
+        assertTrue(NetconfMessageUtil.isOKMessage(okMessage));
+        assertFalse(NetconfMessageUtil.isErrorMessage(okMessage));
 
-        Document errorMessage = XmlFileLoader
-                .xmlFileToDocument("netconfMessages/communicationError/testClientSendsRpcReply_expectedResponse.xml");
-        assertTrue(NetconfMessageUtil.isErrorMessage(new NetconfMessage(errorMessage)));
-        assertFalse(NetconfMessageUtil.isOKMessage(new NetconfMessage(errorMessage)));
+        final NetconfMessage errorMessage = new NetconfMessage(XmlFileLoader.xmlFileToDocument(
+            "netconfMessages/communicationError/testClientSendsRpcReply_expectedResponse.xml"));
+        assertTrue(NetconfMessageUtil.isErrorMessage(errorMessage));
+        assertFalse(NetconfMessageUtil.isOKMessage(errorMessage));
 
-        Document helloMessage = XmlFileLoader.xmlFileToDocument("netconfMessages/client_hello.xml");
-        Collection<String> caps =
-                NetconfMessageUtil.extractCapabilitiesFromHello(new NetconfMessage(helloMessage).getDocument());
+        final Collection<String> caps = NetconfMessageUtil.extractCapabilitiesFromHello(
+            XmlFileLoader.xmlFileToDocument("netconfMessages/client_hello.xml"));
         assertTrue(caps.contains("urn:ietf:params:netconf:base:1.0"));
         assertTrue(caps.contains("urn:ietf:params:netconf:base:1.1"));
     }
index 2d7737af27bdaf85c601dc73cb040810f734a182..bbe27f126d53ed5444f3e5d08e3b6d030bc9c3e0 100644 (file)
@@ -9,11 +9,11 @@ package org.opendaylight.netconf.util.test;
 
 import static java.util.Objects.requireNonNull;
 
-import com.google.common.io.ByteSource;
 import java.io.IOException;
 import java.io.InputStream;
 import java.nio.charset.StandardCharsets;
 import javax.xml.parsers.ParserConfigurationException;
+import org.eclipse.jdt.annotation.NonNull;
 import org.opendaylight.netconf.api.NetconfMessage;
 import org.opendaylight.netconf.api.xml.XmlUtil;
 import org.w3c.dom.Document;
@@ -22,7 +22,7 @@ import org.xml.sax.SAXException;
 
 public final class XmlFileLoader {
     private XmlFileLoader() {
-
+        // Hidden on purpose
     }
 
     public static NetconfMessage xmlFileToNetconfMessage(final String fileName) throws IOException, SAXException,
@@ -42,27 +42,19 @@ public final class XmlFileLoader {
 
     public static Document xmlFileToDocument(final String fileName) throws IOException, SAXException,
             ParserConfigurationException {
-        try (InputStream resourceAsStream = XmlFileLoader.class.getClassLoader().getResourceAsStream(fileName)) {
-            requireNonNull(resourceAsStream, fileName);
-            final Document doc = XmlUtil.readXmlToDocument(resourceAsStream);
-            return doc;
+        try (InputStream resource = getResourceAsStream(fileName)) {
+            return XmlUtil.readXmlToDocument(resource);
         }
     }
 
     public static String fileToString(final String fileName) throws IOException {
-        try (InputStream resourceAsStream = XmlFileLoader.class.getClassLoader().getResourceAsStream(fileName)) {
-            requireNonNull(resourceAsStream);
-            return new ByteSource() {
-                @Override
-                public InputStream openStream() {
-                    return resourceAsStream;
-                }
-            }.asCharSource(StandardCharsets.UTF_8).read();
-
+        try (InputStream resource = getResourceAsStream(fileName)) {
+            return new String(resource.readAllBytes(), StandardCharsets.UTF_8);
         }
     }
 
-    public static InputStream getResourceAsStream(final String fileName) {
-        return XmlFileLoader.class.getClassLoader().getResourceAsStream(fileName);
+    public static @NonNull InputStream getResourceAsStream(final String fileName) {
+        final String resourceName = requireNonNull(fileName);
+        return requireNonNull(XmlFileLoader.class.getClassLoader().getResourceAsStream(resourceName), resourceName);
     }
 }