You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

111 lines
4.3 KiB

package com.qyft.ms;
import com.qyft.ms.app.model.entity.OperationLog;
import com.qyft.ms.app.model.entity.SysSettings;
import com.qyft.ms.app.model.form.CMDForm;
import com.qyft.ms.app.service.*;
import com.qyft.ms.device.service.DeviceTcpCMDService;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.mockito.*;
import java.util.*;
import static com.qyft.ms.app.common.generator.PathGenerator.generatePathPoints;
import static org.mockito.Mockito.*;
public class SprayTest {
@InjectMocks
private CMDService cmdService;
@Mock
private WebSocketService webSocketService;
@Mock
private DeviceTcpCMDService deviceTcpCMDService;
@Mock
private OperationLogService operationLogService;
@Mock
private MatrixCraftService matrixCraftService;
@Mock
private ISysSettingsService sysSettingsService;
@BeforeEach
void setUp() {
MockitoAnnotations.openMocks(this);
}
@Test
void testStartWork() {
// 1. 准备模拟数据
CMDForm form = new CMDForm();
form.setCommandId("cmd123");
form.setCommandName("startWork");
Map<String, Object> params = new HashMap<>();
params.put("space", 1);
params.put("routeType", 1);
params.put("movementSpeed", 100);
params.put("height", 50);
params.put("voltage", 100);
params.put("matrixFlowVelocity", 20);
params.put("matrixCraftId", 1);
List<Map<String, Integer>> positionList = new ArrayList<>();
Map<String, Integer> position = new HashMap<>();
position.put("x1", 10);
position.put("y1", 20);
position.put("x2", 30);
position.put("y2", 40);
position.put("index", 0);
positionList.add(position);
params.put("position", positionList);
form.setParams(params);
// 2. 模拟依赖行为
SysSettings mockSetting = new SysSettings();
mockSetting.setValue("100,200,300"); // 模拟托盘坐标
when(sysSettingsService.getSlidePositionList()).thenReturn(Collections.singletonList(mockSetting));
when(deviceTcpCMDService.switchThreeWayValveToSpray()).thenReturn(true);
when(deviceTcpCMDService.setMotorSpeed(anyString(), anyInt())).thenReturn(true);
when(deviceTcpCMDService.moveMotorToPosition(anyString(), anyDouble())).thenReturn(true);
when(deviceTcpCMDService.turnOnHighVoltage(anyDouble())).thenReturn(true);
when(deviceTcpCMDService.controlValve(anyString(), anyBoolean())).thenReturn(true);
when(deviceTcpCMDService.syringePumpMoveAtSpeed(anyInt())).thenReturn(true);
when(deviceTcpCMDService.turnOffSyringePump()).thenReturn(true);
when(deviceTcpCMDService.motorMoveToHome(anyString())).thenReturn(true);
// OperationLog mockLog = new OperationLog();
// when(operationLogService.getIng()).thenReturn(mockLog);
// when(operationLogService.add(any())).thenReturn(true);
// when(operationLogService.updateById(any())).thenReturn(true);
// 3. 调用测试方法
String result = cmdService.startWork(form);
// 4. 验证
verify(deviceTcpCMDService, times(1)).switchThreeWayValveToSpray();
verify(deviceTcpCMDService, times(1)).setMotorSpeed("x", 100);
verify(deviceTcpCMDService, times(1)).setMotorSpeed("y", 100);
verify(deviceTcpCMDService, times(1)).setMotorSpeed("z", 100);
verify(deviceTcpCMDService, times(1)).moveMotorToPosition("z", 350); // z + height (300+50)
verify(deviceTcpCMDService, times(1)).moveMotorToPosition("x", 110); // x+10
verify(deviceTcpCMDService, times(1)).moveMotorToPosition("y", 220); // y+20
verify(deviceTcpCMDService, times(1)).turnOnHighVoltage(100);
// 日志校验
// verify(operationLogService, times(1)).add(any(OperationLog.class));
// verify(operationLogService, times(1)).updateById(any(OperationLog.class));
// 轨迹移动和其他控制指令同理,这里可以补充更多verify
verify(deviceTcpCMDService, atLeastOnce()).moveMotorToPosition(eq("x"), anyDouble());
verify(deviceTcpCMDService, atLeastOnce()).moveMotorToPosition(eq("y"), anyDouble());
// 6. 说明
System.out.println("✅ startWork测试通过!");
}
}