From a9585d867a2b432f6d75237e0857dd8af2daf554 Mon Sep 17 00:00:00 2001 From: Christof Rabensteiner Date: Mon, 9 Dec 2019 08:53:56 +0100 Subject: Test Refactor: Move SOAP Client Utilities into Dedicated Class --- src/test/java/at/gv/egiz/moazs/ITEndToEndTest.java | 86 ++++++++-------------- src/test/java/at/gv/egiz/moazs/TestUtils.java | 36 +++++++++ 2 files changed, 66 insertions(+), 56 deletions(-) create mode 100644 src/test/java/at/gv/egiz/moazs/TestUtils.java (limited to 'src/test') diff --git a/src/test/java/at/gv/egiz/moazs/ITEndToEndTest.java b/src/test/java/at/gv/egiz/moazs/ITEndToEndTest.java index f350681..5baddfc 100644 --- a/src/test/java/at/gv/egiz/moazs/ITEndToEndTest.java +++ b/src/test/java/at/gv/egiz/moazs/ITEndToEndTest.java @@ -26,6 +26,7 @@ import at.gv.egiz.moazs.repository.DeliveryRepository; import at.gv.egiz.moazs.scheme.NotificationResponse; import at.gv.egiz.moazs.scheme.RequestStatusResponse; import at.gv.zustellung.app2mzs.xsd.DeliveryNotificationACKType; +import at.gv.zustellung.app2mzs.xsd.DeliveryNotificationType; import at.gv.zustellung.app2mzs.xsd.DeliveryResponseType; import at.gv.zustellung.app2mzs.xsd.Mzs2AppPortType; import at.gv.zustellung.msg.xsd.App2ZusePort; @@ -46,6 +47,7 @@ import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Primary; import org.springframework.test.context.junit4.SpringRunner; +import javax.annotation.PostConstruct; import java.io.File; import java.io.IOException; import java.net.URI; @@ -58,6 +60,8 @@ import java.util.Collection; import java.util.List; import java.util.function.Consumer; +import static at.gv.egiz.moazs.TestUtils.formatFile; +import static at.gv.egiz.moazs.TestUtils.sendSOAP; import static at.gv.zustellung.app2mzs.xsd.DeliveryNotificationACKType.deliveryNotificationACKTypeBuilder; import static at.gv.zustellung.app2mzs.xsd.persondata.IdentificationType.Value.valueBuilder; import static at.gv.zustellung.app2mzs.xsd.persondata.IdentificationType.identificationTypeBuilder; @@ -83,14 +87,18 @@ public class ITEndToEndTest { @LocalServerPort public int port; - private String mzsFrontendURL; + private String mzsEndpoint; + private String msgEndpoint; + private final String basePath = "src/test/resources/at/gv/egiz/moazs/ITEndToEndTest/"; private static Mzs2AppPortType APP; - @Before - public void setupFrontendURL() { - this.mzsFrontendURL = "http://localhost:" + port + "/services"; + @PostConstruct + void init() { + var frontendURI = "http://localhost:" + port + "/services"; + mzsEndpoint = frontendURI + "/mzs/"; + msgEndpoint = frontendURI + "/msg/"; } @TestConfiguration @@ -120,12 +128,17 @@ public class ITEndToEndTest { private Mzs2AppPortType mockApp() { var app = mock(Mzs2AppPortType.class); - when(app.forwardStatus(any())).thenAnswer(i -> ack(i.getArgument(0))); - when(app.forwardNotification(any())).thenAnswer(i -> ack(i.getArgument(0))); + when(app.forwardStatus(any())).thenAnswer(i -> ackStatus(i.getArgument(0))); + when(app.forwardNotification(any())).thenAnswer(i -> ackNotification(i.getArgument(0))); return app; } - private DeliveryNotificationACKType ack(DeliveryResponseType response) { + private DeliveryNotificationACKType ackStatus(DeliveryResponseType response) { + return deliveryNotificationACKTypeBuilder() + .withAppDeliveryID(response.getSuccess().getAppDeliveryID()) + .build(); + } + private DeliveryNotificationACKType ackNotification(DeliveryNotificationType response) { return deliveryNotificationACKTypeBuilder() .withAppDeliveryID(response.getSuccess().getAppDeliveryID()) .build(); @@ -180,16 +193,20 @@ public class ITEndToEndTest { delete(saveSinkFolder); //app sends delivery request to moazs and receives partial success - var partialSuccess = sendMzsDeliveryRequest("mzs-delivery-request.xml"); + String mzsDeliveryRequestBody = formatFile(basePath + "mzs-delivery-request.xml", new String[0]); + var partialSuccess = sendSOAP(mzsEndpoint, mzsDeliveryRequestBody); + assertThat(partialSuccess.statusCode()).isEqualTo(200); assertThat(partialSuccess.body()).contains(List.of(GZ_WATERMARK, "PartialSuccess")); // zusemsg sends async success var statusResponseID = RequestStatusResponse.getResponseID(appDeliveryID); - var msgStatus = formatFile("msg-delivery-request-status.xml", new String[]{ + var msgStatus = formatFile(basePath + "msg-delivery-request-status.xml", new String[]{ DELIVERY_SYSTEM, zsDeliveryID, appDeliveryID, GZ_WATERMARK, timestamp }); - sendMsgResponse(msgStatus); + + sendSOAP(msgEndpoint, msgStatus); + await().untilAsserted(() -> { verify(APP).forwardStatus(any()); assertStatusWrittenToFileSystem(saveSinkFolder, statusResponseID); @@ -197,10 +214,11 @@ public class ITEndToEndTest { // zusemsg sends async notification var notificationResponseID = NotificationResponse.getResponseID(appDeliveryID); - var notification = formatFile("msg-delivery-notification.xml", new String[]{ + var notification = formatFile(basePath + "msg-delivery-notification.xml", new String[]{ DELIVERY_SYSTEM, zsDeliveryID, appDeliveryID, GZ_WATERMARK, timestamp, timestamp }); - sendMsgResponse(notification); + + sendSOAP(msgEndpoint, notification); await().untilAsserted(() -> { verify(APP).forwardNotification(any()); assertStatusWrittenToFileSystem(saveSinkFolder, notificationResponseID); @@ -224,14 +242,6 @@ public class ITEndToEndTest { assertThat(count).isEqualTo(2); } - private String readFile(File file) { - try { - return readFileToString(file, StandardCharsets.UTF_8); - } catch (IOException e) { - throw new RuntimeException(e); - } - } - private void delete(String file) { try { FileUtils.deleteDirectory(new File(file)); @@ -240,40 +250,4 @@ public class ITEndToEndTest { } } - private String formatFile(String templateFile, String... values) throws IOException { - var path = basePath + templateFile; - var templateString = FileUtils.readFileToString(new File(path), StandardCharsets.UTF_8); - return String.format(templateString, values); - } - - private HttpResponse sendMsgResponse(String bodyString) throws IOException, InterruptedException { - - var body = HttpRequest.BodyPublishers.ofString(bodyString); - var client = HttpClient.newBuilder().version(HttpClient.Version.HTTP_1_1).build(); - var request = HttpRequest.newBuilder() - .uri(URI.create(mzsFrontendURL + "/msg/")) - .header("Content-Type", "text/xml;charset=UTF-8") - .header("SOAPAction", "\"\"") - .POST(body) - .build(); - - return client.send(request, ofString()); - - } - - private HttpResponse sendMzsDeliveryRequest(String fileName) throws IOException, InterruptedException { - - var path = basePath + fileName; - var client = HttpClient.newBuilder().version(HttpClient.Version.HTTP_1_1).build(); - var request = HttpRequest.newBuilder() - .uri(URI.create(mzsFrontendURL + "/mzs/")) - .header("Content-Type", "text/xml;charset=UTF-8") - .header("SOAPAction", "\"\"") - .POST(HttpRequest.BodyPublishers.ofFile(Paths.get(path))) - .build(); - - return client.send(request, ofString()); - - } - } diff --git a/src/test/java/at/gv/egiz/moazs/TestUtils.java b/src/test/java/at/gv/egiz/moazs/TestUtils.java new file mode 100644 index 0000000..96d3a22 --- /dev/null +++ b/src/test/java/at/gv/egiz/moazs/TestUtils.java @@ -0,0 +1,36 @@ +package at.gv.egiz.moazs; + +import org.apache.commons.io.FileUtils; + +import java.io.File; +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.charset.StandardCharsets; + +import static java.net.http.HttpResponse.BodyHandlers.ofString; + +public class TestUtils { + + public static String formatFile(String path, String... values) throws IOException { + var templateString = FileUtils.readFileToString(new File(path), StandardCharsets.UTF_8); + return String.format(templateString, values); + } + + public static HttpResponse sendSOAP(String endpoint, String bodyString) throws IOException, InterruptedException { + + var body = HttpRequest.BodyPublishers.ofString(bodyString); + var client = HttpClient.newBuilder().version(HttpClient.Version.HTTP_1_1).build(); + var request = HttpRequest.newBuilder() + .uri(URI.create(endpoint)) + .header("Content-Type", "text/xml;charset=UTF-8") + .header("SOAPAction", "\"\"") + .POST(body) + .build(); + + return client.send(request, ofString()); + + } +} -- cgit v1.2.3