-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
부모/자식 관계를 만들면서 entity <-> dto 간 변환 로직, 댓글 서비스에서 댓글과 대댓글을 저장하고 삭제하는 방법, 댓글과 대댓글을 db 데이터로부터 변환할 때 서로 계층 구조를 만들고 각각 정렬하는 과정을 구현
- Loading branch information
Showing
7 changed files
with
304 additions
and
48 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
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
181 changes: 181 additions & 0 deletions
181
src/test/java/com/fastcampus/projectboard/dto/response/ArticleWithCommentsResponseTest.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,181 @@ | ||
package com.fastcampus.projectboard.dto.response; | ||
|
||
import com.fastcampus.projectboard.dto.ArticleCommentDto; | ||
import com.fastcampus.projectboard.dto.ArticleWithCommentsDto; | ||
import com.fastcampus.projectboard.dto.HashtagDto; | ||
import com.fastcampus.projectboard.dto.UserAccountDto; | ||
import org.junit.jupiter.api.DisplayName; | ||
import org.junit.jupiter.api.Test; | ||
|
||
import java.time.LocalDateTime; | ||
import java.util.Iterator; | ||
import java.util.Set; | ||
|
||
import static org.assertj.core.api.Assertions.assertThat; | ||
|
||
@DisplayName("DTO - 댓글을 포함한 게시글 응답 테스트") | ||
class ArticleWithCommentsResponseTest { | ||
|
||
@DisplayName("자식 댓글이 없는 게시글 + 댓글 dto를 api 응답으로 변환할 때, 댓글을 시간 내림차순 + ID 오름차순으로 정리한다.") | ||
@Test | ||
void givenArticleWithCommentsDtoWithoutChildComments_whenMapping_thenOrganizesCommentsWithCertainOrder() { | ||
// Given | ||
LocalDateTime now = LocalDateTime.now(); | ||
Set<ArticleCommentDto> articleCommentDtos = Set.of( | ||
createArticleCommentDto(1L, null, now), | ||
createArticleCommentDto(2L, null, now.plusDays(1L)), | ||
createArticleCommentDto(3L, null, now.plusDays(3L)), | ||
createArticleCommentDto(4L, null, now), | ||
createArticleCommentDto(5L, null, now.plusDays(5L)), | ||
createArticleCommentDto(6L, null, now.plusDays(4L)), | ||
createArticleCommentDto(7L, null, now.plusDays(2L)), | ||
createArticleCommentDto(8L, null, now.plusDays(7L)) | ||
); | ||
ArticleWithCommentsDto input = createArticleWithCommentsDto(articleCommentDtos); | ||
|
||
// When | ||
ArticleWithCommentsResponse actual = ArticleWithCommentsResponse.from(input); | ||
|
||
// Then | ||
assertThat(actual.articleCommentsResponse()) | ||
.containsExactly( | ||
createArticleCommentResponse(8L, null, now.plusDays(7L)), | ||
createArticleCommentResponse(5L, null, now.plusDays(5L)), | ||
createArticleCommentResponse(6L, null, now.plusDays(4L)), | ||
createArticleCommentResponse(3L, null, now.plusDays(3L)), | ||
createArticleCommentResponse(7L, null, now.plusDays(2L)), | ||
createArticleCommentResponse(2L, null, now.plusDays(1L)), | ||
createArticleCommentResponse(1L, null, now), | ||
createArticleCommentResponse(4L, null, now) | ||
); | ||
} | ||
|
||
@DisplayName("게시글 + 댓글 dto를 api 응답으로 변환할 때, 댓글 부모 자식 관계를 각각의 규칙으로 정렬하여 정리한다.") | ||
@Test | ||
void givenArticleWithCommentsDto_whenMapping_thenOrganizesParentAndChildCommentsWithCertainOrders() { | ||
// Given | ||
LocalDateTime now = LocalDateTime.now(); | ||
Set<ArticleCommentDto> articleCommentDtos = Set.of( | ||
createArticleCommentDto(1L, null, now), | ||
createArticleCommentDto(2L, 1L, now.plusDays(1L)), | ||
createArticleCommentDto(3L, 1L, now.plusDays(3L)), | ||
createArticleCommentDto(4L, 1L, now), | ||
createArticleCommentDto(5L, null, now.plusDays(5L)), | ||
createArticleCommentDto(6L, null, now.plusDays(4L)), | ||
createArticleCommentDto(7L, 6L, now.plusDays(2L)), | ||
createArticleCommentDto(8L, 6L, now.plusDays(7L)) | ||
); | ||
ArticleWithCommentsDto input = createArticleWithCommentsDto(articleCommentDtos); | ||
|
||
// When | ||
ArticleWithCommentsResponse actual = ArticleWithCommentsResponse.from(input); | ||
|
||
// Then | ||
assertThat(actual.articleCommentsResponse()) | ||
.containsExactly( | ||
createArticleCommentResponse(5L, null, now.plusDays(5)), | ||
createArticleCommentResponse(6L, null, now.plusDays(4)), | ||
createArticleCommentResponse(1L, null, now) | ||
) | ||
.flatExtracting(ArticleCommentResponse::childComments) | ||
.containsExactly( | ||
createArticleCommentResponse(7L, 6L, now.plusDays(2L)), | ||
createArticleCommentResponse(8L, 6L, now.plusDays(7L)), | ||
createArticleCommentResponse(4L, 1L, now), | ||
createArticleCommentResponse(2L, 1L, now.plusDays(1L)), | ||
createArticleCommentResponse(3L, 1L, now.plusDays(3L)) | ||
); | ||
} | ||
|
||
@DisplayName("게시글 + 댓글 dto를 api 응답으로 변환할 때, 부모 자식 관계 깊이(depth)는 제한이 없다.") | ||
@Test | ||
void givenArticleWithCommentsDto_whenMapping_thenOrganizesParentAndChildCommentsWithoutDepthLimit() { | ||
// Given | ||
LocalDateTime now = LocalDateTime.now(); | ||
Set<ArticleCommentDto> articleCommentDtos = Set.of( | ||
createArticleCommentDto(1L, null, now), | ||
createArticleCommentDto(2L, 1L, now.plusDays(1L)), | ||
createArticleCommentDto(3L, 2L, now.plusDays(2L)), | ||
createArticleCommentDto(4L, 3L, now.plusDays(3L)), | ||
createArticleCommentDto(5L, 4L, now.plusDays(4L)), | ||
createArticleCommentDto(6L, 5L, now.plusDays(5L)), | ||
createArticleCommentDto(7L, 6L, now.plusDays(6L)), | ||
createArticleCommentDto(8L, 7L, now.plusDays(7L)) | ||
); | ||
ArticleWithCommentsDto input = createArticleWithCommentsDto(articleCommentDtos); | ||
|
||
// When | ||
ArticleWithCommentsResponse actual = ArticleWithCommentsResponse.from(input); | ||
|
||
// Then | ||
Iterator<ArticleCommentResponse> iterator = actual.articleCommentsResponse().iterator(); | ||
long i = 1L; | ||
while (iterator.hasNext()) { | ||
ArticleCommentResponse articleCommentResponse = iterator.next(); | ||
assertThat(articleCommentResponse) | ||
.hasFieldOrPropertyWithValue("id", i) | ||
.hasFieldOrPropertyWithValue("parentCommentId", i == 1L ? null : i - 1L) | ||
.hasFieldOrPropertyWithValue("createdAt", now.plusDays(i - 1L)); | ||
|
||
iterator = articleCommentResponse.childComments().iterator(); | ||
i++; | ||
} | ||
} | ||
|
||
|
||
private ArticleWithCommentsDto createArticleWithCommentsDto(Set<ArticleCommentDto> articleCommentDtos) { | ||
return ArticleWithCommentsDto.of( | ||
1L, | ||
createUserAccountDto(), | ||
articleCommentDtos, | ||
"title", | ||
"content", | ||
Set.of(HashtagDto.of("java")), | ||
LocalDateTime.now(), | ||
"uno", | ||
LocalDateTime.now(), | ||
"uno" | ||
); | ||
} | ||
|
||
private UserAccountDto createUserAccountDto() { | ||
return UserAccountDto.of( | ||
"uno", | ||
"password", | ||
"uno@mail.com", | ||
"Uno", | ||
"This is memo", | ||
LocalDateTime.now(), | ||
"uno", | ||
LocalDateTime.now(), | ||
"uno" | ||
); | ||
} | ||
|
||
private ArticleCommentDto createArticleCommentDto(Long id, Long parentCommentId, LocalDateTime createdAt) { | ||
return ArticleCommentDto.of( | ||
id, | ||
1L, | ||
createUserAccountDto(), | ||
parentCommentId, | ||
"test comment " + id, | ||
createdAt, | ||
"uno", | ||
createdAt, | ||
"uno" | ||
); | ||
} | ||
|
||
private ArticleCommentResponse createArticleCommentResponse(Long id, Long parentCommentId, LocalDateTime createdAt) { | ||
return ArticleCommentResponse.of( | ||
id, | ||
"test comment " + id, | ||
createdAt, | ||
"uno@mail.com", | ||
"Uno", | ||
"uno", | ||
parentCommentId | ||
); | ||
} | ||
|
||
} |
Oops, something went wrong.