PromotionCouponBatchServiceImplDiscountLimitTest.java 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. package com.ruoyi.system.service.impl;
  2. import com.ruoyi.common.exception.ServiceException;
  3. import com.ruoyi.system.domain.PromotionCouponBatch;
  4. import com.ruoyi.system.domain.PromotionCouponRule;
  5. import com.ruoyi.system.mapper.PosFoodMapper;
  6. import com.ruoyi.system.mapper.PromotionCouponBatchMapper;
  7. import com.ruoyi.system.mapper.PromotionCouponRuleMapper;
  8. import com.ruoyi.system.service.ISysConfigService;
  9. import org.junit.jupiter.api.BeforeEach;
  10. import org.junit.jupiter.api.Test;
  11. import org.junit.jupiter.api.extension.ExtendWith;
  12. import org.mockito.Mock;
  13. import org.mockito.junit.jupiter.MockitoExtension;
  14. import org.springframework.test.util.ReflectionTestUtils;
  15. import java.math.BigDecimal;
  16. import static org.junit.jupiter.api.Assertions.assertThrows;
  17. import static org.mockito.ArgumentMatchers.any;
  18. import static org.mockito.Mockito.never;
  19. import static org.mockito.Mockito.verify;
  20. import static org.mockito.Mockito.verifyNoInteractions;
  21. import static org.mockito.Mockito.when;
  22. @ExtendWith(MockitoExtension.class)
  23. class PromotionCouponBatchServiceImplDiscountLimitTest {
  24. @Mock
  25. private PromotionCouponBatchMapper batchMapper;
  26. @Mock
  27. private PromotionCouponRuleMapper ruleMapper;
  28. @Mock
  29. private ISysConfigService configService;
  30. @Mock
  31. private PosFoodMapper posFoodMapper;
  32. private PromotionCouponBatchServiceImpl service;
  33. @BeforeEach
  34. void setUp() {
  35. when(configService.selectConfigByKey("promotion.discount.limit.enabled"))
  36. .thenReturn("true");
  37. when(configService.selectConfigByKey("promotion.discount.limit.ratio"))
  38. .thenReturn("20");
  39. PromotionDiscountLimitChecker checker = new PromotionDiscountLimitChecker();
  40. ReflectionTestUtils.setField(checker, "configService", configService);
  41. ReflectionTestUtils.setField(checker, "posFoodMapper", posFoodMapper);
  42. service = new PromotionCouponBatchServiceImpl();
  43. ReflectionTestUtils.setField(service, "promotionCouponBatchMapper", batchMapper);
  44. ReflectionTestUtils.setField(service, "ruleMapper", ruleMapper);
  45. ReflectionTestUtils.setField(service, "discountLimitChecker", checker);
  46. }
  47. @Test
  48. void createRejectsOverLimitCouponBeforeAnyDatabaseWrite() {
  49. PromotionCouponBatch batch = batch(null, 1, 100, 0);
  50. PromotionCouponRule rule = fullReductionRule("100", "21");
  51. assertThrows(ServiceException.class, () -> service.createBatch(batch, rule));
  52. verifyNoInteractions(batchMapper, ruleMapper);
  53. }
  54. @Test
  55. void unreceivedCouponUpdateRejectsOverLimitRuleBeforeReplacingStoredData() {
  56. PromotionCouponBatch existing = batch(7L, 1, 100, 0);
  57. existing.setStatus(0);
  58. existing.setRemainCount(100);
  59. when(batchMapper.selectById(7L)).thenReturn(existing);
  60. PromotionCouponBatch update = batch(7L, null, 100, null);
  61. assertThrows(ServiceException.class,
  62. () -> service.updateBatch(update, fullReductionRule("100", "21")));
  63. verify(batchMapper).selectById(7L);
  64. verify(batchMapper, never()).updateById(any(PromotionCouponBatch.class));
  65. verifyNoInteractions(ruleMapper);
  66. }
  67. private PromotionCouponBatch batch(Long id, Integer type, Integer totalCount,
  68. Integer receivedCount) {
  69. PromotionCouponBatch batch = new PromotionCouponBatch();
  70. batch.setId(id);
  71. batch.setCouponType(type);
  72. batch.setTotalCount(totalCount);
  73. batch.setReceivedCount(receivedCount);
  74. return batch;
  75. }
  76. private PromotionCouponRule fullReductionRule(String threshold, String amount) {
  77. PromotionCouponRule rule = new PromotionCouponRule();
  78. rule.setThreshold(new BigDecimal(threshold));
  79. rule.setAmount(new BigDecimal(amount));
  80. return rule;
  81. }
  82. }