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 activityRecordCaptor; @Captor private ArgumentCaptor 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("

活动内容

"); 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 buildUserPage(AppUser... users) { Page page = new Page(1, 500); page.setRecords(users == null ? Collections.emptyList() : Arrays.asList(users)); page.setTotal(users == null ? 0 : users.length); return page; } }