package com.sinata.appointment.service; import static org.assertj.core.api.Assertions.assertThat; import static org.mockito.ArgumentMatchers.any; import static org.mockito.Mockito.mock; import static org.mockito.Mockito.when; import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper; import com.baomidou.mybatisplus.extension.plugins.pagination.Page; import com.fasterxml.jackson.databind.ObjectMapper; import com.sinata.appointment.entity.AppointmentOrder; import com.sinata.appointment.entity.MpUser; import com.sinata.appointment.mapper.AppointmentOrderMapper; import com.sinata.appointment.mapper.AppointmentVehicleMapper; import com.sinata.appointment.mapper.AppointmentVisitorMapper; import com.sinata.appointment.mapper.ApprovalInstanceMapper; import com.sinata.appointment.mapper.ApprovalTaskMapper; import com.sinata.appointment.mapper.BizVisitReasonMapper; import com.sinata.appointment.mapper.MpPassAccessAreaMapper; import com.sinata.appointment.mapper.MpUserMapper; import com.sinata.appointment.mapper.MpVisitorMapper; import com.sinata.appointment.mapper.SysDeptMapper; import jakarta.servlet.http.HttpServletResponse; import java.util.List; import org.junit.jupiter.api.Test; import org.springframework.mock.web.MockHttpServletResponse; class ExportStreamingTest { @Test void appointmentExportWritesWorkbookToResponseStream() throws Exception { AppointmentOrderMapper appointmentOrderMapper = mock(AppointmentOrderMapper.class); when(appointmentOrderMapper.pageAdminOrders(any(Page.class), any(), any(), any(), any(), any(), any(), any(), any(), any(), any(), any())) .thenReturn(new Page(1, 200)); AppointmentApplicationService service = new AppointmentApplicationService( appointmentOrderMapper, mock(AppointmentVehicleMapper.class), mock(AppointmentVisitorMapper.class), mock(ApprovalInstanceMapper.class), mock(ApprovalTaskMapper.class), mock(BizVisitReasonMapper.class), mock(MpUserMapper.class), mock(MpVisitorMapper.class), mock(MpPassAccessAreaMapper.class), mock(SysDeptMapper.class), new ObjectMapper()); MockHttpServletResponse response = new MockHttpServletResponse(); service.exportExcel(response, null, null, null, null, null, null, null, null, null, null, null); assertWorkbookResponse(response, "appointments.xlsx"); } @Test void visitorExportWritesWorkbookToResponseStream() throws Exception { MpUserMapper mpUserMapper = mock(MpUserMapper.class); when(mpUserMapper.selectPage(any(Page.class), any(LambdaQueryWrapper.class))) .thenReturn(new Page(1, 500)); VisitorUserManageApplicationService service = new VisitorUserManageApplicationService( mpUserMapper, mock(com.sinata.appointment.mapper.AppointmentOrderMapper.class)); MockHttpServletResponse response = new MockHttpServletResponse(); service.exportExcel(response, null, null, null, null, null, null); assertWorkbookResponse(response, "visitor-users.xlsx"); } private void assertWorkbookResponse(MockHttpServletResponse response, String filename) { assertThat(response.getContentType()) .contains("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet"); assertThat(response.getHeader("Content-Disposition")).contains(filename); assertThat(response.getContentAsByteArray()).startsWith(new byte[] {'P', 'K'}); } }