Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
KnowledgePlanet
road-map
xfg-dev-tech-mock
提交
142674a2
xfg-dev-tech-mock
项目概览
KnowledgePlanet
/
road-map
/
xfg-dev-tech-mock
通知
79
Star
12
Fork
5
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
0
列表
看板
标记
里程碑
合并请求
0
DevOps
流水线
流水线任务
计划
Wiki
0
Wiki
分析
仓库
DevOps
项目成员
Pages
xfg-dev-tech-mock
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
0
Issue
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
Pages
DevOps
DevOps
流水线
流水线任务
计划
分析
分析
仓库分析
DevOps
Wiki
0
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
流水线任务
提交
Issue看板
提交
142674a2
编写于
10月 22, 2023
作者:
小傅哥
⛹
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
feat: mock 单元测试
上级
038ffc5f
变更
8
隐藏空白更改
内联
并排
Showing
8 changed file
with
164 addition
and
13 deletion
+164
-13
xfg-dev-tech-app/src/main/resources/application-dev.yml
xfg-dev-tech-app/src/main/resources/application-dev.yml
+3
-3
xfg-dev-tech-app/src/test/java/cn/bugstack/xfg/dev/tech/infrastructure/gateway/adapter/ZSXQAdapterTest.java
.../tech/infrastructure/gateway/adapter/ZSXQAdapterTest.java
+88
-0
xfg-dev-tech-app/src/test/java/cn/bugstack/xfg/dev/tech/test/ApiTest.java
.../src/test/java/cn/bugstack/xfg/dev/tech/test/ApiTest.java
+0
-2
xfg-dev-tech-app/src/test/java/cn/bugstack/xfg/dev/tech/test/MockTest.java
...src/test/java/cn/bugstack/xfg/dev/tech/test/MockTest.java
+61
-0
xfg-dev-tech-domain/src/main/java/cn/bugstack/xfg/dev/tech/domain/zsxq/adapter/IZSXQAdapter.java
...gstack/xfg/dev/tech/domain/zsxq/adapter/IZSXQAdapter.java
+1
-1
xfg-dev-tech-domain/src/main/java/cn/bugstack/xfg/dev/tech/domain/zsxq/service/AiReply.java
...cn/bugstack/xfg/dev/tech/domain/zsxq/service/AiReply.java
+8
-6
xfg-dev-tech-infrastructure/src/main/java/cn/bugstack/xfg/dev/tech/infrastructure/gateway/adapter/ZSXQAdapter.java
.../dev/tech/infrastructure/gateway/adapter/ZSXQAdapter.java
+2
-1
xfg-dev-tech-trigger/src/main/java/cn/bugstack/xfg/dev/tech/job/ReplyJob.java
.../src/main/java/cn/bugstack/xfg/dev/tech/job/ReplyJob.java
+1
-0
未找到文件。
xfg-dev-tech-app/src/main/resources/application-dev.yml
浏览文件 @
142674a2
...
@@ -8,18 +8,18 @@ chatglm:
...
@@ -8,18 +8,18 @@ chatglm:
sdk
:
sdk
:
config
:
config
:
# 状态;true = 开启、false 关闭
# 状态;true = 开启、false 关闭
enabled
:
tru
e
enabled
:
fals
e
# 官网地址
# 官网地址
api-host
:
https://open.bigmodel.cn/
api-host
:
https://open.bigmodel.cn/
# 官网申请 https://open.bigmodel.cn/usercenter/apikeys
# 官网申请 https://open.bigmodel.cn/usercenter/apikeys
api-secret-key
:
d570f7c5d289cdac2abdfdc562
e39f3f.trqz1dH8ZK6ED7Pg
api-secret-key
:
d570f7c5d289cdac2abdfdc562
****.trqz1dH8*****
# 知识星球配置信息;id -> 星球ID、user-id -> 用户ID、cookie -> 登录cookie
# 知识星球配置信息;id -> 星球ID、user-id -> 用户ID、cookie -> 登录cookie
zsxq
:
zsxq
:
config
:
config
:
id
:
28885518425541
id
:
28885518425541
user-id
:
241858242255511
user-id
:
241858242255511
cookie
:
zsxq_access_token=E0538FF2-B440-69E9-57D1-59E
A37B9C0C6_9D76421394C6F474
cookie
:
zsxq_access_token=E0538FF2-B440-69E9-57D1-59E
******
# 日志
# 日志
logging
:
logging
:
...
...
xfg-dev-tech-app/src/test/java/cn/bugstack/xfg/dev/tech/infrastructure/gateway/adapter/ZSXQAdapterTest.java
0 → 100644
浏览文件 @
142674a2
package
cn.bugstack.xfg.dev.tech.infrastructure.gateway.adapter
;
import
cn.bugstack.xfg.dev.tech.domain.zsxq.model.vo.TopicsItemVO
;
import
cn.bugstack.xfg.dev.tech.infrastructure.gateway.api.IZSXQApi
;
import
cn.bugstack.xfg.dev.tech.infrastructure.gateway.dto.*
;
import
com.alibaba.fastjson.JSON
;
import
lombok.extern.slf4j.Slf4j
;
import
org.junit.Test
;
import
org.junit.runner.RunWith
;
import
org.mockito.InjectMocks
;
import
org.mockito.Mock
;
import
org.mockito.junit.MockitoJUnitRunner
;
import
java.io.IOException
;
import
java.util.Arrays
;
import
java.util.List
;
import
static
org
.
junit
.
Assert
.*;
import
static
org
.
mockito
.
Mockito
.
verify
;
import
static
org
.
mockito
.
Mockito
.
when
;
@Slf4j
@RunWith
(
MockitoJUnitRunner
.
class
)
public
class
ZSXQAdapterTest
{
@Mock
private
IZSXQApi
mockZsxqApi
;
@InjectMocks
private
ZSXQAdapter
zsxqAdapterUnderTest
;
@Test
public
void
testQueryTopics
()
throws
Exception
{
// Setup
final
List
<
TopicsItemVO
>
expectedResult
=
Arrays
.
asList
(
TopicsItemVO
.
builder
()
.
topicId
(
0L
)
.
talk
(
"talk"
)
.
showCommentsItems
(
Arrays
.
asList
(
TopicsItemVO
.
ShowCommentsItem
.
builder
()
.
userId
(
0L
)
.
build
()))
.
build
());
// Configure IZSXQApi.topics(...).
final
ResponseDTO
responseDTO
=
new
ResponseDTO
();
final
RespData
respData
=
new
RespData
();
final
TopicsItem
topicsItem
=
new
TopicsItem
();
final
ShowCommentsItem
showCommentsItem
=
new
ShowCommentsItem
();
final
Owner
owner
=
new
Owner
();
owner
.
setUserId
(
0L
);
showCommentsItem
.
setOwner
(
owner
);
topicsItem
.
setShowComments
(
Arrays
.
asList
(
showCommentsItem
));
final
Talk
talk
=
new
Talk
();
talk
.
setText
(
"talk"
);
topicsItem
.
setTalk
(
talk
);
topicsItem
.
setTopicId
(
0L
);
respData
.
setTopics
(
Arrays
.
asList
(
topicsItem
));
responseDTO
.
setRespData
(
respData
);
when
(
mockZsxqApi
.
topics
()).
thenReturn
(
responseDTO
);
// Run the test
final
List
<
TopicsItemVO
>
result
=
zsxqAdapterUnderTest
.
queryTopics
();
// Verify the results
assertEquals
(
expectedResult
,
result
);
log
.
info
(
"测试结果:{}"
,
JSON
.
toJSONString
(
result
));
}
@Test
public
void
testQueryTopics_IZSXQApiThrowsIOException
()
throws
Exception
{
// Setup
when
(
mockZsxqApi
.
topics
()).
thenThrow
(
IOException
.
class
);
// Run the test
assertThrows
(
RuntimeException
.
class
,
()
->
zsxqAdapterUnderTest
.
queryTopics
());
}
@Test
public
void
testComment
()
{
// Setup
// Run the test
final
boolean
result
=
zsxqAdapterUnderTest
.
comment
(
0L
,
"content"
);
// Verify the results
assertTrue
(
result
);
verify
(
mockZsxqApi
).
comment
(
0L
,
"content"
);
}
}
xfg-dev-tech-app/src/test/java/cn/bugstack/xfg/dev/tech/test/ApiTest.java
浏览文件 @
142674a2
...
@@ -25,8 +25,6 @@ public class ApiTest {
...
@@ -25,8 +25,6 @@ public class ApiTest {
@Test
@Test
public
void
test_IAiReply
()
{
public
void
test_IAiReply
()
{
aiReply
.
doAiReply
();
aiReply
.
doAiReply
();
}
}
}
}
xfg-dev-tech-app/src/test/java/cn/bugstack/xfg/dev/tech/test/MockTest.java
0 → 100644
浏览文件 @
142674a2
package
cn.bugstack.xfg.dev.tech.test
;
import
cn.bugstack.chatglm.model.ChatCompletionRequest
;
import
cn.bugstack.chatglm.session.OpenAiSession
;
import
cn.bugstack.xfg.dev.tech.domain.zsxq.adapter.IZSXQAdapter
;
import
cn.bugstack.xfg.dev.tech.domain.zsxq.model.vo.TopicsItemVO
;
import
cn.bugstack.xfg.dev.tech.domain.zsxq.service.IAiReply
;
import
com.fasterxml.jackson.core.JsonProcessingException
;
import
lombok.extern.slf4j.Slf4j
;
import
okhttp3.Request
;
import
okhttp3.sse.EventSource
;
import
okhttp3.sse.EventSourceListener
;
import
org.jetbrains.annotations.NotNull
;
import
org.jetbrains.annotations.Nullable
;
import
org.junit.Test
;
import
org.junit.runner.RunWith
;
import
org.mockito.Mockito
;
import
org.mockito.junit.MockitoJUnitRunner
;
import
org.springframework.boot.test.context.SpringBootTest
;
import
org.springframework.boot.test.mock.mockito.MockBean
;
import
org.springframework.test.context.junit4.SpringRunner
;
import
javax.annotation.Resource
;
import
java.util.ArrayList
;
import
java.util.List
;
import
java.util.concurrent.CountDownLatch
;
/**
* @author Fuzhengwei bugstack.cn @小傅哥
* @description Mock 测试
* @create 2023-10-22 11:20
*/
@Slf4j
@RunWith
(
SpringRunner
.
class
)
@SpringBootTest
public
class
MockTest
{
@Resource
private
IAiReply
aiReply
;
@MockBean
private
IZSXQAdapter
izsxqAdapter
;
@Test
public
void
test_doAiReply_
()
throws
InterruptedException
,
JsonProcessingException
{
Mockito
.
when
(
izsxqAdapter
.
queryTopics
()).
thenReturn
(
new
ArrayList
<
TopicsItemVO
>()
{{
TopicsItemVO
topicsItemVO
=
new
TopicsItemVO
();
topicsItemVO
.
setTopicId
(
10001L
);
topicsItemVO
.
setTalk
(
"<e type=\"mention\" uid=\"241858242255511\" title=\"%40%E5%B0%8F%E5%82%85%E5%93%A5\" /> 提问 java 冒泡排序"
);
add
(
topicsItemVO
);
}});
Mockito
.
when
(
izsxqAdapter
.
comment
(
Mockito
.
anyLong
(),
Mockito
.
anyString
())).
thenReturn
(
true
);
aiReply
.
doAiReply
();
// 等待;ChatGLM 异步回复
new
CountDownLatch
(
1
).
await
();
}
}
xfg-dev-tech-domain/src/main/java/cn/bugstack/xfg/dev/tech/domain/zsxq/adapter/IZSXQAdapter.java
浏览文件 @
142674a2
...
@@ -13,6 +13,6 @@ public interface IZSXQAdapter {
...
@@ -13,6 +13,6 @@ public interface IZSXQAdapter {
List
<
TopicsItemVO
>
queryTopics
();
List
<
TopicsItemVO
>
queryTopics
();
void
comment
(
long
topicId
,
String
content
);
boolean
comment
(
long
topicId
,
String
content
);
}
}
xfg-dev-tech-domain/src/main/java/cn/bugstack/xfg/dev/tech/domain/zsxq/service/AiReply.java
浏览文件 @
142674a2
...
@@ -38,7 +38,6 @@ public class AiReply implements IAiReply {
...
@@ -38,7 +38,6 @@ public class AiReply implements IAiReply {
@Value
(
"${zsxq.config.user-id}"
)
@Value
(
"${zsxq.config.user-id}"
)
private
Long
userId
;
private
Long
userId
;
private
final
String
regex
=
"<e type=\"mention\" uid=\"(\\d+)\" title=\"(.*?)\" /> (.*)"
;
private
volatile
Set
<
Long
>
topicIds
=
new
HashSet
<>();
private
volatile
Set
<
Long
>
topicIds
=
new
HashSet
<>();
@Override
@Override
...
@@ -53,6 +52,7 @@ public class AiReply implements IAiReply {
...
@@ -53,6 +52,7 @@ public class AiReply implements IAiReply {
String
text
=
topicsItem
.
getTalk
();
String
text
=
topicsItem
.
getTalk
();
// "<e type="mention" uid="241858242255511" title="%40%E5%B0%8F%E5%82%85%E5%93%A5" /> 提问 java 冒泡排序"
// "<e type="mention" uid="241858242255511" title="%40%E5%B0%8F%E5%82%85%E5%93%A5" /> 提问 java 冒泡排序"
String
regex
=
"<e type=\"mention\" uid=\"(\\d+)\" title=\"(.*?)\" /> (.*)"
;
Pattern
pattern
=
Pattern
.
compile
(
regex
);
Pattern
pattern
=
Pattern
.
compile
(
regex
);
Matcher
matcher
=
pattern
.
matcher
(
text
);
Matcher
matcher
=
pattern
.
matcher
(
text
);
...
@@ -65,7 +65,8 @@ public class AiReply implements IAiReply {
...
@@ -65,7 +65,8 @@ public class AiReply implements IAiReply {
if
(
null
==
openAiSession
)
{
if
(
null
==
openAiSession
)
{
log
.
info
(
"你没有开启 ChatGLM 参考yml配置文件来开启"
);
log
.
info
(
"你没有开启 ChatGLM 参考yml配置文件来开启"
);
// 你可以使用 ChatGLM SDK 进行回答,回复问题;
// 你可以使用 ChatGLM SDK 进行回答,回复问题;
zsxqAdapter
.
comment
(
topicId
,
"【测试,只回答圈我的帖子】对接 ChatGLM SDK https://bugstack.cn/md/project/chatgpt/sdk/chatglm-sdk-java.html 回答:"
+
remainingText
);
boolean
status
=
zsxqAdapter
.
comment
(
topicId
,
"【测试,只回答圈我的帖子】对接 ChatGLM SDK https://bugstack.cn/md/project/chatgpt/sdk/chatglm-sdk-java.html 回答:"
+
remainingText
);
log
.
info
(
"回贴完成:{}"
,
status
);
}
else
{
}
else
{
log
.
info
(
"ChatGLM 进入回答 {} {}"
,
topicId
,
remainingText
);
log
.
info
(
"ChatGLM 进入回答 {} {}"
,
topicId
,
remainingText
);
if
(
topicIds
.
contains
(
topicId
))
{
if
(
topicIds
.
contains
(
topicId
))
{
...
@@ -94,12 +95,12 @@ public class AiReply implements IAiReply {
...
@@ -94,12 +95,12 @@ public class AiReply implements IAiReply {
openAiSession
.
completions
(
request
,
new
EventSourceListener
()
{
openAiSession
.
completions
(
request
,
new
EventSourceListener
()
{
@Override
@Override
public
void
onEvent
(
EventSource
eventSource
,
@Nullable
String
id
,
@Nullable
String
type
,
String
data
)
{
public
void
onEvent
(
EventSource
eventSource
,
@Nullable
String
id
,
@Nullable
String
type
,
String
data
)
{
ChatCompletionResponse
chatCompletionResponse
=
com
.
alibaba
.
fastjson
.
JSON
.
parseObject
(
data
,
ChatCompletionResponse
.
class
);
ChatCompletionResponse
chatCompletionResponse
=
JSON
.
parseObject
(
data
,
ChatCompletionResponse
.
class
);
log
.
info
(
"测试结果 onEvent:{}"
,
chatCompletionResponse
.
getData
());
log
.
info
(
"测试结果 onEvent:{}"
,
chatCompletionResponse
.
getData
());
// type 消息类型,add 增量,finish 结束,error 错误,interrupted 中断
// type 消息类型,add 增量,finish 结束,error 错误,interrupted 中断
if
(
EventType
.
finish
.
getCode
().
equals
(
type
))
{
if
(
EventType
.
finish
.
getCode
().
equals
(
type
))
{
ChatCompletionResponse
.
Meta
meta
=
com
.
alibaba
.
fastjson
.
JSON
.
parseObject
(
chatCompletionResponse
.
getMeta
(),
ChatCompletionResponse
.
Meta
.
class
);
ChatCompletionResponse
.
Meta
meta
=
JSON
.
parseObject
(
chatCompletionResponse
.
getMeta
(),
ChatCompletionResponse
.
Meta
.
class
);
log
.
info
(
"[输出结束] Tokens {}"
,
com
.
alibaba
.
fastjson
.
JSON
.
toJSONString
(
meta
));
log
.
info
(
"[输出结束] Tokens {}"
,
JSON
.
toJSONString
(
meta
));
}
}
content
.
append
(
chatCompletionResponse
.
getData
());
content
.
append
(
chatCompletionResponse
.
getData
());
}
}
...
@@ -121,7 +122,8 @@ public class AiReply implements IAiReply {
...
@@ -121,7 +122,8 @@ public class AiReply implements IAiReply {
}
}
String
subContent
=
contents
.
substring
(
startIndex
,
endIndex
);
String
subContent
=
contents
.
substring
(
startIndex
,
endIndex
);
zsxqAdapter
.
comment
(
topicId
,
subContent
);
boolean
status
=
zsxqAdapter
.
comment
(
topicId
,
subContent
);
log
.
info
(
"回贴完成:{}"
,
status
);
startIndex
=
endIndex
;
startIndex
=
endIndex
;
endIndex
+=
maxLength
;
endIndex
+=
maxLength
;
...
...
xfg-dev-tech-infrastructure/src/main/java/cn/bugstack/xfg/dev/tech/infrastructure/gateway/adapter/ZSXQAdapter.java
浏览文件 @
142674a2
...
@@ -58,8 +58,9 @@ public class ZSXQAdapter implements IZSXQAdapter {
...
@@ -58,8 +58,9 @@ public class ZSXQAdapter implements IZSXQAdapter {
}
}
@Override
@Override
public
void
comment
(
long
topicId
,
String
content
)
{
public
boolean
comment
(
long
topicId
,
String
content
)
{
zsxqApi
.
comment
(
topicId
,
content
);
zsxqApi
.
comment
(
topicId
,
content
);
return
true
;
}
}
}
}
xfg-dev-tech-trigger/src/main/java/cn/bugstack/xfg/dev/tech/job/ReplyJob.java
浏览文件 @
142674a2
...
@@ -21,6 +21,7 @@ public class ReplyJob {
...
@@ -21,6 +21,7 @@ public class ReplyJob {
@Scheduled
(
cron
=
"0/10 * * * * ?"
)
@Scheduled
(
cron
=
"0/10 * * * * ?"
)
public
void
exec
()
throws
Exception
{
public
void
exec
()
throws
Exception
{
log
.
info
(
"自动回帖任务开始执行..."
);
aiReply
.
doAiReply
();
aiReply
.
doAiReply
();
}
}
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录