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
207
208
209
210
211
212
213
214
215
216
217
218
219
package com.ruoyi.account.service.impl;
 
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ruoyi.account.api.dto.ChargingCardBalanceDTO;
import com.ruoyi.account.api.dto.ChargingCardExcelDTO;
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.mapper.TChargingCardMapper;
import com.ruoyi.common.core.context.SecurityContextHolder;
import com.ruoyi.common.core.exception.ServiceException;
import com.ruoyi.common.core.domain.R;
import com.ruoyi.common.security.service.TokenService;
import com.ruoyi.system.api.domain.SysUser;
import com.ruoyi.system.api.feignClient.SysUserClient;
import com.ruoyi.system.api.model.LoginUser;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.mockito.ArgumentCaptor;
import org.mockito.Mock;
import org.mockito.junit.jupiter.MockitoExtension;
import org.springframework.test.util.ReflectionTestUtils;
 
import java.math.BigDecimal;
import java.util.Arrays;
import java.util.Collections;
 
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNotNull;
import static org.junit.jupiter.api.Assertions.assertSame;
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.anyList;
import static org.mockito.ArgumentMatchers.anyString;
import static org.mockito.ArgumentMatchers.isNull;
import static org.mockito.Mockito.doReturn;
import static org.mockito.Mockito.never;
import static org.mockito.Mockito.spy;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;
 
@ExtendWith(MockitoExtension.class)
class TChargingCardServiceImplTest {
 
    @Mock private TChargingCardMapper mapper;
    @Mock private TokenService tokenService;
    @Mock private SysUserClient sysUserClient;
    private TChargingCardServiceImpl service;
 
    @BeforeEach
    void setUp() {
        service = spy(new TChargingCardServiceImpl());
        ReflectionTestUtils.setField(service, "baseMapper", mapper);
        ReflectionTestUtils.setField(service, "tokenService", tokenService);
        ReflectionTestUtils.setField(service, "sysUserClient", sysUserClient);
        SecurityContextHolder.setUserId("123");
    }
 
    @AfterEach
    void tearDown() {
        SecurityContextHolder.remove();
    }
 
    @Test
    void selectCardPageUsesSafeDefaultsAndPassesQuery() {
        Page<TChargingCard> expected = new Page<>(1, 10);
        when(mapper.selectCardPage(any(Page.class), any())).thenReturn(expected);
 
        IPage<TChargingCard> result = service.selectCardPage(null);
 
        ArgumentCaptor<Page<TChargingCard>> page = ArgumentCaptor.forClass(Page.class);
        verify(mapper).selectCardPage(page.capture(), any());
        assertEquals(1, page.getValue().getCurrent());
        assertEquals(50, page.getValue().getSize());
        assertSame(expected, result);
    }
 
    @Test
    void saveCardTrimsAndSetsDefaults() {
        when(mapper.countByCardNo(anyString(), isNull())).thenReturn(0);
        when(mapper.insert(any(TChargingCard.class))).thenAnswer(invocation -> 1);
        ChargingCardSaveDTO dto = new ChargingCardSaveDTO();
        dto.setCardNo(" CARD-1 ");
 
        assertTrue(service.saveCard(dto));
 
        ArgumentCaptor<TChargingCard> card = ArgumentCaptor.forClass(TChargingCard.class);
        verify(mapper).insert(card.capture());
        assertEquals("CARD-1", card.getValue().getCardNo());
        assertEquals(BigDecimal.ZERO, card.getValue().getBalance());
        assertEquals(Integer.valueOf(1), card.getValue().getStatus());
        assertEquals(Integer.valueOf(0), card.getValue().getDelFlag());
        assertEquals(Long.valueOf(123), card.getValue().getCreateBy());
        assertNotNull(card.getValue().getCreateTime());
    }
 
    @Test
    void saveCardRejectsDuplicate() {
        when(mapper.countByCardNo("CARD-1", null)).thenReturn(1);
        ChargingCardSaveDTO dto = new ChargingCardSaveDTO();
        dto.setCardNo("CARD-1");
 
        ServiceException exception = assertThrows(ServiceException.class, () -> service.saveCard(dto));
 
        assertEquals("卡号已存在", exception.getMessage());
        verify(mapper, never()).insert(any());
    }
 
    @Test
    void updateCardRequiresExistingCardAndDoesNotChangeBalance() {
        ChargingCardSaveDTO dto = new ChargingCardSaveDTO();
        dto.setId(7L);
        dto.setCardNo(" CARD-7 ");
        dto.setBalance(new BigDecimal("999.99"));
        when(mapper.countByCardNo("CARD-7", 7L)).thenReturn(0);
        TChargingCard existing = new TChargingCard();
        existing.setId(7L);
        when(mapper.selectById(7L)).thenReturn(existing);
        when(mapper.updateById(any(TChargingCard.class))).thenReturn(1);
 
        assertTrue(service.updateCard(dto));
 
        ArgumentCaptor<TChargingCard> card = ArgumentCaptor.forClass(TChargingCard.class);
        verify(mapper).updateById(card.capture());
        assertEquals("CARD-7", card.getValue().getCardNo());
        assertEquals(null, card.getValue().getBalance());
    }
 
    @Test
    void deleteCardsRemovesDistinctIds() {
        TChargingCard first = new TChargingCard();
        first.setId(1L);
        TChargingCard second = new TChargingCard();
        second.setId(2L);
        doReturn(Arrays.asList(first, second)).when(service).listByIds(anyList());
        doReturn(true).when(service).removeByIds(anyList());
 
        assertTrue(service.deleteCards(Arrays.asList(1L, 1L, 2L)));
 
        verify(service).removeByIds(anyList());
    }
 
    @Test
    void updateStatusRejectsInvalidStatus() {
        ChargingCardStatusDTO dto = new ChargingCardStatusDTO();
        dto.setId(1L);
        dto.setStatus(3);
 
        ServiceException exception = assertThrows(ServiceException.class, () -> service.updateStatus(dto));
 
        assertEquals("卡片状态参数错误", exception.getMessage());
    }
 
    @Test
    void updateBalanceAdjustsAndRecordsForAllCards() {
        LoginUser loginUser = new LoginUser();
        loginUser.setUserid(123L);
        SysUser user = new SysUser();
        user.setOperationPassword(com.ruoyi.common.security.utils.SecurityUtils.encryptPassword("pwd"));
        when(tokenService.getLoginUser()).thenReturn(loginUser);
        when(sysUserClient.getSysUser(123L)).thenReturn(R.ok(user));
        TChargingCard first = new TChargingCard();
        first.setId(1L);
        TChargingCard second = new TChargingCard();
        second.setId(2L);
        when(mapper.selectBatchIds(anyList())).thenReturn(Arrays.asList(first, second));
        when(mapper.adjustBalance(anyList(), any(BigDecimal.class), any(Integer.class), any(BigDecimal.class), any(Long.class)))
                .thenReturn(2);
        ChargingCardBalanceDTO dto = new ChargingCardBalanceDTO();
        dto.setIds(Arrays.asList(1L, 2L));
        dto.setType(1);
        dto.setAmount(new BigDecimal("10.00"));
        dto.setOperationPassword("pwd");
        dto.setRemark(" test ");
 
        assertTrue(service.updateBalance(dto));
 
        verify(mapper).insertBalanceRecord(1L, 1, new BigDecimal("10.00"), "test", 123L);
        verify(mapper).insertBalanceRecord(2L, 1, new BigDecimal("10.00"), "test", 123L);
    }
 
    @Test
    void updateBalanceRejectsMissingPasswordBeforeDatabaseUpdate() {
        ChargingCardBalanceDTO dto = new ChargingCardBalanceDTO();
        dto.setIds(Collections.singletonList(1L));
        dto.setType(1);
        dto.setAmount(BigDecimal.ONE);
 
        ServiceException exception = assertThrows(ServiceException.class, () -> service.updateBalance(dto));
 
        assertEquals("请输入操作密码", exception.getMessage());
        verify(mapper, never()).adjustBalance(anyList(), any(), any(), any(), any());
    }
 
    @Test
    void importCardsRejectsDuplicateRows() {
        ChargingCardExcelDTO first = new ChargingCardExcelDTO();
        first.setCardNo("CARD-1");
        ChargingCardExcelDTO second = new ChargingCardExcelDTO();
        second.setCardNo(" CARD-1 ");
 
        ServiceException exception = assertThrows(ServiceException.class,
                () -> service.importCards(Arrays.asList(first, second)));
 
        assertEquals("第3行卡号在文件中重复", exception.getMessage());
    }
 
    @Test
    void selectExportListUsesEmptyQueryWhenNull() {
        when(mapper.selectExportList(any())).thenReturn(Collections.emptyList());
 
        assertTrue(service.selectExportList(null).isEmpty());
        verify(mapper).selectExportList(any());
    }
}