From 663acd51cc9f4ca175499e4f4ea43a61cc5d3afb Mon Sep 17 00:00:00 2001 From: Rob Rudin Date: Mon, 21 Oct 2024 09:32:41 -0400 Subject: [PATCH] #211 Added method to read XML document with namespaces Needed in a lot of our other repositories; avoids having to make an ugly array of namespaces every time. --- .../junit5/AbstractMarkLogicTest.java | 28 ++++++++++++++++ .../marklogic/junit5/spring/XmlNodeTest.java | 32 +++++++++++++++---- 2 files changed, 53 insertions(+), 7 deletions(-) diff --git a/marklogic-junit5/src/main/java/com/marklogic/junit5/AbstractMarkLogicTest.java b/marklogic-junit5/src/main/java/com/marklogic/junit5/AbstractMarkLogicTest.java index 04e3b5f..1d282a9 100644 --- a/marklogic-junit5/src/main/java/com/marklogic/junit5/AbstractMarkLogicTest.java +++ b/marklogic-junit5/src/main/java/com/marklogic/junit5/AbstractMarkLogicTest.java @@ -12,9 +12,11 @@ import com.marklogic.test.unit.TestModule; import com.marklogic.test.unit.TestResult; import com.marklogic.test.unit.TestSuiteResult; +import org.jdom2.Namespace; import org.junit.jupiter.api.Assertions; import org.junit.jupiter.api.BeforeEach; +import java.util.ArrayList; import java.util.List; /** @@ -74,6 +76,15 @@ protected XmlNode parseXml(String xml) { return new XmlNode(xml, getNamespaceProvider().getNamespaces()); } + /** + * Read an XML document without making any assertions on its collections. + * + * @since 1.5.0 + */ + protected XmlNode readXmlDocument(String uri) { + return readXmlDocument(uri, (String[]) null); + } + /** * Read the XML document at the given URI and return an XmlNode for making assertions on the contents of the XML. * @@ -89,6 +100,23 @@ protected XmlNode readXmlDocument(String uri, String... expectedCollections) { return new XmlNode(uri, xml, getNamespaceProvider().getNamespaces()); } + /** + * Read an XML document with the given namespaces included in the returned {@code XmlNode}. + * + * @since 1.5.0 + */ + protected XmlNode readXmlDocument(String uri, Namespace... namespaces) { + String xml = getDatabaseClient().newXMLDocumentManager().read(uri, new StringHandle()).get(); + List list = new ArrayList<>(); + for (Namespace ns : getNamespaceProvider().getNamespaces()) { + list.add(ns); + } + for (Namespace ns : namespaces) { + list.add(ns); + } + return new XmlNode(uri, xml, list.toArray(new Namespace[0])); + } + /** * Read the JSON document at the given URI and return a JsonNode. * diff --git a/marklogic-junit5/src/test/java/com/marklogic/junit5/spring/XmlNodeTest.java b/marklogic-junit5/src/test/java/com/marklogic/junit5/spring/XmlNodeTest.java index c11a0eb..621e4af 100644 --- a/marklogic-junit5/src/test/java/com/marklogic/junit5/spring/XmlNodeTest.java +++ b/marklogic-junit5/src/test/java/com/marklogic/junit5/spring/XmlNodeTest.java @@ -4,22 +4,30 @@ import com.marklogic.junit5.MarkLogicNamespaceProvider; import com.marklogic.junit5.NamespaceProvider; import com.marklogic.junit5.XmlNode; +import org.jdom2.Namespace; import org.junit.jupiter.api.Assertions; +import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; import static org.junit.jupiter.api.Assertions.assertEquals; import static org.junit.jupiter.api.Assertions.assertNotNull; -public class XmlNodeTest extends AbstractSpringMarkLogicTest { +class XmlNodeTest extends AbstractSpringMarkLogicTest { + + private static final String TEST_URI = "/test/1.xml"; + + private boolean useCustomNamespaceProvider = true; @Override protected NamespaceProvider getNamespaceProvider() { - return new MarkLogicNamespaceProvider("m", "org:example"); + return useCustomNamespaceProvider ? + new MarkLogicNamespaceProvider("m", "org:example") : + super.getNamespaceProvider(); } - @Test - public void test() { - getDatabaseClient().newXMLDocumentManager().write("/test/1.xml", + @BeforeEach + void setup() { + getDatabaseClient().newXMLDocumentManager().write(TEST_URI, new StringHandle("" + "" + "red" + @@ -27,10 +35,13 @@ public void test() { "medium" + "hello" + "")); + } - XmlNode xml = readXmlDocument("/test/1.xml"); + @Test + public void test() { + XmlNode xml = readXmlDocument(TEST_URI); - assertEquals("/test/1.xml", xml.getUri()); + assertEquals(TEST_URI, xml.getUri()); xml.assertElementValue("/m:message/m:size", "medium"); assertEquals("medium", xml.getElementValue("/m:message/m:size")); assertEquals("true", xml.getAttributeValue("/m:message/m:color[. = 'red']", "important")); @@ -47,4 +58,11 @@ public void test() { xml.prettyPrint(); assertNotNull(xml.getPrettyXml()); } + + @Test + void readWithNamespaces() { + useCustomNamespaceProvider = false; + XmlNode xml = readXmlDocument(TEST_URI, Namespace.getNamespace("m", "org:example")); + xml.assertElementValue("/m:message/m:size", "medium"); + } }