puzhibing
2026-09-28 83188a6c8ea2f123519378da736382a9136a4331
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
package com.ruoyi.order.service.impl;
 
import com.ruoyi.account.api.feignClient.VinManagementClient;
import com.ruoyi.account.api.model.TVinManagement;
import com.ruoyi.chargingPile.api.feignClient.ChargingGunClient;
import com.ruoyi.chargingPile.api.model.TChargingGun;
import com.ruoyi.common.core.domain.R;
import com.ruoyi.order.api.dto.DeviceStartChargingDTO;
import com.ruoyi.order.api.model.TVinChargingOrder;
import com.ruoyi.order.api.vo.ChargingOrderVO;
import com.ruoyi.order.api.vo.DeviceStartChargingVO;
import com.ruoyi.order.mapper.TVinChargingOrderMapper;
import com.ruoyi.other.api.domain.TFleet;
import com.ruoyi.other.api.domain.TGroupCompany;
import com.ruoyi.other.api.feignClient.GroupCompanyClient;
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.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import java.util.concurrent.TimeUnit;
 
import static org.junit.jupiter.api.Assertions.assertEquals;
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.ArgumentMatchers.anyList;
import static org.mockito.ArgumentMatchers.anyLong;
import static org.mockito.ArgumentMatchers.anyString;
import static org.mockito.Mockito.lenient;
import static org.mockito.Mockito.never;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;
 
/**
 * VIN码充电订单「所属公司/所属车队」单测:
 * 1) 建单时把 VIN 管理记录的 companyId/fleetId 作为快照写入订单;
 * 2) 列表/导出按订单快照 companyId/fleetId 批量远程取名,失败或缺失时保持为空且不抛异常。
 */
@ExtendWith(MockitoExtension.class)
class TVinChargingOrderCompanyFleetTest {
 
    private static final String VIN = "LFV3A23C8K3123456";
 
    @InjectMocks
    private TVinChargingOrderServiceImpl service;
 
    @Mock
    private TVinChargingOrderMapper mapper;
    @Mock
    private ChargingGunClient chargingGunClient;
    @Mock
    private VinManagementClient vinManagementClient;
    @Mock
    private GroupCompanyClient groupCompanyClient;
    @Mock
    private org.redisson.api.RedissonClient redissonClient;
    @Mock
    private org.redisson.api.RLock lock;
 
    private void fillCompanyFleetNames(List<ChargingOrderVO> list) {
        ReflectionTestUtils.invokeMethod(service, "fillCompanyFleetNames", list);
    }
 
    @Test
    void writesCompanyAndFleetSnapshotWhenBuildingOrder() throws Exception {
        ReflectionTestUtils.setField(service, "baseMapper", mapper);
        when(redissonClient.getLock(anyString())).thenReturn(lock);
        when(lock.tryLock(anyLong(), anyLong(), any(TimeUnit.class))).thenReturn(true);
        lenient().when(lock.isHeldByCurrentThread()).thenReturn(true);
        when(mapper.insert(any(TVinChargingOrder.class))).thenReturn(1);
 
        TChargingGun gun = new TChargingGun();
        gun.setId(6);
        when(chargingGunClient.getChargingGunByCode(any())).thenReturn(R.ok(gun));
 
        TVinManagement vinRecord = new TVinManagement();
        vinRecord.setId(1L);
        vinRecord.setVin(VIN);
        vinRecord.setPlateNumber("川A123456");
        vinRecord.setStatus(1);
        vinRecord.setCompanyId(12);
        vinRecord.setFleetId(3);
        when(vinManagementClient.getVinByCode(VIN)).thenReturn(R.ok(vinRecord));
 
        DeviceStartChargingDTO dto = new DeviceStartChargingDTO();
        dto.setChargingPileCode("P001");
        dto.setChargingGunCode("G001");
        dto.setAccount(new StringBuilder(VIN).reverse().toString());
 
        DeviceStartChargingVO vo = service.startVinCharging(dto);
 
        assertEquals(Integer.valueOf(1), vo.getAuthentication());
        ArgumentCaptor<TVinChargingOrder> captor = ArgumentCaptor.forClass(TVinChargingOrder.class);
        verify(mapper).insert(captor.capture());
        TVinChargingOrder saved = captor.getValue();
        assertEquals(Integer.valueOf(12), saved.getCompanyId());
        assertEquals(Integer.valueOf(3), saved.getFleetId());
        assertEquals(Long.valueOf(1L), saved.getVinId());
        assertEquals(VIN, saved.getActualVin());
        assertEquals(VIN, saved.getExpectedVin());
        assertEquals("川A123456", saved.getPlateNum());
    }
 
    @Test
    void fillsCompanyAndFleetNamesFromOrderSnapshot() {
        when(groupCompanyClient.listCompaniesByIds(anyList()))
                .thenReturn(R.ok(Collections.singletonList(company(12, "甲公司"))));
        when(groupCompanyClient.listFleetsByIds(anyList()))
                .thenReturn(R.ok(Collections.singletonList(fleet(3, "一队"))));
 
        ChargingOrderVO withFleet = order(12, 3);
        ChargingOrderVO companyOnly = order(12, null);
        ChargingOrderVO unmatched = order(99, 88);
        List<ChargingOrderVO> list = new ArrayList<>(Arrays.asList(withFleet, companyOnly, unmatched));
 
        fillCompanyFleetNames(list);
 
        assertEquals("甲公司", withFleet.getCompanyName());
        assertEquals("一队", withFleet.getFleetName());
        assertEquals("甲公司", companyOnly.getCompanyName());
        assertNull(companyOnly.getFleetName());
        assertNull(unmatched.getCompanyName());
        assertNull(unmatched.getFleetName());
 
        ArgumentCaptor<List<Integer>> companyIds = ArgumentCaptor.forClass(List.class);
        verify(groupCompanyClient).listCompaniesByIds(companyIds.capture());
        assertTrue(companyIds.getValue().containsAll(Arrays.asList(12, 99)));
        assertEquals(2, companyIds.getValue().size());
        ArgumentCaptor<List<Integer>> fleetIds = ArgumentCaptor.forClass(List.class);
        verify(groupCompanyClient).listFleetsByIds(fleetIds.capture());
        assertTrue(fleetIds.getValue().containsAll(Arrays.asList(3, 88)));
        assertEquals(2, fleetIds.getValue().size());
    }
 
    @Test
    void keepsNamesBlankWhenRemoteFails() {
        when(groupCompanyClient.listCompaniesByIds(anyList())).thenReturn(R.fail("公司服务调用失败"));
        when(groupCompanyClient.listFleetsByIds(anyList())).thenThrow(new RuntimeException("车队服务不可用"));
 
        ChargingOrderVO item = order(12, 3);
        List<ChargingOrderVO> list = new ArrayList<>(Collections.singletonList(item));
 
        fillCompanyFleetNames(list);
 
        assertNull(item.getCompanyName());
        assertNull(item.getFleetName());
    }
 
    @Test
    void skipsRemoteCallsWhenOrdersHaveNoOwnership() {
        ChargingOrderVO item = new ChargingOrderVO();
        item.setCode("VD202609280001");
        List<ChargingOrderVO> list = new ArrayList<>(Collections.singletonList(item));
 
        fillCompanyFleetNames(list);
 
        verify(groupCompanyClient, never()).listCompaniesByIds(anyList());
        verify(groupCompanyClient, never()).listFleetsByIds(anyList());
        assertNull(item.getCompanyName());
        assertNull(item.getFleetName());
    }
 
    private ChargingOrderVO order(Integer companyId, Integer fleetId) {
        ChargingOrderVO item = new ChargingOrderVO();
        item.setCode("VD202609280002");
        item.setCompanyId(companyId);
        item.setFleetId(fleetId);
        return item;
    }
 
    private TGroupCompany company(Integer id, String name) {
        TGroupCompany company = new TGroupCompany();
        company.setId(id);
        company.setName(name);
        return company;
    }
 
    private TFleet fleet(Integer id, String fleetName) {
        TFleet fleet = new TFleet();
        fleet.setId(id);
        fleet.setFleetName(fleetName);
        return fleet;
    }
}