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)); } }