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; /* * 全场满就送 * 全场满FruitPromotion.totalGift * 送1单位(FruitSubPromotion.unit)FruitSubPromotion.promotionProductId对应的商品 * */ public class IosOrderServiceForTotalGiftTest extends BaseIosOrderServiceTestCase { private static final BigDecimal PRODUCT_1_PRICE = BigDecimal.valueOf(10.4); private static final BigDecimal PRODUCT_2_PRICE = BigDecimal.valueOf(8.2); private static final BigDecimal PRODUCT_3_PRICE = BigDecimal.valueOf(3.1); @Before public void setUp() { super.setUp(); mockService4TotalGift(); } // 全场送-未达到FruitPromotion.totalGift,不送 @Test public void testCaculatePromotionInfo_under_totalGift_no_gift() { int giftCategorySize = 0;// 送 int productNum = 1;// 10 int promotionTipsSize = 1; SettlementResponse response = getResponse4WithSameProduct(productNum); assertTotalGiftResponse(productNum, 0, response, promotionTipsSize, giftCategorySize, 0); } // 全场送-达到FruitPromotion.totalGift,1倍,送1份 @Test public void testCaculatePromotionInfo_reach_totalGift_1_times_1_gift() { int giftCategorySize = 1;// 送 int productNum = 3;// 30 int promotionTipsSize = 1; SettlementResponse response = getResponse4WithSameProduct(productNum); assertTotalGiftResponse(productNum, 0, response, promotionTipsSize, giftCategorySize, 1); } // 全场送-达到FruitPromotion.totalGift,2倍,送2份 @Test public void testCaculatePromotionInfo_reach_totalGift_2_times_2_gift() { int giftCategorySize = 1;// 送 int productNum = 5;// 50 int promotionTipsSize = 1; SettlementResponse response = getResponse4WithSameProduct(productNum); assertTotalGiftResponse(productNum, 0, response, promotionTipsSize, giftCategorySize, 2); } // 全场送-达到FruitPromotion.totalGift,2.4倍,送2份 @Test public void testCaculatePromotionInfo_reach_totalGift_2p5_times_2_gift() { int giftCategorySize = 1;// 送 int productNum = 6;// 60 int promotionTipsSize = 1; SettlementResponse response = getResponse4WithSameProduct(productNum); assertTotalGiftResponse(productNum, 0, response, promotionTipsSize, giftCategorySize, 2); } // 全场送-未达到FruitPromotion.totalGift,不送 @Test public void testCaculatePromotionInfo_under_totalGift_no_gift_with_2_diff_product() { int giftCategorySize = 0;// 送 int productNum1 = 1;// 10 int productNum2 = 1;// 8 int promotionTipsSize = 1; SettlementResponse response = getResponseWith2DifferentProducts( productNum1, productNum2); assertTotalGiftResponse(productNum1, productNum2, response, promotionTipsSize, giftCategorySize, 0); } // 全场送-达到FruitPromotion.totalGift一倍,送一个 @Test public void testCaculatePromotionInfo_1_gift_with_3_diff_product() { int giftCategorySize = 1;// 送 int productNum1 = 2;// 20 int productNum2 = 1;// 8 int promotionTipsSize = 1; SettlementResponse response = getResponseWith2DifferentProducts( productNum1, productNum2); assertTotalGiftResponse(productNum1, productNum2, response, promotionTipsSize, giftCategorySize, 1); } // 全场送-达到FruitPromotion.totalGift两倍,送2个 @Test public void testCaculatePromotionInfo_2_gift_with_6_diff_product() { int giftCategorySize = 1;// 送 int productNum1 = 5;// 50 int productNum2 = 1;// 8 int promotionTipsSize = 1; SettlementResponse response = getResponseWith2DifferentProducts( productNum1, productNum2); assertTotalGiftResponse(productNum1, productNum2, response, promotionTipsSize, giftCategorySize, 2); } // 全场送-达到FruitPromotion.totalGift一倍,送1个 @Test public void testCaculatePromotionInfo_1_gift_with_5_diff_product() { int giftCategorySize = 1;// 送 int productNum1 = 3;// 30 int productNum2 = 2;// 16 int promotionTipsSize = 1; SettlementResponse response = getResponseWith2DifferentProducts( productNum1, productNum2); assertTotalGiftResponse(productNum1, productNum2, response, promotionTipsSize, giftCategorySize, 1); } private void mockService4TotalGift() { 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_GIFT, productId_1, promotion_productId_1, "2 spec productId_1,1 unit promotion_productId_1 as gift", Integer.MAX_VALUE, BigDecimal.TEN, BigDecimal.ZERO, BigDecimal.ZERO, BigDecimal.valueOf(25)); Mockito.when( fruitPromotionService.getAvailablePromotionBy(null, FruitPromotion.PromotionType.TOTAL_GIFT)).thenReturn( Arrays.asList(fruitPromotion1)); } private void assertTotalGiftResponse(int productNum1, int productNum2, 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 totalPrice = product1total.add(product2total); BigDecimal actualTotalPrice = totalPrice; String msg = "[商品1]个数:" + productNum1 + " [商品2]个数:" + productNum2 + "\n" + "原价:" + totalPrice + "=10*" + productNum1 + "+ 8*" + 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(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()) { // 第一种礼物个数 ProductFreeInfo gift = freeProductInfos.get(0); assertEquals(firstGiftNum, gift.getQuantity()); System.out.println("gift:" + gift); } // 提示促销信息 List<String> promotionTips = response.getPromotionTips(); assertNotNull(promotionTips); assertEquals(promotionTipsSize, promotionTips.size()); for (String tip : promotionTips) { System.out.println("tip:" + tip); } } }