package com.ruoyi.account.service.impl;
|
|
import com.baomidou.mybatisplus.core.MybatisConfiguration;
|
import com.baomidou.mybatisplus.core.metadata.IPage;
|
import com.baomidou.mybatisplus.core.metadata.TableInfoHelper;
|
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
|
import org.apache.ibatis.builder.MapperBuilderAssistant;
|
import com.ruoyi.account.api.dto.VinManagementExcelDTO;
|
import com.ruoyi.account.api.dto.VinManagementQueryDTO;
|
import com.ruoyi.account.api.dto.VinManagementSaveDTO;
|
import com.ruoyi.account.api.model.TVinManagement;
|
import com.ruoyi.account.mapper.TVinManagementMapper;
|
import com.ruoyi.common.core.context.SecurityContextHolder;
|
import com.ruoyi.common.core.domain.R;
|
import com.ruoyi.common.core.exception.ServiceException;
|
import com.ruoyi.order.api.feignClient.ChargingOrderClient;
|
import com.ruoyi.order.api.vo.VinChargingStatusVO;
|
import org.junit.jupiter.api.AfterEach;
|
import org.junit.jupiter.api.BeforeAll;
|
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.Captor;
|
import org.mockito.Mock;
|
import org.mockito.junit.jupiter.MockitoExtension;
|
import org.springframework.test.util.ReflectionTestUtils;
|
|
import java.util.Arrays;
|
import java.util.Collections;
|
import java.util.List;
|
|
import static org.junit.jupiter.api.Assertions.assertEquals;
|
import static org.junit.jupiter.api.Assertions.assertNotNull;
|
import static org.junit.jupiter.api.Assertions.assertSame;
|
import static org.junit.jupiter.api.Assertions.assertThrows;
|
import static org.junit.jupiter.api.Assertions.assertTrue;
|
import static org.mockito.ArgumentMatchers.any;
|
import static org.mockito.ArgumentMatchers.anyCollection;
|
import static org.mockito.ArgumentMatchers.anyString;
|
import static org.mockito.ArgumentMatchers.eq;
|
import static org.mockito.ArgumentMatchers.isNull;
|
import static org.mockito.Mockito.never;
|
import static org.mockito.Mockito.times;
|
import static org.mockito.Mockito.verify;
|
import static org.mockito.Mockito.verifyNoInteractions;
|
import static org.mockito.Mockito.when;
|
|
@ExtendWith(MockitoExtension.class)
|
class TVinManagementServiceImplTest {
|
|
private static final Long CURRENT_USER = 123L;
|
|
@Mock
|
private TVinManagementMapper mapper;
|
|
@Mock
|
private ChargingOrderClient chargingOrderClient;
|
|
@Captor
|
private ArgumentCaptor<Page<TVinManagement>> pageCaptor;
|
|
@Captor
|
private ArgumentCaptor<VinManagementQueryDTO> queryCaptor;
|
|
@Captor
|
private ArgumentCaptor<TVinManagement> entityCaptor;
|
|
private TVinManagementServiceImpl service;
|
|
@BeforeAll
|
static void initTableInfo() {
|
// MP 的 removeByIds 会通过 TableInfoHelper.getTableInfo 取表元数据,
|
// 单元测试不启动 Spring,需要手动初始化 TableInfo,否则返回 null 导致 NPE
|
TableInfoHelper.initTableInfo(new MapperBuilderAssistant(new MybatisConfiguration(), ""), TVinManagement.class);
|
}
|
|
@BeforeEach
|
void setUp() {
|
service = new TVinManagementServiceImpl();
|
ReflectionTestUtils.setField(service, "baseMapper", mapper);
|
ReflectionTestUtils.setField(service, "chargingOrderClient", chargingOrderClient);
|
SecurityContextHolder.setUserId(String.valueOf(CURRENT_USER));
|
}
|
|
@AfterEach
|
void tearDown() {
|
SecurityContextHolder.remove();
|
}
|
|
// ---------------- selectPageList ----------------
|
|
@Test
|
void selectPageListWithNullQueryUsesDefaultPagination() {
|
Page<TVinManagement> expected = new Page<>(1, 10);
|
when(mapper.selectPageList(any(Page.class), any())).thenReturn(expected);
|
|
IPage<TVinManagement> result = service.selectPageList(null);
|
|
verify(mapper).selectPageList(pageCaptor.capture(), queryCaptor.capture());
|
assertEquals(1, pageCaptor.getValue().getCurrent());
|
assertEquals(50, pageCaptor.getValue().getSize()); // BasePage 默认 pageSize=50
|
assertNotNull(queryCaptor.getValue());
|
assertSame(expected, result);
|
}
|
|
@Test
|
void selectPageListWithInvalidParamsFallsBackToDefaults() {
|
VinManagementQueryDTO query = new VinManagementQueryDTO();
|
query.setPageCurr(0);
|
query.setPageSize(-1);
|
when(mapper.selectPageList(any(Page.class), any())).thenReturn(new Page<>());
|
|
service.selectPageList(query);
|
|
verify(mapper).selectPageList(pageCaptor.capture(), any());
|
assertEquals(1, pageCaptor.getValue().getCurrent());
|
assertEquals(10, pageCaptor.getValue().getSize());
|
}
|
|
@Test
|
void selectPageListPassesQueryAndPagination() {
|
VinManagementQueryDTO query = new VinManagementQueryDTO();
|
query.setPageCurr(3);
|
query.setPageSize(20);
|
query.setVin("LSV");
|
Page<TVinManagement> expected = new Page<>(3, 20);
|
expected.setRecords(Collections.singletonList(new TVinManagement()));
|
when(mapper.selectPageList(any(Page.class), any())).thenReturn(expected);
|
|
IPage<TVinManagement> result = service.selectPageList(query);
|
|
verify(mapper).selectPageList(pageCaptor.capture(), queryCaptor.capture());
|
assertEquals(3, pageCaptor.getValue().getCurrent());
|
assertEquals(20, pageCaptor.getValue().getSize());
|
assertSame(query, queryCaptor.getValue());
|
assertEquals(1, result.getRecords().size());
|
}
|
|
// ---------------- saveVin ----------------
|
|
@Test
|
void saveVinSucceedsAndSetsDefaults() {
|
when(mapper.countByVin(anyString(), isNull())).thenReturn(0);
|
when(mapper.insert(any(TVinManagement.class))).thenReturn(1);
|
|
VinManagementSaveDTO dto = new VinManagementSaveDTO();
|
dto.setVin(" LSVAM4187C2184842 ");
|
dto.setPlateNumber(" 川A12345 ");
|
dto.setPhone("13800138000");
|
dto.setCompanyName(" 测试公司 ");
|
dto.setRemark(" 备注 ");
|
|
boolean result = service.saveVin(dto);
|
|
assertTrue(result);
|
verify(mapper).insert(entityCaptor.capture());
|
TVinManagement saved = entityCaptor.getValue();
|
assertEquals("LSVAM4187C2184842", saved.getVin());
|
assertEquals("川A12345", saved.getPlateNumber());
|
assertEquals("13800138000", saved.getPhone());
|
assertEquals("测试公司", saved.getCompanyName());
|
assertEquals("备注", saved.getRemark());
|
assertEquals(Integer.valueOf(1), saved.getStatus());
|
assertEquals(Integer.valueOf(0), saved.getDelFlag());
|
assertNotNull(saved.getCreateTime());
|
assertEquals(CURRENT_USER, saved.getCreateBy());
|
}
|
|
@Test
|
void saveVinRejectsNullVin() {
|
VinManagementSaveDTO dto = new VinManagementSaveDTO();
|
dto.setVin(null);
|
|
ServiceException ex = assertThrows(ServiceException.class, () -> service.saveVin(dto));
|
|
assertEquals("VIN码不能为空", ex.getMessage());
|
verifyNoInteractions(mapper);
|
}
|
|
@Test
|
void saveVinRejectsTooLongVin() {
|
VinManagementSaveDTO dto = new VinManagementSaveDTO();
|
dto.setVin(longVin(65));
|
|
ServiceException ex = assertThrows(ServiceException.class, () -> service.saveVin(dto));
|
|
assertEquals("VIN码不能超过64个字符", ex.getMessage());
|
verifyNoInteractions(mapper);
|
}
|
|
@Test
|
void saveVinRejectsInvalidStatus() {
|
VinManagementSaveDTO dto = new VinManagementSaveDTO();
|
dto.setVin("LSVAM4187C2184842");
|
dto.setStatus(3);
|
|
ServiceException ex = assertThrows(ServiceException.class, () -> service.saveVin(dto));
|
|
assertEquals("VIN码状态参数错误", ex.getMessage());
|
verifyNoInteractions(mapper);
|
}
|
|
@Test
|
void saveVinRejectsDuplicateVin() {
|
when(mapper.countByVin(anyString(), isNull())).thenReturn(1);
|
|
VinManagementSaveDTO dto = new VinManagementSaveDTO();
|
dto.setVin("LSVAM4187C2184842");
|
|
ServiceException ex = assertThrows(ServiceException.class, () -> service.saveVin(dto));
|
|
assertEquals("VIN码已存在", ex.getMessage());
|
verify(mapper, never()).insert(any());
|
}
|
|
// ---------------- updateVin ----------------
|
|
@Test
|
void updateVinSucceedsAndSetsAuditFields() {
|
when(mapper.selectById(9L)).thenReturn(new TVinManagement());
|
when(mapper.countByVin(anyString(), eq(9L))).thenReturn(0);
|
when(mapper.updateById(any(TVinManagement.class))).thenReturn(1);
|
|
VinManagementSaveDTO dto = new VinManagementSaveDTO();
|
dto.setId(9L);
|
dto.setVin("LSVAM4187C2184842");
|
dto.setPlateNumber("川B88888");
|
dto.setStatus(2);
|
|
boolean result = service.updateVin(dto);
|
|
assertTrue(result);
|
verify(mapper).updateById(entityCaptor.capture());
|
TVinManagement updated = entityCaptor.getValue();
|
assertEquals(Long.valueOf(9L), updated.getId());
|
assertEquals("LSVAM4187C2184842", updated.getVin());
|
assertEquals("川B88888", updated.getPlateNumber());
|
assertEquals(Integer.valueOf(2), updated.getStatus());
|
assertNotNull(updated.getUpdateTime());
|
assertEquals(CURRENT_USER, updated.getUpdateBy());
|
}
|
|
@Test
|
void updateVinRejectsMissingId() {
|
VinManagementSaveDTO dto = new VinManagementSaveDTO();
|
dto.setVin("LSVAM4187C2184842");
|
|
ServiceException ex = assertThrows(ServiceException.class, () -> service.updateVin(dto));
|
|
assertEquals("VIN码ID不能为空", ex.getMessage());
|
verifyNoInteractions(mapper);
|
}
|
|
@Test
|
void updateVinRejectsNotExist() {
|
when(mapper.selectById(9L)).thenReturn(null);
|
|
VinManagementSaveDTO dto = new VinManagementSaveDTO();
|
dto.setId(9L);
|
dto.setVin("LSVAM4187C2184842");
|
|
ServiceException ex = assertThrows(ServiceException.class, () -> service.updateVin(dto));
|
|
assertEquals("VIN码不存在或已删除", ex.getMessage());
|
verify(mapper, never()).updateById(any());
|
}
|
|
@Test
|
void updateVinRejectsDuplicateVin() {
|
// validate 中 countByVin 命中重复即抛异常,selectById 不会被执行
|
when(mapper.countByVin(anyString(), eq(9L))).thenReturn(1);
|
|
VinManagementSaveDTO dto = new VinManagementSaveDTO();
|
dto.setId(9L);
|
dto.setVin("LSVAM4187C2184842");
|
|
ServiceException ex = assertThrows(ServiceException.class, () -> service.updateVin(dto));
|
|
assertEquals("VIN码已存在", ex.getMessage());
|
verify(mapper, never()).updateById(any());
|
}
|
|
// ---------------- deleteVins ----------------
|
|
@Test
|
void deleteVinsRejectsNullOrEmptyIds() {
|
ServiceException ex1 = assertThrows(ServiceException.class, () -> service.deleteVins(null));
|
assertEquals("请选择需要删除的VIN码", ex1.getMessage());
|
|
ServiceException ex2 = assertThrows(ServiceException.class, () -> service.deleteVins(Collections.emptyList()));
|
assertEquals("请选择需要删除的VIN码", ex2.getMessage());
|
|
verifyNoInteractions(mapper);
|
}
|
|
@Test
|
void deleteVinsRejectsMissingRecords() {
|
when(mapper.selectBatchIds(anyCollection())).thenReturn(Collections.singletonList(new TVinManagement()));
|
|
ServiceException ex = assertThrows(ServiceException.class, () -> service.deleteVins(Arrays.asList(1L, 2L)));
|
|
assertEquals("部分VIN码不存在或已删除", ex.getMessage());
|
verify(mapper, never()).deleteBatchIds(any());
|
}
|
|
@Test
|
void deleteVinsSucceedsWithDistinctIds() {
|
when(mapper.selectBatchIds(anyCollection()))
|
.thenReturn(Arrays.asList(new TVinManagement(), new TVinManagement()));
|
when(mapper.deleteBatchIds(anyCollection())).thenReturn(1);
|
|
boolean result = service.deleteVins(Arrays.asList(1L, 2L, 2L));
|
|
assertTrue(result);
|
@SuppressWarnings("unchecked")
|
ArgumentCaptor<List<Long>> idsCaptor = ArgumentCaptor.forClass(List.class);
|
verify(mapper).deleteBatchIds(idsCaptor.capture());
|
assertEquals(2, idsCaptor.getValue().size());
|
assertEquals(Long.valueOf(1L), idsCaptor.getValue().get(0));
|
assertEquals(Long.valueOf(2L), idsCaptor.getValue().get(1));
|
}
|
|
// ---------------- updateStatus ----------------
|
|
@Test
|
void updateStatusRejectsNullId() {
|
ServiceException ex = assertThrows(ServiceException.class, () -> service.updateStatus(null, 1));
|
|
assertEquals("VIN码状态参数错误", ex.getMessage());
|
verifyNoInteractions(mapper);
|
}
|
|
@Test
|
void updateStatusRejectsInvalidStatus() {
|
ServiceException ex = assertThrows(ServiceException.class, () -> service.updateStatus(9L, 0));
|
|
assertEquals("VIN码状态参数错误", ex.getMessage());
|
verifyNoInteractions(mapper);
|
}
|
|
@Test
|
void updateStatusRejectsNotExist() {
|
when(mapper.selectById(9L)).thenReturn(null);
|
|
ServiceException ex = assertThrows(ServiceException.class, () -> service.updateStatus(9L, 2));
|
|
assertEquals("VIN码不存在或已删除", ex.getMessage());
|
verify(mapper, never()).updateById(any());
|
}
|
|
@Test
|
void updateStatusSucceeds() {
|
when(mapper.selectById(9L)).thenReturn(new TVinManagement());
|
when(mapper.updateById(any(TVinManagement.class))).thenReturn(1);
|
|
boolean result = service.updateStatus(9L, 2);
|
|
assertTrue(result);
|
verify(mapper).updateById(entityCaptor.capture());
|
TVinManagement updated = entityCaptor.getValue();
|
assertEquals(Long.valueOf(9L), updated.getId());
|
assertEquals(Integer.valueOf(2), updated.getStatus());
|
assertNotNull(updated.getUpdateTime());
|
assertEquals(CURRENT_USER, updated.getUpdateBy());
|
}
|
|
// ---------------- selectExportList ----------------
|
|
@Test
|
void selectExportListReturnsMapperResult() {
|
VinManagementExcelDTO item = new VinManagementExcelDTO();
|
item.setVin("LSVAM4187C2184842");
|
when(mapper.selectExportList(any())).thenReturn(Collections.singletonList(item));
|
|
VinManagementQueryDTO query = new VinManagementQueryDTO();
|
query.setStatus(1);
|
|
List<VinManagementExcelDTO> result = service.selectExportList(query);
|
|
assertEquals(1, result.size());
|
assertEquals("LSVAM4187C2184842", result.get(0).getVin());
|
verify(mapper).selectExportList(queryCaptor.capture());
|
assertSame(query, queryCaptor.getValue());
|
}
|
|
@Test
|
void selectExportListWithNullQueryUsesEmptyDto() {
|
when(mapper.selectExportList(any())).thenReturn(Collections.emptyList());
|
|
List<VinManagementExcelDTO> result = service.selectExportList(null);
|
|
assertTrue(result.isEmpty());
|
verify(mapper).selectExportList(queryCaptor.capture());
|
assertNotNull(queryCaptor.getValue());
|
}
|
|
// ---------------- importVins ----------------
|
|
@Test
|
void importVinsRejectsNullOrEmptyItems() {
|
ServiceException ex1 = assertThrows(ServiceException.class, () -> service.importVins(null));
|
assertEquals("导入数据不能为空", ex1.getMessage());
|
|
ServiceException ex2 = assertThrows(ServiceException.class, () -> service.importVins(Collections.emptyList()));
|
assertEquals("导入数据不能为空", ex2.getMessage());
|
|
verifyNoInteractions(mapper);
|
}
|
|
@Test
|
void importVinsRejectsEmptyVinAtRow() {
|
VinManagementExcelDTO item = new VinManagementExcelDTO();
|
item.setVin("");
|
|
ServiceException ex = assertThrows(ServiceException.class,
|
() -> service.importVins(Collections.singletonList(item)));
|
|
assertEquals("第2行VIN码不能为空", ex.getMessage());
|
verifyNoInteractions(mapper);
|
}
|
|
@Test
|
void importVinsRejectsTooLongVinAtRow() {
|
VinManagementExcelDTO item = new VinManagementExcelDTO();
|
item.setVin(longVin(65));
|
|
ServiceException ex = assertThrows(ServiceException.class,
|
() -> service.importVins(Collections.singletonList(item)));
|
|
assertEquals("第2行VIN码不能超过64个字符", ex.getMessage());
|
verifyNoInteractions(mapper);
|
}
|
|
@Test
|
void importVinsRejectsDuplicateVinWithinFile() {
|
VinManagementExcelDTO first = new VinManagementExcelDTO();
|
first.setVin("LSVAM4187C2184842");
|
VinManagementExcelDTO second = new VinManagementExcelDTO();
|
second.setVin("LSVAM4187C2184842");
|
|
ServiceException ex = assertThrows(ServiceException.class,
|
() -> service.importVins(Arrays.asList(first, second)));
|
|
assertEquals("第3行VIN码已存在或重复", ex.getMessage());
|
// 第一行校验时已调用过 countByVin,仅断言不执行 insert
|
verify(mapper, never()).insert(any());
|
}
|
|
@Test
|
void importVinsRejectsVinExistingInDb() {
|
when(mapper.countByVin(anyString(), isNull())).thenReturn(1);
|
|
VinManagementExcelDTO item = new VinManagementExcelDTO();
|
item.setVin("LSVAM4187C2184842");
|
|
ServiceException ex = assertThrows(ServiceException.class,
|
() -> service.importVins(Collections.singletonList(item)));
|
|
assertEquals("第2行VIN码已存在或重复", ex.getMessage());
|
verify(mapper, never()).insert(any());
|
}
|
|
@Test
|
void importVinsRejectsInvalidStatusAtRow() {
|
VinManagementExcelDTO item = new VinManagementExcelDTO();
|
item.setVin("LSVAM4187C2184842");
|
item.setStatus("3");
|
|
ServiceException ex = assertThrows(ServiceException.class,
|
() -> service.importVins(Collections.singletonList(item)));
|
|
assertEquals("第2行启用状态错误", ex.getMessage());
|
verify(mapper, never()).insert(any());
|
}
|
|
@Test
|
void importVinsSucceedsAndParsesStatus() {
|
when(mapper.countByVin(anyString(), isNull())).thenReturn(0);
|
when(mapper.insert(any(TVinManagement.class))).thenReturn(1);
|
|
VinManagementExcelDTO first = new VinManagementExcelDTO();
|
first.setVin(" LSVAM4187C2184842 ");
|
first.setPlateNumber(" 川A12345 ");
|
first.setPhone("13800138000");
|
first.setCompanyName(" 公司A ");
|
first.setRemark(" 备注A ");
|
first.setStatus("启用");
|
|
VinManagementExcelDTO second = new VinManagementExcelDTO();
|
second.setVin("LSVAM4187C2184843");
|
second.setStatus("2");
|
|
VinManagementExcelDTO third = new VinManagementExcelDTO();
|
third.setVin("LSVAM4187C2184844");
|
third.setStatus(null);
|
|
String result = service.importVins(Arrays.asList(first, second, third));
|
|
assertEquals("成功导入3条VIN码数据", result);
|
verify(mapper, times(3)).insert(entityCaptor.capture());
|
List<TVinManagement> saved = entityCaptor.getAllValues();
|
assertEquals("LSVAM4187C2184842", saved.get(0).getVin());
|
assertEquals("川A12345", saved.get(0).getPlateNumber());
|
assertEquals("公司A", saved.get(0).getCompanyName());
|
assertEquals("备注A", saved.get(0).getRemark());
|
assertEquals(Integer.valueOf(1), saved.get(0).getStatus());
|
assertEquals(Integer.valueOf(2), saved.get(1).getStatus());
|
assertEquals(Integer.valueOf(1), saved.get(2).getStatus());
|
assertNotNull(saved.get(0).getCreateTime());
|
assertEquals(CURRENT_USER, saved.get(0).getCreateBy());
|
}
|
|
@Test
|
void selectAppletVehicleListFillsChargingStatusInOneBatch() {
|
TVinManagement chargingVehicle = new TVinManagement();
|
chargingVehicle.setId(11L);
|
TVinManagement idleVehicle = new TVinManagement();
|
idleVehicle.setId(22L);
|
when(mapper.selectAppletVehicleList(88L)).thenReturn(Arrays.asList(chargingVehicle, idleVehicle));
|
VinChargingStatusVO status = new VinChargingStatusVO();
|
status.setVinId(11L);
|
status.setChargingGunId(33);
|
when(chargingOrderClient.getChargingVinStatus(Arrays.asList(11L, 22L)))
|
.thenReturn(R.ok(Collections.singletonList(status)));
|
|
List<TVinManagement> result = service.selectAppletVehicleList(88L);
|
|
assertEquals(Boolean.TRUE, result.get(0).getCharging());
|
assertEquals(Integer.valueOf(33), result.get(0).getChargingGunId());
|
assertEquals(Boolean.FALSE, result.get(1).getCharging());
|
verify(chargingOrderClient).getChargingVinStatus(Arrays.asList(11L, 22L));
|
}
|
|
@Test
|
void selectAppletVehicleListTreatsFeignFailureAsIdle() {
|
TVinManagement vehicle = new TVinManagement();
|
vehicle.setId(11L);
|
when(mapper.selectAppletVehicleList(88L)).thenReturn(Collections.singletonList(vehicle));
|
when(chargingOrderClient.getChargingVinStatus(Collections.singletonList(11L)))
|
.thenReturn(R.fail("order unavailable"));
|
|
List<TVinManagement> result = service.selectAppletVehicleList(88L);
|
|
assertEquals(Boolean.FALSE, result.get(0).getCharging());
|
assertEquals(null, result.get(0).getChargingGunId());
|
}
|
|
@Test
|
void selectAppletVehicleListTreatsFeignExceptionAsIdle() {
|
TVinManagement vehicle = new TVinManagement();
|
vehicle.setId(11L);
|
when(mapper.selectAppletVehicleList(88L)).thenReturn(Collections.singletonList(vehicle));
|
when(chargingOrderClient.getChargingVinStatus(Collections.singletonList(11L)))
|
.thenThrow(new RuntimeException("timeout"));
|
|
assertEquals(Boolean.FALSE, service.selectAppletVehicleList(88L).get(0).getCharging());
|
}
|
|
@Test
|
void bindAppletVehicleByVinUsesAtomicConditionalUpdate() {
|
TVinManagement vehicle = new TVinManagement();
|
vehicle.setId(9L);
|
vehicle.setVin("LSVAM4187C2184842");
|
when(mapper.selectEnabledVehicle("LSVAM4187C2184842")).thenReturn(vehicle);
|
when(mapper.bindAppletVehicle(9L, 88L)).thenReturn(1);
|
assertTrue(service.bindAppletVehicle(88L, " LSVAM4187C2184842 "));
|
verify(mapper).bindAppletVehicle(9L, 88L);
|
}
|
|
@Test
|
void bindAppletVehicleRejectsMissingDisabledOrDeletedVehicle() {
|
when(mapper.selectEnabledVehicle("UNKNOWN")).thenReturn(null);
|
ServiceException ex = assertThrows(ServiceException.class,
|
() -> service.bindAppletVehicle(88L, "UNKNOWN"));
|
assertEquals("该车辆不存在", ex.getMessage());
|
}
|
|
@Test
|
void bindAppletVehicleRejectsVehicleAlreadyBoundToCurrentUser() {
|
TVinManagement vehicle = new TVinManagement();
|
vehicle.setId(9L);
|
vehicle.setAppUserId(88L);
|
when(mapper.selectEnabledVehicle("川A12345")).thenReturn(vehicle);
|
ServiceException ex = assertThrows(ServiceException.class,
|
() -> service.bindAppletVehicle(88L, "川A12345"));
|
assertEquals("该车辆已绑定当前用户", ex.getMessage());
|
verify(mapper, never()).bindAppletVehicle(any(), any());
|
}
|
|
@Test
|
void bindAppletVehicleDoesNotOverwriteOtherUser() {
|
TVinManagement vehicle = new TVinManagement();
|
vehicle.setId(9L);
|
vehicle.setAppUserId(99L);
|
when(mapper.selectEnabledVehicle("川A12345")).thenReturn(vehicle);
|
ServiceException ex = assertThrows(ServiceException.class,
|
() -> service.bindAppletVehicle(88L, "川A12345"));
|
assertEquals("该车辆已被其他用户绑定", ex.getMessage());
|
verify(mapper, never()).bindAppletVehicle(any(), any());
|
}
|
|
@Test
|
void unbindAppletVehicleRequiresIdAndOwner() {
|
when(mapper.unbindAppletVehicle(9L, 88L)).thenReturn(1);
|
assertTrue(service.unbindAppletVehicle(9L, 88L));
|
when(mapper.unbindAppletVehicle(10L, 88L)).thenReturn(0);
|
ServiceException ex = assertThrows(ServiceException.class,
|
() -> service.unbindAppletVehicle(10L, 88L));
|
assertEquals("该车辆不存在", ex.getMessage());
|
}
|
|
@Test
|
void getVinIdsByAppUserIdReturnsMapperResult() {
|
when(mapper.selectIdsByAppUserId(88L)).thenReturn(Arrays.asList(1L, 2L));
|
assertEquals(Arrays.asList(1L, 2L), service.getVinIdsByAppUserId(88L));
|
}
|
|
private String longVin(int length) {
|
StringBuilder sb = new StringBuilder();
|
for (int i = 0; i < length; i++) {
|
sb.append('L');
|
}
|
return sb.toString();
|
}
|
}
|