should create article success

This commit is contained in:
aisensiy
2017-08-15 10:47:18 +08:00
parent b1e8632c3b
commit 00f778c087
32 changed files with 825 additions and 37 deletions

View File

@@ -0,0 +1,134 @@
package io.spring.api;
import io.restassured.RestAssured;
import io.spring.application.article.ArticleData;
import io.spring.application.article.ArticleQueryService;
import io.spring.application.profile.ProfileData;
import io.spring.core.article.Article;
import io.spring.core.article.ArticleRepository;
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.Arrays;
import java.util.HashMap;
import java.util.Map;
import java.util.Optional;
import static io.restassured.RestAssured.given;
import static org.hamcrest.core.IsEqual.equalTo;
import static org.junit.Assert.*;
import static org.mockito.Matchers.any;
import static org.mockito.Matchers.eq;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;
import static org.springframework.boot.test.context.SpringBootTest.WebEnvironment.RANDOM_PORT;
@RunWith(SpringRunner.class)
@SpringBootTest(webEnvironment = RANDOM_PORT)
public class ArticlesApiTest extends TestWithCurrentUser {
@LocalServerPort
private int port;
@MockBean
private ArticleRepository articleRepository;
@MockBean
private ArticleQueryService articleQueryService;
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(email, username, defaultAvatar);
}
@Test
public void should_create_article_success() throws Exception {
String title = "How to train your dragon";
String slug = "how-to-train-your-dragon";
String description = "Ever wonder how?";
String body = "You have to believe";
String[] tagList = {"reactjs", "angularjs", "dragons"};
Map<String, Object> param = prepareParam(title, description, body, tagList);
when(articleRepository.toSlug(eq(title))).thenReturn(slug);
ArticleData articleData = new ArticleData(
"123",
slug,
title,
description,
body,
false,
0,
new DateTime(),
new DateTime(),
Arrays.asList(tagList),
new ProfileData("userid", user.getUsername(), user.getBio(), user.getImage(), false));
when(articleQueryService.findById(any(), any())).thenReturn(Optional.of(articleData));
given()
.contentType("application/json")
.header("Authorization", "Token " + token)
.body(param)
.when()
.post("/articles")
.then()
.statusCode(200)
.body("article.title", equalTo(title))
.body("article.favorited", equalTo(false))
.body("article.body", equalTo(body))
.body("article.favoritesCount", equalTo(0))
.body("article.author.username", equalTo(user.getUsername()))
.body("article.author.id", equalTo(null));
verify(articleRepository).save(any());
}
@Test
public void should_get_error_message_with_wrong_parameter() throws Exception {
String title = "How to train your dragon";
String slug = "how-to-train-your-dragon";
String description = "Ever wonder how?";
String body = "";
String[] tagList = {"reactjs", "angularjs", "dragons"};
Map<String, Object> param = prepareParam(title, description, body, tagList);
when(articleRepository.toSlug(eq(title))).thenReturn(slug);
given()
.contentType("application/json")
.header("Authorization", "Token " + token)
.body(param)
.when()
.post("/articles")
.then()
.statusCode(422)
.body("errors.body[0]", equalTo("can't be empty"));
}
private HashMap<String, Object> prepareParam(final String title, final String description, final String body, final String[] tagList) {
return new HashMap<String, Object>() {{
put("article", new HashMap<String, Object>() {{
put("title", title);
put("description", description);
put("body", body);
put("tagList", tagList);
}});
}};
}
}

View File

@@ -12,56 +12,35 @@ 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.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)
@RunWith(SpringRunner.class)
public class CurrentUserApiTest {
@MockBean
private UserRepository userRepository;
@MockBean
private UserReadService userReadService;
public class CurrentUserApiTest extends TestWithCurrentUser {
@LocalServerPort
private int port;
@Autowired
private JwtService jwtService;
private User user;
private UserData userData;
private String token;
private String email;
private String username;
private String defaultAvatar;
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";
user = new User(email, username, "123", "", defaultAvatar);
when(userRepository.findByUsername(eq(username))).thenReturn(Optional.of(user));
userData = new UserData(user.getId(), email, username, "", defaultAvatar);
when(userReadService.findOne(eq(username))).thenReturn(userData);
token = jwtService.toToken(userData);
userFixture(email, username, defaultAvatar);
}
@Test

View File

@@ -0,0 +1,39 @@
package io.spring.api;
import io.spring.application.JwtService;
import io.spring.application.user.UserData;
import io.spring.application.user.UserReadService;
import io.spring.core.user.User;
import io.spring.core.user.UserRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.mock.mockito.MockBean;
import java.util.Optional;
import static org.mockito.Matchers.eq;
import static org.mockito.Mockito.when;
class TestWithCurrentUser {
@MockBean
protected UserRepository userRepository;
@MockBean
protected UserReadService userReadService;
protected User user;
protected UserData userData;
protected String token;
@Autowired
private JwtService jwtService;
protected void userFixture(String email, String username, String defaultAvatar) {
user = new User(email, username, "123", "", defaultAvatar);
when(userRepository.findByUsername(eq(username))).thenReturn(Optional.of(user));
userData = new UserData(user.getId(), email, username, "", defaultAvatar);
when(userReadService.findByUsername(eq(username))).thenReturn(userData);
token = jwtService.toToken(userData);
}
}

View File

@@ -55,7 +55,7 @@ public class UsersApiTest {
when(jwtService.toToken(any())).thenReturn("123");
UserData userData = new UserData("123", email, username, "", defaultAvatar);
when(userReadService.findOne(eq(username))).thenReturn(userData);
when(userReadService.findByUsername(eq(username))).thenReturn(userData);
when(userRepository.findByUsername(eq(username))).thenReturn(Optional.empty());
when(userRepository.findByEmail(eq(email))).thenReturn(Optional.empty());
@@ -178,7 +178,7 @@ public class UsersApiTest {
UserData userData = new UserData("123", email, username, "", defaultAvatar);
when(userRepository.findByEmail(eq(email))).thenReturn(Optional.of(user));
when(userReadService.findOne(eq(username))).thenReturn(userData);
when(userReadService.findByUsername(eq(username))).thenReturn(userData);
when(jwtService.toToken(any())).thenReturn("123");
Map<String, Object> param = new HashMap<String, Object>() {{
@@ -212,7 +212,7 @@ public class UsersApiTest {
UserData userData = new UserData(user.getId(), email, username, "", defaultAvatar);
when(userRepository.findByEmail(eq(email))).thenReturn(Optional.of(user));
when(userReadService.findOne(eq(username))).thenReturn(userData);
when(userReadService.findByUsername(eq(username))).thenReturn(userData);
Map<String, Object> param = new HashMap<String, Object>() {{
put("user", new HashMap<String, Object>() {{

View File

@@ -0,0 +1,52 @@
package io.spring.application.article;
import io.spring.core.article.Article;
import io.spring.core.article.ArticleRepository;
import io.spring.core.user.User;
import io.spring.core.user.UserRepository;
import io.spring.infrastructure.article.MyBatisArticleRepository;
import io.spring.infrastructure.user.MyBatisUserRepository;
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.boot.test.context.SpringBootTest;
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.hamcrest.CoreMatchers.notNullValue;
import static org.junit.Assert.*;
@RunWith(SpringRunner.class)
@MybatisTest
@Import({ArticleQueryService.class, MyBatisUserRepository.class, MyBatisArticleRepository.class})
public class ArticleQueryServiceTest {
@Autowired
private ArticleQueryService queryService;
@Autowired
private ArticleRepository articleRepository;
@Autowired
private UserRepository userRepository;
@Test
public void should_fetch_article_success() throws Exception {
User user = new User("aisensiy@gmail.com", "aisensiy", "123", "", "");
userRepository.save(user);
Article article = new Article("test", "test", "desc", "body", new String[]{"java", "spring"}, user.getId());
articleRepository.save(article);
Optional<ArticleData> optional = queryService.findById(article.getId(), user);
assertThat(optional.isPresent(), is(true));
ArticleData fetched = optional.get();
assertThat(fetched.getFavoritesCount(), is(0));
assertThat(fetched.isFavorited(), is(false));
assertThat(fetched.getCreatedAt(), notNullValue());
assertThat(fetched.getUpdatedAt(), notNullValue());
}
}

View File

@@ -0,0 +1,50 @@
package io.spring.infrastructure.article;
import io.spring.core.article.Article;
import io.spring.core.article.ArticleRepository;
import io.spring.core.article.Tag;
import io.spring.core.user.User;
import io.spring.core.user.UserRepository;
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({MyBatisArticleRepository.class, MyBatisUserRepository.class})
public class MyBatisArticleRepositoryTest {
@Autowired
private ArticleRepository articleRepository;
@Autowired
private UserRepository userRepository;
private User user;
@Before
public void setUp() throws Exception {
user = new User("aisensiy@gmail.com", "aisensiy", "123", "bio", "default");
userRepository.save(user);
}
@Test
public void should_create_and_fetch_article_success() throws Exception {
Article article = new Article("test", "test", "desc", "body", new String[]{"java", "spring"}, user.getId());
articleRepository.save(article);
Optional<Article> optional = articleRepository.findById(article.getId());
assertThat(optional.isPresent(), is(true));
assertThat(optional.get(), is(article));
assertThat(optional.get().getTags().contains(new Tag("java")), is(true));
assertThat(optional.get().getTags().contains(new Tag("spring")), is(true));
}
}