feed and tag
This commit is contained in:
@@ -22,7 +22,6 @@ public class TestHelper {
|
||||
}
|
||||
|
||||
public static ArticleData getArticleDataFromArticleAndUser(Article article, User user) {
|
||||
DateTime time = new DateTime();
|
||||
return new ArticleData(
|
||||
article.getId(),
|
||||
article.getSlug(),
|
||||
@@ -31,8 +30,8 @@ public class TestHelper {
|
||||
article.getBody(),
|
||||
false,
|
||||
0,
|
||||
time,
|
||||
time,
|
||||
article.getCreatedAt(),
|
||||
article.getUpdatedAt(),
|
||||
Arrays.asList("joda"),
|
||||
new ProfileData(user.getId(), user.getUsername(), user.getBio(), user.getImage(), false));
|
||||
}
|
||||
|
||||
@@ -46,6 +46,7 @@ public class ArticlesApiTest extends TestWithCurrentUser {
|
||||
@Before
|
||||
public void setUp() throws Exception {
|
||||
RestAssured.port = port;
|
||||
userFixture();
|
||||
}
|
||||
|
||||
@Test
|
||||
@@ -114,9 +115,8 @@ public class ArticlesApiTest extends TestWithCurrentUser {
|
||||
@Test
|
||||
public void should_read_article_success() throws Exception {
|
||||
String slug = "test-new-article";
|
||||
Article article = new Article("Test New Article", "Desc", "Body", new String[]{"java", "spring", "jpg"}, user.getId());
|
||||
|
||||
DateTime time = new DateTime();
|
||||
Article article = new Article("Test New Article", "Desc", "Body", new String[]{"java", "spring", "jpg"}, user.getId(), time);
|
||||
ArticleData articleData = TestHelper.getArticleDataFromArticleAndUser(article, user);
|
||||
|
||||
when(articleQueryService.findBySlug(eq(slug), eq(null))).thenReturn(Optional.of(articleData));
|
||||
|
||||
@@ -12,6 +12,7 @@ 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.restassured.RestAssured.given;
|
||||
import static io.spring.TestHelper.articleDataFixture;
|
||||
import static java.util.Arrays.asList;
|
||||
import static org.mockito.Matchers.eq;
|
||||
@@ -36,11 +37,35 @@ public class ListArticleApiTest extends TestWithCurrentUser {
|
||||
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);
|
||||
when(articleQueryService.findRecentArticles(eq(null), eq(null), eq(null), eq(new Page(0, 20)), eq(null))).thenReturn(articleDataList);
|
||||
RestAssured.when()
|
||||
.get("/articles")
|
||||
.prettyPeek()
|
||||
.then()
|
||||
.statusCode(200);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void should_get_feeds_401_without_login() throws Exception {
|
||||
RestAssured.when()
|
||||
.get("/articles/feed")
|
||||
.prettyPeek()
|
||||
.then()
|
||||
.statusCode(401);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void should_get_feeds_success() throws Exception {
|
||||
ArticleDataList articleDataList = new ArticleDataList(
|
||||
asList(articleDataFixture("1", user), articleDataFixture("2", user)), 2);
|
||||
when(articleQueryService.findUserFeed(eq(user), eq(new Page(0, 20)))).thenReturn(articleDataList);
|
||||
|
||||
given()
|
||||
.header("Authorization", "Token " + token)
|
||||
.when()
|
||||
.get("/articles/feed")
|
||||
.prettyPeek()
|
||||
.then()
|
||||
.statusCode(200);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -5,6 +5,7 @@ import io.spring.core.article.Article;
|
||||
import io.spring.core.article.ArticleRepository;
|
||||
import io.spring.core.favorite.ArticleFavorite;
|
||||
import io.spring.core.favorite.ArticleFavoriteRepository;
|
||||
import io.spring.core.user.FollowRelation;
|
||||
import io.spring.core.user.User;
|
||||
import io.spring.core.user.UserRepository;
|
||||
import io.spring.infrastructure.article.MyBatisArticleRepository;
|
||||
@@ -19,7 +20,6 @@ import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.context.annotation.Import;
|
||||
import org.springframework.test.context.junit4.SpringRunner;
|
||||
|
||||
import java.util.Arrays;
|
||||
import java.util.Optional;
|
||||
|
||||
import static org.hamcrest.CoreMatchers.anyOf;
|
||||
@@ -29,7 +29,11 @@ import static org.junit.Assert.*;
|
||||
|
||||
@RunWith(SpringRunner.class)
|
||||
@MybatisTest
|
||||
@Import({ArticleQueryService.class, MyBatisUserRepository.class, MyBatisArticleRepository.class, MyBatisArticleFavoriteRepository.class})
|
||||
@Import({
|
||||
ArticleQueryService.class,
|
||||
MyBatisUserRepository.class,
|
||||
MyBatisArticleRepository.class,
|
||||
MyBatisArticleFavoriteRepository.class})
|
||||
public class ArticleQueryServiceTest {
|
||||
@Autowired
|
||||
private ArticleQueryService queryService;
|
||||
@@ -83,12 +87,12 @@ public class ArticleQueryServiceTest {
|
||||
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());
|
||||
ArticleDataList recentArticles = queryService.findRecentArticles(null, null, null, new Page(), user);
|
||||
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));
|
||||
ArticleDataList nodata = queryService.findRecentArticles(null, null, null, new Page(2, 10), user);
|
||||
assertThat(nodata.getCount(), is(2));
|
||||
assertThat(nodata.getArticleDatas().size(), is(0));
|
||||
}
|
||||
@@ -101,7 +105,7 @@ public class ArticleQueryServiceTest {
|
||||
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());
|
||||
ArticleDataList recentArticles = queryService.findRecentArticles(null, user.getId(), null, new Page(), user);
|
||||
assertThat(recentArticles.getArticleDatas().size(), is(1));
|
||||
assertThat(recentArticles.getCount(), is(1));
|
||||
}
|
||||
@@ -117,10 +121,13 @@ public class ArticleQueryServiceTest {
|
||||
ArticleFavorite articleFavorite = new ArticleFavorite(article.getId(), anotherUser.getId());
|
||||
articleFavoriteRepository.save(articleFavorite);
|
||||
|
||||
ArticleDataList recentArticles = queryService.findRecentArticles(null, null, anotherUser.getId(), new Page());
|
||||
ArticleDataList recentArticles = queryService.findRecentArticles(null, null, anotherUser.getId(), new Page(), anotherUser);
|
||||
assertThat(recentArticles.getArticleDatas().size(), is(1));
|
||||
assertThat(recentArticles.getCount(), is(1));
|
||||
assertThat(recentArticles.getArticleDatas().get(0).getId(), is(article.getId()));
|
||||
ArticleData articleData = recentArticles.getArticleDatas().get(0);
|
||||
assertThat(articleData.getId(), is(article.getId()));
|
||||
assertThat(articleData.getFavoritesCount(), is(1));
|
||||
assertThat(articleData.isFavorited(), is(true));
|
||||
}
|
||||
|
||||
@Test
|
||||
@@ -128,12 +135,43 @@ public class ArticleQueryServiceTest {
|
||||
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());
|
||||
ArticleDataList recentArticles = queryService.findRecentArticles("spring", null, null, new Page(), user);
|
||||
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());
|
||||
ArticleDataList notag = queryService.findRecentArticles("notag", null, null, new Page(), user);
|
||||
assertThat(notag.getCount(), is(0));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void should_show_following_if_user_followed_author() throws Exception {
|
||||
User anotherUser = new User("other@email.com", "other", "123", "", "");
|
||||
userRepository.save(anotherUser);
|
||||
|
||||
FollowRelation followRelation = new FollowRelation(anotherUser.getId(), user.getId());
|
||||
userRepository.saveRelation(followRelation);
|
||||
|
||||
ArticleDataList recentArticles = queryService.findRecentArticles(null, null, null, new Page(), anotherUser);
|
||||
assertThat(recentArticles.getCount(), is(1));
|
||||
ArticleData articleData = recentArticles.getArticleDatas().get(0);
|
||||
assertThat(articleData.getProfileData().isFollowing(), is(true));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void should_get_user_feed() throws Exception {
|
||||
User anotherUser = new User("other@email.com", "other", "123", "", "");
|
||||
userRepository.save(anotherUser);
|
||||
|
||||
FollowRelation followRelation = new FollowRelation(anotherUser.getId(), user.getId());
|
||||
userRepository.saveRelation(followRelation);
|
||||
|
||||
ArticleDataList userFeed = queryService.findUserFeed(user, new Page());
|
||||
assertThat(userFeed.getCount(), is(0));
|
||||
|
||||
ArticleDataList anotherUserFeed = queryService.findUserFeed(anotherUser, new Page());
|
||||
assertThat(anotherUserFeed.getCount(), is(1));
|
||||
ArticleData articleData = anotherUserFeed.getArticleDatas().get(0);
|
||||
assertThat(articleData.getProfileData().isFollowing(), is(true));
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,33 @@
|
||||
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;
|
||||
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 static org.hamcrest.CoreMatchers.is;
|
||||
import static org.junit.Assert.*;
|
||||
|
||||
@RunWith(SpringRunner.class)
|
||||
@MybatisTest
|
||||
@Import({TagsQueryService.class, MyBatisArticleRepository.class})
|
||||
public class TagsQueryServiceTest {
|
||||
@Autowired
|
||||
private TagsQueryService tagsQueryService;
|
||||
|
||||
@Autowired
|
||||
private ArticleRepository articleRepository;
|
||||
|
||||
@Test
|
||||
public void should_get_all_tags() throws Exception {
|
||||
articleRepository.save(new Article("test", "test", "test", new String[]{"java"}, "123"));
|
||||
assertThat(tagsQueryService.allTags().contains("java"), is(true));
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user