From a907571ac6d23866d3614d3d68ed05dcac0f4264 Mon Sep 17 00:00:00 2001 From: "kould.li" Date: Thu, 4 Aug 2022 15:51:09 +0800 Subject: [PATCH] =?UTF-8?q?feat(test):=20=E6=96=B0=E5=A2=9EArticleService?= =?UTF-8?q?=E9=83=A8=E5=88=86=E5=8D=95=E5=85=83=E6=B5=8B=E8=AF=95,?= =?UTF-8?q?=E9=83=A8=E5=88=86=E5=8A=9F=E8=83=BD=E4=BC=98=E5=8C=96,sql?= =?UTF-8?q?=E6=96=87=E4=BB=B6=E6=96=B0=E5=A2=9E=E6=B5=8B=E8=AF=95=E6=95=B0?= =?UTF-8?q?=E6=8D=AE?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 涵盖ArticleService单元测试方法: postArticle findArticles findArticleDTOById findUserArticlesByIdUser incrementArticleViewCount updatePerfect delete ArticleService将用户获取逻辑转移到Controller中 sql文件新增用户测试数据 --- pom.xml | 5 + .../java/com/rymcu/forest/dto/ArticleDTO.java | 6 + .../com/rymcu/forest/dto/ArticleTagDTO.java | 6 + .../java/com/rymcu/forest/dto/Author.java | 6 + .../service/impl/LuceneServiceImpl.java | 3 +- .../impl/PortfolioLuceneServiceImpl.java | 3 +- .../forest/lucene/util/ArticleIndexUtil.java | 3 + .../rymcu/forest/service/ArticleService.java | 13 +- .../com/rymcu/forest/service/TagService.java | 2 +- .../service/impl/ArticleServiceImpl.java | 20 +- .../forest/service/impl/TagServiceImpl.java | 9 +- .../web/api/article/ArticleController.java | 30 ++- src/main/resources/static/forest.sql | 4 + .../forest/service/ArticleServiceTest.java | 229 ++++++++++++++++++ 14 files changed, 303 insertions(+), 36 deletions(-) create mode 100644 src/test/java/com/rymcu/forest/service/ArticleServiceTest.java diff --git a/pom.xml b/pom.xml index 336080b..cb92596 100644 --- a/pom.xml +++ b/pom.xml @@ -292,6 +292,11 @@ + + junit + junit + test + diff --git a/src/main/java/com/rymcu/forest/dto/ArticleDTO.java b/src/main/java/com/rymcu/forest/dto/ArticleDTO.java index baad171..4b7e370 100644 --- a/src/main/java/com/rymcu/forest/dto/ArticleDTO.java +++ b/src/main/java/com/rymcu/forest/dto/ArticleDTO.java @@ -2,7 +2,10 @@ package com.rymcu.forest.dto; import com.alibaba.fastjson.annotation.JSONField; import com.fasterxml.jackson.annotation.JsonFormat; +import lombok.AllArgsConstructor; +import lombok.Builder; import lombok.Data; +import lombok.NoArgsConstructor; import java.util.Date; import java.util.List; @@ -11,6 +14,9 @@ import java.util.List; * @author ronger */ @Data +@Builder +@NoArgsConstructor +@AllArgsConstructor public class ArticleDTO { @JsonFormat(shape = JsonFormat.Shape.STRING) private Long idArticle; diff --git a/src/main/java/com/rymcu/forest/dto/ArticleTagDTO.java b/src/main/java/com/rymcu/forest/dto/ArticleTagDTO.java index c43cc23..44585b2 100644 --- a/src/main/java/com/rymcu/forest/dto/ArticleTagDTO.java +++ b/src/main/java/com/rymcu/forest/dto/ArticleTagDTO.java @@ -1,12 +1,18 @@ package com.rymcu.forest.dto; import com.fasterxml.jackson.annotation.JsonFormat; +import lombok.AllArgsConstructor; +import lombok.Builder; import lombok.Data; +import lombok.NoArgsConstructor; /** * @author ronger */ @Data +@Builder +@NoArgsConstructor +@AllArgsConstructor public class ArticleTagDTO { @JsonFormat(shape = JsonFormat.Shape.STRING) private Long idArticleTag; diff --git a/src/main/java/com/rymcu/forest/dto/Author.java b/src/main/java/com/rymcu/forest/dto/Author.java index c0c06f8..a15cc53 100644 --- a/src/main/java/com/rymcu/forest/dto/Author.java +++ b/src/main/java/com/rymcu/forest/dto/Author.java @@ -1,12 +1,18 @@ package com.rymcu.forest.dto; import com.fasterxml.jackson.annotation.JsonFormat; +import lombok.AllArgsConstructor; +import lombok.Builder; import lombok.Data; +import lombok.NoArgsConstructor; /** * @author ronger */ @Data +@Builder +@NoArgsConstructor +@AllArgsConstructor public class Author { @JsonFormat(shape = JsonFormat.Shape.STRING) diff --git a/src/main/java/com/rymcu/forest/lucene/service/impl/LuceneServiceImpl.java b/src/main/java/com/rymcu/forest/lucene/service/impl/LuceneServiceImpl.java index 64c0bba..8d8b8d3 100644 --- a/src/main/java/com/rymcu/forest/lucene/service/impl/LuceneServiceImpl.java +++ b/src/main/java/com/rymcu/forest/lucene/service/impl/LuceneServiceImpl.java @@ -65,7 +65,8 @@ public class LuceneServiceImpl implements LuceneService { try { int totalCount = list.size(); int perThreadCount = 3000; - int threadCount = totalCount / perThreadCount + (totalCount % perThreadCount == 0 ? 0 : 1); + // 加1避免线程池的参数为0 + int threadCount = totalCount / perThreadCount + (totalCount % perThreadCount == 0 ? 0 : 1) + 1; ExecutorService pool = Executors.newFixedThreadPool(threadCount); CountDownLatch countDownLatch1 = new CountDownLatch(1); CountDownLatch countDownLatch2 = new CountDownLatch(threadCount); diff --git a/src/main/java/com/rymcu/forest/lucene/service/impl/PortfolioLuceneServiceImpl.java b/src/main/java/com/rymcu/forest/lucene/service/impl/PortfolioLuceneServiceImpl.java index 964ac3c..4d35c34 100644 --- a/src/main/java/com/rymcu/forest/lucene/service/impl/PortfolioLuceneServiceImpl.java +++ b/src/main/java/com/rymcu/forest/lucene/service/impl/PortfolioLuceneServiceImpl.java @@ -55,7 +55,8 @@ public class PortfolioLuceneServiceImpl implements PortfolioLuceneService { try { int totalCount = list.size(); int perThreadCount = 3000; - int threadCount = totalCount / perThreadCount + (totalCount % perThreadCount == 0 ? 0 : 1); + // 加1避免线程池的参数为0 + int threadCount = totalCount / perThreadCount + (totalCount % perThreadCount == 0 ? 0 : 1) + 1; ExecutorService pool = Executors.newFixedThreadPool(threadCount); CountDownLatch countDownLatch1 = new CountDownLatch(1); CountDownLatch countDownLatch2 = new CountDownLatch(threadCount); diff --git a/src/main/java/com/rymcu/forest/lucene/util/ArticleIndexUtil.java b/src/main/java/com/rymcu/forest/lucene/util/ArticleIndexUtil.java index ecbf6f4..9b3880e 100644 --- a/src/main/java/com/rymcu/forest/lucene/util/ArticleIndexUtil.java +++ b/src/main/java/com/rymcu/forest/lucene/util/ArticleIndexUtil.java @@ -24,6 +24,9 @@ public class ArticleIndexUtil { private static final String PATH = System.getProperty("user.dir") + StrUtil.SLASH + LucenePath.ARTICLE_INDEX_PATH; + private static final String WINDOW_PATH = + System.getProperty("user.dir") + StrUtil.BACKSLASH + "lucene\\index\\article"; + /** 删除所有运行中保存的索引 */ public static void deleteAllIndex() { if (FileUtil.exist(LucenePath.ARTICLE_INCREMENT_INDEX_PATH)) { diff --git a/src/main/java/com/rymcu/forest/service/ArticleService.java b/src/main/java/com/rymcu/forest/service/ArticleService.java index f483229..cca8f77 100644 --- a/src/main/java/com/rymcu/forest/service/ArticleService.java +++ b/src/main/java/com/rymcu/forest/service/ArticleService.java @@ -4,12 +4,11 @@ import com.rymcu.forest.core.service.Service; import com.rymcu.forest.dto.ArticleDTO; import com.rymcu.forest.dto.ArticleSearchDTO; import com.rymcu.forest.entity.Article; +import com.rymcu.forest.entity.User; import com.rymcu.forest.web.api.exception.BaseApiException; -import javax.servlet.http.HttpServletRequest; import java.io.UnsupportedEncodingException; import java.util.List; -import java.util.Map; /** * @author ronger @@ -55,12 +54,12 @@ public interface ArticleService extends Service
{ /** * 新增/更新文章 * @param article - * @param request + * @param user * @throws UnsupportedEncodingException * @throws BaseApiException * @return * */ - Long postArticle(ArticleDTO article, HttpServletRequest request) throws UnsupportedEncodingException, BaseApiException; + Long postArticle(ArticleDTO article, User user) throws UnsupportedEncodingException, BaseApiException; /** * 删除文章 @@ -86,10 +85,9 @@ public interface ArticleService extends Service
{ /** * 查询草稿文章类别 - * @throws BaseApiException * @return */ - List findDrafts() throws BaseApiException; + List findDrafts(Long userId); /** * 查询作品集下文章 @@ -111,11 +109,12 @@ public interface ArticleService extends Service
{ * 更新文章标签 * @param idArticle * @param tags + * @param userId * @return * @throws UnsupportedEncodingException * @throws BaseApiException */ - Boolean updateTags(Long idArticle, String tags) throws UnsupportedEncodingException, BaseApiException; + Boolean updateTags(Long idArticle, String tags, Long userId) throws UnsupportedEncodingException, BaseApiException; /** * 更新文章优选状态 diff --git a/src/main/java/com/rymcu/forest/service/TagService.java b/src/main/java/com/rymcu/forest/service/TagService.java index 0b36b76..9affe2d 100644 --- a/src/main/java/com/rymcu/forest/service/TagService.java +++ b/src/main/java/com/rymcu/forest/service/TagService.java @@ -23,7 +23,7 @@ public interface TagService extends Service { * @throws BaseApiException * @return * */ - Integer saveTagArticle(Article article, String articleContentHtml) throws UnsupportedEncodingException, BaseApiException; + Integer saveTagArticle(Article article, String articleContentHtml, Long userId) throws UnsupportedEncodingException, BaseApiException; /** * 清除未使用标签 diff --git a/src/main/java/com/rymcu/forest/service/impl/ArticleServiceImpl.java b/src/main/java/com/rymcu/forest/service/impl/ArticleServiceImpl.java index dc7c9ad..14f043c 100644 --- a/src/main/java/com/rymcu/forest/service/impl/ArticleServiceImpl.java +++ b/src/main/java/com/rymcu/forest/service/impl/ArticleServiceImpl.java @@ -101,16 +101,12 @@ public class ArticleServiceImpl extends AbstractService
implements Arti @Override @Transactional(rollbackFor = {UnsupportedEncodingException.class, BaseApiException.class}) - public Long postArticle(ArticleDTO article, HttpServletRequest request) throws UnsupportedEncodingException, BaseApiException { + public Long postArticle(ArticleDTO article, User user) throws UnsupportedEncodingException, BaseApiException { boolean isUpdate = false; String articleTitle = article.getArticleTitle(); String articleTags = article.getArticleTags(); String articleContent = article.getArticleContent(); String articleContentHtml = XssUtils.filterHtmlCode(article.getArticleContentHtml()); - User user = UserUtils.getCurrentUserByToken(); - if (Objects.isNull(user)) { - throw new BaseApiException(ErrorCode.INVALID_TOKEN); - } String reservedTag = checkTags(articleTags); boolean notification = false; if (StringUtils.isNotBlank(reservedTag)) { @@ -178,7 +174,7 @@ public class ArticleServiceImpl extends AbstractService
implements Arti newArticle.setArticlePermalink(domain + "/draft/" + newArticleId); newArticle.setArticleLink("/draft/" + newArticleId); } - tagService.saveTagArticle(newArticle, articleContentHtml); + tagService.saveTagArticle(newArticle, articleContentHtml, user.getIdUser()); if (StringUtils.isNotBlank(articleContentHtml)) { String previewContent = Html2TextUtil.getContent(articleContentHtml); @@ -237,12 +233,8 @@ public class ArticleServiceImpl extends AbstractService
implements Arti } @Override - public List findDrafts() throws BaseApiException { - User user = UserUtils.getCurrentUserByToken(); - if (Objects.isNull(user)) { - throw new BaseApiException(ErrorCode.INVALID_TOKEN); - } - List list = articleMapper.selectDrafts(user.getIdUser()); + public List findDrafts(Long userId) { + List list = articleMapper.selectDrafts(userId); list.forEach(articleDTO -> genArticle(articleDTO, 0)); return list; } @@ -263,14 +255,14 @@ public class ArticleServiceImpl extends AbstractService
implements Arti @Override @Transactional(rollbackFor = Exception.class) - public Boolean updateTags(Long idArticle, String tags) throws UnsupportedEncodingException, BaseApiException { + public Boolean updateTags(Long idArticle, String tags, Long userId) throws UnsupportedEncodingException, BaseApiException { Article article = articleMapper.selectByPrimaryKey(idArticle); if (!Objects.nonNull(article)) { throw new ContentNotExistException("更新失败,文章不存在!"); } article.setArticleTags(tags); articleMapper.updateArticleTags(idArticle, tags); - tagService.saveTagArticle(article, ""); + tagService.saveTagArticle(article, "", userId); return true; } diff --git a/src/main/java/com/rymcu/forest/service/impl/TagServiceImpl.java b/src/main/java/com/rymcu/forest/service/impl/TagServiceImpl.java index 7b6bfdf..c59c9a4 100644 --- a/src/main/java/com/rymcu/forest/service/impl/TagServiceImpl.java +++ b/src/main/java/com/rymcu/forest/service/impl/TagServiceImpl.java @@ -42,8 +42,7 @@ public class TagServiceImpl extends AbstractService implements TagService { @Override @Transactional(rollbackFor = {UnsupportedEncodingException.class, BaseApiException.class}) - public Integer saveTagArticle(Article article, String articleContentHtml) throws UnsupportedEncodingException, BaseApiException { - User user = UserUtils.getCurrentUserByToken(); + public Integer saveTagArticle(Article article, String articleContentHtml, Long userId) throws UnsupportedEncodingException, BaseApiException { String articleTags = article.getArticleTags(); if (StringUtils.isNotBlank(articleTags)) { String[] tags = articleTags.split(","); @@ -80,7 +79,7 @@ public class TagServiceImpl extends AbstractService implements TagService { tagMapper.updateByPrimaryKeySelective(tag); addTagArticle = true; } - Integer countUserTag = tagMapper.selectCountUserTagById(user.getIdUser(), tag.getIdTag()); + Integer countUserTag = tagMapper.selectCountUserTagById(userId, tag.getIdTag()); if (countUserTag == 0) { addUserTag = true; } @@ -92,7 +91,7 @@ public class TagServiceImpl extends AbstractService implements TagService { tagMapper.insertTagArticle(tag.getIdTag(), article.getIdArticle()); } if (addUserTag) { - tagMapper.insertUserTag(tag.getIdTag(), user.getIdUser(), 1); + tagMapper.insertUserTag(tag.getIdTag(), userId, 1); } } return 1; @@ -108,7 +107,7 @@ public class TagServiceImpl extends AbstractService implements TagService { } else { article.setArticleTags("待分类"); } - saveTagArticle(article, articleContentHtml); + saveTagArticle(article, articleContentHtml, userId); } } return 0; diff --git a/src/main/java/com/rymcu/forest/web/api/article/ArticleController.java b/src/main/java/com/rymcu/forest/web/api/article/ArticleController.java index 6fa41a1..9fc6272 100644 --- a/src/main/java/com/rymcu/forest/web/api/article/ArticleController.java +++ b/src/main/java/com/rymcu/forest/web/api/article/ArticleController.java @@ -10,19 +10,22 @@ import com.rymcu.forest.dto.CommentDTO; import com.rymcu.forest.entity.Article; import com.rymcu.forest.entity.ArticleThumbsUp; import com.rymcu.forest.entity.Sponsor; +import com.rymcu.forest.entity.User; import com.rymcu.forest.enumerate.Module; import com.rymcu.forest.service.ArticleService; import com.rymcu.forest.service.ArticleThumbsUpService; import com.rymcu.forest.service.CommentService; import com.rymcu.forest.service.SponsorService; +import com.rymcu.forest.util.UserUtils; import com.rymcu.forest.web.api.exception.BaseApiException; +import com.rymcu.forest.web.api.exception.ErrorCode; import org.springframework.web.bind.annotation.*; import javax.annotation.Resource; -import javax.servlet.http.HttpServletRequest; import java.io.UnsupportedEncodingException; import java.util.List; import java.util.Map; +import java.util.Objects; /** * @author ronger @@ -47,14 +50,22 @@ public class ArticleController { } @PostMapping("/post") - public GlobalResult postArticle(@RequestBody ArticleDTO article, HttpServletRequest request) throws BaseApiException, UnsupportedEncodingException { - return GlobalResultGenerator.genSuccessResult(articleService.postArticle(article, request)); + public GlobalResult postArticle(@RequestBody ArticleDTO article) throws BaseApiException, UnsupportedEncodingException { + User user = UserUtils.getCurrentUserByToken(); + if (Objects.isNull(user)) { + throw new BaseApiException(ErrorCode.INVALID_TOKEN); + } + return GlobalResultGenerator.genSuccessResult(articleService.postArticle(article, user)); } @PutMapping("/post") @AuthorshipInterceptor(moduleName = Module.ARTICLE) - public GlobalResult updateArticle(@RequestBody ArticleDTO article, HttpServletRequest request) throws BaseApiException, UnsupportedEncodingException { - return GlobalResultGenerator.genSuccessResult(articleService.postArticle(article, request)); + public GlobalResult updateArticle(@RequestBody ArticleDTO article) throws BaseApiException, UnsupportedEncodingException { + User user = UserUtils.getCurrentUserByToken(); + if (Objects.isNull(user)) { + throw new BaseApiException(ErrorCode.INVALID_TOKEN); + } + return GlobalResultGenerator.genSuccessResult(articleService.postArticle(article, user)); } @DeleteMapping("/delete/{idArticle}") @@ -71,7 +82,11 @@ public class ArticleController { @GetMapping("/drafts") public GlobalResult> drafts(@RequestParam(defaultValue = "0") Integer page, @RequestParam(defaultValue = "10") Integer rows) throws BaseApiException { PageHelper.startPage(page, rows); - List list = articleService.findDrafts(); + User user = UserUtils.getCurrentUserByToken(); + if (Objects.isNull(user)) { + throw new BaseApiException(ErrorCode.INVALID_TOKEN); + } + List list = articleService.findDrafts(user.getIdUser()); PageInfo pageInfo = new PageInfo<>(list); return GlobalResultGenerator.genSuccessResult(pageInfo); } @@ -86,7 +101,8 @@ public class ArticleController { public GlobalResult updateTags(@RequestBody Article article) throws BaseApiException, UnsupportedEncodingException { Long idArticle = article.getIdArticle(); String articleTags = article.getArticleTags(); - return GlobalResultGenerator.genSuccessResult(articleService.updateTags(idArticle, articleTags)); + User user = UserUtils.getCurrentUserByToken(); + return GlobalResultGenerator.genSuccessResult(articleService.updateTags(idArticle, articleTags, user.getIdUser())); } @PostMapping("/thumbs-up") diff --git a/src/main/resources/static/forest.sql b/src/main/resources/static/forest.sql index b6d5752..fbde953 100644 --- a/src/main/resources/static/forest.sql +++ b/src/main/resources/static/forest.sql @@ -342,6 +342,10 @@ insert into forest.forest_user (id, account, password, nickname, real_name, sex, status, created_time, updated_time, last_login_time, signature) values (1, 'admin', '8ce2dd866238958ac4f07870766813cdaa39a9b83a8c75e26aa50f23', 'admin', 'admin', '0', '0', null, 'admin@rymcu.com', null, '0', '2021-01-25 18:21:51', '2021-01-25 18:21:54', null, null); +insert into forest.forest_user (id, account, password, nickname, real_name, sex, avatar_type, avatar_url, email, phone, + status, created_time, updated_time, last_login_time, signature) +values (2, 'testUser', '8ce2dd866238958ac4f07870766813cdaa39a9b83a8c75e26aa50f23', 'testUser', 'testUser', '0', '0', null, 'testUser@rymcu.com', + null, '0', '2021-01-25 18:21:51', '2021-01-25 18:21:54', null, null); insert into forest.forest_user_role (id_user, id_role, created_time) values (1, 1, '2021-01-25 18:22:12'); diff --git a/src/test/java/com/rymcu/forest/service/ArticleServiceTest.java b/src/test/java/com/rymcu/forest/service/ArticleServiceTest.java new file mode 100644 index 0000000..1b0baee --- /dev/null +++ b/src/test/java/com/rymcu/forest/service/ArticleServiceTest.java @@ -0,0 +1,229 @@ +package com.rymcu.forest.service; + +import com.rymcu.forest.dto.ArticleDTO; +import com.rymcu.forest.dto.ArticleSearchDTO; +import com.rymcu.forest.dto.ArticleTagDTO; +import com.rymcu.forest.dto.Author; +import com.rymcu.forest.entity.User; +import com.rymcu.forest.web.api.exception.BaseApiException; +import org.junit.FixMethodOrder; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; +import org.junit.runner.RunWith; +import org.junit.runners.MethodSorters; +import org.springframework.beans.BeanUtils; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.boot.test.context.SpringBootTest; +import org.springframework.test.context.junit4.SpringRunner; +import org.springframework.transaction.annotation.Transactional; + +import java.io.UnsupportedEncodingException; +import java.util.ArrayList; +import java.util.Collections; +import java.util.List; +import java.util.Map; +import java.util.stream.Collectors; + +import static org.junit.jupiter.api.Assertions.*; + +@SpringBootTest +@RunWith(SpringRunner.class) +@Transactional +// 顺序执行单元测试 +@FixMethodOrder(MethodSorters.DEFAULT) +class ArticleServiceTest { + + /** + * 测试用的Article数据,用于该单元测试的一系列操作 + */ + private final ArticleDTO testArticle; + + /** + * 与Article相关联的测试User数据(建表时提前放入) + */ + private final User testUser = new User(); + + { + // 构建数据之间的关联结构 + Author testAuthor = Author.builder() + .idUser(2L) + .userArticleCount("0") + .userAccount("testUser") + .userNickname("testUser") + .userAvatarURL(null) + .build(); + BeanUtils.copyProperties(testAuthor, testUser); + + ArticleTagDTO tagDTO = ArticleTagDTO.builder() + .tagTitle("Test") + .tagDescription("Test") + .idTag(111) + .tagAuthorId(testUser.getIdUser()) + .build(); + + List 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