云
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
package cn.iocoder.yudao.module.crm.service.quotation;
 
import cn.iocoder.yudao.framework.common.exception.ServiceException;
import cn.iocoder.yudao.module.crm.dal.dataobject.quotation.CrmSaleQuotationDO;
import cn.iocoder.yudao.module.crm.dal.dataobject.quotation.CrmSaleQuotationProductDO;
import cn.iocoder.yudao.module.crm.dal.dataobject.quotation.CrmSaleQuotationRevisionDO;
import cn.iocoder.yudao.module.crm.dal.mysql.quotation.CrmSaleQuotationMapper;
import cn.iocoder.yudao.module.crm.dal.mysql.quotation.CrmSaleQuotationProductMapper;
import cn.iocoder.yudao.module.crm.dal.mysql.quotation.CrmSaleQuotationRevisionMapper;
import cn.iocoder.yudao.module.mdm.api.item.MdmItemApi;
import org.junit.jupiter.api.Test;
import org.springframework.test.util.ReflectionTestUtils;
 
import java.lang.reflect.Proxy;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
 
import static cn.iocoder.yudao.module.crm.enums.ErrorCodeConstants.SALE_QUOTATION_REVISION_EXISTS;
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 CrmSaleQuotationRevisionTest {
 
    private final CrmSaleQuotationServiceImpl service = new CrmSaleQuotationServiceImpl();
    private final List<CrmSaleQuotationRevisionDO> inserted = new ArrayList<>();
 
    private void prepare(long existingRevisionCount) {
        CrmSaleQuotationRevisionMapper revisionMapper = stub(CrmSaleQuotationRevisionMapper.class,
                (proxy, method, args) -> switch (method.getName()) {
                    case "selectByQuotationIdAndRevisionNo" ->
                            (Integer) args[1] <= existingRevisionCount ? new CrmSaleQuotationRevisionDO() : null;
                    case "insert" -> {
                        inserted.add((CrmSaleQuotationRevisionDO) args[0]);
                        yield 1;
                    }
                    default -> throw new AssertionError(method.getName());
                });
        CrmSaleQuotationProductMapper productMapper = stub(CrmSaleQuotationProductMapper.class,
                (proxy, method, args) -> {
                    if ("selectListByQuotationId".equals(method.getName())) {
                        return List.of(new CrmSaleQuotationProductDO().setItemId(1103L)
                                .setCount(new BigDecimal("2"))
                                .setQuotationPrice(new BigDecimal("100")));
                    }
                    throw new AssertionError(method.getName());
                });
        CrmSaleQuotationMapper quoteMapper = stub(CrmSaleQuotationMapper.class,
                (proxy, method, args) -> 1);
        MdmItemApi itemApi = stub(MdmItemApi.class, (proxy, method, args) -> Map.of());
        ReflectionTestUtils.setField(service, "revisionMapper", revisionMapper);
        ReflectionTestUtils.setField(service, "saleQuotationProductMapper", productMapper);
        ReflectionTestUtils.setField(service, "saleQuotationMapper", quoteMapper);
        ReflectionTestUtils.setField(service, "mdmItemApi", itemApi);
    }
 
    @Test
    void eachSubmitIncrementsRevision() {
        prepare(0);
        CrmSaleQuotationDO quote = new CrmSaleQuotationDO().setId(1L).setNo("BJ1").setRevisionNo(0);
 
        ReflectionTestUtils.invokeMethod(service, "recordRevision", quote, 291L);
        ReflectionTestUtils.invokeMethod(service, "recordRevision", quote, 291L);
 
        assertEquals(2, inserted.size());
        assertEquals(1, inserted.get(0).getRevisionNo());
        assertEquals(2, inserted.get(1).getRevisionNo());
        assertEquals(2, quote.getRevisionNo());
        assertNotNull(inserted.get(0).getItemsSnapshot());
        assertEquals(1, inserted.get(0).getStatus());
    }
 
    @Test
    void duplicateRevisionRejected() {
        prepare(1);
        CrmSaleQuotationDO quote = new CrmSaleQuotationDO().setId(1L).setNo("BJ1").setRevisionNo(0);
 
        ServiceException ex = assertThrows(ServiceException.class,
                () -> ReflectionTestUtils.invokeMethod(service, "recordRevision", quote, 291L));
        assertEquals(SALE_QUOTATION_REVISION_EXISTS.getCode(), ex.getCode());
    }
 
    private static <T> T stub(Class<T> type, java.lang.reflect.InvocationHandler handler) {
        return type.cast(Proxy.newProxyInstance(type.getClassLoader(), new Class<?>[]{type}, handler));
    }
}