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 @@ -9,7 +9,7 @@
public interface SubjectRepository extends JpaRepository<Subject, Long> {
Optional<Subject> findByCode(String code);

Optional<Subject> findByName(String name);
Optional<Subject> findFirstByNameOrderByIdAsc(String name);

boolean existsByCode(String code);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ public class CompletedSubjectService {
public CompletedSubjectResponse add(Long userId, CompletedSubjectAddRequest request) {
Subject subject =
subjectRepository
.findByName(request.getSubjectName())
.findFirstByNameOrderByIdAsc(request.getSubjectName())
.orElseThrow(
() ->
new BusinessException(ErrorCode.VALIDATION_FAILED, "subjectName ์ด ์กด์žฌํ•˜์ง€ ์•Š์Šต๋‹ˆ๋‹ค."));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ class CompletedSubjectServiceTest {
void add_success_returnsSavedRow() { // ์ •์ƒ: ์ €์žฅ ํ›„ id/๊ณผ๋ชฉ/ํ•™๋…„/ํ•™๊ธฐ ๋ฐ˜ํ™˜
Subject subject = mock(Subject.class);
given(subject.getId()).willReturn(142L);
given(subjectRepository.findByName("์ž๋ฃŒ๊ตฌ์กฐ")).willReturn(Optional.of(subject));
given(subjectRepository.findFirstByNameOrderByIdAsc("์ž๋ฃŒ๊ตฌ์กฐ")).willReturn(Optional.of(subject));
given(userCompletedSubjectRepository.existsByUserIdAndSubjectId(1L, 142L)).willReturn(false);
given(userRepository.getReferenceById(1L)).willReturn(user);

Expand All @@ -64,7 +64,7 @@ void add_success_returnsSavedRow() { // ์ •์ƒ: ์ €์žฅ ํ›„ id/๊ณผ๋ชฉ/ํ•™๋…„/ํ•™

@Test
void add_subjectNotFound_throws422() { // ์—†๋Š” ๊ณผ๋ชฉ ์ด๋ฆ„์ด๋ฉด 422
given(subjectRepository.findByName("์—†๋Š”๊ณผ๋ชฉ")).willReturn(Optional.empty());
given(subjectRepository.findFirstByNameOrderByIdAsc("์—†๋Š”๊ณผ๋ชฉ")).willReturn(Optional.empty());

assertThatThrownBy(() -> service.add(1L, addRequest("์—†๋Š”๊ณผ๋ชฉ", 1, 1)))
.isInstanceOf(BusinessException.class)
Expand All @@ -75,11 +75,30 @@ void add_subjectNotFound_throws422() { // ์—†๋Š” ๊ณผ๋ชฉ ์ด๋ฆ„์ด๋ฉด 422
verify(eventPublisher, never()).publishEvent(any());
}

@Test
void add_duplicateName_picksFirstSubject() { // ๋™๋ช… ๊ณผ๋ชฉ์ด ์—ฌ๋Ÿฌ ๊ฐœ์—ฌ๋„ ์ฒซ ๋ฒˆ์งธ ๊ณผ๋ชฉ์œผ๋กœ ์ •์ƒ ์ถ”๊ฐ€
Subject first = mock(Subject.class);
given(first.getId()).willReturn(100L);
// ์นดํƒˆ๋กœ๊ทธ์— ๋™๋ช… ๊ณผ๋ชฉ์ด ์—ฌ๋Ÿฌ ๊ฑด ์žˆ์–ด๋„ repository ๊ฐ€ id ์˜ค๋ฆ„์ฐจ์ˆœ ์ฒซ ๊ณผ๋ชฉ๋งŒ ๋ฐ˜ํ™˜
given(subjectRepository.findFirstByNameOrderByIdAsc("๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค")).willReturn(Optional.of(first));
given(userCompletedSubjectRepository.existsByUserIdAndSubjectId(1L, 100L)).willReturn(false);
given(userRepository.getReferenceById(1L)).willReturn(user);

UserCompletedSubject saved = UserCompletedSubject.of(user, first, 1, 1);
ReflectionTestUtils.setField(saved, "id", 9L);
given(userCompletedSubjectRepository.save(any())).willReturn(saved);

CompletedSubjectResponse result = service.add(1L, addRequest("๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค", 1, 1));

assertThat(result.subjectId()).isEqualTo(100L);
verify(eventPublisher).publishEvent(new CompletedSubjectsChangedEvent(1L));
}

@Test
void add_duplicate_throws409() { // ์ด๋ฏธ ์ด์ˆ˜ํ•œ ๊ณผ๋ชฉ์ด๋ฉด 409
Subject subject = mock(Subject.class);
given(subject.getId()).willReturn(142L);
given(subjectRepository.findByName("์ž๋ฃŒ๊ตฌ์กฐ")).willReturn(Optional.of(subject));
given(subjectRepository.findFirstByNameOrderByIdAsc("์ž๋ฃŒ๊ตฌ์กฐ")).willReturn(Optional.of(subject));
given(userCompletedSubjectRepository.existsByUserIdAndSubjectId(1L, 142L)).willReturn(true);

assertThatThrownBy(() -> service.add(1L, addRequest("์ž๋ฃŒ๊ตฌ์กฐ", 2, 1)))
Expand Down
Loading