package cn.iocoder.yudao.module.crm.service.quotation; import cn.iocoder.yudao.framework.common.exception.ServiceException; import cn.iocoder.yudao.module.crm.dal.dataobject.bidbond.CrmBidBondDO; import cn.iocoder.yudao.module.crm.dal.dataobject.quotation.CrmSaleQuotationDO; import cn.iocoder.yudao.module.crm.dal.mysql.bidbond.CrmBidBondMapper; import cn.iocoder.yudao.module.crm.dal.mysql.quotation.CrmSaleQuotationMapper; import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; import org.springframework.test.util.ReflectionTestUtils; import java.lang.reflect.Proxy; import static cn.iocoder.yudao.module.crm.enums.ErrorCodeConstants.SALE_QUOTATION_CONVERT_FAIL_BID_NOT_WON; import static cn.iocoder.yudao.module.crm.enums.ErrorCodeConstants.SALE_QUOTATION_CONVERT_FAIL_BID_QUOTATION_MISMATCH; import static cn.iocoder.yudao.module.crm.enums.ErrorCodeConstants.SALE_QUOTATION_CONVERT_FAIL_BID_REQUIRED; import static cn.iocoder.yudao.module.crm.enums.ErrorCodeConstants.SALE_QUOTATION_CONVERT_FAIL_NOT_DRAFT; import static org.junit.jupiter.api.Assertions.assertEquals; import static org.junit.jupiter.api.Assertions.assertThrows; class CrmSaleQuotationConvertGateTest { private final CrmSaleQuotationServiceImpl service = new CrmSaleQuotationServiceImpl(); @BeforeEach void setUp() { ReflectionTestUtils.setField(service, "expiryHelper", new CrmSaleQuotationExpiryHelper()); } @Test void needBidQuotationWithoutBondCannotConvert() { prepare(new CrmSaleQuotationDO().setId(1L).setNo("BJ1").setStatus(2).setNeedBid(true), null); ServiceException ex = assertThrows(ServiceException.class, () -> service.convertToContract(1L, 291L)); assertEquals(SALE_QUOTATION_CONVERT_FAIL_BID_REQUIRED.getCode(), ex.getCode()); } @Test void nullNeedBidTreatedAsRequired() { prepare(new CrmSaleQuotationDO().setId(1L).setNo("BJ1").setStatus(2), null); ServiceException ex = assertThrows(ServiceException.class, () -> service.convertToContract(1L, 291L)); assertEquals(SALE_QUOTATION_CONVERT_FAIL_BID_REQUIRED.getCode(), ex.getCode()); } @Test void notNeedBidQuotationUsesOriginalStatusRule() { prepare(new CrmSaleQuotationDO().setId(1L).setNo("BJ1").setStatus(1).setNeedBid(false), null); ServiceException ex = assertThrows(ServiceException.class, () -> service.convertToContract(1L, 291L)); assertEquals(SALE_QUOTATION_CONVERT_FAIL_NOT_DRAFT.getCode(), ex.getCode()); } @Test void boundQuotationRequiresWonBid() { prepare(new CrmSaleQuotationDO().setId(1L).setNo("BJ1").setStatus(2).setBidBondId(9L), new CrmBidBondDO().setId(9L).setNo("BZ9").setBidResult(2).setQuotationId(1L)); ServiceException ex = assertThrows(ServiceException.class, () -> service.convertToContract(1L, 291L)); assertEquals(SALE_QUOTATION_CONVERT_FAIL_BID_NOT_WON.getCode(), ex.getCode()); } @Test void boundQuotationRequiresMatchingBid() { prepare(new CrmSaleQuotationDO().setId(1L).setNo("BJ1").setStatus(2).setBidBondId(9L), new CrmBidBondDO().setId(9L).setNo("BZ9").setBidResult(1).setQuotationId(8L)); ServiceException ex = assertThrows(ServiceException.class, () -> service.convertToContract(1L, 291L)); assertEquals(SALE_QUOTATION_CONVERT_FAIL_BID_QUOTATION_MISMATCH.getCode(), ex.getCode()); } @Test void wonQuotationMustBeApproved() { prepare(new CrmSaleQuotationDO().setId(1L).setNo("BJ1").setStatus(0).setBidBondId(9L), new CrmBidBondDO().setId(9L).setNo("BZ9").setBidResult(1).setQuotationId(1L)); ServiceException ex = assertThrows(ServiceException.class, () -> service.convertToContract(1L, 291L)); assertEquals(SALE_QUOTATION_CONVERT_FAIL_NOT_DRAFT.getCode(), ex.getCode()); } private void prepare(CrmSaleQuotationDO quote, CrmBidBondDO bond) { CrmSaleQuotationMapper quoteMapper = stub(CrmSaleQuotationMapper.class, (proxy, method, args) -> { if ("selectById".equals(method.getName())) { return quote; } throw new AssertionError(method.getName()); }); CrmBidBondMapper bondMapper = stub(CrmBidBondMapper.class, (proxy, method, args) -> { if ("selectById".equals(method.getName())) { return bond; } throw new AssertionError(method.getName()); }); ReflectionTestUtils.setField(service, "saleQuotationMapper", quoteMapper); ReflectionTestUtils.setField(service, "bidBondMapper", bondMapper); } private static T stub(Class type, java.lang.reflect.InvocationHandler handler) { return type.cast(Proxy.newProxyInstance(type.getClassLoader(), new Class[]{type}, handler)); } }