RiderIncomeServiceTest.java 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  1. package com.ruoyi.app.pay;
  2. import com.fasterxml.jackson.databind.ObjectMapper;
  3. import com.ruoyi.common.exception.ServiceException;
  4. import com.ruoyi.common.utils.MessageUtils;
  5. import com.ruoyi.system.domain.InfoUser;
  6. import com.ruoyi.system.domain.vo.RiderIncomeItem;
  7. import com.ruoyi.system.domain.vo.RiderIncomeSummary;
  8. import com.ruoyi.system.mapper.RiderIncomeMapper;
  9. import com.ruoyi.system.service.IInfoUserService;
  10. import org.junit.jupiter.api.AfterEach;
  11. import org.junit.jupiter.api.BeforeEach;
  12. import org.junit.jupiter.api.Test;
  13. import org.mockito.MockedStatic;
  14. import java.math.BigDecimal;
  15. import java.util.List;
  16. import static org.junit.jupiter.api.Assertions.*;
  17. import static org.mockito.ArgumentMatchers.*;
  18. import static org.mockito.Mockito.*;
  19. class RiderIncomeServiceTest {
  20. private final RiderIncomeMapper mapper = mock(RiderIncomeMapper.class);
  21. private final IInfoUserService users = mock(IInfoUserService.class);
  22. private final RiderIncomeService service = new RiderIncomeService(mapper, users);
  23. private MockedStatic<MessageUtils> messages;
  24. private InfoUser rider;
  25. private RiderIncomeSummary summary;
  26. @BeforeEach
  27. void setUp() {
  28. messages = mockStatic(MessageUtils.class);
  29. messages.when(() -> MessageUtils.message(anyString())).thenAnswer(call -> call.getArgument(0));
  30. rider = new InfoUser(); rider.setUserId(7L); rider.setUserType("2");
  31. rider.setStatus("0"); rider.setDelFlag("0");
  32. when(users.selectInfoUserByUserId(7L)).thenReturn(rider);
  33. summary = new RiderIncomeSummary(); summary.setTotalIncome(new BigDecimal("1250.00"));
  34. summary.setTotalOrderCount(18L); summary.setFoodOrderCount(12L); summary.setFlashOrderCount(6L);
  35. when(mapper.summarize(eq(7L), anyString(), anyString())).thenReturn(summary);
  36. when(mapper.selectPage(eq(7L), anyString(), anyString(), anyString(), anyLong(), anyInt()))
  37. .thenReturn(List.of());
  38. }
  39. @AfterEach void tearDown() { messages.close(); }
  40. @Test
  41. void categoriesOnlyChangeListAndTotalNotTopSummary() {
  42. for (String source : List.of("ALL", "FOOD", "FLASH")) {
  43. var result = service.list(7L, "2026-11-05 00:00:00", "2026-11-05 23:59:59", source, 2, 10);
  44. assertSame(summary, result.summary());
  45. assertEquals(source.equals("ALL") ? 18L : source.equals("FOOD") ? 12L : 6L, result.total());
  46. verify(mapper).selectPage(7L, "2026-11-05 00:00:00", "2026-11-05 23:59:59", source, 10L, 10);
  47. }
  48. verify(mapper, times(3)).summarize(7L, "2026-11-05 00:00:00", "2026-11-05 23:59:59");
  49. }
  50. @Test
  51. void allTimeHasNoTimeBoundsAndStillKeepsSummaryIndependent() {
  52. when(mapper.summarize(7L, null, null)).thenReturn(summary);
  53. when(mapper.selectPage(7L, null, null, "FLASH", 0L, 20)).thenReturn(List.of());
  54. var result = service.list(7L, null, null, "FLASH", 1, 20);
  55. assertNull(result.startTime()); assertNull(result.endTime());
  56. assertSame(summary, result.summary()); assertEquals(6L, result.total());
  57. verify(mapper).selectPage(7L, null, null, "FLASH", 0L, 20);
  58. }
  59. @Test
  60. void explicitCrossYearRangeIsNotReinterpreted() {
  61. var result = service.list(7L, "2025-12-29 00:00:00", "2026-01-04 23:59:59", "ALL", 1, 20);
  62. assertEquals("2025-12-29 00:00:00", result.startTime());
  63. assertEquals("2026-01-04 23:59:59", result.endTime());
  64. verify(mapper).summarize(7L, "2025-12-29 00:00:00", "2026-01-04 23:59:59");
  65. }
  66. @Test
  67. void leapDateAndYearEndArePassedThroughWithoutAddingADay() {
  68. service.list(7L, "2024-02-01 00:00:00", "2024-02-29 23:59:59", "ALL", 1, 20);
  69. verify(mapper).summarize(7L, "2024-02-01 00:00:00", "2024-02-29 23:59:59");
  70. service.list(7L, "9999-12-31 00:00:00", "9999-12-31 23:59:59", "ALL", 1, 20);
  71. verify(mapper).summarize(7L, "9999-12-31 00:00:00", "9999-12-31 23:59:59");
  72. }
  73. @Test
  74. void arbitraryTimeRangeIsUsedExactlyAndResponseHasNoPeriodOptions() {
  75. var result = service.list(7L, "2026-11-05 12:30:15", "2026-11-05 18:45:30", "ALL", 1, 20);
  76. assertEquals("2026-11-05 12:30:15", result.startTime()); assertEquals("2026-11-05 18:45:30", result.endTime());
  77. verify(mapper).summarize(7L, "2026-11-05 12:30:15", "2026-11-05 18:45:30");
  78. var json = new ObjectMapper().valueToTree(result);
  79. assertFalse(json.has("period")); assertFalse(json.has("periodOptions"));
  80. assertFalse(json.has("startDate")); assertFalse(json.has("endDate"));
  81. }
  82. @Test
  83. void invalidTimesRangesCategoriesAndPaginationAreRejectedBeforeSql() {
  84. String valid = "2026-11-05 12:00:00";
  85. for (String time : List.of("2026-02-30 00:00:00", "2026-1-1 00:00:00", "", "2026-11-05",
  86. "2026-11-05 23:59", "2026-11-05 24:00:00", "2026-11-05 12:60:00", "2026-11-05 12:00:60",
  87. "2026-11-05 12:00:00.123", "2026-11-05 12:00:00' OR 1=1")) {
  88. assertThrows(ServiceException.class, () -> service.list(7L, time, valid, "ALL", 1, 20));
  89. assertThrows(ServiceException.class, () -> service.list(7L, valid, time, "ALL", 1, 20));
  90. }
  91. assertThrows(ServiceException.class, () -> service.list(7L, null, valid, "ALL", 1, 20));
  92. assertThrows(ServiceException.class, () -> service.list(7L, valid, null, "ALL", 1, 20));
  93. assertThrows(ServiceException.class, () -> service.list(7L, "2026-11-05 12:00:01", valid, "ALL", 1, 20));
  94. assertThrows(ServiceException.class, () -> service.list(7L, "0999-01-01 00:00:00", valid, "ALL", 1, 20));
  95. assertThrows(ServiceException.class, () -> service.list(7L, valid, valid, "OTHER", 1, 20));
  96. assertThrows(ServiceException.class, () -> service.list(7L, valid, valid, "ALL", 0, 20));
  97. assertThrows(ServiceException.class, () -> service.list(7L, valid, valid, "ALL", 1, 0));
  98. assertThrows(ServiceException.class, () -> service.list(7L, valid, valid, "ALL", 1, 101));
  99. verifyNoInteractions(mapper);
  100. }
  101. @Test
  102. void nonRidersDeletedDisabledAndMissingAccountsCannotRead() {
  103. for (String type : List.of("0", "1", "3", "4", "5")) {
  104. rider.setUserType(type);
  105. assertThrows(ServiceException.class, () -> service.list(7L, null, null, "ALL", 1, 20));
  106. assertThrows(ServiceException.class, () -> service.detail(7L, 1L));
  107. }
  108. rider.setUserType("2"); rider.setStatus("1");
  109. assertThrows(ServiceException.class, () -> service.detail(7L, 1L));
  110. rider.setStatus("0"); rider.setDelFlag("2");
  111. assertThrows(ServiceException.class, () -> service.detail(7L, 1L));
  112. when(users.selectInfoUserByUserId(7L)).thenReturn(null);
  113. assertThrows(ServiceException.class, () -> service.detail(7L, 1L));
  114. verifyNoInteractions(mapper);
  115. }
  116. @Test
  117. void detailUsesAuthenticatedRiderAndHidesUnavailableRecords() {
  118. var item = new RiderIncomeItem(); item.setBillId(1L);
  119. when(mapper.selectDetail(7L, 1L)).thenReturn(item);
  120. assertSame(item, service.detail(7L, 1L));
  121. assertEquals("rider.income.not.found", assertThrows(ServiceException.class,
  122. () -> service.detail(7L, 2L)).getMessage());
  123. assertThrows(ServiceException.class, () -> service.detail(7L, 0L));
  124. }
  125. @Test
  126. void largePageOffsetDoesNotOverflowInteger() {
  127. service.list(7L, "2026-11-05 00:00:00", "2026-11-05 23:59:59", "ALL", Integer.MAX_VALUE, 100);
  128. verify(mapper).selectPage(7L, "2026-11-05 00:00:00", "2026-11-05 23:59:59", "ALL", 214748364600L, 100);
  129. }
  130. @Test
  131. void responseHasNoWalletWithdrawalTipOrSettlementFields() throws Exception {
  132. var item = new RiderIncomeItem(); item.setIncomeAmount(new BigDecimal("120.00"));
  133. var json = new ObjectMapper().valueToTree(item);
  134. assertEquals(6, json.size());
  135. assertTrue(json.has("billId")); assertTrue(json.has("orderNo")); assertTrue(json.has("orderSource"));
  136. assertFalse(json.has("orderId")); assertTrue(json.has("incomeTime")); assertTrue(json.has("collectedAmount"));
  137. assertEquals(0, new BigDecimal("120.00").compareTo(json.get("incomeAmount").decimalValue()));
  138. }
  139. }