Switch to using java.util.Base64
[yangtools.git] / yang / yang-data-codec-xml / src / test / java / org / opendaylight / yangtools / yang / data / codec / xml / Bug5446Test.java
index e63ab7db07356bfda63870182702cbe66ddaa18f..f9f396ca676f6c33efdd58ff877116a0a1cb5f38 100644 (file)
@@ -5,18 +5,16 @@
  * 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.yangtools.yang.data.codec.xml;
 
 import static java.util.Objects.requireNonNull;
 
-import com.google.common.io.BaseEncoding;
 import java.io.IOException;
 import java.io.InputStream;
 import java.io.StringWriter;
 import java.net.URI;
+import java.util.Base64;
 import java.util.Optional;
-import javax.xml.stream.XMLOutputFactory;
 import javax.xml.stream.XMLStreamException;
 import javax.xml.stream.XMLStreamWriter;
 import javax.xml.transform.OutputKeys;
@@ -33,6 +31,7 @@ import org.junit.Test;
 import org.opendaylight.yangtools.util.xml.UntrustedXML;
 import org.opendaylight.yangtools.yang.common.QName;
 import org.opendaylight.yangtools.yang.common.QNameModule;
+import org.opendaylight.yangtools.yang.common.Revision;
 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.NodeIdentifier;
 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.PathArgument;
 import org.opendaylight.yangtools.yang.data.api.schema.ContainerNode;
@@ -50,43 +49,28 @@ import org.w3c.dom.Node;
 import org.xml.sax.SAXException;
 
 public class Bug5446Test extends XMLTestCase {
-    private static final XMLOutputFactory XML_FACTORY;
-
-    static {
-        XML_FACTORY = XMLOutputFactory.newFactory();
-        XML_FACTORY.setProperty(XMLOutputFactory.IS_REPAIRING_NAMESPACES, Boolean.FALSE);
-    }
-
-    private final QNameModule fooModuleQName;
-    private final QName rootQName;
-    private final QName ipAddressQName;
-    private final SchemaContext schemaContext;
-
-    public Bug5446Test() throws Exception {
-        fooModuleQName = QNameModule.create(new URI("foo"), QName.parseRevision("2015-11-05"));
-        rootQName = QName.create(fooModuleQName, "root");
-        ipAddressQName = QName.create(fooModuleQName, "ip-address");
-
-        schemaContext = YangParserTestUtils.parseYangResource("/bug5446/yang/foo.yang");
-    }
+    private static final QNameModule FOO_MODULE = QNameModule.create(URI.create("foo"), Revision.of("2015-11-05"));
+    private static final QName ROOT_QNAME = QName.create(FOO_MODULE, "root");
+    private static final QName IP_ADDRESS_QNAME = QName.create(FOO_MODULE, "ip-address");
 
     @Test
     public void test() throws Exception {
+        final SchemaContext schemaContext = YangParserTestUtils.parseYangResource("/bug5446/yang/foo.yang");
         final Document doc = loadDocument("/bug5446/xml/foo.xml");
 
         final ContainerNode docNode = createDocNode();
 
-        Optional<DataContainerChild<? extends PathArgument, ?>> root = docNode.getChild(new NodeIdentifier(rootQName));
+        Optional<DataContainerChild<? extends PathArgument, ?>> root = docNode.getChild(new NodeIdentifier(ROOT_QNAME));
         assertTrue(root.orElse(null) instanceof ContainerNode);
 
         Optional<DataContainerChild<? extends PathArgument, ?>> child = ((ContainerNode) root.orElse(null))
-                .getChild(new NodeIdentifier(ipAddressQName));
+                .getChild(new NodeIdentifier(IP_ADDRESS_QNAME));
         assertTrue(child.orElse(null) instanceof LeafNode);
         LeafNode<?> ipAdress = (LeafNode<?>) child.get();
 
         Object value = ipAdress.getValue();
         assertTrue(value instanceof byte[]);
-        assertEquals("fwAAAQ==", BaseEncoding.base64().encode((byte[]) value));
+        assertEquals("fwAAAQ==", Base64.getEncoder().encodeToString((byte[]) value));
 
         DOMResult serializationResult = writeNormalizedNode(docNode, schemaContext);
         assertNotNull(serializationResult);
@@ -102,11 +86,11 @@ public class Bug5446Test extends XMLTestCase {
         assertXMLEqual(expectedXMLString, serializationResultXMLString);
     }
 
-    private ContainerNode createDocNode() {
-        LeafNode<byte[]> ipAddress = ImmutableNodes.leafNode(ipAddressQName, BaseEncoding.base64().decode("fwAAAQ=="));
-        ContainerNode root = ImmutableContainerNodeBuilder.create().withNodeIdentifier(new NodeIdentifier(rootQName))
+    private static ContainerNode createDocNode() {
+        LeafNode<byte[]> ipAddress = ImmutableNodes.leafNode(IP_ADDRESS_QNAME, Base64.getDecoder().decode("fwAAAQ=="));
+        ContainerNode root = ImmutableContainerNodeBuilder.create().withNodeIdentifier(new NodeIdentifier(ROOT_QNAME))
                 .withChild(ipAddress).build();
-        return ImmutableContainerNodeBuilder.create().withNodeIdentifier(new NodeIdentifier(rootQName)).withChild(root)
+        return ImmutableContainerNodeBuilder.create().withNodeIdentifier(new NodeIdentifier(ROOT_QNAME)).withChild(root)
                 .build();
     }
 
@@ -118,7 +102,7 @@ public class Bug5446Test extends XMLTestCase {
         NormalizedNodeStreamWriter normalizedNodeStreamWriter = null;
         XMLStreamWriter writer = null;
         try {
-            writer = XML_FACTORY.createXMLStreamWriter(result);
+            writer = TestFactories.DEFAULT_OUTPUT_FACTORY.createXMLStreamWriter(result);
             normalizedNodeStreamWriter = XMLStreamNormalizedNodeStreamWriter.create(writer, context);
             normalizedNodeWriter = NormalizedNodeWriter.forStreamWriter(normalizedNodeStreamWriter);