Skip to content

Commit 255ba8e

Browse files
committed
test: 게시물 저장 서비스를 테스트한다
1 parent 732a9a2 commit 255ba8e

File tree

1 file changed

+100
-0
lines changed

1 file changed

+100
-0
lines changed
Lines changed: 100 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,100 @@
1+
package daybyquest.post.application;
2+
3+
import static daybyquest.global.error.ExceptionCode.NOT_ACCEPTED_QUEST;
4+
import static daybyquest.support.fixture.PostFixtures.POST_1;
5+
import static daybyquest.support.fixture.QuestFixtures.QUEST_1;
6+
import static org.assertj.core.api.Assertions.assertThat;
7+
import static org.assertj.core.api.Assertions.assertThatThrownBy;
8+
import static org.junit.jupiter.api.Assertions.assertAll;
9+
import static org.mockito.ArgumentMatchers.any;
10+
import static org.mockito.ArgumentMatchers.eq;
11+
import static org.mockito.BDDMockito.then;
12+
13+
import daybyquest.global.error.exception.InvalidDomainException;
14+
import daybyquest.image.domain.Image;
15+
import daybyquest.post.domain.Post;
16+
import daybyquest.post.dto.request.SavePostRequest;
17+
import daybyquest.support.fixture.BadgeFixtures;
18+
import daybyquest.support.test.ServiceTest;
19+
import java.util.List;
20+
import org.junit.jupiter.api.Test;
21+
import org.springframework.beans.factory.annotation.Autowired;
22+
import org.springframework.test.util.ReflectionTestUtils;
23+
import org.springframework.web.multipart.MultipartFile;
24+
25+
public class SavePostServiceTest extends ServiceTest {
26+
27+
@Autowired
28+
private SavePostService savePostService;
29+
30+
@Test
31+
void 게시물을_저장한다() {
32+
// given
33+
final Long aliceId = ALICE를_저장한다();
34+
35+
final SavePostRequest request = 게시물_생성_요청(POST_1.생성(aliceId));
36+
final List<MultipartFile> files = 사진_파일(POST_1.imageIdentifiers);
37+
38+
// when
39+
final Long postId = savePostService.invoke(aliceId, request, files);
40+
41+
// then
42+
final Post post = posts.getById(postId);
43+
final List<String> actualFiles = post.getImages().stream().map(Image::getIdentifier).toList();
44+
assertAll(() -> {
45+
assertThat(post.getId()).isEqualTo(postId);
46+
assertThat(post.getUserId()).isEqualTo(aliceId);
47+
assertThat(post.getContent()).isEqualTo(POST_1.content);
48+
assertThat(actualFiles).containsExactlyInAnyOrderElementsOf(POST_1.imageIdentifiers);
49+
});
50+
}
51+
52+
@Test
53+
void 퀘스트를_링크할_수_있다() {
54+
// given
55+
final Long aliceId = 중재자_권한으로_ALICE를_저장한다();
56+
57+
final Long badgeId = badges.save(BadgeFixtures.BADGE_1.생성()).getId();
58+
final Long questId = quests.save(QUEST_1.세부사항을_설정한다(QUEST_1.일반_퀘스트_생성(badgeId))).getId();
59+
participants.saveWithUserIdAndQuestId(aliceId, questId);
60+
61+
final SavePostRequest request = 게시물_생성_요청(POST_1.생성(aliceId, questId));
62+
final List<MultipartFile> files = 사진_파일(POST_1.imageIdentifiers);
63+
64+
// when
65+
final Long postId = savePostService.invoke(aliceId, request, files);
66+
67+
// then
68+
final Post post = posts.getById(postId);
69+
assertAll(() -> {
70+
assertThat(post.getQuestId()).isEqualTo(questId);
71+
then(postClient).should().requestJudge(eq(postId), any(), any());
72+
});
73+
}
74+
75+
@Test
76+
void 수행_중이지_않은_퀘스트는_링크할_수_없다() {
77+
// given
78+
final Long aliceId = 중재자_권한으로_ALICE를_저장한다();
79+
80+
final Long badgeId = badges.save(BadgeFixtures.BADGE_1.생성()).getId();
81+
final Long questId = quests.save(QUEST_1.세부사항을_설정한다(QUEST_1.일반_퀘스트_생성(badgeId))).getId();
82+
83+
final SavePostRequest request = 게시물_생성_요청(POST_1.생성(aliceId, questId));
84+
final List<MultipartFile> files = 사진_파일(POST_1.imageIdentifiers);
85+
86+
// when
87+
assertThatThrownBy(() -> savePostService.invoke(aliceId, request, files))
88+
.isInstanceOf(InvalidDomainException.class)
89+
.hasMessageContaining(NOT_ACCEPTED_QUEST.getMessage());
90+
}
91+
92+
private SavePostRequest 게시물_생성_요청(final Post post) {
93+
final SavePostRequest request = new SavePostRequest();
94+
ReflectionTestUtils.setField(request, "content", post.getContent());
95+
if (post.isQuestLinked()) {
96+
ReflectionTestUtils.setField(request, "questId", post.getQuestId());
97+
}
98+
return request;
99+
}
100+
}

0 commit comments

Comments
 (0)