feat(interaction): 点赞与评论按集统计

This commit is contained in:
Zhou Weidong committed 2026-10-21 15:56:26 +08:00
1 parent c7ff729f84
commit c6f729fb27
28 files changed
+683 -41

No files matched your search

@@ -10,9 +10,11 @@ import com.fruitshorts.app.vo.AppDramaDetailVO;
import com.fruitshorts.app.vo.AppFeedVO;
import com.fruitshorts.app.vo.AppSectionVO;
import com.fruitshorts.common.pojo.CursorResult;
import com.fruitshorts.content.service.EpisodeService;
import com.fruitshorts.common.exception.ErrorCode;
import com.fruitshorts.common.exception.ServiceException;
import com.fruitshorts.playback.service.PlaybackService;
import com.fruitshorts.interaction.service.CommentService;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.springframework.test.web.servlet.MockMvc;
@@ -43,7 +45,8 @@ class AppCatalogControllerContractTest {
catalogService = mock(AppCatalogService.class);
catalogMvc = ControllerContractSupport.mockMvcOf(new AppCatalogController(catalogService));
dramaMvc = ControllerContractSupport.mockMvcOf(
new AppDramaController(catalogService, mock(PlaybackService.class)));
new AppDramaController(catalogService, mock(PlaybackService.class),
mock(EpisodeService.class), mock(CommentService.class)));
}
@Test
@@ -3,11 +3,16 @@
package com.fruitshorts.app.controller;
import com.fruitshorts.app.support.ControllerContractSupport;
import com.fruitshorts.content.dal.entity.DramaEpisode;
import com.fruitshorts.content.service.EpisodeService;
import com.fruitshorts.interaction.service.CommentService;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.springframework.test.web.servlet.MockMvc;
import java.util.List;
import java.util.Map;
import static org.mockito.BDDMockito.given;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.verify;
@@ -19,13 +24,15 @@ import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.
class AppCommentControllerContractTest {
private CommentService commentService;
private EpisodeService episodeService;
private MockMvc mockMvc;
@BeforeEach
void setUp() {
commentService = mock(CommentService.class);
episodeService = mock(EpisodeService.class);
mockMvc = ControllerContractSupport.mockMvcOf(
new AppCommentController(commentService));
new AppCommentController(commentService, episodeService));
}
@Test
@@ -40,4 +47,21 @@ class AppCommentControllerContractTest {
verify(commentService).countByDrama(7L);
}
@Test
void should_count_comments_by_episode_when_episode_id_is_given() throws Exception {
DramaEpisode episode = new DramaEpisode();
episode.setId(11L);
episode.setDramaId(7L);
given(episodeService.getPublished(11L)).willReturn(episode);
given(commentService.countByEpisodes(List.of(11L))).willReturn(Map.of(11L, 4L));
mockMvc.perform(get("/app/comments/count")
.param("dramaId", "7")
.param("episodeId", "11"))
.andExpect(status().isOk())
.andExpect(jsonPath("$.data").value("4"));
verify(commentService).countByEpisodes(List.of(11L));
}
}
@@ -5,6 +5,7 @@ package com.fruitshorts.app.controller;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fruitshorts.app.service.AppCatalogService;
import com.fruitshorts.common.pojo.CursorResult;
import com.fruitshorts.content.service.EpisodeService;
import com.fruitshorts.framework.log.AccessDeniedRecorder;
import com.fruitshorts.framework.security.JwtAuthenticationFilter;
import com.fruitshorts.framework.security.JwtTokenProvider;
@@ -90,7 +91,7 @@ class AppCommentCountAnonymousSecurityTest {
@Bean
AppCommentController appCommentController(CommentService commentService) {
return new AppCommentController(commentService);
return new AppCommentController(commentService, mock(EpisodeService.class));
}
@Bean
@@ -3,6 +3,9 @@ package com.fruitshorts.app.controller;
import com.fruitshorts.app.service.AppCatalogService;
import com.fruitshorts.app.support.ControllerContractSupport;
import com.fruitshorts.content.dal.entity.DramaEpisode;
import com.fruitshorts.content.service.EpisodeService;
import com.fruitshorts.interaction.service.CommentService;
import com.fruitshorts.playback.service.PlaybackService;
import com.fruitshorts.playback.vo.PlayRespVO;
import org.junit.jupiter.api.BeforeEach;
@@ -10,6 +13,7 @@ import org.junit.jupiter.api.Test;
import org.springframework.test.web.servlet.MockMvc;
import java.util.List;
import java.util.Map;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.ArgumentMatchers.eq;
@@ -30,13 +34,22 @@ import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.
class AppPlaybackContractTest {
private PlaybackService playbackService;
private EpisodeService episodeService;
private CommentService commentService;
private MockMvc mockMvc;
@BeforeEach
void setUp() {
playbackService = mock(PlaybackService.class);
episodeService = mock(EpisodeService.class);
commentService = mock(CommentService.class);
DramaEpisode episode = new DramaEpisode();
episode.setLikeCount(88L);
given(episodeService.getExisting(any())).willReturn(episode);
given(commentService.countByEpisodes(any())).willReturn(Map.of());
mockMvc = ControllerContractSupport.mockMvcOf(
new AppDramaController(mock(AppCatalogService.class), playbackService));
new AppDramaController(mock(AppCatalogService.class), playbackService,
episodeService, commentService));
}
@Test
@@ -52,7 +65,9 @@ class AppPlaybackContractTest {
.andExpect(jsonPath("$.data.episodeId").isString())
.andExpect(jsonPath("$.data.accessible").value(true))
.andExpect(jsonPath("$.data.accessType").value(PlaybackService.ACCESS_FREE))
.andExpect(jsonPath("$.data.playUrl").value("/files/ep.mp4?s=1"));
.andExpect(jsonPath("$.data.playUrl").value("/files/ep.mp4?s=1"))
.andExpect(jsonPath("$.data.likeCount").value("88"))
.andExpect(jsonPath("$.data.commentCount").value("0"));
}
@Test
@@ -5,6 +5,7 @@ import com.fruitshorts.app.vo.AppDramaCardVO;
import com.fruitshorts.app.vo.AppDramaDetailVO;
import com.fruitshorts.common.pojo.CursorResult;
import com.fruitshorts.content.dal.entity.Drama;
import com.fruitshorts.content.dal.entity.DramaEpisode;
import com.fruitshorts.content.service.BannerService;
import com.fruitshorts.content.service.DramaCategoryService;
import com.fruitshorts.content.service.DramaService;
@@ -55,10 +56,12 @@ class AppCatalogServiceTest {
drama.setLikeCount(3200L);
drama.setFavoriteCount(1800L);
drama.setViewCount(12400L);
DramaEpisode episode = episode(11L, 42L, 55L);
given(dramaService.getPublished(42L)).willReturn(drama);
given(episodeService.listPublishedByDrama(42L)).willReturn(List.of());
given(episodeService.listPublishedByDrama(42L)).willReturn(List.of(episode));
given(dramaCategoryService.listBasic()).willReturn(List.of());
given(commentService.countByDrama(42L)).willReturn(96L);
given(commentService.countByEpisodes(List.of(11L))).willReturn(Map.of(11L, 2L));
AppDramaDetailVO detail = catalogService.dramaDetail(42L);
@@ -66,6 +69,8 @@ class AppCatalogServiceTest {
assertThat(detail.getLikeCount()).isEqualTo(3200L);
assertThat(detail.getFavoriteCount()).isEqualTo(1800L);
assertThat(detail.getCommentCount()).isEqualTo(96L);
assertThat(detail.getEpisodes().get(0).getLikeCount()).isEqualTo(55L);
assertThat(detail.getEpisodes().get(0).getCommentCount()).isEqualTo(2L);
verify(commentService).countByDrama(42L);
}
@@ -83,10 +88,13 @@ class AppCatalogServiceTest {
second.setViewCount(800L);
second.setLikeCount(120L);
second.setFavoriteCount(80L);
given(episodeService.firstPlayableByDramas(List.of(1L, 2L))).willReturn(Map.of());
DramaEpisode firstEpisode = episode(11L, 1L, 320L);
DramaEpisode secondEpisode = episode(22L, 2L, 120L);
given(episodeService.firstPlayableByDramas(List.of(1L, 2L)))
.willReturn(Map.of(1L, firstEpisode, 2L, secondEpisode));
given(dramaCategoryService.listBasic()).willReturn(List.of());
given(commentService.countByDramas(List.of(1L, 2L)))
.willReturn(Map.of(1L, 9L));
given(commentService.countByEpisodes(List.of(11L, 22L)))
.willReturn(Map.of(11L, 9L));
List<AppDramaCardVO> cards = catalogService.toCards(List.of(first, second));
@@ -97,7 +105,7 @@ class AppCatalogServiceTest {
assertThat(cards.get(1).getLikeCount()).isEqualTo(120L);
assertThat(cards.get(1).getFavoriteCount()).isEqualTo(80L);
assertThat(cards.get(1).getCommentCount()).isZero();
verify(commentService).countByDramas(List.of(1L, 2L));
verify(commentService).countByEpisodes(List.of(11L, 22L));
verify(commentService, never()).countByDrama(any());
}
@@ -107,14 +115,17 @@ class AppCatalogServiceTest {
given(dramaService.listPublishedFeed(List.of("ANIME"), null, 21))
.willReturn(List.of(anime));
given(dramaCategoryService.listBasic()).willReturn(List.of());
given(episodeService.firstPlayableByDramas(List.of(1L))).willReturn(Map.of());
given(commentService.countByDramas(List.of(1L))).willReturn(Map.of(1L, 3L));
DramaEpisode preview = episode(11L, 1L, 45L);
given(episodeService.firstPlayableByDramas(List.of(1L)))
.willReturn(Map.of(1L, preview));
given(commentService.countByEpisodes(List.of(11L))).willReturn(Map.of(11L, 3L));
CursorResult<AppDramaCardVO> result = catalogService.feed("ANIME", null, null);
assertThat(result.getList()).hasSize(1);
assertThat(result.getList().get(0).getDramaType()).isEqualTo("ANIME");
assertThat(result.getList().get(0).getCommentCount()).isEqualTo(3L);
assertThat(result.getList().get(0).getLikeCount()).isEqualTo(45L);
assertThat(result.isHasMore()).isFalse();
verify(dramaService).listPublishedFeed(List.of("ANIME"), null, 21);
}
@@ -140,7 +151,7 @@ class AppCatalogServiceTest {
.willReturn(List.of(second));
given(dramaCategoryService.listBasic()).willReturn(List.of());
given(episodeService.firstPlayableByDramas(any())).willReturn(Map.of());
given(commentService.countByDramas(any())).willReturn(Map.of());
given(commentService.countByEpisodes(any())).willReturn(Map.of());
CursorResult<AppDramaCardVO> firstPage = catalogService.feed(null, null, 1);
CursorResult<AppDramaCardVO> secondPage = catalogService.feed(null, firstPage.getNextCursor(), 1);
@@ -160,4 +171,12 @@ class AppCatalogServiceTest {
drama.setDramaType(dramaType);
return drama;
}
private DramaEpisode episode(Long id, Long dramaId, Long likeCount) {
DramaEpisode episode = new DramaEpisode();
episode.setId(id);
episode.setDramaId(dramaId);
episode.setLikeCount(likeCount);
return episode;
}
}
@@ -109,4 +109,23 @@ class CommentServiceCountTest {
assertThat(lambda.getParamNameValuePairs().values())
.contains(7L, 8L, "deleted");
}
@Test
void should_group_comment_counts_by_episode() {
ArgumentCaptor<Wrapper<ContentComment>> captor = ArgumentCaptor.forClass(Wrapper.class);
given(commentMapper.selectMaps(captor.capture())).willReturn(List.of(
Map.of("episode_id", 11L, "comment_count", 4L),
Map.of("episode_id", 12L, "comment_count", 2L)));
Map<Long, Long> counts = commentService.countByEpisodes(
Arrays.asList(11L, 12L, 12L, null));
assertThat(counts).containsEntry(11L, 4L).containsEntry(12L, 2L);
Wrapper<ContentComment> wrapper = captor.getValue();
assertThat(wrapper.getSqlSegment())
.contains("episode_id")
.contains("status")
.contains("deleted")
.contains("GROUP BY");
}
}
@@ -0,0 +1,113 @@
// Author: fruit.com.cn
package com.fruitshorts.app.service;
import com.baomidou.mybatisplus.core.MybatisConfiguration;
import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.TableInfoHelper;
import com.fruitshorts.common.exception.ErrorCode;
import com.fruitshorts.common.exception.ServiceException;
import com.fruitshorts.content.dal.entity.DramaEpisode;
import com.fruitshorts.content.dal.mapper.DramaEpisodeMapper;
import com.fruitshorts.content.service.DramaService;
import com.fruitshorts.content.service.EpisodeService;
import com.fruitshorts.content.vo.EpisodeSaveReqVO;
import org.apache.ibatis.builder.MapperBuilderAssistant;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.mockito.ArgumentCaptor;
import java.util.List;
import java.util.Map;
import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.assertThatThrownBy;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.ArgumentMatchers.isNull;
import static org.mockito.BDDMockito.given;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.never;
import static org.mockito.Mockito.verify;
/** K89:剧集点赞编辑、原子增减与剧级批量汇总。 */
class EpisodeServiceK89Test {
private DramaEpisodeMapper episodeMapper;
private EpisodeService episodeService;
@BeforeAll
static void initTableInfo() {
TableInfoHelper.initTableInfo(
new MapperBuilderAssistant(new MybatisConfiguration(), ""), DramaEpisode.class);
}
@BeforeEach
void setUp() {
episodeMapper = mock(DramaEpisodeMapper.class);
episodeService = new EpisodeService(episodeMapper, mock(DramaService.class));
}
@Test
@SuppressWarnings("unchecked")
void should_use_greatest_zero_when_episode_like_decrements() {
given(episodeMapper.update(isNull(), any())).willReturn(1);
ArgumentCaptor<Wrapper<DramaEpisode>> captor = ArgumentCaptor.forClass(Wrapper.class);
episodeService.adjustLikeCount(11L, -1);
verify(episodeMapper).update(isNull(), captor.capture());
LambdaUpdateWrapper<DramaEpisode> update =
(LambdaUpdateWrapper<DramaEpisode>) captor.getValue();
assertThat(update.getSqlSet()).contains("GREATEST(like_count - 1, 0)");
}
@Test
void should_update_episode_like_count_without_overwriting_when_null() {
DramaEpisode episode = new DramaEpisode();
episode.setId(11L);
episode.setDramaId(5L);
episode.setEpNo(1);
episode.setLikeCount(8L);
given(episodeMapper.selectById(11L)).willReturn(episode);
EpisodeSaveReqVO req = new EpisodeSaveReqVO();
req.setDramaId(5L);
req.setNumber(1);
req.setLikeCount(20L);
episodeService.update(11L, req);
assertThat(episode.getLikeCount()).isEqualTo(20L);
}
@Test
void should_reject_negative_episode_like_count() {
DramaEpisode episode = new DramaEpisode();
episode.setId(11L);
episode.setDramaId(5L);
episode.setEpNo(1);
given(episodeMapper.selectById(11L)).willReturn(episode);
EpisodeSaveReqVO req = new EpisodeSaveReqVO();
req.setDramaId(5L);
req.setNumber(1);
req.setLikeCount(-1L);
assertThatThrownBy(() -> episodeService.update(11L, req))
.isInstanceOf(ServiceException.class)
.satisfies(error -> assertThat(((ServiceException) error).getCode())
.isEqualTo(ErrorCode.BAD_REQUEST.getCode()));
verify(episodeMapper, never()).updateById(any(DramaEpisode.class));
}
@Test
void should_sum_episode_likes_by_drama() {
given(episodeMapper.selectMaps(any())).willReturn(List.of(
Map.of("drama_id", 5L, "like_count", 120L),
Map.of("drama_id", 6L, "like_count", 30L)));
Map<Long, Long> sums = episodeService.sumLikeCountsByDramas(List.of(5L, 6L));
assertThat(sums).containsEntry(5L, 120L).containsEntry(6L, 30L);
}
}
@@ -0,0 +1,112 @@
// Author: fruit.com.cn
package com.fruitshorts.interaction.service;
import com.baomidou.mybatisplus.core.MybatisConfiguration;
import com.baomidou.mybatisplus.core.metadata.TableInfoHelper;
import com.fruitshorts.common.exception.ErrorCode;
import com.fruitshorts.common.exception.ServiceException;
import com.fruitshorts.content.dal.entity.Drama;
import com.fruitshorts.content.dal.entity.DramaEpisode;
import com.fruitshorts.content.service.DramaService;
import com.fruitshorts.content.service.EpisodeService;
import com.fruitshorts.interaction.dal.entity.ContentLike;
import com.fruitshorts.interaction.dal.mapper.ContentCommentMapper;
import com.fruitshorts.interaction.dal.mapper.ContentLikeMapper;
import com.fruitshorts.interaction.vo.LikeToggleReqVO;
import com.fruitshorts.interaction.vo.LikeToggleRespVO;
import org.apache.ibatis.builder.MapperBuilderAssistant;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.assertThatThrownBy;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.BDDMockito.given;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.never;
import static org.mockito.Mockito.verify;
/** K89:点赞目标支持已上线的分集。 */
class LikeServiceEpisodeTest {
private ContentLikeMapper likeMapper;
private EpisodeService episodeService;
private DramaService dramaService;
private LikeService likeService;
@BeforeAll
static void initTableInfo() {
TableInfoHelper.initTableInfo(
new MapperBuilderAssistant(new MybatisConfiguration(), ""), ContentLike.class);
}
@BeforeEach
void setUp() {
likeMapper = mock(ContentLikeMapper.class);
episodeService = mock(EpisodeService.class);
dramaService = mock(DramaService.class);
likeService = new LikeService(
likeMapper,
mock(ContentCommentMapper.class),
dramaService,
episodeService);
}
@Test
void should_increment_episode_like_and_return_latest_count() {
DramaEpisode published = episode(6L);
given(episodeService.getPublished(11L)).willReturn(published);
given(dramaService.getPublished(5L)).willReturn(new Drama());
given(likeMapper.selectOne(any())).willReturn(null);
given(episodeService.getExisting(11L)).willReturn(published);
LikeToggleRespVO result = likeService.toggle(9L, request(11L));
assertThat(result.getLiked()).isTrue();
assertThat(result.getCount()).isEqualTo(6L);
verify(episodeService).adjustLikeCount(11L, 1);
}
@Test
void should_decrement_episode_like_when_cancelled() {
DramaEpisode published = episode(5L);
given(episodeService.getPublished(11L)).willReturn(published);
given(dramaService.getPublished(5L)).willReturn(new Drama());
given(likeMapper.selectOne(any())).willReturn(new ContentLike());
given(episodeService.getExisting(11L)).willReturn(published);
LikeToggleRespVO result = likeService.toggle(9L, request(11L));
assertThat(result.getLiked()).isFalse();
assertThat(result.getCount()).isEqualTo(5L);
verify(likeMapper).physicalDelete(9L, LikeService.TYPE_EPISODE, 11L);
verify(episodeService).adjustLikeCount(11L, -1);
}
@Test
void should_reject_missing_or_unpublished_episode() {
given(episodeService.getPublished(11L))
.willThrow(new ServiceException(ErrorCode.DATA_NOT_FOUND, "剧集不存在或未上线"));
assertThatThrownBy(() -> likeService.toggle(9L, request(11L)))
.isInstanceOf(ServiceException.class);
verify(likeMapper, never()).insert(any(ContentLike.class));
}
private DramaEpisode episode(Long likeCount) {
DramaEpisode episode = new DramaEpisode();
episode.setId(11L);
episode.setDramaId(5L);
episode.setLikeCount(likeCount);
return episode;
}
private LikeToggleReqVO request(Long episodeId) {
LikeToggleReqVO req = new LikeToggleReqVO();
req.setTargetType(LikeService.TYPE_EPISODE);
req.setTargetId(episodeId);
return req;
}
}