package com.trafficaudit.freightforecast.service;
|
|
import com.trafficaudit.auditengine.service.TrackMileageCitySourceRuleService;
|
import com.trafficaudit.dataimport.dto.ImportResult;
|
import com.trafficaudit.dataimport.mapper.H2032EnterpriseMonthlyMapper;
|
import com.trafficaudit.dataimport.mapper.ScaleSplitTransportMapper;
|
import com.trafficaudit.dataimport.mapper.TransportAuthVehicleMapper;
|
import com.trafficaudit.dataimport.mapper.VehicleTrackMileageMapper;
|
import com.trafficaudit.dataimport.service.DataImportService;
|
import com.trafficaudit.freightforecast.entity.FreightForecastReportRound;
|
import com.trafficaudit.freightforecast.mapper.FreightForecastAboveEnterpriseMapper;
|
import com.trafficaudit.freightforecast.mapper.FreightForecastAboveHistoryMapper;
|
import com.trafficaudit.freightforecast.mapper.FreightForecastBaseMonthMapper;
|
import com.trafficaudit.freightforecast.mapper.FreightForecastBelowResultMapper;
|
import com.trafficaudit.freightforecast.mapper.FreightForecastReportRoundMapper;
|
import org.apache.poi.ss.usermodel.Row;
|
import org.apache.poi.ss.usermodel.Sheet;
|
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
|
import org.junit.jupiter.api.Test;
|
import org.junit.jupiter.api.extension.ExtendWith;
|
import org.mockito.ArgumentCaptor;
|
import org.mockito.InjectMocks;
|
import org.mockito.Mock;
|
import org.mockito.junit.jupiter.MockitoExtension;
|
import org.springframework.jdbc.core.JdbcTemplate;
|
import org.springframework.mock.web.MockMultipartFile;
|
import org.springframework.web.multipart.MultipartFile;
|
|
import java.io.ByteArrayOutputStream;
|
import java.util.ArrayList;
|
import java.util.Collections;
|
import java.util.LinkedHashMap;
|
import java.util.Map;
|
|
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.ArgumentMatchers.any;
|
import static org.mockito.ArgumentMatchers.anyString;
|
import static org.mockito.ArgumentMatchers.eq;
|
import static org.mockito.Mockito.never;
|
import static org.mockito.Mockito.verify;
|
import static org.mockito.Mockito.when;
|
|
@ExtendWith(MockitoExtension.class)
|
class FreightForecastServiceImportTest {
|
|
@Mock
|
private FreightForecastBaseMonthMapper baseMapper;
|
@Mock
|
private FreightForecastBelowResultMapper belowMapper;
|
@Mock
|
private FreightForecastAboveEnterpriseMapper aboveMapper;
|
@Mock
|
private FreightForecastAboveHistoryMapper historyMapper;
|
@Mock
|
private FreightForecastReportRoundMapper roundMapper;
|
@Mock
|
private H2032EnterpriseMonthlyMapper h2032Mapper;
|
@Mock
|
private TransportAuthVehicleMapper authMapper;
|
@Mock
|
private VehicleTrackMileageMapper trackMapper;
|
@Mock
|
private TrackMileageCitySourceRuleService trackCitySourceRuleService;
|
@Mock
|
private ScaleSplitTransportMapper scaleSplitMapper;
|
@Mock
|
private JdbcTemplate jdbcTemplate;
|
@Mock
|
private DataImportService dataImportService;
|
|
@InjectMocks
|
private FreightForecastService service;
|
|
@Test
|
void importReportUpdatesH2032AndRecordsVersionLedger() throws Exception {
|
prepareAnnualEnterprises();
|
when(dataImportService.importH2032(any(MultipartFile.class), eq("2026-11")))
|
.thenReturn(new ImportResult(1, 0, new ArrayList<>()));
|
when(roundMapper.selectOne(any())).thenReturn(null);
|
|
FreightForecastReportRound result = service.importReport(
|
reportFile(), "2026-11", FreightForecastService.STAGE_AUDIT, "审核中第1版");
|
|
assertEquals("2026-11", result.getReportPeriod());
|
assertEquals(FreightForecastService.STAGE_AUDIT, result.getReportStage());
|
assertEquals(1, result.getRevisionNo());
|
assertEquals(1, result.getRowCount());
|
verify(dataImportService).importH2032(any(MultipartFile.class), eq("2026-11"));
|
|
ArgumentCaptor<FreightForecastReportRound> captor =
|
ArgumentCaptor.forClass(FreightForecastReportRound.class);
|
verify(roundMapper).insert(captor.capture());
|
assertEquals("2026年11月货运月报.xlsx", captor.getValue().getFileName());
|
}
|
|
@Test
|
void importReportRejectsFailedH2032WithoutRecordingVersion() throws Exception {
|
prepareAnnualEnterprises();
|
when(dataImportService.importH2032(any(MultipartFile.class), eq("2026-11")))
|
.thenReturn(new ImportResult(0, 1,
|
Collections.singletonList("第 2 行(测试物流有限公司):写入失败")));
|
|
IllegalArgumentException error = assertThrows(IllegalArgumentException.class,
|
() -> service.importReport(
|
reportFile(), "2026-11", FreightForecastService.STAGE_AUDIT, null));
|
|
assertTrue(error.getMessage().contains("已整体回滚"));
|
verify(roundMapper, never()).insert(any());
|
}
|
|
@Test
|
void importReportRejectsRowCountMismatchWithoutRecordingVersion() throws Exception {
|
prepareAnnualEnterprises();
|
when(dataImportService.importH2032(any(MultipartFile.class), eq("2026-11")))
|
.thenReturn(new ImportResult(0, 0, new ArrayList<>()));
|
|
IllegalArgumentException error = assertThrows(IllegalArgumentException.class,
|
() -> service.importReport(
|
reportFile(), "2026-11", FreightForecastService.STAGE_FINAL, null));
|
|
assertTrue(error.getMessage().contains("写入数量不一致"));
|
verify(roundMapper, never()).insert(any());
|
}
|
|
@Test
|
void importReportPropagatesH2032FailureWithoutRecordingVersion() throws Exception {
|
prepareAnnualEnterprises();
|
when(dataImportService.importH2032(any(MultipartFile.class), eq("2026-11")))
|
.thenThrow(new IllegalArgumentException("H2032 月报写入失败,已整体回滚"));
|
|
IllegalArgumentException error = assertThrows(IllegalArgumentException.class,
|
() -> service.importReport(
|
reportFile(), "2026-11", FreightForecastService.STAGE_AUDIT, null));
|
|
assertTrue(error.getMessage().contains("已整体回滚"));
|
verify(roundMapper, never()).insert(any());
|
}
|
|
private void prepareAnnualEnterprises() {
|
Map<String, Object> enterprise = new LinkedHashMap<>();
|
enterprise.put("enterprise_name", "测试物流有限公司");
|
enterprise.put("city", "武汉市");
|
when(jdbcTemplate.queryForList(anyString(), eq(2026)))
|
.thenReturn(Collections.singletonList(enterprise));
|
}
|
|
private MockMultipartFile reportFile() throws Exception {
|
try (XSSFWorkbook workbook = new XSSFWorkbook();
|
ByteArrayOutputStream output = new ByteArrayOutputStream()) {
|
Sheet sheet = workbook.createSheet("道路货物运输月度生产情况");
|
Row header = sheet.createRow(0);
|
header.createCell(0).setCellValue("所属地区");
|
header.createCell(1).setCellValue("企业名称");
|
header.createCell(2).setCellValue("货运量_总计");
|
header.createCell(3).setCellValue("货物周转量_总计");
|
Row row = sheet.createRow(1);
|
row.createCell(0).setCellValue("420100");
|
row.createCell(1).setCellValue("测试物流有限公司");
|
row.createCell(2).setCellValue(1000);
|
row.createCell(3).setCellValue(10000);
|
workbook.write(output);
|
return new MockMultipartFile("file", "2026年11月货运月报.xlsx",
|
"application/vnd.openxmlformats-officedocument.spreadsheetml.sheet",
|
output.toByteArray());
|
}
|
}
|
}
|