“mitao”
2026-04-24 b620af46945757c8c578a9f8d3ef472e40544d70
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
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
package com.tcm.training.admin.system.service;
 
import com.baomidou.mybatisplus.core.MybatisConfiguration;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.metadata.TableInfoHelper;
import com.tcm.training.admin.app.notice.entity.AppUserActivityNotice;
import com.tcm.training.admin.app.notice.mapper.AppUserActivityNoticeMapper;
import com.tcm.training.admin.app.notice.service.AppSystemNoticeDispatchCommand;
import com.tcm.training.admin.app.notice.service.AppSystemNoticeDispatchService;
import com.tcm.training.admin.app.notice.service.AppSystemNoticeType;
import com.tcm.training.admin.member.entity.AppUser;
import com.tcm.training.admin.member.mapper.UserMapper;
import com.tcm.training.admin.system.entity.ActivityNotice;
import com.tcm.training.admin.system.event.ActivityNoticeCreatedEvent;
import com.tcm.training.admin.system.mapper.ActivityNoticeMapper;
import org.apache.ibatis.builder.MapperBuilderAssistant;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.mockito.ArgumentCaptor;
import org.mockito.Captor;
import org.mockito.Mock;
import org.mockito.junit.jupiter.MockitoExtension;
 
import java.util.Arrays;
import java.util.Collections;
 
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.Mockito.doThrow;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;
 
/**
 * 活动通知分发服务测试。
 */
@ExtendWith(MockitoExtension.class)
class ActivityNoticeDispatchServiceTest {
 
    @BeforeAll
    static void initLambdaCache() {
        MapperBuilderAssistant assistant = new MapperBuilderAssistant(new MybatisConfiguration(), "");
        TableInfoHelper.initTableInfo(assistant, ActivityNotice.class);
        TableInfoHelper.initTableInfo(assistant, AppUser.class);
        TableInfoHelper.initTableInfo(assistant, AppUserActivityNotice.class);
    }
 
    @Mock
    private UserMapper userMapper;
 
    @Mock
    private AppUserActivityNoticeMapper appUserActivityNoticeMapper;
 
    @Mock
    private ActivityNoticeMapper activityNoticeMapper;
 
    @Mock
    private AppSystemNoticeDispatchService appSystemNoticeDispatchService;
 
    @Captor
    private ArgumentCaptor<AppUserActivityNotice> activityRecordCaptor;
 
    @Captor
    private ArgumentCaptor<AppSystemNoticeDispatchCommand> commandCaptor;
 
    /**
     * 活动通知创建后应为启用用户写入 APP 活动记录和统一系统通知。
     */
    @Test
    void handleActivityNoticeCreatedShouldDispatchSystemNoticeToEnabledUsers() {
        ActivityNoticeDispatchService service = buildService();
        ActivityNotice notice = buildNotice(10L);
        AppUser user = buildUser(100L);
        when(activityNoticeMapper.selectOne(any())).thenReturn(notice);
        when(userMapper.selectPage(any(), any())).thenReturn(buildUserPage(user));
        when(appUserActivityNoticeMapper.selectCount(any())).thenReturn(0L);
 
        service.handleActivityNoticeCreated(new ActivityNoticeCreatedEvent(10L));
 
        verify(appUserActivityNoticeMapper).insert(activityRecordCaptor.capture());
        assertEquals(Long.valueOf(100L), activityRecordCaptor.getValue().getAppUserId());
        assertEquals(Long.valueOf(10L), activityRecordCaptor.getValue().getActivityNoticeId());
        verify(appSystemNoticeDispatchService).createAndPush(commandCaptor.capture());
        assertEquals(Long.valueOf(100L), commandCaptor.getValue().getAppUserId());
        assertEquals(AppSystemNoticeType.ACTIVITY_NOTICE, commandCaptor.getValue().getNoticeTypeCode());
        assertEquals("ACTIVITY_NOTICE", commandCaptor.getValue().getReferenceType());
        assertEquals(Long.valueOf(10L), commandCaptor.getValue().getReferenceId());
    }
 
    /**
     * 单个用户分发失败时不应中断后续用户分发。
     */
    @Test
    void handleActivityNoticeCreatedShouldContinueWhenSingleUserDispatchFails() {
        ActivityNoticeDispatchService service = buildService();
        ActivityNotice notice = buildNotice(20L);
        AppUser firstUser = buildUser(201L);
        AppUser secondUser = buildUser(202L);
        when(activityNoticeMapper.selectOne(any())).thenReturn(notice);
        when(userMapper.selectPage(any(), any())).thenReturn(buildUserPage(firstUser, secondUser));
        when(appUserActivityNoticeMapper.selectCount(any())).thenReturn(0L);
        doThrow(new IllegalStateException("推送异常"))
                .doReturn(null)
                .when(appSystemNoticeDispatchService).createAndPush(any(AppSystemNoticeDispatchCommand.class));
 
        service.handleActivityNoticeCreated(new ActivityNoticeCreatedEvent(20L));
 
        verify(appSystemNoticeDispatchService, org.mockito.Mockito.times(2)).createAndPush(commandCaptor.capture());
        assertEquals(Long.valueOf(201L), commandCaptor.getAllValues().get(0).getAppUserId());
        assertEquals(Long.valueOf(202L), commandCaptor.getAllValues().get(1).getAppUserId());
    }
 
    /**
     * 构造被测服务。
     */
    private ActivityNoticeDispatchService buildService() {
        return new ActivityNoticeDispatchService(
                userMapper,
                appUserActivityNoticeMapper,
                activityNoticeMapper,
                appSystemNoticeDispatchService
        );
    }
 
    /**
     * 构造活动通知主表记录。
     */
    private ActivityNotice buildNotice(Long id) {
        ActivityNotice notice = new ActivityNotice();
        notice.setId(id);
        notice.setTitle("活动通知");
        notice.setContentHtml("<p>活动内容</p>");
        notice.setDeleted(0);
        return notice;
    }
 
    /**
     * 构造启用 APP 用户。
     */
    private AppUser buildUser(Long id) {
        AppUser user = new AppUser();
        user.setId(id);
        user.setStatus(1);
        user.setDeleted(0);
        return user;
    }
 
    /**
     * 构造用户分页结果。
     */
    private Page<AppUser> buildUserPage(AppUser... users) {
        Page<AppUser> page = new Page<AppUser>(1, 500);
        page.setRecords(users == null ? Collections.<AppUser>emptyList() : Arrays.asList(users));
        page.setTotal(users == null ? 0 : users.length);
        return page;
    }
}