xyc
2026-11-12 cb13d459288e2732cb7adc30a8cf80121e3216db
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
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
package com.trafficaudit.dataimport.service;
 
import com.trafficaudit.dataimport.entity.CityTaxiAuth;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
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.Assumptions;
import org.junit.jupiter.api.Test;
 
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.InputStream;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.List;
 
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNotNull;
import static org.junit.jupiter.api.Assertions.assertNull;
import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.junit.jupiter.api.Assertions.assertTrue;
 
class CityTaxiAuthWorkbookParserTest {
    private static final String HEADER_PLATE = "\u8f66\u724c";
    private static final String PLATE = "\u9102A12345";
    private static final String VEHICLE_TYPE = "\u5c0f\u578b\u8f7f\u8f66";
    private static final String BIZ_TYPE = "\u516c\u53f8\u7ecf\u8425";
    private static final String OWNER = "\u5f20\u4e09";
    private static final String LICENSE = "420100600001";
    private static final String FUEL = "\u6c7d\u6cb9";
    private static final String ARCHIVE = "420100000001";
    private static final String ROAD_CERT = "\u6b66\u6c49" + "420100000002";
    private static final String OPERATOR = "\u6b66\u6c49\u67d0\u516c\u53f8";
    private static final String ADDRESS = "\u6e56\u5317\u7701\u6b66\u6c49\u5e02";
    private static final String OP_STATUS = "\u8425\u8fd0";
    private static final String CERT_STATUS = "\u6709\u6548";
 
    private final CityTaxiAuthWorkbookParser parser = new CityTaxiAuthWorkbookParser();
 
    @Test
    void parsesLegacyXlsWithEventExtractor() throws Exception {
        byte[] bytes = workbookBytes(new HSSFWorkbook(), true);
 
        List<CityTaxiAuth> rows = parser.parse(new ByteArrayInputStream(bytes));
 
        assertEquals(1, rows.size());
        assertFields(rows.get(0));
    }
 
    @Test
    void parsesXlsxWithWorkbookFactory() throws Exception {
        byte[] bytes = workbookBytes(new XSSFWorkbook(), true);
 
        List<CityTaxiAuth> rows = parser.parse(new ByteArrayInputStream(bytes));
 
        assertEquals(1, rows.size());
        assertFields(rows.get(0));
    }
 
    @Test
    void rejectsWrongHeader() throws Exception {
        byte[] bytes = workbookBytes(new HSSFWorkbook(), false);
 
        IllegalStateException error = assertThrows(IllegalStateException.class,
            () -> parser.parse(new ByteArrayInputStream(bytes)));
 
        assertTrue(error.getMessage().contains(HEADER_PLATE));
    }
 
    @Test
    void allowsShortLegacyRowAndLeavesMissingFieldsNull() throws Exception {
        byte[] bytes;
        try (HSSFWorkbook workbook = new HSSFWorkbook();
             ByteArrayOutputStream output = new ByteArrayOutputStream()) {
            Sheet sheet = workbook.createSheet();
            sheet.createRow(0).createCell(0).setCellValue(HEADER_PLATE);
            sheet.createRow(1).createCell(0).setCellValue(PLATE);
            workbook.write(output);
            bytes = output.toByteArray();
        }
 
        List<CityTaxiAuth> rows = parser.parse(new ByteArrayInputStream(bytes));
 
        assertEquals(1, rows.size());
        assertEquals(PLATE, rows.get(0).getPlateNo());
        assertNull(rows.get(0).getVehicleType());
        assertNull(rows.get(0).getRoadCertNo());
        assertNull(rows.get(0).getCertStatus());
    }
 
    @Test
    void parsesConfiguredProductionWorkbook() throws Exception {
        String configured = System.getProperty("cityTaxiAuth.source", "");
        Assumptions.assumeTrue(!configured.trim().isEmpty(),
            "cityTaxiAuth.source is not configured");
        Path path = Paths.get(configured);
        Assumptions.assumeTrue(Files.isRegularFile(path), "configured source file does not exist");
 
        long startedAt = System.nanoTime();
        List<CityTaxiAuth> rows;
        try (InputStream input = Files.newInputStream(path)) {
            rows = parser.parse(input);
        }
        long elapsedMs = (System.nanoTime() - startedAt) / 1_000_000;
 
        assertEquals(41469, rows.size());
        assertTrue(rows.stream().allMatch(row -> row.getPlateNo() != null));
        assertTrue(rows.stream().allMatch(row -> row.getRoadCertNo() != null));
        CityTaxiAuth sample = rows.stream()
            .filter(row -> "\u9102SD50883\u7eff".equals(row.getPlateNo()))
            .findFirst()
            .orElseThrow(() -> new AssertionError("production sample plate not found"));
        assertEquals("\u968f\u5dde422903000390", sample.getRoadCertNo());
        assertEquals("\u968f\u5dde421300600383", sample.getLicenseNo());
        assertEquals("\u8d75\u5e86", sample.getOwnerName());
        assertTrue(elapsedMs < 30_000, "legacy xls parsing took " + elapsedMs + " ms");
        System.out.println("cityTaxiAuth.source parsed " + rows.size()
            + " rows in " + elapsedMs + " ms");
    }
 
    private byte[] workbookBytes(Workbook workbook, boolean validHeader) throws Exception {
        try (ByteArrayOutputStream output = new ByteArrayOutputStream()) {
            Sheet sheet = workbook.createSheet();
            Row header = sheet.createRow(0);
            header.createCell(0).setCellValue(validHeader ? HEADER_PLATE : "other");
            Row row = sheet.createRow(1);
            row.createCell(0).setCellValue(PLATE);
            row.createCell(1).setCellValue(VEHICLE_TYPE);
            row.createCell(2).setCellValue(BIZ_TYPE);
            row.createCell(3).setCellValue(OWNER);
            row.createCell(4).setCellValue(LICENSE);
            row.createCell(7).setCellValue(FUEL);
            row.createCell(8).setCellValue(ARCHIVE);
            row.createCell(9).setCellValue(ROAD_CERT);
            row.createCell(17).setCellValue(OPERATOR);
            row.createCell(18).setCellValue(ADDRESS);
            row.createCell(19).setCellValue(OP_STATUS);
            row.createCell(20).setCellValue(CERT_STATUS);
            workbook.write(output);
            return output.toByteArray();
        } finally {
            workbook.close();
        }
    }
 
    private void assertFields(CityTaxiAuth row) {
        assertNotNull(row);
        assertEquals(PLATE, row.getPlateNo());
        assertEquals(VEHICLE_TYPE, row.getVehicleType());
        assertEquals(BIZ_TYPE, row.getBizType());
        assertEquals(OWNER, row.getOwnerName());
        assertEquals(LICENSE, row.getLicenseNo());
        assertEquals(FUEL, row.getFuelType());
        assertEquals(ARCHIVE, row.getArchiveNo());
        assertEquals(ROAD_CERT, row.getRoadCertNo());
        assertEquals(OPERATOR, row.getOperatorName());
        assertEquals(ADDRESS, row.getOperatorAddr());
        assertEquals(OP_STATUS, row.getOpStatus());
        assertEquals(CERT_STATUS, row.getCertStatus());
    }
}