본문 바로가기

패스트캠퍼스

패스트캠퍼스 환급챌린지 19일차 : 9개 도메인 프로젝트로 끝내는 백엔드 웹 개발 (Java/Spring) 초격차 패키지 Online 강의 후기

본 포스팅은 패스트캠퍼스 환급 챌린지 참여를 위해 작성하였습니다.

공부 시작 시각 인증

공부 시작 시각 인증

수강 인증 사진

수강 인증 사진

PostApplicationTestTemplate.java

package org.fastcampus.post.application;

import org.fastcampus.fake.*;
import org.fastcampus.post.application.dto.*;
import org.fastcampus.post.domain.*;
import org.fastcampus.post.domain.content.*;
import org.fastcampus.user.application.UserService;
import org.fastcampus.user.application.dto.CreateUserRequestDto;
import org.fastcampus.user.domain.User;

public class PostApplicationTestTemplate {
    static final UserService userService = FakeObjectFactory.getUserService();
    static final PostService postService = FakeObjectFactory.getPostService();
    static final CommentService commentService = FakeObjectFactory.getCommentService();

    static final User user = userService.createUser(new CreateUserRequestDto("user1", null));
    static final User otherUser = userService.createUser(new CreateUserRequestDto("user2", null));

    static final CreatePostRequestDto postRequestDto = new CreatePostRequestDto(user.getId(), "test-content", PostPublicationState.PUBLIC);
    static final Post post = postService.createPost(postRequestDto);

    static final CreateCommentRequestDto commentRequestDto = new CreateCommentRequestDto(post.getId(), user.getId(), "test-comment");
}

PostServiceTest.java

package org.fastcampus.post.application;

import org.fastcampus.post.application.dto.LikeRequestDto;
import org.fastcampus.post.application.dto.UpdatePostRequestDto;
import org.fastcampus.post.domain.*;
import org.fastcampus.post.domain.content.*;
import org.junit.jupiter.api.Test;

import static org.junit.jupiter.api.Assertions.*;

class PostServiceTest extends PostApplicationTestTemplate {
    @Test
    void givenPostRequestDto_whenCreatePost_thenPostIsCreated() {
        // when
        Post savePost = postService.createPost(postRequestDto);

        // then
        Post post = postService.getPost(savePost.getId());
        assertEquals(savePost, post);
    }

    @Test
    void givenCreatePost_whenUpdate_thenReturnUpdatedPost() {
        //  given
        Post savedPost = postService.createPost(postRequestDto);

        //  when
        UpdatePostRequestDto updateDto = new UpdatePostRequestDto(savedPost.getId(), user.getId(), "updated-content", PostPublicationState.PRIVATE);
        Post updatedPost = postService.updatePost(updateDto);

        //  then
        assertEquals(savedPost.getId(), updatedPost.getId());
        assertEquals(savedPost.getContent(), updatedPost.getContent());
        assertEquals(savedPost.getAuthor(), updatedPost.getAuthor());
    }

    @Test
    void givenPostRequestDto_whenLikePost_thenPostIsLiked() {
        // given
        Post savedPost = postService.createPost(postRequestDto);

        // when
        LikeRequestDto likeDto = new LikeRequestDto(savedPost.getId(), otherUser.getId());
        postService.likePost(likeDto);

        // then
        assertEquals(1, savedPost.getLikeCount());
    }

    @Test
    void givenPostRequestDto_whenUnlikePost_thenPostIsUnliked() {
        // given
        Post savedPost = postService.createPost(postRequestDto);
        LikeRequestDto likeDto = new LikeRequestDto(savedPost.getId(), otherUser.getId());
        postService.likePost(likeDto);

        // when
        postService.unlikePost(likeDto);

        // then
        assertEquals(0, savedPost.getLikeCount());
    }

    @Test
    void givenPostRequestDto_whenUnlikePost_thenPostIsNotUnliked() {
        // given
        Post savedPost = postService.createPost(postRequestDto);

        // when
        LikeRequestDto likeDto = new LikeRequestDto(savedPost.getId(), otherUser.getId());
        postService.unlikePost(likeDto);

        // then
        assertEquals(0, savedPost.getLikeCount());
    }
}

CommentServiceTest.java

package org.fastcampus.post.application;

import org.fastcampus.post.application.dto.LikeRequestDto;
import org.fastcampus.post.application.dto.UpdateCommentRequestDto;
import org.fastcampus.post.domain.comment.*;
import org.junit.jupiter.api.Test;

import static org.junit.jupiter.api.Assertions.*;

class CommentServiceTest extends PostApplicationTestTemplate {
    @Test
    void givenCommentRequestDto_whenCreateComment_thenCommentIsCreated() {
        // when
        Comment savedComment = commentService.createComment(commentRequestDto);

        // then
        Comment comment = commentService.getComment(savedComment.getId());
        assertEquals(savedComment, comment);
    }

    @Test
    void givenCommentRequestDto_whenUpdateComment_thenCommentIsUpdated() {
        //  given
        Comment savedComment = commentService.createComment(commentRequestDto);

        //  when
        UpdateCommentRequestDto updateDto = new UpdateCommentRequestDto(savedComment.getId(), user.getId(), "updated-comment");
        Comment updatedComment = commentService.updateComment(updateDto);

        //  then
        assertEquals(savedComment.getId(), updatedComment.getId());
        assertEquals(savedComment.getContent(), updatedComment.getContent());
        assertEquals(savedComment.getAuthor(), updatedComment.getAuthor());
    }

    @Test
    void givenCommentRequestDto_whenLikeComment_thenCommentIsLiked() {
        // given
        Comment savedComment = commentService.createComment(commentRequestDto);

        // when
        LikeRequestDto likeDto = new LikeRequestDto(savedComment.getId(), otherUser.getId());
        commentService.likeComment(likeDto);

        // then
        assertEquals(1, savedComment.getLikeCount());
    }

    @Test
    void givenCommentRequestDto_whenUnlikeComment_thenCommentIsUnliked() {
        // given
        Comment savedComment = commentService.createComment(commentRequestDto);
        LikeRequestDto likeDto = new LikeRequestDto(savedComment.getId(), otherUser.getId());
        commentService.likeComment(likeDto);

        // when
        commentService.unlikeComment(likeDto);

        // then
        assertEquals(0, savedComment.getLikeCount());
    }
}

학습 인증샷

학습 인증샷

공부 종료 시각 인증

공부 종료 시각 인증

https://bit.ly/4hTSJNB