package at.gv.egiz.moazs; import at.gv.egiz.moazs.repository.DeliveryRepository; import at.gv.egiz.moazs.scheme.RequestStatusResponse; import org.junit.Test; import org.junit.runner.RunWith; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.test.context.SpringBootTest; import org.springframework.boot.test.context.SpringBootTest.WebEnvironment; import org.springframework.boot.test.context.TestConfiguration; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Primary; import org.springframework.test.context.junit4.SpringRunner; import javax.xml.datatype.DatatypeConfigurationException; import javax.xml.datatype.DatatypeFactory; import javax.xml.datatype.XMLGregorianCalendar; import java.io.IOException; import java.net.URI; import java.net.http.HttpClient; import java.net.http.HttpRequest; import java.net.http.HttpResponse; import java.nio.file.Paths; import java.util.GregorianCalendar; import java.util.function.Consumer; import static at.gv.zustellung.msg.xsd.DeliveryRequestStatusType.Success.successBuilder; import static at.gv.zustellung.msg.xsd.DeliveryRequestStatusType.deliveryRequestStatusTypeBuilder; import static java.net.http.HttpClient.Version; import static org.junit.Assert.assertEquals; @RunWith(SpringRunner.class) @SpringBootTest(webEnvironment = WebEnvironment.DEFINED_PORT) public class ITMzsServiceTest { private final String serviceUri = "http://localhost:8080/services/DeliveryRequest"; private final String basePath = "src/test/resources/at/gv/egiz/moazs/ITMzsServiceTest/"; @TestConfiguration public static class TestConfig { @Autowired private DeliveryRepository repository; @Bean @Primary public Consumer deliveryPipelineThatAlwaysSucceeds() { return appDeliveryId -> { var success = successBuilder() .withDeliverySystem("Test Delivery System") .withZSDeliveryID("ZD-Delivery-ID") .withAppDeliveryID(appDeliveryId) .withDeliveryTimestamp(genTimeStamp()) .build(); var status = deliveryRequestStatusTypeBuilder() .withSuccess(success) .build(); repository.store(new RequestStatusResponse(status)); }; } private XMLGregorianCalendar genTimeStamp() { try { return DatatypeFactory.newInstance().newXMLGregorianCalendar(new GregorianCalendar()); } catch (DatatypeConfigurationException e) { throw new RuntimeException("ups"); } } } @Test public void acceptValidDeliveryRequest() throws IOException, InterruptedException { var response = sendDeliveryRequestFile("validDeliveryRequest.soap"); assertEquals(200, response.statusCode()); } @Test public void rejectRequestWithoutSender() throws IOException, InterruptedException { var response = sendDeliveryRequestFile("missingSender.soap"); assertEquals(500, response.statusCode()); } @Test public void rejectBothProfileAndCorporateBody() throws IOException, InterruptedException { var response = sendDeliveryRequestFile("profileAndCorporateBody.soap"); assertEquals(500, response.statusCode()); } @Test public void rejectFormallyIncorrectDeliveryRequest() throws IOException, InterruptedException { var response = sendDeliveryRequestFile("formallyIncorrectDeliveryRequest.soap"); assertEquals(500, response.statusCode()); } @Test public void rejectRequestWithoutAppDeliveryID() throws IOException, InterruptedException { var response = sendDeliveryRequestFile("missingAppDeliveryId.soap"); assertEquals(500, response.statusCode()); } @Test public void rejectRequestWithoutMetaData() throws IOException, InterruptedException { var response = sendDeliveryRequestFile("missingMetaData.soap"); assertEquals(500, response.statusCode()); } private HttpResponse sendDeliveryRequestFile(String fileName) throws IOException, InterruptedException { var path = basePath + fileName; var client = HttpClient.newBuilder().version(Version.HTTP_1_1).build(); var request = HttpRequest.newBuilder() .uri(URI.create(serviceUri)) .header("Content-Type", "text/xml;charset=UTF-8") .header("SOAPAction", "\"\"") .POST(HttpRequest.BodyPublishers.ofFile(Paths.get(path))) .build(); return client.send(request, HttpResponse.BodyHandlers.ofString()); } }