package com.zy.asrs.controller; import com.core.common.R; import com.zy.asrs.domain.param.StationCommandMoveParam; import com.zy.asrs.entity.BasDevp; import com.zy.asrs.service.BasDevpService; import com.zy.core.dispatch.StationCommandDispatchResult; import com.zy.core.dispatch.StationCommandDispatcher; import com.zy.core.cache.SlaveConnection; import com.zy.core.enums.SlaveType; import com.zy.core.enums.StationCommandType; import com.zy.core.model.command.StationCommand; import com.zy.core.thread.StationThread; import org.junit.jupiter.api.Test; import org.springframework.test.util.ReflectionTestUtils; import java.util.Collections; import static org.junit.jupiter.api.Assertions.assertEquals; import static org.junit.jupiter.api.Assertions.assertThrows; import static org.mockito.ArgumentMatchers.any; import static org.mockito.ArgumentMatchers.eq; import static org.mockito.ArgumentMatchers.same; import static org.mockito.Mockito.mock; import static org.mockito.Mockito.verify; import static org.mockito.Mockito.when; class StationControllerTest { @Test void controller_noLongerKeepsDispatcherFallbackHelper() { assertThrows(NoSuchMethodException.class, () -> StationController.class.getDeclaredMethod("getStationCommandDispatcher")); } @Test void commandClearPath_callsThreadClearPath() { StationController controller = new StationController(); BasDevpService basDevpService = mock(BasDevpService.class); StationThread stationThread = mock(StationThread.class); ReflectionTestUtils.setField(controller, "basDevpService", basDevpService); BasDevp basDevp = new BasDevp(); basDevp.setStationList("[{\"deviceNo\":1,\"stationId\":145}]"); when(basDevpService.list(any(com.baomidou.mybatisplus.core.conditions.Wrapper.class))) .thenReturn(Collections.singletonList(basDevp)); when(stationThread.clearPath(10335)).thenReturn(true); StationCommandMoveParam param = new StationCommandMoveParam(); param.setStationId(145); param.setTaskNo(10335); SlaveConnection.put(SlaveType.Devp, 1, stationThread); try { R result = controller.commandClearPath(param); assertEquals(200, result.get("code")); verify(stationThread).clearPath(10335); } finally { SlaveConnection.remove(SlaveType.Devp, 1); } } @Test void commandMove_dispatchesViaStationCommandDispatcher() { StationController controller = new StationController(); BasDevpService basDevpService = mock(BasDevpService.class); StationThread stationThread = mock(StationThread.class); StationCommandDispatcher dispatcher = mock(StationCommandDispatcher.class); StationCommand command = new StationCommand(); ReflectionTestUtils.setField(controller, "basDevpService", basDevpService); ReflectionTestUtils.setField(controller, "stationCommandDispatcher", dispatcher); BasDevp basDevp = new BasDevp(); basDevp.setStationList("[{\"deviceNo\":1,\"stationId\":145}]"); when(basDevpService.list(any(com.baomidou.mybatisplus.core.conditions.Wrapper.class))) .thenReturn(Collections.singletonList(basDevp)); when(stationThread.getCommand(StationCommandType.MOVE, 10335, 145, 188, 0)).thenReturn(command); when(dispatcher.dispatch(1, command, "station-controller", "manual-move")) .thenReturn(StationCommandDispatchResult.accepted("accepted", 1, "station-controller", "manual-move")); StationCommandMoveParam param = new StationCommandMoveParam(); param.setStationId(145); param.setTaskNo(10335); param.setTargetStationId(188); SlaveConnection.put(SlaveType.Devp, 1, stationThread); try { R result = controller.commandMove(param); assertEquals(200, result.get("code")); verify(dispatcher).dispatch(eq(1), same(command), eq("station-controller"), eq("manual-move")); } finally { SlaveConnection.remove(SlaveType.Devp, 1); } } }