list recent article

This commit is contained in:
aisensiy
2017-08-17 14:27:29 +08:00
parent 0c12253ef0
commit 0358fde252
11 changed files with 293 additions and 34 deletions

View File

@@ -0,0 +1,39 @@
package io.spring;
import io.spring.application.article.ArticleData;
import io.spring.application.profile.ProfileData;
import io.spring.core.article.Article;
import io.spring.core.user.User;
import org.joda.time.DateTime;
import java.util.ArrayList;
import java.util.Arrays;
public class TestHelper {
public static ArticleData articleDataFixture(String seed, User user) {
DateTime now = new DateTime();
return new ArticleData(
seed + "id",
"title-" + seed,
"title " + seed,
"desc " + seed,
"body " + seed, false, 0, now, now, new ArrayList<>(),
new ProfileData(user.getId(), user.getUsername(), user.getBio(), user.getImage(), false));
}
public static ArticleData getArticleDataFromArticleAndUser(Article article, User user) {
DateTime time = new DateTime();
return new ArticleData(
article.getId(),
article.getSlug(),
article.getTitle(),
article.getDescription(),
article.getBody(),
false,
0,
time,
time,
Arrays.asList("joda"),
new ProfileData(user.getId(), user.getUsername(), user.getBio(), user.getImage(), false));
}
}

View File

@@ -1,6 +1,7 @@
package io.spring.api;
import io.restassured.RestAssured;
import io.spring.TestHelper;
import io.spring.application.article.ArticleData;
import io.spring.application.article.ArticleQueryService;
import io.spring.application.profile.ProfileData;
@@ -42,17 +43,9 @@ public class ArticlesApiTest extends TestWithCurrentUser {
@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();
}
@Test
@@ -124,18 +117,7 @@ public class ArticlesApiTest extends TestWithCurrentUser {
Article article = new Article("Test New Article", "Desc", "Body", new String[]{"java", "spring", "jpg"}, user.getId());
DateTime time = new DateTime();
ArticleData articleData = new ArticleData(
article.getId(),
article.getSlug(),
article.getTitle(),
article.getDescription(),
article.getBody(),
false,
0,
time,
time,
Arrays.asList("joda"),
new ProfileData(user.getId(), user.getUsername(), user.getBio(), user.getImage(), false));
ArticleData articleData = TestHelper.getArticleDataFromArticleAndUser(article, user);
when(articleQueryService.findBySlug(eq(slug), eq(null))).thenReturn(Optional.of(articleData));
@@ -168,18 +150,7 @@ public class ArticlesApiTest extends TestWithCurrentUser {
Article article = new Article(title, description, body, new String[]{"java", "spring", "jpg"}, user.getId());
DateTime time = new DateTime();
ArticleData articleData = new ArticleData(
article.getId(),
article.getSlug(),
article.getTitle(),
article.getDescription(),
article.getBody(),
false,
0,
time,
time,
Arrays.asList("joda"),
new ProfileData(user.getId(), user.getUsername(), user.getBio(), user.getImage(), false));
ArticleData articleData = TestHelper.getArticleDataFromArticleAndUser(article, user);
when(articleRepository.findBySlug(eq(article.getSlug()))).thenReturn(Optional.of(article));
when(articleQueryService.findBySlug(eq(article.getSlug()), eq(user))).thenReturn(Optional.of(articleData));

View File

@@ -0,0 +1,46 @@
package io.spring.api;
import io.restassured.RestAssured;
import io.spring.application.Page;
import io.spring.application.article.ArticleDataList;
import io.spring.application.article.ArticleQueryService;
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 static io.spring.TestHelper.articleDataFixture;
import static java.util.Arrays.asList;
import static org.mockito.Matchers.eq;
import static org.mockito.Mockito.when;
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
@RunWith(SpringRunner.class)
public class ListArticleApiTest extends TestWithCurrentUser {
@MockBean
private ArticleQueryService articleQueryService;
@LocalServerPort
private int port;
@Before
public void setUp() throws Exception {
RestAssured.port = port;
userFixture();
}
@Test
public void should_get_default_article_list() throws Exception {
ArticleDataList articleDataList = new ArticleDataList(
asList(articleDataFixture("1", user), articleDataFixture("2", user)), 2);
when(articleQueryService.findRecentArticles(eq(null), eq(null), eq(null), eq(new Page(0, 20)))).thenReturn(articleDataList);
RestAssured.when()
.get("/articles")
.prettyPeek()
.then()
.statusCode(200);
}
}

View File

@@ -1,5 +1,6 @@
package io.spring.application.article;
import io.spring.application.Page;
import io.spring.core.article.Article;
import io.spring.core.article.ArticleRepository;
import io.spring.core.favorite.ArticleFavorite;
@@ -9,6 +10,7 @@ import io.spring.core.user.UserRepository;
import io.spring.infrastructure.article.MyBatisArticleRepository;
import io.spring.infrastructure.favorite.MyBatisArticleFavoriteRepository;
import io.spring.infrastructure.user.MyBatisUserRepository;
import org.joda.time.DateTime;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
@@ -75,4 +77,63 @@ public class ArticleQueryServiceTest {
assertThat(articleData.getFavoritesCount(), is(1));
assertThat(articleData.isFavorited(), is(true));
}
@Test
public void should_get_default_article_list() throws Exception {
Article anotherArticle = new Article("new article", "desc", "body", new String[]{"test"}, user.getId(), new DateTime().minusHours(1));
articleRepository.save(anotherArticle);
ArticleDataList recentArticles = queryService.findRecentArticles(null, null, null, new Page());
assertThat(recentArticles.getCount(), is(2));
assertThat(recentArticles.getArticleDatas().size(), is(2));
assertThat(recentArticles.getArticleDatas().get(0).getId(), is(article.getId()));
ArticleDataList nodata = queryService.findRecentArticles(null, null, null, new Page(2, 10));
assertThat(nodata.getCount(), is(2));
assertThat(nodata.getArticleDatas().size(), is(0));
}
@Test
public void should_query_article_by_author() throws Exception {
User anotherUser = new User("other@email.com", "other", "123", "", "");
userRepository.save(anotherUser);
Article anotherArticle = new Article("new article", "desc", "body", new String[]{"test"}, anotherUser.getId());
articleRepository.save(anotherArticle);
ArticleDataList recentArticles = queryService.findRecentArticles(null, user.getId(), null, new Page());
assertThat(recentArticles.getArticleDatas().size(), is(1));
assertThat(recentArticles.getCount(), is(1));
}
@Test
public void should_query_article_by_favorite() throws Exception {
User anotherUser = new User("other@email.com", "other", "123", "", "");
userRepository.save(anotherUser);
Article anotherArticle = new Article("new article", "desc", "body", new String[]{"test"}, anotherUser.getId());
articleRepository.save(anotherArticle);
ArticleFavorite articleFavorite = new ArticleFavorite(article.getId(), anotherUser.getId());
articleFavoriteRepository.save(articleFavorite);
ArticleDataList recentArticles = queryService.findRecentArticles(null, null, anotherUser.getId(), new Page());
assertThat(recentArticles.getArticleDatas().size(), is(1));
assertThat(recentArticles.getCount(), is(1));
assertThat(recentArticles.getArticleDatas().get(0).getId(), is(article.getId()));
}
@Test
public void should_query_article_by_tag() throws Exception {
Article anotherArticle = new Article("new article", "desc", "body", new String[]{"test"}, user.getId());
articleRepository.save(anotherArticle);
ArticleDataList recentArticles = queryService.findRecentArticles("spring", null, null, new Page());
assertThat(recentArticles.getArticleDatas().size(), is(1));
assertThat(recentArticles.getCount(), is(1));
assertThat(recentArticles.getArticleDatas().get(0).getId(), is(article.getId()));
ArticleDataList notag = queryService.findRecentArticles("notag", null, null, new Page());
assertThat(notag.getCount(), is(0));
}
}