package org.xmx0632.deliciousfruit.service; import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertNotNull; import static org.junit.Assert.assertTrue; import java.math.BigDecimal; import java.util.Arrays; import java.util.List; import org.junit.Before; import org.junit.Test; import org.mockito.Mockito; import org.xmx0632.deliciousfruit.api.v1.bo.Result; import org.xmx0632.deliciousfruit.api.v1.bo.SettlementResponse; import org.xmx0632.deliciousfruit.api.v1.bo.SettlementResponse.ProductFreeInfo; import org.xmx0632.deliciousfruit.api.v1.bo.SettlementResponse.PromotionDetails; import org.xmx0632.deliciousfruit.api.v1.bo.SettlementResponse.PromotionInfo; import org.xmx0632.deliciousfruit.entity.FruitPromotion; /* * 全场满立减是 * 全场满FruitPromotion.totalPriceoff * 立减(FruitPromotion.priceoff)金额,跟FruitSubPromotion没关系 * */ public class IosOrderServiceForTotalDeductTest extends BaseIosOrderServiceTestCase { private static final BigDecimal TOTAL_PRICEOFF_THREHOLD = BigDecimal .valueOf(25); private static final BigDecimal PRICEOFF = BigDecimal.valueOf(2.1); private static final BigDecimal PRODUCT_1_PRICE = BigDecimal.valueOf(10.3); private static final BigDecimal PRODUCT_2_PRICE = BigDecimal.valueOf(8.8); private static final BigDecimal PRODUCT_3_PRICE = BigDecimal.valueOf(3.0); @Before public void setUp() { super.setUp(); mockService4TotalDeduct(); } // 全场立减-未达到FruitPromotion.totalPriceoff,不减 @Test public void testCaculatePromotionInfo_under_totalPriceoff_no_priceoff() { int giftCategorySize = 0;// 送 int productNum1 = 1;// 10 int productNum2 = 0;// 8*0 int promotionTipsSize = 1; BigDecimal priceOff = PRICEOFF.multiply(BigDecimal.valueOf(0));// 折扣金额 SettlementResponse response = getResponseWith2DifferentProducts( productNum1, productNum2); assertTotalDeductResponse(productNum1, productNum2, response, promotionTipsSize, giftCategorySize, 0, priceOff); } // 全场立减-达到1倍FruitPromotion.totalPriceoff,减FruitPromotion.priceoff @Test public void testCaculatePromotionInfo_reach_totalPriceoff_priceoff_once() { int giftCategorySize = 0;// 送 int productNum1 = 1;// 10*1 int productNum2 = 2;// 8*2 int promotionTipsSize = 1; BigDecimal priceOff = PRICEOFF.multiply(BigDecimal.valueOf(1));// 折扣金额 SettlementResponse response = getResponseWith2DifferentProducts( productNum1, productNum2); assertTotalDeductResponse(productNum1, productNum2, response, promotionTipsSize, giftCategorySize, 0, priceOff); } // 全场立减-达到2倍FruitPromotion.totalPriceoff,减FruitPromotion.priceoff两次 @Test public void testCaculatePromotionInfo_reach_totalPriceoff_priceoff_2_times() { int giftCategorySize = 0;// 送 int productNum1 = 4; int productNum2 = 2; int promotionTipsSize = 1; SettlementResponse response = getResponseWith2DifferentProducts( productNum1, productNum2); BigDecimal priceOff = PRICEOFF.multiply(BigDecimal.valueOf(2));// 折扣金额 assertTotalDeductResponse(productNum1, productNum2, response, promotionTipsSize, giftCategorySize, 0, priceOff); } // 全场立减-达到3倍FruitPromotion.totalPriceoff,减FruitPromotion.priceoff两次 @Test public void testCaculatePromotionInfo_reach_totalPriceoff_2p5_times_priceoff_2_times() { int giftCategorySize = 0;// 送 int productNum1 = 3; int productNum2 = 6; int promotionTipsSize = 1; BigDecimal priceOff = PRICEOFF.multiply(BigDecimal.valueOf(3));// 折扣金额 SettlementResponse response = getResponseWith2DifferentProducts( productNum1, productNum2); assertTotalDeductResponse(productNum1, productNum2, response, promotionTipsSize, giftCategorySize, 0, priceOff); } private void mockService4TotalDeduct() { mockFruitProductService("苹果", "箱", productId_1, BigDecimal.ZERO, PRODUCT_1_PRICE); mockFruitProductService("桔子", "箱", productId_2, BigDecimal.ZERO, PRODUCT_2_PRICE); mockFruitProductService("香蕉", "箱", productId_3, BigDecimal.ZERO, PRODUCT_3_PRICE); // mock fruitPromotionService FruitPromotion fruitPromotion1 = getFruitPromotion( FruitPromotion.PromotionType.TOTAL_DEDUCT, productId_1, promotion_productId_1, "2 spec productId_1,1 unit promotion_productId_1 as gift", Integer.MAX_VALUE, BigDecimal.TEN, TOTAL_PRICEOFF_THREHOLD, PRICEOFF, BigDecimal.ZERO); Mockito.when( fruitPromotionService.getAvailablePromotionBy(null, FruitPromotion.PromotionType.TOTAL_DEDUCT)).thenReturn( Arrays.asList(fruitPromotion1)); } private void assertTotalDeductResponse(int productNum1, int productNum2, SettlementResponse response, int promotionTipsSize, int giftCategorySize, int firstGiftNum, BigDecimal priceOff) { BigDecimal product1total = PRODUCT_1_PRICE.multiply(BigDecimal .valueOf(productNum1)); BigDecimal product2total = PRODUCT_2_PRICE.multiply(BigDecimal .valueOf(productNum2)); BigDecimal totalPrice = product1total.add(product2total); BigDecimal actualTotalPrice = totalPrice.subtract(priceOff); String msg = "[商品1]个数:" + productNum1 + " [商品2]个数:" + productNum2 + "\n" + "原价:" + totalPrice + "=" + PRODUCT_1_PRICE + "*" + productNum1 + "+ " + PRODUCT_2_PRICE + "*" + productNum2 + " 优惠价:" + actualTotalPrice + "\n" + "提示优惠信息数:" + promotionTipsSize; System.out.println(msg); assertNotNull(response); assertEquals(Result.SUCCESS_RESULT.toString(), response.getResult() .toString()); assertEquals(TRANSACTION_ID, response.getTransactionID()); // 价格 assertEquals(totalPrice, response.getTotalPrice()); assertEquals(actualTotalPrice, response.getActualAmount()); PromotionInfo promotionInfo = response.getPromotionInfo(); assertNotNull(promotionInfo); // 满总价格,减一定金额 PromotionDetails promotionForTotal = promotionInfo .getPromotionForTotal(); assertNotNull(promotionForTotal); assertEquals(totalPrice.subtract(actualTotalPrice), promotionForTotal.getPriceOff()); // 没有单独减价的产品 List<ProductFreeInfo> freeTotalInfos = promotionForTotal .getProductsFree(); assertNotNull(freeTotalInfos); assertEquals(0, freeTotalInfos.size()); // 单件商品不打折 PromotionDetails promotionForProduct = promotionInfo .getPromotionForProduct(); assertNotNull(promotionForProduct); assertEquals(BigDecimal.ZERO, promotionForProduct.getPriceOff()); List<ProductFreeInfo> freeProductInfos = promotionForProduct .getProductsFree(); assertNotNull(freeProductInfos); assertEquals(giftCategorySize, freeProductInfos.size()); assertTrue(freeProductInfos.isEmpty()); // 提示促销信息 List<String> promotionTips = response.getPromotionTips(); assertNotNull(promotionTips); assertEquals(promotionTipsSize, promotionTips.size()); for (String tip : promotionTips) { System.out.println("tip:" + tip); } } }