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;
import org.xmx0632.deliciousfruit.global.ConfigConstant;
/*
* 全场满就送
* 全场满FruitPromotion.totalGift
* 送1单位(FruitSubPromotion.unit)FruitSubPromotion.promotionProductId对应的商品
*
*/
public class IosOrderServiceForAllTest extends BaseIosOrderServiceTestCase {
// 满就减
private static final BigDecimal PRICEOFF = BigDecimal.valueOf(2.1);
private static final BigDecimal TOTAL_PRICEOFF_THREHOLD = BigDecimal
.valueOf(25);// 满25就减2.1
private static final BigDecimal _2ND_DEDUCT_FOR_PRODUCT1 = BigDecimal
.valueOf(2.3);
private static final BigDecimal _2ND_DEDUCT_FOR_PRODUCT2 = BigDecimal
.valueOf(0.2);
private static final BigDecimal _2ND_DEDUCT_FOR_PRODUCT3 = BigDecimal
.valueOf(0.0);
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();
}
// 第二件单品立减金额,未满,不送
// 单品满就送商品-未达到标准,不送;
// 全场满立减(FruitPromotion.priceoff)金额,未达到不减
// 全场满送商品-未达到FruitPromotion.totalGift,不送;
@Test
public void testCaculatePromotionInfo_under_totalGift_no_gift_with_2_diff_product() {
mockService();
int giftCategorySize = 0;// 送
int productNum1 = 1;
int productNum2 = 1;
int productNum3 = 1;
int promotionTipsSize = 4;
SettlementResponse response = getResponseWith3DifferentProducts(
productNum1, productNum2, productNum3);
assertResponse(productNum1, productNum2, productNum3, response,
promotionTipsSize, giftCategorySize);
}
// 第二件单品立减金额,满2件,送一件
// 单品满就送商品-满2件,送一件;
// 全场满立减(FruitPromotion.priceoff)金额,满一倍,减一次
// 全场满送商品-达到FruitPromotion.totalGift一倍,送一件商品;
@Test
public void testCaculatePromotionInfo_1() {
mockService();
int productNum1 = 2;
int productNum2 = 2;
int productNum3 = 2;
int giftCategorySize = 5;// 累加一共送5件
int promotionTipsSize = 4;
SettlementResponse response = getResponseWith3DifferentProducts(
productNum1, productNum2, productNum3);
assertResponse(productNum1, productNum2, productNum3, response,
promotionTipsSize, giftCategorySize);
}
// 第二件单品立减金额,满5件,送2件
// 单品满就送商品-满5件,送2件;
// 全场满立减(FruitPromotion.priceoff)金额,满2倍,减2次
// 全场满送商品-达到FruitPromotion.totalGift2倍,送2件商品;
@Test
public void testCaculatePromotionInfo_2() {
mockService();
int giftCategorySize = 5;// 累加一共送5件
int productNum1 = 5;// 送5/2=2件
int productNum2 = 5;// 送5/2=2件
int productNum3 = 5;
int promotionTipsSize = 4;
SettlementResponse response = getResponseWith3DifferentProducts(
productNum1, productNum2, productNum3);
assertResponse(productNum1, productNum2, productNum3, response,
promotionTipsSize, giftCategorySize);
}
// 全场满立减优先,再全场满就送
@Test
public void testCaculatePromotionInfo_totalDeductFirst() {
Mockito.when(configService.getByName(ConfigConstant.TOTAL_DEDUCT_FIRST))
.thenReturn("true");
mockService();
int giftCategorySize = 5;// 累加一共送5件
int productNum1 = 5;// 送5/2=2件
int productNum2 = 5;// 送5/2=2件
int productNum3 = 5;
int promotionTipsSize = 4;
SettlementResponse response = getResponseWith3DifferentProducts(
productNum1, productNum2, productNum3);
assertResponse(productNum1, productNum2, productNum3, response,
promotionTipsSize, giftCategorySize);
}
// 全场满就送优先,再全场满立减
@Test
public void testCaculatePromotionInfo_totalGiftFirst() {
Mockito.when(configService.getByName(ConfigConstant.TOTAL_DEDUCT_FIRST))
.thenReturn("false");
mockService();
int giftCategorySize = 5;// 累加一共送5件
int productNum1 = 5;// 送5/2=2件
int productNum2 = 5;// 送5/2=2件
int productNum3 = 5;
int promotionTipsSize = 4;
SettlementResponse response = getResponseWith3DifferentProducts(
productNum1, productNum2, productNum3);
assertResponse(productNum1, productNum2, productNum3, response,
promotionTipsSize, giftCategorySize);
assertEquals(BigDecimal.valueOf(10.0), response.getFreight());
assertEquals(BigDecimal.valueOf(10.0), response.getFreightPay());
}
// 全场满就送优先,再全场满立减,满100块免运费
@Test
public void testCaculatePromotionInfo_totalGiftFirst_noFeight() {
Mockito.when(configService.getByName(ConfigConstant.TOTAL_DEDUCT_FIRST))
.thenReturn("false");
mockService();
int giftCategorySize = 5;// 累加一共送5件
int productNum1 = 5;// 送5/2=2件
int productNum2 = 5;// 送5/2=2件
int productNum3 = 8;
int promotionTipsSize = 4;
SettlementResponse response = getResponseWith3DifferentProducts(
productNum1, productNum2, productNum3);
assertResponse(productNum1, productNum2, productNum3, response,
promotionTipsSize, giftCategorySize);
assertEquals(BigDecimal.valueOf(10.0), response.getFreight());
assertEquals(BigDecimal.valueOf(0.0), response.getFreightPay());
}
private void mockService() {
// mock 单品第二件立减
mockFruitProductService("苹果", "箱", productId_1,
_2ND_DEDUCT_FOR_PRODUCT1, PRODUCT_1_PRICE);
mockFruitProductService("桔子", "箱", productId_2,
_2ND_DEDUCT_FOR_PRODUCT2, PRODUCT_2_PRICE);
mockFruitProductService("香蕉", "箱", productId_3,
_2ND_DEDUCT_FOR_PRODUCT3, PRODUCT_3_PRICE);
// mock fruitPromotionService
// 单品满就送
FruitPromotion fruitPromotionProductDeduct = 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.valueOf(0.0),
BigDecimal.valueOf(0.0), BigDecimal.valueOf(0.0));
Mockito.when(
fruitPromotionService.getAvailablePromotionBy(null,
FruitPromotion.PromotionType.PRODUCT_GIFT, productId_1))
.thenReturn(Arrays.asList(fruitPromotionProductDeduct));
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.valueOf(0.0),
BigDecimal.valueOf(0.0), BigDecimal.valueOf(0.0));
Mockito.when(
fruitPromotionService.getAvailablePromotionBy(null,
FruitPromotion.PromotionType.PRODUCT_GIFT, productId_2))
.thenReturn(Arrays.asList(fruitPromotion2));
// 全场满就送
FruitPromotion fruitPromotionTotalGift = 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.valueOf(0.0),
BigDecimal.valueOf(0.0), BigDecimal.valueOf(25.0));
Mockito.when(
fruitPromotionService.getAvailablePromotionBy(null,
FruitPromotion.PromotionType.TOTAL_GIFT)).thenReturn(
Arrays.asList(fruitPromotionTotalGift));
// 全场满就减
FruitPromotion fruitPromotionTotalDeduct = 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.valueOf(0.0));
Mockito.when(
fruitPromotionService.getAvailablePromotionBy(null,
FruitPromotion.PromotionType.TOTAL_DEDUCT)).thenReturn(
Arrays.asList(fruitPromotionTotalDeduct));
}
private void assertResponse(int productNum1, int productNum2,
int productNum3, SettlementResponse response,
int promotionTipsSize, int giftCategorySize) {
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 priceOffProduct1 = _2ND_DEDUCT_FOR_PRODUCT1
.multiply(BigDecimal.valueOf(productNum1 / 2));
BigDecimal priceOffProduct2 = _2ND_DEDUCT_FOR_PRODUCT2
.multiply(BigDecimal.valueOf(productNum2 / 2));
BigDecimal totalProductPriceoff = priceOffProduct1
.add(priceOffProduct2);
BigDecimal tmpProductTotal = totalPrice.subtract(totalProductPriceoff);
int totalDeductTimes = tmpProductTotal.divide(TOTAL_PRICEOFF_THREHOLD,
BigDecimal.ROUND_HALF_UP).intValue();
System.out.println("totalProductPriceoff:" + totalProductPriceoff
+ " tmpProductTotal:" + tmpProductTotal + " totalDeductTimes:"
+ totalDeductTimes);
BigDecimal totalPriceoff = totalProductPriceoff.add(PRICEOFF
.multiply(BigDecimal.valueOf(totalDeductTimes)));
BigDecimal actualTotalPrice = totalPrice.subtract(totalPriceoff);
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(totalPriceoff, promotionForTotal.getPriceOff());
List<ProductFreeInfo> freeTotalInfos = promotionForTotal
.getProductsFree();
assertNotNull(freeTotalInfos);
assertEquals(0, freeTotalInfos.size());
// 单品送/第二件立减
PromotionDetails promotionForProduct = promotionInfo
.getPromotionForProduct();
assertNotNull(promotionForProduct);
assertEquals(totalProductPriceoff.intValue(), promotionForProduct
.getPriceOff().intValue());
// 提示促销信息
List<String> promotionTips = response.getPromotionTips();
assertNotNull(promotionTips);
for (String tip : promotionTips) {
System.out.println("tip:" + tip);
}
List<ProductFreeInfo> freeProductInfos = promotionForProduct
.getProductsFree();
assertNotNull(freeProductInfos);
if (!freeProductInfos.isEmpty()) {
for (ProductFreeInfo productFreeInfo : freeProductInfos) {
System.out.println("productFreeInfo:" + productFreeInfo);
}
}
assertEquals(giftCategorySize, freeProductInfos.size());
assertEquals(promotionTipsSize, promotionTips.size());
}
}