I have a function that takes in an ODataObject which contains some XML from an event fired from a message bus. I'm writing test cases to check how my function handles XML coming from the message bus that consists of only this: <?xml version="1.0" encoding="UTF-8"?>.
This is how I'm creating the DataObject that gets passed in to my other test cases
final Document document = DOMUtil.parse(TestEventXML)
final DataObject dataObject = new ODataObject(document.getDocumentElement())
myListener.onMessage(dataObject)
However, passing in the above string as TestEventXML returns this error:
java.lang.NullPointerException: Cannot invoke method getDocumentElement() on null object
because calling DOMUtil.parse() on an incorrectly formatted XML string will return null.
So, I need some kind of work around that would allow me to create a org.w3c.dom.Node that I can pass into the constructor for ODataObject out of an XML string that I have created. Any ideas, suggestions, etc would be greatly appreciated.
EDIT - Summary of question
To clarify, I know that the String I'm trying to pass in is just the header of an XML file with no content. But sometimes that's what comes through the message bus, so I need to test that my function handles it correctly. My function does have code in place to handle malformed XML, so my question is not how to check if XML is malformed. The problem is that I need to create a DataObject to test that I am handling it correctly. But I cannot create a DataObject using malformed XML because parse returns null.
EDIT 2 - How I'm currently handling Malformed/Null XML
In my onMessage function, I call the toString method on the incoming DataObject, which returns a string of the XML in the DataObject. Then, to create a groovy.util.Node object, I try new XmlParser().parseText(xmlString) and catch all SAXParseExcetions, which result from the XML being malformed or null. The onMessage function then just returns, skipping all the parsing code in the rest of the function.
Addendum - Another question that might answer my original question
org.w3c.dom.Node is an interface, so I can't just do new 0DataObject(new Node()). Is there a way to create a Node and set the correct attribute to be my XML String?
Please comment with any questions you have.
ODataObjectthat was formed from nearly-empty XML? Maybe you want to see how theODataObjectis constructed and focus on finding a way to construct theODataObjectmanually, instead of theNode. (Maybe I led you down the wrong path in my answer by focusing on theNode-- sorry!)MockForclass. I make a mock of theODataObject, pass in a properly formatted XML and then intercept thetoStringfunction to return the malformed XML. Not pretty, but it works for now.