package at.gv.egiz.moazs; import at.gv.egiz.moazs.repository.DeliveryRepository; import at.gv.egiz.moazs.scheme.RequestStatusResponse; import org.junit.Before; 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.boot.web.server.LocalServerPort; 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.assertj.core.api.Assertions.assertThat; @RunWith(SpringRunner.class) @SpringBootTest(webEnvironment = WebEnvironment.RANDOM_PORT) public class ITMzsServiceTest { private final String basePath = "src/test/resources/at/gv/egiz/moazs/ITMzsServiceTest/"; @LocalServerPort public int port; private String serviceUri; @Before public void setupServiceURI() { this.serviceUri = "http://localhost:" + port + "/services/mzs"; } @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)); }; } } @Test public void acceptValidDeliveryRequest() throws IOException, InterruptedException { var response = sendDeliveryRequestFile("validDeliveryRequest.soap"); assertThat(response.statusCode()).isEqualTo(200); assertThat(response.body()).contains("valid-delivery-request-id"); } @Test public void rejectRequestWithoutSender() throws IOException, InterruptedException { var response = sendDeliveryRequestFile("missingSender.soap"); assertThat(response.statusCode()).isEqualTo(500); } @Test public void rejectBothProfileAndCorporateBody() throws IOException, InterruptedException { var response = sendDeliveryRequestFile("profileAndCorporateBody.soap"); assertThat(response.statusCode()).isEqualTo(500); } @Test public void rejectFormallyIncorrectDeliveryRequest() throws IOException, InterruptedException { var response = sendDeliveryRequestFile("formallyIncorrectDeliveryRequest.soap"); assertThat(response.statusCode()).isEqualTo(500); } @Test public void rejectRequestWithoutAppDeliveryID() throws IOException, InterruptedException { var response = sendDeliveryRequestFile("missingAppDeliveryId.soap"); assertThat(response.statusCode()).isEqualTo(500); } @Test public void rejectRequestWithoutMetaData() throws IOException, InterruptedException { var response = sendDeliveryRequestFile("missingMetaData.soap"); assertThat(response.statusCode()).isEqualTo(500); } 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()); } public static XMLGregorianCalendar genTimeStamp() { try { return DatatypeFactory.newInstance().newXMLGregorianCalendar(new GregorianCalendar()); } catch (DatatypeConfigurationException e) { throw new RuntimeException("ups"); } } }