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 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 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 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 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()); } }