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