package com.ruoyi.app.order; import com.ruoyi.app.order.dto.OrderCreateInput; import com.ruoyi.app.order.dto.OrderCreatItem; import com.ruoyi.common.exception.ServiceException; import com.ruoyi.common.utils.MessageUtils; import com.ruoyi.system.domain.InfoUser; import com.ruoyi.system.domain.PosStore; import com.ruoyi.system.service.IInfoAddressService; import com.ruoyi.system.service.IPosStoreService; import com.ruoyi.system.utils.JwtUtil; import org.junit.jupiter.api.*; import org.junit.jupiter.params.ParameterizedTest; import org.junit.jupiter.params.provider.ValueSource; import org.mockito.*; import org.springframework.test.util.ReflectionTestUtils; import java.util.List; import static org.junit.jupiter.api.Assertions.*; import static org.mockito.Mockito.*; /** 新订单不得把带分角的运费写进订单,再流入骑手/商家账单。 */ class OrderWholeYuanFreightTest { MockedStatic messages; MockedConstruction tokens; @BeforeEach void setup() { messages=mockStatic(MessageUtils.class); messages.when(() -> MessageUtils.message(anyString())).thenAnswer(i -> i.getArgument(0)); tokens=mockConstruction(JwtUtil.class,(jwt, ctx)->when(jwt.getusid("token")).thenReturn("9")); } @AfterEach void cleanup() { tokens.close(); messages.close(); } OrderCreateInput input(double freight) { OrderCreatItem item=new OrderCreatItem(); item.setMdId(101L); item.setShId(9L); item.setAmount(100); item.setFreight(freight); item.setType(0L); OrderCreateInput input=new OrderCreateInput(); input.setType(0L); input.setItems(List.of(item)); return input; } InfoUser merchant() { InfoUser user=new InfoUser(); user.setUserId(9L); user.setUserType("1"); return user; } PosOrderShOprateController merchantController() { PosOrderShOprateController controller=new PosOrderShOprateController(); IPosStoreService stores=mock(IPosStoreService.class); PosStore store=new PosStore(); store.setUserId(9L); when(stores.getById(101L)).thenReturn(store); ReflectionTestUtils.setField(controller,"posStoreService",stores); return controller; } @ParameterizedTest @ValueSource(doubles={40.5,0.01,-1,Double.NaN,Double.POSITIVE_INFINITY}) void merchantCannotCreateFractionalOrInvalidFreight(double freight) { ServiceException error=assertThrows(ServiceException.class, ()->merchantController().validateMerchantOrderInput(merchant(),input(freight))); assertEquals("order.freight.wholeYuan.required",error.getMessage()); } @ParameterizedTest @ValueSource(doubles={0,40,100}) void wholeYuanFreightKeepsExistingMerchantFlow(double freight) { assertDoesNotThrow(()->merchantController().validateMerchantOrderInput(merchant(),input(freight))); } @Test void userRejectsFractionalFreightBeforeAddressAndOrderWrites() { UserOrderController controller=new UserOrderController(); IInfoAddressService addresses=mock(IInfoAddressService.class); ReflectionTestUtils.setField(controller,"infoAddressService",addresses); ServiceException error=assertThrows(ServiceException.class,()->controller.createOrder("token",input(40.5))); assertEquals("order.freight.wholeYuan.required",error.getMessage()); verifyNoInteractions(addresses); } @Test void removedLegacyCreateEntryHasNoControllerMethodOrPostMapping() { for (java.lang.reflect.Method method : PosOrderController.class.getDeclaredMethods()) { assertNotEquals("addorder", method.getName()); var mapping = method.getAnnotation(org.springframework.web.bind.annotation.PostMapping.class); if (mapping != null) { assertFalse(java.util.Arrays.asList(mapping.value()).contains("/addorder")); assertFalse(java.util.Arrays.asList(mapping.path()).contains("/addorder")); } } } }