package at.gv.egiz.moazs;
import at.gv.egiz.moazs.scheme.SOAPUtils;
import org.junit.Before;
import org.junit.Test;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import java.nio.charset.StandardCharsets;
import static org.assertj.core.api.Assertions.assertThat;
public class SOAPUtilsTest {
SOAPUtils utils;
private static final String SOAP_MESSAGE =
"" +
"https://testzustellsystem.egiz.gv.atzs-valid-delivery-requ" +
"est-idvalid-delivery-request-id12345" +
"";
public static final String FORMATTED_MESSAGE =
"\n" +
" \n" +
" \n" +
" formatted-adid\n" +
" \n" +
" \n" +
"\n";
public static final String CLUTTERED_MESSAGE =
"\n" +
"\n" +
" \n" +
" \n" +
" cluttered-adid\n" +
" \n" +
" \n" +
"\n";
private static final String MISSING_BODY_MESSAGE =
"";
private static final String EMPTY_BODY_MESSAGE =
" ";
public static final String EMPTY_APP_DELIVERY_ID_MESSAGE =
"\n" +
" \n" +
" \n" +
" \n" +
" \n" +
" \n" +
"\n";
public static final String BLANK_APP_DELIVERY_ID_MESSAGE =
"\n" +
" \n" +
" \n" +
" \n" +
" \n" +
" \n" +
"\n";
@Before
public void setup() {
utils = new SOAPUtils();
}
@Test
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");
}
@Test
public void canUnwrapFormattedSoapMessage() throws Exception {
getFirstChildAndCheckLocalName(FORMATTED_MESSAGE, "DeliveryNotification");
}
@Test
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);
}
@Test
public void getAppDeliveryIDFromSoapMessage() throws Exception {
getAppDeliveryIDAndCheck(SOAP_MESSAGE, "valid-delivery-request-id");
}
@Test
public void getAppDeliveryIDFromFormattedMessage() throws Exception {
getAppDeliveryIDAndCheck(FORMATTED_MESSAGE, "formatted-adid");
}
@Test
public void getAppDeliveryIDFromClutteredMessage() throws Exception {
getAppDeliveryIDAndCheck(CLUTTERED_MESSAGE, "cluttered-adid");
}
@Test(expected = MoaZSException.class)
public void failToRetrieveAppDeliveryIDWhenBodyIsMissing() throws Exception {
getAppDeliveryIDAndCheck(MISSING_BODY_MESSAGE, "");
}
@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, "");
}
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);
}
}