|
|
|
@ -18,11 +18,13 @@ import org.mockito.junit.jupiter.MockitoExtension; |
|
|
|
import org.springframework.data.domain.Page; |
|
|
|
import org.springframework.data.domain.PageImpl; |
|
|
|
|
|
|
|
import java.time.LocalDateTime; |
|
|
|
import java.util.*; |
|
|
|
|
|
|
|
import static org.junit.jupiter.api.Assertions.*; |
|
|
|
import static org.mockito.ArgumentMatchers.any; |
|
|
|
import static org.mockito.ArgumentMatchers.eq; |
|
|
|
import static org.mockito.ArgumentMatchers.isNull; |
|
|
|
import static org.mockito.Mockito.*; |
|
|
|
|
|
|
|
@ExtendWith(MockitoExtension.class) |
|
|
|
@ -51,6 +53,7 @@ class JobReviewServiceTest { |
|
|
|
.rating(4).content("不错的岗位").status("PUBLISHED").build(); |
|
|
|
// 维度相关默认空数据(旧用例不传 dimensionScores;lenient 避免部分用例未用到)
|
|
|
|
lenient().when(dimensionScoreRepository.findByReviewId(any())).thenReturn(List.of()); |
|
|
|
lenient().when(dimensionScoreRepository.findByReviewIdIn(any())).thenReturn(List.of()); |
|
|
|
lenient().when(dimensionScoreRepository.aggregateByDimension(any())).thenReturn(List.of()); |
|
|
|
// 企业回复默认无(toView 会查询)
|
|
|
|
lenient().when(replyRepository.findByReviewIdAndModerationStatus(any(), any())) |
|
|
|
@ -145,12 +148,64 @@ class JobReviewServiceTest { |
|
|
|
|
|
|
|
@Test void listReviews_shouldReturnPublishedOnly() { |
|
|
|
when(jobService.getJob(JOB_ID)).thenReturn(Job.builder().id(JOB_ID).build()); |
|
|
|
when(reviewRepository.findByJobIdAndStatusAndModerationStatusInOrderByCreatedAtDesc(eq(JOB_ID), eq("PUBLISHED"), any(), any())) |
|
|
|
when(reviewRepository.findPublicReviews(eq(JOB_ID), eq("PUBLISHED"), any(), |
|
|
|
isNull(), isNull(), isNull(), eq(false), any())) |
|
|
|
.thenReturn(new PageImpl<>(List.of(review))); |
|
|
|
var r = reviewService.listReviews(JOB_ID, 0, 20); |
|
|
|
assertEquals(1, r.getTotalElements()); |
|
|
|
} |
|
|
|
|
|
|
|
// ── 列表筛选(好评/中评/差评/未回复/时间区间) ────────────
|
|
|
|
|
|
|
|
@Test void listReviews_withFilters_shouldPassToRepository() { |
|
|
|
when(jobService.getJob(JOB_ID)).thenReturn(Job.builder().id(JOB_ID).build()); |
|
|
|
LocalDateTime from = LocalDateTime.of(2026, 1, 1, 0, 0); |
|
|
|
LocalDateTime to = LocalDateTime.of(2026, 6, 1, 0, 0); |
|
|
|
when(reviewRepository.findPublicReviews(eq(JOB_ID), eq("PUBLISHED"), any(), |
|
|
|
eq("GOOD"), eq(from), eq(to), eq(true), any())) |
|
|
|
.thenReturn(new PageImpl<>(List.of(review))); |
|
|
|
var r = reviewService.listReviews(JOB_ID, "GOOD", from, to, true, 0, 20); |
|
|
|
assertEquals(1, r.getTotalElements()); |
|
|
|
verify(reviewRepository).findPublicReviews(eq(JOB_ID), eq("PUBLISHED"), any(), |
|
|
|
eq("GOOD"), eq(from), eq(to), eq(true), any()); |
|
|
|
} |
|
|
|
|
|
|
|
@Test void listReviews_invalidRatingType_shouldReturn400() { |
|
|
|
when(jobService.getJob(JOB_ID)).thenReturn(Job.builder().id(JOB_ID).build()); |
|
|
|
BusinessException ex = assertThrows(BusinessException.class, |
|
|
|
() -> reviewService.listReviews(JOB_ID, "EXCELLENT", |
|
|
|
LocalDateTime.now(), null, false, 0, 20)); |
|
|
|
assertEquals(ErrorCode.BAD_REQUEST.getCode(), ex.getErrorCodeInt()); |
|
|
|
} |
|
|
|
|
|
|
|
@Test void listReviews_fromAfterTo_shouldReturn400() { |
|
|
|
when(jobService.getJob(JOB_ID)).thenReturn(Job.builder().id(JOB_ID).build()); |
|
|
|
LocalDateTime from = LocalDateTime.of(2026, 6, 1, 0, 0); |
|
|
|
LocalDateTime to = LocalDateTime.of(2026, 1, 1, 0, 0); |
|
|
|
BusinessException ex = assertThrows(BusinessException.class, |
|
|
|
() -> reviewService.listReviews(JOB_ID, null, from, to, false, 0, 20)); |
|
|
|
assertEquals(ErrorCode.BAD_REQUEST.getCode(), ex.getErrorCodeInt()); |
|
|
|
} |
|
|
|
|
|
|
|
@Test void listReviews_onlyFromNoTo_shouldBeAllowed() { |
|
|
|
when(jobService.getJob(JOB_ID)).thenReturn(Job.builder().id(JOB_ID).build()); |
|
|
|
LocalDateTime from = LocalDateTime.of(2026, 1, 1, 0, 0); |
|
|
|
when(reviewRepository.findPublicReviews(eq(JOB_ID), eq("PUBLISHED"), any(), |
|
|
|
isNull(), eq(from), isNull(), eq(false), any())) |
|
|
|
.thenReturn(new PageImpl<>(List.of(review))); |
|
|
|
var r = reviewService.listReviews(JOB_ID, null, from, null, false, 0, 20); |
|
|
|
assertEquals(1, r.getTotalElements()); |
|
|
|
} |
|
|
|
|
|
|
|
@Test void listReviews_nullFilters_shouldDefaultFalse() { |
|
|
|
when(jobService.getJob(JOB_ID)).thenReturn(Job.builder().id(JOB_ID).build()); |
|
|
|
when(reviewRepository.findPublicReviews(eq(JOB_ID), eq("PUBLISHED"), any(), |
|
|
|
isNull(), isNull(), isNull(), eq(false), any())) |
|
|
|
.thenReturn(new PageImpl<>(List.of(review))); |
|
|
|
var r = reviewService.listReviews(JOB_ID, null, null, null, false, 0, 20); |
|
|
|
assertEquals(1, r.getTotalElements()); |
|
|
|
} |
|
|
|
|
|
|
|
@Test void updateReview_shouldSucceed() { |
|
|
|
mockStudentRole(STUDENT_ID); |
|
|
|
when(reviewRepository.findById(REVIEW_ID)).thenReturn(Optional.of(review)); |
|
|
|
|