-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #22 from Findy-org/feat/즐겨찾기-이름-변경
[FINDY-33] feat: 즐겨찾기 이름 변경
- Loading branch information
Showing
10 changed files
with
240 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
8 changes: 8 additions & 0 deletions
8
src/main/java/org/findy/findy_be/bookmark/application/update/UpdateBookmark.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
package org.findy.findy_be.bookmark.application.update; | ||
|
||
import org.findy.findy_be.bookmark.dto.request.UpdateBookmarkRequest; | ||
|
||
public interface UpdateBookmark { | ||
|
||
void invoke(String userId, Long bookmarkId, UpdateBookmarkRequest request); | ||
} |
35 changes: 35 additions & 0 deletions
35
src/main/java/org/findy/findy_be/bookmark/application/update/UpdateBookmarkService.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
package org.findy.findy_be.bookmark.application.update; | ||
|
||
import static org.findy.findy_be.common.exception.ErrorCode.*; | ||
|
||
import org.findy.findy_be.bookmark.domain.Bookmark; | ||
import org.findy.findy_be.bookmark.dto.request.UpdateBookmarkRequest; | ||
import org.findy.findy_be.bookmark.repository.BookmarkRepository; | ||
import org.springframework.stereotype.Service; | ||
import org.springframework.transaction.annotation.Transactional; | ||
|
||
import jakarta.persistence.EntityNotFoundException; | ||
import lombok.RequiredArgsConstructor; | ||
|
||
@Service | ||
@Transactional | ||
@RequiredArgsConstructor | ||
public class UpdateBookmarkService implements UpdateBookmark { | ||
|
||
private final BookmarkRepository bookmarkRepository; | ||
|
||
@Override | ||
public void invoke(final String userId, final Long bookmarkId, final UpdateBookmarkRequest request) { | ||
Bookmark bookmark = bookmarkRepository.findById(bookmarkId) | ||
.orElseThrow( | ||
() -> new EntityNotFoundException(String.format(NOT_FOUND_BOOKMARK_BY_ID.getMessage(), bookmarkId))); | ||
validateBookmarkUser(userId, bookmark); | ||
bookmark.updateName(request.name()); | ||
} | ||
|
||
private static void validateBookmarkUser(final String userId, final Bookmark bookmark) { | ||
if (!bookmark.getUser().getUserId().equals(userId)) { | ||
throw new IllegalArgumentException(FORBIDDEN_BOOKMARK_ACCESS.getMessage()); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
13 changes: 13 additions & 0 deletions
13
src/main/java/org/findy/findy_be/bookmark/dto/request/UpdateBookmarkRequest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
package org.findy.findy_be.bookmark.dto.request; | ||
|
||
import io.swagger.v3.oas.annotations.media.Schema; | ||
import jakarta.validation.constraints.NotNull; | ||
|
||
@Schema(description = "즐겨찾기 이름 변경 DTO") | ||
public record UpdateBookmarkRequest( | ||
|
||
@NotNull(message = "북마크 이름은 비어있을 수 없습니다.") | ||
@Schema(description = "북마크 이름", example = "서촌") | ||
String name | ||
) { | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
105 changes: 105 additions & 0 deletions
105
src/test/java/org/findy/findy_be/bookmark/application/update/UpdateBookmarkServiceTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,105 @@ | ||
package org.findy.findy_be.bookmark.application.update; | ||
|
||
import static org.assertj.core.api.Assertions.*; | ||
import static org.findy.findy_be.common.exception.ErrorCode.*; | ||
import static org.mockito.Mockito.*; | ||
|
||
import java.time.LocalDateTime; | ||
import java.util.Optional; | ||
|
||
import org.findy.findy_be.auth.oauth.domain.SocialProviderType; | ||
import org.findy.findy_be.bookmark.domain.Bookmark; | ||
import org.findy.findy_be.bookmark.dto.request.UpdateBookmarkRequest; | ||
import org.findy.findy_be.bookmark.repository.BookmarkRepository; | ||
import org.findy.findy_be.common.MockTest; | ||
import org.findy.findy_be.user.domain.RoleType; | ||
import org.findy.findy_be.user.domain.User; | ||
import org.junit.jupiter.api.BeforeEach; | ||
import org.junit.jupiter.api.DisplayName; | ||
import org.junit.jupiter.api.Test; | ||
import org.mockito.InjectMocks; | ||
import org.mockito.Mock; | ||
import org.mockito.MockitoAnnotations; | ||
|
||
import jakarta.persistence.EntityNotFoundException; | ||
|
||
class UpdateBookmarkServiceTest extends MockTest { | ||
|
||
@Mock | ||
private BookmarkRepository bookmarkRepository; | ||
|
||
@InjectMocks | ||
private UpdateBookmarkService updateBookmarkService; | ||
|
||
private User user; | ||
private Bookmark bookmark; | ||
|
||
@BeforeEach | ||
void setUp() { | ||
MockitoAnnotations.openMocks(this); | ||
user = User.create("N49sfgdahdKz_fp-223424er1N3D6kd", "나경호", "[email protected]", "Y", | ||
"https://github.com/account", SocialProviderType.NAVER, RoleType.USER, | ||
LocalDateTime.now(), LocalDateTime.now()); | ||
bookmark = Bookmark.createCustomType("서촌", user); | ||
} | ||
|
||
@DisplayName("[성공] 즐겨찾기 이름 업데이트") | ||
@Test | ||
void 즐겨찾기_이름_업데이트() throws Exception { | ||
// given | ||
Long customBookmarkId = 1L; | ||
UpdateBookmarkRequest request = new UpdateBookmarkRequest("새로운 이름"); | ||
when(bookmarkRepository.findById(customBookmarkId)).thenReturn(Optional.of(bookmark)); | ||
|
||
// when | ||
updateBookmarkService.invoke(user.getUserId(), customBookmarkId, request); | ||
|
||
// then | ||
assertThat(bookmark.getName()).isEqualTo("새로운 이름"); | ||
verify(bookmarkRepository, times(1)).findById(customBookmarkId); | ||
} | ||
|
||
@DisplayName("[실패] 존재하지 않는 즐겨찾기 ID일 경우 EntityNotFoundException 발생") | ||
@Test | ||
void 존재하지_않는_즐겨찾기_ID일_경우() { | ||
// given | ||
Long nonExistentBookmarkId = 999L; | ||
UpdateBookmarkRequest request = new UpdateBookmarkRequest("새로운 이름"); | ||
when(bookmarkRepository.findById(nonExistentBookmarkId)).thenReturn(Optional.empty()); | ||
|
||
// when & then | ||
assertThatThrownBy(() -> updateBookmarkService.invoke(user.getUserId(), nonExistentBookmarkId, request)) | ||
.isInstanceOf(EntityNotFoundException.class) | ||
.hasMessageContaining(String.format(NOT_FOUND_BOOKMARK_BY_ID.getMessage(), nonExistentBookmarkId)); | ||
} | ||
|
||
@DisplayName("[실패] 다른 사용자의 즐겨찾기를 업데이트하려고 할 경우 IllegalArgumentException 발생") | ||
@Test | ||
void 다른_사용자의_즐겨찾기를_업데이트하려고_할_경우() { | ||
// given | ||
Long customBookmarkId = 1L; | ||
UpdateBookmarkRequest request = new UpdateBookmarkRequest("새로운 이름"); | ||
when(bookmarkRepository.findById(customBookmarkId)).thenReturn(Optional.of(bookmark)); | ||
|
||
// when & then | ||
assertThatThrownBy(() -> updateBookmarkService.invoke("다른사용자ID", customBookmarkId, request)) | ||
.isInstanceOf(IllegalArgumentException.class) | ||
.hasMessageContaining(FORBIDDEN_BOOKMARK_ACCESS.getMessage()); | ||
} | ||
|
||
@DisplayName("[성공] 동일한 이름 요청 시 업데이트하지 않음") | ||
@Test | ||
void 동일한_이름_요청_시() throws Exception { | ||
// given | ||
Long customBookmarkId = 1L; | ||
UpdateBookmarkRequest request = new UpdateBookmarkRequest("서촌"); // 기존 이름과 동일 | ||
when(bookmarkRepository.findById(customBookmarkId)).thenReturn(Optional.of(bookmark)); | ||
|
||
// when | ||
updateBookmarkService.invoke(user.getUserId(), customBookmarkId, request); | ||
|
||
// then | ||
assertThat(bookmark.getName()).isEqualTo("서촌"); // 이름이 변경되지 않음 | ||
verify(bookmarkRepository, times(1)).findById(customBookmarkId); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters