| | |
| | | package com.ruoyi.other.service.impl; |
| | | |
| | | import com.baomidou.mybatisplus.core.MybatisConfiguration; |
| | | import com.baomidou.mybatisplus.core.metadata.TableInfoHelper; |
| | | import com.baomidou.mybatisplus.extension.plugins.pagination.Page; |
| | | import com.ruoyi.account.api.dto.VinManagementQueryDTO; |
| | | import com.ruoyi.account.api.feignClient.FleetUnbindClient; |
| | |
| | | import com.ruoyi.other.api.domain.TChargeAuthorization; |
| | | import com.ruoyi.other.api.domain.TFleet; |
| | | import com.ruoyi.other.api.domain.TGroupCompany; |
| | | import com.ruoyi.other.api.dto.FleetImportDTO; |
| | | import com.ruoyi.other.api.feignClient.FleetOrderAggregateClient; |
| | | import com.ruoyi.other.api.vo.FleetOrderAggregateVO; |
| | | import com.ruoyi.other.mapper.TChargeAuthorizationMapper; |
| | | import com.ruoyi.other.mapper.TFleetMapper; |
| | | import com.ruoyi.other.service.CompanyAccountAccessService; |
| | | import com.ruoyi.other.service.TGroupCompanyService; |
| | | import org.apache.ibatis.builder.MapperBuilderAssistant; |
| | | import org.junit.jupiter.api.BeforeAll; |
| | | import org.junit.jupiter.api.Test; |
| | | import org.mockito.ArgumentCaptor; |
| | | import org.mockito.Mockito; |
| | |
| | | * 远程客户端和 Mapper 以 Mock 替代,不依赖 Spring 容器与数据库。 |
| | | */ |
| | | class TFleetServiceImplTest { |
| | | |
| | | @BeforeAll |
| | | static void initTableInfo() { |
| | | // MP 的 lambdaQuery 会通过 TableInfoHelper 取表元数据解析 lambda 列名, |
| | | // 单元测试不启动 Spring,需要手动初始化 TableInfo,否则抛 MybatisPlusException |
| | | TableInfoHelper.initTableInfo(new MapperBuilderAssistant(new MybatisConfiguration(), ""), TFleet.class); |
| | | } |
| | | |
| | | @Test |
| | | void passesFleetIdAndPagingToVinService() { |
| | |
| | | Mockito.verify(service, Mockito.never()).removeByIds(Mockito.anyList()); |
| | | } |
| | | |
| | | @Test |
| | | void importFleetsForcesCompanyAccountOwnership() { |
| | | // 公司账号:导入车队强制归属本公司,Excel 中的所属公司仅作展示信息,不做公司名校验 |
| | | TFleetMapper fleetMapper = Mockito.mock(TFleetMapper.class); |
| | | Mockito.when(fleetMapper.selectCount(Mockito.any())).thenReturn(0L); |
| | | Mockito.when(fleetMapper.insert(Mockito.any(TFleet.class))).thenReturn(1); |
| | | CompanyAccountAccessService access = Mockito.mock(CompanyAccountAccessService.class); |
| | | Mockito.when(access.getCurrentCompanyId()).thenReturn(2); |
| | | |
| | | TFleetServiceImpl service = Mockito.spy(new TFleetServiceImpl()); |
| | | ReflectionTestUtils.setField(service, "baseMapper", fleetMapper); |
| | | ReflectionTestUtils.setField(service, "companyAccountAccessService", access); |
| | | |
| | | FleetImportDTO item = new FleetImportDTO(); |
| | | item.setFleetName("公司导入车队"); |
| | | item.setCompanyName("随意填写的公司"); |
| | | |
| | | String result = service.importFleets(Collections.singletonList(item)); |
| | | |
| | | assertTrue(result.contains("成功导入")); |
| | | ArgumentCaptor<TFleet> captor = ArgumentCaptor.forClass(TFleet.class); |
| | | Mockito.verify(fleetMapper).insert(captor.capture()); |
| | | assertEquals(Integer.valueOf(2), captor.getValue().getCompanyId()); |
| | | } |
| | | |
| | | @Test |
| | | void importFleetsRequiresCompanyNameForNonCompanyAccount() { |
| | | // 平台/合作商账号:无公司锁定时所属公司必填 |
| | | TFleetMapper fleetMapper = Mockito.mock(TFleetMapper.class); |
| | | CompanyAccountAccessService access = Mockito.mock(CompanyAccountAccessService.class); |
| | | Mockito.when(access.getCurrentCompanyId()).thenReturn(null); |
| | | |
| | | TFleetServiceImpl service = Mockito.spy(new TFleetServiceImpl()); |
| | | ReflectionTestUtils.setField(service, "baseMapper", fleetMapper); |
| | | ReflectionTestUtils.setField(service, "companyAccountAccessService", access); |
| | | |
| | | FleetImportDTO item = new FleetImportDTO(); |
| | | item.setFleetName("无公司车队"); |
| | | |
| | | ServiceException ex = assertThrows(ServiceException.class, |
| | | () -> service.importFleets(Collections.singletonList(item))); |
| | | |
| | | assertTrue(ex.getMessage().contains("所属公司不能为空"), ex.getMessage()); |
| | | Mockito.verify(fleetMapper, Mockito.never()).insert(Mockito.any(TFleet.class)); |
| | | } |
| | | |
| | | private TFleet fleet(Integer id) { |
| | | TFleet fleet = new TFleet(); |
| | | fleet.setId(id); |