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