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

151 lines
5.6 KiB
Java
Raw Normal View History

2017-08-15 15:36:07 +07:00
package io.spring.api;
2017-08-24 09:39:09 +07:00
import io.restassured.module.mockmvc.RestAssuredMockMvc;
import io.spring.JacksonCustomizations;
import io.spring.api.security.WebSecurityConfig;
2017-08-18 16:08:27 +07:00
import io.spring.application.CommentQueryService;
import io.spring.application.data.CommentData;
import io.spring.application.data.ProfileData;
2017-08-15 15:36:07 +07:00
import io.spring.core.article.Article;
import io.spring.core.article.ArticleRepository;
2017-08-15 17:16:06 +07:00
import io.spring.core.comment.Comment;
2017-08-15 15:36:07 +07:00
import io.spring.core.comment.CommentRepository;
2017-08-15 17:16:06 +07:00
import io.spring.core.user.User;
2017-08-15 15:36:07 +07:00
import org.junit.Before;
import org.junit.Test;
2017-08-24 09:39:09 +07:00
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.autoconfigure.web.servlet.WebMvcTest;
2017-08-15 15:36:07 +07:00
import org.springframework.boot.test.mock.mockito.MockBean;
2017-08-24 09:39:09 +07:00
import org.springframework.context.annotation.Import;
import org.springframework.test.web.servlet.MockMvc;
2017-08-15 15:36:07 +07:00
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;
2017-08-24 09:39:09 +07:00
import static io.restassured.module.mockmvc.RestAssuredMockMvc.given;
2017-08-15 15:36:07 +07:00
import static org.hamcrest.core.IsEqual.equalTo;
2018-05-10 13:50:44 +07:00
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.ArgumentMatchers.anyString;
import static org.mockito.ArgumentMatchers.eq;
2017-08-15 15:36:07 +07:00
import static org.mockito.Mockito.when;
2017-08-24 09:39:09 +07:00
@WebMvcTest(CommentsApi.class)
@Import({WebSecurityConfig.class, JacksonCustomizations.class})
2017-08-15 15:36:07 +07:00
public class CommentsApiTest extends TestWithCurrentUser {
@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 17:16:06 +07:00
private Comment comment;
2017-08-24 09:39:09 +07:00
@Autowired
private MockMvc mvc;
2017-08-15 15:36:07 +07:00
@Before
public void setUp() throws Exception {
2017-08-24 09:39:09 +07:00
RestAssuredMockMvc.mockMvc(mvc);
super.setUp();
2017-08-15 15:36:07 +07:00
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 17:16:06 +07:00
comment = new Comment("comment", user.getId(), article.getId());
2017-08-15 16:52:23 +07:00
commentData = new CommentData(
2017-08-15 17:16:06 +07:00
comment.getId(),
comment.getBody(),
comment.getArticleId(),
comment.getCreatedAt(),
comment.getCreatedAt(),
2017-08-15 16:52:23 +07:00
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 {
2017-08-18 11:09:07 +07:00
when(commentQueryService.findByArticleId(anyString(), eq(null))).thenReturn(Arrays.asList(commentData));
2017-08-24 09:39:09 +07:00
RestAssuredMockMvc.when()
2017-08-15 16:52:23 +07:00
.get("/articles/{slug}/comments", article.getSlug())
.prettyPeek()
.then()
.statusCode(200)
.body("comments[0].id", equalTo(commentData.getId()));
}
2017-08-15 17:16:06 +07:00
@Test
public void should_delete_comment_success() throws Exception {
2017-08-16 14:58:51 +07:00
when(commentRepository.findById(eq(article.getId()), eq(comment.getId()))).thenReturn(Optional.of(comment));
2017-08-15 17:16:06 +07:00
given()
.header("Authorization", "Token " + token)
.when()
.delete("/articles/{slug}/comments/{id}", article.getSlug(), comment.getId())
.then()
.statusCode(204);
}
@Test
public void should_get_403_if_not_author_of_article_or_author_of_comment_when_delete_comment() throws Exception {
User anotherUser = new User("other@example.com", "other", "123", "", "");
when(userRepository.findByUsername(eq(anotherUser.getUsername()))).thenReturn(Optional.of(anotherUser));
2017-08-24 09:39:09 +07:00
when(jwtService.getSubFromToken(any())).thenReturn(Optional.of(anotherUser.getId()));
when(userRepository.findById(eq(anotherUser.getId()))).thenReturn(Optional.ofNullable(anotherUser));
2017-08-15 17:16:06 +07:00
when(commentRepository.findById(eq(article.getId()), eq(comment.getId()))).thenReturn(Optional.of(comment));
2017-08-18 16:08:27 +07:00
String token = jwtService.toToken(anotherUser);
when(userRepository.findById(eq(anotherUser.getId()))).thenReturn(Optional.of(anotherUser));
2017-08-15 17:16:06 +07:00
given()
.header("Authorization", "Token " + token)
.when()
.delete("/articles/{slug}/comments/{id}", article.getSlug(), comment.getId())
.then()
.statusCode(403);
}
2017-08-15 15:36:07 +07:00
}