package at.gv.egiz.moazs; import at.gv.egiz.moazs.pipeline.DeliveryPipeline; import at.gv.egiz.moazs.repository.DeliveryRepository; import at.gv.zustellung.msg.xsd.DeliveryRequestStatusType.DeliveryRequestStatusTypeBuilder; import at.gv.zustellung.msg.xsd.DeliveryRequestStatusType.Success.SuccessBuilder; import org.junit.Test; import org.junit.runner.RunWith; import org.slf4j.Logger; import org.slf4j.LoggerFactory; 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 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 static java.net.http.HttpClient.Version; import static org.junit.Assert.assertEquals; @RunWith(SpringRunner.class) @SpringBootTest(webEnvironment = WebEnvironment.DEFINED_PORT) public class App2MzsServiceTest { private final String serviceUri = "http://localhost:8080/services/DeliveryRequest"; private final String basePath = "src/test/resources/at/gv/egiz/moazs/App2MzsServiceTest/"; private final static Logger logger = LoggerFactory.getLogger(App2MzsServiceTest.class); @TestConfiguration public static class TestConfig { @Autowired private DeliveryRepository repository; @Bean @Primary public DeliveryPipeline deliveryPipelineThatAlwaysSucceeds() { return appDeliveryId -> { var success = new SuccessBuilder() .withAppDeliveryID(appDeliveryId) .build(); var status = new DeliveryRequestStatusTypeBuilder() .withSuccess(success) .build(); repository.add(status); }; } } @Test public void acceptValidDeliveryRequest() throws IOException, InterruptedException { var response = sendDeliveryRequestFile("validDeliveryRequest.soap"); assertEquals(200, 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()); } }