package com.zy.asrs.task.support; import com.zy.asrs.entity.WrkMast; import com.zy.asrs.service.WrkMastService; import com.zy.common.entity.Parameter; import org.junit.jupiter.api.AfterEach; import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; import org.junit.jupiter.api.extension.ExtendWith; import org.mockito.Mock; import org.mockito.junit.jupiter.MockitoExtension; import org.springframework.test.util.ReflectionTestUtils; import java.lang.reflect.Constructor; import java.lang.reflect.Field; import java.util.Arrays; import java.util.Collections; import static org.junit.jupiter.api.Assertions.assertEquals; import static org.junit.jupiter.api.Assertions.assertNull; import static org.junit.jupiter.api.Assertions.assertTrue; import static org.mockito.ArgumentMatchers.any; import static org.mockito.Mockito.when; @ExtendWith(MockitoExtension.class) class OutboundBatchSeqReleaseGuardTest { @Mock private WrkMastService wrkMastService; private OutboundBatchSeqReleaseGuard guard; @BeforeEach void setUp() throws Exception { guard = new OutboundBatchSeqReleaseGuard(); ReflectionTestUtils.setField(guard, "wrkMastService", wrkMastService); setThreshold("2"); } @AfterEach void tearDown() throws Exception { setParameterInstance(null); } @Test void rejectsEmptyBatchSeq() { String message = guard.validateReady("ORD1", ""); assertEquals("出库进仓编号(batchSeq)为空,跳过下发, userNo=ORD1", message); } @Test void blocksWhenEarlierBatchSeqStillRunning() { when(wrkMastService.selectList(any())).thenReturn(Arrays.asList( wrkMast("1", 12L), wrkMast("2", 11L))); String message = guard.validateReady("ORD1", "2"); assertTrue(message.contains("blockingBatchSeq=1")); } @Test void allowsWhenEarlierBatchSeqIsReleaseStatusAndStatus25BelowThreshold() { when(wrkMastService.selectList(any())).thenReturn(Arrays.asList( wrkMast("1", 25L), wrkMast("2", 11L))); assertNull(guard.validateReady("ORD1", "2")); } @Test void blocksWhenStatus25CountReachesThreshold() throws Exception { setThreshold("1"); when(wrkMastService.selectList(any())).thenReturn(Arrays.asList( wrkMast("1", 25L), wrkMast("2", 11L))); String message = guard.validateReady("ORD1", "2"); assertTrue(message.contains("status25Count=1")); assertTrue(message.contains("threshold=1")); } @Test void treatsErpReportPendingStatusAsReleased() { when(wrkMastService.selectList(any())).thenReturn(Arrays.asList( wrkMast("1", 17L), wrkMast("2", 11L))); assertNull(guard.validateReady("ORD1", "2")); } @Test void treatsStationOutCompleteStatusAsReleased() { when(wrkMastService.selectList(any())).thenReturn(Arrays.asList( wrkMast("1", 26L), wrkMast("2", 11L))); assertNull(guard.validateReady("ORD1", "2")); } @Test void firstBatchSeqIsNotBlockedByThreshold() throws Exception { setThreshold("1"); when(wrkMastService.selectList(any())).thenReturn(Collections.singletonList(wrkMast("1", 25L))); assertNull(guard.validateReady("ORD1", "1")); } private static WrkMast wrkMast(String batchSeq, Long wrkSts) { WrkMast wrkMast = new WrkMast(); wrkMast.setBatchSeq(batchSeq); wrkMast.setWrkSts(wrkSts); return wrkMast; } private static void setThreshold(String threshold) throws Exception { Constructor constructor = Parameter.class.getDeclaredConstructor(); constructor.setAccessible(true); Parameter parameter = constructor.newInstance(); parameter.setOutBatchSeqStatus25Threshold(threshold); setParameterInstance(parameter); } private static void setParameterInstance(Parameter parameter) throws Exception { Field instance = Parameter.class.getDeclaredField("instance"); instance.setAccessible(true); instance.set(null, parameter); } }