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

  1. package com.qyft.ms;
  2. import com.qyft.ms.app.model.entity.OperationLog;
  3. import com.qyft.ms.app.model.entity.SysSettings;
  4. import com.qyft.ms.app.model.form.CMDForm;
  5. import com.qyft.ms.app.service.*;
  6. import com.qyft.ms.device.service.DeviceTcpCMDService;
  7. import org.junit.jupiter.api.BeforeEach;
  8. import org.junit.jupiter.api.Test;
  9. import org.mockito.*;
  10. import java.util.*;
  11. import static com.qyft.ms.app.common.generator.PathGenerator.generatePathPoints;
  12. import static org.mockito.Mockito.*;
  13. public class SprayTest {
  14. @InjectMocks
  15. private CMDService cmdService;
  16. @Mock
  17. private WebSocketService webSocketService;
  18. @Mock
  19. private DeviceTcpCMDService deviceTcpCMDService;
  20. @Mock
  21. private OperationLogService operationLogService;
  22. @Mock
  23. private MatrixCraftService matrixCraftService;
  24. @Mock
  25. private ISysSettingsService sysSettingsService;
  26. @BeforeEach
  27. void setUp() {
  28. MockitoAnnotations.openMocks(this);
  29. }
  30. @Test
  31. void testStartWork() {
  32. // 1. 准备模拟数据
  33. CMDForm form = new CMDForm();
  34. form.setCommandId("cmd123");
  35. form.setCommandName("startWork");
  36. Map<String, Object> params = new HashMap<>();
  37. params.put("space", 1);
  38. params.put("routeType", 1);
  39. params.put("movementSpeed", 100);
  40. params.put("height", 50);
  41. params.put("voltage", 100);
  42. params.put("matrixFlowVelocity", 20);
  43. params.put("matrixCraftId", 1);
  44. List<Map<String, Integer>> positionList = new ArrayList<>();
  45. Map<String, Integer> position = new HashMap<>();
  46. position.put("x1", 10);
  47. position.put("y1", 20);
  48. position.put("x2", 30);
  49. position.put("y2", 40);
  50. position.put("index", 0);
  51. positionList.add(position);
  52. params.put("position", positionList);
  53. form.setParams(params);
  54. // 2. 模拟依赖行为
  55. SysSettings mockSetting = new SysSettings();
  56. mockSetting.setValue("100,200,300"); // 模拟托盘坐标
  57. when(sysSettingsService.getSlidePositionList()).thenReturn(Collections.singletonList(mockSetting));
  58. when(deviceTcpCMDService.switchThreeWayValveToSpray()).thenReturn(true);
  59. when(deviceTcpCMDService.setMotorSpeed(anyString(), anyInt())).thenReturn(true);
  60. when(deviceTcpCMDService.moveMotorToPosition(anyString(), anyDouble())).thenReturn(true);
  61. when(deviceTcpCMDService.turnOnHighVoltage(anyDouble())).thenReturn(true);
  62. when(deviceTcpCMDService.controlValve(anyString(), anyBoolean())).thenReturn(true);
  63. when(deviceTcpCMDService.syringePumpMoveAtSpeed(anyInt())).thenReturn(true);
  64. when(deviceTcpCMDService.turnOffSyringePump()).thenReturn(true);
  65. when(deviceTcpCMDService.motorMoveToHome(anyString())).thenReturn(true);
  66. // OperationLog mockLog = new OperationLog();
  67. // when(operationLogService.getIng()).thenReturn(mockLog);
  68. // when(operationLogService.add(any())).thenReturn(true);
  69. // when(operationLogService.updateById(any())).thenReturn(true);
  70. // 3. 调用测试方法
  71. String result = cmdService.startWork(form);
  72. // 4. 验证
  73. verify(deviceTcpCMDService, times(1)).switchThreeWayValveToSpray();
  74. verify(deviceTcpCMDService, times(1)).setMotorSpeed("x", 100);
  75. verify(deviceTcpCMDService, times(1)).setMotorSpeed("y", 100);
  76. verify(deviceTcpCMDService, times(1)).setMotorSpeed("z", 100);
  77. verify(deviceTcpCMDService, times(1)).moveMotorToPosition("z", 350); // z + height (300+50)
  78. verify(deviceTcpCMDService, times(1)).moveMotorToPosition("x", 110); // x+10
  79. verify(deviceTcpCMDService, times(1)).moveMotorToPosition("y", 220); // y+20
  80. verify(deviceTcpCMDService, times(1)).turnOnHighVoltage(100);
  81. // 日志校验
  82. // verify(operationLogService, times(1)).add(any(OperationLog.class));
  83. // verify(operationLogService, times(1)).updateById(any(OperationLog.class));
  84. // 轨迹移动和其他控制指令同理,这里可以补充更多verify
  85. verify(deviceTcpCMDService, atLeastOnce()).moveMotorToPosition(eq("x"), anyDouble());
  86. verify(deviceTcpCMDService, atLeastOnce()).moveMotorToPosition(eq("y"), anyDouble());
  87. // 6. 说明
  88. System.out.println("✅ startWork测试通过!");
  89. }
  90. }