package cn.iocoder.yudao.module.mes.service.qc.imports; import cn.iocoder.yudao.module.crm.api.customer.CrmCustomerApi; import cn.iocoder.yudao.module.mes.controller.admin.qc.oqc.vo.MesQcOqcSaveReqVO; import jakarta.validation.Validation; import org.junit.jupiter.api.Test; import org.springframework.test.util.ReflectionTestUtils; import java.math.BigDecimal; import java.time.LocalDateTime; import java.util.List; import static org.junit.jupiter.api.Assertions.*; import static org.mockito.Mockito.*; class MesQcOptionalClientTest { @Test void emptyCustomerDoesNotCallCrmOrRequireCustomerPermission() { CrmCustomerApi crm = mock(CrmCustomerApi.class); MesQcImportHelper helper = new MesQcImportHelper(); ReflectionTestUtils.setField(helper, "crmCustomerApi", crm); assertNull(helper.resolveClientId(null)); assertNull(helper.resolveClientId(" ")); helper.validateClientPermission(null, ""); verifyNoInteractions(crm); } @Test void aSuppliedUnknownCustomerIsStillRejected() { CrmCustomerApi crm = mock(CrmCustomerApi.class); when(crm.getCustomerSimpleList()).thenReturn(List.of()); MesQcImportHelper helper = new MesQcImportHelper(); ReflectionTestUtils.setField(helper, "crmCustomerApi", crm); assertThrows(IllegalArgumentException.class, () -> helper.resolveClientId("不存在的客户")); } @Test void customerPermissionsStillApplyWhenACustomerIsSupplied() { CrmCustomerApi crm = mock(CrmCustomerApi.class); when(crm.getPermittedCustomerIds()).thenReturn(List.of(10L)); MesQcImportHelper helper = new MesQcImportHelper(); ReflectionTestUtils.setField(helper, "crmCustomerApi", crm); helper.validateClientPermission(10L, "可见客户"); assertThrows(IllegalArgumentException.class, () -> helper.validateClientPermission(20L, "不可见客户")); } @Test void saveRequestAllowsEmptyCustomerButStillRequiresMaterial() { MesQcOqcSaveReqVO req = new MesQcOqcSaveReqVO(); req.setCode("OQC-OPTIONAL-CUSTOMER");req.setName("出货检验");req.setItemId(10L); req.setOutQuantity(BigDecimal.TEN);req.setCheckQuantity(BigDecimal.ONE); req.setQualifiedQuantity(BigDecimal.ONE);req.setUnqualifiedQuantity(BigDecimal.ZERO); req.setOutDate(LocalDateTime.now());req.setInspectDate(LocalDateTime.now());req.setInspectorUserId(1L); try (var factory = Validation.buildDefaultValidatorFactory()) { assertTrue(factory.getValidator().validate(req).isEmpty()); req.setItemId(null); assertFalse(factory.getValidator().validate(req).isEmpty()); } } }