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;
|
}
|
}
|