云
16 小时以前 d9a5ba1de2fce6bd8c50dace505e4bf7864043ba
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
package cn.iocoder.yudao.module.srm.service.supplier;
 
import cn.iocoder.yudao.module.srm.controller.admin.supplier.vo.SrmSupplierSaveReqVO;
import cn.iocoder.yudao.module.srm.dal.dataobject.SrmSupplierDO;
import cn.iocoder.yudao.module.srm.dal.mysql.SrmSupplierMapper;
import cn.iocoder.yudao.module.system.api.agent.imports.AgentImportCommitReqVO;
import cn.iocoder.yudao.module.system.api.agent.imports.AgentImportPreflightReqVO;
import cn.iocoder.yudao.module.system.api.agent.imports.AgentImportPreflightRespVO;
import cn.iocoder.yudao.module.system.api.agent.imports.AgentImportRowReceipt;
import cn.iocoder.yudao.module.system.framework.agent.AgentPrincipal;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.springframework.test.util.ReflectionTestUtils;
 
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
 
import static org.junit.jupiter.api.Assertions.*;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.Mockito.*;
 
class SrmSupplierImportAdapterTest {
 
    private static final AgentPrincipal PRINCIPAL = new AgentPrincipal(1L, 1L, "live");
 
    private SrmSupplierImportAdapter adapter;
    private SrmSupplierService supplierService;
    private SrmSupplierMapper supplierMapper;
 
    @BeforeEach
    void setUp() {
        adapter = new SrmSupplierImportAdapter();
        supplierService = mock(SrmSupplierService.class);
        supplierMapper = mock(SrmSupplierMapper.class);
        ReflectionTestUtils.setField(adapter, "supplierService", supplierService);
        ReflectionTestUtils.setField(adapter, "supplierMapper", supplierMapper);
    }
 
    @Test
    void preflightMarksCreateSkipAndBlockedRows() {
        SrmSupplierDO existing = new SrmSupplierDO();
        existing.setId(7L);
        when(supplierMapper.selectByCode("SUP-OLD")).thenReturn(existing);
        when(supplierMapper.selectByCode("SUP-NEW")).thenReturn(null);
 
        AgentImportPreflightRespVO plan = adapter.preflight(PRINCIPAL, request(
                row("r1", "SUP-NEW", "新供应商"),
                row("r2", "SUP-OLD", "旧供应商"),
                row("r3", "", "缺编码")));
 
        assertEquals("CREATE", plan.getRows().get(0).getAction());
        assertEquals("SKIP", plan.getRows().get(1).getAction());
        assertEquals(7L, plan.getRows().get(1).getTargetId());
        assertEquals("BLOCKED", plan.getRows().get(2).getAction());
        assertEquals(1, plan.getSummary().getCreate());
        assertEquals(1, plan.getSummary().getSkip());
        assertEquals(1, plan.getSummary().getBlocked());
        assertFalse(plan.getCanCommit());
        assertNotNull(plan.getPreflightHash());
        assertTrue(plan.getRows().get(1).getWarnings().stream()
                .anyMatch(item -> "SUPPLIER_EXISTS".equals(item.getCode())));
    }
 
    @Test
    void duplicateCodeInFileBlocksAllOccurrences() {
        when(supplierMapper.selectByCode(any())).thenReturn(null);
 
        AgentImportPreflightRespVO plan = adapter.preflight(PRINCIPAL, request(
                row("r1", "SUP-DUP", "供应商A"),
                row("r2", "SUP-DUP", "供应商B")));
 
        assertEquals("BLOCKED", plan.getRows().get(0).getAction());
        assertEquals("BLOCKED", plan.getRows().get(1).getAction());
        assertEquals(2, plan.getSummary().getBlocked());
        assertFalse(plan.getCanCommit());
    }
 
    @Test
    void preflightHashIsStableForSameInput() {
        when(supplierMapper.selectByCode(any())).thenReturn(null);
        AgentImportPreflightReqVO request = request(row("r1", "SUP-NEW", "新供应商"));
 
        assertEquals(adapter.preflight(PRINCIPAL, request).getPreflightHash(),
                adapter.preflight(PRINCIPAL, request).getPreflightHash());
    }
 
    @Test
    void commitCreatesSupplierAndReturnsReceipt() {
        when(supplierService.createSupplier(any())).thenReturn(99L);
        AgentImportPreflightReqVO.Row row = row("r1", "SUP-NEW", "新供应商");
 
        AgentImportRowReceipt receipt = adapter.commitRow(PRINCIPAL, new AgentImportCommitReqVO(),
                AgentImportPreflightRespVO.Row.builder().candidateId("r1").action("CREATE").build(), row);
 
        assertEquals("CREATE", receipt.action());
        assertEquals(99L, receipt.targetId());
        assertEquals("SUP-NEW", receipt.code());
        verify(supplierService).createSupplier(any(SrmSupplierSaveReqVO.class));
    }
 
    private static AgentImportPreflightReqVO request(AgentImportPreflightReqVO.Row... rows) {
        AgentImportPreflightReqVO request = new AgentImportPreflightReqVO();
        request.setAttachmentId(1L);
        request.setSourceSha256("a".repeat(64));
        request.setProposalRevision("0");
        request.setRows(List.of(rows));
        return request;
    }
 
    private static AgentImportPreflightReqVO.Row row(String candidateId, String code, String name) {
        Map<String, String> values = new LinkedHashMap<>();
        values.put("code", code);
        values.put("name", name);
        AgentImportPreflightReqVO.Row row = new AgentImportPreflightReqVO.Row();
        row.setCandidateId(candidateId);
        row.setRowNo(1);
        row.setValues(values);
        return row;
    }
}