groovy 应用
Yu.Feng 2021-05-30 17:00:00 jmeter
# groovy 应用
# 1. 取出json中某个字段
import groovy.json.JsonOutput
import groovy.json.JsonSlurper
String res = '{"code":"0000","codeDescription":"成功","toastMessage":"成功","caseList":[{"caseNumber":"EC00014EF","caseStates":"待接收"},{"caseNumber":"EC00014SE","caseStates":"新创建"},{"caseNumber":"EC00014S7","caseStates":"处理中"},{"caseNumber":"EC00014S6","caseStates":"处理中"},{"caseNumber":"EC00014S5","caseStates":"处理中"},{"caseNumber":"EC00014S4","caseStates":"处理中"},{"caseNumber":"EC00014S3","caseStates":"处理中"},{"caseNumber":"EC00014RW","caseStates":"处理中"},{"caseNumber":"EC00014RV","caseStates":"处理中"}],"pageSize":10,"ResponseCode":"8000001","ResponseCodeDescription":"成功","StandardQueryID":null,"QueryTime":null}'
// String res = prev.getResponseDataAsString()
def jsonStr = new JsonSlurper().parseText(res)
// jsonStr['caseList'].each{ item ->
// if (item['caseStates'] == '新创建'){
// print item['caseNumber']
// }
// }
print jsonStr['caseList'].findAll{item -> item['caseStates'] == '新创建'}[0]['caseNumber']
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
# 2. groovy 发送http请求
import groovy.json.JsonOutput
import groovy.json.JsonSlurper
def jsonMap = [:]
def markLoans = new URL("http://10.1.5.215:8080/api/contracts/"+ contractNo +"/mark-loans/").openConnection();
def markLoansMap = [:]
markLoansMap.put("loanDate",repayDay+"T16:15:00.000+0800")
markLoansMap.put("loanResults","SUCCESS")
markLoansMap.put("paymentNo",paymentNo)
def markLoansMessage = JsonOutput.toJson(markLoansMap)
log.info(markLoansMessage)
markLoans.setRequestMethod("POST")
markLoans.setDoOutput(true)
markLoans.setRequestProperty("Content-Type", "application/json")
markLoans.getOutputStream().write(markLoansMessage.getBytes("UTF-8"));
def markLoansRC = markLoans.getResponseCode();
def markLoansData
log.info("markLoansRC:"+markLoansRC);
if(markLoansRC.equals(200)) {
markLoansData=markLoans.getInputStream().getText();
}
log.info("markLoansData:"+markLoansData)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
# # (opens new window)3. groovy 执行sql
import groovy.sql.Sql
def krdburl="jdbc:mysql://krtestdb01.c1gls1uyilgy.rds.cn-northwest-1.amazonaws.com.cn:3306/"
def username="fengyu"
def password="e1e29c84ea7"
def driverClass="com.mysql.jdbc.Driver"
def sql = Sql.newInstance(krdburl, username, password, driverClass)
def CID
sql.eachRow("select * from cifdb.t_customer_base where mobile_phone in ('${phone}')") { row ->
// println(row.id)
CID = row.id
}
println CID
//sql.connection.autoCommit = false
sql.execute("delete from cifdb.t_customer_base where mobile_phone = '${phone}'")
def del = "delete from cifdb.t_customer_partner where cid = '"+CID+"'"
sql.execute(del)
def del1 = "delete from oidc.user_partner_bind where cif_user_id = '"+CID+"'"
sql.execute(del1)
def del2 = "delete from credit.t_partner_data_record where cid = '"+CID+"'"
sql.execute(del2)
def del3 = "delete from pbank.t_rpt_action_log where out_biz_id = (select t1.out_biz_id from pbank.t_rpt_base_info_anlyz t1 where t1.cid= '"+CID+"')"
sql.execute(del3)
def del4 = "delete from pbank.t_rpt_base_info_anlyz where cid = '"+CID+"'"
sql.execute(del4)
def del5 = "delete from cifdb.t_customer_card where cid in (select id from cifdb.t_customer_base where id ='"+CID+"')"
def del6 = "delete from cifdb.t_customer_trankeyword where cid in (select id from cifdb.t_customer_base where id ='"+CID+"')"
def del7 = "delete from cifdb.t_customer_posekeyword where cid in (select id from cifdb.t_customer_base where id ='"+CID+"')"
def del8 = "delete from cifdb.t_customer_base where id ='"+CID+"'"
def del9 = "delete from cifdb.t_customer_base_log where baseid ='"+CID+"'"
def del10 = "delete from cifdb.t_customer_linkman where cid ='"+CID+"'"
def del11 = "delete from cifdb.t_customer_linkman_log where cid ='"+CID+"'"
def del12 = "delete from accountdb.t_account_base where cid ='"+CID+"'"
def del13 = "delete from cifdb.t_customer_lbs where cid ='"+CID+"'"
def del14 = "delete from cifdb.t_customer_lbs_log where cid ='"+CID+"'"
def del15 = "delete from cifdb.t_customer_address where cid ='"+CID+"'"
def del16 = "delete from cifdb.t_customer_address_log where cid ='"+CID+"'"
def del17 = "delete from oidc.user_login_log where phone_num= '${phone}'"
def del18 = "delete from cifdb.t_customer_credentials where idcard_no= '${credentials}'"
def del19 = "delete from cifdb.t_customer_credentials_log where idcard_no= '${credentials}'"
def del20 = "delete from cifdb.t_customer_base_log where identity_no= '${credentials}'"
sql.execute(del5)
sql.execute(del6)
sql.execute(del7)
sql.execute(del8)
sql.execute(del9)
sql.execute(del10)
sql.execute(del11)
sql.execute(del12)
sql.execute(del13)
sql.execute(del14)
sql.execute(del15)
sql.execute(del16)
sql.execute(del17)
sql.execute(del18)
sql.execute(del19)
sql.execute(del20)
sql.close()
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63