diff options
Diffstat (limited to 'src/test/java/at/gv/egiz/moazs/SOAPUtilsTest.java')
-rw-r--r-- | src/test/java/at/gv/egiz/moazs/SOAPUtilsTest.java | 143 |
1 files changed, 127 insertions, 16 deletions
diff --git a/src/test/java/at/gv/egiz/moazs/SOAPUtilsTest.java b/src/test/java/at/gv/egiz/moazs/SOAPUtilsTest.java index 31aa197..ff0060e 100644 --- a/src/test/java/at/gv/egiz/moazs/SOAPUtilsTest.java +++ b/src/test/java/at/gv/egiz/moazs/SOAPUtilsTest.java @@ -4,10 +4,8 @@ import at.gv.egiz.moazs.scheme.SOAPUtils; import org.junit.Before; import org.junit.Test; import org.w3c.dom.Element; -import org.xml.sax.SAXException; +import org.w3c.dom.Node; -import javax.xml.parsers.ParserConfigurationException; -import java.io.IOException; import java.nio.charset.StandardCharsets; import static org.assertj.core.api.Assertions.assertThat; @@ -25,39 +23,152 @@ public class SOAPUtilsTest { "est-id</ZSDeliveryID><AppDeliveryID>valid-delivery-request-id</AppDeliveryID><GZ>12345</GZ>" + "</PartialSuccess></DeliveryResponse></soap:Body></soap:Envelope>"; + + public static final String FORMATTED_MESSAGE = + "<soapenv:Envelope\n" + + " xmlns:soapenv=\"http://schemas.xmlsoap.org/soap/envelope/\"\n" + + " xmlns:msg=\"http://reference.e-government.gv.at/namespace/zustellung/msg/phase2/20181206#\">\n" + + " <soapenv:Body>\n" + + " <msg:DeliveryNotification>\n" + + " <msg:AppDeliveryID>formatted-adid</msg:AppDeliveryID>\n" + + " </msg:DeliveryNotification>\n" + + " </soapenv:Body>\n" + + "</soapenv:Envelope>\n"; + + public static final String CLUTTERED_MESSAGE = + "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n" + + "<soapenv:Envelope " + + " xmlns:soapenv=\"http://schemas.xmlsoap.org/soap/envelope/\"\n " + + " xmlns:msg=\"http://reference.e-government.gv.at/namespace/zustellung/msg/phase2/20181206#\">\n" + + " <soapenv:Body unexpected-attribute=\"unexpectedvalue\"> \n" + + " <msg:DeliveryNotification>\n" + + " <msg:AppDeliveryID attribute=\"some-value\"><element-that-got-lost />cluttered-adid</msg:AppDeliveryID>\n" + + " </msg:DeliveryNotification>\n" + + " </soapenv:Body>\n" + + "</soapenv:Envelope>\n"; + + private static final String MISSING_BODY_MESSAGE = + "<soap:Envelope xmlns:soap=\"http://schemas.xmlsoap.org/soap/envelope/\"><NotABodyTag></NotABodyTag></soap:Envelope>"; + + private static final String EMPTY_BODY_MESSAGE = + "<soap:Envelope xmlns:soap=\"http://schemas.xmlsoap.org/soap/envelope/\"><soap:Body> </soap:Body></soap:Envelope>"; + + public static final String EMPTY_APP_DELIVERY_ID_MESSAGE = + "<soapenv:Envelope\n" + + " xmlns:soapenv=\"http://schemas.xmlsoap.org/soap/envelope/\"\n" + + " xmlns:msg=\"http://reference.e-government.gv.at/namespace/zustellung/msg/phase2/20181206#\">\n" + + " <soapenv:Body>\n" + + " <msg:DeliveryNotification>\n" + + " <msg:AppDeliveryID />\n" + + " </msg:DeliveryNotification>\n" + + " </soapenv:Body>\n" + + "</soapenv:Envelope>\n"; + + public static final String BLANK_APP_DELIVERY_ID_MESSAGE = + "<soapenv:Envelope\n" + + " xmlns:soapenv=\"http://schemas.xmlsoap.org/soap/envelope/\"\n" + + " xmlns:msg=\"http://reference.e-government.gv.at/namespace/zustellung/msg/phase2/20181206#\">\n" + + " <soapenv:Body>\n" + + " <msg:DeliveryNotification>\n" + + " <msg:AppDeliveryID> </msg:AppDeliveryID>\n" + + " </msg:DeliveryNotification>\n" + + " </soapenv:Body>\n" + + "</soapenv:Envelope>\n"; + @Before public void setup() { utils = new SOAPUtils(); } @Test - public void toDom() throws ParserConfigurationException, SAXException, IOException { - byte[] bytes = SOAP_MESSAGE.getBytes(StandardCharsets.UTF_8); + public void canParseSoapMessage() throws Exception { + parseAndCheckLocalName(SOAP_MESSAGE); + } + + @Test + public void canParseFormattedSoapMessage() throws Exception { + parseAndCheckLocalName(FORMATTED_MESSAGE); + } + + @Test + public void canParseClutteredSoapMessage() throws Exception { + parseAndCheckLocalName(CLUTTERED_MESSAGE); + } + private void parseAndCheckLocalName(String text) throws Exception { + byte[] bytes = text.getBytes(StandardCharsets.UTF_8); Element root = utils.toDOM(bytes); + assertThat(root.getLocalName()).isEqualTo("Envelope"); + } + + @Test + public void canUnwrapSoapMessage() throws Exception { + getFirstChildAndCheckLocalName(SOAP_MESSAGE, "DeliveryResponse"); + } - assertThat(root.getTagName()).isEqualTo("soap:Envelope"); + @Test + public void canUnwrapFormattedSoapMessage() throws Exception { + getFirstChildAndCheckLocalName(FORMATTED_MESSAGE, "DeliveryNotification"); } @Test - public void unwrapSoapEnvelope() throws ParserConfigurationException, SAXException, IOException { - byte[] bytes = SOAP_MESSAGE.getBytes(StandardCharsets.UTF_8); + public void canUnwrapClutteredSoapMessage() throws Exception { + getFirstChildAndCheckLocalName(CLUTTERED_MESSAGE, "DeliveryNotification"); + } + + @Test(expected=MoaZSException.class) + public void failUnwrapMissingBody() throws Exception { + getFirstChildAndCheckLocalName(MISSING_BODY_MESSAGE, ""); + } + + @Test(expected=MoaZSException.class) + public void failUnwrapEmptyBody() throws Exception { + getFirstChildAndCheckLocalName(EMPTY_BODY_MESSAGE, ""); + } + + private void getFirstChildAndCheckLocalName(String message, String expectedLocalNameOfChild) throws Exception { + byte[] bytes = message.getBytes(StandardCharsets.UTF_8); Element soapRoot = utils.toDOM(bytes); + Node child = utils.getChildElementOfSoapBody(soapRoot); + var actualLocalName = child.getLocalName(); + assertThat(actualLocalName).isEqualTo(expectedLocalNameOfChild); + } - byte[] unwrappedMessage = utils.unwrapSoapEnvelope(soapRoot); + @Test + public void getAppDeliveryIDFromSoapMessage() throws Exception { + getAppDeliveryIDAndCheck(SOAP_MESSAGE, "valid-delivery-request-id"); + } - Element deliveryResponseRoot = utils.toDOM(unwrappedMessage); - assertThat(deliveryResponseRoot.getTagName()).isEqualTo("DeliveryResponse"); + @Test + public void getAppDeliveryIDFromFormattedMessage() throws Exception { + getAppDeliveryIDAndCheck(FORMATTED_MESSAGE, "formatted-adid"); } @Test - public void getAppDeliveryID() throws ParserConfigurationException, SAXException, IOException { - byte[] bytes = SOAP_MESSAGE.getBytes(StandardCharsets.UTF_8); - Element soapRoot = utils.toDOM(bytes); + public void getAppDeliveryIDFromClutteredMessage() throws Exception { + getAppDeliveryIDAndCheck(CLUTTERED_MESSAGE, "cluttered-adid"); + } + + @Test(expected = MoaZSException.class) + public void failToRetrieveAppDeliveryIDWhenBodyIsMissing() throws Exception { + getAppDeliveryIDAndCheck(MISSING_BODY_MESSAGE, ""); + } - String appDeliveryID = utils.getAppDeliveryIDFrom(soapRoot); + @Test(expected = MoaZSException.class) + public void failToRetrieveEmptyAppDeliveryID() throws Exception { + getAppDeliveryIDAndCheck(EMPTY_APP_DELIVERY_ID_MESSAGE, ""); + } + + @Test(expected = MoaZSException.class) + public void failToRetrieveBlankAppDeliveryID() throws Exception { + getAppDeliveryIDAndCheck(BLANK_APP_DELIVERY_ID_MESSAGE, ""); + } - assertThat(appDeliveryID).isEqualTo("valid-delivery-request-id"); + private void getAppDeliveryIDAndCheck(String message, String expectedValue) throws Exception { + byte[] bytes = message.getBytes(StandardCharsets.UTF_8); + Element soapRoot = utils.toDOM(bytes); + String actualAppDeliveryID = utils.getAppDeliveryIDFrom(soapRoot); + assertThat(actualAppDeliveryID).isEqualTo(expectedValue); } } |