2017-08-15 09:47:18 +07:00
|
|
|
package io.spring.application.article;
|
|
|
|
|
2017-08-18 16:08:27 +07:00
|
|
|
import io.spring.application.ArticleQueryService;
|
2017-08-17 13:27:29 +07:00
|
|
|
import io.spring.application.Page;
|
2017-08-18 16:08:27 +07:00
|
|
|
import io.spring.application.data.ArticleData;
|
|
|
|
import io.spring.application.data.ArticleDataList;
|
2017-08-15 09:47:18 +07:00
|
|
|
import io.spring.core.article.Article;
|
|
|
|
import io.spring.core.article.ArticleRepository;
|
2017-08-16 10:51:20 +07:00
|
|
|
import io.spring.core.favorite.ArticleFavorite;
|
|
|
|
import io.spring.core.favorite.ArticleFavoriteRepository;
|
2017-08-17 16:17:37 +07:00
|
|
|
import io.spring.core.user.FollowRelation;
|
2017-08-15 09:47:18 +07:00
|
|
|
import io.spring.core.user.User;
|
|
|
|
import io.spring.core.user.UserRepository;
|
2017-08-18 16:08:27 +07:00
|
|
|
import io.spring.infrastructure.repository.MyBatisArticleRepository;
|
|
|
|
import io.spring.infrastructure.repository.MyBatisArticleFavoriteRepository;
|
|
|
|
import io.spring.infrastructure.repository.MyBatisUserRepository;
|
2017-08-17 13:27:29 +07:00
|
|
|
import org.joda.time.DateTime;
|
2017-08-16 10:51:20 +07:00
|
|
|
import org.junit.Before;
|
2017-08-15 09:47:18 +07:00
|
|
|
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.hamcrest.CoreMatchers.notNullValue;
|
2017-08-18 16:08:27 +07:00
|
|
|
import static org.junit.Assert.assertThat;
|
2017-08-15 09:47:18 +07:00
|
|
|
|
|
|
|
@RunWith(SpringRunner.class)
|
|
|
|
@MybatisTest
|
2017-08-17 16:17:37 +07:00
|
|
|
@Import({
|
|
|
|
ArticleQueryService.class,
|
|
|
|
MyBatisUserRepository.class,
|
|
|
|
MyBatisArticleRepository.class,
|
|
|
|
MyBatisArticleFavoriteRepository.class})
|
2017-08-15 09:47:18 +07:00
|
|
|
public class ArticleQueryServiceTest {
|
|
|
|
@Autowired
|
|
|
|
private ArticleQueryService queryService;
|
|
|
|
|
|
|
|
@Autowired
|
|
|
|
private ArticleRepository articleRepository;
|
|
|
|
|
|
|
|
@Autowired
|
|
|
|
private UserRepository userRepository;
|
|
|
|
|
2017-08-16 10:51:20 +07:00
|
|
|
@Autowired
|
|
|
|
private ArticleFavoriteRepository articleFavoriteRepository;
|
2017-08-15 09:47:18 +07:00
|
|
|
|
2017-08-16 10:51:20 +07:00
|
|
|
private User user;
|
|
|
|
private Article article;
|
|
|
|
|
|
|
|
@Before
|
|
|
|
public void setUp() throws Exception {
|
|
|
|
user = new User("aisensiy@gmail.com", "aisensiy", "123", "", "");
|
|
|
|
userRepository.save(user);
|
2017-08-18 16:08:27 +07:00
|
|
|
article = new Article("test", "desc", "body", new String[]{"java", "spring"}, user.getId(), new DateTime());
|
2017-08-15 09:47:18 +07:00
|
|
|
articleRepository.save(article);
|
2017-08-16 10:51:20 +07:00
|
|
|
}
|
|
|
|
|
|
|
|
@Test
|
|
|
|
public void should_fetch_article_success() throws Exception {
|
2017-08-15 09:47:18 +07:00
|
|
|
|
|
|
|
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());
|
2017-08-16 14:58:51 +07:00
|
|
|
assertThat(fetched.getTagList().contains("java"), is(true));
|
2017-08-15 09:47:18 +07:00
|
|
|
}
|
2017-08-16 10:51:20 +07:00
|
|
|
|
|
|
|
@Test
|
|
|
|
public void should_get_article_with_right_favorite_and_favorite_count() throws Exception {
|
|
|
|
User anotherUser = new User("other@test.com", "other", "123", "", "");
|
|
|
|
userRepository.save(anotherUser);
|
|
|
|
articleFavoriteRepository.save(new ArticleFavorite(article.getId(), anotherUser.getId()));
|
|
|
|
|
|
|
|
ArticleData articleData = queryService.findById(article.getId(), anotherUser).get();
|
|
|
|
assertThat(articleData.getFavoritesCount(), is(1));
|
|
|
|
assertThat(articleData.isFavorited(), is(true));
|
|
|
|
}
|
2017-08-17 13:27:29 +07:00
|
|
|
|
|
|
|
@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);
|
|
|
|
|
2017-08-17 16:17:37 +07:00
|
|
|
ArticleDataList recentArticles = queryService.findRecentArticles(null, null, null, new Page(), user);
|
2017-08-17 13:27:29 +07:00
|
|
|
assertThat(recentArticles.getCount(), is(2));
|
|
|
|
assertThat(recentArticles.getArticleDatas().size(), is(2));
|
|
|
|
assertThat(recentArticles.getArticleDatas().get(0).getId(), is(article.getId()));
|
|
|
|
|
2017-08-17 16:17:37 +07:00
|
|
|
ArticleDataList nodata = queryService.findRecentArticles(null, null, null, new Page(2, 10), user);
|
2017-08-17 13:27:29 +07:00
|
|
|
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);
|
|
|
|
|
2017-08-19 08:55:24 +07:00
|
|
|
ArticleDataList recentArticles = queryService.findRecentArticles(null, user.getUsername(), null, new Page(), user);
|
2017-08-17 13:27:29 +07:00
|
|
|
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);
|
|
|
|
|
2017-08-19 08:55:24 +07:00
|
|
|
ArticleDataList recentArticles = queryService.findRecentArticles(null, null, anotherUser.getUsername(), new Page(), anotherUser);
|
2017-08-17 13:27:29 +07:00
|
|
|
assertThat(recentArticles.getArticleDatas().size(), is(1));
|
|
|
|
assertThat(recentArticles.getCount(), is(1));
|
2017-08-17 16:17:37 +07:00
|
|
|
ArticleData articleData = recentArticles.getArticleDatas().get(0);
|
|
|
|
assertThat(articleData.getId(), is(article.getId()));
|
|
|
|
assertThat(articleData.getFavoritesCount(), is(1));
|
|
|
|
assertThat(articleData.isFavorited(), is(true));
|
2017-08-17 13:27:29 +07:00
|
|
|
}
|
|
|
|
|
|
|
|
@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);
|
|
|
|
|
2017-08-17 16:17:37 +07:00
|
|
|
ArticleDataList recentArticles = queryService.findRecentArticles("spring", null, null, new Page(), user);
|
2017-08-17 13:27:29 +07:00
|
|
|
assertThat(recentArticles.getArticleDatas().size(), is(1));
|
|
|
|
assertThat(recentArticles.getCount(), is(1));
|
|
|
|
assertThat(recentArticles.getArticleDatas().get(0).getId(), is(article.getId()));
|
|
|
|
|
2017-08-17 16:17:37 +07:00
|
|
|
ArticleDataList notag = queryService.findRecentArticles("notag", null, null, new Page(), user);
|
2017-08-17 13:27:29 +07:00
|
|
|
assertThat(notag.getCount(), is(0));
|
|
|
|
}
|
2017-08-17 16:17:37 +07:00
|
|
|
|
|
|
|
@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));
|
|
|
|
}
|
2017-08-15 09:47:18 +07:00
|
|
|
}
|