puzhibing
5 小时以前 26795c6ce013bb204fa57a8324db7a7c77aea5b8
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
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
package com.ruoyi.order.service.impl;
 
import com.baomidou.mybatisplus.core.MybatisConfiguration;
import com.baomidou.mybatisplus.core.metadata.TableInfoHelper;
import com.ruoyi.chargingPile.api.dto.GetSiteListDTO;
import com.ruoyi.chargingPile.api.feignClient.ChargingGunClient;
import com.ruoyi.chargingPile.api.feignClient.ChargingPileClient;
import com.ruoyi.chargingPile.api.feignClient.SiteClient;
import com.ruoyi.chargingPile.api.model.TChargingGun;
import com.ruoyi.chargingPile.api.model.TChargingPile;
import com.ruoyi.common.core.domain.R;
import com.ruoyi.common.core.web.domain.AjaxResult;
import com.ruoyi.common.security.service.TokenService;
import com.ruoyi.integration.api.feignClient.SendMessageClient;
import com.ruoyi.integration.api.model.PlatformStopCharging;
import com.ruoyi.order.api.model.TCardChargingOrder;
import com.ruoyi.order.mapper.TCardChargingOrderMapper;
import com.ruoyi.system.api.model.LoginUser;
import org.apache.ibatis.builder.MapperBuilderAssistant;
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.InjectMocks;
import org.mockito.Mock;
import org.mockito.junit.jupiter.MockitoExtension;
import org.springframework.test.util.ReflectionTestUtils;
 
import java.util.Collections;
 
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.Mockito.lenient;
import static org.mockito.Mockito.never;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;
 
/**
 * 管理后台刷卡充电订单「停止充电」单测。
 * 覆盖:数据范围校验、充电枪匹配、状态拦截、条件更新并发保护、停机指令下发。
 */
@ExtendWith(MockitoExtension.class)
class TCardChargingOrderStopChargingTest {
 
    private static final Long LOGIN_USER_ID = 9L;
    private static final Integer AUTHORIZED_SITE_ID = 7;
    private static final Long ORDER_ID = 11L;
    private static final Integer GUN_ID = 22;
    private static final Integer PILE_ID = 33;
 
    @InjectMocks
    private TCardChargingOrderServiceImpl service;
    @Mock
    private TCardChargingOrderMapper mapper;
    @Mock
    private TokenService tokenService;
    @Mock
    private SiteClient siteClient;
    @Mock
    private ChargingGunClient chargingGunClient;
    @Mock
    private ChargingPileClient chargingPileClient;
    @Mock
    private SendMessageClient sendMessageClient;
 
    @BeforeEach
    void setUp() {
        ReflectionTestUtils.setField(service, "baseMapper", mapper);
        // lambdaUpdate 解析 getter 引用依赖 MyBatis-Plus 实体元信息缓存,单测环境需显式初始化
        TableInfoHelper.initTableInfo(new MapperBuilderAssistant(new MybatisConfiguration(), ""),
                TCardChargingOrder.class);
        // 默认:条件更新成功
        lenient().when(mapper.update(any(), any())).thenReturn(1);
    }
 
    // ===== 一、正常停充 =====
 
    @Test
    void stopChargingAdvancesStatusAndSendsCommand() {
        TCardChargingOrder order = order(3);
        when(mapper.selectById(ORDER_ID)).thenReturn(order);
        mockAuthorizedLogin();
        mockDevice(order);
 
        AjaxResult result = service.stopCharging(String.valueOf(ORDER_ID), GUN_ID);
 
        assertEquals(Integer.valueOf(200), result.get("code"));
        ArgumentCaptor<PlatformStopCharging> captor = ArgumentCaptor.forClass(PlatformStopCharging.class);
        verify(sendMessageClient).platformStopCharging(captor.capture());
        assertEquals("PILE-33", captor.getValue().getCharging_pile_code());
        assertEquals("GUN-22", captor.getValue().getCharging_gun_code());
        assertEquals(Integer.valueOf(4), order.getStatus());
        assertEquals(Integer.valueOf(1), order.getEndMode());
        verify(mapper).update(any(), any());
    }
 
    // ===== 二、入参与订单校验 =====
 
    @Test
    void rejectsInvalidOrderId() {
        AjaxResult result = service.stopCharging("abc", GUN_ID);
 
        assertEquals(Integer.valueOf(500), result.get("code"));
        assertEquals("刷卡充电订单ID无效", result.get("msg"));
        verify(sendMessageClient, never()).platformStopCharging(any(PlatformStopCharging.class));
    }
 
    @Test
    void rejectsOrderNotFound() {
        when(mapper.selectById(ORDER_ID)).thenReturn(null);
 
        AjaxResult result = service.stopCharging(String.valueOf(ORDER_ID), GUN_ID);
 
        assertEquals(Integer.valueOf(500), result.get("code"));
        assertEquals("刷卡充电订单不存在或无权操作", result.get("msg"));
        verify(sendMessageClient, never()).platformStopCharging(any(PlatformStopCharging.class));
    }
 
    @Test
    void rejectsOrderOutsideAuthorizationScope() {
        TCardChargingOrder order = order(3);
        order.setSiteId(AUTHORIZED_SITE_ID + 1);
        when(mapper.selectById(ORDER_ID)).thenReturn(order);
        mockAuthorizedLogin();
 
        AjaxResult result = service.stopCharging(String.valueOf(ORDER_ID), GUN_ID);
 
        assertEquals(Integer.valueOf(500), result.get("code"));
        assertEquals("刷卡充电订单不存在或无权操作", result.get("msg"));
        verify(sendMessageClient, never()).platformStopCharging(any(PlatformStopCharging.class));
    }
 
    @Test
    void rejectsMismatchedGun() {
        TCardChargingOrder order = order(3);
        when(mapper.selectById(ORDER_ID)).thenReturn(order);
        mockAuthorizedLogin();
 
        AjaxResult result = service.stopCharging(String.valueOf(ORDER_ID), GUN_ID + 1);
 
        assertEquals(Integer.valueOf(500), result.get("code"));
        assertEquals("充电枪与订单不匹配", result.get("msg"));
        verify(sendMessageClient, never()).platformStopCharging(any(PlatformStopCharging.class));
    }
 
    // ===== 三、状态拦截 =====
 
    @Test
    void rejectsOrderNotStartedCharge() {
        TCardChargingOrder order = order(2);
        when(mapper.selectById(ORDER_ID)).thenReturn(order);
        mockAuthorizedLogin();
 
        AjaxResult result = service.stopCharging(String.valueOf(ORDER_ID), GUN_ID);
 
        assertEquals(Integer.valueOf(500), result.get("code"));
        assertEquals("还未开始充电", result.get("msg"));
        verify(sendMessageClient, never()).platformStopCharging(any(PlatformStopCharging.class));
    }
 
    @Test
    void rejectsRepeatedStopOperation() {
        TCardChargingOrder order = order(4);
        when(mapper.selectById(ORDER_ID)).thenReturn(order);
        mockAuthorizedLogin();
 
        AjaxResult result = service.stopCharging(String.valueOf(ORDER_ID), GUN_ID);
 
        assertEquals(Integer.valueOf(500), result.get("code"));
        assertEquals("不能重复操作", result.get("msg"));
        verify(sendMessageClient, never()).platformStopCharging(any(PlatformStopCharging.class));
    }
 
    // ===== 四、设备与并发 =====
 
    @Test
    void rejectsWhenDeviceMissing() {
        TCardChargingOrder order = order(3);
        when(mapper.selectById(ORDER_ID)).thenReturn(order);
        mockAuthorizedLogin();
 
        AjaxResult result = service.stopCharging(String.valueOf(ORDER_ID), GUN_ID);
 
        assertEquals(Integer.valueOf(500), result.get("code"));
        assertEquals("未找到订单对应的充电设备", result.get("msg"));
        verify(sendMessageClient, never()).platformStopCharging(any(PlatformStopCharging.class));
    }
 
    @Test
    void rejectsWhenStatusChangedConcurrently() {
        lenient().when(mapper.update(any(), any())).thenReturn(0);
        TCardChargingOrder order = order(3);
        when(mapper.selectById(ORDER_ID)).thenReturn(order);
        mockAuthorizedLogin();
        mockDevice(order);
 
        AjaxResult result = service.stopCharging(String.valueOf(ORDER_ID), GUN_ID);
 
        assertEquals(Integer.valueOf(500), result.get("code"));
        assertEquals("订单状态已变化,请刷新后重试", result.get("msg"));
        verify(sendMessageClient, never()).platformStopCharging(any(PlatformStopCharging.class));
    }
 
    // ===== 辅助方法 =====
 
    private TCardChargingOrder order(Integer status) {
        TCardChargingOrder order = new TCardChargingOrder();
        order.setId(ORDER_ID);
        order.setCode("SD202610140001");
        order.setSiteId(AUTHORIZED_SITE_ID);
        order.setChargingGunId(GUN_ID);
        order.setChargingPileId(PILE_ID);
        order.setStatus(status);
        return order;
    }
 
    private void mockAuthorizedLogin() {
        LoginUser loginUser = new LoginUser();
        loginUser.setUserid(LOGIN_USER_ID);
        lenient().when(tokenService.getLoginUser()).thenReturn(loginUser);
        GetSiteListDTO site = new GetSiteListDTO();
        site.setId(AUTHORIZED_SITE_ID);
        lenient().when(siteClient.getSiteListByUserId(LOGIN_USER_ID))
                .thenReturn(R.ok(Collections.singletonList(site)));
    }
 
    private void mockDevice(TCardChargingOrder order) {
        TChargingGun gun = new TChargingGun();
        gun.setId(order.getChargingGunId());
        gun.setCode("GUN-" + order.getChargingGunId());
        lenient().when(chargingGunClient.getChargingGunById(order.getChargingGunId())).thenReturn(R.ok(gun));
        TChargingPile pile = new TChargingPile();
        pile.setId(order.getChargingPileId());
        pile.setCode("PILE-" + order.getChargingPileId());
        lenient().when(chargingPileClient.getChargingPileById(order.getChargingPileId())).thenReturn(R.ok(pile));
    }
}