xyc
3 天以前 1c9505d00bd076898cdc5a7d916106e175a91119
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
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());
        }
    }
}