From d98759f6016df66a81e6097ebe3c18011cb5b964 Mon Sep 17 00:00:00 2001 From: Robert Varga Date: Tue, 24 Aug 2021 01:35:44 +0200 Subject: [PATCH] Move SendErrorExceptionUtil 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 --- .../impl/NetconfServerSessionListener.java | 1 - .../netconf/impl}/SendErrorExceptionUtil.java | 3 +- .../util/DeserializerExceptionHandler.java | 2 +- .../impl}/SendErrorExceptionUtilTest.java | 45 +++++++++++-------- .../src/test/resources/messages}/rpc.xml | 0 .../src/test/resources/messages}/rpc_ns.xml | 0 .../mapping/AbstractNetconfOperationTest.java | 15 +++---- .../util/messages/NetconfMessageUtilTest.java | 22 +++++---- .../netconf/util/test/XmlFileLoader.java | 26 ++++------- 9 files changed, 54 insertions(+), 60 deletions(-) rename netconf/{netconf-util/src/main/java/org/opendaylight/netconf/util/messages => netconf-impl/src/main/java/org/opendaylight/netconf/impl}/SendErrorExceptionUtil.java (99%) rename netconf/{netconf-util/src/test/java/org/opendaylight/netconf/util/messages => netconf-impl/src/test/java/org/opendaylight/netconf/impl}/SendErrorExceptionUtilTest.java (66%) rename netconf/{netconf-util/src/test/resources/netconfMessages => netconf-impl/src/test/resources/messages}/rpc.xml (100%) rename netconf/{netconf-util/src/test/resources/netconfMessages => netconf-impl/src/test/resources/messages}/rpc_ns.xml (100%) diff --git a/netconf/netconf-impl/src/main/java/org/opendaylight/netconf/impl/NetconfServerSessionListener.java b/netconf/netconf-impl/src/main/java/org/opendaylight/netconf/impl/NetconfServerSessionListener.java index c641d4158d..141edb98d3 100644 --- a/netconf/netconf-impl/src/main/java/org/opendaylight/netconf/impl/NetconfServerSessionListener.java +++ b/netconf/netconf-impl/src/main/java/org/opendaylight/netconf/impl/NetconfServerSessionListener.java @@ -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; diff --git a/netconf/netconf-util/src/main/java/org/opendaylight/netconf/util/messages/SendErrorExceptionUtil.java b/netconf/netconf-impl/src/main/java/org/opendaylight/netconf/impl/SendErrorExceptionUtil.java 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 4f93674224..b119c3738b 100644 --- a/netconf/netconf-util/src/main/java/org/opendaylight/netconf/util/messages/SendErrorExceptionUtil.java +++ b/netconf/netconf-impl/src/main/java/org/opendaylight/netconf/impl/SendErrorExceptionUtil.java @@ -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; diff --git a/netconf/netconf-impl/src/main/java/org/opendaylight/netconf/impl/util/DeserializerExceptionHandler.java b/netconf/netconf-impl/src/main/java/org/opendaylight/netconf/impl/util/DeserializerExceptionHandler.java index 4c7c0b8964..6deea762f4 100644 --- a/netconf/netconf-impl/src/main/java/org/opendaylight/netconf/impl/util/DeserializerExceptionHandler.java +++ b/netconf/netconf-impl/src/main/java/org/opendaylight/netconf/impl/util/DeserializerExceptionHandler.java @@ -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; diff --git a/netconf/netconf-util/src/test/java/org/opendaylight/netconf/util/messages/SendErrorExceptionUtilTest.java b/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 b5421384d6..5dbd480f60 100644 --- a/netconf/netconf-util/src/test/java/org/opendaylight/netconf/util/messages/SendErrorExceptionUtilTest.java +++ b/netconf/netconf-impl/src/test/java/org/opendaylight/netconf/impl/SendErrorExceptionUtilTest.java @@ -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 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)); + } + } } diff --git a/netconf/netconf-util/src/test/resources/netconfMessages/rpc.xml b/netconf/netconf-impl/src/test/resources/messages/rpc.xml similarity index 100% rename from netconf/netconf-util/src/test/resources/netconfMessages/rpc.xml rename to netconf/netconf-impl/src/test/resources/messages/rpc.xml diff --git a/netconf/netconf-util/src/test/resources/netconfMessages/rpc_ns.xml b/netconf/netconf-impl/src/test/resources/messages/rpc_ns.xml similarity index 100% rename from netconf/netconf-util/src/test/resources/netconfMessages/rpc_ns.xml rename to netconf/netconf-impl/src/test/resources/messages/rpc_ns.xml diff --git a/netconf/netconf-util/src/test/java/org/opendaylight/netconf/util/mapping/AbstractNetconfOperationTest.java b/netconf/netconf-util/src/test/java/org/opendaylight/netconf/util/mapping/AbstractNetconfOperationTest.java index ad3ab9eaa0..5420432f01 100644 --- a/netconf/netconf-util/src/test/java/org/opendaylight/netconf/util/mapping/AbstractNetconfOperationTest.java +++ b/netconf/netconf-util/src/test/java/org/opendaylight/netconf/util/mapping/AbstractNetconfOperationTest.java @@ -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(""); } 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); } diff --git a/netconf/netconf-util/src/test/java/org/opendaylight/netconf/util/messages/NetconfMessageUtilTest.java b/netconf/netconf-util/src/test/java/org/opendaylight/netconf/util/messages/NetconfMessageUtilTest.java index 53d2c585ef..aa953fe77b 100644 --- a/netconf/netconf-util/src/test/java/org/opendaylight/netconf/util/messages/NetconfMessageUtilTest.java +++ b/netconf/netconf-util/src/test/java/org/opendaylight/netconf/util/messages/NetconfMessageUtilTest.java @@ -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 caps = - NetconfMessageUtil.extractCapabilitiesFromHello(new NetconfMessage(helloMessage).getDocument()); + final Collection 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")); } diff --git a/netconf/netconf-util/src/test/java/org/opendaylight/netconf/util/test/XmlFileLoader.java b/netconf/netconf-util/src/test/java/org/opendaylight/netconf/util/test/XmlFileLoader.java index 2d7737af27..bbe27f126d 100644 --- a/netconf/netconf-util/src/test/java/org/opendaylight/netconf/util/test/XmlFileLoader.java +++ b/netconf/netconf-util/src/test/java/org/opendaylight/netconf/util/test/XmlFileLoader.java @@ -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); } } -- 2.36.6