Skip to content

Commit

Permalink
Merge pull request #166 from DayByQuest/test/postService
Browse files Browse the repository at this point in the history
[Test] 게시물 저장 서비스를 테스트한다
  • Loading branch information
vectorch9 authored Dec 19, 2023
2 parents 7fd825e + 255ba8e commit 6f781e5
Show file tree
Hide file tree
Showing 3 changed files with 117 additions and 3 deletions.
3 changes: 2 additions & 1 deletion src/main/java/daybyquest/post/domain/Post.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
import jakarta.persistence.EntityListeners;
import jakarta.persistence.EnumType;
import jakarta.persistence.Enumerated;
import jakarta.persistence.FetchType;
import jakarta.persistence.GeneratedValue;
import jakarta.persistence.Id;
import jakarta.persistence.JoinColumn;
Expand Down Expand Up @@ -66,7 +67,7 @@ public class Post {
@Column(length = MAX_CONTENT_SIZE)
private String content;

@ElementCollection
@ElementCollection(fetch = FetchType.EAGER)
@CollectionTable(name = "post_image", joinColumns = @JoinColumn(name = "post_id"))
@OrderColumn(name = "order")
private List<Image> images;
Expand Down
100 changes: 100 additions & 0 deletions src/test/java/daybyquest/post/application/SavePostServiceTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
package daybyquest.post.application;

import static daybyquest.global.error.ExceptionCode.NOT_ACCEPTED_QUEST;
import static daybyquest.support.fixture.PostFixtures.POST_1;
import static daybyquest.support.fixture.QuestFixtures.QUEST_1;
import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.assertThatThrownBy;
import static org.junit.jupiter.api.Assertions.assertAll;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.ArgumentMatchers.eq;
import static org.mockito.BDDMockito.then;

import daybyquest.global.error.exception.InvalidDomainException;
import daybyquest.image.domain.Image;
import daybyquest.post.domain.Post;
import daybyquest.post.dto.request.SavePostRequest;
import daybyquest.support.fixture.BadgeFixtures;
import daybyquest.support.test.ServiceTest;
import java.util.List;
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 SavePostServiceTest extends ServiceTest {

@Autowired
private SavePostService savePostService;

@Test
void 게시물을_저장한다() {
// given
final Long aliceId = ALICE를_저장한다();

final SavePostRequest request = 게시물_생성_요청(POST_1.생성(aliceId));
final List<MultipartFile> files = 사진_파일(POST_1.imageIdentifiers);

// when
final Long postId = savePostService.invoke(aliceId, request, files);

// then
final Post post = posts.getById(postId);
final List<String> actualFiles = post.getImages().stream().map(Image::getIdentifier).toList();
assertAll(() -> {
assertThat(post.getId()).isEqualTo(postId);
assertThat(post.getUserId()).isEqualTo(aliceId);
assertThat(post.getContent()).isEqualTo(POST_1.content);
assertThat(actualFiles).containsExactlyInAnyOrderElementsOf(POST_1.imageIdentifiers);
});
}

@Test
void 퀘스트를_링크할_수_있다() {
// given
final Long aliceId = 중재자_권한으로_ALICE를_저장한다();

final Long badgeId = badges.save(BadgeFixtures.BADGE_1.생성()).getId();
final Long questId = quests.save(QUEST_1.세부사항을_설정한다(QUEST_1.일반_퀘스트_생성(badgeId))).getId();
participants.saveWithUserIdAndQuestId(aliceId, questId);

final SavePostRequest request = 게시물_생성_요청(POST_1.생성(aliceId, questId));
final List<MultipartFile> files = 사진_파일(POST_1.imageIdentifiers);

// when
final Long postId = savePostService.invoke(aliceId, request, files);

// then
final Post post = posts.getById(postId);
assertAll(() -> {
assertThat(post.getQuestId()).isEqualTo(questId);
then(postClient).should().requestJudge(eq(postId), any(), any());
});
}

@Test
void 수행_중이지_않은_퀘스트는_링크할_수_없다() {
// given
final Long aliceId = 중재자_권한으로_ALICE를_저장한다();

final Long badgeId = badges.save(BadgeFixtures.BADGE_1.생성()).getId();
final Long questId = quests.save(QUEST_1.세부사항을_설정한다(QUEST_1.일반_퀘스트_생성(badgeId))).getId();

final SavePostRequest request = 게시물_생성_요청(POST_1.생성(aliceId, questId));
final List<MultipartFile> files = 사진_파일(POST_1.imageIdentifiers);

// when
assertThatThrownBy(() -> savePostService.invoke(aliceId, request, files))
.isInstanceOf(InvalidDomainException.class)
.hasMessageContaining(NOT_ACCEPTED_QUEST.getMessage());
}

private SavePostRequest 게시물_생성_요청(final Post post) {
final SavePostRequest request = new SavePostRequest();
ReflectionTestUtils.setField(request, "content", post.getContent());
if (post.isQuestLinked()) {
ReflectionTestUtils.setField(request, "questId", post.getQuestId());
}
return request;
}
}
17 changes: 15 additions & 2 deletions src/test/java/daybyquest/support/test/ServiceTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,15 @@
import daybyquest.group.domain.Groups;
import daybyquest.interest.domain.Interests;
import daybyquest.participant.domain.Participants;
import daybyquest.post.application.PostClient;
import daybyquest.post.domain.Posts;
import daybyquest.quest.domain.Quests;
import daybyquest.relation.domain.Follows;
import daybyquest.support.config.StubInfraConfig;
import daybyquest.support.util.DatabaseCleaner;
import daybyquest.user.domain.User;
import daybyquest.user.domain.Users;
import java.util.List;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeEach;
import org.springframework.beans.factory.annotation.Autowired;
Expand Down Expand Up @@ -67,6 +69,9 @@ public class ServiceTest {
@MockBean
protected DateTimeProvider dataTimeProvider;

@MockBean
protected PostClient postClient;

@SpyBean
private AuditingHandler handler;

Expand Down Expand Up @@ -116,8 +121,16 @@ void cleanDatabase() {
return new NoOffsetIdPage(null, 5);
}

protected MultipartFile 사진_파일() {
return new MockMultipartFile("image", "image.png",
protected MultipartFile 사진_파일(final String name) {
return new MockMultipartFile("image", name,
MediaType.MULTIPART_FORM_DATA_VALUE, "file content".getBytes());
}

protected MultipartFile 사진_파일() {
return 사진_파일("image.png");
}

protected List<MultipartFile> 사진_파일(final List<String> names) {
return names.stream().map(this::사진_파일).toList();
}
}

0 comments on commit 6f781e5

Please sign in to comment.