|
|
@@ -0,0 +1,93 @@
|
|
|
+package com.etotem.cfc.service;
|
|
|
+
|
|
|
+import static org.junit.jupiter.api.Assertions.*;
|
|
|
+import static org.mockito.ArgumentMatchers.*;
|
|
|
+import static org.mockito.Mockito.*;
|
|
|
+
|
|
|
+import java.lang.reflect.Field;
|
|
|
+import java.util.Map;
|
|
|
+
|
|
|
+import org.junit.jupiter.api.Test;
|
|
|
+import org.junit.jupiter.api.extension.ExtendWith;
|
|
|
+import org.mockito.InjectMocks;
|
|
|
+import org.mockito.Mock;
|
|
|
+import org.mockito.Spy;
|
|
|
+import org.mockito.junit.jupiter.MockitoExtension;
|
|
|
+import org.mockito.junit.jupiter.MockitoSettings;
|
|
|
+import org.mockito.quality.Strictness;
|
|
|
+
|
|
|
+import com.etotem.cfc.mapper.PackageOrderMapper;
|
|
|
+import com.etotem.cfc.mapper.ProductOrderMapper;
|
|
|
+
|
|
|
+@MockitoSettings(strictness = Strictness.LENIENT)
|
|
|
+@ExtendWith(MockitoExtension.class)
|
|
|
+class PaymentServiceORDRoutingTest {
|
|
|
+
|
|
|
+ @Mock
|
|
|
+ private PackageOrderMapper packageOrderMapper;
|
|
|
+ @Mock
|
|
|
+ private ProductOrderMapper productOrderMapper;
|
|
|
+ @Mock
|
|
|
+ private CommissionService commissionService;
|
|
|
+ @Mock
|
|
|
+ private ProductOrderService productOrderService;
|
|
|
+ @Mock
|
|
|
+ private MembershipService membershipService;
|
|
|
+
|
|
|
+ @Spy
|
|
|
+ @InjectMocks
|
|
|
+ private PaymentService paymentService;
|
|
|
+
|
|
|
+ @Test
|
|
|
+ @SuppressWarnings("unchecked")
|
|
|
+ void ordPrefix_routesToMembershipService() throws Exception {
|
|
|
+ String json = "{\"out_trade_no\":\"ORD202608291234\",\"transaction_id\":\"wx_tx1\"}";
|
|
|
+ doReturn(json).when(paymentService).decryptAes256Gcm(anyString(), anyString(), anyString());
|
|
|
+
|
|
|
+ String requestBody = "{\"resource\":{\"ciphertext\":\"abc\",\"associated_data\":\"def\",\"nonce\":\"ghi\"}}";
|
|
|
+
|
|
|
+ Map<String, Object> result = paymentService.handleWechatNotify(requestBody, "mock-signature");
|
|
|
+
|
|
|
+ assertEquals("SUCCESS", result.get("code"));
|
|
|
+ verify(membershipService).processPaymentCallback("ORD202608291234", "wx_tx1", "wechat");
|
|
|
+ verify(productOrderService, never()).handlePaymentSuccess(any(), any());
|
|
|
+ }
|
|
|
+
|
|
|
+ @Test
|
|
|
+ @SuppressWarnings("unchecked")
|
|
|
+ void poPrefix_routesToProductOrderService() throws Exception {
|
|
|
+ String json = "{\"out_trade_no\":\"PO202608290001\",\"transaction_id\":\"wx_tx2\"}";
|
|
|
+ doReturn(json).when(paymentService).decryptAes256Gcm(anyString(), anyString(), anyString());
|
|
|
+
|
|
|
+ String requestBody = "{\"resource\":{\"ciphertext\":\"abc\",\"associated_data\":\"def\",\"nonce\":\"ghi\"}}";
|
|
|
+
|
|
|
+ Map<String, Object> result = paymentService.handleWechatNotify(requestBody, "mock-signature");
|
|
|
+
|
|
|
+ assertEquals("SUCCESS", result.get("code"));
|
|
|
+ verify(productOrderService).handlePaymentSuccess("PO202608290001", "wx_tx2");
|
|
|
+ verify(membershipService, never()).processPaymentCallback(any(), any(), any());
|
|
|
+ }
|
|
|
+
|
|
|
+ @Test
|
|
|
+ @SuppressWarnings("unchecked")
|
|
|
+ void unknownPrefix_routesToHandlePaymentCallback() throws Exception {
|
|
|
+ String json = "{\"out_trade_no\":\"PKG202608290001\",\"transaction_id\":\"wx_tx3\"}";
|
|
|
+ doReturn(json).when(paymentService).decryptAes256Gcm(anyString(), anyString(), anyString());
|
|
|
+
|
|
|
+ String requestBody = "{\"resource\":{\"ciphertext\":\"abc\",\"associated_data\":\"def\",\"nonce\":\"ghi\"}}";
|
|
|
+
|
|
|
+ when(packageOrderMapper.selectOne(any())).thenReturn(null);
|
|
|
+
|
|
|
+ Map<String, Object> result = paymentService.handleWechatNotify(requestBody, "mock-signature");
|
|
|
+
|
|
|
+ assertEquals("SUCCESS", result.get("code"));
|
|
|
+ verify(membershipService, never()).processPaymentCallback(any(), any(), any());
|
|
|
+ verify(productOrderService, never()).handlePaymentSuccess(any(), any());
|
|
|
+ }
|
|
|
+
|
|
|
+ private void setField(Object target, String fieldName, Object value) throws Exception {
|
|
|
+ Field field = target.getClass().getDeclaredField(fieldName);
|
|
|
+ field.setAccessible(true);
|
|
|
+ field.set(target, value);
|
|
|
+ }
|
|
|
+}
|