From abfebebdd5d3fd2f668dd5f71879d64f8bce1ebf Mon Sep 17 00:00:00 2001 From: owen Date: Sun, 1 Oct 2023 17:12:33 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BA=A4=E6=98=93=EF=BC=9A=E5=A2=9E=E5=8A=A0?= =?UTF-8?q?=20=E7=A7=AF=E5=88=86=E6=8A=B5=E6=89=A3=E8=AE=A1=E7=AE=97?= =?UTF-8?q?=E5=99=A8=20=E5=8D=95=E5=85=83=E6=B5=8B=E8=AF=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../TradePointUsePriceCalculator.java | 5 +- .../TradePointGiveCalculatorTest.java | 7 +- .../TradePointUsePriceCalculatorTest.java | 332 ++++++++++++++++++ 3 files changed, 339 insertions(+), 5 deletions(-) create mode 100644 yudao-module-mall/yudao-module-trade-biz/src/test/java/cn/iocoder/yudao/module/trade/service/price/calculator/TradePointUsePriceCalculatorTest.java diff --git a/yudao-module-mall/yudao-module-trade-biz/src/main/java/cn/iocoder/yudao/module/trade/service/price/calculator/TradePointUsePriceCalculator.java b/yudao-module-mall/yudao-module-trade-biz/src/main/java/cn/iocoder/yudao/module/trade/service/price/calculator/TradePointUsePriceCalculator.java index 1ba0d3d19..aae6e5f53 100644 --- a/yudao-module-mall/yudao-module-trade-biz/src/main/java/cn/iocoder/yudao/module/trade/service/price/calculator/TradePointUsePriceCalculator.java +++ b/yudao-module-mall/yudao-module-trade-biz/src/main/java/cn/iocoder/yudao/module/trade/service/price/calculator/TradePointUsePriceCalculator.java @@ -20,7 +20,6 @@ import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionU import static cn.iocoder.yudao.framework.common.util.collection.CollectionUtils.filterList; import static cn.iocoder.yudao.module.trade.enums.ErrorCodeConstants.PRICE_CALCULATE_PAY_PRICE_ILLEGAL; -// TODO @疯狂:搞个单测,嘿嘿; /** * 使用积分的 {@link TradePriceCalculator} 实现类 * @@ -38,6 +37,8 @@ public class TradePointUsePriceCalculator implements TradePriceCalculator { @Override public void calculate(TradePriceCalculateReqBO param, TradePriceCalculateRespBO result) { + // 默认使用积分为 0 + result.setUsePoint(0); // 1.1 校验是否使用积分 if (!BooleanUtil.isTrue(param.getPointStatus())) { result.setUsePoint(0); @@ -78,7 +79,7 @@ public class TradePointUsePriceCalculator implements TradePriceCalculator { private boolean isDeductPointEnable(MemberConfigRespDTO config) { return config != null && - !BooleanUtil.isTrue(config.getPointTradeDeductEnable()) && // 积分功能是否启用 + BooleanUtil.isTrue(config.getPointTradeDeductEnable()) && // 积分功能是否启用 config.getPointTradeDeductUnitPrice() != null && config.getPointTradeDeductUnitPrice() > 0; // 有没有配置:1 积分抵扣多少分 } diff --git a/yudao-module-mall/yudao-module-trade-biz/src/test/java/cn/iocoder/yudao/module/trade/service/price/calculator/TradePointGiveCalculatorTest.java b/yudao-module-mall/yudao-module-trade-biz/src/test/java/cn/iocoder/yudao/module/trade/service/price/calculator/TradePointGiveCalculatorTest.java index e203e6614..c0c21c36e 100644 --- a/yudao-module-mall/yudao-module-trade-biz/src/test/java/cn/iocoder/yudao/module/trade/service/price/calculator/TradePointGiveCalculatorTest.java +++ b/yudao-module-mall/yudao-module-trade-biz/src/test/java/cn/iocoder/yudao/module/trade/service/price/calculator/TradePointGiveCalculatorTest.java @@ -35,7 +35,7 @@ public class TradePointGiveCalculatorTest extends BaseMockitoUnitTest { // 准备参数 TradePriceCalculateReqBO param = new TradePriceCalculateReqBO() - .setUserId(233L).setCouponId(1024L) + .setUserId(233L) .setItems(asList( new TradePriceCalculateReqBO.Item().setSkuId(10L).setCount(2).setSelected(true), // 全局积分 new TradePriceCalculateReqBO.Item().setSkuId(20L).setCount(3).setSelected(true), // 全局积分 + SKU 积分 @@ -61,8 +61,9 @@ public class TradePointGiveCalculatorTest extends BaseMockitoUnitTest { TradePriceCalculatorHelper.recountAllPrice(result); // mock 方法(积分配置 信息) - MemberConfigRespDTO memberConfig = randomPojo(MemberConfigRespDTO.class, o -> o.setPointTradeDeductEnable(true) - .setPointTradeGivePoint(100)); + MemberConfigRespDTO memberConfig = randomPojo(MemberConfigRespDTO.class, + o -> o.setPointTradeDeductEnable(true) // 启用积分折扣 + .setPointTradeGivePoint(100)); // 1 元赠送多少分 when(memberConfigApi.getConfig()).thenReturn(memberConfig); // 调用 diff --git a/yudao-module-mall/yudao-module-trade-biz/src/test/java/cn/iocoder/yudao/module/trade/service/price/calculator/TradePointUsePriceCalculatorTest.java b/yudao-module-mall/yudao-module-trade-biz/src/test/java/cn/iocoder/yudao/module/trade/service/price/calculator/TradePointUsePriceCalculatorTest.java new file mode 100644 index 000000000..5d59c781c --- /dev/null +++ b/yudao-module-mall/yudao-module-trade-biz/src/test/java/cn/iocoder/yudao/module/trade/service/price/calculator/TradePointUsePriceCalculatorTest.java @@ -0,0 +1,332 @@ +package cn.iocoder.yudao.module.trade.service.price.calculator; + +import cn.iocoder.yudao.framework.test.core.ut.BaseMockitoUnitTest; +import cn.iocoder.yudao.module.member.api.config.MemberConfigApi; +import cn.iocoder.yudao.module.member.api.config.dto.MemberConfigRespDTO; +import cn.iocoder.yudao.module.member.api.user.MemberUserApi; +import cn.iocoder.yudao.module.member.api.user.dto.MemberUserRespDTO; +import cn.iocoder.yudao.module.promotion.enums.common.PromotionTypeEnum; +import cn.iocoder.yudao.module.trade.enums.order.TradeOrderTypeEnum; +import cn.iocoder.yudao.module.trade.service.price.bo.TradePriceCalculateReqBO; +import cn.iocoder.yudao.module.trade.service.price.bo.TradePriceCalculateRespBO; +import org.junit.jupiter.api.Test; +import org.mockito.InjectMocks; +import org.mockito.Mock; + +import java.util.ArrayList; + +import static cn.iocoder.yudao.framework.test.core.util.RandomUtils.randomPojo; +import static java.util.Arrays.asList; +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertTrue; +import static org.mockito.Mockito.when; + +/** + * {@link TradePointUsePriceCalculator } 的单元测试类 + * + * @author owen + */ +public class TradePointUsePriceCalculatorTest extends BaseMockitoUnitTest { + + @InjectMocks + private TradePointUsePriceCalculator tradePointUsePriceCalculator; + + @Mock + private MemberConfigApi memberConfigApi; + @Mock + private MemberUserApi memberUserApi; + + @Test + public void testCalculate_success() { + // 准备参数 + TradePriceCalculateReqBO param = new TradePriceCalculateReqBO() + .setUserId(233L).setPointStatus(true) // 是否使用积分 + .setItems(asList( + new TradePriceCalculateReqBO.Item().setSkuId(10L).setCount(2).setSelected(true), // 使用积分 + new TradePriceCalculateReqBO.Item().setSkuId(20L).setCount(3).setSelected(true), // 使用积分 + new TradePriceCalculateReqBO.Item().setSkuId(30L).setCount(5).setSelected(false) // 未选中,不使用积分 + )); + TradePriceCalculateRespBO result = new TradePriceCalculateRespBO() + .setType(TradeOrderTypeEnum.NORMAL.getType()) + .setPrice(new TradePriceCalculateRespBO.Price()) + .setPromotions(new ArrayList<>()) + .setItems(asList( + new TradePriceCalculateRespBO.OrderItem().setSkuId(10L).setCount(2).setSelected(true) + .setPrice(100).setSpuId(1L), + new TradePriceCalculateRespBO.OrderItem().setSkuId(20L).setCount(3).setSelected(true) + .setPrice(50).setSpuId(2L), + new TradePriceCalculateRespBO.OrderItem().setSkuId(30L).setCount(5).setSelected(false) + .setPrice(30).setSpuId(3L) + )); + // 保证价格被初始化上 + TradePriceCalculatorHelper.recountPayPrice(result.getItems()); + TradePriceCalculatorHelper.recountAllPrice(result); + + // mock 方法(积分配置 信息) + MemberConfigRespDTO memberConfig = randomPojo(MemberConfigRespDTO.class, + o -> o.setPointTradeDeductEnable(true) // 启用积分折扣 + .setPointTradeDeductUnitPrice(1) // 1 积分抵扣多少金额(单位分) + .setPointTradeDeductMaxPrice(100)); // 积分抵扣最大值 + when(memberConfigApi.getConfig()).thenReturn(memberConfig); + // mock 方法(会员 信息) + MemberUserRespDTO user = randomPojo(MemberUserRespDTO.class, o -> o.setId(param.getUserId()).setPoint(100)); + when(memberUserApi.getUser(user.getId())).thenReturn(user); + + // 调用 + tradePointUsePriceCalculator.calculate(param, result); + // 断言:使用了多少积分 + assertEquals(result.getUsePoint(), 100); + // 断言:Price 部分 + TradePriceCalculateRespBO.Price price = result.getPrice(); + assertEquals(price.getTotalPrice(), 350); + assertEquals(price.getPayPrice(), 250); + assertEquals(price.getPointPrice(), 100); + // 断言:SKU 1 + TradePriceCalculateRespBO.OrderItem orderItem01 = result.getItems().get(0); + assertEquals(orderItem01.getSkuId(), 10L); + assertEquals(orderItem01.getCount(), 2); + assertEquals(orderItem01.getPrice(), 100); + assertEquals(orderItem01.getPointPrice(), 57); + assertEquals(orderItem01.getPayPrice(), 143); + // 断言:SKU 2 + TradePriceCalculateRespBO.OrderItem orderItem02 = result.getItems().get(1); + assertEquals(orderItem02.getSkuId(), 20L); + assertEquals(orderItem02.getCount(), 3); + assertEquals(orderItem02.getPrice(), 50); + assertEquals(orderItem02.getPointPrice(), 43); + assertEquals(orderItem02.getPayPrice(), 107); + // 断言:SKU 3 + TradePriceCalculateRespBO.OrderItem orderItem03 = result.getItems().get(2); + assertEquals(orderItem03.getSkuId(), 30L); + assertEquals(orderItem03.getCount(), 5); + assertEquals(orderItem03.getPrice(), 30); + assertEquals(orderItem03.getPointPrice(), 0); + assertEquals(orderItem03.getPayPrice(), 150); + // 断言:Promotion 部分 + assertEquals(result.getPromotions().size(), 1); + TradePriceCalculateRespBO.Promotion promotion01 = result.getPromotions().get(0); + assertEquals(promotion01.getId(), user.getId()); + assertEquals(promotion01.getName(), "积分抵扣"); + assertEquals(promotion01.getType(), PromotionTypeEnum.POINT.getType()); + assertEquals(promotion01.getTotalPrice(), 350); + assertEquals(promotion01.getDiscountPrice(), 100); + assertTrue(promotion01.getMatch()); + assertEquals(promotion01.getDescription(), "积分抵扣:省 1.00 元"); + assertEquals(promotion01.getItems().size(), 2); + TradePriceCalculateRespBO.PromotionItem promotionItem011 = promotion01.getItems().get(0); + assertEquals(promotionItem011.getSkuId(), 10L); + assertEquals(promotionItem011.getTotalPrice(), 200); + assertEquals(promotionItem011.getDiscountPrice(), 57); + TradePriceCalculateRespBO.PromotionItem promotionItem012 = promotion01.getItems().get(1); + assertEquals(promotionItem012.getSkuId(), 20L); + assertEquals(promotionItem012.getTotalPrice(), 150); + assertEquals(promotionItem012.getDiscountPrice(), 43); + } + + /** + * 当用户积分充足时,抵扣的金额为:配置表的“积分抵扣最大值” + */ + @Test + public void testCalculate_TradeDeductMaxPrice() { + // 准备参数 + TradePriceCalculateReqBO param = new TradePriceCalculateReqBO() + .setUserId(233L).setPointStatus(true) // 是否使用积分 + .setItems(asList( + new TradePriceCalculateReqBO.Item().setSkuId(10L).setCount(2).setSelected(true), // 使用积分 + new TradePriceCalculateReqBO.Item().setSkuId(20L).setCount(3).setSelected(true), // 使用积分 + new TradePriceCalculateReqBO.Item().setSkuId(30L).setCount(5).setSelected(false) // 未选中,不使用积分 + )); + TradePriceCalculateRespBO result = new TradePriceCalculateRespBO() + .setType(TradeOrderTypeEnum.NORMAL.getType()) + .setPrice(new TradePriceCalculateRespBO.Price()) + .setPromotions(new ArrayList<>()) + .setItems(asList( + new TradePriceCalculateRespBO.OrderItem().setSkuId(10L).setCount(2).setSelected(true) + .setPrice(100).setSpuId(1L), + new TradePriceCalculateRespBO.OrderItem().setSkuId(20L).setCount(3).setSelected(true) + .setPrice(50).setSpuId(2L), + new TradePriceCalculateRespBO.OrderItem().setSkuId(30L).setCount(5).setSelected(false) + .setPrice(30).setSpuId(3L) + )); + // 保证价格被初始化上 + TradePriceCalculatorHelper.recountPayPrice(result.getItems()); + TradePriceCalculatorHelper.recountAllPrice(result); + + // mock 方法(积分配置 信息) + MemberConfigRespDTO memberConfig = randomPojo(MemberConfigRespDTO.class, + o -> o.setPointTradeDeductEnable(true) // 启用积分折扣 + .setPointTradeDeductUnitPrice(1) // 1 积分抵扣多少金额(单位分) + .setPointTradeDeductMaxPrice(50)); // 积分抵扣最大值 + when(memberConfigApi.getConfig()).thenReturn(memberConfig); + // mock 方法(会员 信息) + MemberUserRespDTO user = randomPojo(MemberUserRespDTO.class, o -> o.setId(param.getUserId()).setPoint(100)); + when(memberUserApi.getUser(user.getId())).thenReturn(user); + + // 调用 + tradePointUsePriceCalculator.calculate(param, result); + // 断言:使用了多少积分 + assertEquals(result.getUsePoint(), 50); + // 断言:Price 部分 + TradePriceCalculateRespBO.Price price = result.getPrice(); + assertEquals(price.getTotalPrice(), 350); + assertEquals(price.getPayPrice(), 300); + assertEquals(price.getPointPrice(), 50); + // 断言:SKU 1 + TradePriceCalculateRespBO.OrderItem orderItem01 = result.getItems().get(0); + assertEquals(orderItem01.getSkuId(), 10L); + assertEquals(orderItem01.getCount(), 2); + assertEquals(orderItem01.getPrice(), 100); + assertEquals(orderItem01.getPointPrice(), 28); + assertEquals(orderItem01.getPayPrice(), 172); + // 断言:SKU 2 + TradePriceCalculateRespBO.OrderItem orderItem02 = result.getItems().get(1); + assertEquals(orderItem02.getSkuId(), 20L); + assertEquals(orderItem02.getCount(), 3); + assertEquals(orderItem02.getPrice(), 50); + assertEquals(orderItem02.getPointPrice(), 22); + assertEquals(orderItem02.getPayPrice(), 128); + // 断言:SKU 3 + TradePriceCalculateRespBO.OrderItem orderItem03 = result.getItems().get(2); + assertEquals(orderItem03.getSkuId(), 30L); + assertEquals(orderItem03.getCount(), 5); + assertEquals(orderItem03.getPrice(), 30); + assertEquals(orderItem03.getPointPrice(), 0); + assertEquals(orderItem03.getPayPrice(), 150); + // 断言:Promotion 部分 + assertEquals(result.getPromotions().size(), 1); + TradePriceCalculateRespBO.Promotion promotion01 = result.getPromotions().get(0); + assertEquals(promotion01.getId(), user.getId()); + assertEquals(promotion01.getName(), "积分抵扣"); + assertEquals(promotion01.getType(), PromotionTypeEnum.POINT.getType()); + assertEquals(promotion01.getTotalPrice(), 350); + assertEquals(promotion01.getDiscountPrice(), 50); + assertTrue(promotion01.getMatch()); + assertEquals(promotion01.getDescription(), "积分抵扣:省 0.50 元"); + assertEquals(promotion01.getItems().size(), 2); + TradePriceCalculateRespBO.PromotionItem promotionItem011 = promotion01.getItems().get(0); + assertEquals(promotionItem011.getSkuId(), 10L); + assertEquals(promotionItem011.getTotalPrice(), 200); + assertEquals(promotionItem011.getDiscountPrice(), 28); + TradePriceCalculateRespBO.PromotionItem promotionItem012 = promotion01.getItems().get(1); + assertEquals(promotionItem012.getSkuId(), 20L); + assertEquals(promotionItem012.getTotalPrice(), 150); + assertEquals(promotionItem012.getDiscountPrice(), 22); + } + + /** + * 订单不使用积分,不会产生优惠 + */ + @Test + public void testCalculate_PointStatusFalse() { + // 准备参数 + TradePriceCalculateReqBO param = new TradePriceCalculateReqBO() + .setUserId(233L).setPointStatus(false) // 是否使用积分 + .setItems(asList( + new TradePriceCalculateReqBO.Item().setSkuId(10L).setCount(2).setSelected(true), // 使用积分 + new TradePriceCalculateReqBO.Item().setSkuId(20L).setCount(3).setSelected(true), // 使用积分 + new TradePriceCalculateReqBO.Item().setSkuId(30L).setCount(5).setSelected(false) // 未选中,不使用积分 + )); + TradePriceCalculateRespBO result = new TradePriceCalculateRespBO() + .setType(TradeOrderTypeEnum.NORMAL.getType()) + .setPrice(new TradePriceCalculateRespBO.Price()) + .setPromotions(new ArrayList<>()) + .setItems(asList( + new TradePriceCalculateRespBO.OrderItem().setSkuId(10L).setCount(2).setSelected(true) + .setPrice(100).setSpuId(1L), + new TradePriceCalculateRespBO.OrderItem().setSkuId(20L).setCount(3).setSelected(true) + .setPrice(50).setSpuId(2L), + new TradePriceCalculateRespBO.OrderItem().setSkuId(30L).setCount(5).setSelected(false) + .setPrice(30).setSpuId(3L) + )); + // 保证价格被初始化上 + TradePriceCalculatorHelper.recountPayPrice(result.getItems()); + TradePriceCalculatorHelper.recountAllPrice(result); + + // 调用 + tradePointUsePriceCalculator.calculate(param, result); + // 断言:没有使用积分 + assertNotUsePoint(result); + } + + /** + * 会员积分不足,不会产生优惠 + */ + @Test + public void testCalculate_UserPointNotEnough() { + // 准备参数 + TradePriceCalculateReqBO param = new TradePriceCalculateReqBO() + .setUserId(233L).setPointStatus(true) // 是否使用积分 + .setItems(asList( + new TradePriceCalculateReqBO.Item().setSkuId(10L).setCount(2).setSelected(true), // 使用积分 + new TradePriceCalculateReqBO.Item().setSkuId(20L).setCount(3).setSelected(true), // 使用积分 + new TradePriceCalculateReqBO.Item().setSkuId(30L).setCount(5).setSelected(false) // 未选中,不使用积分 + )); + TradePriceCalculateRespBO result = new TradePriceCalculateRespBO() + .setType(TradeOrderTypeEnum.NORMAL.getType()) + .setPrice(new TradePriceCalculateRespBO.Price()) + .setPromotions(new ArrayList<>()) + .setItems(asList( + new TradePriceCalculateRespBO.OrderItem().setSkuId(10L).setCount(2).setSelected(true) + .setPrice(100).setSpuId(1L), + new TradePriceCalculateRespBO.OrderItem().setSkuId(20L).setCount(3).setSelected(true) + .setPrice(50).setSpuId(2L), + new TradePriceCalculateRespBO.OrderItem().setSkuId(30L).setCount(5).setSelected(false) + .setPrice(30).setSpuId(3L) + )); + // 保证价格被初始化上 + TradePriceCalculatorHelper.recountPayPrice(result.getItems()); + TradePriceCalculatorHelper.recountAllPrice(result); + + // mock 方法(积分配置 信息) + MemberConfigRespDTO memberConfig = randomPojo(MemberConfigRespDTO.class, + o -> o.setPointTradeDeductEnable(true) // 启用积分折扣 + .setPointTradeDeductUnitPrice(1) // 1 积分抵扣多少金额(单位分) + .setPointTradeDeductMaxPrice(100)); // 积分抵扣最大值 + when(memberConfigApi.getConfig()).thenReturn(memberConfig); + // mock 方法(会员 信息) + MemberUserRespDTO user = randomPojo(MemberUserRespDTO.class, o -> o.setId(param.getUserId()).setPoint(0)); + when(memberUserApi.getUser(user.getId())).thenReturn(user); + + // 调用 + tradePointUsePriceCalculator.calculate(param, result); + + // 断言:没有使用积分 + assertNotUsePoint(result); + } + + /** + * 断言:没有使用积分 + */ + private static void assertNotUsePoint(TradePriceCalculateRespBO result) { + // 断言:使用了多少积分 + assertEquals(result.getUsePoint(), 0); + // 断言:Price 部分 + TradePriceCalculateRespBO.Price price = result.getPrice(); + assertEquals(price.getTotalPrice(), 350); + assertEquals(price.getPayPrice(), 350); + assertEquals(price.getPointPrice(), 0); + // 断言:SKU 1 + TradePriceCalculateRespBO.OrderItem orderItem01 = result.getItems().get(0); + assertEquals(orderItem01.getSkuId(), 10L); + assertEquals(orderItem01.getCount(), 2); + assertEquals(orderItem01.getPrice(), 100); + assertEquals(orderItem01.getPointPrice(), 0); + assertEquals(orderItem01.getPayPrice(), 200); + // 断言:SKU 2 + TradePriceCalculateRespBO.OrderItem orderItem02 = result.getItems().get(1); + assertEquals(orderItem02.getSkuId(), 20L); + assertEquals(orderItem02.getCount(), 3); + assertEquals(orderItem02.getPrice(), 50); + assertEquals(orderItem02.getPointPrice(), 0); + assertEquals(orderItem02.getPayPrice(), 150); + // 断言:SKU 3 + TradePriceCalculateRespBO.OrderItem orderItem03 = result.getItems().get(2); + assertEquals(orderItem03.getSkuId(), 30L); + assertEquals(orderItem03.getCount(), 5); + assertEquals(orderItem03.getPrice(), 30); + assertEquals(orderItem03.getPointPrice(), 0); + assertEquals(orderItem03.getPayPrice(), 150); + // 断言:Promotion 部分 + assertEquals(result.getPromotions().size(), 0); + } +} \ No newline at end of file