longz
2026-07-24 5b23c1a87779c9e7f17d56a5566dba7eac46d81a
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
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());
    }
}