create comment

This commit is contained in:
aisensiy
2017-08-15 16:36:07 +08:00
parent 9dbb008dcb
commit 445311ee1b
15 changed files with 479 additions and 4 deletions

View File

@@ -0,0 +1,111 @@
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;
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;
@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));
}
@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");
}});
}};
CommentData commentData = new CommentData(
"123",
"comment",
article.getId(),
new DateTime(),
new DateTime(),
new ProfileData(user.getId(), user.getUsername(), user.getBio(), user.getImage(), false));
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"));
}
}

View File

@@ -0,0 +1,52 @@
package io.spring.application.comment;
import io.spring.core.comment.Comment;
import io.spring.core.comment.CommentRepository;
import io.spring.core.user.User;
import io.spring.core.user.UserRepository;
import io.spring.infrastructure.comment.MyBatisCommentRepository;
import io.spring.infrastructure.user.MyBatisUserRepository;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mybatis.spring.boot.test.autoconfigure.MybatisTest;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Import;
import org.springframework.test.context.junit4.SpringRunner;
import java.util.Optional;
import static org.hamcrest.CoreMatchers.is;
import static org.junit.Assert.*;
@MybatisTest
@RunWith(SpringRunner.class)
@Import({MyBatisCommentRepository.class, MyBatisUserRepository.class, CommentQueryService.class})
public class CommentQueryServiceTest {
@Autowired
private CommentRepository commentRepository;
@Autowired
private UserRepository userRepository;
@Autowired
private CommentQueryService commentQueryService;
private User user;
@Before
public void setUp() throws Exception {
user = new User("aisensiy@test.com", "aisensiy", "123", "", "");
userRepository.save(user);
}
@Test
public void should_read_comment_success() throws Exception {
Comment comment = new Comment("content", user.getId(), "123");
commentRepository.save(comment);
Optional<CommentData> optional = commentQueryService.findById(comment.getId(), user);
assertThat(optional.isPresent(), is(true));
CommentData commentData = optional.get();
assertThat(commentData.getProfileData().getUsername(), is(user.getUsername()));
}
}

View File

@@ -0,0 +1,33 @@
package io.spring.infrastructure.comment;
import io.spring.core.comment.Comment;
import io.spring.core.comment.CommentRepository;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mybatis.spring.boot.test.autoconfigure.MybatisTest;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Import;
import org.springframework.test.context.junit4.SpringRunner;
import java.util.Optional;
import static org.hamcrest.CoreMatchers.is;
import static org.junit.Assert.*;
@MybatisTest
@RunWith(SpringRunner.class)
@Import({MyBatisCommentRepository.class})
public class MyBatisCommentRepositoryTest {
@Autowired
private CommentRepository commentRepository;
@Test
public void should_create_and_fetch_comment_success() throws Exception {
Comment comment = new Comment("content", "123", "456");
commentRepository.save(comment);
Optional<Comment> optional = commentRepository.findById(comment.getId());
assertThat(optional.isPresent(), is(true));
assertThat(optional.get(), is(comment));
}
}