| ¶Ô±ÈÐÂÎļþ |
| | |
| | | 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, "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; |
| | | } |
| | | } |