Skip to content

Commit

Permalink
commentController test added
Browse files Browse the repository at this point in the history
  • Loading branch information
zenibakoLee committed Nov 20, 2023
1 parent 5b04f25 commit 757990d
Show file tree
Hide file tree
Showing 6 changed files with 139 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ public CreateCommentService(CommentRepository commentRepository) {
}

public CommentDto createCommentDto(String postId, CommentDto commentDto) {

Comment comment = new Comment(PostId.of(postId), CommentAuthor.of(commentDto.getAuthor()), MultilineText.of(commentDto.getContent()));
commentRepository.save(comment);
return new CommentDto(comment);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ public CommentDto updateCommentDto(String commentId, CommentDto commentDto) {
if (comment == null) {
throw new PostNotFound();
}

comment.update(MultilineText.of(commentDto.getContent()));
return new CommentDto(comment);
}
Expand Down
19 changes: 18 additions & 1 deletion src/main/java/kr/megaptera/assignment/dtos/CommentDto.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package kr.megaptera.assignment.dtos;

import com.fasterxml.jackson.annotation.JsonCreator;
import com.fasterxml.jackson.annotation.JsonProperty;
import kr.megaptera.assignment.models.Comment;

public class CommentDto {
Expand All @@ -19,7 +21,13 @@ public CommentDto(Comment comment) {
this.content = comment.content().toString();
}

public CommentDto(String content) {
@JsonCreator
public CommentDto(@JsonProperty("content") String content) {
this.content = content;
}

public CommentDto(String author, String content) {
this.author = author;
this.content = content;
}

Expand All @@ -46,4 +54,13 @@ public String getContent() {
public void setContent(String content) {
this.content = content;
}

@Override
public String toString() {
return "CommentDto{" +
"id='" + id + '\'' +
", author='" + author + '\'' +
", content='" + content + '\'' +
'}';
}
}
12 changes: 11 additions & 1 deletion src/main/java/kr/megaptera/assignment/models/Comment.java
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ public Comment(PostId postId, CommentAuthor author, MultilineText content) {
this.author = author;
this.content = content;
}

public PostId postId() {
return postId;
}
Expand All @@ -38,4 +38,14 @@ private static String generate() {
public void update(MultilineText content) {
this.content = content;
}

@Override
public String toString() {
return "Comment{" +
"postId=" + postId +
", id=" + id +
", author=" + author +
", content=" + content +
'}';
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -33,4 +33,15 @@ public void save(Comment comment) {
public void delete(CommentId commentId) {
comments.remove(commentId);
}

public void clear() {
comments.clear();
}

@Override
public String toString() {
return "CommentRepository{" +
"comments=" + comments +
'}';
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,102 @@
package kr.megaptera.assignment.controllers;

import kr.megaptera.assignment.dtos.CommentDto;
import kr.megaptera.assignment.models.Comment;
import kr.megaptera.assignment.models.CommentAuthor;
import kr.megaptera.assignment.models.MultilineText;
import kr.megaptera.assignment.models.Post;
import kr.megaptera.assignment.models.PostAuthor;
import kr.megaptera.assignment.models.PostId;
import kr.megaptera.assignment.models.PostTitle;
import kr.megaptera.assignment.repositories.CommentRepository;
import kr.megaptera.assignment.repositories.PostRepository;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.boot.test.web.client.TestRestTemplate;
import org.springframework.http.client.HttpComponentsClientHttpRequestFactory;

import java.util.List;

import static org.assertj.core.api.Assertions.assertThat;

@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
class CommentControllerTest {
// TODO: PostControllerTest 를 참고해서 테스트를 작성해 주세요.
@Autowired
private TestRestTemplate restTemplate;
@Autowired
private PostRepository postRepository;
@Autowired
private CommentRepository commentRepository;

private final String COMMENTS_CONTROLLER_URL;

public CommentControllerTest(@Value("${local.server.port}") int port) {
COMMENTS_CONTROLLER_URL = "http://localhost:" + port + "/comments";
}

private final Post SAMPLE_POST = new Post(
PostId.of("0001POST"),
PostTitle.of("제목"),
PostAuthor.of("작성자"),
MultilineText.of("내용")
);
private final Comment SAMPLE_COMMENT = new Comment(
SAMPLE_POST.id(),
CommentAuthor.of("commentAuthor"),
MultilineText.of("commentContent")
);


@BeforeEach
void setup() {
postRepository.clear();
postRepository.save(SAMPLE_POST);
commentRepository.clear();
commentRepository.save(SAMPLE_COMMENT);
}

@Test
@DisplayName("GET /comments?postId={postId}")
void list() {
List<CommentDto> result = restTemplate.getForObject(COMMENTS_CONTROLLER_URL + "?postId=" + SAMPLE_POST.id(), List.class);
assertThat(result.size()).isEqualTo(1);
}

@Test
@DisplayName("POST /comments?postId={postId}")
void create() {
CommentDto commentDto = new CommentDto("createdAuthor", "createdContent");

restTemplate.postForLocation(COMMENTS_CONTROLLER_URL + "?postId=" + SAMPLE_POST.id(), commentDto);

String body = restTemplate.getForObject(COMMENTS_CONTROLLER_URL + "?postId=" + SAMPLE_POST.id(), String.class);
assertThat(body).contains("createdAuthor");
}

@Test
@DisplayName("PATCH /comments/{id}?postId={postId}")
void update() {
CommentDto commentDto = new CommentDto("updatedContent");
restTemplate.getRestTemplate().setRequestFactory(new HttpComponentsClientHttpRequestFactory());
CommentDto updatedComment = restTemplate.patchForObject(COMMENTS_CONTROLLER_URL + "/" + SAMPLE_COMMENT.id() + "?postId=" + SAMPLE_POST.id()
, commentDto, CommentDto.class);

assertThat(updatedComment.getId()).isEqualTo(SAMPLE_COMMENT.id().toString());
assertThat(updatedComment.getContent()).isEqualTo(commentDto.getContent());
}

@Test
@DisplayName("DELETE /comments/{id}?postId={postId}")
void deletePost() {
restTemplate.delete(COMMENTS_CONTROLLER_URL + "/" + SAMPLE_COMMENT.id() + "?postId=" + SAMPLE_POST.id());

List<CommentDto> result = restTemplate.getForObject(COMMENTS_CONTROLLER_URL + "?postId=" + SAMPLE_POST.id(), List.class);

assertThat(result.size()).isEqualTo(0);
}
}

0 comments on commit 757990d

Please sign in to comment.