package com.talentcard.server; import static org.assertj.core.api.Assertions.assertThat; import java.util.Map; import com.talentcard.server.business.PageResult; import com.talentcard.server.business.admin.AdminBusinessService; import com.talentcard.server.business.admin.NewsArticleRequest; import com.talentcard.server.business.portal.PortalService; import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; import org.springframework.boot.test.context.SpringBootTest; import org.springframework.jdbc.core.JdbcTemplate; import org.springframework.test.context.ActiveProfiles; import org.springframework.beans.factory.annotation.Autowired; @SpringBootTest @ActiveProfiles("test") class TalentCardServerApplicationTests { @Autowired private JdbcTemplate jdbcTemplate; @Autowired private PortalService portalService; @Autowired private AdminBusinessService adminBusinessService; @BeforeEach void clearNewsArticles() { jdbcTemplate.update("delete from news_article"); } @Test void contextLoads() { } @Test void publishedNewsIsListedAndDetailIncrementsViewCount() { jdbcTemplate.update(""" insert into news_article(title, summary, source, content, view_count, is_top, sort_no, publish_time, status) values (?, ?, ?, ?, ?, ?, ?, current_timestamp, ?) """, "已发布资讯", "摘要", "东区发布", "

正文

", 7, 1, 10, 1); jdbcTemplate.update(""" insert into news_article(title, summary, source, content, view_count, is_top, sort_no, publish_time, status) values (?, ?, ?, ?, ?, ?, ?, current_timestamp, ?) """, "草稿资讯", "草稿摘要", "东区发布", "

草稿

", 0, 0, 0, 0); PageResult> page = portalService.news(1, 10); assertThat(page.total()).isEqualTo(1); assertThat(page.records()).hasSize(1); Long id = ((Number) page.records().get(0).get("id")).longValue(); Map detail = portalService.newsDetail(id); assertThat(detail.get("title")).isEqualTo("已发布资讯"); assertThat(((Number) detail.get("viewCount")).intValue()).isEqualTo(8); } @Test void adminCanPublishNewsForThePortalAndContentIsSanitized() { Map draft = adminBusinessService.createNews(new NewsArticleRequest( "后台发布资讯", "用于联调", "", "东区发布", "管理员", "

安全正文

", true, 12, 0), "测试管理员"); Long id = ((Number) draft.get("id")).longValue(); assertThat(portalService.news(1, 10).total()).isZero(); adminBusinessService.updateNewsStatus(id, (byte) 1); PageResult> visible = portalService.news(1, 10); Map detail = portalService.newsDetail(id); assertThat(visible.total()).isEqualTo(1); assertThat(detail.get("content").toString()).contains("安全正文").doesNotContain("script"); } }