tags = new ArrayList<>();
+ tags.add(tagDTO);
+
+ testArticle = ArticleDTO.builder()
+ .articleAuthor(testAuthor)
+ .articleAuthorId(testAuthor.getIdUser())
+ .articleContent("Test")
+ .articleLink("Test")
+ .articlePerfect("0")
+ .articlePermalink("Test")
+ .articleAuthorName(testAuthor.getUserNickname())
+ .articleCommentCount(0)
+ .articleStatus("0")
+ .articleTags("Test")
+ .articleContentHtml("Test
")
+ .articleTitle("Test")
+ .articleType("0")
+ .articlePreviewContent("Test")
+ .articleSponsorCount(12)
+ .articlePermalink("Test")
+ .articleViewCount(0)
+ .tags(tags)
+ .build();
+ }
+
+ @Autowired
+ private ArticleService articleService;
+
+ /**
+ * 将测试用的Article数据插入数据库中(会回滚的:))
+ *
+ * 测试数据是否会返回Article的Id,并且Id会填充到测试数据中
+ * @throws UnsupportedEncodingException
+ * @throws BaseApiException
+ */
+ @Test
+ @BeforeEach
+ public void postArticle() throws UnsupportedEncodingException, BaseApiException {
+ Long articleId = articleService.postArticle(testArticle, testUser);
+ testArticle.setIdArticle(articleId);
+ assertNotNull(articleId);
+ }
+
+ /**
+ * 测试条件查询
+ *
+ * 无参数时返回所有Article
+ */
+ @Test
+ void findArticles() {
+ // 无参数时返回参数不应为EmptyList
+ List articlesAll = articleService.findArticles(new ArticleSearchDTO());
+ assertNotNull(articlesAll);
+ assertNotEquals(Collections.emptyList(), articlesAll);
+
+ // 测试条件查询是否含有目标数据
+ ArticleSearchDTO articleSearchDTO = new ArticleSearchDTO();
+ articleSearchDTO.setSearchText(testArticle.getArticleContent());
+ Map idArticleMap = articleService.findArticles(articleSearchDTO)
+ .stream()
+ .collect(Collectors.toMap(ArticleDTO::getIdArticle, item -> item));
+ assertNotNull(idArticleMap.get(testArticle.getIdArticle()));
+ }
+
+ /**
+ * 测试通过Id获取Article
+ */
+ @Test
+ void findArticleDTOById() {
+ // 测试参数为
+ ArticleDTO articleDTOByIdNull = articleService.findArticleDTOById(null, 0);
+ assertNull(articleDTOByIdNull);
+ ArticleDTO articleDTOById = articleService.findArticleDTOById(testArticle.getIdArticle(), 0);
+ assertNotNull(articleDTOById);
+ }
+
+// @Test
+// void findArticlesByTopicUri() {
+// List articlesByTopicUri = articleService.findArticlesByTopicUri();
+// }
+
+// @Test
+// void findArticlesByTagName() {
+// List articlesByTagName = articleService.findArticlesByTagName();
+// }
+
+ /**
+ * 通过UserId获取对应的Articles
+ */
+ @Test
+ void findUserArticlesByIdUser() {
+ List userArticlesByIdUser = articleService.findUserArticlesByIdUser(testArticle.getArticleAuthorId());
+ assertNotEquals(Collections.emptyList(), userArticlesByIdUser);
+ }
+
+ /**
+ * 测试文章浏览增量后是否成功
+ */
+ @Test
+ void incrementArticleViewCount() {
+ ArticleDTO articleDTOByIdBefore = articleService.findArticleDTOById(testArticle.getIdArticle(), 0);
+ articleService.incrementArticleViewCount(testArticle.getIdArticle());
+ ArticleDTO articleDTOByIdAfter = articleService.findArticleDTOById(testArticle.getIdArticle(), 0);
+ assertEquals(articleDTOByIdBefore.getArticleViewCount() + 1, articleDTOByIdAfter.getArticleViewCount());
+ }
+
+// @Test
+// void share() {
+// String share = articleService.share();
+// }
+
+// @Test
+// void findDrafts() {
+// List drafts = articleService.findDrafts(testArticle.getArticleAuthorId());
+// }
+
+// @Test
+// void findArticlesByIdPortfolio() {
+// List articlesByIdPortfolio = articleService.findArticlesByIdPortfolio();
+// }
+
+// @Test
+// void selectUnbindArticles() {
+// List articleDTOS = articleService.selectUnbindArticles();
+// }
+
+// @Test
+// void findAnnouncements() {
+// List announcements = articleService.findAnnouncements();
+// }
+
+// @Test
+// void updateTags() {
+// Boolean aBoolean = articleService.updateTags();
+// }
+
+ /**
+ * 测试更新文章为优选\非优选
+ */
+ @Test
+ void updatePerfect() {
+ articleService.updatePerfect(testArticle.getIdArticle(), "1");
+ ArticleDTO articleDTOByIdAfter1 = articleService.findArticleDTOById(testArticle.getIdArticle(), 0);
+ assertEquals("1", articleDTOByIdAfter1.getArticlePerfect());
+
+ articleService.updatePerfect(testArticle.getIdArticle(), "0");
+ ArticleDTO articleDTOByIdAfter2 = articleService.findArticleDTOById(testArticle.getIdArticle(), 0);
+ assertEquals("0", articleDTOByIdAfter2.getArticlePerfect());
+ }
+
+ /**
+ * 测试数据删除是否成功
+ *
+ * 运行该测试时,可能会产生以下错误:
+ * cn.hutool.core.io.IORuntimeException: Path [xxxxxxx] is not directory!
+ * 这是由于Lucene的路径通配符默认为linux的,解决方式:
+ * 将ArticleIndexUtil.deleteIndex()方法中的PATH改为WINDOW_PATH即可 :)
+ * @throws BaseApiException 基础Api错误
+ */
+ @Test
+ void delete() throws BaseApiException {
+ articleService.delete(testArticle.getIdArticle());
+ ArticleDTO articleDTOByIdAfter = articleService.findArticleDTOById(testArticle.getIdArticle(), 0);
+ assertNull(articleDTOByIdAfter);
+ }
+}
\ No newline at end of file