package com.trafficaudit.dataimport.service;
|
|
import com.trafficaudit.dataimport.entity.VehicleTrackMileage;
|
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.assertNull;
|
import static org.junit.jupiter.api.Assertions.assertThrows;
|
import static org.junit.jupiter.api.Assertions.assertTrue;
|
|
class TrackMileageWorkbookParserTest {
|
private static final String ENTERPRISE_NAME = "\u4f01\u4e1a\u540d\u79f0";
|
private static final String MONTHLY_MILEAGE = "\u6708\u5ea6\u603b\u91cc\u7a0b(\u516c\u91cc)";
|
private static final String TRACKED_VEHICLES = "\u6709\u91cc\u7a0b\u7684\u8f66\u8f86\u6570";
|
private static final String ANOMALY_MILEAGE = "\u5f02\u5e38\u91cc\u7a0b(\u516c\u91cc)";
|
|
@Test
|
void parsesMultipleCategoriesAndOffsetHeader() throws Exception {
|
try (Workbook workbook = new XSSFWorkbook()) {
|
Sheet summary = workbook.createSheet("Summary");
|
summary.createRow(0).createCell(0).setCellValue("Summary only");
|
|
createDataSheet(workbook, "\u89c4\u4e0a\u4f01\u4e1a", 1, "Above Co", 1234.5, 7, 88.25);
|
createDataSheet(workbook, "\u4e24\u5ba2\u4f01\u4e1a", 0, "Passenger Co", 5678.9, 12, null);
|
|
List<VehicleTrackMileage> rows = TrackMileageWorkbookParser.parse(workbook, "2026-09");
|
|
assertEquals(2, rows.size());
|
assertEquals(VehicleTrackMileage.CATEGORY_ABOVE_SCALE, rows.get(0).getEnterpriseCategory());
|
assertEquals("Above Co", rows.get(0).getEnterpriseName());
|
assertEquals(1234.5, rows.get(0).getMonthlyMileage());
|
assertEquals(7, rows.get(0).getTrackedVehicles());
|
assertEquals(88.25, rows.get(0).getAnomalyMileage());
|
assertEquals(VehicleTrackMileage.CATEGORY_PASSENGER, rows.get(1).getEnterpriseCategory());
|
assertNull(rows.get(1).getAnomalyMileage());
|
}
|
}
|
|
@Test
|
void rejectsDuplicateEnterpriseWithinCategory() throws Exception {
|
try (Workbook workbook = new XSSFWorkbook()) {
|
Sheet sheet = workbook.createSheet("\u89c4\u4e0a\u4f01\u4e1a");
|
Row header = sheet.createRow(0);
|
header.createCell(0).setCellValue(ENTERPRISE_NAME);
|
header.createCell(1).setCellValue(MONTHLY_MILEAGE);
|
header.createCell(2).setCellValue(TRACKED_VEHICLES);
|
createRow(sheet, 1, "Duplicate Co", 100.0, 1, null);
|
createRow(sheet, 2, "Duplicate Co", 200.0, 2, null);
|
|
IllegalArgumentException error = assertThrows(IllegalArgumentException.class,
|
() -> TrackMileageWorkbookParser.parse(workbook, "2026-09"));
|
assertTrue(error.getMessage().contains("Duplicate enterprise"));
|
}
|
}
|
|
@Test
|
void rejectsWorkbookWithoutTrackHeaders() throws Exception {
|
try (Workbook workbook = new XSSFWorkbook()) {
|
workbook.createSheet("Summary").createRow(0).createCell(0).setCellValue("No data");
|
assertThrows(IllegalArgumentException.class,
|
() -> TrackMileageWorkbookParser.parse(workbook, "2026-09"));
|
}
|
}
|
|
private static void createDataSheet(Workbook workbook, String name, int headerRowIndex,
|
String enterprise, double mileage, int vehicles, Double anomaly) {
|
Sheet sheet = workbook.createSheet(name);
|
if (headerRowIndex > 0) {
|
sheet.createRow(0).createCell(0).setCellValue("Generated summary title");
|
}
|
Row header = sheet.createRow(headerRowIndex);
|
header.createCell(0).setCellValue(ENTERPRISE_NAME);
|
header.createCell(1).setCellValue(MONTHLY_MILEAGE);
|
header.createCell(2).setCellValue(TRACKED_VEHICLES);
|
header.createCell(3).setCellValue(ANOMALY_MILEAGE);
|
createRow(sheet, headerRowIndex + 1, enterprise, mileage, vehicles, anomaly);
|
}
|
|
private static void createRow(Sheet sheet, int rowIndex, String enterprise,
|
double mileage, int vehicles, Double anomaly) {
|
Row row = sheet.createRow(rowIndex);
|
row.createCell(0).setCellValue(enterprise);
|
row.createCell(1).setCellValue(mileage);
|
row.createCell(2).setCellValue(vehicles);
|
if (anomaly != null) row.createCell(3).setCellValue(anomaly);
|
}
|
}
|