Skip to content

Commit

Permalink
feat(x2/last): return PDU raw b64 encoded
Browse files Browse the repository at this point in the history
The ObjectMapper is not smart enough to translate that object, so we
intend to use our own decoder for that.
  • Loading branch information
Takuto88 committed Oct 15, 2024
1 parent 146513a commit 9172cca
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -18,10 +22,15 @@ public X2Controller(final X2X3Memory x2X3Memory) {
}

@GetMapping("/last")
public ResponseEntity<PduObject> getLast() {
public ResponseEntity<String> 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()));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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);
Expand Down

0 comments on commit 9172cca

Please sign in to comment.