package org.xmx0632.deliciousfruit.service; import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertNotNull; 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; /* * 买FruitSubPromotion.productId对应的产品 * 满FruitSubPromotion.promotionThreshold 数量的规格(FruitProduct.spec) * 送1单位(FruitSubPromotion.unit)FruitSubPromotion.promotionProductId对应的产品 * */ public class IosOrderServiceForProductGiftTest extends BaseIosOrderServiceTestCase { private static final BigDecimal PRODUCT_3_PRICE = BigDecimal.valueOf(3.1); private static final BigDecimal PRODUCT_2_PRICE = BigDecimal.valueOf(8.2); private static final BigDecimal PRODUCT_1_PRICE = BigDecimal.valueOf(10.3); @Before public void setUp() { super.setUp(); mockService4ProductGift(); } // 单品满(数量)就送-未达到标准,不送 @Test public void testCaculatePromotionInfo_under_promotionThreshold_no_gift() { int giftCategorySize = 0;// 送 int productNum1 = 1; int promotionTipsSize = 1; SettlementResponse response = getResponse4WithSameProduct(productNum1); assertProductGiftResponse(productNum1, 0, 0, response, promotionTipsSize, giftCategorySize, 0); } // 单品满(数量)就送-达到标准1倍,送1份,不提示 @Test public void testCaculatePromotionInfo_reach_promotionThreshold_1_times() { int giftCategorySize = 1;// 送 int productNum1 = 2; int promotionTipsSize = 1; SettlementResponse response = getResponse4WithSameProduct(productNum1); assertProductGiftResponse(productNum1, 0, 0, response, promotionTipsSize, giftCategorySize, 1); } // 单品满(数量)就送-达到标准2倍,送2份 @Test public void testCaculatePromotionInfo_reach_promotionThreshold_2_times() { int giftCategorySize = 1;// 送 int productNum1 = 4; int promotionTipsSize = 1; SettlementResponse response = getResponse4WithSameProduct(productNum1); assertProductGiftResponse(productNum1, 0, 0, response, promotionTipsSize, giftCategorySize, 2); } // 单品满(数量)就送-达到标准2.5倍,送2份 @Test public void testCaculatePromotionInfo_reach_promotionThreshold_2p5_times() { int giftCategorySize = 1;// 送 int productNum1 = 5; int promotionTipsSize = 1; SettlementResponse response = getResponse4WithSameProduct(productNum1); assertProductGiftResponse(productNum1, 0, 0, response, promotionTipsSize, giftCategorySize, 2); } // 2种单品满(数量)就送-未达到标准,不送 @Test public void testCaculatePromotionInfo_under_promotionThreshold_no_gift_diff_prod() { int giftCategorySize = 0;// 送 int productNum1 = 1; int productNum2 = 1; int promotionTipsSize = 2; SettlementResponse response = getResponseWith2DifferentProducts( productNum1, productNum2); assertProductGiftResponse(productNum1, productNum2, 0, response, promotionTipsSize, giftCategorySize, 0); } // 2种单品满(数量)就送-达到标准1倍,送1份 @Test public void testCaculatePromotionInfo_reach_promotionThreshold_1_times_diff_prod() { int giftCategorySize = 2;// 送 int productNum1 = 2; int productNum2 = 2; int promotionTipsSize = 2; SettlementResponse response = getResponseWith2DifferentProducts( productNum1, productNum2); assertProductGiftResponse(productNum1, productNum2, 0, response, promotionTipsSize, giftCategorySize, 1); } // 2种单品满(数量)就送-达到标准2倍,送2份 @Test public void testCaculatePromotionInfo_reach_promotionThreshold_2_times_diff_prod() { int giftCategorySize = 2;// 送 int productNum1 = 4; int productNum2 = 4; int promotionTipsSize = 2; SettlementResponse response = getResponseWith2DifferentProducts( productNum1, productNum2); assertProductGiftResponse(productNum1, productNum2, 0, response, promotionTipsSize, giftCategorySize, 2); } // 2种单品满(数量)就送-达到标准2.5倍,送2份 @Test public void testCaculatePromotionInfo_reach_promotionThreshold_2p5_times_diff_prod() { int giftCategorySize = 2;// 送 int productNum1 = 5; int productNum2 = 5; int promotionTipsSize = 2; SettlementResponse response = getResponseWith2DifferentProducts( productNum1, productNum2); assertProductGiftResponse(productNum1, productNum2, 0, response, promotionTipsSize, giftCategorySize, 2); } // 3种单品满(数量)就送-1种无促销,2种有促销,未达到标准,不送 @Test public void testCaculatePromotionInfo_1_under_promotionThreshold_no_gift_diff_prod() { int giftCategorySize = 0;// 送 int productNum1 = 1; int productNum2 = 1; int productNum3 = 1; int promotionTipsSize = 2; SettlementResponse response = getResponseWith3DifferentProducts( productNum1, productNum2, productNum3); assertProductGiftResponse(productNum1, productNum2, productNum3, response, promotionTipsSize, giftCategorySize, 0); } // 3种单品满(数量)就送-1种无促销,2种有促销,2种达到标准,送2种 @Test public void testCaculatePromotionInfo_1() { int giftCategorySize = 2;// 送 int productNum1 = 2; int productNum2 = 2; int productNum3 = 1; int promotionTipsSize = 2; SettlementResponse response = getResponseWith3DifferentProducts( productNum1, productNum2, productNum3); assertProductGiftResponse(productNum1, productNum2, productNum3, response, promotionTipsSize, giftCategorySize, 1); } // 3种单品满(数量)就送-1种无促销,2种有促销,1种达到标准,送1种 @Test public void testCaculatePromotionInfo_2() { int giftCategorySize = 1;// 送 int productNum1 = 2; int productNum2 = 1; int productNum3 = 1; int promotionTipsSize = 2; SettlementResponse response = getResponseWith3DifferentProducts( productNum1, productNum2, productNum3); assertProductGiftResponse(productNum1, productNum2, productNum3, response, promotionTipsSize, giftCategorySize, 1); } // 3种单品满(数量)就送-1种无促销,2种有促销,2种达到标准3.5倍,送2种 @Test public void testCaculatePromotionInfo_3() { int giftCategorySize = 2;// 送 int productNum1 = 7; int productNum2 = 5; int productNum3 = 3; int promotionTipsSize = 2; SettlementResponse response = getResponseWith3DifferentProducts( productNum1, productNum2, productNum3); assertProductGiftResponse(productNum1, productNum2, productNum3, response, promotionTipsSize, giftCategorySize, 3); } private void assertProductGiftResponse(int productNum1, int productNum2, int productNum3, SettlementResponse response, int promotionTipsSize, int giftCategorySize, int firstGiftNum) { BigDecimal product1total = PRODUCT_1_PRICE.multiply(BigDecimal .valueOf(productNum1)); BigDecimal product2total = PRODUCT_2_PRICE.multiply(BigDecimal .valueOf(productNum2)); BigDecimal product3total = PRODUCT_3_PRICE.multiply(BigDecimal .valueOf(productNum3)); BigDecimal totalPrice = product1total.add(product2total).add( product3total); BigDecimal actualTotalPrice = totalPrice; String msg = "[商品1]个数:" + productNum1 + " [商品2]个数:" + productNum2 + " [商品3]个数:" + productNum3 + "\n" + "原价:" + totalPrice + "=" + PRODUCT_1_PRICE + "*" + productNum1 + "+ " + PRODUCT_2_PRICE + "*" + productNum2 + "+ " + PRODUCT_3_PRICE + "*" + productNum3 + " 优惠价:" + 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(BigDecimal.ZERO, 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()); if (!freeProductInfos.isEmpty()) { // TODO 判断每种赠送产品的个数 for (ProductFreeInfo productFreeInfo : freeProductInfos) { System.out.println("productFreeInfo:" + productFreeInfo); } } // 提示促销信息 List<String> promotionTips = response.getPromotionTips(); assertNotNull(promotionTips); assertEquals(promotionTipsSize, promotionTips.size()); for (String tip : promotionTips) { System.out.println("tip:" + tip); } } private void mockService4ProductGift() { 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.PRODUCT_GIFT, productId_1, promotion_productId_1, "2 spec productId_1,1 unit promotion_productId_1 as gift", 2, PRODUCT_1_PRICE, BigDecimal.ZERO, BigDecimal.ZERO, BigDecimal.ZERO); Mockito.when( fruitPromotionService.getAvailablePromotionBy(null, FruitPromotion.PromotionType.PRODUCT_GIFT, productId_1)) .thenReturn(Arrays.asList(fruitPromotion1)); FruitPromotion fruitPromotion2 = getFruitPromotion( FruitPromotion.PromotionType.PRODUCT_GIFT, productId_2, promotion_productId_2, "2 spec productId_2,1 unit promotion_productId_2 as gift", 2, PRODUCT_2_PRICE, BigDecimal.ZERO, BigDecimal.ZERO, BigDecimal.ZERO); Mockito.when( fruitPromotionService.getAvailablePromotionBy(null, FruitPromotion.PromotionType.PRODUCT_GIFT, productId_2)) .thenReturn(Arrays.asList(fruitPromotion2)); } }