package com.tcm.training.admin.member.service;
|
|
import cn.idev.excel.FastExcel;
|
import com.tcm.training.admin.member.mapper.ClinicQueryMapper;
|
import com.tcm.training.admin.member.row.ClinicListRow;
|
import org.junit.jupiter.api.Test;
|
import org.springframework.mock.web.MockHttpServletResponse;
|
|
import java.io.ByteArrayInputStream;
|
import java.util.Arrays;
|
import java.util.Collections;
|
import java.util.List;
|
import java.util.Map;
|
|
import static org.junit.jupiter.api.Assertions.assertEquals;
|
import static org.mockito.Mockito.mock;
|
import static org.mockito.Mockito.when;
|
|
class ClinicExportTest {
|
|
@Test
|
void shouldExportAddressAgentAndPlainPhone() throws Exception {
|
ClinicListRow row = new ClinicListRow();
|
row.setPhone("13800138000");
|
row.setAddress("人民路88号");
|
row.setAgentName("测试省代");
|
|
List<Map<Integer, String>> sheet = exportSheet(row);
|
|
assertEquals(Arrays.asList("序号", "诊所", "电话", "诊所区域", "详细地址",
|
"省代", "绑定用户数", "状态", "注册时间"),
|
Arrays.asList(sheet.get(0).values().toArray(new String[0])));
|
assertEquals("13800138000", sheet.get(1).get(2));
|
assertEquals("人民路88号", sheet.get(1).get(4));
|
assertEquals("测试省代", sheet.get(1).get(5));
|
}
|
|
@Test
|
void shouldUsePlaceholderForMissingAddressAndAgent() throws Exception {
|
List<Map<Integer, String>> sheet = exportSheet(new ClinicListRow());
|
|
assertEquals("-", sheet.get(1).get(4));
|
assertEquals("-", sheet.get(1).get(5));
|
}
|
|
/** 读取实际生成的 Excel,同时验证表头顺序和字段映射。 */
|
private List<Map<Integer, String>> exportSheet(ClinicListRow row) throws Exception {
|
ClinicQueryMapper mapper = mock(ClinicQueryMapper.class);
|
when(mapper.selectClinicList(null, null, null, null, null, null, null))
|
.thenReturn(Collections.singletonList(row));
|
ClinicAppService service = new ClinicAppService(mapper, null, null, null, null, null);
|
MockHttpServletResponse response = new MockHttpServletResponse();
|
service.export(null, null, null, null, null, null, null, response);
|
return FastExcel.read(new ByteArrayInputStream(response.getContentAsByteArray()))
|
.headRowNumber(0).sheet().doReadSync();
|
}
|
}
|