MerchantCashOrderNotificationTest.java 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. package com.ruoyi.app.order;
  2. import com.alibaba.fastjson2.JSON;
  3. import com.ruoyi.app.order.dto.OrderCreateInput;
  4. import com.ruoyi.app.order.dto.OrderCreatItem;
  5. import com.ruoyi.common.exception.ServiceException;
  6. import com.ruoyi.common.utils.MessageUtils;
  7. import com.ruoyi.system.domain.*;
  8. import com.ruoyi.system.service.*;
  9. import com.ruoyi.system.utils.AuthContext;
  10. import org.junit.jupiter.api.*;
  11. import org.junit.jupiter.params.ParameterizedTest;
  12. import org.junit.jupiter.params.provider.ValueSource;
  13. import org.mockito.*;
  14. import org.springframework.test.util.ReflectionTestUtils;
  15. import org.springframework.transaction.support.TransactionSynchronization;
  16. import org.springframework.transaction.support.TransactionSynchronizationManager;
  17. import java.util.List;
  18. import static org.junit.jupiter.api.Assertions.*;
  19. import static org.mockito.ArgumentMatchers.*;
  20. import static org.mockito.Mockito.*;
  21. class MerchantCashOrderNotificationTest {
  22. private final PosOrderShOprateController controller = new PosOrderShOprateController();
  23. private final IPosOrderService orders = mock(IPosOrderService.class);
  24. private final IOrderParentService parents = mock(IOrderParentService.class);
  25. private final MerchantNotificationRouter router = mock(MerchantNotificationRouter.class);
  26. private MockedStatic<AuthContext> auth;
  27. private MockedStatic<MessageUtils> messages;
  28. @BeforeEach
  29. void setup() {
  30. auth = mockStatic(AuthContext.class);
  31. auth.when(AuthContext::requireUserId).thenReturn(101L);
  32. messages = mockStatic(MessageUtils.class);
  33. messages.when(() -> MessageUtils.message(anyString())).thenAnswer(i -> i.getArgument(0));
  34. InfoUser merchant = new InfoUser();
  35. merchant.setUserId(101L);
  36. merchant.setUserType("1");
  37. IInfoUserService users = mock(IInfoUserService.class);
  38. when(users.getOne(any())).thenReturn(merchant);
  39. IPosStoreService stores = mock(IPosStoreService.class);
  40. PosStore store = new PosStore();
  41. store.setId(9);
  42. store.setUserId(101L);
  43. when(stores.getById(9L)).thenReturn(store);
  44. when(stores.list(any(com.baomidou.mybatisplus.core.conditions.Wrapper.class))).thenReturn(List.of(store));
  45. OrderNumberReservationService reservations = mock(OrderNumberReservationService.class);
  46. when(reservations.reserveIfUnused(anyString(), anyString())).thenReturn(true);
  47. ReflectionTestUtils.setField(controller, "orderNumberReservations", reservations);
  48. ReflectionTestUtils.setField(controller, "infoUserService", users);
  49. ReflectionTestUtils.setField(controller, "posStoreService", stores);
  50. ReflectionTestUtils.setField(controller, "posOrderService", orders);
  51. ReflectionTestUtils.setField(controller, "orderParentService", parents);
  52. ReflectionTestUtils.setField(controller, "operatingHoursService", mock(IOperatingHoursService.class));
  53. ReflectionTestUtils.setField(controller, "selfDeliveryService", mock(SelfDeliveryService.class));
  54. ReflectionTestUtils.setField(controller, "stallQualificationGateService", mock(StallQualificationGateService.class));
  55. ReflectionTestUtils.setField(controller, "merchantNotificationRouter", router);
  56. when(orders.save(any())).thenReturn(true);
  57. when(parents.save(any())).thenReturn(true);
  58. TransactionSynchronizationManager.initSynchronization();
  59. }
  60. @AfterEach
  61. void cleanup() {
  62. TransactionSynchronizationManager.clearSynchronization();
  63. auth.close();
  64. messages.close();
  65. }
  66. @ParameterizedTest
  67. @ValueSource(longs = {0, 1, 2})
  68. void cashNotifiesOnlyAfterCommitWithFullOrderNumber(long type) {
  69. controller.createOrder("token", input("4", type));
  70. verifyNoInteractions(router);
  71. commit();
  72. ArgumentCaptor<String> body = ArgumentCaptor.forClass(String.class);
  73. verify(router).sendStoreNewOrder(eq(9L), eq(101L), eq("no.message.push.message"),
  74. eq("no.message.push.new.order"), body.capture(), eq("DD-CASH-100"), eq(type));
  75. assertEquals("DD-CASH-100", JSON.parseObject(body.getValue()).getString("ddId"));
  76. assertEquals(0, JSON.parseObject(body.getValue()).getIntValue("type"));
  77. assertEquals(1, JSON.parseObject(body.getValue()).getIntValue("pushType"));
  78. ArgumentCaptor<PosOrder> saved = ArgumentCaptor.forClass(PosOrder.class);
  79. verify(orders).save(saved.capture());
  80. assertEquals(0L, saved.getValue().getPayStatus());
  81. verifyNoMoreInteractions(router);
  82. }
  83. @Test
  84. void rollbackDoesNotNotify() {
  85. controller.createOrder("token", input("4", 2));
  86. TransactionSynchronizationManager.getSynchronizations().forEach(
  87. s -> s.afterCompletion(TransactionSynchronization.STATUS_ROLLED_BACK));
  88. verifyNoInteractions(router);
  89. }
  90. @Test
  91. void failedSaveDoesNotNotify() {
  92. when(orders.save(any())).thenReturn(false);
  93. assertThrows(ServiceException.class, () -> controller.createOrder("token", input("4", 2)));
  94. commit();
  95. verifyNoInteractions(router);
  96. }
  97. @Test
  98. void notificationFailureDoesNotFailCommittedCreation() {
  99. doThrow(new IllegalStateException("push unavailable")).when(router).sendStoreNewOrder(
  100. anyLong(), anyLong(), anyString(), anyString(), anyString(), anyString(), anyLong());
  101. controller.createOrder("token", input("4", 2));
  102. assertDoesNotThrow(this::commit);
  103. verify(router).sendStoreNewOrder(anyLong(), anyLong(), anyString(), anyString(), anyString(), anyString(), anyLong());
  104. }
  105. @ParameterizedTest
  106. @ValueSource(strings = {"1", "2", "3", "5", "6"})
  107. void otherPaymentMethodsDoNotSendCashNotification(String method) {
  108. controller.createOrder("token", input(method, 2));
  109. commit();
  110. verifyNoInteractions(router);
  111. }
  112. private void commit() {
  113. TransactionSynchronizationManager.getSynchronizations().forEach(TransactionSynchronization::afterCommit);
  114. }
  115. private OrderCreateInput input(String method, long type) {
  116. OrderCreatItem item = new OrderCreatItem();
  117. item.setMdId(9L);
  118. item.setShId(101L);
  119. item.setAmount(280);
  120. item.setType(type);
  121. OrderCreateInput input = new OrderCreateInput();
  122. input.setDdId("DD-CASH-100");
  123. input.setPaymentMethod(method);
  124. input.setItems(List.of(item));
  125. return input;
  126. }
  127. }