[매입] mg_recv_svc → 전문수신 @Service #8
1 changed files with 107 additions and 0 deletions
|
|
@ -0,0 +1,107 @@
|
|||
package com.klaro.acquirecore.acquiring.service;
|
||||
|
||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||
import com.klaro.acquirecore.acquiring.dto.TxMessage;
|
||||
import org.junit.jupiter.api.BeforeEach;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import java.nio.charset.StandardCharsets;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.*;
|
||||
|
||||
class TxMessageCodecTest {
|
||||
|
||||
private TxMessageCodec codec;
|
||||
|
||||
@BeforeEach
|
||||
void setUp() {
|
||||
codec = new TxMessageCodec(new ObjectMapper());
|
||||
}
|
||||
|
||||
@Test
|
||||
void unpack_validJsonMessage_parsesCorrectly() {
|
||||
// Given
|
||||
String body = "{\"TXID\":\"TX001\",\"MERCHANTID\":\"M001\",\"TERMINALID\":\"T001\",\"AMOUNT\":\"10000\"}";
|
||||
byte[] raw = buildRawMessage("HEADER", body);
|
||||
|
||||
// When
|
||||
TxMessage msg = codec.unpack(raw);
|
||||
|
||||
// Then
|
||||
assertEquals("TX001", msg.getTxId());
|
||||
assertEquals("M001", msg.getMerchantId());
|
||||
assertEquals("T001", msg.getTerminalId());
|
||||
assertEquals("10000", msg.getAmount());
|
||||
}
|
||||
|
||||
@Test
|
||||
void unpack_delimitedMessage_parsesCorrectly() {
|
||||
// Given
|
||||
String body = "TXID=TX002|MERCHANTID=M002|TERMINALID=T002|AMOUNT=20000";
|
||||
byte[] raw = buildRawMessage("HEADER", body);
|
||||
|
||||
// When
|
||||
TxMessage msg = codec.unpack(raw);
|
||||
|
||||
// Then
|
||||
assertEquals("TX002", msg.getTxId());
|
||||
assertEquals("M002", msg.getMerchantId());
|
||||
assertEquals("T002", msg.getTerminalId());
|
||||
assertEquals("20000", msg.getAmount());
|
||||
}
|
||||
|
||||
@Test
|
||||
void unpack_tooShortMessage_throwsException() {
|
||||
// Given
|
||||
byte[] raw = "short".getBytes(StandardCharsets.ISO_8859_1);
|
||||
|
||||
// When/Then
|
||||
assertThrows(IllegalArgumentException.class, () -> codec.unpack(raw));
|
||||
}
|
||||
|
||||
@Test
|
||||
void pack_unpackedMessage_producesValidBytes() {
|
||||
// Given
|
||||
TxMessage msg = new TxMessage();
|
||||
msg.setTxId("TX003");
|
||||
msg.setMerchantId("M003");
|
||||
msg.setTerminalId("T003");
|
||||
msg.setAmount("30000");
|
||||
msg.setStatus("REQ");
|
||||
|
||||
// When
|
||||
byte[] packed = codec.pack(msg);
|
||||
|
||||
// Then
|
||||
assertNotNull(packed);
|
||||
assertTrue(packed.length >= 20);
|
||||
|
||||
// Verify can unpack back
|
||||
TxMessage unpacked = codec.unpack(packed);
|
||||
assertEquals("TX003", unpacked.getTxId());
|
||||
assertEquals("M003", unpacked.getMerchantId());
|
||||
}
|
||||
|
||||
@Test
|
||||
void unpack_packRoundTrip_preservesData() {
|
||||
// Given
|
||||
String body = "{\"TXID\":\"TX004\",\"MERCHANTID\":\"M004\",\"AMOUNT\":\"40000\",\"AUTHNO\":\"123456\"}";
|
||||
byte[] raw = buildRawMessage("HEADER", body);
|
||||
|
||||
// When
|
||||
TxMessage original = codec.unpack(raw);
|
||||
byte[] repacked = codec.pack(original);
|
||||
TxMessage roundTrip = codec.unpack(repacked);
|
||||
|
||||
// Then
|
||||
assertEquals(original.getTxId(), roundTrip.getTxId());
|
||||
assertEquals(original.getMerchantId(), roundTrip.getMerchantId());
|
||||
assertEquals(original.getAmount(), roundTrip.getAmount());
|
||||
assertEquals(original.getAuthNo(), roundTrip.getAuthNo());
|
||||
}
|
||||
|
||||
private byte[] buildRawMessage(String header, String body) {
|
||||
String hdr = String.format("%-20s", header);
|
||||
return (hdr + body).getBytes(StandardCharsets.ISO_8859_1);
|
||||
}
|
||||
}
|
||||
Reference in a new issue