Junjie
2026-04-27 91fbede5aa117b3c010e9cbb8804df90757dbe5c
src/test/java/com/zy/ai/service/AutoTuneApplyServiceImplTest.java
@@ -50,8 +50,10 @@
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.ArgumentMatchers.eq;
import static org.mockito.Mockito.atLeastOnce;
import static org.mockito.Mockito.doThrow;
import static org.mockito.Mockito.inOrder;
import static org.mockito.Mockito.never;
import static org.mockito.Mockito.times;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;
@@ -316,6 +318,27 @@
    }
    @Test
    void applyCacheRefreshFailureDoesNotAppendFailedAuditOrJob() {
        when(configService.getOne(any(Wrapper.class))).thenReturn(config("conveyorStationTaskLimit", "10"));
        doThrow(new IllegalStateException("cache refresh failed"))
                .when(configService).refreshSystemConfigCache();
        AutoTuneApplyResult result = service.apply(request(false,
                command("sys_config", null, "conveyorStationTaskLimit", "15")
        ));
        List<AiAutoTuneChange> changes = savedChanges();
        AiAutoTuneJob updatedJob = updatedJob();
        assertTrue(result.getSuccess());
        assertEquals("success", updatedJob.getStatus());
        assertEquals(1, changes.size());
        assertEquals("success", changes.get(0).getResultStatus());
        verify(aiAutoTuneChangeService, times(1)).saveBatch(any(Collection.class));
        verify(aiAutoTuneJobService, times(1)).updateById(any(AiAutoTuneJob.class));
        verify(configService).refreshSystemConfigCache();
    }
    @Test
    void writeFailureReturnsFailedAuditWithoutThrowing() {
        when(configService.getOne(any(Wrapper.class))).thenReturn(config("conveyorStationTaskLimit", "10"));
        when(configService.saveConfigValue("conveyorStationTaskLimit", "15")).thenThrow(new IllegalStateException("db write failed"));
@@ -438,6 +461,31 @@
    }
    @Test
    void rollbackCacheRefreshFailureDoesNotAppendFailedAuditOrJob() {
        AiAutoTuneJob latestRealJob = job(10L, "manual", "success");
        AiAutoTuneChange configChange = successChange(10L, "sys_config", "", "conveyorStationTaskLimit", "10", "15");
        when(aiAutoTuneChangeService.list(any(Wrapper.class)))
                .thenReturn(List.of(configChange))
                .thenReturn(List.of(configChange));
        when(aiAutoTuneJobService.getById(10L)).thenReturn(latestRealJob);
        when(configService.getOne(any(Wrapper.class))).thenReturn(config("conveyorStationTaskLimit", "15"));
        doThrow(new IllegalStateException("cache refresh failed"))
                .when(configService).refreshSystemConfigCache();
        AutoTuneApplyResult result = service.rollbackLastSuccessfulJob("manual rollback");
        List<AiAutoTuneChange> changes = savedChanges();
        AiAutoTuneJob updatedJob = updatedJob();
        assertTrue(result.getSuccess());
        assertEquals("success", updatedJob.getStatus());
        assertEquals(1, changes.size());
        assertEquals("success", changes.get(0).getResultStatus());
        verify(aiAutoTuneChangeService, times(1)).saveBatch(any(Collection.class));
        verify(aiAutoTuneJobService, times(1)).updateById(any(AiAutoTuneJob.class));
        verify(configService).refreshSystemConfigCache();
    }
    @Test
    void rollbackAuditSaveBatchFailureRollsBackTargetWriteAndReturnsFailedAudit() {
        AiAutoTuneJob latestRealJob = job(10L, "manual", "success");
        AiAutoTuneChange configChange = successChange(10L, "sys_config", "", "conveyorStationTaskLimit", "10", "15");