Commit 66392f2e authored by wujipeng's avatar wujipeng
Browse files

bug修复

parent 4e10634c
package com.platform.business.mapper;
import org.apache.ibatis.builder.xml.XMLMapperBuilder;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.Configuration;
import org.junit.jupiter.api.Test;
import java.io.InputStream;
import static org.junit.jupiter.api.Assertions.assertTrue;
/** 启动前验证新增规则、教师关系及权限SQL都能被MyBatis解析。 */
class PracticeRuleAndTeacherMapperXmlTest {
@Test
void newMapperStatementsAndPermissionFragmentsShouldParse() throws Exception {
Configuration configuration = new Configuration();
parse(configuration, "com/platform/business/mapper/PracticePaperRuleMapper.xml");
parse(configuration, "com/platform/business/mapper/ExamTeacherAssignmentMapper.xml");
parse(configuration, "com/platform/business/mapper/ExamReviewMapper.xml");
parse(configuration, "com/platform/business/mapper/TeacherHomeMapper.xml");
assertTrue(configuration.hasStatement(
"com.platform.business.mapper.PracticePaperRuleMapper.pageDefinitions"));
assertTrue(configuration.hasStatement(
"com.platform.business.mapper.PracticePaperRuleMapper.insertDefinitionDetails"));
assertTrue(configuration.hasStatement(
"com.platform.business.mapper.ExamTeacherAssignmentMapper.insertBatch"));
assertTrue(configuration.hasStatement(
"com.platform.business.mapper.ExamReviewMapper.lockTask"));
assertTrue(configuration.hasStatement(
"com.platform.business.mapper.TeacherHomeMapper.selectUpcomingExams"));
}
private void parse(Configuration configuration, String resource) throws Exception {
try (InputStream input = Resources.getResourceAsStream(resource)) {
XMLMapperBuilder builder = new XMLMapperBuilder(
input, configuration, resource, configuration.getSqlFragments());
builder.parse();
}
}
}
...@@ -96,12 +96,14 @@ class TeacherScoreMapperXmlTest { ...@@ -96,12 +96,14 @@ class TeacherScoreMapperXmlTest {
* 管理端只允许创建未来考试,因此列表一旦重新加入 DATE_ADD/NOW 时间门槛,新增考试 * 管理端只允许创建未来考试,因此列表一旦重新加入 DATE_ADD/NOW 时间门槛,新增考试
* 就会再次被隐藏。这里明确锁定“只按主考教师归属过滤”的同步规则。 * 就会再次被隐藏。这里明确锁定“只按主考教师归属过滤”的同步规则。
*/ */
assertTrue(listSql.contains("where e.chief_id = ?")); assertTrue(listSql.contains("from exam_teacher_assignment chief_assignment"));
assertTrue(listSql.contains("e.chief_id = ?"));
assertFalse(listSql.contains("date_add(e.exam_time")); assertFalse(listSql.contains("date_add(e.exam_time"));
assertFalse(listSql.contains("<= now()")); assertFalse(listSql.contains("<= now()"));
} }
private void assertVisibleExamProtection(String sql) { private void assertVisibleExamProtection(String sql) {
assertTrue(sql.contains("from exam_teacher_assignment chief_assignment"));
assertTrue(sql.contains("e.chief_id = ?")); assertTrue(sql.contains("e.chief_id = ?"));
assertFalse(sql.contains("date_add(e.exam_time")); assertFalse(sql.contains("date_add(e.exam_time"));
} }
......
...@@ -2,18 +2,22 @@ package com.platform.business.service.impl; ...@@ -2,18 +2,22 @@ package com.platform.business.service.impl;
import com.platform.business.constant.ExamAdminConstants; import com.platform.business.constant.ExamAdminConstants;
import com.platform.business.mapper.ExamMapper; import com.platform.business.mapper.ExamMapper;
import com.platform.business.mapper.ExamTeacherAssignmentMapper;
import com.platform.business.model.dto.ExamAdminSaveDTO; import com.platform.business.model.dto.ExamAdminSaveDTO;
import com.platform.business.model.dto.ExamAdminUpdateDTO; import com.platform.business.model.dto.ExamAdminUpdateDTO;
import com.platform.business.model.dto.NotificationPublishDTO; import com.platform.business.model.dto.NotificationPublishDTO;
import com.platform.business.model.entity.ExamEntity; import com.platform.business.model.entity.ExamEntity;
import com.platform.business.model.entity.ExamStudentEntity; import com.platform.business.model.entity.ExamStudentEntity;
import com.platform.business.model.entity.ExamTeacherAssignmentEntity;
import com.platform.business.model.entity.TheoreticalPaperEntity; import com.platform.business.model.entity.TheoreticalPaperEntity;
import com.platform.business.model.entity.PracticePaperRuleDefinitionEntity;
import com.platform.business.model.vo.ExamAdminClassVO; import com.platform.business.model.vo.ExamAdminClassVO;
import com.platform.business.model.vo.ExamAdminDetailVO; import com.platform.business.model.vo.ExamAdminDetailVO;
import com.platform.business.service.ClassAdminService; import com.platform.business.service.ClassAdminService;
import com.platform.business.service.BusinessNotificationPublisher; import com.platform.business.service.BusinessNotificationPublisher;
import com.platform.business.service.ExamStudentService; import com.platform.business.service.ExamStudentService;
import com.platform.business.service.TheoreticalPaperService; import com.platform.business.service.TheoreticalPaperService;
import com.platform.business.service.PracticePaperRuleService;
import com.platform.exception.BusinessException; import com.platform.exception.BusinessException;
import org.junit.jupiter.api.Test; import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith; import org.junit.jupiter.api.extension.ExtendWith;
...@@ -37,6 +41,7 @@ import static org.mockito.ArgumentMatchers.any; ...@@ -37,6 +41,7 @@ import static org.mockito.ArgumentMatchers.any;
import static org.mockito.ArgumentMatchers.anyList; import static org.mockito.ArgumentMatchers.anyList;
import static org.mockito.ArgumentMatchers.eq; import static org.mockito.ArgumentMatchers.eq;
import static org.mockito.Mockito.never; import static org.mockito.Mockito.never;
import static org.mockito.Mockito.lenient;
import static org.mockito.Mockito.times; import static org.mockito.Mockito.times;
import static org.mockito.Mockito.verify; import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when; import static org.mockito.Mockito.when;
...@@ -48,12 +53,18 @@ class ExamAdminServiceImplTest { ...@@ -48,12 +53,18 @@ class ExamAdminServiceImplTest {
@Mock @Mock
private ExamMapper examMapper; private ExamMapper examMapper;
@Mock
private ExamTeacherAssignmentMapper examTeacherAssignmentMapper;
@Mock @Mock
private ClassAdminService classAdminService; private ClassAdminService classAdminService;
@Mock @Mock
private TheoreticalPaperService theoreticalPaperService; private TheoreticalPaperService theoreticalPaperService;
@Mock
private PracticePaperRuleService practicePaperRuleService;
@Mock @Mock
private ExamStudentService examStudentService; private ExamStudentService examStudentService;
...@@ -96,7 +107,7 @@ class ExamAdminServiceImplTest { ...@@ -96,7 +107,7 @@ class ExamAdminServiceImplTest {
mockValidRelationships(); mockValidRelationships();
when(examMapper.countAdminExamDuplicateName(1, "测试考试", null)).thenReturn(0); when(examMapper.countAdminExamDuplicateName(1, "测试考试", null)).thenReturn(0);
when(examMapper.selectAdminExamConflicts(any(), eq(90), eq(1), when(examMapper.selectAdminExamConflicts(any(), eq(90), eq(1),
eq("teacher-1"), eq("teacher-2"), anyList(), eq(null))) anyList(), anyList(), eq(null)))
.thenReturn(Collections.emptyList()); .thenReturn(Collections.emptyList());
when(examMapper.insert(any(ExamEntity.class))).thenAnswer(invocation -> { when(examMapper.insert(any(ExamEntity.class))).thenAnswer(invocation -> {
ExamEntity entity = invocation.getArgument(0); ExamEntity entity = invocation.getArgument(0);
...@@ -145,17 +156,20 @@ class ExamAdminServiceImplTest { ...@@ -145,17 +156,20 @@ class ExamAdminServiceImplTest {
} }
@Test @Test
void createPracticeShouldUseManualTotalAndKeepPaperEmpty() { void createPracticeShouldUsePublishedRuleTotalAndKeepPaperEmpty() {
ExamAdminSaveDTO request = validCreateRequest(); ExamAdminSaveDTO request = validCreateRequest();
request.setType("实训"); request.setType("实训");
request.setPaperId(null); request.setPaperId(null);
request.setTotalScore(new BigDecimal("120")); request.setTotalScore(null);
request.setPracticeRuleId(6);
request.setPassScore(null); request.setPassScore(null);
mockWritableClass(); mockWritableClass();
mockValidRelationships(); mockValidRelationships();
when(practicePaperRuleService.requirePublishedDefinition(6, "污水一级"))
.thenReturn(practiceRule(6, "一级", "120.00"));
when(examMapper.countAdminExamDuplicateName(1, "测试考试", null)).thenReturn(0); when(examMapper.countAdminExamDuplicateName(1, "测试考试", null)).thenReturn(0);
when(examMapper.selectAdminExamConflicts(any(), eq(90), eq(1), when(examMapper.selectAdminExamConflicts(any(), eq(90), eq(1),
eq("teacher-1"), eq("teacher-2"), anyList(), eq(null))) anyList(), anyList(), eq(null)))
.thenReturn(Collections.emptyList()); .thenReturn(Collections.emptyList());
when(examMapper.insert(any(ExamEntity.class))).thenAnswer(invocation -> { when(examMapper.insert(any(ExamEntity.class))).thenAnswer(invocation -> {
ExamEntity entity = invocation.getArgument(0); ExamEntity entity = invocation.getArgument(0);
...@@ -175,6 +189,7 @@ class ExamAdminServiceImplTest { ...@@ -175,6 +189,7 @@ class ExamAdminServiceImplTest {
ArgumentCaptor<ExamEntity> captor = ArgumentCaptor.forClass(ExamEntity.class); ArgumentCaptor<ExamEntity> captor = ArgumentCaptor.forClass(ExamEntity.class);
verify(examMapper).insert(captor.capture()); verify(examMapper).insert(captor.capture());
assertEquals(null, captor.getValue().getPaperId()); assertEquals(null, captor.getValue().getPaperId());
assertEquals(6, captor.getValue().getPracticeRuleId());
assertEquals(new BigDecimal("120.00"), captor.getValue().getTotalScore()); assertEquals(new BigDecimal("120.00"), captor.getValue().getTotalScore());
assertEquals(new BigDecimal("72.00"), captor.getValue().getPassScore()); assertEquals(new BigDecimal("72.00"), captor.getValue().getPassScore());
verify(theoreticalPaperService, never()).getById(any()); verify(theoreticalPaperService, never()).getById(any());
...@@ -217,8 +232,8 @@ class ExamAdminServiceImplTest { ...@@ -217,8 +232,8 @@ class ExamAdminServiceImplTest {
mockValidPaper(); mockValidPaper();
when(examMapper.lockAdminExamUsers(anyList())) when(examMapper.lockAdminExamUsers(anyList()))
.thenReturn(Arrays.asList("student-1", "student-2", "teacher-1", "teacher-2")); .thenReturn(Arrays.asList("student-1", "student-2", "teacher-1", "teacher-2"));
when(examMapper.countAdminExamActiveTeacher("teacher-1")).thenReturn(1); when(examMapper.selectAdminExamActiveTeacherIds(anyList()))
when(examMapper.countAdminExamActiveTeacher("teacher-2")).thenReturn(1); .thenReturn(Arrays.asList("teacher-1", "teacher-2"));
when(examMapper.lockAdminExamStudents(1, request.getStudentIds())) when(examMapper.lockAdminExamStudents(1, request.getStudentIds()))
.thenReturn(Collections.singletonList("student-1")); .thenReturn(Collections.singletonList("student-1"));
...@@ -241,7 +256,7 @@ class ExamAdminServiceImplTest { ...@@ -241,7 +256,7 @@ class ExamAdminServiceImplTest {
conflict.setClassId(1); conflict.setClassId(1);
conflict.setName("已有考试"); conflict.setName("已有考试");
when(examMapper.selectAdminExamConflicts(any(), eq(90), eq(1), when(examMapper.selectAdminExamConflicts(any(), eq(90), eq(1),
eq("teacher-1"), eq("teacher-2"), anyList(), eq(null))) anyList(), anyList(), eq(null)))
.thenReturn(Collections.singletonList(conflict)); .thenReturn(Collections.singletonList(conflict));
BusinessException error = assertThrows(BusinessException.class, BusinessException error = assertThrows(BusinessException.class,
...@@ -265,7 +280,7 @@ class ExamAdminServiceImplTest { ...@@ -265,7 +280,7 @@ class ExamAdminServiceImplTest {
conflict.setChiefExaminerId("teacher-3"); conflict.setChiefExaminerId("teacher-3");
conflict.setInvigilatorId("teacher-4"); conflict.setInvigilatorId("teacher-4");
when(examMapper.selectAdminExamConflicts(any(), eq(90), eq(1), when(examMapper.selectAdminExamConflicts(any(), eq(90), eq(1),
eq("teacher-1"), eq("teacher-2"), anyList(), eq(null))) anyList(), anyList(), eq(null)))
.thenReturn(Collections.singletonList(conflict)); .thenReturn(Collections.singletonList(conflict));
BusinessException error = assertThrows(BusinessException.class, BusinessException error = assertThrows(BusinessException.class,
...@@ -321,6 +336,8 @@ class ExamAdminServiceImplTest { ...@@ -321,6 +336,8 @@ class ExamAdminServiceImplTest {
existing.setType(ExamAdminConstants.TYPE_PRACTICE); existing.setType(ExamAdminConstants.TYPE_PRACTICE);
existing.setExamTime(Timestamp.valueOf("2099-09-18 09:00:00")); existing.setExamTime(Timestamp.valueOf("2099-09-18 09:00:00"));
existing.setInvigilationId("teacher-2"); existing.setInvigilationId("teacher-2");
existing.setChiefId("teacher-1");
existing.setPracticeRuleId(6);
existing.setPaperId("paper-1"); existing.setPaperId("paper-1");
existing.setTotalScore(new BigDecimal("100.00")); existing.setTotalScore(new BigDecimal("100.00"));
existing.setPassScore(new BigDecimal("60.00")); existing.setPassScore(new BigDecimal("60.00"));
...@@ -331,9 +348,11 @@ class ExamAdminServiceImplTest { ...@@ -331,9 +348,11 @@ class ExamAdminServiceImplTest {
when(examMapper.selectAdminExamStudentIds(7)) when(examMapper.selectAdminExamStudentIds(7))
.thenReturn(Arrays.asList("student-1", "student-2")); .thenReturn(Arrays.asList("student-1", "student-2"));
mockValidRelationships(); mockValidRelationships();
when(practicePaperRuleService.requirePublishedDefinition(6, "污水一级"))
.thenReturn(practiceRule(6, "一级", "100.00"));
when(examMapper.countAdminExamDuplicateName(1, "修改后考试", 7)).thenReturn(0); when(examMapper.countAdminExamDuplicateName(1, "修改后考试", 7)).thenReturn(0);
when(examMapper.selectAdminExamConflicts(any(), eq(120), eq(1), when(examMapper.selectAdminExamConflicts(any(), eq(120), eq(1),
eq("teacher-1"), eq("teacher-2"), anyList(), eq(7))) anyList(), anyList(), eq(7)))
.thenReturn(Collections.emptyList()); .thenReturn(Collections.emptyList());
when(examMapper.updateAdminExam(any(ExamEntity.class))).thenReturn(1); when(examMapper.updateAdminExam(any(ExamEntity.class))).thenReturn(1);
ExamAdminDetailVO detail = new ExamAdminDetailVO(); ExamAdminDetailVO detail = new ExamAdminDetailVO();
...@@ -356,10 +375,57 @@ class ExamAdminServiceImplTest { ...@@ -356,10 +375,57 @@ class ExamAdminServiceImplTest {
ArgumentCaptor<ExamEntity> captor = ArgumentCaptor.forClass(ExamEntity.class); ArgumentCaptor<ExamEntity> captor = ArgumentCaptor.forClass(ExamEntity.class);
verify(examMapper).updateAdminExam(captor.capture()); verify(examMapper).updateAdminExam(captor.capture());
assertEquals(new BigDecimal("70.00"), captor.getValue().getPassScore()); assertEquals(new BigDecimal("70.00"), captor.getValue().getPassScore());
// SQL更新语句不包含以下字段;实体保持null可防止后续误改试卷、监考和总分快照。 // 理论试卷仍不变;实训规则、监考人员及总分快照允许在锁定条件下更新。
assertEquals(null, captor.getValue().getPaperId()); assertEquals(null, captor.getValue().getPaperId());
assertEquals(null, captor.getValue().getInvigilationId()); assertEquals("teacher-2", captor.getValue().getInvigilationId());
assertEquals(null, captor.getValue().getTotalScore()); assertEquals(6, captor.getValue().getPracticeRuleId());
assertEquals(new BigDecimal("100.00"), captor.getValue().getTotalScore());
}
@Test
void updateShouldRecalculateDefaultPassScoreWhenPracticeRuleChanges() {
ExamEntity existing = new ExamEntity();
existing.setId(7);
existing.setClassId(1);
existing.setType(ExamAdminConstants.TYPE_PRACTICE);
existing.setExamTime(Timestamp.valueOf("2099-09-18 09:00:00"));
existing.setChiefId("teacher-1");
existing.setInvigilationId("teacher-2");
existing.setPracticeRuleId(6);
existing.setTotalScore(new BigDecimal("100.00"));
existing.setPassScore(new BigDecimal("70.00"));
when(examMapper.selectAdminExamForUpdate(7)).thenReturn(existing);
when(examMapper.countAdminExamAttempts(7)).thenReturn(0);
when(examMapper.lockAdminExamClass(1)).thenReturn(1);
when(examMapper.selectAdminExamClass(1)).thenReturn(managedClass());
when(examMapper.selectAdminExamStudentIds(7))
.thenReturn(Arrays.asList("student-1", "student-2"));
mockValidRelationships();
when(practicePaperRuleService.requirePublishedDefinition(7, "污水一级"))
.thenReturn(practiceRule(7, "一级", "120.00"));
when(examMapper.countAdminExamDuplicateName(1, "切换实操规则", 7)).thenReturn(0);
when(examMapper.selectAdminExamConflicts(any(), eq(90), eq(1),
anyList(), anyList(), eq(7))).thenReturn(Collections.emptyList());
when(examMapper.updateAdminExam(any(ExamEntity.class))).thenReturn(1);
ExamAdminDetailVO detail = new ExamAdminDetailVO();
detail.setId(7);
detail.setExamTime(Timestamp.valueOf("2099-09-19 10:00:00"));
detail.setDuration(90);
when(examMapper.selectAdminExamDetail(7)).thenReturn(detail);
ExamAdminUpdateDTO request = new ExamAdminUpdateDTO();
request.setName("切换实操规则");
request.setType(ExamAdminConstants.TYPE_PRACTICE);
request.setExamTime(Timestamp.valueOf("2099-09-19 10:00:00"));
request.setDuration(90);
request.setPracticeRuleId(7);
service.update(7, request);
ArgumentCaptor<ExamEntity> captor = ArgumentCaptor.forClass(ExamEntity.class);
verify(examMapper).updateAdminExam(captor.capture());
assertEquals(new BigDecimal("120.00"), captor.getValue().getTotalScore());
assertEquals(new BigDecimal("72.00"), captor.getValue().getPassScore());
} }
@Test @Test
...@@ -370,7 +436,7 @@ class ExamAdminServiceImplTest { ...@@ -370,7 +436,7 @@ class ExamAdminServiceImplTest {
mockValidRelationships(); mockValidRelationships();
when(examMapper.countAdminExamDuplicateName(1, "测试考试", null)).thenReturn(0); when(examMapper.countAdminExamDuplicateName(1, "测试考试", null)).thenReturn(0);
when(examMapper.selectAdminExamConflicts(any(), eq(90), eq(1), when(examMapper.selectAdminExamConflicts(any(), eq(90), eq(1),
eq("teacher-1"), eq("teacher-2"), anyList(), eq(null))) anyList(), anyList(), eq(null)))
.thenReturn(Collections.emptyList()); .thenReturn(Collections.emptyList());
when(examMapper.insert(any(ExamEntity.class))).thenAnswer(invocation -> { when(examMapper.insert(any(ExamEntity.class))).thenAnswer(invocation -> {
ExamEntity entity = invocation.getArgument(0); ExamEntity entity = invocation.getArgument(0);
...@@ -464,10 +530,12 @@ class ExamAdminServiceImplTest { ...@@ -464,10 +530,12 @@ class ExamAdminServiceImplTest {
private void mockValidRelationships() { private void mockValidRelationships() {
when(examMapper.lockAdminExamUsers(anyList())) when(examMapper.lockAdminExamUsers(anyList()))
.thenReturn(Arrays.asList("student-1", "student-2", "teacher-1", "teacher-2")); .thenReturn(Arrays.asList("student-1", "student-2", "teacher-1", "teacher-2"));
when(examMapper.countAdminExamActiveTeacher("teacher-1")).thenReturn(1); when(examMapper.selectAdminExamActiveTeacherIds(anyList()))
when(examMapper.countAdminExamActiveTeacher("teacher-2")).thenReturn(1); .thenReturn(Arrays.asList("teacher-1", "teacher-2"));
when(examMapper.lockAdminExamStudents(eq(1), anyList())) when(examMapper.lockAdminExamStudents(eq(1), anyList()))
.thenReturn(Arrays.asList("student-1", "student-2")); .thenReturn(Arrays.asList("student-1", "student-2"));
lenient().when(examTeacherAssignmentMapper.insertBatch(anyList()))
.thenAnswer(invocation -> ((List<?>) invocation.getArgument(0)).size());
} }
private ExamAdminClassVO managedClass() { private ExamAdminClassVO managedClass() {
...@@ -496,4 +564,77 @@ class ExamAdminServiceImplTest { ...@@ -496,4 +564,77 @@ class ExamAdminServiceImplTest {
request.setStudentIds(Arrays.asList("student-1", "student-2")); request.setStudentIds(Arrays.asList("student-1", "student-2"));
return request; return request;
} }
@Test
void createShouldRejectExplicitEmptyTeacherArrayInsteadOfFallingBackToLegacyField() {
ExamAdminSaveDTO request = validCreateRequest();
request.setChiefExaminerIds(Collections.emptyList());
mockWritableClass();
BusinessException error = assertThrows(BusinessException.class,
() -> service.create(request));
assertEquals("首席考评员至少选择一人", error.getMessage());
verify(examMapper, never()).insert(any(ExamEntity.class));
}
@Test
void createShouldPersistMultiRoleTeachersAndDeduplicateRecipients() {
ExamAdminSaveDTO request = validCreateRequest();
request.setChiefExaminerIds(Arrays.asList("teacher-1", "teacher-3", "teacher-1"));
request.setInvigilatorIds(Arrays.asList("teacher-1", "teacher-2"));
mockWritableClass();
mockValidPaper();
when(examMapper.lockAdminExamUsers(anyList())).thenReturn(Arrays.asList(
"student-1", "student-2", "teacher-1", "teacher-2", "teacher-3"));
when(examMapper.selectAdminExamActiveTeacherIds(anyList()))
.thenReturn(Arrays.asList("teacher-1", "teacher-2", "teacher-3"));
when(examMapper.lockAdminExamStudents(eq(1), anyList()))
.thenReturn(Arrays.asList("student-1", "student-2"));
when(examMapper.countAdminExamDuplicateName(1, "测试考试", null)).thenReturn(0);
when(examMapper.selectAdminExamConflicts(any(), eq(90), eq(1),
anyList(), anyList(), eq(null))).thenReturn(Collections.emptyList());
when(examMapper.insert(any(ExamEntity.class))).thenAnswer(invocation -> {
ExamEntity entity = invocation.getArgument(0);
entity.setId(101);
return 1;
});
when(examTeacherAssignmentMapper.insertBatch(anyList()))
.thenAnswer(invocation -> ((List<?>) invocation.getArgument(0)).size());
when(examStudentService.saveBatch(anyList())).thenReturn(true);
ExamAdminDetailVO detail = new ExamAdminDetailVO();
detail.setId(101);
detail.setExamTime(request.getExamTime());
detail.setDuration(90);
when(examMapper.selectAdminExamDetail(101)).thenReturn(detail);
when(examMapper.selectAdminExamStudentIds(101)).thenReturn(request.getStudentIds());
service.create(request);
@SuppressWarnings("rawtypes")
ArgumentCaptor<List> assignmentCaptor = ArgumentCaptor.forClass(List.class);
verify(examTeacherAssignmentMapper).insertBatch(assignmentCaptor.capture());
List<?> assignments = assignmentCaptor.getValue();
assertEquals(4, assignments.size());
assertEquals("teacher-1", ((ExamTeacherAssignmentEntity) assignments.get(0)).getTeacherId());
assertEquals("CHIEF", ((ExamTeacherAssignmentEntity) assignments.get(0)).getRole());
assertEquals("teacher-1", ((ExamTeacherAssignmentEntity) assignments.get(2)).getTeacherId());
assertEquals("INVIGILATOR", ((ExamTeacherAssignmentEntity) assignments.get(2)).getRole());
ArgumentCaptor<NotificationPublishDTO> notificationCaptor =
ArgumentCaptor.forClass(NotificationPublishDTO.class);
verify(notificationPublisher, times(2))
.publish(notificationCaptor.capture(), eq(null), eq("管理员"));
assertEquals(Arrays.asList("teacher-1", "teacher-3", "teacher-2"),
notificationCaptor.getAllValues().get(0).getRecipientUserIds());
}
private PracticePaperRuleDefinitionEntity practiceRule(int id, String grade, String score) {
PracticePaperRuleDefinitionEntity rule = new PracticePaperRuleDefinitionEntity();
rule.setId(id);
rule.setLevelGrade(grade);
rule.setTotalScore(new BigDecimal(score));
rule.setStatus("PUBLISHED");
return rule;
}
} }
...@@ -23,7 +23,7 @@ import static org.mockito.Mockito.never; ...@@ -23,7 +23,7 @@ import static org.mockito.Mockito.never;
import static org.mockito.Mockito.verify; import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when; import static org.mockito.Mockito.when;
/** 最近考试接口返回实际考试类型并接入当前最新实操配分规则的回归测试。 */ /** 最近考试接口按考试保存的规则ID返回实操配分规则。 */
@ExtendWith(MockitoExtension.class) @ExtendWith(MockitoExtension.class)
class ExamStaudentServiceImplTest { class ExamStaudentServiceImplTest {
...@@ -42,21 +42,22 @@ class ExamStaudentServiceImplTest { ...@@ -42,21 +42,22 @@ class ExamStaudentServiceImplTest {
} }
@Test @Test
void recentShouldAttachLatestEffectiveRuleAndSameEventStudents() { void recentShouldAttachSelectedEffectiveRuleAndSameEventStudents() {
RecentExamStudentVO recent = new RecentExamStudentVO(); RecentExamStudentVO recent = new RecentExamStudentVO();
recent.setExamId(88); recent.setExamId(88);
recent.setLev("污水一级"); recent.setLev("污水一级");
recent.setType("理论考试"); recent.setType("实训");
recent.setPracticeRuleId(6);
PracticePaperRuleConfigurationVO configuration = new PracticePaperRuleConfigurationVO(); PracticePaperRuleConfigurationVO configuration = new PracticePaperRuleConfigurationVO();
configuration.setGrade("一级"); configuration.setGrade("一级");
when(mapper.recent("student-1")).thenReturn(recent); when(mapper.recent("student-1")).thenReturn(recent);
when(practicePaperRuleService.effectiveConfiguration("污水一级")) when(practicePaperRuleService.effectiveConfiguration(6))
.thenReturn(configuration); .thenReturn(configuration);
when(mapper.theSameEvent(88)).thenReturn(Collections.singletonList(new ExamStudentVO())); when(mapper.theSameEvent(88)).thenReturn(Collections.singletonList(new ExamStudentVO()));
RecentExamStudentVO result = service.recent("student-1"); RecentExamStudentVO result = service.recent("student-1");
assertEquals("理论考试", result.getType()); assertEquals("实训", result.getType());
assertEquals(configuration, result.getPracticeRule()); assertEquals(configuration, result.getPracticeRule());
assertEquals(1, result.getData().size()); assertEquals(1, result.getData().size());
} }
...@@ -66,7 +67,7 @@ class ExamStaudentServiceImplTest { ...@@ -66,7 +67,7 @@ class ExamStaudentServiceImplTest {
when(mapper.recent("student-2")).thenReturn(null); when(mapper.recent("student-2")).thenReturn(null);
assertNull(service.recent("student-2")); assertNull(service.recent("student-2"));
verify(practicePaperRuleService, never()).effectiveConfiguration(org.mockito.ArgumentMatchers.anyString()); verify(practicePaperRuleService, never()).effectiveConfiguration(org.mockito.ArgumentMatchers.anyInt());
verify(mapper, never()).theSameEvent(org.mockito.ArgumentMatchers.anyInt()); verify(mapper, never()).theSameEvent(org.mockito.ArgumentMatchers.anyInt());
} }
......
...@@ -4,8 +4,10 @@ import com.baomidou.mybatisplus.core.conditions.Wrapper; ...@@ -4,8 +4,10 @@ import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.platform.business.mapper.PracticePaperRuleMapper; import com.platform.business.mapper.PracticePaperRuleMapper;
import com.platform.business.model.dto.PracticePaperRuleItemDTO; import com.platform.business.model.dto.PracticePaperRuleItemDTO;
import com.platform.business.model.dto.PracticePaperRuleSaveDTO; import com.platform.business.model.dto.PracticePaperRuleSaveDTO;
import com.platform.business.model.entity.PracticePaperRuleDefinitionEntity;
import com.platform.business.model.entity.PracticePaperRuleDetailEntity;
import com.platform.business.model.entity.PracticePaperRuleEntity; import com.platform.business.model.entity.PracticePaperRuleEntity;
import com.platform.business.model.vo.PracticePaperRuleConfigurationVO; import com.platform.business.model.vo.PracticePaperRuleItemVO;
import com.platform.exception.BusinessException; import com.platform.exception.BusinessException;
import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test; import org.junit.jupiter.api.Test;
...@@ -21,13 +23,17 @@ import java.util.Arrays; ...@@ -21,13 +23,17 @@ import java.util.Arrays;
import java.util.List; import java.util.List;
import static org.junit.jupiter.api.Assertions.assertEquals; import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertThrows; import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.junit.jupiter.api.Assertions.assertTrue;
import static org.mockito.ArgumentMatchers.any; import static org.mockito.ArgumentMatchers.any;
import static org.mockito.ArgumentMatchers.anyList;
import static org.mockito.ArgumentMatchers.eq;
import static org.mockito.Mockito.never; import static org.mockito.Mockito.never;
import static org.mockito.Mockito.verify; import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when; import static org.mockito.Mockito.when;
/** 实操组卷规则的等级归一化、默认值、汇总和五题上限测试。 */ /** 具名实操试卷规则的完整保存、状态流转和删除保护测试。 */
@ExtendWith(MockitoExtension.class) @ExtendWith(MockitoExtension.class)
class PracticePaperRuleServiceImplTest { class PracticePaperRuleServiceImplTest {
...@@ -39,98 +45,226 @@ class PracticePaperRuleServiceImplTest { ...@@ -39,98 +45,226 @@ class PracticePaperRuleServiceImplTest {
@BeforeEach @BeforeEach
void setUp() { void setUp() {
service = new PracticePaperRuleServiceImpl(); service = new PracticePaperRuleServiceImpl();
// ServiceImpl通过protected baseMapper访问数据库,单元测试显式注入以隔离Spring容器。 ReflectionTestUtils.setField(service, "ruleMapper", mapper);
ReflectionTestUtils.setField(service, "baseMapper", mapper);
} }
@Test @Test
@SuppressWarnings("unchecked") @SuppressWarnings("unchecked")
void configurationShouldShareSuffixGradeAndCalculateTotals() { void createShouldNormalizeSuffixAndPersistCalculatedSnapshots() {
List<PracticePaperRuleEntity> rows = Arrays.asList( List<PracticePaperRuleEntity> catalog = Arrays.asList(
rule(1, "一级", "运行管理", "项目开发", 20, "20.00", 2), catalog(1, "一级", "运行管理", "项目开发"),
rule(2, "一级", "安全管理", "现场安全管理", 30, "10.50", 1)); catalog(2, "一级", "安全管理", "现场安全管理"));
when(mapper.selectList(any(Wrapper.class))).thenReturn(rows); when(mapper.selectList(any(Wrapper.class))).thenReturn(catalog);
when(mapper.countDuplicateDefinition("一级", "规则A", null)).thenReturn(0);
when(mapper.insertDefinition(any())).thenAnswer(invocation -> {
PracticePaperRuleDefinitionEntity entity = invocation.getArgument(0);
entity.setId(10);
return 1;
});
when(mapper.insertDefinitionDetails(anyList())).thenReturn(2);
when(mapper.selectDefinition(10)).thenReturn(definition(10, "规则A", "一级",
3, 70, "50.50", "DRAFT"));
when(mapper.selectDefinitionDetails(10)).thenReturn(new ArrayList<>());
PracticePaperRuleConfigurationVO result = service.configuration("污泥一级"); PracticePaperRuleSaveDTO request = request("规则A", "污泥一级", Arrays.asList(
item(1, 20, "20.00", 2), item(2, 30, "10.50", 1)));
service.create(request, "admin-1");
assertEquals("一级", result.getGrade()); ArgumentCaptor<PracticePaperRuleDefinitionEntity> header =
assertEquals("污泥一级", result.getLev()); ArgumentCaptor.forClass(PracticePaperRuleDefinitionEntity.class);
assertEquals(3, result.getTotalQuestionCount()); verify(mapper).insertDefinition(header.capture());
assertEquals(70, result.getTotalDuration()); assertEquals("一级", header.getValue().getLevelGrade());
assertEquals(new BigDecimal("50.50"), result.getTotalScore()); assertEquals(3, header.getValue().getQuestionCount());
assertEquals(2, result.getRuleList().size()); assertEquals(70, header.getValue().getTotalDuration());
assertEquals(new BigDecimal("50.50"), header.getValue().getTotalScore());
assertEquals("DRAFT", header.getValue().getStatus());
@SuppressWarnings("rawtypes")
ArgumentCaptor<List> details = ArgumentCaptor.forClass(List.class);
verify(mapper).insertDefinitionDetails(details.capture());
PracticePaperRuleDetailEntity first = (PracticePaperRuleDetailEntity) details.getValue().get(0);
assertEquals(10, first.getRuleId());
assertEquals(1, first.getCatalogId());
} }
@Test @Test
void saveShouldRejectMoreThanFiveQuestionsBeforeAnyUpdate() { @SuppressWarnings("unchecked")
List<PracticePaperRuleEntity> stored = new ArrayList<>(); void createShouldRejectMoreThanFiveQuestionsBeforeWriting() {
List<PracticePaperRuleEntity> catalog = new ArrayList<>();
List<PracticePaperRuleItemDTO> submitted = new ArrayList<>(); List<PracticePaperRuleItemDTO> submitted = new ArrayList<>();
for (int index = 1; index <= 6; index++) { for (int index = 1; index <= 6; index++) {
stored.add(rule(index, "五级", "项目" + index, "单元" + index, catalog.add(catalog(index, "五级", "项目" + index, "单元" + index));
20, "20.00", 0));
submitted.add(item(index, 20, "20.00", 1)); submitted.add(item(index, 20, "20.00", 1));
} }
when(mapper.selectByGradeForUpdate("五级")).thenReturn(stored); when(mapper.selectList(any(Wrapper.class))).thenReturn(catalog);
PracticePaperRuleSaveDTO request = new PracticePaperRuleSaveDTO();
request.setLev("污水五级");
request.setRuleList(submitted);
BusinessException error = assertThrows(BusinessException.class, BusinessException error = assertThrows(BusinessException.class,
() -> service.saveConfiguration(request, "admin-1")); () -> service.create(request("规则B", "污水五级", submitted), "admin-1"));
assertEquals("考核总题数必须在1到5题之间", error.getMessage()); assertEquals("考核总题数必须在1到5题之间", error.getMessage());
verify(mapper, never()).updateById(any(PracticePaperRuleEntity.class)); verify(mapper, never()).insertDefinition(any());
}
@Test
@SuppressWarnings("unchecked")
void createShouldRejectIncompleteCatalogBeforeWriting() {
when(mapper.selectList(any(Wrapper.class))).thenReturn(Arrays.asList(
catalog(1, "一级", "运行管理", "项目开发"),
catalog(2, "一级", "安全管理", "现场安全管理")));
BusinessException error = assertThrows(BusinessException.class,
() -> service.create(request("规则A", "一级",
Arrays.asList(item(1, 20, "20.00", 1))), "admin-1"));
assertEquals("必须提交一级全部2条标准类目规则", error.getMessage());
verify(mapper, never()).insertDefinition(any());
}
@Test
@SuppressWarnings("unchecked")
void createShouldRejectDuplicateNameWithinSameGrade() {
when(mapper.selectList(any(Wrapper.class))).thenReturn(Arrays.asList(
catalog(1, "一级", "运行管理", "项目开发")));
when(mapper.countDuplicateDefinition("一级", "重复规则", null)).thenReturn(1);
BusinessException error = assertThrows(BusinessException.class,
() -> service.create(request("重复规则", "污水一级",
Arrays.asList(item(1, 20, "20.00", 1))), "admin-1"));
assertEquals("同一职级下实操试卷规则名称已存在", error.getMessage());
verify(mapper, never()).insertDefinition(any());
}
@Test
void updateShouldRejectPublishedRule() {
when(mapper.lockDefinition(7)).thenReturn(
definition(7, "已发布", "三级", 5, 100, "100", "PUBLISHED"));
BusinessException error = assertThrows(BusinessException.class,
() -> service.update(7, new PracticePaperRuleSaveDTO(), "admin-1"));
assertEquals("已发布实操试卷规则不能编辑", error.getMessage());
verify(mapper, never()).updateDefinition(any());
}
@Test
@SuppressWarnings("unchecked")
void updateShouldReplaceCompleteDraftDetails() {
when(mapper.lockDefinition(7)).thenReturn(
definition(7, "旧名称", "一级", 1, 20, "20", "DRAFT"));
when(mapper.selectList(any(Wrapper.class))).thenReturn(Arrays.asList(
catalog(1, "一级", "运行管理", "项目开发")));
when(mapper.countDuplicateDefinition("一级", "新名称", 7)).thenReturn(0);
when(mapper.updateDefinition(any())).thenReturn(1);
when(mapper.insertDefinitionDetails(anyList())).thenReturn(1);
when(mapper.selectDefinition(7)).thenReturn(
definition(7, "新名称", "一级", 2, 50, "30", "DRAFT"));
when(mapper.selectDefinitionDetails(7)).thenReturn(new ArrayList<>());
service.update(7, request("新名称", "一级",
Arrays.asList(item(1, 25, "15.00", 2))), "admin-1");
verify(mapper).deleteDefinitionDetails(7);
verify(mapper).insertDefinitionDetails(anyList());
}
@Test
void publishShouldOnlyMoveDraftDirectlyToPublished() {
when(mapper.lockDefinition(9)).thenReturn(
definition(9, "待发布规则", "三级", 1, 20, "20", "DRAFT"));
PracticePaperRuleItemVO detail = new PracticePaperRuleItemVO();
detail.setCatalogId(1);
detail.setQuestionCount(1);
when(mapper.selectDefinitionDetails(9)).thenReturn(Arrays.asList(detail));
when(mapper.updateDefinitionStatus(eq(9), eq("PUBLISHED"), eq("admin-1"), any()))
.thenReturn(1);
when(mapper.selectDefinition(9)).thenReturn(
definition(9, "待发布规则", "三级", 1, 20, "20", "PUBLISHED"));
assertEquals("PUBLISHED", service.publish(9, "published", "admin-1").getStatus());
verify(mapper).updateDefinitionStatus(eq(9), eq("PUBLISHED"), eq("admin-1"), any());
} }
@Test @Test
void saveShouldApplyTwentyDefaultsAndOnlyUpdateMutableColumns() { void deleteShouldProtectUsedRuleRegardlessOfStatus() {
PracticePaperRuleEntity stored = rule(7, "三级", "运行管理", "工艺运行分析", when(mapper.lockDefinition(8)).thenReturn(
15, "15.00", 0); definition(8, "规则C", "二级", 5, 100, "100", "DRAFT"));
when(mapper.selectByGradeForUpdate("三级")).thenReturn(Arrays.asList(stored)); when(mapper.countDefinitionUsage(8)).thenReturn(1);
when(mapper.updateById(any(PracticePaperRuleEntity.class))).thenReturn(1);
PracticePaperRuleItemDTO submitted = new PracticePaperRuleItemDTO(); BusinessException error = assertThrows(BusinessException.class, () -> service.delete(8));
submitted.setId(7);
submitted.setQuestionCount(1); assertEquals("实操试卷规则已被考试使用,不能删除", error.getMessage());
verify(mapper, never()).deleteDefinition(8);
}
@Test
void deleteShouldAllowUnusedPublishedRule() {
when(mapper.lockDefinition(8)).thenReturn(
definition(8, "规则C", "二级", 5, 100, "100", "PUBLISHED"));
when(mapper.countDefinitionUsage(8)).thenReturn(0);
when(mapper.deleteDefinition(8)).thenReturn(1);
assertTrue(service.delete(8));
verify(mapper).deleteDefinitionDetails(8);
verify(mapper).deleteDefinition(8);
}
@Test
void pageShouldExposePublishedUnusedRuleAsDeletableButNotEditable() {
com.platform.business.model.vo.PracticePaperRuleConfigurationVO row =
new com.platform.business.model.vo.PracticePaperRuleConfigurationVO();
row.setStatus("PUBLISHED");
row.setUsageCount(0);
when(mapper.pageDefinitions(any())).thenReturn(Arrays.asList(row));
com.platform.business.model.vo.PracticePaperRuleConfigurationVO result =
service.queryPage(null).get(0);
assertFalse(result.getEditable());
assertFalse(result.getPublishable());
assertEquals(true, result.getDeletable());
assertEquals("已发布", result.getStatusLabel());
}
private PracticePaperRuleSaveDTO request(String name, String level,
List<PracticePaperRuleItemDTO> items) {
PracticePaperRuleSaveDTO request = new PracticePaperRuleSaveDTO(); PracticePaperRuleSaveDTO request = new PracticePaperRuleSaveDTO();
request.setLev("污水三级"); request.setName(name);
request.setRuleList(Arrays.asList(submitted)); request.setLev(level);
request.setRuleList(items);
PracticePaperRuleConfigurationVO result = service.saveConfiguration(request, "admin-2"); return request;
}
assertEquals(20, result.getTotalDuration());
assertEquals(new BigDecimal("20.00"), result.getTotalScore()); private PracticePaperRuleItemDTO item(int catalogId, int duration, String score, int count) {
ArgumentCaptor<PracticePaperRuleEntity> captor = PracticePaperRuleItemDTO item = new PracticePaperRuleItemDTO();
ArgumentCaptor.forClass(PracticePaperRuleEntity.class); item.setCatalogId(catalogId);
verify(mapper).updateById(captor.capture()); item.setAssessmentDuration(duration);
PracticePaperRuleEntity update = captor.getValue(); item.setScore(new BigDecimal(score));
assertEquals(7, update.getId()); item.setQuestionCount(count);
assertEquals(20, update.getAssessmentDuration()); return item;
assertEquals(new BigDecimal("20.00"), update.getScore()); }
assertEquals(1, update.getQuestionCount());
// 标准类目不进入更新对象,确保客户端永远不能修改Excel类目。 private PracticePaperRuleEntity catalog(int id, String grade, String project, String unit) {
assertEquals(null, update.getProjectName());
assertEquals(null, update.getUnitContent());
}
private PracticePaperRuleEntity rule(int id, String grade, String project, String unit,
int duration, String score, int count) {
PracticePaperRuleEntity row = new PracticePaperRuleEntity(); PracticePaperRuleEntity row = new PracticePaperRuleEntity();
row.setId(id); row.setId(id);
row.setLevelGrade(grade); row.setLevelGrade(grade);
row.setProjectName(project); row.setProjectName(project);
row.setUnitContent(unit); row.setUnitContent(unit);
row.setAssessmentDuration(duration);
row.setScore(new BigDecimal(score));
row.setQuestionCount(count);
return row; return row;
} }
private PracticePaperRuleItemDTO item(int id, int duration, String score, int count) { private PracticePaperRuleDefinitionEntity definition(int id, String name, String grade,
PracticePaperRuleItemDTO item = new PracticePaperRuleItemDTO(); int count, int duration, String score,
item.setId(id); String status) {
item.setAssessmentDuration(duration); PracticePaperRuleDefinitionEntity row = new PracticePaperRuleDefinitionEntity();
item.setScore(new BigDecimal(score)); row.setId(id);
item.setQuestionCount(count); row.setName(name);
return item; row.setLevelGrade(grade);
row.setQuestionCount(count);
row.setTotalDuration(duration);
row.setTotalScore(new BigDecimal(score));
row.setStatus(status);
return row;
} }
} }
...@@ -5,6 +5,7 @@ import org.junit.jupiter.api.Test; ...@@ -5,6 +5,7 @@ import org.junit.jupiter.api.Test;
import java.io.InputStream; import java.io.InputStream;
import java.nio.charset.StandardCharsets; import java.nio.charset.StandardCharsets;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertFalse; import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertTrue; import static org.junit.jupiter.api.Assertions.assertTrue;
...@@ -33,5 +34,33 @@ class PracticePaperRuleMigrationSqlTest { ...@@ -33,5 +34,33 @@ class PracticePaperRuleMigrationSqlTest {
assertTrue(sql.contains("'设施设备操作'")); assertTrue(sql.contains("'设施设备操作'"));
assertFalse(sql.contains("'设施设备擦欧总'")); assertFalse(sql.contains("'设施设备擦欧总'"));
assertTrue(sql.contains("'新业态项目安全管理'")); assertTrue(sql.contains("'新业态项目安全管理'"));
// 五个等级的目录条数必须与迁移约定完全一致,明细迁移会为每个目录生成一条记录。
assertEquals(6, occurrences(sql, "('五级',"));
assertEquals(6, occurrences(sql, "('四级',"));
assertEquals(6, occurrences(sql, "('三级',"));
assertEquals(9, occurrences(sql, "('二级',"));
assertEquals(9, occurrences(sql, "('一级',"));
assertTrue(sql.contains("create table if not exists practice_paper_rule_definition"));
assertTrue(sql.contains("create table if not exists practice_paper_rule_detail"));
assertTrue(sql.contains("create table if not exists exam_teacher_assignment"));
assertTrue(sql.contains("add column practice_rule_id int null"));
assertTrue(sql.contains("'published', null, now(), null, now()"));
assertTrue(sql.contains("insert ignore into practice_paper_rule_definition"));
assertTrue(sql.contains("insert ignore into practice_paper_rule_detail"));
assertTrue(sql.contains("select definition.id, catalog.id"));
assertTrue(sql.contains("insert ignore into exam_teacher_assignment"));
assertTrue(sql.contains("where e.type = '实训' and e.practice_rule_id is null"));
assertTrue(sql.contains("information_schema.columns"));
assertTrue(sql.contains("information_schema.statistics"));
}
private int occurrences(String source, String token) {
int count = 0;
int offset = 0;
while ((offset = source.indexOf(token, offset)) >= 0) {
count++;
offset += token.length();
}
return count;
} }
} }
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment