| File was renamed from ruoyi-admin/src/test/java/com/ruoyi/system/service/impl/TTestServiceImplTest.java |
| | |
| | | package com.ruoyi.system.service.impl; |
| | | package com.battery.business.service.impl; |
| | | |
| | | import static org.junit.jupiter.api.Assertions.assertEquals; |
| | | import static org.junit.jupiter.api.Assertions.assertThrows; |
| | |
| | | import org.mockito.Mock; |
| | | import org.mockito.junit.jupiter.MockitoExtension; |
| | | import com.ruoyi.common.exception.ServiceException; |
| | | import com.ruoyi.system.domain.TTest; |
| | | import com.ruoyi.system.domain.TTestBattery; |
| | | import com.ruoyi.system.domain.TTestChannelBinding; |
| | | import com.ruoyi.system.domain.TTestCollectorChannel; |
| | | import com.ruoyi.system.domain.TTestFieldSnapshot; |
| | | import com.ruoyi.system.domain.TTestPlanStep; |
| | | import com.ruoyi.system.domain.TTestWarningThreshold; |
| | | import com.ruoyi.system.domain.TDetectionChannel; |
| | | import com.ruoyi.system.mapper.TTestMapper; |
| | | import com.ruoyi.system.service.ISharedFieldSnapshotService; |
| | | import com.battery.business.domain.TTest; |
| | | import com.battery.business.domain.TTestBattery; |
| | | import com.battery.business.domain.TTestChannelBinding; |
| | | import com.battery.business.domain.TTestCollectorChannel; |
| | | import com.battery.business.domain.TTestWarningThreshold; |
| | | import com.battery.business.domain.TDetectionChannel; |
| | | import com.battery.business.mapper.TTestMapper; |
| | | |
| | | /** 测试配置服务的业务契约测试。 */ |
| | | @ExtendWith(MockitoExtension.class) |
| | |
| | | @Mock |
| | | private TTestMapper testMapper; |
| | | |
| | | @Mock |
| | | private ISharedFieldSnapshotService sharedFieldSnapshotService; |
| | | |
| | | |
| | | @InjectMocks |
| | | private TTestServiceImpl testService; |
| | |
| | | assertEquals("WAITING", test.getTestStatus()); |
| | | assertEquals("NOT_STARTED", test.getReceptionStatus()); |
| | | assertEquals(7L, test.getCreatorId()); |
| | | verify(testMapper, never()).insertTestPlanStep(any(TTestPlanStep.class)); |
| | | verify(testMapper).insertTestChannelBinding(any(TTestChannelBinding.class)); |
| | | verify(testMapper, times(3)).insertTestCollectorChannel(any(TTestCollectorChannel.class)); |
| | | verify(sharedFieldSnapshotService, never()).saveForTest(anyLong(), any()); |
| | | verify(testMapper).insertTestThreshold(any(TTestWarningThreshold.class)); |
| | | verify(testMapper, never()).insertTestAudit(anyLong(), any(), anyLong(), any(), isNull(), any()); |
| | | } |
| | | |
| | | @Test |
| | |
| | | void createShouldAllowPlanStepsToBeConfiguredLater() |
| | | { |
| | | TTest test = validTest(); |
| | | test.setPlanSteps(Collections.emptyList()); |
| | | mockEnabledOptions(); |
| | | when(testMapper.insertTest(any(TTest.class))).thenAnswer(invocation -> { |
| | | invocation.<TTest>getArgument(0).setTestId(10L); |
| | |
| | | }).when(testMapper).insertTestBattery(any(TTestBattery.class)); |
| | | |
| | | assertEquals(10L, testService.createTest(test)); |
| | | |
| | | verify(testMapper, never()).insertTestPlanStep(any(TTestPlanStep.class)); |
| | | } |
| | | |
| | | @Test |
| | |
| | | assertEquals(1, testService.updateTest(request)); |
| | | assertEquals(45, request.getMonitorTimeoutMinutes()); |
| | | verify(testMapper).deleteTestBindingsByTestId(10L); |
| | | verify(testMapper, never()).deleteTestPlanStepsByTestId(10L); |
| | | verify(testMapper, never()).insertTestAudit(anyLong(), any(), anyLong(), any(), any(), any()); |
| | | } |
| | | |
| | | @Test |
| | | void updateSameGroupShouldPreserveOriginalFieldSnapshot() |
| | | { |
| | | TTest request = validTest(); |
| | | request.setTestId(10L); |
| | | TTest current = validTest(); |
| | | current.setTestId(10L); |
| | | current.setCreatorId(7L); |
| | | current.setReceptionStatus("NOT_STARTED"); |
| | | TTestFieldSnapshot originalSnapshot = new TTestFieldSnapshot(); |
| | | originalSnapshot.setFieldDefinitionId(11L); |
| | | originalSnapshot.setFieldName("额定容量"); |
| | | originalSnapshot.setFieldType("number"); |
| | | originalSnapshot.setFieldValue("72"); |
| | | |
| | | when(testMapper.selectTestByIdForUpdate(10L)).thenReturn(current); |
| | | when(testMapper.selectTestBatteries(10L)).thenReturn(current.getBatteries()); |
| | | mockEnabledOptions(); |
| | | when(testMapper.updateTest(request)).thenReturn(1); |
| | | doAnswer(invocation -> { |
| | | invocation.<TTestBattery>getArgument(0).setRelationId(21L); |
| | | return 1; |
| | | }).when(testMapper).insertTestBattery(any(TTestBattery.class)); |
| | | |
| | | assertEquals(1, testService.updateTest(request)); |
| | | |
| | | verifyNoInteractions(sharedFieldSnapshotService); |
| | | } |
| | | |
| | | @Test |
| | |
| | | when(testMapper.deleteTestById(10L, "operator")).thenReturn(1); |
| | | |
| | | assertEquals(1, testService.deleteTestByIds(new Long[] { 10L }, 7L, "operator")); |
| | | |
| | | verify(testMapper, never()).insertTestAudit(anyLong(), any(), anyLong(), any(), any(), any()); |
| | | } |
| | | |
| | | @Test |
| | | void deleteShouldRejectMismatchedTestAndReceptionStates() |
| | | void deletesStartedTestAndAllChildFacts() |
| | | { |
| | | 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)); |
| | | when(testMapper.deleteTestById(10L, "operator")).thenReturn(1); |
| | | assertEquals(1, testService.deleteTestByIds(new Long[] { 10L }, 7L, "operator")); |
| | | verify(testMapper).deleteTestWarningExceptionsByTestId(10L); |
| | | verify(testMapper).deleteTestDataRevisionActionsByTestId(10L); |
| | | verify(testMapper).deleteTestLogicStepMappingsByTestId(10L); |
| | | } |
| | | |
| | | @Test |
| | |
| | | when(testMapper.updateReceptionStarted(eq(10L), any(), anyBoolean(), eq("operator"))).thenReturn(1); |
| | | |
| | | assertEquals(1, testService.startReception(10L, 7L, "operator")); |
| | | verify(testMapper).insertTestAudit(eq(10L), eq("RECEPTION_STARTED"), eq(7L), eq("operator"), isNull(), any()); |
| | | } |
| | | |
| | | @Test |
| | |
| | | when(testMapper.updateReceptionStarted(eq(10L), any(), anyBoolean(), eq("operator"))).thenReturn(1); |
| | | |
| | | assertEquals(1, testService.startReception(10L, 7L, "operator")); |
| | | verify(testMapper).insertTestAudit(eq(10L), eq("RECEPTION_STARTED"), eq(7L), eq("operator"), isNull(), |
| | | org.mockito.ArgumentMatchers.argThat(value -> value != null && value.contains("runtimeAttach") |
| | | && value.contains("true"))); |
| | | } |
| | | |
| | | @Test |
| | |
| | | |
| | | assertEquals(1, testService.pauseReception(10L, 7L, "operator")); |
| | | verify(testMapper).updateReceptionPaused(eq(10L), eq("operator"), any()); |
| | | verify(testMapper).insertTestAudit(eq(10L), eq("RECEPTION_PAUSED"), eq(7L), eq("operator"), isNull(), |
| | | org.mockito.ArgumentMatchers.argThat(value -> value != null && value.contains("STOPPED"))); |
| | | } |
| | | |
| | | @Test |
| | |
| | | |
| | | assertEquals(1, testService.resumeReception(10L, 7L, "operator")); |
| | | verify(testMapper).updateReceptionResumed(eq(10L), eq("operator"), any()); |
| | | verify(testMapper).insertTestAudit(eq(10L), eq("RECEPTION_RESUMED"), eq(7L), eq("operator"), isNull(), |
| | | org.mockito.ArgumentMatchers.argThat(value -> value != null && value.contains("RESUMED"))); |
| | | } |
| | | |
| | | @Test |
| | |
| | | |
| | | assertEquals(1, testService.stopTest(10L, 7L, "operator")); |
| | | verify(testMapper).updateReceptionStopped(eq(10L), eq("operator"), any()); |
| | | verify(testMapper).insertTestAudit(eq(10L), eq("TEST_STOPPED"), eq(7L), eq("operator"), isNull(), |
| | | org.mockito.ArgumentMatchers.argThat(value -> value != null && value.contains("COMPLETED"))); |
| | | } |
| | | |
| | | @Test |
| | |
| | | when(testMapper.updateReceptionStopped(eq(10L), eq("operator"), any())).thenReturn(1); |
| | | |
| | | testService.stopTest(10L, 7L, "operator"); |
| | | |
| | | verify(testMapper).insertTestAudit(eq(10L), eq("TEST_STOPPED"), eq(7L), eq("operator"), isNull(), |
| | | org.mockito.ArgumentMatchers.argThat(value -> value != null |
| | | && value.contains("IMMEDIATE"))); |
| | | } |
| | | |
| | | private void mockEnabledOptions() |
| | |
| | | when(testMapper.countTestTypeEnabled(3L)).thenReturn(1); |
| | | } |
| | | |
| | | private void mockGroupSnapshot() |
| | | { |
| | | TTestFieldSnapshot snapshot = new TTestFieldSnapshot(); |
| | | snapshot.setFieldDefinitionId(11L); |
| | | snapshot.setFieldName("额定容量"); |
| | | snapshot.setFieldType("number"); |
| | | snapshot.setFieldValue("72"); |
| | | when(sharedFieldSnapshotService.createForBatteryGroup(1L)).thenReturn(java.util.List.of(snapshot)); |
| | | } |
| | | |
| | | private TTest validTest() |
| | | { |
| | | TTestPlanStep step = new TTestPlanStep(); |
| | | step.setCycleNo(1); |
| | | step.setStepNo(1); |
| | | step.setStepType("CONSTANT_CURRENT_CHARGE"); |
| | | step.setStepName("恒流充电"); |
| | | step.setSortOrder(1); |
| | | LinkedHashMap<String, Object> parameters = new LinkedHashMap<>(); |
| | | parameters.put("currentA", new BigDecimal("1.5")); |
| | | step.setParameters(parameters); |
| | | |
| | | TTestWarningThreshold threshold = new TTestWarningThreshold(); |
| | | threshold.setMetricCode("VOLTAGE"); |
| | | threshold.setMinValue(new BigDecimal("2.5")); |
| | |
| | | test.setReceptionStatus("NOT_STARTED"); |
| | | test.setMonitorTimeoutMinutes(30); |
| | | test.setBatteries(Collections.singletonList(validBattery("BAT-001", 1L, 100L))); |
| | | test.setPlanSteps(Collections.singletonList(step)); |
| | | test.setThresholds(Collections.singletonList(threshold)); |
| | | return test; |
| | | } |