| New file |
| | |
| | | package com.trafficaudit.dataimport.service; |
| | | |
| | | import com.trafficaudit.dataimport.dto.ImportResult; |
| | | import com.trafficaudit.dataimport.entity.PassengerIndividualMonthly; |
| | | import com.trafficaudit.dataimport.mapper.ImportBatchMapper; |
| | | import com.trafficaudit.dataimport.mapper.PassengerIndividualMonthlyMapper; |
| | | 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 org.mockito.ArgumentCaptor; |
| | | import org.springframework.mock.web.MockMultipartFile; |
| | | |
| | | import java.io.ByteArrayOutputStream; |
| | | import java.lang.reflect.Field; |
| | | 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.mockito.Mockito.mock; |
| | | import static org.mockito.Mockito.verify; |
| | | |
| | | class PassengerIndividualImportTest { |
| | | |
| | | @Test |
| | | void importsOnlyTheBlockWhoseTitleMonthMatchesThePeriod() throws Exception { |
| | | PassengerIndividualMonthlyMapper mapper = mock(PassengerIndividualMonthlyMapper.class); |
| | | DataImportService service = new DataImportService(); |
| | | setField(service, "importBatchMapper", mock(ImportBatchMapper.class)); |
| | | setField(service, "passengerIndividualMapper", mapper); |
| | | |
| | | ImportResult result = service.importPassengerIndividual( |
| | | workbookFile(), "2026-08"); |
| | | |
| | | assertEquals(1, result.getSuccess()); |
| | | ArgumentCaptor<PassengerIndividualMonthly> captor = |
| | | ArgumentCaptor.forClass(PassengerIndividualMonthly.class); |
| | | verify(mapper).insert(captor.capture()); |
| | | List<PassengerIndividualMonthly> inserted = captor.getAllValues(); |
| | | assertEquals(1, inserted.size()); |
| | | assertEquals("\u5b9c\u660c\u5e02", inserted.get(0).getEnterpriseName()); |
| | | assertEquals(22.0d, inserted.get(0).getPassengerCount(), 0.001d); |
| | | assertEquals(220.0d, inserted.get(0).getTurnover(), 0.001d); |
| | | } |
| | | |
| | | @Test |
| | | void rejectsWhenNoTitleBlockMatchesThePeriod() throws Exception { |
| | | DataImportService service = new DataImportService(); |
| | | |
| | | Exception error = assertThrows(Exception.class, |
| | | () -> service.importPassengerIndividual(workbookFile(), "2026-09")); |
| | | |
| | | assertTrue(error.getMessage().contains("\u5168\u77019\u6708")); |
| | | assertTrue(error.getMessage().contains("[7, 8]")); |
| | | assertTrue(error.getMessage().contains("\u6807\u9898\u6708\u4efd\u4e0e\u6240\u9009\u62a5\u8868\u671f\u4e00\u81f4")); |
| | | } |
| | | |
| | | private static MockMultipartFile workbookFile() throws Exception { |
| | | try (Workbook workbook = new XSSFWorkbook()) { |
| | | Sheet sheet = workbook.createSheet("\u4e2a\u4f53\u6237\u5206\u5e02\u5dde"); |
| | | int nextRow = addMonthBlock(sheet, 0, 7, "\u6b66\u6c49", 11.0d, 110.0d); |
| | | addMonthBlock(sheet, nextRow, 8, "\u5b9c\u660c", 22.0d, 220.0d); |
| | | |
| | | ByteArrayOutputStream output = new ByteArrayOutputStream(); |
| | | workbook.write(output); |
| | | return new MockMultipartFile( |
| | | "file", "passenger-individual.xlsx", |
| | | "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet", |
| | | output.toByteArray()); |
| | | } |
| | | } |
| | | |
| | | private static int addMonthBlock(Sheet sheet, int startRow, int month, String city, |
| | | double passengerCount, double turnover) { |
| | | Row title = sheet.createRow(startRow); |
| | | title.createCell(0).setCellValue( |
| | | "\u5168\u7701" + month + "\u6708\u4e2a\u4f53\u5ba2\u8fd0\u91cf\u6c47\u603b\u8868"); |
| | | |
| | | Row header = sheet.createRow(startRow + 1); |
| | | header.createCell(0).setCellValue("\u5e02\u5dde"); |
| | | header.createCell(1).setCellValue("\u4e2a\u4f53\u5ba2\u8fd0\u91cf\uff08\u4eba\uff09"); |
| | | header.createCell(2).setCellValue("\u4e2a\u4f53\u65c5\u5ba2\u5468\u8f6c\u91cf\uff08\u4eba\u516c\u91cc\uff09"); |
| | | |
| | | Row data = sheet.createRow(startRow + 2); |
| | | data.createCell(0).setCellValue(city); |
| | | data.createCell(1).setCellValue(passengerCount); |
| | | data.createCell(2).setCellValue(turnover); |
| | | |
| | | Row total = sheet.createRow(startRow + 3); |
| | | total.createCell(0).setCellValue("\u5408\u8ba1"); |
| | | return startRow + 4; |
| | | } |
| | | |
| | | private static void setField(Object target, String fieldName, Object value) throws Exception { |
| | | Field field = DataImportService.class.getDeclaredField(fieldName); |
| | | field.setAccessible(true); |
| | | field.set(target, value); |
| | | } |
| | | } |