puzhibing
2026-09-01 2190401551e7eed22a17777b8733d0e7092d7c3e
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
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
package com.ruoyi.account.controller;
 
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ruoyi.account.api.dto.VinManagementExcelDTO;
import com.ruoyi.account.api.dto.VinManagementQueryDTO;
import com.ruoyi.account.api.dto.VinManagementSaveDTO;
import com.ruoyi.account.api.model.TVinManagement;
import com.ruoyi.account.service.TVinManagementService;
import com.ruoyi.common.core.domain.R;
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.mockito.ArgumentCaptor;
import org.springframework.mock.web.MockHttpServletResponse;
import org.springframework.mock.web.MockMultipartFile;
import org.springframework.test.util.ReflectionTestUtils;
 
import java.io.ByteArrayOutputStream;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
 
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertSame;
import static org.junit.jupiter.api.Assertions.assertTrue;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.ArgumentMatchers.anyList;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.verifyNoInteractions;
import static org.mockito.Mockito.when;
 
class TVinManagementControllerTest {
 
    private TVinManagementController buildController(TVinManagementService service) {
        TVinManagementController controller = new TVinManagementController();
        ReflectionTestUtils.setField(controller, "service", service);
        return controller;
    }
 
    @Test
    void pageReturnsOkWithPageData() {
        TVinManagementService service = mock(TVinManagementService.class);
        Page<TVinManagement> page = new Page<>(1, 10);
        page.setRecords(Collections.singletonList(new TVinManagement()));
        when(service.selectPageList(any())).thenReturn(page);
        TVinManagementController controller = buildController(service);
 
        VinManagementQueryDTO query = new VinManagementQueryDTO();
        query.setVin("LSV");
 
        R<?> result = controller.page(query);
 
        assertEquals(200, result.getCode());
        assertSame(page, result.getData());
        verify(service).selectPageList(query);
    }
 
    @Test
    void addReturnsOkWhenSaved() {
        TVinManagementService service = mock(TVinManagementService.class);
        when(service.saveVin(any())).thenReturn(true);
        TVinManagementController controller = buildController(service);
 
        VinManagementSaveDTO dto = new VinManagementSaveDTO();
        dto.setVin("LSVAM4187C2184842");
 
        R<Boolean> result = controller.add(dto);
 
        assertEquals(200, result.getCode());
        assertEquals(Boolean.TRUE, result.getData());
        verify(service).saveVin(dto);
    }
 
    @Test
    void editReturnsOkWhenUpdated() {
        TVinManagementService service = mock(TVinManagementService.class);
        when(service.updateVin(any())).thenReturn(true);
        TVinManagementController controller = buildController(service);
 
        VinManagementSaveDTO dto = new VinManagementSaveDTO();
        dto.setId(9L);
        dto.setVin("LSVAM4187C2184842");
 
        R<Boolean> result = controller.edit(dto);
 
        assertEquals(200, result.getCode());
        assertEquals(Boolean.TRUE, result.getData());
        verify(service).updateVin(dto);
    }
 
    @Test
    void deleteReturnsOkWhenRemoved() {
        TVinManagementService service = mock(TVinManagementService.class);
        when(service.deleteVins(anyList())).thenReturn(true);
        TVinManagementController controller = buildController(service);
 
        List<Long> ids = Arrays.asList(1L, 2L);
 
        R<Boolean> result = controller.delete(ids);
 
        assertEquals(200, result.getCode());
        assertEquals(Boolean.TRUE, result.getData());
        verify(service).deleteVins(ids);
    }
 
    @Test
    void statusDelegatesIdAndStatusFromDto() {
        TVinManagementService service = mock(TVinManagementService.class);
        when(service.updateStatus(9L, 2)).thenReturn(true);
        TVinManagementController controller = buildController(service);
 
        VinManagementSaveDTO dto = new VinManagementSaveDTO();
        dto.setId(9L);
        dto.setStatus(2);
 
        R<Boolean> result = controller.status(dto);
 
        assertEquals(200, result.getCode());
        assertEquals(Boolean.TRUE, result.getData());
        verify(service).updateStatus(9L, 2);
    }
 
    @Test
    void exportWritesExcelAndDelegatesQuery() throws Exception {
        TVinManagementService service = mock(TVinManagementService.class);
        VinManagementExcelDTO item = new VinManagementExcelDTO();
        item.setVin("LSVAM4187C2184842");
        item.setStatus("1");
        when(service.selectExportList(any())).thenReturn(Collections.singletonList(item));
        TVinManagementController controller = buildController(service);
 
        MockHttpServletResponse response = new MockHttpServletResponse();
        VinManagementQueryDTO query = new VinManagementQueryDTO();
        query.setStatus(1);
 
        controller.export(response, query);
 
        assertTrue(response.getContentType().contains("spreadsheetml"));
        assertTrue(response.getContentAsByteArray().length > 0);
        verify(service).selectExportList(query);
    }
 
    @Test
    void templateDownloadsImportTemplateExcel() {
        TVinManagementService service = mock(TVinManagementService.class);
        TVinManagementController controller = buildController(service);
 
        MockHttpServletResponse response = new MockHttpServletResponse();
 
        controller.template(response);
 
        assertTrue(response.getContentType().contains("spreadsheetml"));
        assertTrue(response.getContentAsByteArray().length > 0);
        verifyNoInteractions(service);
    }
 
    @Test
    void importDataParsesExcelAndReturnsResult() throws Exception {
        TVinManagementService service = mock(TVinManagementService.class);
        when(service.importVins(anyList())).thenReturn("成功导入1条VIN码数据");
        TVinManagementController controller = buildController(service);
 
        MockMultipartFile file = vinExcelFile();
 
        R<String> result = controller.importData(file);
 
        assertEquals(200, result.getCode());
        assertEquals("成功导入1条VIN码数据", result.getData());
        @SuppressWarnings("unchecked")
        ArgumentCaptor<List<VinManagementExcelDTO>> captor = ArgumentCaptor.forClass(List.class);
        verify(service).importVins(captor.capture());
        assertEquals(1, captor.getValue().size());
        VinManagementExcelDTO parsed = captor.getValue().get(0);
        assertEquals("LSVAM4187C2184842", parsed.getVin());
        assertEquals("川A12345", parsed.getPlateNumber());
        assertEquals("13800138000", parsed.getPhone());
        assertEquals("测试公司", parsed.getCompanyName());
        assertEquals("备注", parsed.getRemark());
        assertEquals("1", parsed.getStatus());
    }
 
    private MockMultipartFile vinExcelFile() throws Exception {
        try (XSSFWorkbook workbook = new XSSFWorkbook(); ByteArrayOutputStream output = new ByteArrayOutputStream()) {
            Sheet sheet = workbook.createSheet("VIN码数据");
            Row header = sheet.createRow(0);
            header.createCell(0).setCellValue("VIN码");
            header.createCell(1).setCellValue("车牌号");
            header.createCell(2).setCellValue("手机号");
            header.createCell(3).setCellValue("所属公司");
            header.createCell(4).setCellValue("备注");
            header.createCell(5).setCellValue("启用状态");
            Row row = sheet.createRow(1);
            row.createCell(0).setCellValue("LSVAM4187C2184842");
            row.createCell(1).setCellValue("川A12345");
            row.createCell(2).setCellValue("13800138000");
            row.createCell(3).setCellValue("测试公司");
            row.createCell(4).setCellValue("备注");
            row.createCell(5).setCellValue("启用");
            workbook.write(output);
            return new MockMultipartFile("file", "vin.xlsx",
                    "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet", output.toByteArray());
        }
    }
}