云
5 天以前 a82d847bbb39e2417d4c9773ebc4fc51fdb0b8fc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
package cn.iocoder.yudao.module.crm.service.bidbond;
 
import cn.iocoder.yudao.framework.common.exception.ServiceException;
import cn.iocoder.yudao.module.crm.controller.admin.bidbond.vo.CrmBidBondSaveReqVO;
import cn.iocoder.yudao.module.crm.dal.dataobject.bidbond.CrmBidBondDO;
import cn.iocoder.yudao.module.crm.dal.dataobject.customer.CrmCustomerDO;
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 cn.iocoder.yudao.module.crm.service.customer.CrmCustomerService;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.springframework.test.util.ReflectionTestUtils;
 
import java.lang.reflect.Proxy;
import java.math.BigDecimal;
import java.util.concurrent.atomic.AtomicReference;
 
import static cn.iocoder.yudao.module.crm.enums.ErrorCodeConstants.BID_BOND_QUOTATION_ALREADY_BOUND;
import static cn.iocoder.yudao.module.crm.enums.ErrorCodeConstants.BID_BOND_QUOTATION_CUSTOMER_MISMATCH;
import static cn.iocoder.yudao.module.crm.enums.ErrorCodeConstants.BID_BOND_QUOTATION_STATUS_INVALID;
import static cn.iocoder.yudao.module.crm.enums.ErrorCodeConstants.BID_BOND_WON_QUOTATION_REQUIRED;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNotNull;
import static org.junit.jupiter.api.Assertions.assertThrows;
 
class CrmBidBondQuotationBindingTest {
 
    private final CrmBidBondServiceImpl service = new CrmBidBondServiceImpl();
    private final AtomicReference<CrmBidBondDO> inserted = new AtomicReference<>();
    private final AtomicReference<Object[]> bound = new AtomicReference<>();
 
    @BeforeEach
    void setUp() {
        CrmCustomerService customerService = stub(CrmCustomerService.class, (proxy, method, args) -> {
            if ("getCustomer".equals(method.getName())) {
                return new CrmCustomerDO().setId((Long) args[0]).setName("客户1");
            }
            throw new AssertionError(method.getName());
        });
        CrmBidBondMapper bondMapper = stub(CrmBidBondMapper.class, (proxy, method, args) -> switch (method.getName()) {
            case "selectByNo" -> null;
            case "selectById" -> new CrmBidBondDO().setId((Long) args[0]).setNo("BZ-EXIST");
            case "insert" -> {
                CrmBidBondDO bond = (CrmBidBondDO) args[0];
                bond.setId(100L);
                inserted.set(bond);
                yield 1;
            }
            default -> throw new AssertionError(method.getName());
        });
        ReflectionTestUtils.setField(service, "customerService", customerService);
        ReflectionTestUtils.setField(service, "bidBondMapper", bondMapper);
    }
 
    private void bindQuotation(CrmSaleQuotationDO quotation) {
        CrmSaleQuotationMapper quoteMapper = stub(CrmSaleQuotationMapper.class,
                (proxy, method, args) -> switch (method.getName()) {
                    case "selectById" -> quotation;
                    case "bindBidBond" -> {
                        bound.set(args);
                        yield null;
                    }
                    default -> throw new AssertionError(method.getName());
                });
        ReflectionTestUtils.setField(service, "saleQuotationMapper", quoteMapper);
    }
 
    @Test
    void customerMismatchRejected() {
        bindQuotation(new CrmSaleQuotationDO().setId(5L).setNo("BJ5").setCustomerId(2L).setStatus(2));
        CrmBidBondSaveReqVO req = baseReq();
        req.setQuotationId(5L);
 
        ServiceException ex = assertThrows(ServiceException.class, () -> service.createBidBond(req));
        assertEquals(BID_BOND_QUOTATION_CUSTOMER_MISMATCH.getCode(), ex.getCode());
    }
 
    @Test
    void voidQuotationCannotBeBound() {
        bindQuotation(new CrmSaleQuotationDO().setId(5L).setNo("BJ5").setCustomerId(1L).setStatus(5));
        CrmBidBondSaveReqVO req = baseReq();
        req.setQuotationId(5L);
 
        ServiceException ex = assertThrows(ServiceException.class, () -> service.createBidBond(req));
        assertEquals(BID_BOND_QUOTATION_STATUS_INVALID.getCode(), ex.getCode());
    }
 
    @Test
    void quotationAlreadyBoundRejected() {
        bindQuotation(new CrmSaleQuotationDO().setId(5L).setNo("BJ5").setCustomerId(1L).setStatus(2)
                .setBidBondId(8L));
        CrmBidBondSaveReqVO req = baseReq();
        req.setQuotationId(5L);
 
        ServiceException ex = assertThrows(ServiceException.class, () -> service.createBidBond(req));
        assertEquals(BID_BOND_QUOTATION_ALREADY_BOUND.getCode(), ex.getCode());
    }
 
    @Test
    void wonRequiresQuotation() {
        bindQuotation(new CrmSaleQuotationDO().setId(5L).setNo("BJ5").setCustomerId(1L).setStatus(2));
        CrmBidBondSaveReqVO req = baseReq();
        req.setBidResult(1);
 
        ServiceException ex = assertThrows(ServiceException.class, () -> service.createBidBond(req));
        assertEquals(BID_BOND_WON_QUOTATION_REQUIRED.getCode(), ex.getCode());
    }
 
    @Test
    void createBindsQuotationBack() {
        bindQuotation(new CrmSaleQuotationDO().setId(5L).setNo("BJ5").setCustomerId(1L).setStatus(2)
                .setRevisionNo(2));
        CrmBidBondSaveReqVO req = baseReq();
        req.setQuotationId(5L);
 
        service.createBidBond(req);
 
        assertNotNull(inserted.get());
        assertEquals("BJ5", inserted.get().getQuotationNo());
        assertEquals(2, inserted.get().getQuotationRound());
        assertNotNull(bound.get());
        assertEquals(5L, bound.get()[0]);
        assertEquals(100L, bound.get()[1]);
        assertEquals(inserted.get().getNo(), bound.get()[2]);
    }
 
    private CrmBidBondSaveReqVO baseReq() {
        CrmBidBondSaveReqVO req = new CrmBidBondSaveReqVO();
        req.setCustomerId(1L);
        req.setProjectName("某某项目");
        req.setBondAmount(new BigDecimal("1000"));
        req.setBidResult(0);
        return req;
    }
 
    private static <T> T stub(Class<T> type, java.lang.reflect.InvocationHandler handler) {
        return type.cast(Proxy.newProxyInstance(type.getClassLoader(), new Class<?>[]{type}, handler));
    }
}