OrderWholeYuanFreightTest.java 3.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  1. package com.ruoyi.app.order;
  2. import com.ruoyi.app.order.dto.OrderCreateInput;
  3. import com.ruoyi.app.order.dto.OrderCreatItem;
  4. import com.ruoyi.common.exception.ServiceException;
  5. import com.ruoyi.common.utils.MessageUtils;
  6. import com.ruoyi.system.domain.InfoUser;
  7. import com.ruoyi.system.domain.PosStore;
  8. import com.ruoyi.system.service.IInfoAddressService;
  9. import com.ruoyi.system.service.IPosStoreService;
  10. import com.ruoyi.system.utils.JwtUtil;
  11. import org.junit.jupiter.api.*;
  12. import org.junit.jupiter.params.ParameterizedTest;
  13. import org.junit.jupiter.params.provider.ValueSource;
  14. import org.mockito.*;
  15. import org.springframework.test.util.ReflectionTestUtils;
  16. import java.util.List;
  17. import static org.junit.jupiter.api.Assertions.*;
  18. import static org.mockito.Mockito.*;
  19. /** 新订单不得把带分角的运费写进订单,再流入骑手/商家账单。 */
  20. class OrderWholeYuanFreightTest {
  21. MockedStatic<MessageUtils> messages;
  22. MockedConstruction<JwtUtil> tokens;
  23. @BeforeEach void setup() {
  24. messages=mockStatic(MessageUtils.class);
  25. messages.when(() -> MessageUtils.message(anyString())).thenAnswer(i -> i.getArgument(0));
  26. tokens=mockConstruction(JwtUtil.class,(jwt, ctx)->when(jwt.getusid("token")).thenReturn("9"));
  27. }
  28. @AfterEach void cleanup() { tokens.close(); messages.close(); }
  29. OrderCreateInput input(double freight) {
  30. OrderCreatItem item=new OrderCreatItem(); item.setMdId(101L); item.setShId(9L);
  31. item.setAmount(100); item.setFreight(freight); item.setType(0L);
  32. OrderCreateInput input=new OrderCreateInput(); input.setType(0L); input.setItems(List.of(item));
  33. return input;
  34. }
  35. InfoUser merchant() { InfoUser user=new InfoUser(); user.setUserId(9L); user.setUserType("1"); return user; }
  36. PosOrderShOprateController merchantController() {
  37. PosOrderShOprateController controller=new PosOrderShOprateController();
  38. IPosStoreService stores=mock(IPosStoreService.class);
  39. PosStore store=new PosStore(); store.setUserId(9L);
  40. when(stores.getById(101L)).thenReturn(store);
  41. ReflectionTestUtils.setField(controller,"posStoreService",stores);
  42. return controller;
  43. }
  44. @ParameterizedTest @ValueSource(doubles={40.5,0.01,-1,Double.NaN,Double.POSITIVE_INFINITY})
  45. void merchantCannotCreateFractionalOrInvalidFreight(double freight) {
  46. ServiceException error=assertThrows(ServiceException.class,
  47. ()->merchantController().validateMerchantOrderInput(merchant(),input(freight)));
  48. assertEquals("order.freight.wholeYuan.required",error.getMessage());
  49. }
  50. @ParameterizedTest @ValueSource(doubles={0,40,100})
  51. void wholeYuanFreightKeepsExistingMerchantFlow(double freight) {
  52. assertDoesNotThrow(()->merchantController().validateMerchantOrderInput(merchant(),input(freight)));
  53. }
  54. @Test void userRejectsFractionalFreightBeforeAddressAndOrderWrites() {
  55. UserOrderController controller=new UserOrderController();
  56. IInfoAddressService addresses=mock(IInfoAddressService.class);
  57. ReflectionTestUtils.setField(controller,"infoAddressService",addresses);
  58. ServiceException error=assertThrows(ServiceException.class,()->controller.createOrder("token",input(40.5)));
  59. assertEquals("order.freight.wholeYuan.required",error.getMessage());
  60. verifyNoInteractions(addresses);
  61. }
  62. @Test void removedLegacyCreateEntryHasNoControllerMethodOrPostMapping() {
  63. for (java.lang.reflect.Method method : PosOrderController.class.getDeclaredMethods()) {
  64. assertNotEquals("addorder", method.getName());
  65. var mapping = method.getAnnotation(org.springframework.web.bind.annotation.PostMapping.class);
  66. if (mapping != null) {
  67. assertFalse(java.util.Arrays.asList(mapping.value()).contains("/addorder"));
  68. assertFalse(java.util.Arrays.asList(mapping.path()).contains("/addorder"));
  69. }
  70. }
  71. }
  72. }