package com.zy.core.utils; import com.zy.asrs.entity.BasDevp; import com.zy.asrs.entity.WrkMast; import com.zy.core.model.command.StationCommand; import com.zy.core.model.protocol.StationProtocol; import com.zy.core.model.protocol.StationTaskBufferItem; import com.zy.core.thread.StationThread; import com.zy.core.utils.station.StationRerouteProcessor; import com.zy.core.utils.station.model.RerouteCommandPlan; import com.zy.core.utils.station.model.RerouteContext; import com.zy.core.utils.station.model.RerouteDecision; import com.zy.core.utils.station.model.RerouteExecutionResult; import com.zy.core.utils.station.model.RerouteSceneType; import org.junit.jupiter.api.Test; import java.util.Collections; import java.util.List; import static org.junit.jupiter.api.Assertions.assertEquals; import static org.junit.jupiter.api.Assertions.assertSame; import static org.junit.jupiter.api.Assertions.assertThrows; import static org.junit.jupiter.api.Assertions.assertTrue; import static org.mockito.Mockito.mock; import static org.mockito.Mockito.verify; import static org.mockito.Mockito.when; class StationRerouteProcessorTest { @Test void rerouteProcessor_noLongerKeepsDispatcherFallbackHelper() { assertThrows(NoSuchMethodException.class, () -> StationRerouteProcessor.class.getDeclaredMethod("getStationCommandDispatcher")); } @Test void buildCommandPlan_usesRunBlockCommandBuilderForRunBlockScene() { StationRerouteProcessor processor = new StationRerouteProcessor(); StationThread stationThread = mock(StationThread.class); StationCommand command = new StationCommand(); command.setTaskNo(100); command.setStationId(10); command.setTargetStaNo(20); when(stationThread.getRunBlockRerouteCommand(100, 10, 20, 0, 0.25d)).thenReturn(command); RerouteContext context = RerouteContext.create( RerouteSceneType.RUN_BLOCK_REROUTE, buildBasDevp(1), stationThread, buildStationProtocol(10, 100, 10), buildWrkMast(100, 99), Collections.emptyList(), 0.25d, "checkStationRunBlock_reroute" ).withRunBlockCommand() .withCancelSessionBeforeDispatch() .withResetSegmentCommandsBeforeDispatch(); RerouteCommandPlan plan = processor.buildRerouteCommandPlan( context, RerouteDecision.proceed(20) ); verify(stationThread).getRunBlockRerouteCommand(100, 10, 20, 0, 0.25d); assertSame(command, plan.command()); } @Test void executePlan_skipsWhenCurrentTaskStillExistsInBuffer() { StationRerouteProcessor processor = new StationRerouteProcessor(); StationCommand command = new StationCommand(); command.setTaskNo(100); command.setStationId(10); command.setTargetStaNo(20); StationTaskBufferItem bufferItem = new StationTaskBufferItem(); bufferItem.setTaskNo(100); RerouteContext context = RerouteContext.create( RerouteSceneType.OUT_ORDER, buildBasDevp(1), mock(StationThread.class), buildStationProtocol(10, 100, 10, Collections.singletonList(bufferItem)), buildWrkMast(100, 20), List.of(10, 20), 0.0d, "checkStationOutOrder" ); RerouteExecutionResult result = processor.executeReroutePlan( context, RerouteCommandPlan.dispatch( command, RerouteDecision.proceed(20), "checkStationOutOrder" ) ); assertTrue(result.skipped()); assertEquals("buffer-has-current-task", result.skipReason()); } private static BasDevp buildBasDevp(int devpNo) { BasDevp basDevp = new BasDevp(); basDevp.setDevpNo(devpNo); return basDevp; } private static WrkMast buildWrkMast(int wrkNo, int targetStationId) { WrkMast wrkMast = new WrkMast(); wrkMast.setWrkNo(wrkNo); wrkMast.setStaNo(targetStationId); return wrkMast; } private static StationProtocol buildStationProtocol(int stationId, int taskNo, int targetStationId) { return buildStationProtocol(stationId, taskNo, targetStationId, Collections.emptyList()); } private static StationProtocol buildStationProtocol(int stationId, int taskNo, int targetStationId, List taskBufferItems) { StationProtocol stationProtocol = new StationProtocol(); stationProtocol.setStationId(stationId); stationProtocol.setTaskNo(taskNo); stationProtocol.setTargetStaNo(targetStationId); stationProtocol.setTaskBufferItems(taskBufferItems); return stationProtocol; } }