-
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 #173 from DayByQuest/test/service
[Test] 서비스에 대한 테스트를 작성한다
- Loading branch information
Showing
21 changed files
with
507 additions
and
16 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
10 changes: 10 additions & 0 deletions
10
src/main/java/daybyquest/global/error/exception/NotExistCommentException.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,10 @@ | ||
package daybyquest.global.error.exception; | ||
|
||
import daybyquest.global.error.ExceptionCode; | ||
|
||
public class NotExistCommentException extends CustomException { | ||
|
||
public NotExistCommentException() { | ||
super(ExceptionCode.NOT_EXIST_COMMENT); | ||
} | ||
} |
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
39 changes: 39 additions & 0 deletions
39
src/test/java/daybyquest/badge/application/GetMyBadgesServiceTest.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,39 @@ | ||
package daybyquest.badge.application; | ||
|
||
import static daybyquest.support.fixture.BadgeFixtures.BADGE_1; | ||
import static daybyquest.support.fixture.BadgeFixtures.BADGE_2; | ||
import static daybyquest.support.fixture.BadgeFixtures.BADGE_3; | ||
import static org.assertj.core.api.Assertions.assertThat; | ||
|
||
import daybyquest.badge.dto.response.BadgeResponse; | ||
import daybyquest.badge.dto.response.PageBadgesResponse; | ||
import daybyquest.support.test.ServiceTest; | ||
import java.util.List; | ||
import org.junit.jupiter.api.Test; | ||
import org.springframework.beans.factory.annotation.Autowired; | ||
|
||
public class GetMyBadgesServiceTest extends ServiceTest { | ||
|
||
@Autowired | ||
private GetMyBadgesService getMyBadgesService; | ||
|
||
@Test | ||
void 내_뱃지_목록을_조회한다() { | ||
// given | ||
final Long aliceId = ALICE를_저장한다(); | ||
final Long badge1Id = badges.save(BADGE_1.생성()).getId(); | ||
final Long badge2Id = badges.save(BADGE_2.생성()).getId(); | ||
badges.save(BADGE_3.생성()); | ||
ownings.saveByUserIdAndBadgeId(aliceId, badge1Id); | ||
ownings.saveByUserIdAndBadgeId(aliceId, badge2Id); | ||
|
||
final List<Long> expected = List.of(badge1Id, badge2Id); | ||
|
||
// when | ||
final PageBadgesResponse response = getMyBadgesService.invoke(aliceId, 시간_페이지()); | ||
final List<Long> actual = response.badges().stream().map(BadgeResponse::id).toList(); | ||
|
||
// then | ||
assertThat(actual).containsExactlyInAnyOrderElementsOf(expected); | ||
} | ||
} |
42 changes: 42 additions & 0 deletions
42
src/test/java/daybyquest/badge/application/SaveBadgeServiceTest.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,42 @@ | ||
package daybyquest.badge.application; | ||
|
||
import static daybyquest.support.fixture.BadgeFixtures.BADGE_1; | ||
import static org.assertj.core.api.Assertions.assertThat; | ||
import static org.junit.jupiter.api.Assertions.assertAll; | ||
|
||
import daybyquest.badge.domain.Badge; | ||
import daybyquest.badge.dto.request.SaveBadgeRequest; | ||
import daybyquest.support.test.ServiceTest; | ||
import org.junit.jupiter.api.Test; | ||
import org.springframework.beans.factory.annotation.Autowired; | ||
import org.springframework.test.util.ReflectionTestUtils; | ||
import org.springframework.web.multipart.MultipartFile; | ||
|
||
public class SaveBadgeServiceTest extends ServiceTest { | ||
|
||
@Autowired | ||
private SaveBadgeService saveBadgeService; | ||
|
||
@Test | ||
void 뱃지를_생성한다() { | ||
// given | ||
final SaveBadgeRequest request = 뱃지_생성_요청(BADGE_1.생성()); | ||
final MultipartFile file = 사진_파일(BADGE_1.imageIdentifier); | ||
|
||
// when | ||
final Long badgeId = saveBadgeService.invoke(request, file); | ||
final Badge actual = badges.getById(badgeId); | ||
|
||
// then | ||
assertAll(() -> { | ||
assertThat(actual.getName()).isEqualTo(BADGE_1.name); | ||
assertThat(actual.getImage().getIdentifier()).isEqualTo(BADGE_1.imageIdentifier); | ||
}); | ||
} | ||
|
||
private SaveBadgeRequest 뱃지_생성_요청(final Badge badge) { | ||
final SaveBadgeRequest request = new SaveBadgeRequest(); | ||
ReflectionTestUtils.setField(request, "name", badge.getName()); | ||
return request; | ||
} | ||
} |
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
39 changes: 39 additions & 0 deletions
39
src/test/java/daybyquest/comment/application/GetCommentsByPostIdServiceTest.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,39 @@ | ||
package daybyquest.comment.application; | ||
|
||
import static daybyquest.support.fixture.CommentFixtures.댓글_1; | ||
import static org.assertj.core.api.Assertions.assertThat; | ||
|
||
import daybyquest.comment.dto.response.CommentResponse; | ||
import daybyquest.comment.dto.response.PageCommentsResponse; | ||
import daybyquest.support.fixture.PostFixtures; | ||
import daybyquest.support.test.ServiceTest; | ||
import java.util.List; | ||
import org.junit.jupiter.api.Test; | ||
import org.springframework.beans.factory.annotation.Autowired; | ||
|
||
public class GetCommentsByPostIdServiceTest extends ServiceTest { | ||
|
||
@Autowired | ||
private GetCommentsByPostIdService getCommentsByPostIdService; | ||
|
||
@Test | ||
void 게시물_ID를_통해_댓글_목록을_조회한다() { | ||
// given | ||
final Long aliceId = ALICE를_저장한다(); | ||
final Long bobId = BOB을_저장한다(); | ||
|
||
final Long post1Id = posts.save(PostFixtures.POST_1.생성(aliceId)).getId(); | ||
final Long post2Id = posts.save(PostFixtures.POST_2.생성(aliceId)).getId(); | ||
|
||
final List<Long> expected = List.of(comments.save(댓글_1.생성(post1Id, aliceId)).getId(), | ||
comments.save(댓글_1.생성(post1Id, bobId)).getId()); | ||
comments.save(댓글_1.생성(post2Id, aliceId)); | ||
|
||
// when | ||
final PageCommentsResponse response = getCommentsByPostIdService.invoke(aliceId, post1Id, 페이지()); | ||
final List<Long> actual = response.comments().stream().map(CommentResponse::id).toList(); | ||
|
||
// then | ||
assertThat(actual).containsExactlyInAnyOrderElementsOf(expected); | ||
} | ||
} |
44 changes: 44 additions & 0 deletions
44
src/test/java/daybyquest/comment/application/SaveCommentServiceTest.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,44 @@ | ||
package daybyquest.comment.application; | ||
|
||
import static daybyquest.support.fixture.CommentFixtures.댓글_1; | ||
import static org.assertj.core.api.Assertions.assertThat; | ||
import static org.junit.jupiter.api.Assertions.assertAll; | ||
|
||
import daybyquest.comment.domain.Comment; | ||
import daybyquest.comment.dto.request.SaveCommentRequest; | ||
import daybyquest.support.fixture.PostFixtures; | ||
import daybyquest.support.test.ServiceTest; | ||
import org.junit.jupiter.api.Test; | ||
import org.springframework.beans.factory.annotation.Autowired; | ||
import org.springframework.test.util.ReflectionTestUtils; | ||
|
||
public class SaveCommentServiceTest extends ServiceTest { | ||
|
||
@Autowired | ||
private SaveCommentService saveCommentService; | ||
|
||
@Test | ||
void 댓글을_저장한다() { | ||
// given | ||
final Long aliceId = ALICE를_저장한다(); | ||
final Long postId = posts.save(PostFixtures.POST_1.생성(aliceId)).getId(); | ||
final SaveCommentRequest request = 댓글_생성_요청(댓글_1.생성(postId, aliceId)); | ||
|
||
// when | ||
final Long commentId = saveCommentService.invoke(aliceId, postId, request); | ||
final Comment comment = comments.getById(commentId); | ||
|
||
// then | ||
assertAll(() -> { | ||
assertThat(comment.getUserId()).isEqualTo(aliceId); | ||
assertThat(comment.getPostId()).isEqualTo(postId); | ||
assertThat(comment.getContent()).isEqualTo(댓글_1.content); | ||
}); | ||
} | ||
|
||
private SaveCommentRequest 댓글_생성_요청(final Comment comment) { | ||
final SaveCommentRequest request = new SaveCommentRequest(); | ||
ReflectionTestUtils.setField(request, "content", comment.getContent()); | ||
return request; | ||
} | ||
} |
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
27 changes: 27 additions & 0 deletions
27
src/test/java/daybyquest/dislike/application/DeletePostDislikeServiceTest.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,27 @@ | ||
package daybyquest.dislike.application; | ||
|
||
import static daybyquest.support.fixture.PostFixtures.POST_1; | ||
import static org.assertj.core.api.Assertions.assertThatCode; | ||
|
||
import daybyquest.dislike.domain.PostDislike; | ||
import daybyquest.support.test.ServiceTest; | ||
import org.junit.jupiter.api.Test; | ||
import org.springframework.beans.factory.annotation.Autowired; | ||
|
||
public class DeletePostDislikeServiceTest extends ServiceTest { | ||
|
||
@Autowired | ||
private DeletePostDislikeService deletePostDislikeService; | ||
|
||
@Test | ||
void 관심_없음을_취소한다() { | ||
// given | ||
final Long aliceId = ALICE를_저장한다(); | ||
final Long postId = posts.save(POST_1.생성(aliceId)).getId(); | ||
postDislikes.save(new PostDislike(aliceId, postId)); | ||
|
||
// when & then | ||
assertThatCode(() -> deletePostDislikeService.invoke(aliceId, postId)) | ||
.doesNotThrowAnyException(); | ||
} | ||
} |
59 changes: 59 additions & 0 deletions
59
src/test/java/daybyquest/dislike/application/SavePostDislikeServiceTest.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,59 @@ | ||
package daybyquest.dislike.application; | ||
|
||
import static daybyquest.global.error.ExceptionCode.ALREADY_DISLIKED_POST; | ||
import static daybyquest.support.fixture.PostFixtures.POST_1; | ||
import static org.assertj.core.api.Assertions.assertThatCode; | ||
import static org.assertj.core.api.Assertions.assertThatThrownBy; | ||
|
||
import daybyquest.dislike.domain.PostDislike; | ||
import daybyquest.global.error.exception.InvalidDomainException; | ||
import daybyquest.like.domain.PostLike; | ||
import daybyquest.support.test.ServiceTest; | ||
import org.junit.jupiter.api.Test; | ||
import org.springframework.beans.factory.annotation.Autowired; | ||
|
||
public class SavePostDislikeServiceTest extends ServiceTest { | ||
|
||
@Autowired | ||
private SavePostDislikeService savePostDislikeService; | ||
|
||
@Test | ||
void 관심_없음을_저장한다() { | ||
// given | ||
final Long aliceId = ALICE를_저장한다(); | ||
final Long postId = posts.save(POST_1.생성(aliceId)).getId(); | ||
|
||
// when | ||
savePostDislikeService.invoke(aliceId, postId); | ||
|
||
// then | ||
assertThatThrownBy(() -> postDislikes.save(new PostDislike(aliceId, postId))) | ||
.isInstanceOf(InvalidDomainException.class) | ||
.hasMessageContaining(ALREADY_DISLIKED_POST.getMessage()); | ||
} | ||
|
||
@Test | ||
void 이미_관심_없음을_눌렀으면_저장할_수_없다() { | ||
// given | ||
final Long aliceId = ALICE를_저장한다(); | ||
final Long postId = posts.save(POST_1.생성(aliceId)).getId(); | ||
postDislikes.save(new PostDislike(aliceId, postId)); | ||
|
||
// when & then | ||
assertThatThrownBy(() -> savePostDislikeService.invoke(aliceId, postId)) | ||
.isInstanceOf(InvalidDomainException.class) | ||
.hasMessageContaining(ALREADY_DISLIKED_POST.getMessage()); | ||
} | ||
|
||
@Test | ||
void 관심_없음을_누르면_좋아요가_취소된다() { | ||
// given | ||
final Long aliceId = ALICE를_저장한다(); | ||
final Long postId = posts.save(POST_1.생성(aliceId)).getId(); | ||
postLikes.save(new PostLike(aliceId, postId)); | ||
|
||
// when & then | ||
assertThatCode(() -> savePostDislikeService.invoke(aliceId, postId)) | ||
.doesNotThrowAnyException(); | ||
} | ||
} |
Oops, something went wrong.