48 lines
2.6 KiB
Java
48 lines
2.6 KiB
Java
package com.baiyue.finance;
|
|
|
|
import com.baiyue.finance.service.FinanceSettlementService;
|
|
import com.fasterxml.jackson.databind.ObjectMapper;
|
|
import org.junit.jupiter.params.ParameterizedTest;
|
|
import org.junit.jupiter.params.provider.ValueSource;
|
|
import com.baiyue.finance.service.FinanceWalletService;
|
|
import com.baiyue.finance.service.FinancePhase1OperationService;
|
|
import com.baiyue.finance.service.FinanceService;
|
|
import com.baiyue.finance.service.ReconciliationStatementService;
|
|
import org.springframework.test.util.ReflectionTestUtils;
|
|
|
|
import java.util.LinkedHashMap;
|
|
import java.util.Map;
|
|
|
|
import static org.assertj.core.api.Assertions.assertThat;
|
|
|
|
class FinanceSettlementRequestHashTest {
|
|
/** 模拟不同 JVM 的 Map 遍历顺序;同一付款请求必须得到相同摘要。 */
|
|
@ParameterizedTest
|
|
@ValueSource(classes = {FinanceSettlementService.class, FinanceWalletService.class,
|
|
FinancePhase1OperationService.class, FinanceService.class, ReconciliationStatementService.class})
|
|
void equivalentRequestsIgnoreMapIterationOrderButDetectChangedPayment(Class<?> serviceType) {
|
|
Object service = org.mockito.Mockito.mock(serviceType, org.mockito.Mockito.CALLS_REAL_METHODS);
|
|
ReflectionTestUtils.setField(service, "objectMapper", new ObjectMapper());
|
|
Map<String, Object> body = new LinkedHashMap<>();
|
|
body.put("amount", 190); body.put("expectedVersion", 0);
|
|
Map<String, Object> first = new LinkedHashMap<>();
|
|
first.put("path", "/settlements/202/transactions"); first.put("method", "POST");
|
|
first.put("customerId", 10L); first.put("body", body);
|
|
Map<String, Object> reversedBody = new LinkedHashMap<>();
|
|
reversedBody.put("expectedVersion", 0); reversedBody.put("amount", 190);
|
|
Map<String, Object> second = new LinkedHashMap<>();
|
|
second.put("body", reversedBody); second.put("customerId", 10L);
|
|
second.put("method", "POST"); second.put("path", "/settlements/202/transactions");
|
|
|
|
String original = ReflectionTestUtils.invokeMethod(service, "hash", first);
|
|
String reordered = ReflectionTestUtils.invokeMethod(service, "hash", second);
|
|
assertThat(reordered).isEqualTo(original);
|
|
reversedBody.put("amount", 191);
|
|
String changed = ReflectionTestUtils.invokeMethod(service, "hash", second);
|
|
assertThat(changed).isNotEqualTo(original);
|
|
second.put("customerId", 11L); reversedBody.put("amount", 190);
|
|
String otherCustomer = ReflectionTestUtils.invokeMethod(service, "hash", second);
|
|
assertThat(otherCustomer).isNotEqualTo(original);
|
|
}
|
|
}
|