mirror of
http://192.168.1.205:9980/cf_devdept2/cf_imes_server.git
synced 2026-08-12 21:02:08 +08:00
system微服务dal.dataobject、dal.redis单元测试完善
This commit is contained in:
+2
@@ -7,6 +7,8 @@ package com.cf.imes.module.system.constants.permission;
|
||||
* @since 2024/6/22 11:44
|
||||
*/
|
||||
public final class InternalRoleConstants {
|
||||
private InternalRoleConstants() {
|
||||
}
|
||||
|
||||
/**
|
||||
* 组织管理员角色id
|
||||
|
||||
+6
@@ -1,6 +1,9 @@
|
||||
package com.cf.imes.module.system.dal.dataobject.funds.statistics;
|
||||
|
||||
import lombok.AllArgsConstructor;
|
||||
import lombok.Builder;
|
||||
import lombok.Data;
|
||||
import lombok.NoArgsConstructor;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
|
||||
import java.math.BigDecimal;
|
||||
@@ -10,6 +13,9 @@ import java.math.BigDecimal;
|
||||
* @since 2025/9/30 16:10
|
||||
*/
|
||||
@Data
|
||||
@Builder
|
||||
@AllArgsConstructor
|
||||
@NoArgsConstructor
|
||||
public class FundsTradeTypeGroupStatisticsDO implements Comparable<FundsTradeTypeGroupStatisticsDO> {
|
||||
|
||||
/**
|
||||
|
||||
+6
@@ -1,6 +1,9 @@
|
||||
package com.cf.imes.module.system.dal.dataobject.user;
|
||||
|
||||
import lombok.AllArgsConstructor;
|
||||
import lombok.Builder;
|
||||
import lombok.Data;
|
||||
import lombok.NoArgsConstructor;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
|
||||
/**
|
||||
@@ -8,6 +11,9 @@ import org.jetbrains.annotations.NotNull;
|
||||
* @since 2025/9/30 15:32
|
||||
*/
|
||||
@Data
|
||||
@Builder
|
||||
@AllArgsConstructor
|
||||
@NoArgsConstructor
|
||||
public class UserStatusGroupStatisticsDO implements Comparable<UserStatusGroupStatisticsDO> {
|
||||
/**
|
||||
* 用户数量
|
||||
|
||||
+2
@@ -8,6 +8,8 @@ import com.cf.imes.module.system.dal.dataobject.oauth2.OAuth2AccessTokenDO;
|
||||
* @author 晨丰科技
|
||||
*/
|
||||
public class RedisKeyConstants {
|
||||
private RedisKeyConstants() {
|
||||
}
|
||||
|
||||
/**
|
||||
* 指定部门的所有子部门编号数组的缓存
|
||||
|
||||
+67
@@ -0,0 +1,67 @@
|
||||
package com.cf.imes.module.system.dal.dataobject.funds.statistics;
|
||||
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import java.math.BigDecimal;
|
||||
import java.math.BigInteger;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertTrue;
|
||||
|
||||
/**
|
||||
* {@link FundsTradeTypeGroupStatisticsDO} 的单元测试
|
||||
*
|
||||
* @author Gqr
|
||||
* @since 2025/12/15 11:08
|
||||
*/
|
||||
class FundsTradeTypeGroupStatisticsDOTest {
|
||||
private static final BigDecimal TWO = new BigDecimal(BigInteger.TWO);
|
||||
|
||||
private FundsTradeTypeGroupStatisticsDO dto(String date, BigDecimal amount) {
|
||||
return FundsTradeTypeGroupStatisticsDO.builder()
|
||||
.date(date)
|
||||
.amount(amount)
|
||||
.build();
|
||||
}
|
||||
|
||||
@Test
|
||||
void testCompareDifferentYear() {
|
||||
// 年份大 > 小
|
||||
assertTrue(dto("2023-01-02", BigDecimal.ONE).compareTo(dto("2022-01-02", BigDecimal.ONE)) > 0);
|
||||
}
|
||||
|
||||
@Test
|
||||
void testCompareDifferentMonth() {
|
||||
// 月份大 > 小
|
||||
assertTrue(dto("2023-05-01", BigDecimal.ONE).compareTo(dto("2023-03-01", BigDecimal.ONE)) > 0);
|
||||
}
|
||||
|
||||
@Test
|
||||
void testCompareDifferentWeek() {
|
||||
// 周 大 > 小
|
||||
assertTrue(dto("2023-05-02", BigDecimal.ONE).compareTo(dto("2023-05-01", BigDecimal.ONE)) > 0);
|
||||
// 相同周 状态小 < 大
|
||||
assertTrue(dto("2023-05-02", BigDecimal.ONE).compareTo(dto("2023-05-02", TWO)) < 0);
|
||||
}
|
||||
|
||||
@Test
|
||||
void testBothWithoutWeek() {
|
||||
// 都不带周 状态大 > 小
|
||||
assertTrue(dto("2023-05", new BigDecimal(2)).compareTo(dto("2023-05", BigDecimal.ONE)) > 0);
|
||||
}
|
||||
|
||||
@Test
|
||||
void testOneWithoutMonth() {
|
||||
// 前不带月, 状态小 < 大
|
||||
assertTrue(dto("2023", BigDecimal.ONE).compareTo(dto("2023-05", TWO)) < 0);
|
||||
// 后不带月, 状态大 > 小
|
||||
assertTrue(dto("2023-05", TWO).compareTo(dto("2023", BigDecimal.ONE)) > 0);
|
||||
}
|
||||
|
||||
@Test
|
||||
void testOneWithoutWeek() {
|
||||
// 前不带周, 状态小 < 大
|
||||
assertTrue(dto("2023-05", BigDecimal.ONE).compareTo(dto("2023-05-02", TWO)) < 0);
|
||||
// 后不带周, 状态大 > 小
|
||||
assertTrue(dto("2023-05-02", TWO).compareTo(dto("2023-05", BigDecimal.ONE)) > 0);
|
||||
}
|
||||
}
|
||||
+80
@@ -0,0 +1,80 @@
|
||||
package com.cf.imes.module.system.dal.dataobject.logger;
|
||||
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import java.io.ByteArrayInputStream;
|
||||
import java.io.ByteArrayOutputStream;
|
||||
import java.io.ObjectInputStream;
|
||||
import java.io.ObjectOutputStream;
|
||||
import java.time.LocalDateTime;
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
import static org.junit.jupiter.api.Assertions.assertNotNull;
|
||||
|
||||
/**
|
||||
* {@link OperateLogDO} 的单元测试
|
||||
*
|
||||
* @author Gqr
|
||||
* @since 2025/12/15 11:34
|
||||
*/
|
||||
class OperateLogDOTest {
|
||||
@Test
|
||||
void testJavaSerialization() throws Exception {
|
||||
// given
|
||||
Map<String, Object> exts = new HashMap<>();
|
||||
exts.put("orderId", 10001L);
|
||||
exts.put("status", "PAID");
|
||||
|
||||
OperateLogDO original = new OperateLogDO();
|
||||
original.setId(1L);
|
||||
original.setTraceId("trace-123");
|
||||
original.setUserId(100L);
|
||||
original.setUserType(1);
|
||||
original.setModule("order");
|
||||
original.setName("createOrder");
|
||||
original.setType(1);
|
||||
original.setContent("创建订单");
|
||||
original.setExts(exts);
|
||||
original.setRequestMethod("POST");
|
||||
original.setRequestUrl("/api/order/create");
|
||||
original.setUserIp("127.0.0.1");
|
||||
original.setUserAgent("Mozilla/5.0");
|
||||
original.setJavaMethod("OrderController#create");
|
||||
original.setJavaMethodArgs("{\"productId\":1}");
|
||||
original.setStartTime(LocalDateTime.now());
|
||||
original.setDuration(120);
|
||||
original.setResultCode(0);
|
||||
original.setResultMsg("success");
|
||||
original.setResultData("{\"orderId\":10001}");
|
||||
|
||||
// when:序列化
|
||||
byte[] bytes;
|
||||
try (ByteArrayOutputStream bos = new ByteArrayOutputStream();
|
||||
ObjectOutputStream oos = new ObjectOutputStream(bos)) {
|
||||
|
||||
oos.writeObject(original);
|
||||
bytes = bos.toByteArray();
|
||||
}
|
||||
|
||||
// then:反序列化
|
||||
OperateLogDO deserialized;
|
||||
try (ByteArrayInputStream bis = new ByteArrayInputStream(bytes);
|
||||
ObjectInputStream ois = new ObjectInputStream(bis)) {
|
||||
|
||||
deserialized = (OperateLogDO) ois.readObject();
|
||||
}
|
||||
|
||||
// 核心断言(确保 read/writeObject 被调用)
|
||||
assertNotNull(deserialized);
|
||||
assertEquals(original.getId(), deserialized.getId());
|
||||
assertEquals(original.getTraceId(), deserialized.getTraceId());
|
||||
assertEquals(original.getUserId(), deserialized.getUserId());
|
||||
assertEquals(original.getModule(), deserialized.getModule());
|
||||
assertEquals(original.getName(), deserialized.getName());
|
||||
assertEquals(original.getExts(), deserialized.getExts());
|
||||
assertEquals(original.getResultCode(), deserialized.getResultCode());
|
||||
assertEquals(original.getResultData(), deserialized.getResultData());
|
||||
}
|
||||
}
|
||||
+74
@@ -0,0 +1,74 @@
|
||||
package com.cf.imes.module.system.dal.dataobject.mail;
|
||||
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import java.io.ByteArrayInputStream;
|
||||
import java.io.ByteArrayOutputStream;
|
||||
import java.io.ObjectInputStream;
|
||||
import java.io.ObjectOutputStream;
|
||||
import java.time.LocalDateTime;
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
import static org.junit.jupiter.api.Assertions.assertNotNull;
|
||||
|
||||
/**
|
||||
* {@link MailLogDO} 的单元测试
|
||||
*
|
||||
* @author Gqr
|
||||
* @since 2025/12/15 11:32
|
||||
*/
|
||||
class MailLogDOTest {
|
||||
@Test
|
||||
void testJavaSerialization() throws Exception {
|
||||
// given
|
||||
Map<String, Object> params = new HashMap<>();
|
||||
params.put("code", "RESET_PWD");
|
||||
params.put("expire", 30);
|
||||
|
||||
MailLogDO original = new MailLogDO();
|
||||
original.setId(1L);
|
||||
original.setUserId(100L);
|
||||
original.setUserType(1);
|
||||
original.setToMail("test@example.com");
|
||||
original.setAccountId(10L);
|
||||
original.setFromMail("noreply@example.com");
|
||||
original.setTemplateId(20L);
|
||||
original.setTemplateCode("RESET_PWD");
|
||||
original.setTemplateNickname("系统邮件");
|
||||
original.setTemplateTitle("重置密码");
|
||||
original.setTemplateContent("您的验证码是 ${code}");
|
||||
original.setTemplateParams(params);
|
||||
original.setSendStatus(1);
|
||||
original.setSendTime(LocalDateTime.now());
|
||||
original.setSendMessageId("msg-123");
|
||||
original.setSendException(null);
|
||||
|
||||
// when:序列化
|
||||
byte[] bytes;
|
||||
try (ByteArrayOutputStream bos = new ByteArrayOutputStream();
|
||||
ObjectOutputStream oos = new ObjectOutputStream(bos)) {
|
||||
|
||||
oos.writeObject(original);
|
||||
bytes = bos.toByteArray();
|
||||
}
|
||||
|
||||
// then:反序列化
|
||||
MailLogDO deserialized;
|
||||
try (ByteArrayInputStream bis = new ByteArrayInputStream(bytes);
|
||||
ObjectInputStream ois = new ObjectInputStream(bis)) {
|
||||
|
||||
deserialized = (MailLogDO) ois.readObject();
|
||||
}
|
||||
|
||||
// 基本断言(确保 read/writeObject 被执行)
|
||||
assertNotNull(deserialized);
|
||||
assertEquals(original.getId(), deserialized.getId());
|
||||
assertEquals(original.getUserId(), deserialized.getUserId());
|
||||
assertEquals(original.getToMail(), deserialized.getToMail());
|
||||
assertEquals(original.getTemplateCode(), deserialized.getTemplateCode());
|
||||
assertEquals(original.getTemplateParams(), deserialized.getTemplateParams());
|
||||
assertEquals(original.getSendMessageId(), deserialized.getSendMessageId());
|
||||
}
|
||||
}
|
||||
+66
@@ -0,0 +1,66 @@
|
||||
package com.cf.imes.module.system.dal.dataobject.notify;
|
||||
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import java.io.ByteArrayInputStream;
|
||||
import java.io.ByteArrayOutputStream;
|
||||
import java.io.ObjectInputStream;
|
||||
import java.io.ObjectOutputStream;
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
import static org.junit.jupiter.api.Assertions.assertNotNull;
|
||||
|
||||
/**
|
||||
* {@link NotifyMessageDO} 的单元测试
|
||||
*
|
||||
* @author Gqr
|
||||
* @since 2025/12/15 11:29
|
||||
*/
|
||||
class NotifyMessageDOTest {
|
||||
@Test
|
||||
void testJavaSerialization() throws Exception {
|
||||
// given
|
||||
Map<String, Object> params = new HashMap<>();
|
||||
params.put("orderId", 123L);
|
||||
params.put("amount", 99.9);
|
||||
|
||||
NotifyMessageDO original = NotifyMessageDO.builder()
|
||||
.id(1L)
|
||||
.userId(100L)
|
||||
.userType(1)
|
||||
.templateId(10L)
|
||||
.templateCode("ORDER_PAY")
|
||||
.templateType(1)
|
||||
.templateNickname("系统通知")
|
||||
.templateContent("订单已支付")
|
||||
.templateParams(params)
|
||||
.build();
|
||||
|
||||
// when:序列化
|
||||
byte[] bytes;
|
||||
try (ByteArrayOutputStream bos = new ByteArrayOutputStream();
|
||||
ObjectOutputStream oos = new ObjectOutputStream(bos)) {
|
||||
|
||||
oos.writeObject(original);
|
||||
bytes = bos.toByteArray();
|
||||
}
|
||||
|
||||
// then:反序列化
|
||||
NotifyMessageDO deserialized;
|
||||
try (ByteArrayInputStream bis = new ByteArrayInputStream(bytes);
|
||||
ObjectInputStream ois = new ObjectInputStream(bis)) {
|
||||
|
||||
deserialized = (NotifyMessageDO) ois.readObject();
|
||||
}
|
||||
|
||||
// 验证关键字段
|
||||
assertNotNull(deserialized);
|
||||
assertEquals(original.getId(), deserialized.getId());
|
||||
assertEquals(original.getUserId(), deserialized.getUserId());
|
||||
assertEquals(original.getTemplateCode(), deserialized.getTemplateCode());
|
||||
assertEquals(original.getTemplateContent(), deserialized.getTemplateContent());
|
||||
assertEquals(original.getTemplateParams(), deserialized.getTemplateParams());
|
||||
}
|
||||
}
|
||||
+62
@@ -0,0 +1,62 @@
|
||||
package com.cf.imes.module.system.dal.dataobject.user;
|
||||
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertTrue;
|
||||
|
||||
/**
|
||||
* {@link UserStatusGroupStatisticsDO} 的单元测试
|
||||
*
|
||||
* @author Gqr
|
||||
* @since 2025/12/15 11:25
|
||||
*/
|
||||
class UserStatusGroupStatisticsDOTest {
|
||||
private UserStatusGroupStatisticsDO dto(String date, int count) {
|
||||
return UserStatusGroupStatisticsDO.builder()
|
||||
.date(date)
|
||||
.userCount(count)
|
||||
.build();
|
||||
}
|
||||
|
||||
@Test
|
||||
void testCompareDifferentYear() {
|
||||
// 年份大 > 小
|
||||
assertTrue(dto("2023-01-02", 1).compareTo(dto("2022-01-02", 1)) > 0);
|
||||
}
|
||||
|
||||
@Test
|
||||
void testCompareDifferentMonth() {
|
||||
// 月份大 > 小
|
||||
assertTrue(dto("2023-05-01", 1).compareTo(dto("2023-03-01", 1)) > 0);
|
||||
}
|
||||
|
||||
@Test
|
||||
void testCompareDifferentWeek() {
|
||||
// 周 大 > 小
|
||||
assertTrue(dto("2023-05-02", 1).compareTo(dto("2023-05-01", 1)) > 0);
|
||||
// 相同周 状态小 < 大
|
||||
assertTrue(dto("2023-05-02", 1).compareTo(dto("2023-05-02", 2)) < 0);
|
||||
}
|
||||
|
||||
@Test
|
||||
void testBothWithoutWeek() {
|
||||
// 都不带周 状态大 > 小
|
||||
assertTrue(dto("2023-05", 2).compareTo(dto("2023-05", 1)) > 0);
|
||||
}
|
||||
|
||||
@Test
|
||||
void testOneWithoutMonth() {
|
||||
// 前不带月, 状态小 < 大
|
||||
assertTrue(dto("2023", 1).compareTo(dto("2023-05", 2)) < 0);
|
||||
// 后不带月, 状态大 > 小
|
||||
assertTrue(dto("2023-05", 2).compareTo(dto("2023", 1)) > 0);
|
||||
}
|
||||
|
||||
@Test
|
||||
void testOneWithoutWeek() {
|
||||
// 前不带周, 状态小 < 大
|
||||
assertTrue(dto("2023-05", 1).compareTo(dto("2023-05-02", 2)) < 0);
|
||||
// 后不带周, 状态大 > 小
|
||||
assertTrue(dto("2023-05-02", 2).compareTo(dto("2023-05", 1)) > 0);
|
||||
}
|
||||
}
|
||||
+39
@@ -0,0 +1,39 @@
|
||||
package com.cf.imes.module.system.dal.redis.listener;
|
||||
|
||||
import com.cf.imes.module.system.util.locale.DictDataI18nCacheUtils;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.springframework.data.redis.connection.DefaultMessage;
|
||||
import org.springframework.data.redis.connection.Message;
|
||||
|
||||
import static org.mockito.Mockito.mock;
|
||||
import static org.mockito.Mockito.times;
|
||||
import static org.mockito.Mockito.verify;
|
||||
|
||||
/**
|
||||
* {@link SystemDictDataI18nCacheRefreshRedisListener} 的单元测试
|
||||
*
|
||||
* @author Gqr
|
||||
* @since 2025/12/15 10:11
|
||||
*/
|
||||
class SystemDictDataI18nCacheRefreshRedisListenerTest {
|
||||
@Test
|
||||
void onMessage_shouldClearDictDataI18nCache() {
|
||||
// given
|
||||
SystemDictDataI18nCacheRefreshRedisListener listener =
|
||||
new SystemDictDataI18nCacheRefreshRedisListener();
|
||||
|
||||
Message message = new DefaultMessage(
|
||||
"test".getBytes(),
|
||||
"test-channel".getBytes()
|
||||
);
|
||||
|
||||
// mock 静态方法
|
||||
DictDataI18nCacheUtils dictDataI18nCacheUtils = mock(DictDataI18nCacheUtils.class);
|
||||
|
||||
// when
|
||||
listener.onMessage(message, null);
|
||||
|
||||
// then
|
||||
verify(dictDataI18nCacheUtils, times(1)).clearCache();
|
||||
}
|
||||
}
|
||||
+37
@@ -0,0 +1,37 @@
|
||||
package com.cf.imes.module.system.dal.redis.listener;
|
||||
|
||||
import com.cf.imes.framework.security.core.service.SecurityFrameworkService;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.springframework.data.redis.connection.DefaultMessage;
|
||||
import org.springframework.data.redis.connection.Message;
|
||||
|
||||
import static org.mockito.Mockito.mock;
|
||||
import static org.mockito.Mockito.times;
|
||||
import static org.mockito.Mockito.verify;
|
||||
|
||||
/**
|
||||
* {@link SystemPermissionRefreshRedisListener} 的单元测试
|
||||
*
|
||||
* @author Gqr
|
||||
* @since 2025/12/15 10:23
|
||||
*/
|
||||
class SystemPermissionRefreshRedisListenerTest {
|
||||
@Test
|
||||
void onMessage_shouldClearDictDataI18nCache() {
|
||||
SecurityFrameworkService securityFrameworkService = mock(SecurityFrameworkService.class);
|
||||
// given
|
||||
SystemPermissionRefreshRedisListener listener =
|
||||
new SystemPermissionRefreshRedisListener(securityFrameworkService);
|
||||
|
||||
Message message = new DefaultMessage(
|
||||
"test".getBytes(),
|
||||
"test-channel".getBytes()
|
||||
);
|
||||
|
||||
// when
|
||||
listener.onMessage(message, null);
|
||||
|
||||
// then
|
||||
verify(securityFrameworkService, times(1)).invalidateAllPermissionsCache();
|
||||
}
|
||||
}
|
||||
+152
@@ -0,0 +1,152 @@
|
||||
package com.cf.imes.module.system.dal.redis.oauth2;
|
||||
|
||||
import com.alibaba.fastjson.JSON;
|
||||
import com.cf.imes.framework.test.core.ut.BaseRedisUnitTest;
|
||||
import com.cf.imes.module.system.dal.dataobject.oauth2.OAuth2AccessTokenDO;
|
||||
import com.cf.imes.module.system.dal.redis.RedisKeyConstants;
|
||||
import jakarta.annotation.Resource;
|
||||
import org.junit.jupiter.api.BeforeEach;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.mockito.ArgumentCaptor;
|
||||
import org.springframework.context.annotation.Import;
|
||||
import org.springframework.data.redis.core.Cursor;
|
||||
import org.springframework.data.redis.core.ScanOptions;
|
||||
import org.springframework.data.redis.core.StringRedisTemplate;
|
||||
import org.springframework.data.redis.core.ValueOperations;
|
||||
import org.springframework.test.context.bean.override.mockito.MockitoBean;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.concurrent.TimeUnit;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
import static org.mockito.ArgumentMatchers.any;
|
||||
import static org.mockito.ArgumentMatchers.eq;
|
||||
import static org.mockito.Mockito.mock;
|
||||
import static org.mockito.Mockito.verify;
|
||||
import static org.mockito.Mockito.when;
|
||||
|
||||
/**
|
||||
* {@link OAuth2AccessTokenRedisDAO} 单元测试类
|
||||
*
|
||||
* @author Gqr
|
||||
* @since 2025/12/15 10:51
|
||||
*/
|
||||
@Import(OAuth2AccessTokenRedisDAO.class)
|
||||
class OAuth2AccessTokenRedisDAOTest extends BaseRedisUnitTest {
|
||||
@Resource
|
||||
private OAuth2AccessTokenRedisDAO redisDAO;
|
||||
|
||||
@MockitoBean
|
||||
private StringRedisTemplate stringRedisTemplate;
|
||||
|
||||
private ValueOperations<String, String> valueOperations;
|
||||
|
||||
@BeforeEach
|
||||
void setUp() {
|
||||
valueOperations = mock(ValueOperations.class);
|
||||
|
||||
when(stringRedisTemplate.opsForValue()).thenReturn(valueOperations);
|
||||
}
|
||||
|
||||
@Test
|
||||
void testGet() {
|
||||
String accessToken = "token123";
|
||||
String redisKey = String.format(RedisKeyConstants.OAUTH2_ACCESS_TOKEN, accessToken);
|
||||
|
||||
OAuth2AccessTokenDO tokenDO = new OAuth2AccessTokenDO();
|
||||
tokenDO.setAccessToken(accessToken);
|
||||
|
||||
when(valueOperations.get(redisKey)).thenReturn(JSON.toJSONString(tokenDO));
|
||||
|
||||
OAuth2AccessTokenDO result = redisDAO.get(accessToken);
|
||||
|
||||
assertEquals(accessToken, result.getAccessToken());
|
||||
verify(valueOperations).get(redisKey);
|
||||
}
|
||||
|
||||
@Test
|
||||
void testGetWithKey() {
|
||||
String redisKey = "oauth2_access_token:token123";
|
||||
|
||||
OAuth2AccessTokenDO tokenDO = new OAuth2AccessTokenDO();
|
||||
tokenDO.setAccessToken("token123");
|
||||
|
||||
when(valueOperations.get(redisKey)).thenReturn(JSON.toJSONString(tokenDO));
|
||||
|
||||
OAuth2AccessTokenDO result = redisDAO.getWithKey(redisKey);
|
||||
|
||||
assertEquals("token123", result.getAccessToken());
|
||||
verify(valueOperations).get(redisKey);
|
||||
}
|
||||
|
||||
@Test
|
||||
void testSet() {
|
||||
OAuth2AccessTokenDO tokenDO = new OAuth2AccessTokenDO();
|
||||
tokenDO.setAccessToken("token123");
|
||||
|
||||
redisDAO.set(tokenDO);
|
||||
|
||||
ArgumentCaptor<String> keyCaptor = ArgumentCaptor.forClass(String.class);
|
||||
ArgumentCaptor<String> valueCaptor = ArgumentCaptor.forClass(String.class);
|
||||
|
||||
verify(valueOperations).set(
|
||||
keyCaptor.capture(),
|
||||
valueCaptor.capture(),
|
||||
eq(1L),
|
||||
eq(TimeUnit.DAYS)
|
||||
);
|
||||
|
||||
assertEquals(
|
||||
String.format(RedisKeyConstants.OAUTH2_ACCESS_TOKEN, "token123"),
|
||||
keyCaptor.getValue()
|
||||
);
|
||||
}
|
||||
|
||||
@Test
|
||||
void testDelete() {
|
||||
redisDAO.delete("token123");
|
||||
|
||||
verify(stringRedisTemplate).delete(
|
||||
String.format(RedisKeyConstants.OAUTH2_ACCESS_TOKEN, "token123")
|
||||
);
|
||||
}
|
||||
|
||||
@Test
|
||||
void testDeleteWithKey() {
|
||||
redisDAO.deleteWithKey("oauth2_access_token:token123");
|
||||
|
||||
verify(stringRedisTemplate).delete("oauth2_access_token:token123");
|
||||
}
|
||||
|
||||
@Test
|
||||
void testDeleteList() {
|
||||
redisDAO.deleteList(List.of("t1", "t2"));
|
||||
|
||||
verify(stringRedisTemplate).delete(List.of(
|
||||
String.format(RedisKeyConstants.OAUTH2_ACCESS_TOKEN, "t1"),
|
||||
String.format(RedisKeyConstants.OAUTH2_ACCESS_TOKEN, "t2")
|
||||
));
|
||||
}
|
||||
|
||||
@Test
|
||||
void testExpire() {
|
||||
redisDAO.expire("token123", 10, TimeUnit.MINUTES);
|
||||
|
||||
verify(stringRedisTemplate).expire(
|
||||
String.format(RedisKeyConstants.OAUTH2_ACCESS_TOKEN, "token123"),
|
||||
10,
|
||||
TimeUnit.MINUTES
|
||||
);
|
||||
}
|
||||
|
||||
@Test
|
||||
void testScan() {
|
||||
Cursor<String> cursor = mock(Cursor.class);
|
||||
when(stringRedisTemplate.scan(any(ScanOptions.class))).thenReturn(cursor);
|
||||
|
||||
Cursor<String> result = redisDAO.scan();
|
||||
|
||||
assertEquals(cursor, result);
|
||||
verify(stringRedisTemplate).scan(any(ScanOptions.class));
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user