Skip to content

Commit

Permalink
feature: 북마크, 가게 ID 조회 API 테스트 코드 작성
Browse files Browse the repository at this point in the history
  • Loading branch information
gogo1414 committed Nov 25, 2024
1 parent 1f11d9e commit e7d7f29
Show file tree
Hide file tree
Showing 5 changed files with 114 additions and 19 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -78,10 +78,10 @@ public ResponseEntity<Page<BookmarkResponseDto>> getBookmarks(
content = {@Content(schema = @Schema(implementation = ErrorResponse.class))}),
@ApiResponse(responseCode = "404", description = "회원 조회 실패",
content = {@Content(schema = @Schema(implementation = ErrorResponse.class))})})
public ResponseEntity<List<BookmarkStoreIdsResponseDto>> getBookmarksId(
public ResponseEntity<List<BookmarkStoreIdsResponseDto>> getBookmarkStoreIds(
@LoginMember @Parameter(hidden = true) AuthMember authMember)
{
List<BookmarkStoreIdsResponseDto> bookmarkResponse = bookmarkService.getBookmarkAndStoreIds(authMember);
List<BookmarkStoreIdsResponseDto> bookmarkResponse = bookmarkService.getBookmarkStoreIds(authMember);
return ResponseEntity.ok(bookmarkResponse);
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,14 +1,5 @@
package matal.bookmark.dto.response;

import matal.bookmark.domain.Bookmark;

public record BookmarkStoreIdsResponseDto(Long bookmarkId,
Long storeId) {

public static BookmarkStoreIdsResponseDto from(Bookmark bookmark) {
return new BookmarkStoreIdsResponseDto(
bookmark.getBookmarkId(),
bookmark.getStore().getStoreId()
);
}
}
2 changes: 1 addition & 1 deletion src/main/java/matal/bookmark/service/BookmarkService.java
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ public Page<BookmarkResponseDto> getBookmarks(AuthMember authMember, int page) {
}

@Transactional(readOnly = true)
public List<BookmarkStoreIdsResponseDto> getBookmarkAndStoreIds(AuthMember authMember) {
public List<BookmarkStoreIdsResponseDto> getBookmarkStoreIds(AuthMember authMember) {
validateMember(authMember.memberId());
return bookmarkRepository.findBookmarkStoreIdsByMemberId(authMember.memberId());
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
import com.fasterxml.jackson.databind.ObjectMapper;
import java.util.List;
import matal.bookmark.dto.response.BookmarkResponseDto;
import matal.bookmark.dto.response.BookmarkStoreIdsResponseDto;
import matal.bookmark.service.BookmarkService;
import matal.global.exception.AuthException;
import matal.global.exception.NotFoundException;
Expand Down Expand Up @@ -101,8 +102,6 @@ void beforeEach() {
)
)
);

Pageable pageable = PageRequest.of(0, 10);
}

@Test
Expand Down Expand Up @@ -160,7 +159,7 @@ void testCreateBookmarkFailure2() throws Exception {
}

@Test
@DisplayName("북마크 리스트 조회 성공 테스트")
@DisplayName("북마크 페이지 조회 성공 테스트")
void testGetBookmarksSuccess() throws Exception {
// given
Pageable pageable = PageRequest.of(0, 10);
Expand All @@ -182,7 +181,7 @@ void testGetBookmarksSuccess() throws Exception {
}

@Test
@DisplayName("북마크 리스트 조회 시 세션 정보 없음으로 인한 실패 테스트")
@DisplayName("북마크 페이지 조회 시 세션 정보 없음으로 인한 실패 테스트")
void testGetBookmarksFailure() throws Exception {
// given

Expand All @@ -198,7 +197,7 @@ void testGetBookmarksFailure() throws Exception {
}

@Test
@DisplayName("북마크 리스트 조회 시 회원 정보를 찾지 못해 발생하는 실패 테스트")
@DisplayName("북마크 페이지 조회 시 회원 정보를 찾지 못해 발생하는 실패 테스트")
void testGetBookmarksFailure2() throws Exception {
// given

Expand All @@ -214,6 +213,68 @@ void testGetBookmarksFailure2() throws Exception {
.andDo(print());
}

@Test
@DisplayName("북마크, 가게 아이디 조회 성공 테스트")
void testGetBookmarkStoreIdsSuccess() throws Exception {
// given
List<BookmarkStoreIdsResponseDto> bookmarks = List.of(
new BookmarkStoreIdsResponseDto(
1L, 1L
),
new BookmarkStoreIdsResponseDto(
1L, 2L
)
);


// when
when(bookmarkService.getBookmarkStoreIds(mockMember)).thenReturn(bookmarks);

// then
mockMvc.perform(get("/api/bookmarks/ids")
.contentType("application/json")
.session(mockSession))
.andExpect(status().isOk())
.andExpect(jsonPath("$[0].bookmarkId").value(1L))
.andExpect(jsonPath("$[0].storeId").value(1L))
.andExpect(jsonPath("$[1].bookmarkId").value(1L))
.andExpect(jsonPath("$[1].storeId").value(2L))
.andDo(print());
}

@Test
@DisplayName("북마크 가게 아이디 조회 시 세션 정보 없음으로 인한 실패 테스트")
void testGetBookmarkStoreIdsFailure() throws Exception {
// given

// when
when(bookmarkService.getBookmarkStoreIds(mockMember))
.thenThrow(new AuthException(ResponseCode.SESSION_VALUE_EXCEPTION));

// then
mockMvc.perform(get("/api/bookmarks/ids")
.contentType("application/json"))
.andExpect(status().isUnauthorized())
.andDo(print());
}

@Test
@DisplayName("북마크 가게 아이디 조회 시 회원 정보를 찾지 못해 발생하는 실패 테스트")
void testGetBookmarkStoreIdsFailure2() throws Exception {
// given

// when
doThrow(new NotFoundException(ResponseCode.STORE_NOT_FOUND_ID))
.when(bookmarkService).getBookmarkStoreIds(mockMember);

// then
mockMvc.perform(get("/api/bookmarks/ids")
.contentType("application/json")
.session(mockSession))
.andExpect(status().isNotFound())
.andDo(print());
}

@Test
@DisplayName("북마크 삭제 성공 테스트")
void testDelteBookmarkSuccess() throws Exception {
Expand Down
47 changes: 45 additions & 2 deletions src/test/java/matal/bookmark/service/BookmarkServiceTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
import matal.bookmark.domain.Bookmark;
import matal.bookmark.domain.repository.BookmarkRepository;
import matal.bookmark.dto.response.BookmarkResponseDto;
import matal.bookmark.dto.response.BookmarkStoreIdsResponseDto;
import matal.global.exception.NotFoundException;
import matal.global.exception.ResponseCode;
import matal.member.domain.Member;
Expand Down Expand Up @@ -154,7 +155,7 @@ void testSaveBookmarkFailure3() {
}

@Test
@DisplayName("북마크 리스트 조회 성공 테스트")
@DisplayName("북마크 페이지 조회 성공 테스트")
void testGetBookmarksSuccess() {
// given
List<Bookmark> bookmarks = List.of(
Expand Down Expand Up @@ -184,7 +185,7 @@ void testGetBookmarksSuccess() {
}

@Test
@DisplayName("북마크 리스트 조회 시 회원 정보 없음으로 인한 실패 테스트")
@DisplayName("북마크 페이지 조회 시 회원 정보 없음으로 인한 실패 테스트")
void testGetBookmarksFailure() {
// given

Expand All @@ -196,6 +197,48 @@ void testGetBookmarksFailure() {
assertThrows(NotFoundException.class, () -> bookmarkService.getBookmarks(mockMember, 0));
}

@Test
@DisplayName("북마크 가게 아이디 조회 성공 테스트")
void testGetBookmarkStoresIdsSuccess() {
// given
List<BookmarkStoreIdsResponseDto> bookmarks = List.of(
new BookmarkStoreIdsResponseDto(
1L,
1L
),
new BookmarkStoreIdsResponseDto(
1L,
2L
)
);


// when
when(memberRepository.findById(any())).thenReturn(Optional.of(member));
when(bookmarkRepository.findBookmarkStoreIdsByMemberId(any())).thenReturn(bookmarks);

// then
List<BookmarkStoreIdsResponseDto> response = bookmarkService.getBookmarkStoreIds(mockMember);
assertThat(response.get(0).bookmarkId()).isEqualTo(bookmarks.get(0).bookmarkId());
assertThat(response.get(1).bookmarkId()).isEqualTo(bookmarks.get(1).bookmarkId());
assertThat(response.get(0).storeId()).isEqualTo(bookmarks.get(0).storeId());
assertThat(response.get(1).storeId()).isEqualTo(bookmarks.get(1).storeId());

}

@Test
@DisplayName("북마크 가게 아이디 조회 시 회원 정보 없음으로 인한 실패 테스트")
void testGetBookmarkStoresIdsFailure() {
// given

// when
when(memberRepository.findById(any()))
.thenThrow(new NotFoundException(ResponseCode.MEMBER_NOT_FOUND_ID));

// then
assertThrows(NotFoundException.class, () -> bookmarkService.getBookmarkStoreIds(mockMember));
}

@Test
@DisplayName("북마크 삭제 성공 테스트")
void testDeleteBookmarkSuccess() {
Expand Down

0 comments on commit e7d7f29

Please sign in to comment.