xyc
2 小时以前 5b41c8896ef7850be2672e45b69bb58e149729ec
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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
package com.trafficaudit.dataimport.service;
 
import com.trafficaudit.dataimport.entity.TransportAuthVehicle;
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.xssf.usermodel.XSSFWorkbook;
import org.junit.jupiter.api.Test;
 
import java.util.List;
 
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.junit.jupiter.api.Assertions.assertTrue;
import static org.junit.jupiter.api.Assertions.assertNull;
 
class TransportAuthWorkbookParserTest {
    private static final String ENTERPRISE = "\u6d4b\u8bd5\u8fd0\u8f93\u6709\u9650\u516c\u53f8";
    private static final String CONTAINER_COUNT = "\u96c6\u88c5\u7bb1\u8f66\u8f86\u6570";
 
    @Test
    void parsesContainerCountFromNovemberWorkbook() throws Exception {
        try (Workbook workbook = workbook(true, "24")) {
            List<TransportAuthVehicle> rows = TransportAuthWorkbookParser.parse(workbook, "2026-11");
 
            assertEquals(1, rows.size());
            assertEquals(24, rows.get(0).getContainerCount());
            assertEquals(10, rows.get(0).getTractorCount());
            assertEquals(20, rows.get(0).getTrailerCount());
            assertEquals(5, rows.get(0).getOtherCount());
        }
    }
 
    @Test
    void keepsLegacyOctoberWorkbookCompatible() throws Exception {
        try (Workbook workbook = workbook(false, null)) {
            List<TransportAuthVehicle> rows = TransportAuthWorkbookParser.parse(workbook, "2026-10");
 
            assertEquals(1, rows.size());
            assertNull(rows.get(0).getContainerCount());
        }
    }
 
    @Test
    void distinguishesZeroFromMissingContainerCount() throws Exception {
        try (Workbook workbook = workbookWithContainerValues("0", null)) {
            List<TransportAuthVehicle> rows = TransportAuthWorkbookParser.parse(workbook, "2026-11");
 
            assertEquals(2, rows.size());
            assertEquals(0, rows.get(0).getContainerCount());
            assertNull(rows.get(1).getContainerCount());
        }
    }
 
    @Test
    void requiresContainerCountFromNovember() throws Exception {
        try (Workbook workbook = workbook(false, null)) {
            IllegalArgumentException error = assertThrows(IllegalArgumentException.class,
                () -> TransportAuthWorkbookParser.parse(workbook, "2026-11"));
 
            assertTrue(error.getMessage().contains(CONTAINER_COUNT));
            assertTrue(error.getMessage().contains("2026-11"));
        }
    }
 
    @Test
    void rejectsInvalidContainerCountWithExcelRow() throws Exception {
        try (Workbook workbook = workbook(true, "not-a-number")) {
            IllegalArgumentException error = assertThrows(IllegalArgumentException.class,
                () -> TransportAuthWorkbookParser.parse(workbook, "2026-11"));
 
            assertTrue(error.getMessage().contains("\u7b2c 2 \u884c"));
            assertTrue(error.getMessage().contains(CONTAINER_COUNT));
        }
    }
    private static Workbook workbookWithContainerValues(String... containerValues) {
        Workbook workbook = new XSSFWorkbook();
        Sheet sheet = workbook.createSheet("运政车辆");
        Row header = sheet.createRow(0);
        String[] headers = {"企业名称", "总牵引车数", "总挂车数", "其它车辆数", "挂车总吨位(吨)", "其它车辆总吨位(吨)", CONTAINER_COUNT};
        for (int i = 0; i < headers.length; i++) header.createCell(i).setCellValue(headers[i]);
        for (int i = 0; i < containerValues.length; i++) {
            Row row = sheet.createRow(i + 1);
            row.createCell(0).setCellValue(ENTERPRISE + (i + 1));
            row.createCell(1).setCellValue(10);
            row.createCell(2).setCellValue(20);
            row.createCell(3).setCellValue(5);
            row.createCell(4).setCellValue(100.5);
            row.createCell(5).setCellValue(25.25);
            if (containerValues[i] != null) row.createCell(6).setCellValue(containerValues[i]);
        }
        return workbook;
    }
 
    private static Workbook workbook(boolean includeContainer, String containerValue) {
        Workbook workbook = new XSSFWorkbook();
        Sheet sheet = workbook.createSheet("运政车辆");
        Row header = sheet.createRow(0);
        String[] headers = includeContainer
            ? new String[]{"企业名称", "总牵引车数", "总挂车数", "其它车辆数", "挂车总吨位(吨)", "其它车辆总吨位(吨)", CONTAINER_COUNT}
            : new String[]{"企业名称", "总牵引车数", "总挂车数", "其它车辆数", "挂车总吨位(吨)", "其它车辆总吨位(吨)"};
        for (int i = 0; i < headers.length; i++) header.createCell(i).setCellValue(headers[i]);
 
        Row row = sheet.createRow(1);
        row.createCell(0).setCellValue(ENTERPRISE);
        row.createCell(1).setCellValue(10);
        row.createCell(2).setCellValue(20);
        row.createCell(3).setCellValue(5);
        row.createCell(4).setCellValue(100.5);
        row.createCell(5).setCellValue(25.25);
        if (includeContainer && containerValue != null) row.createCell(6).setCellValue(containerValue);
        return workbook;
    }
}