mirror of
https://gitee.com/huangge1199_admin/vue-pro.git
synced 2024-11-23 07:41:53 +08:00
mall + trade:增加 TradeRewardActivityPriceCalculatorTest 的单元测试
This commit is contained in:
parent
0d08c814ec
commit
73a781cbbe
@ -1,33 +1,24 @@
|
|||||||
package cn.iocoder.yudao.module.promotion.service.price;
|
package cn.iocoder.yudao.module.promotion.service.price;
|
||||||
|
|
||||||
import cn.hutool.core.map.MapUtil;
|
|
||||||
import cn.iocoder.yudao.framework.common.exception.ServiceException;
|
import cn.iocoder.yudao.framework.common.exception.ServiceException;
|
||||||
import cn.iocoder.yudao.framework.test.core.ut.BaseMockitoUnitTest;
|
import cn.iocoder.yudao.framework.test.core.ut.BaseMockitoUnitTest;
|
||||||
import cn.iocoder.yudao.module.product.api.sku.ProductSkuApi;
|
import cn.iocoder.yudao.module.product.api.sku.ProductSkuApi;
|
||||||
import cn.iocoder.yudao.module.product.api.sku.dto.ProductSkuRespDTO;
|
import cn.iocoder.yudao.module.product.api.sku.dto.ProductSkuRespDTO;
|
||||||
import cn.iocoder.yudao.module.promotion.api.price.dto.CouponMeetRespDTO;
|
import cn.iocoder.yudao.module.promotion.api.price.dto.CouponMeetRespDTO;
|
||||||
import cn.iocoder.yudao.module.promotion.api.price.dto.PriceCalculateReqDTO;
|
import cn.iocoder.yudao.module.promotion.api.price.dto.PriceCalculateReqDTO;
|
||||||
import cn.iocoder.yudao.module.promotion.api.price.dto.PriceCalculateRespDTO;
|
|
||||||
import cn.iocoder.yudao.module.promotion.dal.dataobject.coupon.CouponDO;
|
import cn.iocoder.yudao.module.promotion.dal.dataobject.coupon.CouponDO;
|
||||||
import cn.iocoder.yudao.module.promotion.dal.dataobject.reward.RewardActivityDO;
|
import cn.iocoder.yudao.module.promotion.enums.common.PromotionProductScopeEnum;
|
||||||
import cn.iocoder.yudao.module.promotion.enums.common.*;
|
|
||||||
import cn.iocoder.yudao.module.promotion.enums.coupon.CouponStatusEnum;
|
import cn.iocoder.yudao.module.promotion.enums.coupon.CouponStatusEnum;
|
||||||
import cn.iocoder.yudao.module.promotion.service.coupon.CouponService;
|
import cn.iocoder.yudao.module.promotion.service.coupon.CouponService;
|
||||||
import cn.iocoder.yudao.module.promotion.service.discount.DiscountActivityService;
|
|
||||||
import cn.iocoder.yudao.module.promotion.service.discount.bo.DiscountProductDetailBO;
|
|
||||||
import cn.iocoder.yudao.module.promotion.service.reward.RewardActivityService;
|
import cn.iocoder.yudao.module.promotion.service.reward.RewardActivityService;
|
||||||
import org.junit.jupiter.api.Test;
|
import org.junit.jupiter.api.Test;
|
||||||
import org.mockito.InjectMocks;
|
import org.mockito.InjectMocks;
|
||||||
import org.mockito.Mock;
|
import org.mockito.Mock;
|
||||||
|
|
||||||
import java.util.LinkedHashMap;
|
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
import java.util.Map;
|
|
||||||
import java.util.Set;
|
|
||||||
|
|
||||||
import static cn.iocoder.yudao.framework.common.util.collection.SetUtils.asSet;
|
import static cn.iocoder.yudao.framework.common.util.collection.SetUtils.asSet;
|
||||||
import static cn.iocoder.yudao.framework.test.core.util.AssertUtils.assertPojoEquals;
|
import static cn.iocoder.yudao.framework.test.core.util.AssertUtils.assertPojoEquals;
|
||||||
import static cn.iocoder.yudao.framework.test.core.util.RandomUtils.randomLongId;
|
|
||||||
import static cn.iocoder.yudao.framework.test.core.util.RandomUtils.randomPojo;
|
import static cn.iocoder.yudao.framework.test.core.util.RandomUtils.randomPojo;
|
||||||
import static cn.iocoder.yudao.module.promotion.enums.ErrorCodeConstants.COUPON_VALID_TIME_NOT_NOW;
|
import static cn.iocoder.yudao.module.promotion.enums.ErrorCodeConstants.COUPON_VALID_TIME_NOT_NOW;
|
||||||
import static java.util.Arrays.asList;
|
import static java.util.Arrays.asList;
|
||||||
@ -54,179 +45,12 @@ public class PriceServiceTest extends BaseMockitoUnitTest {
|
|||||||
@Mock
|
@Mock
|
||||||
private ProductSkuApi productSkuApi;
|
private ProductSkuApi productSkuApi;
|
||||||
|
|
||||||
/**
|
|
||||||
* 测试满减送活动,匹配的情况
|
|
||||||
*/
|
|
||||||
@Test
|
|
||||||
public void testCalculatePrice_rewardActivity() {
|
|
||||||
// 准备参数
|
|
||||||
PriceCalculateReqDTO calculateReqDTO = new PriceCalculateReqDTO().setUserId(randomLongId())
|
|
||||||
.setItems(asList(new PriceCalculateReqDTO.Item().setSkuId(10L).setCount(2),
|
|
||||||
new PriceCalculateReqDTO.Item().setSkuId(20L).setCount(3),
|
|
||||||
new PriceCalculateReqDTO.Item().setSkuId(30L).setCount(4)));
|
|
||||||
// mock 方法(商品 SKU 信息)
|
|
||||||
ProductSkuRespDTO productSku01 = randomPojo(ProductSkuRespDTO.class, o -> o.setId(10L).setPrice(100).setSpuId(1L));
|
|
||||||
ProductSkuRespDTO productSku02 = randomPojo(ProductSkuRespDTO.class, o -> o.setId(20L).setPrice(50).setSpuId(2L));
|
|
||||||
ProductSkuRespDTO productSku03 = randomPojo(ProductSkuRespDTO.class, o -> o.setId(30L).setPrice(30).setSpuId(3L));
|
|
||||||
when(productSkuApi.getSkuList(eq(asSet(10L, 20L, 30L)))).thenReturn(asList(productSku01, productSku02, productSku03));
|
|
||||||
// mock 方法(限时折扣 DiscountActivity 信息)
|
|
||||||
RewardActivityDO rewardActivity01 = randomPojo(RewardActivityDO.class, o -> o.setId(1000L).setName("活动 1000 号")
|
|
||||||
.setProductSpuIds(asList(10L, 20L)).setConditionType(PromotionConditionTypeEnum.PRICE.getType())
|
|
||||||
.setRules(singletonList(new RewardActivityDO.Rule().setLimit(200).setDiscountPrice(70))));
|
|
||||||
RewardActivityDO rewardActivity02 = randomPojo(RewardActivityDO.class, o -> o.setId(2000L).setName("活动 2000 号")
|
|
||||||
.setProductSpuIds(singletonList(30L)).setConditionType(PromotionConditionTypeEnum.COUNT.getType())
|
|
||||||
.setRules(asList(new RewardActivityDO.Rule().setLimit(1).setDiscountPrice(10),
|
|
||||||
new RewardActivityDO.Rule().setLimit(2).setDiscountPrice(60), // 最大可满足,因为是 4 个
|
|
||||||
new RewardActivityDO.Rule().setLimit(10).setDiscountPrice(100))));
|
|
||||||
Map<RewardActivityDO, Set<Long>> matchRewardActivities = new LinkedHashMap<>();
|
|
||||||
matchRewardActivities.put(rewardActivity01, asSet(1L, 2L));
|
|
||||||
matchRewardActivities.put(rewardActivity02, asSet(3L));
|
|
||||||
when(rewardActivityService.getMatchRewardActivities(eq(asSet(1L, 2L, 3L)))).thenReturn(matchRewardActivities);
|
|
||||||
|
|
||||||
// 调用
|
|
||||||
PriceCalculateRespDTO priceCalculate = priceService.calculatePrice(calculateReqDTO);
|
|
||||||
// 断言 Order 部分
|
|
||||||
PriceCalculateRespDTO.Order order = priceCalculate.getOrder();
|
|
||||||
assertEquals(order.getTotalPrice(), 470);
|
|
||||||
assertEquals(order.getDiscountPrice(), 130);
|
|
||||||
assertEquals(order.getPointPrice(), 0);
|
|
||||||
assertEquals(order.getDeliveryPrice(), 0);
|
|
||||||
assertEquals(order.getPayPrice(), 340);
|
|
||||||
assertNull(order.getCouponId());
|
|
||||||
// 断言 OrderItem 部分
|
|
||||||
assertEquals(order.getItems().size(), 3);
|
|
||||||
PriceCalculateRespDTO.OrderItem orderItem01 = order.getItems().get(0);
|
|
||||||
assertEquals(orderItem01.getSkuId(), 10L);
|
|
||||||
assertEquals(orderItem01.getCount(), 2);
|
|
||||||
assertEquals(orderItem01.getOriginalPrice(), 200);
|
|
||||||
assertEquals(orderItem01.getOriginalUnitPrice(), 100);
|
|
||||||
assertEquals(orderItem01.getDiscountPrice(), 0);
|
|
||||||
assertEquals(orderItem01.getPayPrice(), 200);
|
|
||||||
assertEquals(orderItem01.getOrderPartPrice(), 40);
|
|
||||||
assertEquals(orderItem01.getOrderDividePrice(), 160);
|
|
||||||
PriceCalculateRespDTO.OrderItem orderItem02 = order.getItems().get(1);
|
|
||||||
assertEquals(orderItem02.getSkuId(), 20L);
|
|
||||||
assertEquals(orderItem02.getCount(), 3);
|
|
||||||
assertEquals(orderItem02.getOriginalPrice(), 150);
|
|
||||||
assertEquals(orderItem02.getOriginalUnitPrice(), 50);
|
|
||||||
assertEquals(orderItem02.getDiscountPrice(), 0);
|
|
||||||
assertEquals(orderItem02.getPayPrice(), 150);
|
|
||||||
assertEquals(orderItem02.getOrderPartPrice(), 30);
|
|
||||||
assertEquals(orderItem02.getOrderDividePrice(), 120);
|
|
||||||
PriceCalculateRespDTO.OrderItem orderItem03 = order.getItems().get(2);
|
|
||||||
assertEquals(orderItem03.getSkuId(), 30L);
|
|
||||||
assertEquals(orderItem03.getCount(), 4);
|
|
||||||
assertEquals(orderItem03.getOriginalPrice(), 120);
|
|
||||||
assertEquals(orderItem03.getOriginalUnitPrice(), 30);
|
|
||||||
assertEquals(orderItem03.getDiscountPrice(), 0);
|
|
||||||
assertEquals(orderItem03.getPayPrice(), 120);
|
|
||||||
assertEquals(orderItem03.getOrderPartPrice(), 60);
|
|
||||||
assertEquals(orderItem03.getOrderDividePrice(), 60);
|
|
||||||
// 断言 Promotion 部分(第一个)
|
|
||||||
assertEquals(priceCalculate.getPromotions().size(), 2);
|
|
||||||
PriceCalculateRespDTO.Promotion promotion01 = priceCalculate.getPromotions().get(0);
|
|
||||||
assertEquals(promotion01.getId(), 1000L);
|
|
||||||
assertEquals(promotion01.getName(), "活动 1000 号");
|
|
||||||
assertEquals(promotion01.getType(), PromotionTypeEnum.REWARD_ACTIVITY.getType());
|
|
||||||
assertEquals(promotion01.getTotalPrice(), 350);
|
|
||||||
assertEquals(promotion01.getDiscountPrice(), 70);
|
|
||||||
assertTrue(promotion01.getMatch());
|
|
||||||
assertEquals(promotion01.getDescription(), "满减送:省 0.70 元");
|
|
||||||
assertEquals(promotion01.getItems().size(), 2);
|
|
||||||
PriceCalculateRespDTO.PromotionItem promotionItem011 = promotion01.getItems().get(0);
|
|
||||||
assertEquals(promotionItem011.getSkuId(), 10L);
|
|
||||||
assertEquals(promotionItem011.getOriginalPrice(), 200);
|
|
||||||
assertEquals(promotionItem011.getDiscountPrice(), 40);
|
|
||||||
PriceCalculateRespDTO.PromotionItem promotionItem012 = promotion01.getItems().get(1);
|
|
||||||
assertEquals(promotionItem012.getSkuId(), 20L);
|
|
||||||
assertEquals(promotionItem012.getOriginalPrice(), 150);
|
|
||||||
assertEquals(promotionItem012.getDiscountPrice(), 30);
|
|
||||||
// 断言 Promotion 部分(第二个)
|
|
||||||
PriceCalculateRespDTO.Promotion promotion02 = priceCalculate.getPromotions().get(1);
|
|
||||||
assertEquals(promotion02.getId(), 2000L);
|
|
||||||
assertEquals(promotion02.getName(), "活动 2000 号");
|
|
||||||
assertEquals(promotion02.getType(), PromotionTypeEnum.REWARD_ACTIVITY.getType());
|
|
||||||
assertEquals(promotion02.getTotalPrice(), 120);
|
|
||||||
assertEquals(promotion02.getDiscountPrice(), 60);
|
|
||||||
assertTrue(promotion02.getMatch());
|
|
||||||
assertEquals(promotion02.getDescription(), "满减送:省 0.60 元");
|
|
||||||
PriceCalculateRespDTO.PromotionItem promotionItem02 = promotion02.getItems().get(0);
|
|
||||||
assertEquals(promotion02.getItems().size(), 1);
|
|
||||||
assertEquals(promotionItem02.getSkuId(), 30L);
|
|
||||||
assertEquals(promotionItem02.getOriginalPrice(), 120);
|
|
||||||
assertEquals(promotionItem02.getDiscountPrice(), 60);
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 测试满减送活动,不匹配的情况
|
* 测试满减送活动,不匹配的情况
|
||||||
*/
|
*/
|
||||||
@Test
|
@Test
|
||||||
public void testCalculatePrice_rewardActivityNotMeet() {
|
public void testCalculatePrice_rewardActivityNotMeet() {
|
||||||
// 准备参数
|
|
||||||
PriceCalculateReqDTO calculateReqDTO = new PriceCalculateReqDTO().setUserId(randomLongId())
|
|
||||||
.setItems(asList(new PriceCalculateReqDTO.Item().setSkuId(10L).setCount(2),
|
|
||||||
new PriceCalculateReqDTO.Item().setSkuId(20L).setCount(3)));
|
|
||||||
// mock 方法(商品 SKU 信息)
|
|
||||||
ProductSkuRespDTO productSku01 = randomPojo(ProductSkuRespDTO.class, o -> o.setId(10L).setPrice(100).setSpuId(1L));
|
|
||||||
ProductSkuRespDTO productSku02 = randomPojo(ProductSkuRespDTO.class, o -> o.setId(20L).setPrice(50).setSpuId(2L));
|
|
||||||
when(productSkuApi.getSkuList(eq(asSet(10L, 20L)))).thenReturn(asList(productSku01, productSku02));
|
|
||||||
// mock 方法(限时折扣 DiscountActivity 信息)
|
|
||||||
RewardActivityDO rewardActivity01 = randomPojo(RewardActivityDO.class, o -> o.setId(1000L).setName("活动 1000 号")
|
|
||||||
.setProductSpuIds(asList(10L, 20L)).setConditionType(PromotionConditionTypeEnum.PRICE.getType())
|
|
||||||
.setRules(singletonList(new RewardActivityDO.Rule().setLimit(351).setDiscountPrice(70))));
|
|
||||||
Map<RewardActivityDO, Set<Long>> matchRewardActivities = new LinkedHashMap<>();
|
|
||||||
matchRewardActivities.put(rewardActivity01, asSet(1L, 2L));
|
|
||||||
when(rewardActivityService.getMatchRewardActivities(eq(asSet(1L, 2L)))).thenReturn(matchRewardActivities);
|
|
||||||
|
|
||||||
// 调用
|
|
||||||
PriceCalculateRespDTO priceCalculate = priceService.calculatePrice(calculateReqDTO);
|
|
||||||
// 断言 Order 部分
|
|
||||||
PriceCalculateRespDTO.Order order = priceCalculate.getOrder();
|
|
||||||
assertEquals(order.getTotalPrice(), 350);
|
|
||||||
assertEquals(order.getDiscountPrice(), 0);
|
|
||||||
assertEquals(order.getPointPrice(), 0);
|
|
||||||
assertEquals(order.getDeliveryPrice(), 0);
|
|
||||||
assertEquals(order.getPayPrice(), 350);
|
|
||||||
assertNull(order.getCouponId());
|
|
||||||
// 断言 OrderItem 部分
|
|
||||||
assertEquals(order.getItems().size(), 2);
|
|
||||||
PriceCalculateRespDTO.OrderItem orderItem01 = order.getItems().get(0);
|
|
||||||
assertEquals(orderItem01.getSkuId(), 10L);
|
|
||||||
assertEquals(orderItem01.getCount(), 2);
|
|
||||||
assertEquals(orderItem01.getOriginalPrice(), 200);
|
|
||||||
assertEquals(orderItem01.getOriginalUnitPrice(), 100);
|
|
||||||
assertEquals(orderItem01.getDiscountPrice(), 0);
|
|
||||||
assertEquals(orderItem01.getPayPrice(), 200);
|
|
||||||
assertEquals(orderItem01.getOrderPartPrice(), 0);
|
|
||||||
assertEquals(orderItem01.getOrderDividePrice(), 200);
|
|
||||||
PriceCalculateRespDTO.OrderItem orderItem02 = order.getItems().get(1);
|
|
||||||
assertEquals(orderItem02.getSkuId(), 20L);
|
|
||||||
assertEquals(orderItem02.getCount(), 3);
|
|
||||||
assertEquals(orderItem02.getOriginalPrice(), 150);
|
|
||||||
assertEquals(orderItem02.getOriginalUnitPrice(), 50);
|
|
||||||
assertEquals(orderItem02.getDiscountPrice(), 0);
|
|
||||||
assertEquals(orderItem02.getPayPrice(), 150);
|
|
||||||
assertEquals(orderItem02.getOrderPartPrice(), 0);
|
|
||||||
assertEquals(orderItem02.getOrderDividePrice(), 150);
|
|
||||||
// 断言 Promotion 部分
|
|
||||||
assertEquals(priceCalculate.getPromotions().size(), 1);
|
|
||||||
PriceCalculateRespDTO.Promotion promotion01 = priceCalculate.getPromotions().get(0);
|
|
||||||
assertEquals(promotion01.getId(), 1000L);
|
|
||||||
assertEquals(promotion01.getName(), "活动 1000 号");
|
|
||||||
assertEquals(promotion01.getType(), PromotionTypeEnum.REWARD_ACTIVITY.getType());
|
|
||||||
assertEquals(promotion01.getTotalPrice(), 350);
|
|
||||||
assertEquals(promotion01.getDiscountPrice(), 0);
|
|
||||||
assertFalse(promotion01.getMatch());
|
|
||||||
assertEquals(promotion01.getDescription(), "TODO"); // TODO 芋艿:后面再想想
|
|
||||||
assertEquals(promotion01.getItems().size(), 2);
|
|
||||||
PriceCalculateRespDTO.PromotionItem promotionItem011 = promotion01.getItems().get(0);
|
|
||||||
assertEquals(promotionItem011.getSkuId(), 10L);
|
|
||||||
assertEquals(promotionItem011.getOriginalPrice(), 200);
|
|
||||||
assertEquals(promotionItem011.getDiscountPrice(), 0);
|
|
||||||
PriceCalculateRespDTO.PromotionItem promotionItem012 = promotion01.getItems().get(1);
|
|
||||||
assertEquals(promotionItem012.getSkuId(), 20L);
|
|
||||||
assertEquals(promotionItem012.getOriginalPrice(), 150);
|
|
||||||
assertEquals(promotionItem012.getDiscountPrice(), 0);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
|
@ -27,7 +27,7 @@ import static org.mockito.Mockito.when;
|
|||||||
*
|
*
|
||||||
* @author 芋道源码
|
* @author 芋道源码
|
||||||
*/
|
*/
|
||||||
class TradeCouponPriceCalculatorTest extends BaseMockitoUnitTest {
|
public class TradeCouponPriceCalculatorTest extends BaseMockitoUnitTest {
|
||||||
|
|
||||||
@InjectMocks
|
@InjectMocks
|
||||||
private TradeCouponPriceCalculator tradeCouponPriceCalculator;
|
private TradeCouponPriceCalculator tradeCouponPriceCalculator;
|
||||||
|
@ -0,0 +1,232 @@
|
|||||||
|
package cn.iocoder.yudao.module.trade.service.price.calculator;
|
||||||
|
|
||||||
|
import cn.iocoder.yudao.framework.test.core.ut.BaseMockitoUnitTest;
|
||||||
|
import cn.iocoder.yudao.module.promotion.api.reward.RewardActivityApi;
|
||||||
|
import cn.iocoder.yudao.module.promotion.api.reward.dto.RewardActivityMatchRespDTO;
|
||||||
|
import cn.iocoder.yudao.module.promotion.enums.common.PromotionConditionTypeEnum;
|
||||||
|
import cn.iocoder.yudao.module.promotion.enums.common.PromotionTypeEnum;
|
||||||
|
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.common.util.collection.SetUtils.asSet;
|
||||||
|
import static cn.iocoder.yudao.framework.test.core.util.RandomUtils.randomPojo;
|
||||||
|
import static java.util.Arrays.asList;
|
||||||
|
import static java.util.Collections.singletonList;
|
||||||
|
import static org.junit.jupiter.api.Assertions.*;
|
||||||
|
import static org.mockito.ArgumentMatchers.eq;
|
||||||
|
import static org.mockito.Mockito.when;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* {@link TradeRewardActivityPriceCalculator} 的单元测试类
|
||||||
|
*
|
||||||
|
* @author 芋道源码
|
||||||
|
*/
|
||||||
|
public class TradeRewardActivityPriceCalculatorTest extends BaseMockitoUnitTest {
|
||||||
|
|
||||||
|
@InjectMocks
|
||||||
|
private TradeRewardActivityPriceCalculator tradeRewardActivityPriceCalculator;
|
||||||
|
|
||||||
|
@Mock
|
||||||
|
private RewardActivityApi rewardActivityApi;
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testCalculate_match() {
|
||||||
|
// 准备参数
|
||||||
|
TradePriceCalculateReqBO param = new TradePriceCalculateReqBO()
|
||||||
|
.setItems(asList(
|
||||||
|
new TradePriceCalculateReqBO.Item().setSkuId(10L).setCount(2).setSelected(true), // 匹配活动 1
|
||||||
|
new TradePriceCalculateReqBO.Item().setSkuId(20L).setCount(3).setSelected(true), // 匹配活动 1
|
||||||
|
new TradePriceCalculateReqBO.Item().setSkuId(30L).setCount(4).setSelected(true) // 匹配活动 2
|
||||||
|
));
|
||||||
|
TradePriceCalculateRespBO result = new TradePriceCalculateRespBO()
|
||||||
|
.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(4).setSelected(true)
|
||||||
|
.setPrice(30).setSpuId(3L)
|
||||||
|
));
|
||||||
|
// 保证价格被初始化上
|
||||||
|
TradePriceCalculatorHelper.recountPayPrice(result.getItems());
|
||||||
|
TradePriceCalculatorHelper.recountAllPrice(result);
|
||||||
|
|
||||||
|
// mock 方法(限时折扣 DiscountActivity 信息)
|
||||||
|
when(rewardActivityApi.getMatchRewardActivityList(eq(asSet(1L, 2L, 3L)))).thenReturn(asList(
|
||||||
|
randomPojo(RewardActivityMatchRespDTO.class, o -> o.setId(1000L).setName("活动 1000 号")
|
||||||
|
.setSpuIds(asList(1L, 2L)).setConditionType(PromotionConditionTypeEnum.PRICE.getType())
|
||||||
|
.setRules(singletonList(new RewardActivityMatchRespDTO.Rule().setLimit(200).setDiscountPrice(70)))),
|
||||||
|
randomPojo(RewardActivityMatchRespDTO.class, o -> o.setId(2000L).setName("活动 2000 号")
|
||||||
|
.setSpuIds(singletonList(3L)).setConditionType(PromotionConditionTypeEnum.COUNT.getType())
|
||||||
|
.setRules(asList(new RewardActivityMatchRespDTO.Rule().setLimit(1).setDiscountPrice(10),
|
||||||
|
new RewardActivityMatchRespDTO.Rule().setLimit(2).setDiscountPrice(60), // 最大可满足,因为是 4 个
|
||||||
|
new RewardActivityMatchRespDTO.Rule().setLimit(10).setDiscountPrice(100))))
|
||||||
|
));
|
||||||
|
|
||||||
|
// 调用
|
||||||
|
tradeRewardActivityPriceCalculator.calculate(param, result);
|
||||||
|
// 断言 Order 部分
|
||||||
|
TradePriceCalculateRespBO.Price price = result.getPrice();
|
||||||
|
assertEquals(price.getTotalPrice(), 470);
|
||||||
|
assertEquals(price.getDiscountPrice(), 130);
|
||||||
|
assertEquals(price.getPointPrice(), 0);
|
||||||
|
assertEquals(price.getDeliveryPrice(), 0);
|
||||||
|
assertEquals(price.getCouponPrice(), 0);
|
||||||
|
assertEquals(price.getPayPrice(), 340);
|
||||||
|
assertNull(result.getCouponId());
|
||||||
|
// 断言:SKU 1
|
||||||
|
assertEquals(result.getItems().size(), 3);
|
||||||
|
TradePriceCalculateRespBO.OrderItem orderItem01 = result.getItems().get(0);
|
||||||
|
assertEquals(orderItem01.getSkuId(), 10L);
|
||||||
|
assertEquals(orderItem01.getCount(), 2);
|
||||||
|
assertEquals(orderItem01.getPrice(), 100);
|
||||||
|
assertEquals(orderItem01.getDiscountPrice(), 40);
|
||||||
|
assertEquals(orderItem01.getDeliveryPrice(), 0);
|
||||||
|
assertEquals(orderItem01.getCouponPrice(), 0);
|
||||||
|
assertEquals(orderItem01.getPointPrice(), 0);
|
||||||
|
assertEquals(orderItem01.getPayPrice(), 160);
|
||||||
|
// 断言:SKU 2
|
||||||
|
TradePriceCalculateRespBO.OrderItem orderItem02 = result.getItems().get(1);
|
||||||
|
assertEquals(orderItem02.getSkuId(), 20L);
|
||||||
|
assertEquals(orderItem02.getCount(), 3);
|
||||||
|
assertEquals(orderItem02.getPrice(), 50);
|
||||||
|
assertEquals(orderItem02.getDiscountPrice(), 30);
|
||||||
|
assertEquals(orderItem02.getDeliveryPrice(), 0);
|
||||||
|
assertEquals(orderItem02.getCouponPrice(), 0);
|
||||||
|
assertEquals(orderItem02.getPointPrice(), 0);
|
||||||
|
assertEquals(orderItem02.getPayPrice(), 120);
|
||||||
|
// 断言:SKU 3
|
||||||
|
TradePriceCalculateRespBO.OrderItem orderItem03 = result.getItems().get(2);
|
||||||
|
assertEquals(orderItem03.getSkuId(), 30L);
|
||||||
|
assertEquals(orderItem03.getCount(), 4);
|
||||||
|
assertEquals(orderItem03.getPrice(), 30);
|
||||||
|
assertEquals(orderItem03.getDiscountPrice(), 60);
|
||||||
|
assertEquals(orderItem03.getDeliveryPrice(), 0);
|
||||||
|
assertEquals(orderItem03.getCouponPrice(), 0);
|
||||||
|
assertEquals(orderItem03.getPointPrice(), 0);
|
||||||
|
assertEquals(orderItem03.getPayPrice(), 60);
|
||||||
|
// 断言:Promotion 部分(第一个)
|
||||||
|
assertEquals(result.getPromotions().size(), 2);
|
||||||
|
TradePriceCalculateRespBO.Promotion promotion01 = result.getPromotions().get(0);
|
||||||
|
assertEquals(promotion01.getId(), 1000L);
|
||||||
|
assertEquals(promotion01.getName(), "活动 1000 号");
|
||||||
|
assertEquals(promotion01.getType(), PromotionTypeEnum.REWARD_ACTIVITY.getType());
|
||||||
|
assertEquals(promotion01.getTotalPrice(), 350);
|
||||||
|
assertEquals(promotion01.getDiscountPrice(), 70);
|
||||||
|
assertTrue(promotion01.getMatch());
|
||||||
|
assertEquals(promotion01.getDescription(), "满减送:省 0.70 元");
|
||||||
|
assertEquals(promotion01.getItems().size(), 2);
|
||||||
|
TradePriceCalculateRespBO.PromotionItem promotionItem011 = promotion01.getItems().get(0);
|
||||||
|
assertEquals(promotionItem011.getSkuId(), 10L);
|
||||||
|
assertEquals(promotionItem011.getTotalPrice(), 200);
|
||||||
|
assertEquals(promotionItem011.getDiscountPrice(), 40);
|
||||||
|
TradePriceCalculateRespBO.PromotionItem promotionItem012 = promotion01.getItems().get(1);
|
||||||
|
assertEquals(promotionItem012.getSkuId(), 20L);
|
||||||
|
assertEquals(promotionItem012.getTotalPrice(), 150);
|
||||||
|
assertEquals(promotionItem012.getDiscountPrice(), 30);
|
||||||
|
// 断言:Promotion 部分(第二个)
|
||||||
|
TradePriceCalculateRespBO.Promotion promotion02 = result.getPromotions().get(1);
|
||||||
|
assertEquals(promotion02.getId(), 2000L);
|
||||||
|
assertEquals(promotion02.getName(), "活动 2000 号");
|
||||||
|
assertEquals(promotion02.getType(), PromotionTypeEnum.REWARD_ACTIVITY.getType());
|
||||||
|
assertEquals(promotion02.getTotalPrice(), 120);
|
||||||
|
assertEquals(promotion02.getDiscountPrice(), 60);
|
||||||
|
assertTrue(promotion02.getMatch());
|
||||||
|
assertEquals(promotion02.getDescription(), "满减送:省 0.60 元");
|
||||||
|
TradePriceCalculateRespBO.PromotionItem promotionItem02 = promotion02.getItems().get(0);
|
||||||
|
assertEquals(promotion02.getItems().size(), 1);
|
||||||
|
assertEquals(promotionItem02.getSkuId(), 30L);
|
||||||
|
assertEquals(promotionItem02.getTotalPrice(), 120);
|
||||||
|
assertEquals(promotionItem02.getDiscountPrice(), 60);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testCalculate_notMatch() {
|
||||||
|
// 准备参数
|
||||||
|
TradePriceCalculateReqBO param = new TradePriceCalculateReqBO()
|
||||||
|
.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(4).setSelected(true)
|
||||||
|
));
|
||||||
|
TradePriceCalculateRespBO result = new TradePriceCalculateRespBO()
|
||||||
|
.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)
|
||||||
|
));
|
||||||
|
// 保证价格被初始化上
|
||||||
|
TradePriceCalculatorHelper.recountPayPrice(result.getItems());
|
||||||
|
TradePriceCalculatorHelper.recountAllPrice(result);
|
||||||
|
|
||||||
|
// mock 方法(限时折扣 DiscountActivity 信息)
|
||||||
|
when(rewardActivityApi.getMatchRewardActivityList(eq(asSet(1L, 2L)))).thenReturn(singletonList(
|
||||||
|
randomPojo(RewardActivityMatchRespDTO.class, o -> o.setId(1000L).setName("活动 1000 号")
|
||||||
|
.setSpuIds(asList(1L, 2L)).setConditionType(PromotionConditionTypeEnum.PRICE.getType())
|
||||||
|
.setRules(singletonList(new RewardActivityMatchRespDTO.Rule().setLimit(351).setDiscountPrice(70))))
|
||||||
|
));
|
||||||
|
|
||||||
|
// 调用
|
||||||
|
tradeRewardActivityPriceCalculator.calculate(param, result);
|
||||||
|
// 断言 Order 部分
|
||||||
|
TradePriceCalculateRespBO.Price price = result.getPrice();
|
||||||
|
assertEquals(price.getTotalPrice(), 350);
|
||||||
|
assertEquals(price.getDiscountPrice(), 0);
|
||||||
|
assertEquals(price.getPointPrice(), 0);
|
||||||
|
assertEquals(price.getDeliveryPrice(), 0);
|
||||||
|
assertEquals(price.getCouponPrice(), 0);
|
||||||
|
assertEquals(price.getPayPrice(), 350);
|
||||||
|
assertNull(result.getCouponId());
|
||||||
|
// 断言:SKU 1
|
||||||
|
assertEquals(result.getItems().size(), 2);
|
||||||
|
TradePriceCalculateRespBO.OrderItem orderItem01 = result.getItems().get(0);
|
||||||
|
assertEquals(orderItem01.getSkuId(), 10L);
|
||||||
|
assertEquals(orderItem01.getCount(), 2);
|
||||||
|
assertEquals(orderItem01.getPrice(), 100);
|
||||||
|
assertEquals(orderItem01.getDiscountPrice(), 0);
|
||||||
|
assertEquals(orderItem01.getDeliveryPrice(), 0);
|
||||||
|
assertEquals(orderItem01.getCouponPrice(), 0);
|
||||||
|
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.getDiscountPrice(), 0);
|
||||||
|
assertEquals(orderItem02.getDeliveryPrice(), 0);
|
||||||
|
assertEquals(orderItem02.getCouponPrice(), 0);
|
||||||
|
assertEquals(orderItem02.getPointPrice(), 0);
|
||||||
|
assertEquals(orderItem02.getPayPrice(), 150);
|
||||||
|
// 断言 Promotion 部分
|
||||||
|
assertEquals(result.getPromotions().size(), 1);
|
||||||
|
TradePriceCalculateRespBO.Promotion promotion01 = result.getPromotions().get(0);
|
||||||
|
assertEquals(promotion01.getId(), 1000L);
|
||||||
|
assertEquals(promotion01.getName(), "活动 1000 号");
|
||||||
|
assertEquals(promotion01.getType(), PromotionTypeEnum.REWARD_ACTIVITY.getType());
|
||||||
|
assertEquals(promotion01.getTotalPrice(), 350);
|
||||||
|
assertEquals(promotion01.getDiscountPrice(), 0);
|
||||||
|
assertFalse(promotion01.getMatch());
|
||||||
|
assertEquals(promotion01.getDescription(), "TODO"); // TODO 芋艿:后面再想想
|
||||||
|
assertEquals(promotion01.getItems().size(), 2);
|
||||||
|
TradePriceCalculateRespBO.PromotionItem promotionItem011 = promotion01.getItems().get(0);
|
||||||
|
assertEquals(promotionItem011.getSkuId(), 10L);
|
||||||
|
assertEquals(promotionItem011.getTotalPrice(), 200);
|
||||||
|
assertEquals(promotionItem011.getDiscountPrice(), 0);
|
||||||
|
TradePriceCalculateRespBO.PromotionItem promotionItem012 = promotion01.getItems().get(1);
|
||||||
|
assertEquals(promotionItem012.getSkuId(), 20L);
|
||||||
|
assertEquals(promotionItem012.getTotalPrice(), 150);
|
||||||
|
assertEquals(promotionItem012.getDiscountPrice(), 0);
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
Loading…
Reference in New Issue
Block a user