Skip to content
GitLab
Projects
Groups
Snippets
Help
Loading...
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
G
GatewayApiTestUtil
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Issues
0
Issues
0
List
Boards
Labels
Service Desk
Milestones
Merge Requests
1
Merge Requests
1
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Operations
Operations
Incidents
Environments
Packages & Registries
Packages & Registries
Package Registry
Analytics
Analytics
CI / CD
Repository
Value Stream
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
一兵 刘
GatewayApiTestUtil
Commits
33c87819
Commit
33c87819
authored
Oct 30, 2023
by
fa_pms01
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
支持WebService请求
parent
43cde115
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
94 additions
and
29 deletions
+94
-29
src/main/java/WindowStart.java
src/main/java/WindowStart.java
+58
-16
src/main/java/utils/ThirdApiCaller.java
src/main/java/utils/ThirdApiCaller.java
+36
-13
No files found.
src/main/java/WindowStart.java
View file @
33c87819
...
...
@@ -9,6 +9,8 @@ import utils.HandleFileUtils;
import
utils.ThirdApiCaller
;
import
javax.swing.*
;
import
java.awt.event.WindowAdapter
;
import
java.awt.event.WindowEvent
;
import
java.util.List
;
import
java.util.Map
;
import
java.util.*
;
...
...
@@ -28,6 +30,7 @@ public class WindowStart extends JFrame {
private
JTextArea
outTextArea
;
private
JComboBox
<
Model
>
modelBox
;
private
JComboBox
<
Model
>
coreNumBox
;
private
JComboBox
<
Model
>
typeBox
;
private
List
<
Param
>
paramList
=
new
ArrayList
<>();
public
static
final
String
SPACE
=
"\n\r"
;
...
...
@@ -51,7 +54,13 @@ public class WindowStart extends JFrame {
//循环 根据名称读取对应的文件
if
(
CollectionUtils
.
isNotEmpty
(
fileFullNames
))
{
StringBuilder
sb
=
new
StringBuilder
(
"开始解析文本请求内容:"
).
append
(
SPACE
);
List
<
Param
>
paramListTemp
=
buildTxtData
(
fileFullNames
,
sb
);
Model
typeModel
=
(
Model
)
typeBox
.
getSelectedItem
();
List
<
Param
>
paramListTemp
=
new
ArrayList
<>();
if
(
typeModel
.
getModelCode
()
==
0
){
// Restfull
paramListTemp
=
buildTxtData4RestFull
(
fileFullNames
,
sb
);
}
else
if
(
typeModel
.
getModelCode
()
==
1
)
{
//WebService
paramListTemp
=
buildTxtData4WebService
(
fileFullNames
,
sb
);
}
if
(
CollectionUtils
.
isNotEmpty
(
paramListTemp
)){
paramList
=
paramListTemp
;
inTextArea
.
setText
(
inTextArea
.
getText
()
+
SPACE
+
sb
.
toString
());
...
...
@@ -59,17 +68,10 @@ public class WindowStart extends JFrame {
}
});
fileTextFile
=
new
JTextField
(
"C:\\Users\\GetWind\\Desktop\\TestCaseFileRootPath"
,
50
);
// fileTextFile.addActionListener(new InputListener());
frame
.
add
(
fileTextFile
);
frame
.
add
(
fileInput
);
urlTextFile
=
new
JTextField
(
"http://localhost:9080/gateway/httpServer"
,
50
);
// urlTextFile.addActionListener(new InputListener());
frame
.
add
(
urlTextFile
);
urlTextFile
=
new
JTextField
(
"http://localhost:9080/gateway/httpServer"
,
50
);
urlTextFile
=
new
JTextField
(
"http://localhost:9080/gateway/Service/JobManagementWebService?wsdl"
,
50
);
inTextArea
=
new
JTextArea
(
10
,
30
);
// 设置行数和列数
inTextArea
.
setLineWrap
(
true
);
// 设置自动换行
inTextArea
.
setWrapStyleWord
(
true
);
// 设置按单词换行
// inTextArea.set
JScrollPane
inScrollPane
=
new
JScrollPane
(
inTextArea
);
// 添加滚动条
outTextArea
=
new
JTextArea
(
10
,
30
);
// 设置行数和列数
outTextArea
.
setLineWrap
(
true
);
// 设置自动换行
...
...
@@ -81,7 +83,8 @@ public class WindowStart extends JFrame {
Model
model
=
(
Model
)
modelBox
.
getSelectedItem
();
Model
coreNum
=
(
Model
)
coreNumBox
.
getSelectedItem
();
outTextArea
.
setText
(
"开始执行: 运行模式[ "
+
model
.
getModel
()
+
" ],线程数[ "
+
coreNum
.
getModel
()
+
" ]"
+
SPACE
);
callTest
(
paramList
,
url
,
model
.
getModelCode
(),
coreNum
);
Model
typeModel
=
(
Model
)
typeBox
.
getSelectedItem
();
callTest
(
paramList
,
url
,
model
.
getModelCode
(),
coreNum
,
typeModel
.
getModelCode
());
});
modelBox
=
new
JComboBox
<>();
modelBox
.
addItem
(
new
Model
(
"并行"
,
1
));
...
...
@@ -94,6 +97,9 @@ public class WindowStart extends JFrame {
frame
.
add
(
coreNumBox
);
}
});
typeBox
=
new
JComboBox
<>();
typeBox
.
addItem
(
new
Model
(
"WebService"
,
1
));
typeBox
.
addItem
(
new
Model
(
"RestFull"
,
0
));
coreNumBox
=
new
JComboBox
<>();
coreNumBox
.
addItem
(
new
Model
(
"10"
,
10
));
coreNumBox
.
addItem
(
new
Model
(
"9"
,
9
));
...
...
@@ -109,6 +115,7 @@ public class WindowStart extends JFrame {
fileTextFile
.
setBounds
(
50
,
50
,
450
,
30
);
fileInput
.
setBounds
(
860
,
225
,
100
,
30
);
//设置组件大小和位置
urlTextFile
.
setBounds
(
50
,
100
,
450
,
30
);
//设置组件大小和位置
typeBox
.
setBounds
(
860
,
100
,
100
,
30
);
inScrollPane
.
setBounds
(
50
,
150
,
800
,
200
);
outScrollPane
.
setBounds
(
50
,
350
,
800
,
300
);
startButton
.
setBounds
(
860
,
475
,
100
,
30
);
...
...
@@ -123,14 +130,21 @@ public class WindowStart extends JFrame {
frame
.
add
(
startButton
);
frame
.
add
(
modelBox
);
frame
.
add
(
coreNumBox
);
frame
.
add
(
typeBox
);
frame
.
setVisible
(
true
);
//显示
frame
.
setResizable
(
false
);
//固定大小
frame
.
setDefaultCloseOperation
(
JFrame
.
EXIT_ON_CLOSE
);
//可以关闭窗口
frame
.
addWindowListener
(
new
WindowAdapter
()
{
@Override
public
void
windowClosed
(
WindowEvent
we
)
{
System
.
exit
(
0
);
}
});
}
private
void
callTest
(
List
<
Param
>
paramList
,
String
url
,
int
model
,
Model
coreNum
)
{
private
void
callTest
(
List
<
Param
>
paramList
,
String
url
,
int
model
,
Model
coreNum
,
int
type
)
{
rebuildList
(
paramList
,
url
);
StringBuffer
str
=
new
StringBuffer
();
if
(
model
==
1
){
...
...
@@ -140,12 +154,12 @@ public class WindowStart extends JFrame {
}
ForkJoinPool
pool
=
new
ForkJoinPool
(
coreCount
);
ForkJoinTask
task
=
pool
.
submit
(()
->
{
paramList
.
parallelStream
().
forEach
(
s
->
str
.
append
(
call
(
s
.
getHead
(),
s
.
getBody
(),
s
.
getUrl
())));
paramList
.
parallelStream
().
forEach
(
s
->
str
.
append
(
call
(
s
.
getHead
(),
s
.
getBody
(),
s
.
getUrl
()
,
type
)));
});
task
.
join
();
pool
.
shutdown
();
}
else
{
paramList
.
forEach
(
s
->
str
.
append
(
call
(
s
.
getHead
(),
s
.
getBody
(),
s
.
getUrl
())));
paramList
.
forEach
(
s
->
str
.
append
(
call
(
s
.
getHead
(),
s
.
getBody
(),
s
.
getUrl
()
,
type
)));
}
outTextArea
.
setText
(
outTextArea
.
getText
()
+
str
.
toString
());
}
...
...
@@ -154,7 +168,7 @@ public class WindowStart extends JFrame {
paramList
.
forEach
(
p
->
p
.
setUrl
(
url
));
}
private
static
String
call
(
Map
header
,
Map
bodyObject
,
String
url
){
private
static
String
call
(
Map
header
,
Map
bodyObject
,
String
url
,
int
type
){
StringBuilder
result
=
new
StringBuilder
();
result
.
append
(
"time=["
).
append
(
System
.
currentTimeMillis
()).
append
(
"] "
).
append
(
SPACE
);
try
{
...
...
@@ -162,7 +176,11 @@ public class WindowStart extends JFrame {
argMap
.
put
(
"head"
,
header
);
argMap
.
put
(
"body"
,
bodyObject
);
result
.
append
(
"Request:"
).
append
(
bodyObject
).
append
(
SPACE
).
append
(
"Response:"
);
result
.
append
(
ThirdApiCaller
.
postJson
(
url
,
argMap
,
header
,
String
.
class
));
if
(
type
==
0
){
// Restfull
result
.
append
(
ThirdApiCaller
.
postJson
(
url
,
argMap
,
header
,
String
.
class
));
}
else
if
(
type
==
1
)
{
//WebService
result
.
append
(
ThirdApiCaller
.
postForWS
(
url
,
argMap
));
}
}
catch
(
Exception
e
)
{
result
.
append
(
"Response Error:["
).
append
(
e
.
getMessage
()).
append
(
"]"
);
}
...
...
@@ -170,7 +188,31 @@ public class WindowStart extends JFrame {
}
private
List
<
Param
>
buildTxtData
(
List
<
String
>
fileFullNames
,
StringBuilder
sb
)
{
private
List
<
Param
>
buildTxtData4RestFull
(
List
<
String
>
fileFullNames
,
StringBuilder
sb
)
{
List
<
String
>
failFullNames
=
new
ArrayList
<>();
List
<
Param
>
dtoList
=
new
ArrayList
<>();
for
(
String
fileFullName
:
fileFullNames
)
{
try
{
String
txtDataStr
=
HandleFileUtils
.
readTxtFile
(
fileFullName
);
Map
txtMap
=
JsonUtils
.
toMap
(
txtDataStr
);
sb
.
append
(
txtMap
.
toString
()).
append
(
SPACE
);
Map
head
=
MapUtils
.
getMap
(
txtMap
,
"head"
);
Map
body
=
MapUtils
.
getMap
(
txtMap
,
"body"
);
dtoList
.
add
(
new
Param
(
head
,
body
,
""
));
}
catch
(
Exception
e
)
{
sb
.
append
(
"文件解析失败:"
).
append
(
e
.
getMessage
()).
append
(
SPACE
);
failFullNames
.
add
(
fileFullName
);
}
sb
.
append
(
SPACE
);
}
if
(
CollectionUtils
.
isNotEmpty
(
failFullNames
))
{
failFullNames
.
forEach
(
f
->
sb
.
append
(
"解析失败的文件名:"
).
append
(
f
).
append
(
SPACE
));
}
return
dtoList
;
}
private
List
<
Param
>
buildTxtData4WebService
(
List
<
String
>
fileFullNames
,
StringBuilder
sb
)
{
List
<
String
>
failFullNames
=
new
ArrayList
<>();
List
<
Param
>
dtoList
=
new
ArrayList
<>();
for
(
String
fileFullName
:
fileFullNames
)
{
...
...
src/main/java/utils/ThirdApiCaller.java
View file @
33c87819
package
utils
;
import
com.mycim.framework.utils.lang.StringUtils
;
import
com.mycim.framework.utils.msg.JsonUtils
;
import
org.springframework.http.HttpEntity
;
import
org.springframework.http.HttpHeaders
;
import
org.springframework.http.MediaType
;
import
org.springframework.http.converter.HttpMessageConverter
;
import
org.springframework.http.converter.StringHttpMessageConverter
;
import
org.springframework.util.LinkedMultiValueMap
;
...
...
@@ -12,16 +15,16 @@ import java.util.HashMap;
import
java.util.List
;
import
java.util.Map
;
/**
* @author JP
* @createTime 2021/9/23
*/
public
class
ThirdApiCaller
{
private
static
final
String
FORM_CONTENT_TYPE
=
"application/x-www-form-urlencoded"
;
private
static
final
String
JSON_CONTENT_TYPE
=
"application/json;charset=utf8"
;
private
static
final
String
XML_CONTENT_TYPE
=
"application/xml;charset=utf8"
;
public
class
ThirdApiCaller
{
private
static
final
String
FORM_CONTENT_TYPE
=
"application/x-www-form-urlencoded"
;
private
static
final
String
JSON_CONTENT_TYPE
=
"application/json;charset=utf8"
;
private
static
final
String
XML_CONTENT_TYPE
=
"application/xml;charset=utf8"
;
private
static
final
String
XML_TEXT_CONTENT_TYPE
=
"text/xml;charset=UTF-8"
;
private
static
final
String
NAME_SPACE
=
"http://xml.fa-software.com/mycim/wsdd/"
;
private
static
final
String
SERVICE
=
"execute"
;
private
static
final
String
SUBSTR_START
=
"<Response>"
;
private
static
final
String
SUBSTR_END
=
"</Response>"
;
private
static
RestTemplate
REST_TEMPLATE
;
...
...
@@ -77,11 +80,24 @@ public class ThirdApiCaller {
}
}
public
static
<
T
>
T
postXml
(
String
url
,
Object
body
,
Map
<
String
,
String
>
header
,
Class
<
T
>
clazz
)
{
public
static
String
postForWS
(
String
url
,
Map
<
String
,
Object
>
body
)
{
try
{
HttpHeaders
httpHeaders
=
createXmlHttpHeaders
(
header
);
body
=
body
==
null
?
new
HashMap
<
String
,
String
>()
:
body
;
return
REST_TEMPLATE
.
postForObject
(
url
,
new
HttpEntity
(
body
,
httpHeaders
),
clazz
);
// 创建对象用于xml的拼接
HttpHeaders
headers
=
new
HttpHeaders
();
MediaType
mediaType
=
MediaType
.
parseMediaType
(
XML_TEXT_CONTENT_TYPE
);
headers
.
setContentType
(
mediaType
);
String
soapRequestData
=
"<soapenv:Envelope xmlns:soapenv=\"http://schemas.xmlsoap.org/soap/envelope/\" xmlns:wsdd=\""
+
NAME_SPACE
+
"\">"
+
"<soapenv:Header/>"
+
"<soapenv:Body>"
+
"<wsdd:"
+
SERVICE
+
">"
+
"<Request>"
+
JsonUtils
.
toString
(
body
)
+
"</Request>"
+
"</wsdd:"
+
SERVICE
+
">"
+
"</soapenv:Body>"
+
"</soapenv:Envelope>"
;
HttpEntity
<
String
>
httpEntity
=
new
HttpEntity
<>(
soapRequestData
,
headers
);
return
substrResponse
(
REST_TEMPLATE
.
postForObject
(
url
,
httpEntity
,
String
.
class
));
}
catch
(
Exception
e
)
{
throw
e
;
}
...
...
@@ -120,4 +136,11 @@ public class ThirdApiCaller {
return
headers
;
}
private
static
String
substrResponse
(
String
result
){
if
(
StringUtils
.
isNotBlank
(
result
)
&&
result
.
contains
(
SUBSTR_START
)
&&
result
.
contains
(
SUBSTR_END
)){
result
=
StringUtils
.
substringBetween
(
result
,
SUBSTR_START
,
SUBSTR_END
);
}
return
result
;
}
}
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
.
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment