/*******************************************************************************
* Copyright 2020 Graz University of Technology
* MOA ZS has been developed in a cooperation between EGIZ
* and Graz University of Technology.
* Licensed under the EUPL, Version 1.2 or - as soon they will be approved by
* the European Commission - subsequent versions of the EUPL (the "Licence");
* You may not use this work except in compliance with the Licence.
* You may obtain a copy of the Licence at:
* https://joinup.ec.europa.eu/news/understanding-eupl-v12
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the Licence is distributed on an "AS IS" basis,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the Licence for the specific language governing permissions and
* limitations under the Licence.
*
* This product combines work with different licenses. See the "NOTICE" text
* file for details on the various modules and licenses.
* The "NOTICE" text file is part of the distribution. Any derivative works
* that you distribute must include a readable copy of the "NOTICE" text file.
*******************************************************************************/
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;
/**
* @author Christof Rabensteiner
*
*/
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);
}
}