Skip to content

Commit

Permalink
Merge pull request #215 from marklogic-community/feature/read-xml-nam…
Browse files Browse the repository at this point in the history
…espaces

#211 Added method to read XML document with namespaces
  • Loading branch information
rjrudin authored Oct 21, 2024
2 parents 443db79 + 663acd5 commit 3f7b82b
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;

/**
Expand Down Expand Up @@ -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.
*
Expand All @@ -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<Namespace> 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.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,33 +4,44 @@
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("" +
"<message xmlns='org:example'>" +
"<color important='true'>red</color>" +
"<color>blue</color>" +
"<size>medium</size>" +
"<parent><kid>hello</kid></parent>" +
"</message>"));
}

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"));
Expand All @@ -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");
}
}

0 comments on commit 3f7b82b

Please sign in to comment.