[매입] mg_recv_svc → 전문수신 @Service (ACM-MG-004)
This commit is contained in:
parent
03aec59479
commit
30eec5f570
1 changed files with 158 additions and 0 deletions
|
|
@ -0,0 +1,158 @@
|
|||
package com.klaro.acquirecore.acquiring.service;
|
||||
|
||||
import com.klaro.acquirecore.acquiring.dto.TxMessage;
|
||||
import com.klaro.acquirecore.acquiring.dto.TxResult;
|
||||
import com.klaro.acquirecore.acquiring.repository.TxRepository;
|
||||
import org.junit.jupiter.api.BeforeEach;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.junit.jupiter.api.extension.ExtendWith;
|
||||
import org.mockito.Mock;
|
||||
import org.mockito.junit.jupiter.MockitoExtension;
|
||||
|
||||
import java.nio.charset.StandardCharsets;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.*;
|
||||
import static org.mockito.ArgumentMatchers.any;
|
||||
import static org.mockito.ArgumentMatchers.anyString;
|
||||
import static org.mockito.Mockito.*;
|
||||
|
||||
@ExtendWith(MockitoExtension.class)
|
||||
class TxMessageReceiveServiceTest {
|
||||
|
||||
@Mock
|
||||
private TxRepository txRepository;
|
||||
|
||||
private TxMessageCodec codec;
|
||||
private TxMessageReceiveService service;
|
||||
|
||||
@BeforeEach
|
||||
void setUp() {
|
||||
codec = new TxMessageCodec(new com.fasterxml.jackson.databind.ObjectMapper());
|
||||
service = new TxMessageReceiveService(codec, txRepository);
|
||||
}
|
||||
|
||||
@Test
|
||||
void receive_validApprovalMessage_returnsSuccess() {
|
||||
// Given
|
||||
byte[] raw = buildRawMessage("REQ", "M001", "T001", "10000");
|
||||
when(txRepository.isMerchantActive("M001")).thenReturn(true);
|
||||
when(txRepository.isTerminalActive("M001", "T001")).thenReturn(true);
|
||||
|
||||
// When
|
||||
TxResult result = service.receive(raw);
|
||||
|
||||
// Then
|
||||
assertTrue(result.isSuccess());
|
||||
assertEquals("0000", result.getCode());
|
||||
verify(txRepository).saveTransaction(any(TxMessage.class));
|
||||
}
|
||||
|
||||
@Test
|
||||
void receive_missingMerchantId_returnsError() {
|
||||
// Given
|
||||
byte[] raw = buildRawMessage("REQ", "", "T001", "10000");
|
||||
|
||||
// When
|
||||
TxResult result = service.receive(raw);
|
||||
|
||||
// Then
|
||||
assertFalse(result.isSuccess());
|
||||
assertEquals("1001", result.getCode());
|
||||
}
|
||||
|
||||
@Test
|
||||
void receive_missingTerminalId_returnsError() {
|
||||
// Given
|
||||
byte[] raw = buildRawMessage("REQ", "M001", "", "10000");
|
||||
|
||||
// When
|
||||
TxResult result = service.receive(raw);
|
||||
|
||||
// Then
|
||||
assertFalse(result.isSuccess());
|
||||
assertEquals("1002", result.getCode());
|
||||
}
|
||||
|
||||
@Test
|
||||
void receive_invalidAmount_returnsError() {
|
||||
// Given
|
||||
byte[] raw = buildRawMessage("REQ", "M001", "T001", "invalid");
|
||||
|
||||
// When
|
||||
TxResult result = service.receive(raw);
|
||||
|
||||
// Then
|
||||
assertFalse(result.isSuccess());
|
||||
assertEquals("1004", result.getCode());
|
||||
}
|
||||
|
||||
@Test
|
||||
void receive_inactiveMerchant_returnsError() {
|
||||
// Given
|
||||
byte[] raw = buildRawMessage("REQ", "M001", "T001", "10000");
|
||||
when(txRepository.isMerchantActive("M001")).thenReturn(false);
|
||||
|
||||
// When
|
||||
TxResult result = service.receive(raw);
|
||||
|
||||
// Then
|
||||
assertFalse(result.isSuccess());
|
||||
assertEquals("2001", result.getCode());
|
||||
}
|
||||
|
||||
@Test
|
||||
void receive_inactiveTerminal_returnsError() {
|
||||
// Given
|
||||
byte[] raw = buildRawMessage("REQ", "M001", "T001", "10000");
|
||||
when(txRepository.isMerchantActive("M001")).thenReturn(true);
|
||||
when(txRepository.isTerminalActive("M001", "T001")).thenReturn(false);
|
||||
|
||||
// When
|
||||
TxResult result = service.receive(raw);
|
||||
|
||||
// Then
|
||||
assertFalse(result.isSuccess());
|
||||
assertEquals("2002", result.getCode());
|
||||
}
|
||||
|
||||
@Test
|
||||
void receive_cancelMessage_callsCancellation() {
|
||||
// Given
|
||||
byte[] raw = buildRawMessage("CANCEL", "M001", "T001", "10000");
|
||||
when(txRepository.isMerchantActive("M001")).thenReturn(true);
|
||||
when(txRepository.isTerminalActive("M001", "T001")).thenReturn(true);
|
||||
when(txRepository.transactionExists(anyString())).thenReturn(true);
|
||||
when(txRepository.getTransactionStatus(anyString())).thenReturn("APPROVED");
|
||||
|
||||
// When
|
||||
TxResult result = service.receive(raw);
|
||||
|
||||
// Then
|
||||
assertTrue(result.isSuccess());
|
||||
verify(txRepository).updateTransactionStatus(anyString(), eq("CANCELLED"));
|
||||
}
|
||||
|
||||
@Test
|
||||
void receive_cancelNonExistentTransaction_returnsError() {
|
||||
// Given
|
||||
byte[] raw = buildRawMessage("CANCEL", "M001", "T001", "10000");
|
||||
when(txRepository.isMerchantActive("M001")).thenReturn(true);
|
||||
when(txRepository.isTerminalActive("M001", "T001")).thenReturn(true);
|
||||
when(txRepository.transactionExists(anyString())).thenReturn(false);
|
||||
|
||||
// When
|
||||
TxResult result = service.receive(raw);
|
||||
|
||||
// Then
|
||||
assertFalse(result.isSuccess());
|
||||
assertEquals("3001", result.getCode());
|
||||
}
|
||||
|
||||
private byte[] buildRawMessage(String status, String merchantId, String terminalId, String amount) {
|
||||
String body = String.format(
|
||||
"{\"STATUS\":\"%s\",\"MERCHANTID\":\"%s\",\"TERMINALID\":\"%s\",\"AMOUNT\":\"%s\"}",
|
||||
status, merchantId, terminalId, amount);
|
||||
String header = String.format("%-20s", "TXMSG");
|
||||
return (header + body).getBytes(StandardCharsets.ISO_8859_1);
|
||||
}
|
||||
}
|
||||
Reference in a new issue