package com.ruoyi.chargingPile.service.impl;
|
|
import com.ruoyi.chargingPile.dto.SiteExcelDTO;
|
import com.ruoyi.common.core.exception.ServiceException;
|
import com.ruoyi.common.core.utils.poi.ExcelUtil;
|
import org.apache.poi.ss.usermodel.Row;
|
import org.apache.poi.ss.usermodel.Sheet;
|
import org.apache.poi.ss.usermodel.Workbook;
|
import org.apache.poi.ss.usermodel.WorkbookFactory;
|
import org.junit.jupiter.api.Test;
|
import org.springframework.mock.web.MockHttpServletResponse;
|
import org.springframework.mock.web.MockMultipartFile;
|
|
import java.io.ByteArrayInputStream;
|
import java.io.ByteArrayOutputStream;
|
import java.util.List;
|
|
import static org.junit.jupiter.api.Assertions.assertDoesNotThrow;
|
import static org.junit.jupiter.api.Assertions.assertEquals;
|
import static org.junit.jupiter.api.Assertions.assertNotNull;
|
import static org.junit.jupiter.api.Assertions.assertThrows;
|
import static org.junit.jupiter.api.Assertions.assertTrue;
|
|
class SiteImportExcelTest {
|
|
@Test
|
void generatedTemplateCanBeOpenedAsXlsx() throws Exception {
|
MockHttpServletResponse response = new MockHttpServletResponse();
|
ExcelUtil<SiteExcelDTO> util = new ExcelUtil<>(SiteExcelDTO.class);
|
|
util.importTemplateExcel(response, "站点导入模板", "填写说明");
|
|
byte[] content = response.getContentAsByteArray();
|
assertTrue(content.length > 4);
|
assertEquals('P', content[0]);
|
assertEquals('K', content[1]);
|
assertDoesNotThrow(() -> {
|
try (Workbook ignored = WorkbookFactory.create(new ByteArrayInputStream(content))) {
|
// Opening and closing the generated workbook verifies the complete XLSX ZIP structure.
|
}
|
});
|
}
|
|
@Test
|
void generatedTemplateWithSecondRowHeaderCanBeImported() throws Exception {
|
MockHttpServletResponse response = new MockHttpServletResponse();
|
ExcelUtil<SiteExcelDTO> exportUtil = new ExcelUtil<>(SiteExcelDTO.class);
|
exportUtil.importTemplateExcel(response, "站点导入模板", "填写说明");
|
|
byte[] filledTemplate;
|
try (Workbook workbook = WorkbookFactory.create(
|
new ByteArrayInputStream(response.getContentAsByteArray()));
|
ByteArrayOutputStream output = new ByteArrayOutputStream()) {
|
Sheet sheet = workbook.getSheetAt(0);
|
Row header = sheet.getRow(1);
|
Row data = sheet.createRow(2);
|
for (int column = 0; column < header.getPhysicalNumberOfCells(); column++) {
|
String title = header.getCell(column).getStringCellValue();
|
if ("*站点编号".equals(title)) {
|
data.createCell(column).setCellValue("SITE-IMPORT-001");
|
} else if ("*站点名称".equals(title)) {
|
data.createCell(column).setCellValue("站点导入测试");
|
} else if ("*报装类型".equals(title)) {
|
data.createCell(column).setCellValue("独立报装");
|
}
|
}
|
workbook.write(output);
|
filledTemplate = output.toByteArray();
|
}
|
|
ExcelUtil<SiteExcelDTO> importUtil = new ExcelUtil<>(SiteExcelDTO.class);
|
List<SiteExcelDTO> rows = importUtil.importExcel(
|
new ByteArrayInputStream(filledTemplate), SiteServiceImpl.SITE_IMPORT_TITLE_ROWS);
|
|
assertEquals(1, rows.size());
|
assertEquals("SITE-IMPORT-001", rows.get(0).getCode());
|
assertEquals("站点导入测试", rows.get(0).getName());
|
assertEquals(1, rows.get(0).getBusinessExpandType());
|
}
|
|
@Test
|
void corruptWorkbookReturnsReadableBusinessError() {
|
SiteServiceImpl service = new SiteServiceImpl();
|
MockMultipartFile file = new MockMultipartFile(
|
"file", "站点导入模板.xlsx",
|
"application/vnd.openxmlformats-officedocument.spreadsheetml.sheet",
|
new byte[]{'P', 'K', 3, 4, 1, 2, 3});
|
|
ServiceException error = assertThrows(ServiceException.class,
|
() -> service.importSites(file, false));
|
|
assertEquals("Excel文件已损坏或格式不正确,请重新下载站点导入模板后填写", error.getMessage());
|
assertNotNull(error.getCause());
|
}
|
}
|