패스트캠퍼스

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

이태우(1990년) 2025. 3. 18. 22:13

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

공부 시작 시각 인증

공부 시작 시각 인증

수강 인증 사진

수강 인증 사진

CommentContentTest.java

package org.fastcampus.post.domain.content;

import org.junit.jupiter.api.Test;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.NullAndEmptySource;
import org.junit.jupiter.params.provider.ValueSource;

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

class CommentContentTest {
    @Test
    void givenContentLengthIsOK_whenCreated_thenReturnTextContent() {
        //  given
        String text = "This is a test";

        //  when
        CommentContent content = new CommentContent(text);

        //  then
        assertEquals(text, content.getContentText());
    }

    @Test
    void givenContentLengthIsOver_whenCreated_thenThrowError() {
        //  given
        String content = "a".repeat(101);

        //  when, then
        assertThrows(IllegalArgumentException.class, () -> new CommentContent(content));
    }

    @ParameterizedTest
    @ValueSource(strings = {"닭", "삵"})
    void givenContentLengthIsOverAndKorean_whenCreated_thenThrowError(String koreanWord) {
        //  given
        String content = koreanWord.repeat(101);

        //  when, then
        assertThrows(IllegalArgumentException.class, () -> new CommentContent(content));
    }

    @ParameterizedTest
    @NullAndEmptySource
    void givenContentIsEmpty_whenCreated_thenThrowError(String value) {
        //  when, then
        assertThrows(IllegalArgumentException.class, () -> new CommentContent(value));
    }
}

Comment.java

package org.fastcampus.post.domain.comment;

import org.fastcampus.common.domain.*;
import org.fastcampus.post.domain.*;
import org.fastcampus.post.domain.content.CommentContent;
import org.fastcampus.user.domain.User;

public class Comment {
    private final Long id;
    private final Post post;
    private final User author;
    private final CommentContent content;
    private final PositiveIntegerCounter likeCount;

    public Comment(Long id, Post post, User author, CommentContent content) {
        if (author == null) {
            throw new IllegalArgumentException();
        }

        if (post == null) {
            throw new IllegalArgumentException();
        }

        if (content == null) {
            throw new IllegalArgumentException();
        }

        this.id = id;
        this.post = post;
        this.author = author;
        this.content = content;
        this.likeCount = new PositiveIntegerCounter();
    }

    public void like(User user) {
        if (this.author.equals(user)) {
            throw new IllegalArgumentException();
        }
        likeCount.increase();
    }

    public void unlike(User user) {
        if (this.author.equals(user)) {
            throw new IllegalArgumentException();
        }
        likeCount.decrease();
    }

    public void updateComment(User user, String updateContent) {
        if (!this.author.equals(user)) {
            throw new IllegalArgumentException();
        }

        this.content.updateContent(updateContent);
    }

    public int getLikeCount() {
        return likeCount.getCount();
    }

    public String getContent() {
        return content.getContentText();
    }
}

CommentTest.java

package org.fastcampus.post.domain.comment;

import org.fastcampus.post.domain.*;
import org.fastcampus.post.domain.content.*;
import org.fastcampus.user.domain.*;
import org.junit.jupiter.api.Test;

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

class CommentTest {
    private final UserInfo info = new UserInfo("name", "URL");
    private final User user = new User(1L, info);
    private final User otherUser = new User(2L, info);

    private final Post post = new Post(1L, user, new PostContent("content"));
    private final Comment comment = new Comment(1L, post, user, new CommentContent("content"));

    @Test
    void givenCommentCreated_whenLike_thenIncreaseLikeCount() {
        //  given
        int likeCount = comment.getLikeCount();

        //  when
        comment.like(otherUser);

        //  then
        assertEquals(likeCount + 1, comment.getLikeCount());
    }

    @Test
    void givenCommentCreated_whenLikeByUser_thenThrowError() {
        //  when, then
        assertThrows(IllegalArgumentException.class, () -> comment.like(user));
    }

    @Test
    void givenCommentCreated_whenUnlike_thenDecreaseLikeCount() {
        //  given
        comment.like(otherUser);
        int likeCount = comment.getLikeCount();

        //  when
        comment.unlike(otherUser);

        //  then
        assertEquals(likeCount - 1, comment.getLikeCount());
    }

    @Test
    void givenCommentCreated_whenUnlike_thenLikeCountIsZero() {
        //  when
        comment.unlike(otherUser);

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

    @Test
    void givenCommentCreated_whenUpdateComment_thenContentIsUpdated() {
        //  given
        String updateContent = "updated content";

        //  when
        comment.updateComment(user, updateContent);

        //  then
        assertEquals(updateContent, comment.getContent());
    }

    @Test
    void givenComment_whenUpdateContentLengthIsOver_thenThrowError() {
        //  given
        String content = "This is a test";
        CommentContent commentContent = new CommentContent(content);
        String updatedContent = "a".repeat(101);

        //  when, then
        assertThrows(IllegalArgumentException.class, () -> commentContent.updateContent(updatedContent));
    }
}

학습 인증샷

학습 인증샷

공부 종료 시각 인증

공부 종료 시각 인증

https://bit.ly/4hTSJNB