spring-boot-realworld-examp.../src/test/java/io/spring/api/CommentsApiTest.java

124 lines
4.2 KiB
Java
Raw Normal View History

2017-08-15 15:36:07 +07:00
package io.spring.api;
import io.restassured.RestAssured;
import io.spring.application.comment.CommentData;
import io.spring.application.comment.CommentQueryService;
import io.spring.application.profile.ProfileData;
import io.spring.core.article.Article;
import io.spring.core.article.ArticleRepository;
import io.spring.core.comment.CommentRepository;
import org.joda.time.DateTime;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.boot.context.embedded.LocalServerPort;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.boot.test.mock.mockito.MockBean;
import org.springframework.test.context.junit4.SpringRunner;
2017-08-15 16:52:23 +07:00
import java.util.Arrays;
2017-08-15 15:36:07 +07:00
import java.util.HashMap;
import java.util.Map;
import java.util.Optional;
import static io.restassured.RestAssured.given;
import static org.hamcrest.CoreMatchers.any;
import static org.hamcrest.core.IsEqual.equalTo;
import static org.mockito.Matchers.anyString;
import static org.mockito.Matchers.eq;
import static org.mockito.Mockito.when;
import static org.springframework.boot.test.context.SpringBootTest.WebEnvironment.RANDOM_PORT;
@SpringBootTest(webEnvironment = RANDOM_PORT)
@RunWith(SpringRunner.class)
public class CommentsApiTest extends TestWithCurrentUser {
@LocalServerPort
private int port;
protected String email;
protected String username;
protected String defaultAvatar;
@MockBean
private ArticleRepository articleRepository;
@MockBean
private CommentRepository commentRepository;
@MockBean
private CommentQueryService commentQueryService;
private Article article;
2017-08-15 16:52:23 +07:00
private CommentData commentData;
2017-08-15 15:36:07 +07:00
@Before
public void setUp() throws Exception {
RestAssured.port = port;
email = "john@jacob.com";
username = "johnjacob";
defaultAvatar = "https://static.productionready.io/images/smiley-cyrus.jpg";
userFixture(email, username, defaultAvatar);
article = new Article("title", "desc", "body", new String[]{"test", "java"}, user.getId());
when(articleRepository.findBySlug(eq(article.getSlug()))).thenReturn(Optional.of(article));
2017-08-15 16:52:23 +07:00
commentData = new CommentData(
"123",
"comment",
article.getId(),
new DateTime(),
new DateTime(),
new ProfileData(user.getId(), user.getUsername(), user.getBio(), user.getImage(), false));
2017-08-15 15:36:07 +07:00
}
@Test
public void should_create_comment_success() throws Exception {
Map<String, Object> param = new HashMap<String, Object>() {{
put("comment", new HashMap<String, Object>() {{
put("body", "comment content");
}});
}};
when(commentQueryService.findById(anyString(), eq(user))).thenReturn(Optional.of(commentData));
given()
.contentType("application/json")
.header("Authorization", "Token " + token)
.body(param)
.when()
.post("/articles/{slug}/comments", article.getSlug())
.then()
.statusCode(201)
.body("comment.body", equalTo(commentData.getBody()));
}
@Test
public void should_get_422_with_empty_body() throws Exception {
Map<String, Object> param = new HashMap<String, Object>() {{
put("comment", new HashMap<String, Object>() {{
put("body", "");
}});
}};
given()
.contentType("application/json")
.header("Authorization", "Token " + token)
.body(param)
.when()
.post("/articles/{slug}/comments", article.getSlug())
.then()
.statusCode(422)
.body("errors.body[0]", equalTo("can't be empty"));
}
2017-08-15 16:52:23 +07:00
@Test
public void should_get_comments_of_article_success() throws Exception {
when(commentQueryService.findByArticleSlug(anyString(), eq(null))).thenReturn(Arrays.asList(commentData));
RestAssured.when()
.get("/articles/{slug}/comments", article.getSlug())
.prettyPeek()
.then()
.statusCode(200)
.body("comments[0].id", equalTo(commentData.getId()));
}
2017-08-15 15:36:07 +07:00
}