| | |
| | | import static org.mockito.Mockito.doAnswer; |
| | | import static org.mockito.Mockito.never; |
| | | import static org.mockito.Mockito.verify; |
| | | import static org.mockito.Mockito.verifyNoInteractions; |
| | | import static org.mockito.Mockito.when; |
| | | import static org.mockito.Mockito.times; |
| | | |
| | |
| | | { |
| | | TTest test = validTest(); |
| | | mockEnabledOptions(); |
| | | mockGroupSnapshot(); |
| | | when(testMapper.insertTest(any(TTest.class))).thenAnswer(invocation -> { |
| | | invocation.<TTest>getArgument(0).setTestId(10L); |
| | | return 1; |
| | |
| | | assertEquals("WAITING", test.getTestStatus()); |
| | | assertEquals("NOT_STARTED", test.getReceptionStatus()); |
| | | assertEquals(7L, test.getCreatorId()); |
| | | verify(testMapper).insertTestPlanStep(any(TTestPlanStep.class)); |
| | | verify(testMapper, never()).insertTestPlanStep(any(TTestPlanStep.class)); |
| | | verify(testMapper).insertTestChannelBinding(any(TTestChannelBinding.class)); |
| | | verify(testMapper, times(3)).insertTestCollectorChannel(any(TTestCollectorChannel.class)); |
| | | verify(sharedFieldSnapshotService).saveForTest(eq(10L), any()); |
| | | verify(sharedFieldSnapshotService, never()).saveForTest(anyLong(), any()); |
| | | verify(testMapper).insertTestThreshold(any(TTestWarningThreshold.class)); |
| | | verify(testMapper).insertTestAudit(anyLong(), any(), anyLong(), any(), isNull(), any()); |
| | | verify(testMapper, never()).insertTestAudit(anyLong(), any(), anyLong(), any(), isNull(), any()); |
| | | } |
| | | |
| | | @Test |
| | |
| | | TTest test = validTest(); |
| | | test.setPlanSteps(Collections.emptyList()); |
| | | mockEnabledOptions(); |
| | | mockGroupSnapshot(); |
| | | when(testMapper.insertTest(any(TTest.class))).thenAnswer(invocation -> { |
| | | invocation.<TTest>getArgument(0).setTestId(10L); |
| | | return 1; |
| | |
| | | current.setMonitorTimeoutMinutes(30); |
| | | when(testMapper.selectTestByIdForUpdate(10L)).thenReturn(current); |
| | | when(testMapper.selectTestBatteries(10L)).thenReturn(current.getBatteries()); |
| | | when(sharedFieldSnapshotService.selectByTestId(10L)).thenReturn(java.util.List.of()); |
| | | when(sharedFieldSnapshotService.resolveForUpdate(1L, 1L, java.util.List.of())) |
| | | .thenReturn(java.util.List.of()); |
| | | mockEnabledOptions(); |
| | | when(testMapper.updateTest(request)).thenReturn(1); |
| | | doAnswer(invocation -> { |
| | |
| | | assertEquals(1, testService.updateTest(request)); |
| | | assertEquals(45, request.getMonitorTimeoutMinutes()); |
| | | verify(testMapper).deleteTestBindingsByTestId(10L); |
| | | verify(testMapper).deleteTestPlanStepsByTestId(10L); |
| | | verify(testMapper).insertTestAudit(anyLong(), any(), anyLong(), any(), any(), any()); |
| | | verify(testMapper, never()).deleteTestPlanStepsByTestId(10L); |
| | | verify(testMapper, never()).insertTestAudit(anyLong(), any(), anyLong(), any(), any(), any()); |
| | | } |
| | | |
| | | @Test |
| | |
| | | |
| | | when(testMapper.selectTestByIdForUpdate(10L)).thenReturn(current); |
| | | when(testMapper.selectTestBatteries(10L)).thenReturn(current.getBatteries()); |
| | | when(sharedFieldSnapshotService.selectByTestId(10L)).thenReturn(java.util.List.of(originalSnapshot)); |
| | | when(sharedFieldSnapshotService.resolveForUpdate(1L, 1L, java.util.List.of(originalSnapshot))) |
| | | .thenReturn(java.util.List.of(originalSnapshot)); |
| | | mockEnabledOptions(); |
| | | when(testMapper.updateTest(request)).thenReturn(1); |
| | | doAnswer(invocation -> { |
| | |
| | | |
| | | assertEquals(1, testService.updateTest(request)); |
| | | |
| | | verify(sharedFieldSnapshotService).resolveForUpdate(1L, 1L, java.util.List.of(originalSnapshot)); |
| | | verify(sharedFieldSnapshotService).saveForTest(eq(10L), org.mockito.ArgumentMatchers.argThat(snapshots -> |
| | | snapshots.size() == 1 && "72".equals(snapshots.get(0).getFieldValue()))); |
| | | verifyNoInteractions(sharedFieldSnapshotService); |
| | | } |
| | | |
| | | @Test |
| | |
| | | |
| | | assertEquals(1, testService.deleteTestByIds(new Long[] { 10L }, 7L, "operator")); |
| | | |
| | | verify(testMapper).insertTestAudit(eq(10L), eq("TEST_DELETED"), eq(7L), eq("operator"), any(), isNull()); |
| | | verify(testMapper, never()).insertTestAudit(anyLong(), any(), anyLong(), any(), any(), any()); |
| | | } |
| | | |
| | | @Test |
| | | void deleteShouldRejectMismatchedTestAndReceptionStates() |
| | | { |
| | | TTest current = validTest(); |
| | | current.setTestId(10L); |
| | | current.setTestStatus("RUNNING"); |
| | | current.setReceptionStatus("NOT_STARTED"); |
| | | when(testMapper.selectTestByIdForUpdate(10L)).thenReturn(current); |
| | | |
| | | ServiceException exception = assertThrows(ServiceException.class, |
| | | () -> testService.deleteTestByIds(new Long[] { 10L }, 7L, "operator")); |
| | | |
| | | assertEquals("开始接收后不能删除测试", exception.getMessage()); |
| | | verify(testMapper, never()).deleteTestById(anyLong(), any(String.class)); |
| | | } |
| | | |
| | | @Test |
| | |
| | | { |
| | | TTest current = validTest(); |
| | | current.setTestId(10L); |
| | | current.setTestStatus("WAITING"); |
| | | current.setReceptionStatus("NOT_STARTED"); |
| | | current.getBatteries().get(0).getBinding().setBindingId(30L); |
| | | when(testMapper.selectTestByIdForUpdate(10L)).thenReturn(current); |
| | |
| | | when(testMapper.selectReceptionChannelForUpdate(100L)).thenReturn(channel); |
| | | when(testMapper.countCollectorEnabled(8L)).thenReturn(1); |
| | | when(testMapper.selectCollectorChannelCount(8L)).thenReturn(4); |
| | | when(testMapper.collectorOnline(8L)).thenReturn(true); |
| | | when(testMapper.updateReceptionStarted(eq(10L), any(), anyBoolean(), eq("operator"))).thenReturn(1); |
| | | |
| | | assertEquals(1, testService.startReception(10L, 7L, "operator")); |
| | |
| | | { |
| | | TTest current = validTest(); |
| | | current.setTestId(10L); |
| | | current.setTestStatus("WAITING"); |
| | | current.setReceptionStatus("NOT_STARTED"); |
| | | current.getBatteries().get(0).getBinding().setBindingId(30L); |
| | | when(testMapper.selectTestByIdForUpdate(10L)).thenReturn(current); |
| | |
| | | when(testMapper.selectReceptionChannelForUpdate(100L)).thenReturn(channel); |
| | | when(testMapper.countCollectorEnabled(8L)).thenReturn(1); |
| | | when(testMapper.selectCollectorChannelCount(8L)).thenReturn(4); |
| | | when(testMapper.collectorOnline(8L)).thenReturn(true); |
| | | when(testMapper.updateReceptionStarted(eq(10L), any(), anyBoolean(), eq("operator"))).thenReturn(1); |
| | | |
| | | assertEquals(1, testService.startReception(10L, 7L, "operator")); |
| | |
| | | { |
| | | TTest current = validTest(); |
| | | current.setTestId(10L); |
| | | current.setTestStatus("WAITING"); |
| | | current.setReceptionStatus("NOT_STARTED"); |
| | | current.getBatteries().get(0).getBinding().setBindingId(30L); |
| | | when(testMapper.selectTestByIdForUpdate(10L)).thenReturn(current); |
| | |
| | | when(testMapper.selectReceptionChannelForUpdate(100L)).thenReturn(channel); |
| | | when(testMapper.countCollectorEnabled(8L)).thenReturn(1); |
| | | when(testMapper.selectCollectorChannelCount(8L)).thenReturn(4); |
| | | when(testMapper.collectorOnline(8L)).thenReturn(true); |
| | | when(testMapper.updateReceptionStarted(eq(10L), any(), anyBoolean(), eq("operator"))).thenReturn(1); |
| | | |
| | | assertEquals(1, testService.startReception(10L, 7L, "operator")); |
| | |
| | | } |
| | | |
| | | @Test |
| | | void startReceptionShouldRejectOfflineSourceWithoutWritingAnything() |
| | | void startReceptionShouldAllowOfflineSourcesWithoutBlockingReception() |
| | | { |
| | | TTest current = validTest(); |
| | | current.setTestId(10L); |
| | | current.setTestStatus("WAITING"); |
| | | current.setReceptionStatus("NOT_STARTED"); |
| | | current.getBatteries().get(0).getBinding().setBindingId(30L); |
| | | when(testMapper.selectTestByIdForUpdate(10L)).thenReturn(current); |
| | | TDetectionChannel channel = new TDetectionChannel(); |
| | | channel.setChannelId(100L); |
| | | channel.setOnlineStatus("OFFLINE"); |
| | | when(testMapper.selectReceptionChannelForUpdate(100L)).thenReturn(channel); |
| | | when(testMapper.countCollectorEnabled(8L)).thenReturn(1); |
| | | when(testMapper.selectCollectorChannelCount(8L)).thenReturn(4); |
| | | when(testMapper.updateReceptionStarted(eq(10L), any(), anyBoolean(), eq("operator"))).thenReturn(1); |
| | | |
| | | ServiceException exception = assertThrows(ServiceException.class, |
| | | () -> testService.startReception(10L, 7L, "operator")); |
| | | assertEquals(1, testService.startReception(10L, 7L, "operator")); |
| | | } |
| | | |
| | | assertEquals("检测柜通道 100 当前离线或不可观测", exception.getMessage()); |
| | | verify(testMapper, never()).updateReceptionStarted(any(), any(), anyBoolean(), any()); |
| | | @Test |
| | | void startReceptionShouldAllowOfflineCollectorWithoutBlockingReception() |
| | | { |
| | | TTest current = validTest(); |
| | | current.setTestId(10L); |
| | | current.setTestStatus("WAITING"); |
| | | current.setReceptionStatus("NOT_STARTED"); |
| | | current.getBatteries().get(0).getBinding().setBindingId(30L); |
| | | when(testMapper.selectTestByIdForUpdate(10L)).thenReturn(current); |
| | | TDetectionChannel channel = new TDetectionChannel(); |
| | | channel.setChannelId(100L); |
| | | channel.setOnlineStatus("ONLINE"); |
| | | when(testMapper.selectReceptionChannelForUpdate(100L)).thenReturn(channel); |
| | | when(testMapper.countCollectorEnabled(8L)).thenReturn(1); |
| | | when(testMapper.selectCollectorChannelCount(8L)).thenReturn(4); |
| | | when(testMapper.updateReceptionStarted(eq(10L), any(), anyBoolean(), eq("operator"))).thenReturn(1); |
| | | |
| | | assertEquals(1, testService.startReception(10L, 7L, "operator")); |
| | | } |
| | | |
| | | @Test |
| | |
| | | { |
| | | TTest current = validTest(); |
| | | current.setTestId(10L); |
| | | current.setTestStatus("WAITING"); |
| | | current.setReceptionStatus("NOT_STARTED"); |
| | | current.getBatteries().get(0).getBinding().setBindingId(30L); |
| | | current.getBatteries().get(0).setCollectorChannels(Collections.emptyList()); |
| | |
| | | channel.setLastSeenAtUtc(Instant.parse("2026-08-29T12:00:00Z")); |
| | | when(testMapper.selectReceptionChannelForUpdate(100L)).thenReturn(channel); |
| | | when(testMapper.countCollectorEnabled(8L)).thenReturn(1); |
| | | when(testMapper.collectorOnline(8L)).thenReturn(true); |
| | | when(testMapper.selectCollectorChannelCount(8L)).thenReturn(4); |
| | | |
| | | ServiceException exception = assertThrows(ServiceException.class, |
| | |
| | | } |
| | | |
| | | @Test |
| | | void startReceptionShouldRejectMismatchedTestAndReceptionStates() |
| | | { |
| | | TTest current = validTest(); |
| | | current.setTestId(10L); |
| | | current.setTestStatus("COMPLETED"); |
| | | current.setReceptionStatus("NOT_STARTED"); |
| | | when(testMapper.selectTestByIdForUpdate(10L)).thenReturn(current); |
| | | |
| | | ServiceException exception = assertThrows(ServiceException.class, |
| | | () -> testService.startReception(10L, 7L, "operator")); |
| | | |
| | | assertEquals("当前测试不是未开始接收状态", exception.getMessage()); |
| | | verify(testMapper, never()).updateReceptionStarted(any(), any(), anyBoolean(), any()); |
| | | } |
| | | |
| | | @Test |
| | | void pauseReceptionShouldRejectMismatchedTestAndReceptionStates() |
| | | { |
| | | TTest current = validTest(); |
| | | current.setTestId(10L); |
| | | current.setTestStatus("PAUSED"); |
| | | current.setReceptionStatus("RECEIVING"); |
| | | when(testMapper.selectTestByIdForUpdate(10L)).thenReturn(current); |
| | | |
| | | ServiceException exception = assertThrows(ServiceException.class, |
| | | () -> testService.pauseReception(10L, 7L, "operator")); |
| | | |
| | | assertEquals("当前测试不是接收中状态", exception.getMessage()); |
| | | verify(testMapper, never()).updateReceptionPaused(any(), any(), any()); |
| | | } |
| | | |
| | | @Test |
| | | void pauseReceptionShouldStopPollingAndAppendAudit() |
| | | { |
| | | TTest current = validTest(); |
| | | current.setTestId(10L); |
| | | current.setTestStatus("RUNNING"); |
| | | current.setReceptionStatus("RECEIVING"); |
| | | when(testMapper.selectTestByIdForUpdate(10L)).thenReturn(current); |
| | | when(testMapper.updateReceptionPaused(eq(10L), eq("operator"), any())).thenReturn(1); |
| | |
| | | { |
| | | TTest current = validTest(); |
| | | current.setTestId(10L); |
| | | current.setTestStatus("PAUSED"); |
| | | current.setReceptionStatus("PAUSED"); |
| | | List<TTestBattery> configuredBatteries = current.getBatteries(); |
| | | current.setBatteries(null); |
| | |
| | | { |
| | | TTest current = validTest(); |
| | | current.setTestId(10L); |
| | | current.setTestStatus("PAUSED"); |
| | | current.setReceptionStatus("PAUSED"); |
| | | when(testMapper.selectTestByIdForUpdate(10L)).thenReturn(current); |
| | | when(testMapper.updateReceptionStopped(eq(10L), eq("operator"), any())).thenReturn(1); |
| | |
| | | { |
| | | TTest current = validTest(); |
| | | current.setTestId(10L); |
| | | current.setTestStatus("RUNNING"); |
| | | current.setReceptionStatus("RECEIVING"); |
| | | when(testMapper.selectTestByIdForUpdate(10L)).thenReturn(current); |
| | | when(testMapper.updateReceptionStopped(eq(10L), eq("operator"), any())).thenReturn(1); |
| | |
| | | test.setPrincipalId(9L); |
| | | test.setTestTypeId(3L); |
| | | test.setTestPurpose("验证低温循环性能"); |
| | | test.setTestStatus("WAITING"); |
| | | test.setReceptionStatus("NOT_STARTED"); |
| | | test.setMonitorTimeoutMinutes(30); |
| | | test.setBatteries(Collections.singletonList(validBattery("BAT-001", 1L, 100L))); |
| | | test.setPlanSteps(Collections.singletonList(step)); |