From 069c06f5a11aabc90d8255a668e5b46df1a9fdb3 Mon Sep 17 00:00:00 2001 From: Robert Varga Date: Fri, 29 Sep 2023 23:35:29 +0200 Subject: [PATCH] Migrate netconf-netty-util to xmlunit-core Use xmlunit-core instead of xmlunit-legacy and modernize the code. Change-Id: Ia9f9fba4e9458b9a6465c2f3fe2ab0d304e47bbc Signed-off-by: Robert Varga --- netconf/netconf-netty-util/pom.xml | 10 +-- .../handler/NetconfEXIHandlersTest.java | 38 ++++----- .../exi/NetconfStartExiMessageTest.java | 85 ++++++++----------- 3 files changed, 58 insertions(+), 75 deletions(-) diff --git a/netconf/netconf-netty-util/pom.xml b/netconf/netconf-netty-util/pom.xml index 2d80ff4bce..bba3b3f70c 100644 --- a/netconf/netconf-netty-util/pom.xml +++ b/netconf/netconf-netty-util/pom.xml @@ -90,17 +90,17 @@ - - org.xmlunit - xmlunit-legacy - org.opendaylight.yangtools mockito-configuration - ${project.groupId} + org.opendaylight.netconf netconf-test-util + + org.xmlunit + xmlunit-core + diff --git a/netconf/netconf-netty-util/src/test/java/org/opendaylight/netconf/nettyutil/handler/NetconfEXIHandlersTest.java b/netconf/netconf-netty-util/src/test/java/org/opendaylight/netconf/nettyutil/handler/NetconfEXIHandlersTest.java index aaa30bb6c0..2bdca15596 100644 --- a/netconf/netconf-netty-util/src/test/java/org/opendaylight/netconf/nettyutil/handler/NetconfEXIHandlersTest.java +++ b/netconf/netconf-netty-util/src/test/java/org/opendaylight/netconf/nettyutil/handler/NetconfEXIHandlersTest.java @@ -9,29 +9,24 @@ package org.opendaylight.netconf.nettyutil.handler; import static org.junit.Assert.assertArrayEquals; import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertFalse; -import io.netty.buffer.ByteBuf; import io.netty.buffer.Unpooled; import java.io.ByteArrayOutputStream; -import java.io.IOException; import java.util.ArrayList; import java.util.Arrays; -import java.util.List; -import javax.xml.transform.TransformerException; import javax.xml.transform.dom.DOMSource; import javax.xml.transform.sax.SAXResult; -import org.custommonkey.xmlunit.XMLUnit; import org.junit.Before; import org.junit.Test; import org.opendaylight.netconf.api.messages.NetconfMessage; import org.opendaylight.netconf.api.xml.XmlUtil; import org.opendaylight.netconf.nettyutil.handler.exi.EXIParameters; -import org.opendaylight.netconf.shaded.exificient.core.exceptions.EXIException; -import org.opendaylight.netconf.shaded.exificient.main.api.sax.SAXEncoder; +import org.xmlunit.builder.DiffBuilder; public class NetconfEXIHandlersTest { - private final String msgAsString = ""; + private NetconfMessageToEXIEncoder netconfMessageToEXIEncoder; private NetconfEXIToMessageDecoder netconfEXIToMessageDecoder; private NetconfMessage msg; @@ -39,27 +34,26 @@ public class NetconfEXIHandlersTest { @Before public void setUp() throws Exception { - final NetconfEXICodec codec = NetconfEXICodec.forParameters(EXIParameters.empty()); + final var codec = NetconfEXICodec.forParameters(EXIParameters.empty()); netconfMessageToEXIEncoder = NetconfMessageToEXIEncoder.create(codec); netconfEXIToMessageDecoder = NetconfEXIToMessageDecoder.create(codec); msg = new NetconfMessage(XmlUtil.readXmlToDocument(msgAsString)); - this.msgAsExi = msgToExi(msg, codec); + msgAsExi = msgToExi(msg, codec); } - private static byte[] msgToExi(final NetconfMessage msg, final NetconfEXICodec codec) - throws IOException, EXIException, TransformerException { - final ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream(); - final SAXEncoder encoder = codec.getWriter(); - encoder.setOutputStream(byteArrayOutputStream); + private static byte[] msgToExi(final NetconfMessage msg, final NetconfEXICodec codec) throws Exception { + final var bos = new ByteArrayOutputStream(); + final var encoder = codec.getWriter(); + encoder.setOutputStream(bos); ThreadLocalTransformers.getDefaultTransformer().transform(new DOMSource(msg.getDocument()), - new SAXResult(encoder)); - return byteArrayOutputStream.toByteArray(); + new SAXResult(encoder)); + return bos.toByteArray(); } @Test public void testEncodeDecode() throws Exception { - final ByteBuf buffer = Unpooled.buffer(); + final var buffer = Unpooled.buffer(); netconfMessageToEXIEncoder.encode(null, msg, buffer); final int exiLength = msgAsExi.length; // array from buffer is cca 256 n length, compare only subarray @@ -70,9 +64,13 @@ public class NetconfEXIHandlersTest { assertEquals((byte)0, buffer.array()[i]); } - final List out = new ArrayList<>(); + final var out = new ArrayList<>(); netconfEXIToMessageDecoder.decode(null, buffer, out); - XMLUnit.compareXML(msg.getDocument(), ((NetconfMessage) out.get(0)).getDocument()); + final var diff = DiffBuilder.compare(msg.getDocument()) + .withTest(((NetconfMessage) out.get(0)).getDocument()) + .checkForIdentical() + .build(); + assertFalse(diff.toString(), diff.hasDifferences()); } } diff --git a/netconf/netconf-netty-util/src/test/java/org/opendaylight/netconf/nettyutil/handler/exi/NetconfStartExiMessageTest.java b/netconf/netconf-netty-util/src/test/java/org/opendaylight/netconf/nettyutil/handler/exi/NetconfStartExiMessageTest.java index c3c6e73cdc..68ae7947c5 100644 --- a/netconf/netconf-netty-util/src/test/java/org/opendaylight/netconf/nettyutil/handler/exi/NetconfStartExiMessageTest.java +++ b/netconf/netconf-netty-util/src/test/java/org/opendaylight/netconf/nettyutil/handler/exi/NetconfStartExiMessageTest.java @@ -5,74 +5,59 @@ * 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.nettyutil.handler.exi; -import static org.junit.Assert.assertTrue; +import static org.junit.Assert.assertFalse; -import java.util.Arrays; -import org.custommonkey.xmlunit.Diff; -import org.custommonkey.xmlunit.XMLUnit; import org.junit.Test; -import org.junit.runner.RunWith; -import org.junit.runners.Parameterized; -import org.opendaylight.netconf.api.messages.RpcMessage; import org.opendaylight.netconf.shaded.exificient.core.CodingMode; import org.opendaylight.netconf.shaded.exificient.core.FidelityOptions; +import org.xmlunit.builder.DiffBuilder; -@RunWith(Parameterized.class) public class NetconfStartExiMessageTest { + @Test + public void testCreateEmpty() { + assertCreate(""" + + + bit-packed + + """, EXIParameters.empty()); + } - @Parameterized.Parameters - public static Iterable data() throws Exception { - final String noChangeXml = "\n" - + "\n" - + "bit-packed\n" - + "\n" - + ""; - - - final String fullOptionsXml = "\n" - + "\n" - + "byte-aligned\n" - + "\n" - + "\n" - + "\n" - + "\n" - + "\n" - + "\n" - + "\n" - + "\n" - + ""; - - final FidelityOptions fullOptions = FidelityOptions.createDefault(); + @Test + public void testCreateFull() throws Exception { + final var fullOptions = FidelityOptions.createDefault(); fullOptions.setFidelity(FidelityOptions.FEATURE_LEXICAL_VALUE, true); fullOptions.setFidelity(FidelityOptions.FEATURE_DTD, true); fullOptions.setFidelity(FidelityOptions.FEATURE_COMMENT, true); fullOptions.setFidelity(FidelityOptions.FEATURE_PREFIX, true); fullOptions.setFidelity(FidelityOptions.FEATURE_PI, true); - return Arrays.asList(new Object[][]{ - {noChangeXml, EXIParameters.empty()}, - {fullOptionsXml, new EXIParameters(CodingMode.BYTE_PACKED, fullOptions)}, - }); + assertCreate(""" + + + byte-aligned + + + + + + + + + """, new EXIParameters(CodingMode.BYTE_PACKED, fullOptions)); } - private final String controlXml; - private final EXIParameters exiOptions; + private static void assertCreate(final String control, final EXIParameters exiOptions) { + final var startExiMessage = NetconfStartExiMessageProvider.create(exiOptions, "id"); - public NetconfStartExiMessageTest(final String controlXml, final EXIParameters exiOptions) { - this.controlXml = controlXml; - this.exiOptions = exiOptions; + final var diff = DiffBuilder.compare(control) + .withTest(startExiMessage.getDocument()) + .ignoreWhitespace() + .checkForIdentical() + .build(); + assertFalse(diff.toString(), diff.hasDifferences()); } - @Test - public void testCreate() throws Exception { - final RpcMessage startExiMessage = NetconfStartExiMessageProvider.create(exiOptions, "id"); - - XMLUnit.setIgnoreWhitespace(true); - XMLUnit.setIgnoreAttributeOrder(true); - final Diff diff = XMLUnit.compareXML(XMLUnit.buildControlDocument(controlXml), startExiMessage.getDocument()); - assertTrue(diff.toString(), diff.similar()); - } } \ No newline at end of file -- 2.36.6