下载安装groovy:
https://groovy.jfrog.io/artifactory/dist-release-local/groovy-zips/apache-groovy-binary-4.0.2.zip
下载之后直接解压就可以使用,将bin目录添加到系统环境变量中
在IDE中使用:
import groovy.json.JsonSlurper
class demo {
static def execSQL(reuldid, sourceId, envId) {
String url = "http://godata.test.netease.com//open/api/run/rule/" + reuldid + "?sourceId=" + sourceId + "&envId=" + envId + "&token=c4a90bd4-0d42-4412-a166-fdf4ec8f1951"
// 发送请求获取返回信息
def conn = new URL(url).openConnection()
// 设置请求的方式
conn.requestMethod = "GET"
// 创建JsonSlurper对象
def json = new JsonSlurper()
// 将响应信息序列化成json
def response = json.parseText(conn.content.text)
//提取json中的value
def data = response.data
if (response.code == 200 && data != '') {
return data
} else {
return null
}
}
// 对比数据库与api响应数据
static def contrastresult(dbresponse, apiresponse) {
if (dbresponse != null) {
if (dbresponse.size() == apiresponse.size()) {
def obj = dbresponse[0]
// 将数组中的对象拿出来转成map
obj instanceof Map
// 获取map中的所有key
def key_array = obj.keySet()
for (int i = 0; i < dbresponse.size(); i++) {
for (int j = 0; j < key_array.size(); j++) {
String db_value = dbresponse[i].get(key_array[j])
String api_value = apiresponse[i].get(key_array[j])
if (db_value != api_value) {
return "false"
}
}
}
return 'true'
} else {
return 'false'
}
} else {
return '数据库查询失败'
}
}
static void main(String[] args) {
String reuldid = '7FFA'
String sourceId = '3'
String envId = '1'
def db_data = execSQL(reuldid, sourceId, envId)
def conn = new URL("https://xtpadmin-autotest.zts.com.cn/api/v1/org?userId=8160&token=69a2226713e8b4c4717cf01f66c78974&page=1&orgid=&orgname=").openConnection()
conn.requestMethod = "GET"
def json = new JsonSlurper()
def response_api = json.parseText(conn.content.text)
def api_data = response_api.data.list.data
String retult = contrastresult(db_data, api_data)
println retult
}
}