Skip to content
项目
群组
代码片段
帮助
当前项目
正在载入...
登录 / 注册
切换导航面板
Y
yudao-cloud
项目
项目
详情
活动
周期分析
仓库
仓库
文件
提交
分支
标签
贡献者
图表
比较
统计图
议题
0
议题
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
CI / CD
CI / CD
流水线
作业
日程
统计图
Wiki
Wiki
代码片段
代码片段
成员
成员
折叠边栏
关闭边栏
活动
图像
聊天
创建新问题
作业
提交
问题看板
Open sidebar
hblj
yudao-cloud
Commits
dd1ed7b8
提交
dd1ed7b8
authored
2月 04, 2023
作者:
YunaiV
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
infra:完善 logger 的单元测试
上级
ee7cb505
隐藏空白字符变更
内嵌
并排
正在显示
5 个修改的文件
包含
208 行增加
和
250 行删除
+208
-250
ApiErrorLogServiceImpl.java
...o/module/infra/service/logger/ApiErrorLogServiceImpl.java
+9
-9
serviceImpl.vm
...iz/src/main/resources/codegen/java/service/serviceImpl.vm
+2
-2
serviceTest.vm
...a-biz/src/main/resources/codegen/java/test/serviceTest.vm
+1
-1
ApiAccessLogServiceImplTest.java
...ule/infra/service/logger/ApiAccessLogServiceImplTest.java
+75
-106
ApiErrorLogServiceImplTest.java
...dule/infra/service/logger/ApiErrorLogServiceImplTest.java
+121
-132
没有找到文件。
yudao-module-infra/yudao-module-infra-biz/src/main/java/cn/iocoder/yudao/module/infra/service/logger/ApiErrorLogServiceImpl.java
浏览文件 @
dd1ed7b8
package
cn
.
iocoder
.
yudao
.
module
.
infra
.
service
.
logger
;
import
cn.iocoder.yudao.framework.
apilog.core.service.ApiErrorLog
;
import
cn.iocoder.yudao.framework.
common.pojo.PageResult
;
import
cn.iocoder.yudao.module.infra.api.logger.dto.ApiErrorLogCreateReqDTO
;
import
cn.iocoder.yudao.module.infra.controller.admin.logger.vo.apierrorlog.ApiErrorLogExportReqVO
;
import
cn.iocoder.yudao.module.infra.controller.admin.logger.vo.apierrorlog.ApiErrorLogPageReqVO
;
import
cn.iocoder.yudao.module.infra.convert.logger.ApiErrorLogConvert
;
import
cn.iocoder.yudao.module.infra.dal.dataobject.logger.ApiErrorLogDO
;
import
cn.iocoder.yudao.module.infra.dal.mysql.logger.ApiErrorLogMapper
;
import
cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil
;
import
cn.iocoder.yudao.framework.common.pojo.PageResult
;
import
cn.iocoder.yudao.module.infra.enums.ErrorCodeConstants
;
import
cn.iocoder.yudao.module.infra.enums.logger.ApiErrorLogProcessStatusEnum
;
import
org.springframework.scheduling.annotation.Async
;
import
org.springframework.stereotype.Service
;
import
org.springframework.validation.annotation.Validated
;
...
...
@@ -19,6 +15,10 @@ import javax.annotation.Resource;
import
java.time.LocalDateTime
;
import
java.util.List
;
import
static
cn
.
iocoder
.
yudao
.
framework
.
common
.
exception
.
util
.
ServiceExceptionUtil
.
exception
;
import
static
cn
.
iocoder
.
yudao
.
module
.
infra
.
enums
.
ErrorCodeConstants
.
API_ERROR_LOG_NOT_FOUND
;
import
static
cn
.
iocoder
.
yudao
.
module
.
infra
.
enums
.
ErrorCodeConstants
.
API_ERROR_LOG_PROCESSED
;
/**
* API 错误日志 Service 实现类
*
...
...
@@ -33,8 +33,8 @@ public class ApiErrorLogServiceImpl implements ApiErrorLogService {
@Override
public
void
createApiErrorLog
(
ApiErrorLogCreateReqDTO
createDTO
)
{
ApiErrorLogDO
apiErrorLog
=
ApiErrorLogConvert
.
INSTANCE
.
convert
(
createDTO
)
;
apiErrorLog
.
setProcessStatus
(
ApiErrorLogProcessStatusEnum
.
INIT
.
getStatus
());
ApiErrorLogDO
apiErrorLog
=
ApiErrorLogConvert
.
INSTANCE
.
convert
(
createDTO
)
.
setProcessStatus
(
ApiErrorLogProcessStatusEnum
.
INIT
.
getStatus
());
apiErrorLogMapper
.
insert
(
apiErrorLog
);
}
...
...
@@ -52,10 +52,10 @@ public class ApiErrorLogServiceImpl implements ApiErrorLogService {
public
void
updateApiErrorLogProcess
(
Long
id
,
Integer
processStatus
,
Long
processUserId
)
{
ApiErrorLogDO
errorLog
=
apiErrorLogMapper
.
selectById
(
id
);
if
(
errorLog
==
null
)
{
throw
ServiceExceptionUtil
.
exception
(
ErrorCodeConstants
.
API_ERROR_LOG_NOT_FOUND
);
throw
exception
(
API_ERROR_LOG_NOT_FOUND
);
}
if
(!
ApiErrorLogProcessStatusEnum
.
INIT
.
getStatus
().
equals
(
errorLog
.
getProcessStatus
()))
{
throw
ServiceExceptionUtil
.
exception
(
ErrorCodeConstants
.
API_ERROR_LOG_PROCESSED
);
throw
exception
(
API_ERROR_LOG_PROCESSED
);
}
// 标记处理
apiErrorLogMapper
.
updateById
(
ApiErrorLogDO
.
builder
().
id
(
id
).
processStatus
(
processStatus
)
...
...
yudao-module-infra/yudao-module-infra-biz/src/main/resources/codegen/java/service/serviceImpl.vm
浏览文件 @
dd1ed7b8
...
...
@@ -39,7 +39,7 @@ public class ${table.className}ServiceImpl implements ${table.className}Service
@
Override
public
void
update
${
simpleClassName
}(${
sceneEnum
.
prefixClass
}${
table
.
className
}
UpdateReqVO
updateReqVO
)
{
//
校验存在
this
.
validate
${
simpleClassName
}
Exists
(
updateReqVO
.
getId
());
validate
${
simpleClassName
}
Exists
(
updateReqVO
.
getId
());
//
更新
${
table
.
className
}
DO
updateObj
=
${
table
.
className
}
Convert
.
INSTANCE
.
convert
(
updateReqVO
);
${
classNameVar
}
Mapper
.
updateById
(
updateObj
);
...
...
@@ -48,7 +48,7 @@ public class ${table.className}ServiceImpl implements ${table.className}Service
@
Override
public
void
delete
${
simpleClassName
}(${
primaryColumn
.
javaType
}
id
)
{
//
校验存在
this
.
validate
${
simpleClassName
}
Exists
(
id
);
validate
${
simpleClassName
}
Exists
(
id
);
//
删除
${
classNameVar
}
Mapper
.
deleteById
(
id
);
}
...
...
yudao-module-infra/yudao-module-infra-biz/src/main/resources/codegen/java/test/serviceTest.vm
浏览文件 @
dd1ed7b8
...
...
@@ -52,7 +52,7 @@ import static org.mockito.Mockito.*;
#
if
(${
column
.
listOperation
})
#
set
($
JavaField
=
$
column
.
javaField
.
substring
(
0
,
1
).
toUpperCase
()
+
${
column
.
javaField
.
substring
(
1
)})##
首字母大写
#
if
(${
column
.
listOperationCondition
}
==
"BETWEEN"
)##
BETWEEN
的情况
reqVO
.
set
${
JavaField
}(
(
new
LocalDateTime
[]{}
));
reqVO
.
set
${
JavaField
}(
buildBetweenTime
(
2023
,
2
,
1
,
2023
,
2
,
28
));
#
else
reqVO
.
set
$
JavaField
(
null
);
#
end
...
...
yudao-module-infra/yudao-module-infra-biz/src/test/java/cn/iocoder/yudao/module/infra/service/logger/ApiAccessLogServiceImplTest.java
浏览文件 @
dd1ed7b8
package
cn
.
iocoder
.
yudao
.
module
.
infra
.
service
.
logger
;
import
cn.hutool.core.util.RandomUtil
;
import
cn.iocoder.yudao.framework.common.enums.UserTypeEnum
;
import
cn.iocoder.yudao.framework.common.exception.enums.GlobalErrorCodeConstants
;
import
cn.iocoder.yudao.framework.common.pojo.PageResult
;
import
cn.iocoder.yudao.framework.common.util.object.ObjectUtils
;
import
cn.iocoder.yudao.framework.test.core.ut.BaseDbUnitTest
;
import
cn.iocoder.yudao.framework.test.core.util.RandomUtils
;
import
cn.iocoder.yudao.module.infra.api.logger.dto.ApiAccessLogCreateReqDTO
;
import
cn.iocoder.yudao.module.infra.controller.admin.logger.vo.apiaccesslog.ApiAccessLogExportReqVO
;
import
cn.iocoder.yudao.module.infra.controller.admin.logger.vo.apiaccesslog.ApiAccessLogPageReqVO
;
...
...
@@ -16,149 +13,121 @@ import org.junit.jupiter.api.Test;
import
org.springframework.context.annotation.Import
;
import
javax.annotation.Resource
;
import
java.time.LocalDateTime
;
import
java.util.List
;
import
static
cn
.
iocoder
.
yudao
.
framework
.
common
.
util
.
date
.
DateUtils
.
buildLocalDateTime
;
import
static
cn
.
iocoder
.
yudao
.
framework
.
common
.
util
.
date
.
LocalDateTimeUtils
.
buildBetweenTime
;
import
static
cn
.
iocoder
.
yudao
.
framework
.
common
.
util
.
date
.
LocalDateTimeUtils
.
buildTime
;
import
static
cn
.
iocoder
.
yudao
.
framework
.
common
.
util
.
object
.
ObjectUtils
.
cloneIgnoreId
;
import
static
cn
.
iocoder
.
yudao
.
framework
.
test
.
core
.
util
.
AssertUtils
.
assertPojoEquals
;
import
static
cn
.
iocoder
.
yudao
.
framework
.
test
.
core
.
util
.
RandomUtils
.
randomPojo
;
import
static
org
.
junit
.
jupiter
.
api
.
Assertions
.
assertEquals
;
import
static
org
.
junit
.
jupiter
.
api
.
Assertions
.
assertNotNull
;
@Import
(
ApiAccessLogServiceImpl
.
class
)
public
class
ApiAccessLogServiceImplTest
extends
BaseDbUnitTest
{
@Resource
private
ApiAccessLogService
apiAccessLogService
;
private
ApiAccessLogService
Impl
apiAccessLogService
;
@Resource
private
ApiAccessLogMapper
apiAccessLogMapper
;
@Test
public
void
testGetApiAccessLogPage
()
{
// 构造测试数据
long
userId
=
2233L
;
int
userType
=
UserTypeEnum
.
ADMIN
.
getValue
();
String
applicationName
=
"yudao-test"
;
String
requestUrl
=
"foo"
;
LocalDateTime
beginTime
=
buildLocalDateTime
(
2021
,
3
,
13
);
int
duration
=
1000
;
int
resultCode
=
GlobalErrorCodeConstants
.
SUCCESS
.
getCode
();
ApiAccessLogDO
infApiAccessLogDO
=
RandomUtils
.
randomPojo
(
ApiAccessLogDO
.
class
,
dto
->
{
dto
.
setUserId
(
userId
);
dto
.
setUserType
(
userType
);
dto
.
setApplicationName
(
applicationName
);
dto
.
setRequestUrl
(
requestUrl
);
dto
.
setBeginTime
(
beginTime
);
dto
.
setDuration
(
duration
);
dto
.
setResultCode
(
resultCode
);
ApiAccessLogDO
apiAccessLogDO
=
randomPojo
(
ApiAccessLogDO
.
class
,
o
->
{
o
.
setUserId
(
2233L
);
o
.
setUserType
(
UserTypeEnum
.
ADMIN
.
getValue
());
o
.
setApplicationName
(
"yudao-test"
);
o
.
setRequestUrl
(
"foo"
);
o
.
setBeginTime
(
buildTime
(
2021
,
3
,
13
));
o
.
setDuration
(
1000
);
o
.
setResultCode
(
GlobalErrorCodeConstants
.
SUCCESS
.
getCode
());
});
apiAccessLogMapper
.
insert
(
infApiAccessLogDO
);
// 下面几个都是不匹配的数据
// userId 不同的
apiAccessLogMapper
.
insert
(
ObjectUtils
.
cloneIgnoreId
(
infApiAccessLogDO
,
logDO
->
logDO
.
setUserId
(
3344L
)));
// userType
apiAccessLogMapper
.
insert
(
ObjectUtils
.
cloneIgnoreId
(
infApiAccessLogDO
,
logDO
->
logDO
.
setUserType
(
UserTypeEnum
.
MEMBER
.
getValue
())));
// applicationName 不同的
apiAccessLogMapper
.
insert
(
ObjectUtils
.
cloneIgnoreId
(
infApiAccessLogDO
,
logDO
->
logDO
.
setApplicationName
(
"test"
)));
// requestUrl 不同的
apiAccessLogMapper
.
insert
(
ObjectUtils
.
cloneIgnoreId
(
infApiAccessLogDO
,
logDO
->
logDO
.
setRequestUrl
(
"bar"
)));
// 构造一个早期时间 2021-02-06 00:00:00
apiAccessLogMapper
.
insert
(
ObjectUtils
.
cloneIgnoreId
(
infApiAccessLogDO
,
logDO
->
logDO
.
setBeginTime
(
buildLocalDateTime
(
2021
,
2
,
6
))));
// duration 不同的
apiAccessLogMapper
.
insert
(
ObjectUtils
.
cloneIgnoreId
(
infApiAccessLogDO
,
logDO
->
logDO
.
setDuration
(
100
)));
// resultCode 不同的
apiAccessLogMapper
.
insert
(
ObjectUtils
.
cloneIgnoreId
(
infApiAccessLogDO
,
logDO
->
logDO
.
setResultCode
(
2
)));
// 构造调用参数
apiAccessLogMapper
.
insert
(
apiAccessLogDO
);
// 测试 userId 不匹配
apiAccessLogMapper
.
insert
(
cloneIgnoreId
(
apiAccessLogDO
,
o
->
o
.
setUserId
(
3344L
)));
// 测试 userType 不匹配
apiAccessLogMapper
.
insert
(
cloneIgnoreId
(
apiAccessLogDO
,
o
->
o
.
setUserType
(
UserTypeEnum
.
MEMBER
.
getValue
())));
// 测试 applicationName 不匹配
apiAccessLogMapper
.
insert
(
cloneIgnoreId
(
apiAccessLogDO
,
o
->
o
.
setApplicationName
(
"test"
)));
// 测试 requestUrl 不匹配
apiAccessLogMapper
.
insert
(
cloneIgnoreId
(
apiAccessLogDO
,
o
->
o
.
setRequestUrl
(
"bar"
)));
// 测试 beginTime 不匹配:构造一个早期时间 2021-02-06 00:00:00
apiAccessLogMapper
.
insert
(
cloneIgnoreId
(
apiAccessLogDO
,
o
->
o
.
setBeginTime
(
buildTime
(
2021
,
2
,
6
))));
// 测试 duration 不匹配
apiAccessLogMapper
.
insert
(
cloneIgnoreId
(
apiAccessLogDO
,
o
->
o
.
setDuration
(
100
)));
// 测试 resultCode 不匹配
apiAccessLogMapper
.
insert
(
cloneIgnoreId
(
apiAccessLogDO
,
o
->
o
.
setResultCode
(
2
)));
// 准备参数
ApiAccessLogPageReqVO
reqVO
=
new
ApiAccessLogPageReqVO
();
reqVO
.
setUserId
(
userId
);
reqVO
.
setUserType
(
userType
);
reqVO
.
setApplicationName
(
applicationName
);
reqVO
.
setRequestUrl
(
requestUrl
);
reqVO
.
setBeginTime
(
(
new
LocalDateTime
[]{
buildLocalDateTime
(
2021
,
3
,
12
),
buildLocalDateTime
(
2021
,
3
,
14
)}
));
reqVO
.
setDuration
(
duration
);
reqVO
.
setResultCode
(
resultCode
);
reqVO
.
setUserId
(
2233L
);
reqVO
.
setUserType
(
UserTypeEnum
.
ADMIN
.
getValue
()
);
reqVO
.
setApplicationName
(
"yudao-test"
);
reqVO
.
setRequestUrl
(
"foo"
);
reqVO
.
setBeginTime
(
buildBetweenTime
(
2021
,
3
,
13
,
2021
,
3
,
13
));
reqVO
.
setDuration
(
1000
);
reqVO
.
setResultCode
(
GlobalErrorCodeConstants
.
SUCCESS
.
getCode
()
);
// 调用
service方法
// 调用
PageResult
<
ApiAccessLogDO
>
pageResult
=
apiAccessLogService
.
getApiAccessLogPage
(
reqVO
);
// 断言,只查到了一条符合条件的
assertEquals
(
1
,
pageResult
.
getTotal
());
assertEquals
(
1
,
pageResult
.
getList
().
size
());
assertPojoEquals
(
infA
piAccessLogDO
,
pageResult
.
getList
().
get
(
0
));
assertPojoEquals
(
a
piAccessLogDO
,
pageResult
.
getList
().
get
(
0
));
}
@Test
public
void
testGetApiAccessLogList
()
{
// 构造测试数据
long
userId
=
2233L
;
int
userType
=
UserTypeEnum
.
ADMIN
.
getValue
();
String
applicationName
=
"yudao-test"
;
String
requestUrl
=
"foo"
;
LocalDateTime
beginTime
=
buildLocalDateTime
(
2021
,
3
,
13
);
int
duration
=
1000
;
int
resultCode
=
GlobalErrorCodeConstants
.
SUCCESS
.
getCode
();
ApiAccessLogDO
infApiAccessLogDO
=
RandomUtils
.
randomPojo
(
ApiAccessLogDO
.
class
,
dto
->
{
dto
.
setUserId
(
userId
);
dto
.
setUserType
(
userType
);
dto
.
setApplicationName
(
applicationName
);
dto
.
setRequestUrl
(
requestUrl
);
dto
.
setBeginTime
(
beginTime
);
dto
.
setDuration
(
duration
);
dto
.
setResultCode
(
resultCode
);
ApiAccessLogDO
apiAccessLogDO
=
randomPojo
(
ApiAccessLogDO
.
class
,
o
->
{
o
.
setUserId
(
2233L
);
o
.
setUserType
(
UserTypeEnum
.
ADMIN
.
getValue
());
o
.
setApplicationName
(
"yudao-test"
);
o
.
setRequestUrl
(
"foo"
);
o
.
setBeginTime
(
buildTime
(
2021
,
3
,
13
));
o
.
setDuration
(
1000
);
o
.
setResultCode
(
GlobalErrorCodeConstants
.
SUCCESS
.
getCode
());
});
apiAccessLogMapper
.
insert
(
infApiAccessLogDO
);
// 下面几个都是不匹配的数据
// userId 不同的
apiAccessLogMapper
.
insert
(
ObjectUtils
.
cloneIgnoreId
(
infApiAccessLogDO
,
logDO
->
logDO
.
setUserId
(
3344L
)));
// userType
apiAccessLogMapper
.
insert
(
ObjectUtils
.
cloneIgnoreId
(
infApiAccessLogDO
,
logDO
->
logDO
.
setUserType
(
UserTypeEnum
.
MEMBER
.
getValue
())));
// applicationName 不同的
apiAccessLogMapper
.
insert
(
ObjectUtils
.
cloneIgnoreId
(
infApiAccessLogDO
,
logDO
->
logDO
.
setApplicationName
(
"test"
)));
// requestUrl 不同的
apiAccessLogMapper
.
insert
(
ObjectUtils
.
cloneIgnoreId
(
infApiAccessLogDO
,
logDO
->
logDO
.
setRequestUrl
(
"bar"
)));
// 构造一个早期时间 2021-02-06 00:00:00
apiAccessLogMapper
.
insert
(
ObjectUtils
.
cloneIgnoreId
(
infApiAccessLogDO
,
logDO
->
logDO
.
setBeginTime
(
buildLocalDateTime
(
2021
,
2
,
6
))));
// duration 不同的
apiAccessLogMapper
.
insert
(
ObjectUtils
.
cloneIgnoreId
(
infApiAccessLogDO
,
logDO
->
logDO
.
setDuration
(
100
)));
// resultCode 不同的
apiAccessLogMapper
.
insert
(
ObjectUtils
.
cloneIgnoreId
(
infApiAccessLogDO
,
logDO
->
logDO
.
setResultCode
(
2
)));
// 构造调用参数
apiAccessLogMapper
.
insert
(
apiAccessLogDO
);
// 测试 userId 不匹配
apiAccessLogMapper
.
insert
(
cloneIgnoreId
(
apiAccessLogDO
,
o
->
o
.
setUserId
(
3344L
)));
// 测试 userType 不匹配
apiAccessLogMapper
.
insert
(
cloneIgnoreId
(
apiAccessLogDO
,
o
->
o
.
setUserType
(
UserTypeEnum
.
MEMBER
.
getValue
())));
// 测试 applicationName 不匹配
apiAccessLogMapper
.
insert
(
cloneIgnoreId
(
apiAccessLogDO
,
o
->
o
.
setApplicationName
(
"test"
)));
// 测试 requestUrl 不匹配
apiAccessLogMapper
.
insert
(
cloneIgnoreId
(
apiAccessLogDO
,
o
->
o
.
setRequestUrl
(
"bar"
)));
// 测试 beginTime 不匹配:构造一个早期时间 2021-02-06 00:00:00
apiAccessLogMapper
.
insert
(
cloneIgnoreId
(
apiAccessLogDO
,
o
->
o
.
setBeginTime
(
buildTime
(
2021
,
2
,
6
))));
// 测试 duration 不匹配
apiAccessLogMapper
.
insert
(
cloneIgnoreId
(
apiAccessLogDO
,
o
->
o
.
setDuration
(
100
)));
// 测试 resultCode 不匹配
apiAccessLogMapper
.
insert
(
cloneIgnoreId
(
apiAccessLogDO
,
o
->
o
.
setResultCode
(
2
)));
// 准备参数
ApiAccessLogExportReqVO
reqVO
=
new
ApiAccessLogExportReqVO
();
reqVO
.
setUserId
(
userId
);
reqVO
.
setUserType
(
userType
);
reqVO
.
setApplicationName
(
applicationName
);
reqVO
.
setRequestUrl
(
requestUrl
);
reqVO
.
setBeginTime
(
(
new
LocalDateTime
[]{
buildLocalDateTime
(
2021
,
3
,
12
),
buildLocalDateTime
(
2021
,
3
,
14
)}
));
reqVO
.
setDuration
(
duration
);
reqVO
.
setResultCode
(
resultCode
);
reqVO
.
setUserId
(
2233L
);
reqVO
.
setUserType
(
UserTypeEnum
.
ADMIN
.
getValue
()
);
reqVO
.
setApplicationName
(
"yudao-test"
);
reqVO
.
setRequestUrl
(
"foo"
);
reqVO
.
setBeginTime
(
buildBetweenTime
(
2021
,
3
,
13
,
2021
,
3
,
13
));
reqVO
.
setDuration
(
1000
);
reqVO
.
setResultCode
(
GlobalErrorCodeConstants
.
SUCCESS
.
getCode
()
);
// 调用
service方法
// 调用
List
<
ApiAccessLogDO
>
list
=
apiAccessLogService
.
getApiAccessLogList
(
reqVO
);
// 断言,只查到了一条符合条件的
assertEquals
(
1
,
list
.
size
());
assertPojoEquals
(
infA
piAccessLogDO
,
list
.
get
(
0
));
assertPojoEquals
(
a
piAccessLogDO
,
list
.
get
(
0
));
}
@Test
public
void
testCreateApiAccessLog
Async
()
{
public
void
testCreateApiAccessLog
()
{
// 准备参数
ApiAccessLogCreateReqDTO
createDTO
=
RandomUtils
.
randomPojo
(
ApiAccessLogCreateReqDTO
.
class
,
dto
->
dto
.
setUserType
(
RandomUtil
.
randomEle
(
UserTypeEnum
.
values
()).
getValue
()));
ApiAccessLogCreateReqDTO
createDTO
=
randomPojo
(
ApiAccessLogCreateReqDTO
.
class
);
// 调用
apiAccessLogService
.
createApiAccessLog
(
createDTO
);
// 断言
ApiAccessLogDO
infApiAccessLogDO
=
apiAccessLogMapper
.
selectOne
(
null
);
assertNotNull
(
infApiAccessLogDO
);
assertPojoEquals
(
createDTO
,
infApiAccessLogDO
);
ApiAccessLogDO
apiAccessLogDO
=
apiAccessLogMapper
.
selectOne
(
null
);
assertPojoEquals
(
createDTO
,
apiAccessLogDO
);
}
}
yudao-module-infra/yudao-module-infra-biz/src/test/java/cn/iocoder/yudao/module/infra/service/logger/ApiErrorLogServiceImplTest.java
浏览文件 @
dd1ed7b8
package
cn
.
iocoder
.
yudao
.
module
.
infra
.
service
.
logger
;
import
cn.hutool.core.util.RandomUtil
;
import
cn.iocoder.yudao.framework.common.enums.UserTypeEnum
;
import
cn.iocoder.yudao.framework.common.pojo.PageResult
;
import
cn.iocoder.yudao.framework.common.util.object.ObjectUtils
;
import
cn.iocoder.yudao.framework.test.core.ut.BaseDbUnitTest
;
import
cn.iocoder.yudao.framework.test.core.util.RandomUtils
;
import
cn.iocoder.yudao.module.infra.api.logger.dto.ApiErrorLogCreateReqDTO
;
import
cn.iocoder.yudao.module.infra.controller.admin.logger.vo.apierrorlog.ApiErrorLogExportReqVO
;
import
cn.iocoder.yudao.module.infra.controller.admin.logger.vo.apierrorlog.ApiErrorLogPageReqVO
;
import
cn.iocoder.yudao.module.infra.dal.dataobject.logger.ApiErrorLogDO
;
import
cn.iocoder.yudao.module.infra.dal.mysql.logger.ApiErrorLogMapper
;
import
cn.iocoder.yudao.module.infra.enums.logger.ApiErrorLogProcessStatusEnum
;
import
org.junit.jupiter.api.Assertions
;
import
org.junit.jupiter.api.Test
;
import
org.springframework.context.annotation.Import
;
import
javax.annotation.Resource
;
import
java.time.LocalDateTime
;
import
java.util.List
;
import
static
cn
.
iocoder
.
yudao
.
framework
.
common
.
util
.
date
.
DateUtils
.
buildLocalDateTime
;
import
static
cn
.
hutool
.
core
.
util
.
RandomUtil
.
randomEle
;
import
static
cn
.
iocoder
.
yudao
.
framework
.
common
.
util
.
date
.
LocalDateTimeUtils
.
buildBetweenTime
;
import
static
cn
.
iocoder
.
yudao
.
framework
.
common
.
util
.
date
.
LocalDateTimeUtils
.
buildTime
;
import
static
cn
.
iocoder
.
yudao
.
framework
.
common
.
util
.
object
.
ObjectUtils
.
cloneIgnoreId
;
import
static
cn
.
iocoder
.
yudao
.
framework
.
test
.
core
.
util
.
AssertUtils
.
assertPojoEquals
;
import
static
cn
.
iocoder
.
yudao
.
framework
.
test
.
core
.
util
.
AssertUtils
.
assertServiceException
;
import
static
cn
.
iocoder
.
yudao
.
framework
.
test
.
core
.
util
.
RandomUtils
.
randomLongId
;
import
static
cn
.
iocoder
.
yudao
.
framework
.
test
.
core
.
util
.
RandomUtils
.
randomPojo
;
import
static
cn
.
iocoder
.
yudao
.
module
.
infra
.
enums
.
ErrorCodeConstants
.
API_ERROR_LOG_NOT_FOUND
;
import
static
cn
.
iocoder
.
yudao
.
module
.
infra
.
enums
.
ErrorCodeConstants
.
API_ERROR_LOG_PROCESSED
;
import
static
org
.
junit
.
jupiter
.
api
.
Assertions
.
assertEquals
;
...
...
@@ -35,161 +35,150 @@ public class ApiErrorLogServiceImplTest extends BaseDbUnitTest {
private
ApiErrorLogServiceImpl
apiErrorLogService
;
@Resource
private
ApiErrorLogMapper
infA
piErrorLogMapper
;
private
ApiErrorLogMapper
a
piErrorLogMapper
;
@Test
public
void
testGetApiErrorLogPage
()
{
// 构造测试数据
long
userId
=
2233L
;
int
userType
=
UserTypeEnum
.
ADMIN
.
getValue
();
String
applicationName
=
"yudao-test"
;
String
requestUrl
=
"foo"
;
LocalDateTime
beginTime
=
buildLocalDateTime
(
2021
,
3
,
13
);
int
progressStatus
=
ApiErrorLogProcessStatusEnum
.
INIT
.
getStatus
();
ApiErrorLogDO
infApiErrorLogDO
=
RandomUtils
.
randomPojo
(
ApiErrorLogDO
.
class
,
logDO
->
{
logDO
.
setUserId
(
userId
);
logDO
.
setUserType
(
userType
);
logDO
.
setApplicationName
(
applicationName
);
logDO
.
setRequestUrl
(
requestUrl
);
logDO
.
setExceptionTime
(
beginTime
);
logDO
.
setProcessStatus
(
progressStatus
);
// mock 数据
ApiErrorLogDO
apiErrorLogDO
=
randomPojo
(
ApiErrorLogDO
.
class
,
o
->
{
o
.
setUserId
(
2233L
);
o
.
setUserType
(
UserTypeEnum
.
ADMIN
.
getValue
());
o
.
setApplicationName
(
"yudao-test"
);
o
.
setRequestUrl
(
"foo"
);
o
.
setExceptionTime
(
buildTime
(
2021
,
3
,
13
));
o
.
setProcessStatus
(
ApiErrorLogProcessStatusEnum
.
INIT
.
getStatus
());
});
infApiErrorLogMapper
.
insert
(
infApiErrorLogDO
);
// 下面几个都是不匹配的数据
// userId 不同的
infApiErrorLogMapper
.
insert
(
ObjectUtils
.
cloneIgnoreId
(
infApiErrorLogDO
,
logDO
->
logDO
.
setUserId
(
3344L
)));
// userType
infApiErrorLogMapper
.
insert
(
ObjectUtils
.
cloneIgnoreId
(
infApiErrorLogDO
,
logDO
->
logDO
.
setUserType
(
UserTypeEnum
.
MEMBER
.
getValue
())));
// applicationName 不同的
infApiErrorLogMapper
.
insert
(
ObjectUtils
.
cloneIgnoreId
(
infApiErrorLogDO
,
logDO
->
logDO
.
setApplicationName
(
"test"
)));
// requestUrl 不同的
infApiErrorLogMapper
.
insert
(
ObjectUtils
.
cloneIgnoreId
(
infApiErrorLogDO
,
logDO
->
logDO
.
setRequestUrl
(
"bar"
)));
// 构造一个早期时间 2021-02-06 00:00:00
infApiErrorLogMapper
.
insert
(
ObjectUtils
.
cloneIgnoreId
(
infApiErrorLogDO
,
logDO
->
logDO
.
setExceptionTime
(
buildLocalDateTime
(
2021
,
2
,
6
))));
// progressStatus 不同的
infApiErrorLogMapper
.
insert
(
ObjectUtils
.
cloneIgnoreId
(
infApiErrorLogDO
,
logDO
->
logDO
.
setProcessStatus
(
ApiErrorLogProcessStatusEnum
.
DONE
.
getStatus
())));
// 构造调用参数
apiErrorLogMapper
.
insert
(
apiErrorLogDO
);
// 测试 userId 不匹配
apiErrorLogMapper
.
insert
(
cloneIgnoreId
(
apiErrorLogDO
,
o
->
o
.
setUserId
(
3344L
)));
// 测试 userType 不匹配
apiErrorLogMapper
.
insert
(
cloneIgnoreId
(
apiErrorLogDO
,
logDO
->
logDO
.
setUserType
(
UserTypeEnum
.
MEMBER
.
getValue
())));
// 测试 applicationName 不匹配
apiErrorLogMapper
.
insert
(
cloneIgnoreId
(
apiErrorLogDO
,
logDO
->
logDO
.
setApplicationName
(
"test"
)));
// 测试 requestUrl 不匹配
apiErrorLogMapper
.
insert
(
cloneIgnoreId
(
apiErrorLogDO
,
logDO
->
logDO
.
setRequestUrl
(
"bar"
)));
// 测试 exceptionTime 不匹配:构造一个早期时间 2021-02-06 00:00:00
apiErrorLogMapper
.
insert
(
cloneIgnoreId
(
apiErrorLogDO
,
logDO
->
logDO
.
setExceptionTime
(
buildTime
(
2021
,
2
,
6
))));
// 测试 progressStatus 不匹配
apiErrorLogMapper
.
insert
(
cloneIgnoreId
(
apiErrorLogDO
,
logDO
->
logDO
.
setProcessStatus
(
ApiErrorLogProcessStatusEnum
.
DONE
.
getStatus
())));
// 准备参数
ApiErrorLogPageReqVO
reqVO
=
new
ApiErrorLogPageReqVO
();
reqVO
.
setUserId
(
userId
);
reqVO
.
setUserType
(
userType
);
reqVO
.
setApplicationName
(
applicationName
);
reqVO
.
setRequestUrl
(
requestUrl
);
reqVO
.
setExceptionTime
((
new
LocalDateTime
[]{
buildLocalDateTime
(
2021
,
3
,
12
),
buildLocalDateTime
(
2021
,
3
,
14
)}));
reqVO
.
setProcessStatus
(
progressStatus
);
// 调用service方法
PageResult
<
ApiErrorLogDO
>
pageResult
=
apiErrorLogService
.
getApiErrorLogPage
(
reqVO
);
reqVO
.
setUserId
(
2233L
);
reqVO
.
setUserType
(
UserTypeEnum
.
ADMIN
.
getValue
());
reqVO
.
setApplicationName
(
"yudao-test"
);
reqVO
.
setRequestUrl
(
"foo"
);
reqVO
.
setExceptionTime
(
buildBetweenTime
(
2021
,
3
,
1
,
2021
,
3
,
31
));
reqVO
.
setProcessStatus
(
ApiErrorLogProcessStatusEnum
.
INIT
.
getStatus
());
// 调用
PageResult
<
ApiErrorLogDO
>
pageResult
=
apiErrorLogService
.
getApiErrorLogPage
(
reqVO
);
// 断言,只查到了一条符合条件的
assertEquals
(
1
,
pageResult
.
getTotal
());
assertEquals
(
1
,
pageResult
.
getList
().
size
());
assertPojoEquals
(
infA
piErrorLogDO
,
pageResult
.
getList
().
get
(
0
));
assertPojoEquals
(
a
piErrorLogDO
,
pageResult
.
getList
().
get
(
0
));
}
@Test
public
void
testGetApiErrorLogList
()
{
// 构造测试数据
long
userId
=
2233L
;
int
userType
=
UserTypeEnum
.
ADMIN
.
getValue
();
String
applicationName
=
"yudao-test"
;
String
requestUrl
=
"foo"
;
LocalDateTime
beginTime
=
buildLocalDateTime
(
2021
,
3
,
13
);
int
progressStatus
=
ApiErrorLogProcessStatusEnum
.
INIT
.
getStatus
();
ApiErrorLogDO
infApiErrorLogDO
=
RandomUtils
.
randomPojo
(
ApiErrorLogDO
.
class
,
logDO
->
{
logDO
.
setUserId
(
userId
);
logDO
.
setUserType
(
userType
);
logDO
.
setApplicationName
(
applicationName
);
logDO
.
setRequestUrl
(
requestUrl
);
logDO
.
setExceptionTime
(
beginTime
);
logDO
.
setProcessStatus
(
progressStatus
);
// mock 数据
ApiErrorLogDO
apiErrorLogDO
=
randomPojo
(
ApiErrorLogDO
.
class
,
o
->
{
o
.
setUserId
(
2233L
);
o
.
setUserType
(
UserTypeEnum
.
ADMIN
.
getValue
());
o
.
setApplicationName
(
"yudao-test"
);
o
.
setRequestUrl
(
"foo"
);
o
.
setExceptionTime
(
buildTime
(
2021
,
3
,
13
));
o
.
setProcessStatus
(
ApiErrorLogProcessStatusEnum
.
INIT
.
getStatus
());
});
infApiErrorLogMapper
.
insert
(
infApiErrorLogDO
);
// 下面几个都是不匹配的数据
// userId 不同的
infApiErrorLogMapper
.
insert
(
ObjectUtils
.
cloneIgnoreId
(
infApiErrorLogDO
,
logDO
->
logDO
.
setUserId
(
3344L
)));
// userType
infApiErrorLogMapper
.
insert
(
ObjectUtils
.
cloneIgnoreId
(
infApiErrorLogDO
,
logDO
->
logDO
.
setUserType
(
UserTypeEnum
.
MEMBER
.
getValue
())));
// applicationName 不同的
infApiErrorLogMapper
.
insert
(
ObjectUtils
.
cloneIgnoreId
(
infApiErrorLogDO
,
logDO
->
logDO
.
setApplicationName
(
"test"
)));
// requestUrl 不同的
infApiErrorLogMapper
.
insert
(
ObjectUtils
.
cloneIgnoreId
(
infApiErrorLogDO
,
logDO
->
logDO
.
setRequestUrl
(
"bar"
)));
// 构造一个早期时间 2021-02-06 00:00:00
infApiErrorLogMapper
.
insert
(
ObjectUtils
.
cloneIgnoreId
(
infApiErrorLogDO
,
logDO
->
logDO
.
setExceptionTime
(
buildLocalDateTime
(
2021
,
2
,
6
))));
// progressStatus 不同的
infApiErrorLogMapper
.
insert
(
ObjectUtils
.
cloneIgnoreId
(
infApiErrorLogDO
,
logDO
->
logDO
.
setProcessStatus
(
ApiErrorLogProcessStatusEnum
.
DONE
.
getStatus
())));
// 构造调用参数
apiErrorLogMapper
.
insert
(
apiErrorLogDO
);
// 测试 userId 不匹配
apiErrorLogMapper
.
insert
(
cloneIgnoreId
(
apiErrorLogDO
,
o
->
o
.
setUserId
(
3344L
)));
// 测试 userType 不匹配
apiErrorLogMapper
.
insert
(
cloneIgnoreId
(
apiErrorLogDO
,
logDO
->
logDO
.
setUserType
(
UserTypeEnum
.
MEMBER
.
getValue
())));
// 测试 applicationName 不匹配
apiErrorLogMapper
.
insert
(
cloneIgnoreId
(
apiErrorLogDO
,
logDO
->
logDO
.
setApplicationName
(
"test"
)));
// 测试 requestUrl 不匹配
apiErrorLogMapper
.
insert
(
cloneIgnoreId
(
apiErrorLogDO
,
logDO
->
logDO
.
setRequestUrl
(
"bar"
)));
// 测试 exceptionTime 不匹配:构造一个早期时间 2021-02-06 00:00:00
apiErrorLogMapper
.
insert
(
cloneIgnoreId
(
apiErrorLogDO
,
logDO
->
logDO
.
setExceptionTime
(
buildTime
(
2021
,
2
,
6
))));
// 测试 progressStatus 不匹配
apiErrorLogMapper
.
insert
(
cloneIgnoreId
(
apiErrorLogDO
,
logDO
->
logDO
.
setProcessStatus
(
ApiErrorLogProcessStatusEnum
.
DONE
.
getStatus
())));
// 准备参数
ApiErrorLogExportReqVO
reqVO
=
new
ApiErrorLogExportReqVO
();
reqVO
.
setUserId
(
userId
);
reqVO
.
setUserType
(
userType
);
reqVO
.
setApplicationName
(
applicationName
);
reqVO
.
setRequestUrl
(
requestUrl
);
reqVO
.
setExceptionTime
((
new
LocalDateTime
[]{
buildLocalDateTime
(
2021
,
3
,
12
),
buildLocalDateTime
(
2021
,
3
,
14
)}));
reqVO
.
setProcessStatus
(
progressStatus
);
// 调用service方法
List
<
ApiErrorLogDO
>
list
=
apiErrorLogService
.
getApiErrorLogList
(
reqVO
);
reqVO
.
setUserId
(
2233L
);
reqVO
.
setUserType
(
UserTypeEnum
.
ADMIN
.
getValue
());
reqVO
.
setApplicationName
(
"yudao-test"
);
reqVO
.
setRequestUrl
(
"foo"
);
reqVO
.
setExceptionTime
(
buildBetweenTime
(
2021
,
3
,
1
,
2021
,
3
,
31
));
reqVO
.
setProcessStatus
(
ApiErrorLogProcessStatusEnum
.
INIT
.
getStatus
());
// 调用
List
<
ApiErrorLogDO
>
list
=
apiErrorLogService
.
getApiErrorLogList
(
reqVO
);
// 断言,只查到了一条符合条件的
assertEquals
(
1
,
list
.
size
());
assertPojoEquals
(
infA
piErrorLogDO
,
list
.
get
(
0
));
assertPojoEquals
(
a
piErrorLogDO
,
list
.
get
(
0
));
}
// TODO 芋艿:单元测试,可以拆小一点
@Test
public
void
testUpdateApiErrorLogProcess
()
{
// 先构造两条数据,第一条用于抛出异常,第二条用于正常的执行update操作
Long
processUserId
=
2233L
;
ApiErrorLogDO
first
=
RandomUtils
.
randomPojo
(
ApiErrorLogDO
.
class
,
logDO
->
{
logDO
.
setProcessUserId
(
processUserId
);
logDO
.
setUserType
(
UserTypeEnum
.
ADMIN
.
getValue
());
logDO
.
setProcessStatus
(
ApiErrorLogProcessStatusEnum
.
DONE
.
getStatus
());
});
infApiErrorLogMapper
.
insert
(
first
);
public
void
testCreateApiErrorLog
()
{
// 准备参数
ApiErrorLogCreateReqDTO
createDTO
=
randomPojo
(
ApiErrorLogCreateReqDTO
.
class
);
ApiErrorLogDO
second
=
RandomUtils
.
randomPojo
(
ApiErrorLogDO
.
class
,
logDO
->
{
logDO
.
setProcessUserId
(
1122L
);
logDO
.
setUserType
(
UserTypeEnum
.
ADMIN
.
getValue
());
logDO
.
setProcessStatus
(
ApiErrorLogProcessStatusEnum
.
INIT
.
getStatus
());
});
infApiErrorLogMapper
.
insert
(
second
);
Long
firstId
=
first
.
getId
();
Long
secondId
=
second
.
getId
();
// 执行正常的 update 操作
apiErrorLogService
.
updateApiErrorLogProcess
(
secondId
,
ApiErrorLogProcessStatusEnum
.
DONE
.
getStatus
(),
processUserId
);
ApiErrorLogDO
secondSelect
=
infApiErrorLogMapper
.
selectOne
(
"id"
,
secondId
);
// id 为 0 查询不到,应该抛出异常 API_ERROR_LOG_NOT_FOUND
assertServiceException
(()
->
apiErrorLogService
.
updateApiErrorLogProcess
(
0L
,
ApiErrorLogProcessStatusEnum
.
DONE
.
getStatus
(),
processUserId
),
API_ERROR_LOG_NOT_FOUND
);
// id 为 first 的 progressStatus 为 DONE ,应该抛出 API_ERROR_LOG_PROCESSED
assertServiceException
(()
->
apiErrorLogService
.
updateApiErrorLogProcess
(
firstId
,
ApiErrorLogProcessStatusEnum
.
DONE
.
getStatus
(),
processUserId
),
API_ERROR_LOG_PROCESSED
);
// 验证 progressStatus 是否修改成功
Assertions
.
assertEquals
(
ApiErrorLogProcessStatusEnum
.
DONE
.
getStatus
(),
secondSelect
.
getProcessStatus
());
// 验证 progressUserId 是否修改成功
Assertions
.
assertEquals
(
processUserId
,
secondSelect
.
getProcessUserId
());
// 调用
apiErrorLogService
.
createApiErrorLog
(
createDTO
);
// 断言
ApiErrorLogDO
apiErrorLogDO
=
apiErrorLogMapper
.
selectOne
(
null
);
assertPojoEquals
(
createDTO
,
apiErrorLogDO
);
assertEquals
(
ApiErrorLogProcessStatusEnum
.
INIT
.
getStatus
(),
apiErrorLogDO
.
getProcessStatus
());
}
@Test
public
void
test
CreateApiErrorLogAsync
()
{
public
void
test
UpdateApiErrorLogProcess_success
()
{
// 准备参数
ApiErrorLogCreateReqDTO
createDTO
=
RandomUtils
.
randomPojo
(
ApiErrorLogCreateReqDTO
.
class
,
dto
->
dto
.
setUserType
(
RandomUtil
.
randomEle
(
UserTypeEnum
.
values
()).
getValue
()));
ApiErrorLogDO
apiErrorLogDO
=
randomPojo
(
ApiErrorLogDO
.
class
,
o
->
o
.
setProcessStatus
(
ApiErrorLogProcessStatusEnum
.
INIT
.
getStatus
()));
apiErrorLogMapper
.
insert
(
apiErrorLogDO
);
// 准备参数
Long
id
=
apiErrorLogDO
.
getId
();
Integer
processStatus
=
randomEle
(
ApiErrorLogProcessStatusEnum
.
values
()).
getStatus
();
Long
processUserId
=
randomLongId
();
// 调用
apiErrorLogService
.
createApiErrorLog
(
createDTO
);
apiErrorLogService
.
updateApiErrorLogProcess
(
id
,
processStatus
,
processUserId
);
// 断言
ApiErrorLogDO
infApiErrorLogDO
=
infApiErrorLogMapper
.
selectOne
(
null
);
assertNotNull
(
infApiErrorLogDO
);
assertPojoEquals
(
createDTO
,
infApiErrorLogDO
);
ApiErrorLogDO
dbApiErrorLogDO
=
apiErrorLogMapper
.
selectById
(
apiErrorLogDO
.
getId
());
assertEquals
(
processStatus
,
dbApiErrorLogDO
.
getProcessStatus
());
assertEquals
(
processUserId
,
dbApiErrorLogDO
.
getProcessUserId
());
assertNotNull
(
dbApiErrorLogDO
.
getProcessTime
());
}
@Test
public
void
testUpdateApiErrorLogProcess_processed
()
{
// 准备参数
ApiErrorLogDO
apiErrorLogDO
=
randomPojo
(
ApiErrorLogDO
.
class
,
o
->
o
.
setProcessStatus
(
ApiErrorLogProcessStatusEnum
.
DONE
.
getStatus
()));
apiErrorLogMapper
.
insert
(
apiErrorLogDO
);
// 准备参数
Long
id
=
apiErrorLogDO
.
getId
();
Integer
processStatus
=
randomEle
(
ApiErrorLogProcessStatusEnum
.
values
()).
getStatus
();
Long
processUserId
=
randomLongId
();
// 调用,并断言异常
assertServiceException
(()
->
apiErrorLogService
.
updateApiErrorLogProcess
(
id
,
processStatus
,
processUserId
),
API_ERROR_LOG_PROCESSED
);
}
@Test
public
void
testUpdateApiErrorLogProcess_notFound
()
{
// 准备参数
Long
id
=
randomLongId
();
Integer
processStatus
=
randomEle
(
ApiErrorLogProcessStatusEnum
.
values
()).
getStatus
();
Long
processUserId
=
randomLongId
();
// 调用,并断言异常
assertServiceException
(()
->
apiErrorLogService
.
updateApiErrorLogProcess
(
id
,
processStatus
,
processUserId
),
API_ERROR_LOG_NOT_FOUND
);
}
}
编写
预览
Markdown
格式
0%
重试
或
添加新文件
添加附件
取消
您添加了
0
人
到此讨论。请谨慎行事。
请先完成此评论的编辑!
取消
请
注册
或者
登录
后发表评论