puzhibing
2026-09-09 aa4b21bea0e3278872801183a434527ebd25c09c
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
package com.ruoyi.order.service.impl;
 
import com.ruoyi.chargingPile.api.feignClient.ChargingGunClient;
import com.ruoyi.chargingPile.api.model.TChargingGun;
import com.ruoyi.order.api.model.TVinChargingOrder;
import com.ruoyi.order.mapper.TVinChargingOrderMapper;
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.time.LocalDateTime;
import java.util.Collections;
 
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertNotNull;
import static org.junit.jupiter.api.Assertions.assertNull;
import static org.junit.jupiter.api.Assertions.assertTrue;
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;
 
/**
 * VD 握手处理单测:VIN 回写 + 仅状态=启动中(2)推进为充电中(3)。
 */
@ExtendWith(MockitoExtension.class)
class TVinChargingOrderHandshakeTest {
 
    private static final String VIN = "LFV3A23C8K3123456";
 
    @InjectMocks
    private TVinChargingOrderServiceImpl service;
    @Mock
    private TVinChargingOrderMapper mapper;
    @Mock
    private ChargingGunClient chargingGunClient;
 
    @BeforeEach
    void setUp() {
        ReflectionTestUtils.setField(service, "baseMapper", mapper);
    }
 
    private void mockOrder(TVinChargingOrder order) {
        lenient().when(mapper.selectOne(any())).thenReturn(order);
        lenient().when(mapper.selectList(any()))
                .thenReturn(order == null ? Collections.emptyList() : Collections.singletonList(order));
    }
 
    private TVinChargingOrder order(Integer status) {
        TVinChargingOrder order = new TVinChargingOrder();
        order.setId(2001L);
        order.setCode("VD20260909001");
        order.setStatus(status);
        order.setChargingGunId(6);
        return order;
    }
 
    @Test
    void advancesStartingOrderToChargingAndWritesVin() {
        TVinChargingOrder order = order(2);
        mockOrder(order);
 
        assertTrue(service.handleHandshakeVin("VD20260909001", VIN));
 
        ArgumentCaptor<TVinChargingOrder> orderCaptor = ArgumentCaptor.forClass(TVinChargingOrder.class);
        verify(mapper).updateById(orderCaptor.capture());
        TVinChargingOrder update = orderCaptor.getValue();
        assertEquals(3, update.getStatus());
        assertEquals(VIN, update.getActualVin());
        assertNotNull(update.getStartTime());
 
        ArgumentCaptor<TChargingGun> gunCaptor = ArgumentCaptor.forClass(TChargingGun.class);
        verify(chargingGunClient).updateChargingGunById(gunCaptor.capture());
        TChargingGun gun = gunCaptor.getValue();
        assertEquals(Integer.valueOf(6), gun.getId());
        assertEquals(Integer.valueOf(4), gun.getStatus());
    }
 
    @Test
    void keepsExistingStartTimeWhenAdvancing() {
        TVinChargingOrder order = order(2);
        LocalDateTime existed = LocalDateTime.now().minusMinutes(10);
        order.setStartTime(existed);
        mockOrder(order);
 
        assertTrue(service.handleHandshakeVin("VD20260909001", VIN));
 
        ArgumentCaptor<TVinChargingOrder> captor = ArgumentCaptor.forClass(TVinChargingOrder.class);
        verify(mapper).updateById(captor.capture());
        assertNull(captor.getValue().getStartTime());
    }
 
    @Test
    void advancesStartingOrderEvenWhenVinBlank() {
        TVinChargingOrder order = order(2);
        mockOrder(order);
 
        assertTrue(service.handleHandshakeVin("VD20260909001", ""));
 
        ArgumentCaptor<TVinChargingOrder> captor = ArgumentCaptor.forClass(TVinChargingOrder.class);
        verify(mapper).updateById(captor.capture());
        assertEquals(3, captor.getValue().getStatus());
        assertNull(captor.getValue().getActualVin());
        verify(chargingGunClient).updateChargingGunById(any(TChargingGun.class));
    }
 
    @Test
    void doesNotReviveStartFailedOrder() {
        TVinChargingOrder order = order(-1);
        mockOrder(order);
 
        assertTrue(service.handleHandshakeVin("VD20260909001", VIN));
 
        ArgumentCaptor<TVinChargingOrder> captor = ArgumentCaptor.forClass(TVinChargingOrder.class);
        verify(mapper).updateById(captor.capture());
        assertNull(captor.getValue().getStatus());
        assertEquals(VIN, captor.getValue().getActualVin());
        verify(chargingGunClient, never()).updateChargingGunById(any(TChargingGun.class));
    }
 
    @Test
    void skipsAdvancementForChargingOrder() {
        TVinChargingOrder order = order(3);
        mockOrder(order);
 
        assertTrue(service.handleHandshakeVin("VD20260909001", VIN));
 
        ArgumentCaptor<TVinChargingOrder> captor = ArgumentCaptor.forClass(TVinChargingOrder.class);
        verify(mapper).updateById(captor.capture());
        assertNull(captor.getValue().getStatus());
        assertEquals(VIN, captor.getValue().getActualVin());
        verify(chargingGunClient, never()).updateChargingGunById(any(TChargingGun.class));
    }
 
    @Test
    void skipsUpdateEntirelyForFinishedOrderWithoutVin() {
        TVinChargingOrder order = order(5);
        mockOrder(order);
 
        assertTrue(service.handleHandshakeVin("VD20260909001", ""));
 
        verify(mapper, never()).updateById(any(TVinChargingOrder.class));
        verify(chargingGunClient, never()).updateChargingGunById(any(TChargingGun.class));
    }
 
    @Test
    void returnsFalseWhenOrderMissing() {
        mockOrder(null);
 
        assertFalse(service.handleHandshakeVin("VD20260909001", VIN));
 
        verify(mapper, never()).updateById(any(TVinChargingOrder.class));
        verify(chargingGunClient, never()).updateChargingGunById(any(TChargingGun.class));
    }
}