package org.xmx0632.deliciousfruit.service; import static org.junit.Assert.assertEquals; import static org.junit.Assert.fail; import java.math.BigDecimal; import org.junit.Before; import org.junit.Test; import org.mockito.InjectMocks; import org.mockito.Mock; import org.mockito.Mockito; import org.mockito.MockitoAnnotations; import org.xmx0632.deliciousfruit.api.v1.bo.OrderRequest; import org.xmx0632.deliciousfruit.erp.ErpApiService; import org.xmx0632.deliciousfruit.erp.bo.ErpCard; import org.xmx0632.deliciousfruit.erp.bo.ErpCash; import org.xmx0632.deliciousfruit.erp.bo.ErpCustomer; /** * 异常流程 * * 测试 查询 ERP的抵扣券/礼品卡/积分/账户余额等出现异常时的行为 * * @author mars * */ public class DeductionPayServiceGetDeductionPayExceptionTest { private static final String CUSTOMER_ID = "customerId"; @InjectMocks protected DeductionPayService deductionPayService; @Mock protected ErpApiService erpApiService; @Before public void setUp() throws Exception { MockitoAnnotations.initMocks(this); ErpCustomer erpCustomer = new ErpCustomer(CUSTOMER_ID, "1000", "22.0"); Mockito.when(erpApiService.getCustomerBy(CUSTOMER_ID)).thenReturn( erpCustomer); } // 查询现金券失败,找不到; @Test public void testQueryCashCouponError_not_find() { Mockito.when( erpApiService .getCashBy(DeductionPayServiceHelper.CASH_COUPON_ID_1)) .thenReturn(null); try { OrderRequest orderRequest = DeductionPayServiceHelper .getOrderRequest4CashCoupon(BigDecimal.valueOf(20.3), DeductionPayServiceHelper.CASH_COUPON_ID_1); deductionPayService.getDeductionPay(orderRequest, CUSTOMER_ID); fail("must not reach here"); } catch (Exception e) { assertEquals("can not find cashCoupon!", e.getMessage()); } } // 查询现金券失败,状态异常; @Test public void testQueryCashCouponError_abnormalStatus() { Mockito.when( erpApiService .getCashBy(DeductionPayServiceHelper.CASH_COUPON_ID_1)) .thenReturn(new ErpCash("20", 0)); try { OrderRequest orderRequest = DeductionPayServiceHelper .getOrderRequest4CashCoupon(BigDecimal.valueOf(20.3), DeductionPayServiceHelper.CASH_COUPON_ID_1); deductionPayService.getDeductionPay(orderRequest, CUSTOMER_ID); fail("must not reach here"); } catch (Exception e) { assertEquals("erpCard status abnormal!", e.getMessage()); } } // 现金券抵扣金额与请求金额不匹配 @Test public void testQueryCashCouponError_abnormalPayAmount() { Mockito.when( erpApiService .getCashBy(DeductionPayServiceHelper.CASH_COUPON_ID_1)) .thenReturn(new ErpCash("20", 1)); try { OrderRequest orderRequest = DeductionPayServiceHelper .getOrderRequest4CashCoupon(BigDecimal.valueOf(20.3), DeductionPayServiceHelper.CASH_COUPON_ID_1); deductionPayService.getDeductionPay(orderRequest, CUSTOMER_ID); fail("must not reach here"); } catch (Exception e) { assertEquals( "ErpCash payAmount abnormal! availableAmount:20.0 payAmount:20.3", e.getMessage()); } } // 查询礼品卡失败,找不到 @Test public void testQueryGiftCardError_not_find() { Mockito.when( erpApiService .getCardBy(DeductionPayServiceHelper.GIFT_CARD_ID_1)) .thenReturn(null); try { OrderRequest orderRequest = DeductionPayServiceHelper .getOrderRequest4GiftCard(BigDecimal.valueOf(20.3), DeductionPayServiceHelper.GIFT_CARD_ID_1); deductionPayService.getDeductionPay(orderRequest, CUSTOMER_ID); fail("must not reach here"); } catch (Exception e) { assertEquals("can not find giftCard!", e.getMessage()); } } // 查询礼品卡失败,礼品卡状态异常 @Test public void testQueryGiftCardError_status_abnormal() { Mockito.when( erpApiService .getCardBy(DeductionPayServiceHelper.GIFT_CARD_ID_1)) .thenReturn(new ErpCard("20.3", "0")); try { OrderRequest orderRequest = DeductionPayServiceHelper .getOrderRequest4GiftCard(BigDecimal.valueOf(20.3), DeductionPayServiceHelper.GIFT_CARD_ID_1); deductionPayService.getDeductionPay(orderRequest, CUSTOMER_ID); fail("must not reach here"); } catch (Exception e) { assertEquals( "ErpCard status abnormal! erpCard:ErpCard [strCardNo=null, strPassword=null, decCardValue=null, decValue=20.3, datExpireDate=null, intStatus=0]", e.getMessage()); } } // 查询礼品卡失败,礼品卡可用余额不足请求支付金额 @Test public void testQueryGiftCardError_payAmount_abnormal() { Mockito.when( erpApiService .getCardBy(DeductionPayServiceHelper.GIFT_CARD_ID_1)) .thenReturn(new ErpCard("20", "1")); try { OrderRequest orderRequest = DeductionPayServiceHelper .getOrderRequest4GiftCard(BigDecimal.valueOf(20.3), DeductionPayServiceHelper.GIFT_CARD_ID_1); deductionPayService.getDeductionPay(orderRequest, CUSTOMER_ID); fail("must not reach here"); } catch (Exception e) { assertEquals( "ErpCard available amount is not enough!payAmount:20.3 but available amount:20.0", e.getMessage()); } } // 查询积分失败,查询积分信息失败 @Test public void testQueryPointInfoError_not_find() { Mockito.when(erpApiService.getCustomerBy(CUSTOMER_ID)).thenReturn(null); try { OrderRequest orderRequest = DeductionPayServiceHelper .getOrderRequest4PayByPointInfo(BigDecimal.valueOf(20.3), 20300); deductionPayService.getDeductionPay(orderRequest, CUSTOMER_ID); fail("must not reach here"); } catch (Exception e) { assertEquals("can not find customer with id:customerId", e.getMessage()); } } // 抵扣的积分与金额不对应时提示报错 @Test public void testQueryPointInfoWhenPointCalcError() { Mockito.when(erpApiService.getCustomerBy(CUSTOMER_ID)).thenReturn( new ErpCustomer(CUSTOMER_ID, "2000", "0")); try { OrderRequest orderRequest = DeductionPayServiceHelper .getOrderRequest4PayByPointInfo(BigDecimal.valueOf(20.3), 2031); deductionPayService.getDeductionPay(orderRequest, CUSTOMER_ID); fail("must not reach here"); } catch (Exception e) { assertEquals( "calc PayAmountFromPonit error! payPoint:2031 payAmountFromPonit:20.3 must be 20.31", e.getMessage()); } } // 查询积分失败,积分余额不足 @Test public void testQueryPointInfoError_pointNotEnough() { Mockito.when(erpApiService.getCustomerBy(CUSTOMER_ID)).thenReturn( new ErpCustomer(CUSTOMER_ID, "2000", "0")); try { OrderRequest orderRequest = DeductionPayServiceHelper .getOrderRequest4PayByPointInfo(BigDecimal.valueOf(20.3), 2030); deductionPayService.getDeductionPay(orderRequest, CUSTOMER_ID); fail("must not reach here"); } catch (Exception e) { assertEquals( "point not enough! payPoint:2030 but available point:2000", e.getMessage()); } } // 查询帐户余额失败,找不到 @Test public void testQueryPayAmountFromAccountError_not_find() { Mockito.when(erpApiService.getCustomerBy(CUSTOMER_ID)).thenReturn(null); try { OrderRequest orderRequest = DeductionPayServiceHelper .getOrderRequest4PayAmountFromAccount(BigDecimal .valueOf(22.0)); deductionPayService.getDeductionPay(orderRequest, CUSTOMER_ID); fail("must not reach here"); } catch (Exception e) { assertEquals("can not find customer with id:customerId", e.getMessage()); } } // 查询帐户余额失败,余额不足 @Test public void testQueryPayAmountFromAccountError_notEnoughCoin() { Mockito.when(erpApiService.getCustomerBy(CUSTOMER_ID)).thenReturn( new ErpCustomer(CUSTOMER_ID, "2000", "30")); try { OrderRequest orderRequest = DeductionPayServiceHelper .getOrderRequest4PayAmountFromAccount(BigDecimal .valueOf(31.0)); deductionPayService.getDeductionPay(orderRequest, CUSTOMER_ID); fail("must not reach here"); } catch (Exception e) { assertEquals( "coin not enough! payAmountFromAccount:31.0 but available coin:30.0", e.getMessage()); } } }