|
|
@ -1,10 +1,12 @@ |
|
|
|
package com.qyft.ms; |
|
|
|
|
|
|
|
import com.qyft.ms.app.model.entity.SysSettings; |
|
|
|
import com.qyft.ms.app.model.form.CMDForm; |
|
|
|
import com.qyft.ms.app.model.vo.ExecutionResult; |
|
|
|
import com.qyft.ms.app.service.CMDService; |
|
|
|
import com.qyft.ms.app.service.ISysSettingsService; |
|
|
|
import com.qyft.ms.app.service.OperationLogService; |
|
|
|
import com.qyft.ms.app.service.WebSocketService; |
|
|
|
import com.qyft.ms.device.service.DeviceStatusService; |
|
|
|
import com.qyft.ms.device.service.DeviceTcpCMDService; |
|
|
|
import org.junit.jupiter.api.BeforeEach; |
|
|
|
import org.junit.jupiter.api.DisplayName; |
|
|
@ -13,137 +15,245 @@ import org.mockito.InjectMocks; |
|
|
|
import org.mockito.Mock; |
|
|
|
import org.mockito.MockitoAnnotations; |
|
|
|
import org.springframework.boot.test.context.SpringBootTest; |
|
|
|
import org.springframework.test.context.ActiveProfiles; |
|
|
|
import org.springframework.transaction.annotation.Transactional; |
|
|
|
|
|
|
|
import java.util.ArrayList; |
|
|
|
import java.util.HashMap; |
|
|
|
import java.util.Collections; |
|
|
|
import java.util.List; |
|
|
|
import java.util.Map; |
|
|
|
import java.util.function.Supplier; |
|
|
|
|
|
|
|
import static org.junit.jupiter.api.Assertions.*; |
|
|
|
import static org.mockito.ArgumentMatchers.*; |
|
|
|
import static org.mockito.Mockito.*; |
|
|
|
import static org.junit.jupiter.api.Assertions.assertNotEquals; |
|
|
|
import static org.junit.jupiter.api.Assertions.assertTrue; |
|
|
|
import static org.mockito.Mockito.when; |
|
|
|
|
|
|
|
@DisplayName("指令测试") |
|
|
|
@ActiveProfiles("test") |
|
|
|
@SpringBootTest |
|
|
|
public class CMDServiceTest { |
|
|
|
@Transactional // 测试完成后回滚数据 |
|
|
|
@DisplayName("指令单元测试") |
|
|
|
@SpringBootTest(classes = MatrixSprayApplication.class, |
|
|
|
webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT) |
|
|
|
class CMDServiceTest { |
|
|
|
|
|
|
|
@Mock |
|
|
|
private DeviceTcpCMDService deviceTcpCMDService; |
|
|
|
@InjectMocks |
|
|
|
private CMDService cmdService; |
|
|
|
|
|
|
|
@Mock |
|
|
|
private WebSocketService webSocketService; |
|
|
|
|
|
|
|
@Mock |
|
|
|
private DeviceTcpCMDService deviceTcpCMDService; |
|
|
|
@Mock |
|
|
|
private OperationLogService operationLogService; |
|
|
|
|
|
|
|
@InjectMocks |
|
|
|
private CMDService cmdService; |
|
|
|
|
|
|
|
private CMDForm form; |
|
|
|
@Mock |
|
|
|
private ISysSettingsService sysSettingsService; |
|
|
|
@Mock |
|
|
|
private DeviceStatusService deviceStatusService; |
|
|
|
|
|
|
|
@BeforeEach |
|
|
|
void setUp() { |
|
|
|
MockitoAnnotations.openMocks(this); |
|
|
|
form = new CMDForm(); |
|
|
|
form.setCommandId("test-command"); |
|
|
|
form.setCommandName("test-command"); |
|
|
|
} |
|
|
|
|
|
|
|
@DisplayName("测试轴移动") |
|
|
|
@DisplayName("电机移动") |
|
|
|
@Test |
|
|
|
void testMoveMotorToPosition_Success() { |
|
|
|
// 准备参数 |
|
|
|
Map<String, Object> params = new HashMap<>(); |
|
|
|
params.put("axis", "x"); |
|
|
|
params.put("position", 100.0); |
|
|
|
form.setParams(params); |
|
|
|
void moveMotorToPosition() { |
|
|
|
CMDForm form = createForm(Map.of("axis", "X", "position", 100.0)); |
|
|
|
when(deviceTcpCMDService.moveMotorToPosition("X", 100.0)).thenReturn(true); |
|
|
|
|
|
|
|
// 模拟设备服务调用 |
|
|
|
when(deviceTcpCMDService.moveMotorToPosition(anyString(), anyDouble())).thenReturn(true); |
|
|
|
assertTrue(cmdService.moveMotorToPosition(form)); |
|
|
|
} |
|
|
|
|
|
|
|
// 执行测试 |
|
|
|
boolean result = cmdService.moveMotorToPosition(form); |
|
|
|
@DisplayName("三通阀") |
|
|
|
@Test |
|
|
|
void switchThreeWayValve() { |
|
|
|
CMDForm form = createForm(Map.of("type", "clear_nozzle")); |
|
|
|
when(deviceTcpCMDService.switchThreeWayValve("clear_nozzle")).thenReturn(true); |
|
|
|
|
|
|
|
// 验证结果 |
|
|
|
assertTrue(result); |
|
|
|
verify(deviceTcpCMDService, times(1)).moveMotorToPosition("x", 100.0); |
|
|
|
assertTrue(cmdService.switchThreeWayValve(form)); |
|
|
|
} |
|
|
|
|
|
|
|
@DisplayName("测试开始喷涂") |
|
|
|
@DisplayName("阀门控制") |
|
|
|
@Test |
|
|
|
void testStartWork_InvalidPosition() { |
|
|
|
// 准备无效参数 |
|
|
|
Map<String, Object> params = new HashMap<>(); |
|
|
|
params.put("position", new ArrayList<>()); // 空位置列表 |
|
|
|
form.setParams(params); |
|
|
|
void controlValve() { |
|
|
|
CMDForm form = createForm(Map.of("type", "Nozzle", "isOpen", true)); |
|
|
|
when(deviceTcpCMDService.controlValve("Nozzle", true)).thenReturn(true); |
|
|
|
|
|
|
|
// 执行测试 |
|
|
|
String result = cmdService.startWork(form); |
|
|
|
assertTrue(cmdService.controlValve(form)); |
|
|
|
} |
|
|
|
|
|
|
|
// 验证返回错误信息 |
|
|
|
assertEquals("position参数错误", result); |
|
|
|
} |
|
|
|
|
|
|
|
@DisplayName("测试开始喷涂路径规划") |
|
|
|
@Test |
|
|
|
void testStartWork_PathGeneration() { |
|
|
|
// 准备有效参数 |
|
|
|
Map<String, Object> params = new HashMap<>(); |
|
|
|
List<Map<String, Integer>> positions = new ArrayList<>(); |
|
|
|
Map<String, Integer> pos = new HashMap<>(); |
|
|
|
pos.put("x1", 10); |
|
|
|
pos.put("y1", 20); |
|
|
|
pos.put("x2", 30); |
|
|
|
pos.put("y2", 40); |
|
|
|
pos.put("index", 0); |
|
|
|
positions.add(pos); |
|
|
|
params.put("position", positions); |
|
|
|
params.put("space", 5); |
|
|
|
params.put("routeType", 1); |
|
|
|
params.put("height", 50); |
|
|
|
params.put("movementSpeed", 100); |
|
|
|
form.setParams(params); |
|
|
|
@DisplayName("以指定电压值开启高压电") |
|
|
|
@Test |
|
|
|
void turnOnHighVoltage() { |
|
|
|
CMDForm form = createForm(Map.of("voltage", 4500)); |
|
|
|
when(deviceTcpCMDService.turnOnHighVoltage(4500)).thenReturn(true); |
|
|
|
|
|
|
|
// 模拟依赖项 |
|
|
|
when(deviceTcpCMDService.switchThreeWayValveToSpray()).thenReturn(true); |
|
|
|
when(deviceTcpCMDService.setMotorSpeed(anyString(), anyInt())).thenReturn(true); |
|
|
|
when(deviceTcpCMDService.moveMotorToPosition(anyString(), anyDouble())).thenReturn(true); |
|
|
|
assertTrue(cmdService.turnOnHighVoltage(form)); |
|
|
|
} |
|
|
|
|
|
|
|
// 执行测试 |
|
|
|
String result = cmdService.startWork(form); |
|
|
|
@DisplayName("关闭高压电") |
|
|
|
@Test |
|
|
|
void turnOffHighVoltage() { |
|
|
|
CMDForm form = new CMDForm(); |
|
|
|
when(deviceTcpCMDService.turnOffHighVoltage()).thenReturn(true); |
|
|
|
|
|
|
|
// 验证命令链长度 |
|
|
|
assertNotNull(result); |
|
|
|
verify(deviceTcpCMDService, atLeastOnce()).switchThreeWayValveToSpray(); |
|
|
|
assertTrue(cmdService.turnOffHighVoltage(form)); |
|
|
|
} |
|
|
|
|
|
|
|
@DisplayName("注射泵开启") |
|
|
|
@Test |
|
|
|
void testControlValve_NozzleOpen() { |
|
|
|
Map<String, Object> params = new HashMap<>(); |
|
|
|
params.put("valveType", "Nozzle"); |
|
|
|
params.put("isOpen", true); |
|
|
|
form.setParams(params); |
|
|
|
void turnOnSyringePump() { |
|
|
|
CMDForm form = createForm(Map.of("rotationSpeed", 30.0)); |
|
|
|
when(deviceTcpCMDService.turnOnSyringePump(30.0)).thenReturn(true); |
|
|
|
|
|
|
|
assertTrue(cmdService.turnOnSyringePump(form)); |
|
|
|
} |
|
|
|
|
|
|
|
@DisplayName("注射泵关闭") |
|
|
|
@Test |
|
|
|
void turnOffSyringePump() { |
|
|
|
CMDForm form =createForm(Map.of()); |
|
|
|
when(deviceTcpCMDService.turnOffSyringePump()).thenReturn(true); |
|
|
|
System.out.println("参数===========" + form); |
|
|
|
|
|
|
|
assertTrue(cmdService.turnOffSyringePump(form)); |
|
|
|
} |
|
|
|
|
|
|
|
when(deviceTcpCMDService.controlValve(anyString(), anyBoolean())).thenReturn(true); |
|
|
|
@DisplayName("设置电机电流") |
|
|
|
@Test |
|
|
|
void setMotorRunningCurrent() { |
|
|
|
CMDForm form = createForm(Map.of("axis", "X", "current", 1.5)); |
|
|
|
when(deviceTcpCMDService.setMotorRunningCurrent("X", 1.5)).thenReturn(true); |
|
|
|
|
|
|
|
assertTrue(cmdService.setMotorRunningCurrent(form)); |
|
|
|
} |
|
|
|
|
|
|
|
@DisplayName("托盘推出") |
|
|
|
@Test |
|
|
|
void trayOut() { |
|
|
|
CMDForm form = new CMDForm(); |
|
|
|
when(deviceTcpCMDService.moveMotorToPosition("Y", 150)).thenReturn(true); |
|
|
|
|
|
|
|
boolean result = cmdService.controlValve(form); |
|
|
|
assertTrue(result); |
|
|
|
verify(deviceTcpCMDService).controlValve("Nozzle", true); |
|
|
|
assertTrue(cmdService.trayOut(form)); |
|
|
|
} |
|
|
|
|
|
|
|
@DisplayName("托盘推入") |
|
|
|
@Test |
|
|
|
void testRun_CommandFailure() { |
|
|
|
// 模拟一个失败的指令 |
|
|
|
List<Supplier<Boolean>> cmdList = new ArrayList<>(); |
|
|
|
cmdList.add(() -> false); // 直接返回失败 |
|
|
|
void trayIn() { |
|
|
|
CMDForm form = new CMDForm(); |
|
|
|
when(deviceTcpCMDService.moveMotorToPosition("Y", 0)).thenReturn(true); |
|
|
|
|
|
|
|
// 执行内部方法 |
|
|
|
cmdService.run(cmdList, form); |
|
|
|
assertTrue(cmdService.trayIn(form)); |
|
|
|
} |
|
|
|
|
|
|
|
@DisplayName("电机停止") |
|
|
|
@Test |
|
|
|
void stopMotor() { |
|
|
|
CMDForm form = createForm(Map.of("axis", "Z")); |
|
|
|
when(deviceTcpCMDService.stopMotor("Z")).thenReturn(true); |
|
|
|
|
|
|
|
assertTrue(cmdService.stopMotor(form)); |
|
|
|
} |
|
|
|
|
|
|
|
// 验证WebSocket错误消息推送 |
|
|
|
verify(webSocketService).pushMsg(any(), any(ExecutionResult.class)); |
|
|
|
@DisplayName("照明灯开启") |
|
|
|
@Test |
|
|
|
void turnOnLightPanel() { |
|
|
|
CMDForm form = new CMDForm(); |
|
|
|
when(deviceTcpCMDService.turnOnLightPanel()).thenReturn(true); |
|
|
|
|
|
|
|
assertTrue(cmdService.turnOnLightPanel(form)); |
|
|
|
} |
|
|
|
|
|
|
|
@DisplayName("照明灯关闭") |
|
|
|
@Test |
|
|
|
void turnOffLightPanel() { |
|
|
|
CMDForm form = new CMDForm(); |
|
|
|
when(deviceTcpCMDService.turnOffLightPanel()).thenReturn(true); |
|
|
|
|
|
|
|
assertTrue(cmdService.turnOffLightPanel(form)); |
|
|
|
} |
|
|
|
|
|
|
|
@DisplayName("电机回原点") |
|
|
|
@Test |
|
|
|
void motorMoveToHome() { |
|
|
|
CMDForm form = createForm(Map.of("axis", "Y")); |
|
|
|
when(deviceTcpCMDService.motorMoveToHome("Y")).thenReturn(true); |
|
|
|
|
|
|
|
assertTrue(cmdService.motorMoveToHome(form)); |
|
|
|
} |
|
|
|
|
|
|
|
@DisplayName("预充") |
|
|
|
@Test |
|
|
|
void startPrefill() { |
|
|
|
CMDForm form = createForm(Map.of("rotationSpeed", 50.0)); |
|
|
|
when(deviceTcpCMDService.switchThreeWayValve("clear_spray")).thenReturn(true); |
|
|
|
when(deviceTcpCMDService.controlValve("Nozzle", true)).thenReturn(true); |
|
|
|
when(deviceTcpCMDService.turnOnSyringePump(50.0)).thenReturn(true); |
|
|
|
|
|
|
|
assertTrue(cmdService.startPrefill(form)); |
|
|
|
} |
|
|
|
|
|
|
|
@DisplayName("停止预充") |
|
|
|
@Test |
|
|
|
void stopPrefill() { |
|
|
|
CMDForm form = new CMDForm(); |
|
|
|
when(deviceTcpCMDService.turnOffSyringePump()).thenReturn(true); |
|
|
|
when(deviceTcpCMDService.controlValve("Nozzle", false)).thenReturn(true); |
|
|
|
|
|
|
|
assertTrue(cmdService.stopPrefill(form)); |
|
|
|
} |
|
|
|
|
|
|
|
@DisplayName("开始喷头清洗") |
|
|
|
@Test |
|
|
|
void startWash_nozzleType() { |
|
|
|
CMDForm form = createForm(Map.of("type", "nozzle")); |
|
|
|
when(deviceTcpCMDService.moveMotorToPosition("X", 173.08)).thenReturn(true); |
|
|
|
when(deviceTcpCMDService.moveMotorToPosition("Y", 75.2)).thenReturn(true); |
|
|
|
when(deviceTcpCMDService.moveMotorToPosition("Z", 70)).thenReturn(true); |
|
|
|
when(deviceTcpCMDService.switchThreeWayValve("clear_nozzle")).thenReturn(true); |
|
|
|
when(deviceTcpCMDService.controlValve("Nozzle", true)).thenReturn(true); |
|
|
|
|
|
|
|
assertTrue(cmdService.startWash(form)); |
|
|
|
} |
|
|
|
|
|
|
|
@DisplayName("结束清洗") |
|
|
|
@Test |
|
|
|
void stopWash() { |
|
|
|
CMDForm form = new CMDForm(); |
|
|
|
when(deviceTcpCMDService.switchThreeWayValve("close_all")).thenReturn(true); |
|
|
|
when(deviceTcpCMDService.controlValve("Cleaning", false)).thenReturn(true); |
|
|
|
when(deviceTcpCMDService.controlValve("Nozzle", false)).thenReturn(true); |
|
|
|
|
|
|
|
assertTrue(cmdService.stopWash(form)); |
|
|
|
} |
|
|
|
|
|
|
|
@DisplayName("设置点击运行速度") |
|
|
|
@Test |
|
|
|
void setMotorSpeed() { |
|
|
|
CMDForm form = createForm(Map.of("axis", "X", "speed", 100.0)); |
|
|
|
when(deviceTcpCMDService.setMotorSpeed("X", 100.0)).thenReturn(true); |
|
|
|
|
|
|
|
assertTrue(cmdService.setMotorSpeed(form)); |
|
|
|
} |
|
|
|
|
|
|
|
@DisplayName("开始喷涂") |
|
|
|
@Test |
|
|
|
void startWork_pathGeneration() { |
|
|
|
CMDForm form = createForm(Map.of( |
|
|
|
"position", List.of(Map.of("x1", 0, "y1", 0, "x2", 20, "y2", 50, "index", 0)), |
|
|
|
"space", 5, |
|
|
|
"routeType", 1 |
|
|
|
)); |
|
|
|
|
|
|
|
SysSettings setting = new SysSettings(); |
|
|
|
setting.setValue("10,20,30"); |
|
|
|
when(sysSettingsService.getSlidePositionList()).thenReturn(Collections.singletonList(setting)); |
|
|
|
|
|
|
|
String result = cmdService.startWork(form); |
|
|
|
System.out.println(result); |
|
|
|
|
|
|
|
assertNotEquals("路径规划失败", result); |
|
|
|
} |
|
|
|
|
|
|
|
// 工具方法,简化Form构造 |
|
|
|
private CMDForm createForm(Map<String, Object> params) { |
|
|
|
CMDForm form = new CMDForm(); |
|
|
|
form.setParams(params); |
|
|
|
return form; |
|
|
|
} |
|
|
|
} |
|
|
|
} |