From c2263de3b71f2f68da4c4d0dae856c88e8cc6b0c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E4=BD=A0=E4=B8=80=E4=B8=AA=E4=BA=BA=E5=9C=A8=E8=BF=99?= =?UTF-8?q?=E5=84=BF=E5=B9=B2=E5=98=9B=E4=BD=A0=E6=98=AF=E6=9D=A5=E6=8B=89?= =?UTF-8?q?=E5=B1=8E=E7=9A=84=E5=90=A7?= <1421374934@qq.com> Date: Wed, 5 Apr 2023 18:46:09 +0800 Subject: [PATCH] =?UTF-8?q?:bug:=20=E9=83=A8=E5=88=86=E6=B5=8B=E8=AF=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../forest/service/PortfolioServiceTest.java | 208 +++++++++++++++++- .../forest/service/ProductServiceTest.java | 20 ++ 2 files changed, 227 insertions(+), 1 deletion(-) diff --git a/src/test/java/com/rymcu/forest/service/PortfolioServiceTest.java b/src/test/java/com/rymcu/forest/service/PortfolioServiceTest.java index fe53d82..b10f7b5 100644 --- a/src/test/java/com/rymcu/forest/service/PortfolioServiceTest.java +++ b/src/test/java/com/rymcu/forest/service/PortfolioServiceTest.java @@ -1,48 +1,254 @@ package com.rymcu.forest.service; +import com.github.pagehelper.PageInfo; import com.rymcu.forest.base.BaseServiceTest; +import com.rymcu.forest.core.exception.BusinessException; +import com.rymcu.forest.core.exception.ServiceException; +import com.rymcu.forest.core.exception.UltraViresException; +import com.rymcu.forest.dto.*; +import com.rymcu.forest.entity.Portfolio; +import com.rymcu.forest.entity.User; import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.DisplayName; import org.junit.jupiter.api.Test; +import org.springframework.beans.BeanUtils; +import org.springframework.beans.factory.annotation.Autowired; +import java.io.UnsupportedEncodingException; +import java.util.ArrayList; +import java.util.List; + +import static org.junit.jupiter.api.Assertions.*; + +/** + * 作品集测试 + */ class PortfolioServiceTest extends BaseServiceTest { + /** + * 测试用的Article数据,用于该单元测试的一系列操作 + */ + private final ArticleDTO testArticle; + private final UserDTO userDTO = new UserDTO(); + private final Portfolio portfolio = new Portfolio(); + /** + * 与Article相关联的测试User数据(建表时提前放入) + */ + private final User testUser = new User(); + PortfolioArticleDTO portfolioArticleDTO = new PortfolioArticleDTO(); + ; + @Autowired + private PortfolioService portfolioService; + @Autowired + private ArticleService articleService; + + { + userDTO.setIdUser(2L); + + portfolio.setPortfolioDescriptionHtml("test"); + portfolio.setPortfolioDescription("test"); + portfolio.setPortfolioAuthorId(userDTO.getIdUser()); + portfolio.setPortfolioTitle("test"); + + // 构建数据之间的关联结构 + 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(); + } + @BeforeEach - void setUp() { + void setUp() throws UnsupportedEncodingException { + Long articleId = articleService.postArticle(testArticle, testUser); + assertNotNull(articleId); + testArticle.setIdArticle(articleId); } @Test + @DisplayName("查询用户作品集") void findUserPortfoliosByUser() { + List userPortfoliosByUser = portfolioService.findUserPortfoliosByUser(userDTO); + assertTrue(userPortfoliosByUser.isEmpty()); } @Test + @DisplayName("查询作品集") void findPortfolioDTOById() { + PortfolioDTO portfolioDTO = portfolioService.findPortfolioDTOById(1L, null); + assertNull(portfolioDTO.getIdPortfolio()); } @Test + @DisplayName("保持/更新作品集") void postPortfolio() { + List userPortfoliosByUser = portfolioService.findUserPortfoliosByUser(userDTO); + assertTrue(userPortfoliosByUser.isEmpty()); + portfolioService.postPortfolio(portfolio); + + Portfolio portfolio1 = portfolioService.postPortfolio(portfolio); + assertNotNull(portfolio1.getIdPortfolio()); + assertEquals(portfolio, portfolio1); + + userPortfoliosByUser = portfolioService.findUserPortfoliosByUser(userDTO); + assertEquals(1, userPortfoliosByUser.size()); + + PortfolioDTO portfolioDTO = portfolioService.findPortfolioDTOById(portfolio1.getIdPortfolio(), null); + assertEquals(portfolio1.getPortfolioDescription(), portfolioDTO.getPortfolioDescription()); + + } @Test + @DisplayName("查询作品集下未绑定文章") void findUnbindArticles() { + assertThrows( + BusinessException.class, () -> portfolioService.findUnbindArticles(1, 10, "test", portfolio.getIdPortfolio(), userDTO.getIdUser()) + ); + + Portfolio portfolio1 = portfolioService.postPortfolio(portfolio); + + assertThrows( + UltraViresException.class, () -> portfolioService.findUnbindArticles(1, 10, "test", portfolio.getIdPortfolio(), 1L) + ); + + PageInfo articles = portfolioService.findUnbindArticles(1, 10, "test", portfolio.getIdPortfolio(), userDTO.getIdUser()); + assertEquals(1L, articles.getTotal()); } @Test + @DisplayName("绑定文章") void bindArticle() { + portfolioService.postPortfolio(portfolio); + + portfolioArticleDTO.setIdArticle(testArticle.getIdArticle()); + portfolioArticleDTO.setIdPortfolio(portfolio.getIdPortfolio()); + boolean b = portfolioService.bindArticle(portfolioArticleDTO); + + assertTrue(b); + + assertThrows(BusinessException.class, () -> portfolioService.bindArticle(portfolioArticleDTO)); + } @Test + @DisplayName("更新文章排序号") void updateArticleSortNo() { + portfolioService.postPortfolio(portfolio); + + assertThrows(ServiceException.class, () -> portfolioService.updateArticleSortNo(portfolioArticleDTO)); + portfolioArticleDTO.setIdArticle(testArticle.getIdArticle()); + portfolioArticleDTO.setIdPortfolio(portfolio.getIdPortfolio()); + portfolioArticleDTO.setSortNo(10); + + assertThrows(ServiceException.class, () -> portfolioService.updateArticleSortNo(portfolioArticleDTO)); + + portfolioService.bindArticle(portfolioArticleDTO); + + boolean b = portfolioService.updateArticleSortNo(portfolioArticleDTO); + assertTrue(b); + + portfolioService.updateArticleSortNo(portfolioArticleDTO); + assertTrue(b); } @Test + @DisplayName("取消绑定文章") void unbindArticle() { + assertThrows(ServiceException.class, () -> portfolioService.unbindArticle(null, null)); + + portfolioService.postPortfolio(portfolio); + + portfolioArticleDTO.setIdArticle(testArticle.getIdArticle()); + portfolioArticleDTO.setIdPortfolio(portfolio.getIdPortfolio()); + portfolioArticleDTO.setSortNo(10); + portfolioService.bindArticle(portfolioArticleDTO); + + boolean b = portfolioService.unbindArticle(portfolio.getIdPortfolio(), portfolioArticleDTO.getIdArticle()); + assertTrue(b); } + @Test + @DisplayName("删除作品集") void deletePortfolio() { + portfolioService.postPortfolio(portfolio); + assertThrows(IllegalArgumentException.class, () -> portfolioService.deletePortfolio(null, null, null)); + + assertThrows(NullPointerException.class, () -> portfolioService.deletePortfolio(portfolio.getIdPortfolio(), null, 3)); + + + assertThrows(UltraViresException.class, () -> portfolioService.deletePortfolio(portfolio.getIdPortfolio(), 1L, 3)); + + boolean b = portfolioService.deletePortfolio(portfolio.getIdPortfolio(), userDTO.getIdUser(), 3); + assertTrue(b); + + portfolio.setIdPortfolio(null); + portfolioService.postPortfolio(portfolio); + b = portfolioService.deletePortfolio(portfolio.getIdPortfolio(), userDTO.getIdUser(), 1); + assertTrue(b); + + assertThrows(BusinessException.class, () -> portfolioService.deletePortfolio(portfolio.getIdPortfolio(), userDTO.getIdUser(), 1)); + + portfolio.setIdPortfolio(null); + portfolioService.postPortfolio(portfolio); + + portfolioArticleDTO.setIdArticle(testArticle.getIdArticle()); + portfolioArticleDTO.setIdPortfolio(portfolio.getIdPortfolio()); + portfolioArticleDTO.setSortNo(10); + portfolioService.bindArticle(portfolioArticleDTO); + + assertThrows(BusinessException.class, () -> portfolioService.deletePortfolio(portfolio.getIdPortfolio(), userDTO.getIdUser(), 1)); + + portfolioService.unbindArticle(portfolio.getIdPortfolio(), portfolioArticleDTO.getIdArticle()); + + portfolioService.deletePortfolio(portfolio.getIdPortfolio(), userDTO.getIdUser(), 1); + assertTrue(b); + } @Test + @DisplayName("获取作品集列表数据") void findPortfolios() { + List portfolios = portfolioService.findPortfolios(); + assertTrue(portfolios.isEmpty()); + + portfolioService.postPortfolio(portfolio); + portfolios = portfolioService.findPortfolios(); + assertFalse(portfolios.isEmpty()); } } \ No newline at end of file diff --git a/src/test/java/com/rymcu/forest/service/ProductServiceTest.java b/src/test/java/com/rymcu/forest/service/ProductServiceTest.java index ff96b0c..eeecc87 100644 --- a/src/test/java/com/rymcu/forest/service/ProductServiceTest.java +++ b/src/test/java/com/rymcu/forest/service/ProductServiceTest.java @@ -1,20 +1,40 @@ package com.rymcu.forest.service; import com.rymcu.forest.base.BaseServiceTest; +import com.rymcu.forest.dto.ProductDTO; import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.DisplayName; import org.junit.jupiter.api.Test; +import org.springframework.beans.factory.annotation.Autowired; + +import java.util.List; + +import static org.junit.jupiter.api.Assertions.*; class ProductServiceTest extends BaseServiceTest { + @Autowired + private ProductService productService; + @BeforeEach void setUp() { } @Test + @DisplayName("查询产品列表") void findProducts() { + List products = productService.findProducts(); + assertFalse(products.isEmpty()); } @Test + @DisplayName("获取产品详情") void findProductDTOById() { + ProductDTO productDTOById = productService.findProductDTOById(1, null); + assertNotNull(productDTOById); + ProductDTO productDTOById2 = productService.findProductDTOById(1, 1); + + assertEquals(productDTOById.getProductTitle(), productDTOById2.getProductTitle()); + assertNotEquals(productDTOById.getProductContent(), productDTOById2.getProductContent()); } } \ No newline at end of file