Migrate netconf-netty-util to xmlunit-core
[netconf.git] / netconf / netconf-netty-util / src / test / java / org / opendaylight / netconf / nettyutil / handler / NetconfEXIHandlersTest.java
index b1b6e6d56ab3fea5a3554c7e3614cf034371c383..2bdca15596c5da64ebcd7ede52f01bd024add39e 100644 (file)
@@ -5,34 +5,28 @@
  * 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;
 
 import static org.junit.Assert.assertArrayEquals;
 import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertFalse;
 
-import com.google.common.collect.Lists;
-import io.netty.buffer.ByteBuf;
 import io.netty.buffer.Unpooled;
-import java.io.ByteArrayInputStream;
 import java.io.ByteArrayOutputStream;
-import java.io.IOException;
+import java.util.ArrayList;
 import java.util.Arrays;
-import java.util.List;
-import org.custommonkey.xmlunit.XMLUnit;
+import javax.xml.transform.dom.DOMSource;
+import javax.xml.transform.sax.SAXResult;
 import org.junit.Before;
 import org.junit.Test;
-import org.opendaylight.controller.config.util.xml.XmlUtil;
-import org.opendaylight.netconf.api.NetconfMessage;
-import org.openexi.proc.common.EXIOptions;
-import org.openexi.proc.common.EXIOptionsException;
-import org.openexi.sax.Transmogrifier;
-import org.openexi.sax.TransmogrifierException;
-import org.xml.sax.InputSource;
+import org.opendaylight.netconf.api.messages.NetconfMessage;
+import org.opendaylight.netconf.api.xml.XmlUtil;
+import org.opendaylight.netconf.nettyutil.handler.exi.EXIParameters;
+import org.xmlunit.builder.DiffBuilder;
 
 public class NetconfEXIHandlersTest {
-
     private final String msgAsString = "<netconf-message/>";
+
     private NetconfMessageToEXIEncoder netconfMessageToEXIEncoder;
     private NetconfEXIToMessageDecoder netconfEXIToMessageDecoder;
     private NetconfMessage msg;
@@ -40,26 +34,26 @@ public class NetconfEXIHandlersTest {
 
     @Before
     public void setUp() throws Exception {
-        final NetconfEXICodec codec = new NetconfEXICodec(new EXIOptions());
+        final var codec = NetconfEXICodec.forParameters(EXIParameters.empty());
         netconfMessageToEXIEncoder = NetconfMessageToEXIEncoder.create(codec);
         netconfEXIToMessageDecoder = NetconfEXIToMessageDecoder.create(codec);
 
         msg = new NetconfMessage(XmlUtil.readXmlToDocument(msgAsString));
-        this.msgAsExi = msgToExi(msgAsString, codec);
+        msgAsExi = msgToExi(msg, codec);
     }
 
-    private static byte[] msgToExi(final String msgAsString,final NetconfEXICodec codec)
-            throws EXIOptionsException, TransmogrifierException, IOException {
-        final ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
-        final Transmogrifier transmogrifier = codec.getTransmogrifier();
-        transmogrifier.setOutputStream(byteArrayOutputStream);
-        transmogrifier.encode(new InputSource(new ByteArrayInputStream(msgAsString.getBytes())));
-        return byteArrayOutputStream.toByteArray();
+    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 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<Object> out = Lists.newArrayList();
+        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());
     }
-}
\ No newline at end of file
+}