From d7a1c8f7ca482a48f637bffc15d4f416265f60f8 Mon Sep 17 00:00:00 2001 From: YunaiV Date: Tue, 8 Nov 2022 19:32:49 +0800 Subject: [PATCH] =?UTF-8?q?promotion=EF=BC=9A=E5=A2=9E=E5=8A=A0=E8=8E=B7?= =?UTF-8?q?=E5=BE=97=E4=BC=98=E6=83=A0=E5=8A=B5=E5=8C=B9=E9=85=8D=E7=BB=93?= =?UTF-8?q?=E6=9E=9C=E7=9A=84=E5=88=97=E8=A1=A8=E7=9A=84=E5=8D=95=E5=85=83?= =?UTF-8?q?=E6=B5=8B=E8=AF=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../service/price/PriceServiceTest.java | 55 +++++++++++++++++++ 1 file changed, 55 insertions(+) diff --git a/yudao-module-mall/yudao-module-promotion-biz/src/test/java/cn/iocoder/yudao/module/promotion/service/price/PriceServiceTest.java b/yudao-module-mall/yudao-module-promotion-biz/src/test/java/cn/iocoder/yudao/module/promotion/service/price/PriceServiceTest.java index 5263b3404..3eb01a2e0 100644 --- a/yudao-module-mall/yudao-module-promotion-biz/src/test/java/cn/iocoder/yudao/module/promotion/service/price/PriceServiceTest.java +++ b/yudao-module-mall/yudao-module-promotion-biz/src/test/java/cn/iocoder/yudao/module/promotion/service/price/PriceServiceTest.java @@ -1,14 +1,17 @@ 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.test.core.ut.BaseMockitoUnitTest; import cn.iocoder.yudao.module.product.api.sku.ProductSkuApi; 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.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.reward.RewardActivityDO; import cn.iocoder.yudao.module.promotion.enums.common.*; +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.discount.DiscountActivityService; import cn.iocoder.yudao.module.promotion.service.discount.bo.DiscountProductDetailBO; @@ -18,16 +21,20 @@ import org.mockito.InjectMocks; import org.mockito.Mock; import java.util.LinkedHashMap; +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.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.module.promotion.enums.ErrorCodeConstants.COUPON_VALID_TIME_NOT_NOW; 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.doThrow; import static org.mockito.Mockito.when; /** @@ -448,4 +455,52 @@ public class PriceServiceTest extends BaseMockitoUnitTest { assertEquals(promotionItem012.getDiscountPrice(), 30); } + @Test + public void testGetMeetCouponList() { + // 准备参数 + PriceCalculateReqDTO calculateReqDTO = new PriceCalculateReqDTO().setUserId(1024L) + .setItems(singletonList(new PriceCalculateReqDTO.Item().setSkuId(10L).setCount(2))); + // mock 方法(商品 SKU 信息) + ProductSkuRespDTO productSku = randomPojo(ProductSkuRespDTO.class, o -> o.setId(10L).setPrice(100)); + when(productSkuApi.getSkuList(eq(asSet(10L)))).thenReturn(singletonList(productSku)); + // mock 方法(情况一:优惠劵未到使用时间) + CouponDO coupon01 = randomPojo(CouponDO.class); + doThrow(new ServiceException(COUPON_VALID_TIME_NOT_NOW)).when(couponService).validCoupon(coupon01); + // mock 方法(情况二:所结算商品没有符合条件的商品) + CouponDO coupon02 = randomPojo(CouponDO.class); + // mock 方法(情况三:使用金额不足) + CouponDO coupon03 = randomPojo(CouponDO.class, o -> o.setProductScope(PromotionProductScopeEnum.ALL.getScope()) + .setUsePrice(300)); + // mock 方法(情况五:满足条件) + CouponDO coupon04 = randomPojo(CouponDO.class, o -> o.setProductScope(PromotionProductScopeEnum.ALL.getScope()) + .setUsePrice(190)); + // mock 方法(获得用户的待使用优惠劵) + when(couponService.getCouponList(eq(1024L), eq(CouponStatusEnum.UNUSED.getStatus()))) + .thenReturn(asList(coupon01, coupon02, coupon03, coupon04)); + // 调用 + List list = priceService.getMeetCouponList(calculateReqDTO); + // 断言 + assertEquals(list.size(), 4); + // 断言情况一:优惠劵未到使用时间 + CouponMeetRespDTO couponMeetRespDTO01 = list.get(0); + assertPojoEquals(couponMeetRespDTO01, coupon01); + assertFalse(couponMeetRespDTO01.getMeet()); + assertEquals(couponMeetRespDTO01.getMeetTip(), "优惠劵未到使用时间"); + // 断言情况二:所结算商品没有符合条件的商品 + CouponMeetRespDTO couponMeetRespDTO02 = list.get(1); + assertPojoEquals(couponMeetRespDTO02, coupon02); + assertFalse(couponMeetRespDTO02.getMeet()); + assertEquals(couponMeetRespDTO02.getMeetTip(), "所结算商品没有符合条件的商品"); + // 断言情况三:差 %s 元可用优惠劵 + CouponMeetRespDTO couponMeetRespDTO03 = list.get(2); + assertPojoEquals(couponMeetRespDTO03, coupon03); + assertFalse(couponMeetRespDTO03.getMeet()); + assertEquals(couponMeetRespDTO03.getMeetTip(), "差 1.00 元可用优惠劵"); + // 断言情况四:满足条件 + CouponMeetRespDTO couponMeetRespDTO04 = list.get(3); + assertPojoEquals(couponMeetRespDTO04, coupon04); + assertTrue(couponMeetRespDTO04.getMeet()); + assertNull(couponMeetRespDTO04.getMeetTip()); + } + }