This commit is contained in:
aisensiy
2017-08-18 12:09:07 +08:00
parent d6bf680a97
commit c3029aa636
16 changed files with 189 additions and 41 deletions

View File

@@ -115,7 +115,7 @@ public class CommentsApiTest extends TestWithCurrentUser {
@Test
public void should_get_comments_of_article_success() throws Exception {
when(commentQueryService.findByArticleSlug(anyString(), eq(null))).thenReturn(Arrays.asList(commentData));
when(commentQueryService.findByArticleId(anyString(), eq(null))).thenReturn(Arrays.asList(commentData));
RestAssured.when()
.get("/articles/{slug}/comments", article.getSlug())
.prettyPeek()

View File

@@ -1,20 +1,29 @@
package io.spring.api;
import io.restassured.RestAssured;
import io.spring.application.JwtService;
import io.spring.application.user.UserData;
import io.spring.core.user.User;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
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.is;
import static org.hamcrest.CoreMatchers.not;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.core.IsEqual.equalTo;
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)
@@ -24,16 +33,9 @@ public class CurrentUserApiTest extends TestWithCurrentUser {
@LocalServerPort
private int port;
protected String email;
protected String username;
protected String defaultAvatar;
@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();
}
@@ -51,7 +53,7 @@ public class CurrentUserApiTest extends TestWithCurrentUser {
.body("user.email", equalTo(email))
.body("user.username", equalTo(username))
.body("user.bio", equalTo(""))
.body("user.image", equalTo("https://static.productionready.io/images/smiley-cyrus.jpg"))
.body("user.image", equalTo(defaultAvatar))
.body("user.token", equalTo(token));
}
@@ -81,14 +83,18 @@ public class CurrentUserApiTest extends TestWithCurrentUser {
public void should_update_current_user_profile() throws Exception {
String newEmail = "newemail@example.com";
String newBio = "updated";
String newUsername = "newusernamee";
Map<String, Object> param = new HashMap<String, Object>() {{
put("user", new HashMap<String, Object>() {{
put("email", newEmail);
put("bio", newBio);
put("username", newUsername);
}});
}};
when(userReadService.findByUsername(eq(newUsername))).thenReturn(new UserData(user.getId(), newEmail, newUsername, newBio, user.getImage()));
given()
.contentType("application/json")
.header("Authorization", "Token " + token)
@@ -96,11 +102,44 @@ public class CurrentUserApiTest extends TestWithCurrentUser {
.when()
.put("/user")
.then()
.statusCode(200);
.statusCode(200)
.body("user.token", not(token));
}
assertThat(user.getEmail(), is(newEmail));
assertThat(user.getBio(), is(newBio));
assertThat(user.getImage(), is(defaultAvatar));
@Test
public void should_get_error_if_email_exists_when_update_user_profile() throws Exception {
String newEmail = "newemail@example.com";
String newBio = "updated";
String newUsername = "newusernamee";
Map<String, Object> param = prepareUpdateParam(newEmail, newBio, newUsername);
when(userRepository.findByEmail(eq(newEmail))).thenReturn(Optional.of(new User(newEmail, "username", "123", "", "")));
when(userRepository.findByUsername(eq(newUsername))).thenReturn(Optional.empty());
when(userReadService.findByUsername(eq(newUsername))).thenReturn(new UserData(user.getId(), newEmail, newUsername, newBio, user.getImage()));
given()
.contentType("application/json")
.header("Authorization", "Token " + token)
.body(param)
.when()
.put("/user")
.prettyPeek()
.then()
.statusCode(422)
.body("errors.email[0]", equalTo("email already exist"));
}
private HashMap<String, Object> prepareUpdateParam(final String newEmail, final String newBio, final String newUsername) {
return new HashMap<String, Object>() {{
put("user", new HashMap<String, Object>() {{
put("email", newEmail);
put("bio", newBio);
put("username", newUsername);
}});
}};
}
@Test

View File

@@ -1,9 +1,13 @@
package io.spring.application.comment;
import io.spring.core.article.Article;
import io.spring.core.article.ArticleRepository;
import io.spring.core.comment.Comment;
import io.spring.core.comment.CommentRepository;
import io.spring.core.user.FollowRelation;
import io.spring.core.user.User;
import io.spring.core.user.UserRepository;
import io.spring.infrastructure.article.MyBatisArticleRepository;
import io.spring.infrastructure.comment.MyBatisCommentRepository;
import io.spring.infrastructure.user.MyBatisUserRepository;
import org.junit.Before;
@@ -14,6 +18,7 @@ import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Import;
import org.springframework.test.context.junit4.SpringRunner;
import java.util.List;
import java.util.Optional;
import static org.hamcrest.CoreMatchers.is;
@@ -21,7 +26,7 @@ import static org.junit.Assert.*;
@MybatisTest
@RunWith(SpringRunner.class)
@Import({MyBatisCommentRepository.class, MyBatisUserRepository.class, CommentQueryService.class})
@Import({MyBatisCommentRepository.class, MyBatisUserRepository.class, CommentQueryService.class, MyBatisArticleRepository.class})
public class CommentQueryServiceTest {
@Autowired
private CommentRepository commentRepository;
@@ -31,6 +36,10 @@ public class CommentQueryServiceTest {
@Autowired
private CommentQueryService commentQueryService;
@Autowired
private ArticleRepository articleRepository;
private User user;
@Before
@@ -49,4 +58,23 @@ public class CommentQueryServiceTest {
CommentData commentData = optional.get();
assertThat(commentData.getProfileData().getUsername(), is(user.getUsername()));
}
@Test
public void should_read_comments_of_article() throws Exception {
Article article = new Article("title", "desc", "body", new String[]{"java"}, user.getId());
articleRepository.save(article);
User user2 = new User("user2@email.com", "user2", "123", "", "");
userRepository.save(user2);
userRepository.saveRelation(new FollowRelation(user.getId(), user2.getId()));
Comment comment1 = new Comment("content1", user.getId(), article.getId());
commentRepository.save(comment1);
Comment comment2 = new Comment("content2", user2.getId(), article.getId());
commentRepository.save(comment2);
List<CommentData> comments = commentQueryService.findByArticleId(article.getId(), user);
assertThat(comments.size(), is(2));
}
}

View File

@@ -2,8 +2,6 @@ package io.spring.application.tag;
import io.spring.core.article.Article;
import io.spring.core.article.ArticleRepository;
import io.spring.core.article.Tag;
import io.spring.infrastructure.article.ArticleMapper;
import io.spring.infrastructure.article.MyBatisArticleRepository;
import org.junit.Test;
import org.junit.runner.RunWith;