云
4 天以前 f02d0df2f1dfc7a8b775cf50d45375e57628a5ef
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
package cn.iocoder.yudao.module.mes.service.wm.productsales;
 
import cn.iocoder.yudao.framework.common.exception.ServiceException;
import cn.iocoder.yudao.module.mes.dal.dataobject.qc.oqc.MesQcOqcDO;
import cn.iocoder.yudao.module.mes.dal.dataobject.wm.materialstock.MesWmMaterialStockDO;
import cn.iocoder.yudao.module.mes.dal.dataobject.wm.productsales.MesWmProductSalesLineDO;
import cn.iocoder.yudao.module.mes.dal.mysql.qc.ncr.MesQcNcrMapper;
import cn.iocoder.yudao.module.mes.dal.mysql.qc.oqc.MesQcOqcMapper;
import cn.iocoder.yudao.module.mes.dal.mysql.wm.materialstock.MesWmMaterialStockMapper;
import com.baomidou.mybatisplus.core.conditions.Wrapper;
import org.junit.jupiter.api.Test;
import org.springframework.test.util.ReflectionTestUtils;
import java.math.BigDecimal;
import static org.junit.jupiter.api.Assertions.*;
import static org.mockito.ArgumentMatchers.*;
import static org.mockito.Mockito.*;
 
class MesWmOqcConcessionGuardTest {
    @Test void enforcesInspectedBatchAndWholeLineQuantity() {
        var guard = new MesWmOqcConcessionGuard();
        var ncrs = mock(MesQcNcrMapper.class);
        var oqcs = mock(MesQcOqcMapper.class);
        var stocks = mock(MesWmMaterialStockMapper.class);
        ReflectionTestUtils.setField(guard, "ncrMapper", ncrs);
        ReflectionTestUtils.setField(guard, "oqcMapper", oqcs);
        ReflectionTestUtils.setField(guard, "stockMapper", stocks);
        var line = new MesWmProductSalesLineDO().setId(1L).setItemId(2L).setOqcId(3L).setQuantity(BigDecimal.TEN);
        when(ncrs.selectCount(any(Wrapper.class))).thenReturn(1L);
        when(oqcs.selectById(3L)).thenReturn(new MesQcOqcDO().setSourceLineId(1L).setItemId(2L).setBatchCode("B1"));
        when(stocks.selectById(4L)).thenReturn(new MesWmMaterialStockDO().setItemId(2L).setBatchCode("B1"));
        when(stocks.selectById(5L)).thenReturn(new MesWmMaterialStockDO().setItemId(2L).setBatchCode("B2"));
        assertDoesNotThrow(() -> guard.validate(line, 4L, "B1", BigDecimal.TEN));
        assertThrows(ServiceException.class, () -> guard.validate(line, 5L, "B1", BigDecimal.TEN));
        assertThrows(ServiceException.class, () -> guard.validate(line, 4L, "B2", BigDecimal.TEN));
        assertThrows(ServiceException.class, () -> guard.validate(line, null, "B1", BigDecimal.TEN));
        assertThrows(ServiceException.class, () -> guard.validate(line, 4L, "B1", BigDecimal.ZERO));
        assertDoesNotThrow(() -> guard.validateQuantity(line, BigDecimal.TEN));
        assertThrows(ServiceException.class, () -> guard.validateQuantity(line, BigDecimal.ONE));
    }
}