云
2 天以前 657698fd2fb32794932474648dbaee9e284e38c8
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
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());
        }
    }
}