yanderong
2026-09-16 d4d70fe3a3a20f5fc29c9f37a1f89854a376597f
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
package com.ruoyi.account.controller;
 
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ruoyi.account.api.dto.ChargingCardAuthDTO;
import com.ruoyi.account.api.dto.ChargingCardBalanceDTO;
import com.ruoyi.account.api.dto.ChargingCardDeductDTO;
import com.ruoyi.account.api.dto.ChargingCardExcelDTO;
import com.ruoyi.account.api.dto.ChargingCardQueryDTO;
import com.ruoyi.account.api.dto.ChargingCardSaveDTO;
import com.ruoyi.account.api.dto.ChargingCardStatusDTO;
import com.ruoyi.account.api.model.TChargingCard;
import com.ruoyi.account.api.vo.ChargingCardDeductVO;
import com.ruoyi.account.service.TChargingCardService;
import com.ruoyi.common.core.domain.R;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.mockito.Mock;
import org.mockito.junit.jupiter.MockitoExtension;
import org.springframework.mock.web.MockHttpServletResponse;
import org.springframework.mock.web.MockMultipartFile;
import org.springframework.test.util.ReflectionTestUtils;
import org.junit.jupiter.api.extension.ExtendWith;
 
import java.util.Collections;
 
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertTrue;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;
 
@ExtendWith(MockitoExtension.class)
class TChargingCardControllerTest {
 
    @Mock
    private TChargingCardService service;
 
    private TChargingCardController controller;
 
    @BeforeEach
    void setUp() {
        controller = new TChargingCardController();
        ReflectionTestUtils.setField(controller, "chargingCardService", service);
    }
 
    @Test
    void pageDelegatesToService() {
        ChargingCardQueryDTO query = new ChargingCardQueryDTO();
        Page<TChargingCard> expected = new Page<>(1, 10);
        when(service.selectCardPage(query)).thenReturn(expected);
 
        R<?> result = controller.page(query);
 
        assertEquals(200, result.getCode());
        assertEquals(expected, result.getData());
        verify(service).selectCardPage(query);
    }
 
    @Test
    void addEditDeleteStatusAndBalanceDelegateToService() {
        ChargingCardSaveDTO save = new ChargingCardSaveDTO();
        ChargingCardStatusDTO status = new ChargingCardStatusDTO();
        ChargingCardBalanceDTO balance = new ChargingCardBalanceDTO();
        when(service.saveCard(save)).thenReturn(true);
        when(service.updateCard(save)).thenReturn(true);
        when(service.deleteCards(Collections.singletonList(1L))).thenReturn(true);
        when(service.updateStatus(status)).thenReturn(true);
        when(service.updateBalance(balance)).thenReturn(true);
 
        assertTrue(controller.add(save).getData());
        assertTrue(controller.edit(save).getData());
        assertTrue(controller.delete(Collections.singletonList(1L)).getData());
        assertTrue(controller.status(status).getData());
        assertTrue(controller.balance(balance).getData());
 
        verify(service).saveCard(save);
        verify(service).updateCard(save);
        verify(service).deleteCards(Collections.singletonList(1L));
        verify(service).updateStatus(status);
        verify(service).updateBalance(balance);
    }
 
    @Test
    void exportWritesExcelResponse() {
        ChargingCardQueryDTO query = new ChargingCardQueryDTO();
        ChargingCardExcelDTO card = new ChargingCardExcelDTO();
        card.setCardNo("CARD-1");
        when(service.selectExportList(query)).thenReturn(Collections.singletonList(card));
        MockHttpServletResponse response = new MockHttpServletResponse();
 
        controller.export(response, query);
 
        assertEquals(200, response.getStatus());
        assertTrue(response.getContentAsByteArray().length > 0);
        assertTrue(response.getContentType().contains("application/"));
        verify(service).selectExportList(query);
    }
 
    @Test
    void importTemplateWritesExcelResponse() {
        MockHttpServletResponse response = new MockHttpServletResponse();
 
        controller.importTemplate(response);
 
        assertEquals(200, response.getStatus());
        assertTrue(response.getContentAsByteArray().length > 0);
    }
 
    @Test
    void importDataReadsFileAndDelegates() throws Exception {
        MockMultipartFile file = new MockMultipartFile("file", "cards.xlsx",
                "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet", excelBytes());
        when(service.importCards(any())).thenReturn("成功导入1条卡片数据");
 
        R<String> result = controller.importData(file);
 
        assertEquals(200, result.getCode());
        assertEquals("成功导入1条卡片数据", result.getData());
        verify(service).importCards(any());
    }
 
    @Test
    void innerVerifyChargingDelegatesToService() {
        ChargingCardAuthDTO dto = new ChargingCardAuthDTO();
        dto.setCardNo("CARD-1");
        TChargingCard card = new TChargingCard();
        card.setId(1L);
        when(service.verifyCardForCharging(dto)).thenReturn(card);
 
        R<TChargingCard> result = controller.verifyCharging(dto);
 
        assertEquals(200, result.getCode());
        assertEquals(card, result.getData());
        verify(service).verifyCardForCharging(dto);
    }
 
    @Test
    void innerLockAndUnlockDelegateToService() {
        when(service.lockCardForCharging(1L, "SD20260909001")).thenReturn(true);
        when(service.unlockCardForCharging(1L, "SD20260909001")).thenReturn(true);
 
        assertTrue(controller.lockForCharging(1L, "SD20260909001").getData());
        assertTrue(controller.unlockForCharging(1L, "SD20260909001").getData());
        verify(service).lockCardForCharging(1L, "SD20260909001");
        verify(service).unlockCardForCharging(1L, "SD20260909001");
    }
 
    @Test
    void innerDeductedOrderCodesDelegatesToService() {
        java.util.List<String> orderCodes = Collections.singletonList("SD20260909001");
        when(service.listDeductedOrderCodes(orderCodes)).thenReturn(orderCodes);
 
        R<java.util.List<String>> result = controller.listDeductedOrderCodes(orderCodes);
 
        assertEquals(200, result.getCode());
        assertEquals(orderCodes, result.getData());
        verify(service).listDeductedOrderCodes(orderCodes);
    }
 
    @Test
    void innerDeductChargingDelegatesToService() {
        ChargingCardDeductDTO dto = new ChargingCardDeductDTO();
        dto.setCardId(1L);
        dto.setOrderCode("SD20260909001");
        dto.setAmount(java.math.BigDecimal.TEN);
        ChargingCardDeductVO vo = new ChargingCardDeductVO();
        vo.setDuplicated(false);
        when(service.deductBalanceForCharging(dto)).thenReturn(vo);
 
        R<ChargingCardDeductVO> result = controller.deductForCharging(dto);
 
        assertEquals(200, result.getCode());
        assertEquals(vo, result.getData());
        verify(service).deductBalanceForCharging(dto);
    }
 
    private byte[] excelBytes() throws Exception {
        org.apache.poi.xssf.usermodel.XSSFWorkbook workbook = new org.apache.poi.xssf.usermodel.XSSFWorkbook();
        try {
            org.apache.poi.ss.usermodel.Sheet sheet = workbook.createSheet("卡片数据");
            sheet.createRow(0).createCell(0).setCellValue("卡号");
            sheet.createRow(1).createCell(0).setCellValue("CARD-1");
            java.io.ByteArrayOutputStream output = new java.io.ByteArrayOutputStream();
            workbook.write(output);
            return output.toByteArray();
        } finally {
            workbook.close();
        }
    }
}