From 9172ccac58fe95ac8826e2837eb7fd420b847262 Mon Sep 17 00:00:00 2001 From: Lennart Rosam Date: Tue, 15 Oct 2024 16:37:10 +0200 Subject: [PATCH] feat(x2/last): return PDU raw b64 encoded The ObjectMapper is not smart enough to translate that object, so we intend to use our own decoder for that. --- .../li/simulator/controller/X2Controller.java | 13 +++++++++++-- .../sipgate/li/simulator/e2e/InterceptsX2Test.java | 10 +++++++++- 2 files changed, 20 insertions(+), 3 deletions(-) diff --git a/src/main/java/com/sipgate/li/simulator/controller/X2Controller.java b/src/main/java/com/sipgate/li/simulator/controller/X2Controller.java index ad673c2..0dd6df2 100644 --- a/src/main/java/com/sipgate/li/simulator/controller/X2Controller.java +++ b/src/main/java/com/sipgate/li/simulator/controller/X2Controller.java @@ -2,6 +2,10 @@ import com.sipgate.li.lib.x2x3.PduObject; import com.sipgate.li.simulator.x2x3.X2X3Memory; +import java.io.ByteArrayOutputStream; +import java.io.DataOutputStream; +import java.io.IOException; +import java.util.Base64; import org.springframework.http.ResponseEntity; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.RequestMapping; @@ -18,10 +22,15 @@ public X2Controller(final X2X3Memory x2X3Memory) { } @GetMapping("/last") - public ResponseEntity getLast() { + public ResponseEntity getLast() throws IOException { if (x2X3Memory.getLast() == null) { return ResponseEntity.notFound().build(); } - return ResponseEntity.ok(x2X3Memory.getLast()); + + final var pduBytes = new ByteArrayOutputStream(); + final var pduStream = new DataOutputStream(pduBytes); + x2X3Memory.getLast().writeTo(pduStream); + + return ResponseEntity.ok(Base64.getEncoder().encodeToString(pduBytes.toByteArray())); } } diff --git a/src/test/java/com/sipgate/li/simulator/e2e/InterceptsX2Test.java b/src/test/java/com/sipgate/li/simulator/e2e/InterceptsX2Test.java index f2a7243..e531523 100644 --- a/src/test/java/com/sipgate/li/simulator/e2e/InterceptsX2Test.java +++ b/src/test/java/com/sipgate/li/simulator/e2e/InterceptsX2Test.java @@ -3,6 +3,9 @@ import static org.assertj.core.api.Assertions.assertThat; import com.sipgate.li.lib.x2x3.*; +import io.netty.buffer.Unpooled; +import java.util.ArrayList; +import java.util.Base64; import org.junit.jupiter.api.Tag; import org.junit.jupiter.api.Test; import org.junit.jupiter.api.extension.ExtendWith; @@ -20,7 +23,12 @@ void simulator_receives_x2(final SimulatorClient simulatorClient) throws Excepti simulatorClient.post("/sip", "application/octet-stream", body, Void.class, 204); // THEN - final PduObject result = simulatorClient.get("/x2/last", PduObject.class); + final var base64edByteStreamOfAPduObject = simulatorClient.get("/x2/last", String.class); + final var decoded = Unpooled.wrappedBuffer(Base64.getDecoder().decode(base64edByteStreamOfAPduObject)); + final var outList = new ArrayList<>(); + new X2X3Decoder(Integer.MAX_VALUE, Integer.MAX_VALUE).decode(decoded, outList); + + final PduObject result = (PduObject) outList.get(0); assertThat(result).isNotNull(); assertThat(result.pduType()).isEqualTo(PduType.X2_PDU); assertThat(result.payloadFormat()).isEqualTo(PayloadFormat.SIP);