hejianhao
2026-08-28 16a57a8fc6868cea9171a1119698572407a2fc03
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
package com.tcm.training.admin.app.home.service;
 
import com.baomidou.mybatisplus.core.MybatisConfiguration;
import com.baomidou.mybatisplus.core.metadata.TableInfoHelper;
import com.tcm.training.admin.app.home.vo.AppHomeLiveCourseVO;
import com.tcm.training.admin.app.live.support.AppLiveCourseDisplaySupport;
import com.tcm.training.admin.live.entity.LiveCourse;
import com.tcm.training.admin.live.entity.LiveSession;
import com.tcm.training.admin.live.mapper.LiveCourseMapper;
import com.tcm.training.admin.live.mapper.LiveSessionMapper;
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.junit.jupiter.api.extension.ExtendWith;
import org.mockito.Mock;
import org.mockito.junit.jupiter.MockitoExtension;
 
import java.time.LocalDateTime;
import java.util.Arrays;
import java.util.List;
 
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNull;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.Mockito.when;
 
/**
 * APP 首页养生直播聚合服务测试。
 */
@ExtendWith(MockitoExtension.class)
class AppHomeLiveCourseServiceTest {
 
    @BeforeAll
    static void initLambdaCache() {
        MapperBuilderAssistant assistant = new MapperBuilderAssistant(new MybatisConfiguration(), "");
        TableInfoHelper.initTableInfo(assistant, LiveCourse.class);
        TableInfoHelper.initTableInfo(assistant, LiveSession.class);
    }
 
    @Mock
    private LiveCourseMapper liveCourseMapper;
 
    @Mock
    private LiveSessionMapper liveSessionMapper;
 
    private AppHomeLiveCourseService appHomeLiveCourseService;
 
    /**
     * 每个用例前重建被测首页服务。
     */
    @BeforeEach
    void setUp() {
        appHomeLiveCourseService = new AppHomeLiveCourseService(
                liveCourseMapper,
                liveSessionMapper,
                new AppLiveCourseDisplaySupport()
        );
    }
 
    /**
     * 首页课程状态应忽略历史残留的直播中场次,只认时间线上的当前有效场次。
     */
    @Test
    void listHomeLiveCoursesShouldIgnoreHistoricalLivingSession() {
        LiveCourse endedCourse = buildCourse(2L, "养生直播课程2");
        LiveCourse livingCourse = buildCourse(1L, "这里是养生功法直播间");
        LiveSession endedCourseStaleLiving = buildSession(2001L, 2L, LocalDateTime.now().minusDays(1),
                AppLiveCourseDisplaySupport.SESSION_STATUS_LIVING, "https://cdn.example/stale.m3u8");
        LiveSession endedCourseLatestEnded = buildSession(2002L, 2L, LocalDateTime.now().minusHours(2),
                AppLiveCourseDisplaySupport.SESSION_STATUS_ENDED, "https://cdn.example/ended.m3u8");
        endedCourseLatestEnded.setActualEndTime(LocalDateTime.now().minusHours(1));
        LiveSession livingCourseCurrent = buildSession(1001L, 1L, LocalDateTime.now().minusMinutes(5),
                AppLiveCourseDisplaySupport.SESSION_STATUS_LIVING, "https://cdn.example/current.m3u8");
 
        when(liveCourseMapper.selectList(any())).thenReturn(Arrays.asList(endedCourse, livingCourse));
        when(liveSessionMapper.selectList(any())).thenReturn(Arrays.asList(
                endedCourseStaleLiving,
                endedCourseLatestEnded,
                livingCourseCurrent
        ));
 
        List<AppHomeLiveCourseVO> cards = appHomeLiveCourseService.listHomeLiveCourses();
 
        assertEquals(2, cards.size());
        assertEquals(Long.valueOf(1L), cards.get(0).getCourseId());
        assertEquals("LIVING", cards.get(0).getDisplayStatus());
        assertEquals(Long.valueOf(1001L), cards.get(0).getCurrentSessionId());
        assertEquals("https://cdn.example/current.m3u8", cards.get(0).getVideoUrl());
 
        assertEquals(Long.valueOf(2L), cards.get(1).getCourseId());
        assertEquals("ENDED", cards.get(1).getDisplayStatus());
        assertNull(cards.get(1).getCurrentSessionId());
        assertNull(cards.get(1).getVideoUrl());
    }
 
    /**
     * 构造首页直播课程测试数据。
     */
    private LiveCourse buildCourse(Long courseId, String title) {
        LiveCourse course = new LiveCourse();
        course.setId(courseId);
        course.setTitle(title);
        course.setCoverImg("https://cdn.example/cover-" + courseId + ".png");
        course.setBannerImgs(Arrays.asList("https://cdn.example/live-" + courseId + ".png"));
        course.setStatus(1);
        course.setDeleted(0);
        return course;
    }
 
    /**
     * 构造首页直播场次测试数据。
     */
    private LiveSession buildSession(Long sessionId, Long courseId, LocalDateTime startTime, Integer liveStatus, String videoUrl) {
        LiveSession session = new LiveSession();
        session.setId(sessionId);
        session.setLiveCourseId(courseId);
        session.setStartTime(startTime);
        session.setLiveStatus(liveStatus);
        session.setVideoUrl(videoUrl);
        return session;
    }
}