From ac5dac6c7c45e4c2ecf3cc7ccebac78d65ca9141 Mon Sep 17 00:00:00 2001
From: puzhibing <393733352@qq.com>
Date: 星期一, 28 九月 2026 11:24:08 +0800
Subject: [PATCH] 增加新功能和修改原有功能
---
ruoyi-service/ruoyi-account/src/test/java/com/ruoyi/account/service/impl/TChargingCardServiceImplTest.java | 119 ++++-------------------------------------------------------
1 files changed, 9 insertions(+), 110 deletions(-)
diff --git a/ruoyi-service/ruoyi-account/src/test/java/com/ruoyi/account/service/impl/TChargingCardServiceImplTest.java b/ruoyi-service/ruoyi-account/src/test/java/com/ruoyi/account/service/impl/TChargingCardServiceImplTest.java
index 6271f31..252b2e4 100644
--- a/ruoyi-service/ruoyi-account/src/test/java/com/ruoyi/account/service/impl/TChargingCardServiceImplTest.java
+++ b/ruoyi-service/ruoyi-account/src/test/java/com/ruoyi/account/service/impl/TChargingCardServiceImplTest.java
@@ -6,7 +6,6 @@
import com.ruoyi.account.api.dto.ChargingCardAllocateDTO;
import com.ruoyi.account.api.dto.ChargingCardAllocateItemDTO;
import com.ruoyi.account.api.dto.ChargingCardAuthDTO;
-import com.ruoyi.account.api.dto.ChargingCardBalanceDTO;
import com.ruoyi.account.api.dto.ChargingCardDeductDTO;
import com.ruoyi.account.api.dto.ChargingCardExcelDTO;
import com.ruoyi.account.api.dto.ChargingCardSaveDTO;
@@ -18,14 +17,10 @@
import com.ruoyi.common.core.context.SecurityContextHolder;
import com.ruoyi.common.core.exception.ServiceException;
import com.ruoyi.common.core.domain.R;
-import com.ruoyi.common.security.service.TokenService;
import com.ruoyi.common.security.utils.SecurityUtils;
import com.ruoyi.other.api.domain.TFleet;
import com.ruoyi.other.api.domain.TGroupCompany;
import com.ruoyi.other.api.feignClient.GroupCompanyClient;
-import com.ruoyi.system.api.domain.SysUser;
-import com.ruoyi.system.api.feignClient.SysUserClient;
-import com.ruoyi.system.api.model.LoginUser;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
@@ -67,8 +62,6 @@
class TChargingCardServiceImplTest {
@Mock private TChargingCardMapper mapper;
- @Mock private TokenService tokenService;
- @Mock private SysUserClient sysUserClient;
@Mock private TransactionTemplate transactionTemplate;
@Mock private GroupCompanyClient groupCompanyClient;
@Mock private RedissonClient redissonClient;
@@ -79,8 +72,6 @@
void setUp() throws InterruptedException {
service = spy(new TChargingCardServiceImpl());
ReflectionTestUtils.setField(service, "baseMapper", mapper);
- ReflectionTestUtils.setField(service, "tokenService", tokenService);
- ReflectionTestUtils.setField(service, "sysUserClient", sysUserClient);
ReflectionTestUtils.setField(service, "transactionTemplate", transactionTemplate);
ReflectionTestUtils.setField(service, "groupCompanyClient", groupCompanyClient);
ReflectionTestUtils.setField(service, "redissonClient", redissonClient);
@@ -183,47 +174,6 @@
ServiceException exception = assertThrows(ServiceException.class, () -> service.updateStatus(dto));
assertEquals("卡片状态参数错误", exception.getMessage());
- }
-
- @Test
- void updateBalanceAdjustsAndRecordsForAllCards() {
- LoginUser loginUser = new LoginUser();
- loginUser.setUserid(123L);
- SysUser user = new SysUser();
- user.setOperationPassword(com.ruoyi.common.security.utils.SecurityUtils.encryptPassword("pwd"));
- when(tokenService.getLoginUser()).thenReturn(loginUser);
- when(sysUserClient.getSysUser(123L)).thenReturn(R.ok(user));
- TChargingCard first = new TChargingCard();
- first.setId(1L);
- TChargingCard second = new TChargingCard();
- second.setId(2L);
- when(mapper.selectBatchIds(anyList())).thenReturn(Arrays.asList(first, second));
- when(mapper.adjustBalance(anyList(), any(BigDecimal.class), any(Integer.class), any(BigDecimal.class), any(Long.class)))
- .thenReturn(2);
- ChargingCardBalanceDTO dto = new ChargingCardBalanceDTO();
- dto.setIds(Arrays.asList(1L, 2L));
- dto.setType(1);
- dto.setAmount(new BigDecimal("10.00"));
- dto.setOperationPassword("pwd");
- dto.setRemark(" test ");
-
- assertTrue(service.updateBalance(dto));
-
- verify(mapper).insertBalanceRecord(1L, 1, new BigDecimal("10.00"), "test", 123L);
- verify(mapper).insertBalanceRecord(2L, 1, new BigDecimal("10.00"), "test", 123L);
- }
-
- @Test
- void updateBalanceRejectsMissingPasswordBeforeDatabaseUpdate() {
- ChargingCardBalanceDTO dto = new ChargingCardBalanceDTO();
- dto.setIds(Collections.singletonList(1L));
- dto.setType(1);
- dto.setAmount(BigDecimal.ONE);
-
- ServiceException exception = assertThrows(ServiceException.class, () -> service.updateBalance(dto));
-
- assertEquals("请输入操作密码", exception.getMessage());
- verify(mapper, never()).adjustBalance(anyList(), any(), any(), any(), any());
}
@Test
@@ -372,31 +322,6 @@
assertTrue(exception.getMessage().contains("正在充电中"));
verify(mapper, never()).updateById(any());
- }
-
- @Test
- void updateBalanceRejectsCardInChargingLock() {
- LoginUser loginUser = new LoginUser();
- loginUser.setUserid(123L);
- SysUser user = new SysUser();
- user.setOperationPassword(SecurityUtils.encryptPassword("pwd"));
- when(tokenService.getLoginUser()).thenReturn(loginUser);
- when(sysUserClient.getSysUser(123L)).thenReturn(R.ok(user));
- TChargingCard locked = new TChargingCard();
- locked.setId(1L);
- locked.setCardNo("CARD-1");
- locked.setChargingFlag(1);
- when(mapper.selectBatchIds(anyList())).thenReturn(Collections.singletonList(locked));
- ChargingCardBalanceDTO dto = new ChargingCardBalanceDTO();
- dto.setIds(Collections.singletonList(1L));
- dto.setType(2);
- dto.setAmount(new BigDecimal("10.00"));
- dto.setOperationPassword("pwd");
-
- ServiceException exception = assertThrows(ServiceException.class, () -> service.updateBalance(dto));
-
- assertTrue(exception.getMessage().contains("正在充电中"));
- verify(mapper, never()).adjustBalance(anyList(), any(), any(), any(), any());
}
// ==================== 卡片所属公司/车队 + 初始分配额度 ====================
@@ -853,7 +778,6 @@
@Test
void allocateQuotaAddsCardBalanceAndDeductsCompanyBalance() {
- mockOperationPassword();
when(groupCompanyClient.getCompanyById(9)).thenReturn(R.ok(allocationCompany(9, "测试公司")));
TChargingCard first = allocateCard(1L, 9, "CARD-1", "10.00");
TChargingCard second = allocateCard(2L, 9, "CARD-2", "20.00");
@@ -864,7 +788,7 @@
.thenReturn(1);
when(groupCompanyClient.deductBalance(9, new BigDecimal("30.00"))).thenReturn(R.ok(new BigDecimal("970.00")));
- ChargingCardAllocateVO vo = service.allocateQuota(allocateDto(9, "pwd",
+ ChargingCardAllocateVO vo = service.allocateQuota(allocateDto(9,
allocateItem(1L, "10.00"), allocateItem(2L, "20.00")));
assertEquals(new BigDecimal("970.00"), vo.getAvailableBalance());
@@ -877,13 +801,12 @@
@Test
void allocateQuotaRejectsSharedModeCompany() {
- mockOperationPassword();
TGroupCompany shared = allocationCompany(9, "共享公司");
shared.setAccountMode(1);
when(groupCompanyClient.getCompanyById(9)).thenReturn(R.ok(shared));
ServiceException exception = assertThrows(ServiceException.class,
- () -> service.allocateQuota(allocateDto(9, "pwd", allocateItem(1L, "10.00"))));
+ () -> service.allocateQuota(allocateDto(9, allocateItem(1L, "10.00"))));
assertEquals("该公司为共享模式,不支持额度分配", exception.getMessage());
verify(groupCompanyClient, never()).deductBalance(any(Integer.class), any(BigDecimal.class));
@@ -891,13 +814,12 @@
@Test
void allocateQuotaRejectsCardOfAnotherCompany() {
- mockOperationPassword();
when(groupCompanyClient.getCompanyById(9)).thenReturn(R.ok(allocationCompany(9, "测试公司")));
TChargingCard foreign = allocateCard(1L, 8, "CARD-1", "10.00");
doReturn(Collections.singletonList(foreign)).when(service).listByIds(anyCollection());
ServiceException exception = assertThrows(ServiceException.class,
- () -> service.allocateQuota(allocateDto(9, "pwd", allocateItem(1L, "10.00"))));
+ () -> service.allocateQuota(allocateDto(9, allocateItem(1L, "10.00"))));
assertTrue(exception.getMessage().contains("不属于所选公司"));
verify(groupCompanyClient, never()).deductBalance(any(Integer.class), any(BigDecimal.class));
@@ -905,7 +827,6 @@
@Test
void allocateQuotaRefundsCompanyBalanceWhenCardUpdateFails() {
- mockOperationPassword();
when(groupCompanyClient.getCompanyById(9)).thenReturn(R.ok(allocationCompany(9, "测试公司")));
TChargingCard card = allocateCard(1L, 9, "CARD-1", "10.00");
doReturn(Collections.singletonList(card)).when(service).listByIds(anyCollection());
@@ -915,7 +836,7 @@
any(Long.class))).thenReturn(0);
ServiceException exception = assertThrows(ServiceException.class,
- () -> service.allocateQuota(allocateDto(9, "pwd", allocateItem(1L, "10.00"))));
+ () -> service.allocateQuota(allocateDto(9, allocateItem(1L, "10.00"))));
assertTrue(exception.getMessage().contains("余额分配失败"));
verify(groupCompanyClient).refundBalance(9, new BigDecimal("10.00"));
@@ -923,14 +844,13 @@
@Test
void allocateQuotaReportsInsufficientCompanyBalance() {
- mockOperationPassword();
when(groupCompanyClient.getCompanyById(9)).thenReturn(R.ok(allocationCompany(9, "测试公司")));
TChargingCard card = allocateCard(1L, 9, "CARD-1", "0.00");
doReturn(Collections.singletonList(card)).when(service).listByIds(anyCollection());
when(groupCompanyClient.deductBalance(9, new BigDecimal("500.00"))).thenReturn(R.fail("公司可用余额不足"));
ServiceException exception = assertThrows(ServiceException.class,
- () -> service.allocateQuota(allocateDto(9, "pwd", allocateItem(1L, "500.00"))));
+ () -> service.allocateQuota(allocateDto(9, allocateItem(1L, "500.00"))));
assertEquals("公司可分配余额不足,无法完成本次分配", exception.getMessage());
verify(mapper, never()).adjustBalance(anyList(), any(BigDecimal.class), any(Integer.class),
@@ -940,7 +860,7 @@
@Test
void allocateQuotaRejectsInvalidAmount() {
ServiceException exception = assertThrows(ServiceException.class,
- () -> service.allocateQuota(allocateDto(9, "pwd", allocateItem(1L, "0"))));
+ () -> service.allocateQuota(allocateDto(9, allocateItem(1L, "0"))));
assertEquals("分配金额必须大于0", exception.getMessage());
verify(groupCompanyClient, never()).getCompanyById(any(Integer.class));
@@ -949,43 +869,23 @@
@Test
void allocateQuotaRejectsDuplicateCards() {
ServiceException exception = assertThrows(ServiceException.class,
- () -> service.allocateQuota(allocateDto(9, "pwd", allocateItem(1L, "10.00"), allocateItem(1L, "5.00"))));
+ () -> service.allocateQuota(allocateDto(9, allocateItem(1L, "10.00"), allocateItem(1L, "5.00"))));
assertEquals("分配明细中存在重复卡片", exception.getMessage());
}
@Test
- void allocateQuotaRejectsMissingPassword() {
- ServiceException exception = assertThrows(ServiceException.class,
- () -> service.allocateQuota(allocateDto(9, null, allocateItem(1L, "10.00"))));
-
- assertEquals("请输入操作密码", exception.getMessage());
- verify(mapper, never()).adjustBalance(anyList(), any(BigDecimal.class), any(Integer.class),
- any(BigDecimal.class), any(Long.class));
- }
-
- @Test
void allocateQuotaRejectsChargingLockedCard() {
- mockOperationPassword();
when(groupCompanyClient.getCompanyById(9)).thenReturn(R.ok(allocationCompany(9, "测试公司")));
TChargingCard card = allocateCard(1L, 9, "CARD-1", "10.00");
card.setChargingFlag(1);
doReturn(Collections.singletonList(card)).when(service).listByIds(anyCollection());
ServiceException exception = assertThrows(ServiceException.class,
- () -> service.allocateQuota(allocateDto(9, "pwd", allocateItem(1L, "10.00"))));
+ () -> service.allocateQuota(allocateDto(9, allocateItem(1L, "10.00"))));
assertTrue(exception.getMessage().contains("充电中"));
verify(groupCompanyClient, never()).deductBalance(any(Integer.class), any(BigDecimal.class));
- }
-
- private void mockOperationPassword() {
- LoginUser loginUser = new LoginUser();
- loginUser.setUserid(123L);
- SysUser user = new SysUser();
- user.setOperationPassword(SecurityUtils.encryptPassword("pwd"));
- when(tokenService.getLoginUser()).thenReturn(loginUser);
- when(sysUserClient.getSysUser(123L)).thenReturn(R.ok(user));
}
private TChargingCard allocateCard(Long id, Integer companyId, String cardNo, String balance) {
@@ -1006,11 +906,10 @@
return item;
}
- private ChargingCardAllocateDTO allocateDto(Integer companyId, String password, ChargingCardAllocateItemDTO... items) {
+ private ChargingCardAllocateDTO allocateDto(Integer companyId, ChargingCardAllocateItemDTO... items) {
ChargingCardAllocateDTO dto = new ChargingCardAllocateDTO();
dto.setCompanyId(companyId);
dto.setItems(Arrays.asList(items));
- dto.setOperationPassword(password);
return dto;
}
}
--
Gitblit v1.7.1