Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,9 @@ List<Long> findIdsByStatusAndProcessingStartedAtBefore(
Pageable pageable
);

@Query("select a.user.userId from Analysis a where a.id = :analysisId")
Optional<Long> findUserIdById(@Param("analysisId") Long analysisId);

@Query("""
select a from Analysis a
where a.playing.id in :playingIds and a.status = :status
Expand Down
9 changes: 4 additions & 5 deletions src/main/java/com/mr/domain/mentor/service/MentorService.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.mr.domain.analysis.entity.Analysis;
import com.mr.domain.analysis.exception.AnalysisErrorStatus;
import com.mr.domain.analysis.repository.AnalysisRepository;
import com.mr.domain.mentor.dto.res.MentorMessageHistoryResponseDTO;
Expand All @@ -28,9 +27,9 @@ public class MentorService {
private final ObjectMapper objectMapper;

public MentorMessageHistoryResponseDTO getMessageHistory(Long userId, Long analysisId) {
Analysis analysis = analysisRepository.findById(analysisId)
Long ownerId = analysisRepository.findUserIdById(analysisId)
.orElseThrow(() -> new GeneralException(AnalysisErrorStatus.ANALYSIS_NOT_FOUND));
validateOwner(analysis, userId);
validateOwner(ownerId, userId);

List<MentorMessageHistoryResponseDTO.Message> messages = mentorMessageRepository
.findByMentorChatSessionAnalysisIdOrderByCreatedAtAscIdAsc(analysisId)
Expand All @@ -44,8 +43,8 @@ public MentorMessageHistoryResponseDTO getMessageHistory(Long userId, Long analy
return new MentorMessageHistoryResponseDTO(analysisId, messages);
}

private void validateOwner(Analysis analysis, Long userId) {
if (!Objects.equals(analysis.getUser().getUserId(), userId)) {
private void validateOwner(Long ownerId, Long userId) {
if (!Objects.equals(ownerId, userId)) {
throw new GeneralException(MentorErrorStatus.MENTOR_ACCESS_DENIED);
}
}
Expand Down
24 changes: 5 additions & 19 deletions src/test/java/com/mr/domain/mentor/service/MentorServiceTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,15 +8,13 @@
import static org.mockito.Mockito.verifyNoInteractions;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.mr.domain.analysis.entity.Analysis;
import com.mr.domain.analysis.exception.AnalysisErrorStatus;
import com.mr.domain.analysis.repository.AnalysisRepository;
import com.mr.domain.mentor.dto.res.MentorMessageHistoryResponseDTO;
import com.mr.domain.mentor.entity.MentorMessage;
import com.mr.domain.mentor.entity.enums.MessageRole;
import com.mr.domain.mentor.exception.MentorErrorStatus;
import com.mr.domain.mentor.repository.MentorMessageRepository;
import com.mr.domain.user.entity.User;
import com.mr.global.apipayload.exception.GeneralException;
import java.time.LocalDateTime;
import java.util.List;
Expand Down Expand Up @@ -51,8 +49,7 @@ void setUp() {
@Test
@DisplayName("대화 세션이 없으면 빈 메시지 목록 반환")
void getMessageHistory_noSession_returnsEmptyMessages() {
Analysis analysis = analysisOwnedBy(1L);
given(analysisRepository.findById(10L)).willReturn(Optional.of(analysis));
given(analysisRepository.findUserIdById(10L)).willReturn(Optional.of(1L));
given(mentorMessageRepository.findByMentorChatSessionAnalysisIdOrderByCreatedAtAscIdAsc(10L))
.willReturn(List.of());

Expand All @@ -65,7 +62,6 @@ void getMessageHistory_noSession_returnsEmptyMessages() {
@Test
@DisplayName("대화 메시지와 판단 근거 JSON 반환")
void getMessageHistory_success() {
Analysis analysis = analysisOwnedBy(1L);
MentorMessage message = mock(MentorMessage.class);
LocalDateTime createdAt = LocalDateTime.of(2026, 7, 1, 14, 35);

Expand All @@ -76,7 +72,7 @@ void getMessageHistory_success() {
""");
given(message.getContent()).willReturn("텐션음을 자주 사용했어요.");
given(message.getCreatedAt()).willReturn(createdAt);
given(analysisRepository.findById(10L)).willReturn(Optional.of(analysis));
given(analysisRepository.findUserIdById(10L)).willReturn(Optional.of(1L));
given(mentorMessageRepository.findByMentorChatSessionAnalysisIdOrderByCreatedAtAscIdAsc(10L))
.willReturn(List.of(message));

Expand All @@ -92,8 +88,7 @@ void getMessageHistory_success() {
@Test
@DisplayName("다른 사용자의 분석이면 접근 거부")
void getMessageHistory_otherOwner_throwsAccessDenied() {
Analysis analysis = analysisOwnedBy(1L);
given(analysisRepository.findById(10L)).willReturn(Optional.of(analysis));
given(analysisRepository.findUserIdById(10L)).willReturn(Optional.of(1L));

assertThatThrownBy(() -> mentorService.getMessageHistory(2L, 10L))
.isInstanceOf(GeneralException.class)
Expand All @@ -105,7 +100,7 @@ void getMessageHistory_otherOwner_throwsAccessDenied() {
@Test
@DisplayName("분석이 없으면 분석 미존재 오류")
void getMessageHistory_analysisNotFound_throwsNotFound() {
given(analysisRepository.findById(10L)).willReturn(Optional.empty());
given(analysisRepository.findUserIdById(10L)).willReturn(Optional.empty());

assertThatThrownBy(() -> mentorService.getMessageHistory(1L, 10L))
.isInstanceOf(GeneralException.class)
Expand All @@ -115,7 +110,6 @@ void getMessageHistory_analysisNotFound_throwsNotFound() {
@Test
@DisplayName("판단 근거 JSON 하나가 손상되어도 나머지 대화 내역 반환")
void getMessageHistory_invalidReferences_returnsNullAndContinues() {
Analysis analysis = analysisOwnedBy(1L);
MentorMessage invalidMessage = mock(MentorMessage.class);
MentorMessage validMessage = mock(MentorMessage.class);

Expand All @@ -126,7 +120,7 @@ void getMessageHistory_invalidReferences_returnsNullAndContinues() {
given(validMessage.getId()).willReturn(31L);
given(validMessage.getRole()).willReturn(MessageRole.USER);
given(validMessage.getContent()).willReturn("다음 질문");
given(analysisRepository.findById(10L)).willReturn(Optional.of(analysis));
given(analysisRepository.findUserIdById(10L)).willReturn(Optional.of(1L));
given(mentorMessageRepository.findByMentorChatSessionAnalysisIdOrderByCreatedAtAscIdAsc(10L))
.willReturn(List.of(invalidMessage, validMessage));

Expand All @@ -138,12 +132,4 @@ void getMessageHistory_invalidReferences_returnsNullAndContinues() {
assertThat(response.messages().get(1).mentorMessageId()).isEqualTo(31L);
assertThat(response.messages().get(1).content()).isEqualTo("다음 질문");
}

private Analysis analysisOwnedBy(Long userId) {
User user = mock(User.class);
Analysis analysis = mock(Analysis.class);
given(user.getUserId()).willReturn(userId);
given(analysis.getUser()).willReturn(user);
return analysis;
}
}
Loading