패스트캠퍼스

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

이태우(1990년) 2025. 3. 17. 23:29

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

공부 시작 시각 인증

공부 시작 시각 인증

수강 인증 사진

수강 인증 사진

DateTimeInfoTest.java

package org.fastcampus.post.domain.common;

import org.junit.jupiter.api.Test;

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

class DatetimeInfoTest {
    @Test
    void givenDatetimeInfo_whenUpdateEditDatetime_thenIsEditedIsTrue() {
        //  given
        DatetimeInfo datetimeInfo = new DatetimeInfo();

        //  when
        datetimeInfo.updateEditDatetime();
        boolean isEdited = datetimeInfo.isEdited();

        //  then
        assertTrue(isEdited);
    }
}

PostContentTest.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 PostContentTest {
    @Test
    void givenContentLengthIsOK_whenCreated_thenReturnTextContent() {
        //  given
        String text = "This is a test";

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

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

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

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

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

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

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

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

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

    @Test
    void givenContent_whenUpdateContent_thenReturnUpdatedContent() {
        //  given
        String content = "This is a test";
        PostContent postContent = new PostContent(content);
        String updatedContent = "This is an updated test";

        //  when
        postContent.updateContent(updatedContent);

        //  then
        assertEquals(updatedContent, postContent.contentText);
    }

    @Test
    void givenContent_whenUpdateContentLengthIsOver_thenThrowError() {
        //  given
        String content = "This is a test";
        PostContent postContent = new PostContent(content);
        String updatedContent = "a".repeat(501);

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

    @Test
    void givenContent_whenUpdateContentLengthIsUnder_thenThrowError() {
        //  given
        String content = "This is a test";
        PostContent postContent = new PostContent(content);
        String updatedContent = "a".repeat(4);

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

Post.java

package org.fastcampus.post.domain;

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

public class Post {
    private final Long id;
    private final User author;
    private final PostContent content;
    private final PositiveIntegerCounter likeCount;
    private PostPublicationState state;

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

        this.id = id;
        this.author = author;
        this.content = content;
        this.likeCount = new PositiveIntegerCounter();
        this.state = PostPublicationState.PUBLIC;
    }

    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 updatePost(User user, String updateContent, PostPublicationState state) {
        if (!this.author.equals(user)) {
            throw new IllegalArgumentException();
        }

        this.content.updateContent(updateContent);
        this.state = state;
    }

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

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

PostTest.java

package org.fastcampus.post.domain;

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

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

class PostTest {
    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"));

    @Test
    void givenPost_whenLike_thenIncreaseLikeCount() {
        //  given
        int likeCount = post.getLikeCount();

        //  when
        post.like(otherUser);

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

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

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

        //  when
        post.unlike(otherUser);

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

    @Test
    void givenPostCreated_whenUnlike_thenLikeCountIsZero() {
        //  when
        post.unlike(otherUser);

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

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

        //  when
        post.updatePost(user, updateContent, PostPublicationState.PUBLIC);

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

    @Test
    void givenPostCreated_whenUpdatePostByOtherUser_thenThrowError() {
        //  when, then
        assertThrows(IllegalArgumentException.class, () -> post.updatePost(otherUser, "updated content", PostPublicationState.PUBLIC));
    }
}

학습 인증샷

학습 인증샷

공부 종료 시각 인증

공부 종료 시각 인증

https://bit.ly/4hTSJNB