feat: 新增会话误操作结束接口

This commit is contained in:
2026-08-15 21:24:59 +08:00
parent 0413013f24
commit a6f684c8b7
7 changed files with 175 additions and 0 deletions
@@ -4,9 +4,11 @@ import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.guo.learningprogresstracker.entity.StudyReportsEntity;
import com.guo.learningprogresstracker.entity.StudySessionsEntity;
import com.guo.learningprogresstracker.enums.StudySessionStateEnum;
import com.guo.learningprogresstracker.exception.ErrorParameterException;
import com.guo.learningprogresstracker.mapper.StudyReportFragmentsMapper;
import com.guo.learningprogresstracker.mapper.StudyReportsMapper;
import com.guo.learningprogresstracker.mapper.StudySessionsMapper;
import com.guo.learningprogresstracker.service.StudyExpectationsService;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
@@ -17,6 +19,7 @@ import org.mockito.junit.jupiter.MockitoSettings;
import org.mockito.quality.Strictness;
import org.mockito.junit.jupiter.MockitoExtension;
import java.io.Serializable;
import java.time.LocalDateTime;
import java.util.Optional;
@@ -41,10 +44,15 @@ class StudySessionsServiceImplTest {
@Mock
private StudySessionsMapper studySessionsMapper;
@Mock
private StudyExpectationsService studyExpectationsService;
@BeforeEach
void setUp() {
// stub updateById,避免调用真实的 getBaseMapper()
doReturn(true).when(studySessionsService).updateById(any());
// stub removeById,避免调用真实的 getBaseMapper()
doReturn(true).when(studySessionsService).removeById(any(Serializable.class));
}
private StudySessionsEntity createSession(String state, int minutesAgo) {
@@ -210,4 +218,99 @@ class StudySessionsServiceImplTest {
verify(studySessionsService).updateById(session);
}
// ============ 误操作结束(零数据关闭) ============
/**
* 场景11:确认语正确且未产生残片 → 删除预期与会话,零数据关闭
*/
@Test
void abortStudySession_success_shouldDeleteEverything() throws Exception {
StudySessionsEntity session = createSession(StudySessionStateEnum.ONGOING.name(), 5);
session.setId(1);
mockGetOneOpt(session);
doReturn(0L).when(studyReportFragmentsMapper).selectCount(any());
studySessionsService.abortStudySession("SESSION_TEST", StudySessionsServiceImpl.MISOPERATION_CONFIRMATION);
verify(studyExpectationsService).deleteBySessionNum("SESSION_TEST");
verify(studySessionsService).removeById(1);
}
/**
* 场景12:确认语不正确 → 抛异常,不删除任何数据
*/
@Test
void abortStudySession_wrongConfirmation_shouldThrow() {
StudySessionsEntity session = createSession(StudySessionStateEnum.ONGOING.name(), 5);
mockGetOneOpt(session);
ErrorParameterException ex = assertThrows(ErrorParameterException.class, () ->
studySessionsService.abortStudySession("SESSION_TEST", "我不是确认语"));
assertEquals("确认语输入不正确,请核对后重新输入", ex.getMessage());
verify(studyExpectationsService, never()).deleteBySessionNum(any());
verify(studySessionsService, never()).removeById(any());
}
/**
* 场景13:确认语首尾空白应被忽略,仍视为正确
*/
@Test
void abortStudySession_confirmationWithSpaces_shouldSucceed() throws Exception {
StudySessionsEntity session = createSession(StudySessionStateEnum.PAUSED.name(), 5);
session.setId(2);
mockGetOneOpt(session);
doReturn(0L).when(studyReportFragmentsMapper).selectCount(any());
studySessionsService.abortStudySession("SESSION_TEST",
" " + StudySessionsServiceImpl.MISOPERATION_CONFIRMATION + " ");
verify(studyExpectationsService).deleteBySessionNum("SESSION_TEST");
verify(studySessionsService).removeById(2);
}
/**
* 场景14:已产生学习残片 → 抛异常,不能误操作结束
*/
@Test
void abortStudySession_hasFragments_shouldThrow() {
StudySessionsEntity session = createSession(StudySessionStateEnum.ONGOING.name(), 5);
mockGetOneOpt(session);
doReturn(1L).when(studyReportFragmentsMapper).selectCount(any());
ErrorParameterException ex = assertThrows(ErrorParameterException.class, () ->
studySessionsService.abortStudySession("SESSION_TEST", StudySessionsServiceImpl.MISOPERATION_CONFIRMATION));
assertEquals("这次学习已经产生了学习残片,不能按误操作结束了哦", ex.getMessage());
verify(studyExpectationsService, never()).deleteBySessionNum(any());
verify(studySessionsService, never()).removeById(any());
}
/**
* 场景15:会话已结束 → 抛异常
*/
@Test
void abortStudySession_endedSession_shouldThrow() {
StudySessionsEntity session = createSession(StudySessionStateEnum.ENDED.name(), 5);
mockGetOneOpt(session);
ErrorParameterException ex = assertThrows(ErrorParameterException.class, () ->
studySessionsService.abortStudySession("SESSION_TEST", StudySessionsServiceImpl.MISOPERATION_CONFIRMATION));
assertEquals("这次学习会话已经结束,不能再误操作结束了", ex.getMessage());
verify(studySessionsService, never()).removeById(any());
}
/**
* 场景16:会话不存在 → 抛异常
*/
@Test
void abortStudySession_sessionNotFound_shouldThrow() {
doReturn(Optional.empty()).when(studySessionsService).getOneOpt(any(Wrapper.class));
assertThrows(ErrorParameterException.class, () ->
studySessionsService.abortStudySession("NOT_EXIST", StudySessionsServiceImpl.MISOPERATION_CONFIRMATION));
}
}