Skip to content
项目
群组
代码片段
帮助
当前项目
正在载入...
登录 / 注册
切换导航面板
kt-keystone
项目
项目
详情
活动
周期分析
仓库
仓库
文件
提交
分支
标签
贡献者
图表
比较
统计图
议题
0
议题
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
CI / CD
CI / CD
流水线
作业
日程
统计图
Wiki
Wiki
代码片段
代码片段
成员
成员
折叠边栏
关闭边栏
活动
图像
聊天
创建新问题
作业
提交
问题看板
Open sidebar
Matrix
kt-keystone
Commits
16bb641f
提交
16bb641f
authored
1月 21, 2022
作者:
黄夏豪
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
feat(base): 补充了httpclientActator的todo
上级
7e61e683
隐藏空白字符变更
内嵌
并排
正在显示
6 个修改的文件
包含
185 行增加
和
33 行删除
+185
-33
CheckPointActuator.java
...a/org/matrix/actuators/checkpoint/CheckPointActuator.java
+8
-26
HttpClientActuator.java
...a/org/matrix/actuators/httpclient/HttpClientActuator.java
+42
-6
CompleteExpressionUtil.java
...ava/org/matrix/actuators/util/CompleteExpressionUtil.java
+60
-0
SpringUtils.java
kt-base/src/main/java/org/matrix/util/SpringUtils.java
+38
-0
HttpClientActuatorTest.java
...java/org/matrix/actuators/sql/HttpClientActuatorTest.java
+37
-0
SqlExpActuatorTest.java
...est/java/org/matrix/actuators/sql/SqlExpActuatorTest.java
+0
-1
没有找到文件。
kt-base/src/main/java/org/matrix/actuators/checkpoint/CheckPointActuator.java
浏览文件 @
16bb641f
...
...
@@ -14,7 +14,10 @@ import org.apache.commons.io.IOUtils;
import
org.apache.commons.lang3.StringUtils
;
import
org.matrix.actuators.Actuator
;
import
org.matrix.actuators.httpclient.HttpResponseDetail
;
import
org.matrix.actuators.sql.SqlExpActuator
;
import
org.matrix.actuators.util.CompleteExpressionUtil
;
import
org.matrix.exception.CheckPointException
;
import
org.matrix.util.SpringUtils
;
import
org.springframework.core.io.ClassPathResource
;
import
org.springframework.http.HttpStatus
;
...
...
@@ -53,6 +56,8 @@ public class CheckPointActuator implements Actuator {
this
.
envId
=
env
;
}
public
CheckPointResult
httpCheck
(
HttpResponseDetail
httpResponseDetail
,
CheckPoint
checkPoint
)
{
CheckPointResult
checkPointResult
=
new
CheckPointResult
();
//根据checkPoint里的细节数据开始检测
...
...
@@ -81,7 +86,8 @@ public class CheckPointActuator implements Actuator {
public
CheckPointResultDetail
jsonPathCheck
(
JsonPathCheckPoint
jsonPathCheckPoint
,
Object
jsonObject
)
{
String
expression
;
try
{
expression
=
completeJsonPathExpression
(
jsonPathCheckPoint
,
jsonObject
);
//todo 李迪凡 先将result中的动态变量都还原出来 (先后顺序不能乱)得先还原动态变量
expression
=
CompleteExpressionUtil
.
completeJsonPathExpression
(
jsonPathCheckPoint
.
getExpression
(),
jsonObject
);
}
catch
(
IllegalArgumentException
e
)
{
return
new
CheckPointResultDetail
(
false
,
...
...
@@ -125,32 +131,8 @@ public class CheckPointActuator implements Actuator {
return
jsEngine
;
}
/**
*
* @param jsonObject 这个Object 是由 Configuration.defaultConfiguration().jsonProvider().parse(“json字符串”); 这个方法转换过来的 请不要乱传
* @return
*/
public
String
completeJsonPathExpression
(
JsonPathCheckPoint
jsonPathCheckPoint
,
Object
jsonObject
)
{
String
result
=
jsonPathCheckPoint
.
getExpression
();
//todo 李迪凡 先将result中的动态变量都还原出来 (先后顺序不能乱)得先还原动态变量
//将jsonPath都还原出来
String
jsonPathRegex
=
"\\{(\\s)*\\$.*?}"
;
result
=
regexExpression
(
result
,
jsonPathRegex
,
jsonObject
,
(
o
,
s
)
->
JSON
.
toJSONString
(
JsonPath
.
read
(
o
,
s
.
substring
(
1
,
s
.
length
()
-
1
))));
return
result
;
}
public
static
String
regexExpression
(
String
expression
,
String
regex
,
Object
sourceContent
,
BiFunction
<
Object
,
String
,
String
>
biFunction
){
String
result
=
expression
;
Pattern
pattern
=
Pattern
.
compile
(
regex
);
Matcher
mat
=
pattern
.
matcher
(
result
);
while
(
mat
.
find
())
{
String
group
=
mat
.
group
();
String
read
=
biFunction
.
apply
(
sourceContent
,
group
);
result
=
mat
.
replaceAll
(
read
);
}
return
result
;
}
...
...
kt-base/src/main/java/org/matrix/actuators/httpclient/HttpClientActuator.java
浏览文件 @
16bb641f
...
...
@@ -3,6 +3,7 @@ package org.matrix.actuators.httpclient;
import
io.netty.handler.codec.http.HttpHeaderValues
;
import
org.apache.commons.lang3.StringUtils
;
import
org.matrix.actuators.Actuator
;
import
org.matrix.actuators.util.CompleteExpressionUtil
;
import
org.matrix.config.HttpRequestConfig
;
import
org.apache.http.Consts
;
import
org.apache.http.HttpEntity
;
...
...
@@ -48,9 +49,9 @@ public class HttpClientActuator implements Actuator {
CloseableHttpClient
client
;
private
Long
projectId
;
private
final
Long
projectId
;
private
Long
env
;
private
final
Long
envId
;
public
HttpClientActuator
(
HttpRequestConfig
config
,
Long
env
,
Long
projectId
)
{
...
...
@@ -62,31 +63,66 @@ public class HttpClientActuator implements Actuator {
e
.
printStackTrace
();
}
this
.
projectId
=
projectId
;
this
.
env
=
env
;
this
.
env
Id
=
env
;
}
public
CloseableHttpClient
getClient
()
{
return
client
;
}
/**
* 初始化请求内数据并补全动态变量
*/
private
void
completeHttpRequestDetail
(
HttpRequestDetail
httpRequestDetail
){
for
(
RequestHeader
header
:
httpRequestDetail
.
getHeaders
())
{
//获取动态变量,并将动态变量替换进去
//todo 李迪凡 将header中的key和value里的 动态变量补全
//获取动态变量,并将动态变量替换进去
header
.
setName
(
CompleteExpressionUtil
.
completeDynamicVariable
(
header
.
getName
(),
envId
,
projectId
)
);
header
.
setValue
(
CompleteExpressionUtil
.
completeDynamicVariable
(
header
.
getValue
(),
envId
,
projectId
)
);
}
for
(
RequestBody
requestBody
:
httpRequestDetail
.
getRequestBodies
())
{
//todo 李迪凡 将requestBody中的key和value里的 动态变量补全
requestBody
.
setKey
(
CompleteExpressionUtil
.
completeDynamicVariable
(
requestBody
.
getKey
(),
envId
,
projectId
)
);
requestBody
.
setValue
(
CompleteExpressionUtil
.
completeDynamicVariable
(
requestBody
.
getValue
(),
envId
,
projectId
)
);
}
if
(
StringUtils
.
isEmpty
(
httpRequestDetail
.
getStringValue
())){
//todo 李迪凡 将httpRequestDetail的stringValue 动态变量补全
httpRequestDetail
.
setStringValue
(
CompleteExpressionUtil
.
completeDynamicVariable
(
httpRequestDetail
.
getStringValue
(),
envId
,
projectId
)
);
}
//todo 李迪凡 将httpRequestDetail的url 动态变量补全
//todo 李迪凡 将httpRequestDetail的url 动态变量补全
httpRequestDetail
.
setUrl
(
CompleteExpressionUtil
.
completeDynamicVariable
(
httpRequestDetail
.
getUrl
(),
envId
,
projectId
));
}
/**
* 发起Http请求
*
...
...
kt-base/src/main/java/org/matrix/actuators/util/CompleteExpressionUtil.java
0 → 100644
浏览文件 @
16bb641f
package
org
.
matrix
.
actuators
.
util
;
import
com.alibaba.fastjson.JSON
;
import
com.jayway.jsonpath.JsonPath
;
import
org.matrix.actuators.checkpoint.JsonPathCheckPoint
;
import
org.matrix.actuators.sql.SqlExpActuator
;
import
org.matrix.util.SpringUtils
;
import
java.util.function.BiFunction
;
import
java.util.regex.Matcher
;
import
java.util.regex.Pattern
;
/**
* 补全http和检验点中的表达式的一些公共方法
* @author huangxiahao
*/
public
class
CompleteExpressionUtil
{
public
static
final
String
JSON_PATH_REG
=
"\\{(\\s)*\\$.*?}"
;
public
static
final
String
ACTION_REG
=
"\\$(?=\\{(\\s)*pre\\.)(.*?)(?<=})(\\[(.*?)])?"
;
public
static
final
String
ENV_VARIABLE_REG
=
"\\#\\{(.*?)}"
;
public
static
final
String
DYNAMIC_VARIABLE_REG
=
"\\$\\{(((?!pre\\.).)*?)}(\\[(.*?)])?"
;
public
static
String
completeDynamicVariable
(
String
expression
,
Long
envId
,
Long
projectId
)
{
String
result
=
expression
;
result
=
regexExpression
(
result
,
DYNAMIC_VARIABLE_REG
,
null
,
(
o
,
s
)
->
getSqlExpActuator
().
parseVarByName
(
s
,
envId
,
projectId
));
return
result
;
}
/**
*
* @param jsonObject 这个Object 是由 Configuration.defaultConfiguration().jsonProvider().parse(“json字符串”); 这个方法转换过来的 请不要乱传
* @return
*/
public
static
String
completeJsonPathExpression
(
String
expression
,
Object
jsonObject
)
{
String
result
=
expression
;
result
=
regexExpression
(
result
,
JSON_PATH_REG
,
jsonObject
,
(
o
,
s
)
->
JSON
.
toJSONString
(
JsonPath
.
read
(
o
,
s
.
substring
(
1
,
s
.
length
()
-
1
))));
return
result
;
}
public
static
String
regexExpression
(
String
expression
,
String
regex
,
Object
sourceContent
,
BiFunction
<
Object
,
String
,
String
>
biFunction
){
String
result
=
expression
;
Pattern
pattern
=
Pattern
.
compile
(
regex
);
Matcher
mat
=
pattern
.
matcher
(
result
);
while
(
mat
.
find
())
{
String
group
=
mat
.
group
();
String
read
=
biFunction
.
apply
(
sourceContent
,
group
);
result
=
mat
.
replaceAll
(
read
);
}
return
result
;
}
private
static
SqlExpActuator
getSqlExpActuator
(){
return
SpringUtils
.
getBean
(
"sqlExpActuator"
);
}
}
kt-base/src/main/java/org/matrix/util/SpringUtils.java
0 → 100644
浏览文件 @
16bb641f
package
org
.
matrix
.
util
;
import
com.mongodb.lang.NonNullApi
;
import
org.springframework.beans.BeansException
;
import
org.springframework.context.ApplicationContext
;
import
org.springframework.context.ApplicationContextAware
;
import
org.springframework.stereotype.Component
;
import
java.util.Map
;
/**
* 用来获取spring的bean 的工具
*
* @author huangxiahao
*/
@Component
public
class
SpringUtils
implements
ApplicationContextAware
{
private
static
ApplicationContext
applicationContext
;
@Override
public
void
setApplicationContext
(
ApplicationContext
applicationContext
)
throws
BeansException
{
SpringUtils
.
applicationContext
=
applicationContext
;
}
public
static
<
T
>
T
getBean
(
String
beanName
)
{
if
(
applicationContext
.
containsBean
(
beanName
))
{
return
(
T
)
applicationContext
.
getBean
(
beanName
);
}
else
{
return
null
;
}
}
public
static
<
T
>
Map
<
String
,
T
>
getBeansOfType
(
Class
<
T
>
baseType
)
{
return
applicationContext
.
getBeansOfType
(
baseType
);
}
}
kt-base/src/test/java/org/matrix/actuators/sql/HttpClientActuatorTest.java
0 → 100644
浏览文件 @
16bb641f
package
org
.
matrix
.
actuators
.
sql
;
import
com.alibaba.fastjson.JSON
;
import
org.junit.runner.RunWith
;
import
org.matrix.actuators.httpclient.HttpClientActuator
;
import
org.matrix.actuators.httpclient.HttpRequestDetail
;
import
org.matrix.config.HttpRequestConfig
;
import
org.springframework.boot.test.context.SpringBootTest
;
import
org.springframework.test.context.junit4.SpringRunner
;
@RunWith
(
SpringRunner
.
class
)
@SpringBootTest
public
class
HttpClientActuatorTest
{
public
void
test
(){
HttpClientActuator
httpClientActuator
=
new
HttpClientActuator
(
new
HttpRequestConfig
(),
0
l
,
0
l
);
String
json
=
"{\n"
+
" \"url\":\"http://127.0.0.1:13245/test/sayHelloJson\",\n"
+
" \"method\":\"POST\",\n"
+
" \"requestType\":\"JSON\",\n"
+
" \"stringValue\":\"{\\\"name\\\":\\\"李四\\\"}\",\n"
+
" \"headers\":[\n"
+
" {\n"
+
" \"name\":\"cookie\",\n"
+
" \"value\":\"123456\"\n"
+
" }\n"
+
" ]\n"
+
"}"
;
HttpRequestDetail
httpRequestDetail
=
JSON
.
parseObject
(
json
,
HttpRequestDetail
.
class
);
httpClientActuator
.
sendHttpRequest
(
httpRequestDetail
);
}
}
kt-base/src/test/java/org/matrix/actuators/sql/SqlExpActuatorTest.java
浏览文件 @
16bb641f
...
...
@@ -35,7 +35,6 @@ class SqlExpActuatorTest {
@Test
void
parseVarByName
()
{
String
reuslt
=
sqlExpActuator
.
parseVarByName
(
"${componentName}[2]"
,
1L
,
1L
);
System
.
out
.
println
(
reuslt
);
...
...
编写
预览
Markdown
格式
0%
重试
或
添加新文件
添加附件
取消
您添加了
0
人
到此讨论。请谨慎行事。
请先完成此评论的编辑!
取消
请
注册
或者
登录
后发表评论