Commit 7cdccc4d by 吴博

feat: [none] addcase

parent c39b7f00
......@@ -3,7 +3,11 @@
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>com.pcloud.universe</groupId>
<artifactId>universe-commons-parent</artifactId>
<version>3.0.0-SNAPSHOT</version>
</parent>
<groupId>com.lemon</groupId>
<artifactId>Lemon</artifactId>
<version>1.0-SNAPSHOT</version>
......@@ -21,6 +25,7 @@
<aspectj.version>1.9.8</aspectj.version>
<jackson.version>2.6.5</jackson.version>
<universe-error-alarm.version>3.0.1-SNAPSHOT</universe-error-alarm.version>
</properties>
......@@ -71,6 +76,15 @@
<dependencies>
<dependency>
<groupId>com.pcloud.universe</groupId>
<artifactId>universe-error-alarm</artifactId>
<version>${universe-error-alarm.version}</version>
</dependency>
<!-- JSON类型操作坐标-->
<dependency>
<groupId>com.alibaba</groupId>
......
......@@ -22,8 +22,9 @@ public class Rays7ServerCase extends BaseCase {
// String token = "430f837c00f9978dffec9d4c0c05c751";
// 创建post请求
HttpPost post= new HttpPost(url);
post.setHeader("Content-Type", "application/json");
post.setEntity(new StringEntity(json));
post.setHeader("Content-Type", "application/json;charset=UTF-8");
post.setHeader("Authorization", "Bearer");
post.setEntity(new StringEntity(json, "UTF-8"));
DefaultHttpClient client = new DefaultHttpClient();
// 执行get请求,并使用response来存放执行后返回的响应内容
HttpResponse response = client.execute(post);
......@@ -48,7 +49,9 @@ public class Rays7ServerCase extends BaseCase {
public Object[][] datas0() {
//Object[m][n] m代表执行次数,n代表参数个数
Object[][] datas = {
{0, "", "http://192.168.8.40:8270/aibrain/v1.0/digitalRange/testManager", "{\"event\":\"SDK_MSG_REPORT\",\"origin\":\"WECHAT_OFFICIAL\",\"wxSdkMsgDTO\":{\"content\":{\"textMsgDTO\":{\"content\":\"泛悦城\"}},\"contentType\":\"Text\",\"digitalPersonId\":1,\"msgId\":\"24123340480981103\",\"msgRemoteId\":\"24123340480981103\",\"sdkCode\":\"66059\",\"senderSdkCode\":\"o9vkPwnl1Nd4JdBDSpcujb4d4JsU\",\"userId\":182833195}}","{\"errCode\":0,\"message\":\"操作成功!\"}"}, // RAYS跳RAYS
{0, "", "https://adviser.raysgo.com/aibrain/v1.0/digitalRange/testManager",
"{\"event\":\"SDK_MSG_REPORT\",\"origin\":\"WECHAT_H5\",\"wxSdkMsgDTO\":{\"clientHost\":\"192.168.92.114\",\"clientPort\":59634,\"content\":{\"textMsgDTO\":{\"content\":\"答案2\"}},\"contentType\":\"Text\",\"conversationId\":\"143773987\",\"digitalPersonId\":2174,\"language\":\"zh-CN\",\"messageMetaId\":4252464657572101,\"msgRemoteId\":\"4252464657572100\",\"sdkCode\":\"23\",\"senderSdkCode\":\"143773987\",\"serverHost\":\"192.168.92.118\",\"serverPort\":9000,\"userId\":143773987}}"
,"{\"errCode\":0,\"message\":\"操作成功!\"}"}, // RAYS跳RAYS
};
return datas;
}
......
......@@ -4,12 +4,16 @@ import com.alibaba.fastjson.JSONPath;
import com.lemon.constants.Constants;
import com.lemon.pojo.API;
import com.lemon.pojo.Case;
import com.lemon.pojo.TestRecord;
import com.lemon.utils.AuthorizationUtils;
import com.lemon.utils.ExcelUtils;
import com.lemon.utils.SqlUtils;
import com.pcloud.universe.wework.client.MessageBuilder;
import com.pcloud.universe.wework.client.WeworkRobotSender;
import io.qameta.allure.Description;
import io.qameta.allure.Step;
import org.apache.commons.lang3.StringUtils;
import org.apache.http.client.utils.DateUtils;
import org.testng.annotations.DataProvider;
import org.testng.annotations.Test;
import org.testng.Assert;
......@@ -22,49 +26,94 @@ public class ServerCase extends BaseCase {
@Test(dataProvider = "data1", description = "1A:A段和B段sql完全匹配") // DTO数据传输
@Description("1A:A段和B段sql完全匹配")
public void testManager1A(API api, Case cas) throws Exception {
System.out.println("==================================================接口请求分割线==================================================");
//0、执行前休眠30秒
Thread.sleep(1000 * 10);
//1、参数化替换
String params = replace(cas.getParams());
String sql = replace(cas.getCheckSQL());
String Bsql = replace(cas.getCheckBSQL());
cas.setParams(params);
cas.setCheckSQL(sql);
cas.setCheckBSQL(Bsql);
//2、数据库前置查询结果(断言必须在接口执行前后都查询)
Object beforeASqlResult = SqlUtils.querySingle(cas.getCheckSQL());
Object beforeBSqlResult = SqlUtils.querySingle(cas.getCheckBSQL());
//3、调用接口
String body = call(api, cas, false);
// 3.1 将登录的token存储(仅限于登录接口特有)
AuthorizationUtils.storeToken(body);
String assertContent = "Fail";
String body = "";
Object afterASqlResult = null;
Object afterBSqlResult = null;
try {
System.out.println("testManager1A==================================================接口请求分割线==================================================");
//0、执行前休眠30秒
Thread.sleep(1000 * 2);
//1、参数化替换
String params = replace(cas.getParams());
String sql = replace(cas.getCheckSQL());
String Bsql = replace(cas.getCheckBSQL());
cas.setParams(params);
cas.setCheckSQL(sql);
cas.setCheckBSQL(Bsql);
//2、数据库前置查询结果(断言必须在接口执行前后都查询)
Object beforeASqlResult = SqlUtils.querySingle(cas.getCheckSQL());
Object beforeBSqlResult = SqlUtils.querySingle(cas.getCheckBSQL());
//3、调用接口
body = call(api, cas, false);
// 3.1 将登录的token存储(仅限于登录接口特有)
AuthorizationUtils.storeToken(body);
// System.out.println(AuthorizationUtils.env);
//4、断言响应结果,Excel中预期响应数据与实际响应数据进行对比
boolean assertResponseFlag = assertResponse(cas, body);
//5、添加接口响应回写excel内容
addWBD(Integer.parseInt(cas.getId()), Constants.ACTURL_WRITER_BACK_CELL_NUM, body);
//6、数据库后置查询结果
System.out.println("A-sql: " + cas.getCheckSQL());
Object afterASqlResult = SqlUtils.querySingle(cas.getCheckSQL());
System.out.println("B-sql: " + cas.getCheckBSQL());
Thread.sleep(1000 * 30); //验证B段SQL的休眠时间60秒
Object afterBSqlResult = SqlUtils.querySingle(cas.getCheckBSQL());
//4、断言响应结果,Excel中预期响应数据与实际响应数据进行对比
boolean assertResponseFlag = assertResponse(cas, body);
//5、添加接口响应回写excel内容
addWBD(Integer.parseInt(cas.getId()), Constants.ACTURL_WRITER_BACK_CELL_NUM, body);
//6、数据库后置查询结果
System.out.println("A-sql: " + cas.getCheckSQL());
afterASqlResult = SqlUtils.querySingle(cas.getCheckSQL());
// 查不到 ,等待5秒再查
if (null == afterASqlResult) {
Thread.sleep(1000 * 5);
afterASqlResult = SqlUtils.querySingle(cas.getCheckSQL());
}
System.out.println("B-sql: " + cas.getCheckBSQL());
afterBSqlResult = SqlUtils.querySingle(cas.getCheckBSQL());
// 查不到 ,等待5秒再查
if (null == afterBSqlResult) {
Thread.sleep(1000 * 5);
afterBSqlResult = SqlUtils.querySingle(cas.getCheckBSQL());
// 查不到 ,等待10秒再查
if (null == afterBSqlResult) {
Thread.sleep(1000 * 10);
afterBSqlResult = SqlUtils.querySingle(cas.getCheckBSQL());
}
}
//7-1、A段数据库断言
boolean sqlAFlag = assertSql(cas.getaExpectValue(), beforeASqlResult == null ? null : beforeASqlResult.toString(), afterASqlResult == null ? null : afterASqlResult.toString());
String assertSqlAFlag = (sqlAFlag) ? "Pass" : "Fail";
addWBD(Integer.parseInt(cas.getId()), Constants.A_ASSERT_RESULT_CELL_NUM, assertSqlAFlag);
System.out.println("数据库A-sql断言结果:" + sqlAFlag);
//7-2、B段数据库断言
boolean sqlBFlag = assertSql(cas.getbExpectValue(), beforeBSqlResult == null ? null : beforeBSqlResult.toString(), afterBSqlResult == null ? null : afterBSqlResult.toString());
String assertSqlBFlag = (sqlBFlag) ? "Pass" : "Fail";
addWBD(Integer.parseInt(cas.getId()), Constants.B_ASSERT_RESULT_CELL_NUM, assertSqlBFlag);
System.out.println("数据库B-sql断言结果:" + sqlBFlag);
//8、添加断言回写内容
assertContent = (assertResponseFlag && sqlAFlag && sqlBFlag) ? "Pass" : "Fail";
addWBD(Integer.parseInt(cas.getId()), Constants.ASSERT_RESULT_CELL_NUM, assertContent);
Assert.assertEquals(assertContent, "Pass");
} catch (Exception e) {
log.error("接口测试失败", e);
} finally {
// 同时将测试结果新增到数据库中
TestRecord testRecord = new TestRecord();
testRecord.setCaseId(Integer.valueOf(cas.getId()));
testRecord.setCaseDesc(cas.getDesc());
testRecord.setInquiry(cas.getParams());
testRecord.setExpectContent(cas.getExpectValue());
testRecord.setInputContent(cas.getParams());
testRecord.setOutputContent(null != afterASqlResult ? afterASqlResult.toString() : null + " | " + null != afterBSqlResult ? afterBSqlResult.toString() : null);
testRecord.setState("Pass".equals(assertContent) ? 1 : 0);
testRecord.setTestVersion(DateUtils.formatDate(new Date(), "yyyyMMdd HH:mm"));
testRecord.setCreateTime(new Date());
testRecord.setUpdateTime(new Date());
testRecord.setIsDelete(0);
SqlUtils.insertTestRecord(testRecord);
// 如果失败则发送消息提醒
if (!"Pass".equals(assertContent)) {
sendAlarm(cas.getParams());
}
}
//7-1、A段数据库断言
boolean sqlAFlag = assertSql(cas.getaExpectValue(), beforeASqlResult == null ? null : beforeASqlResult.toString(), afterASqlResult == null ? null : afterASqlResult.toString());
String assertSqlAFlag = (sqlAFlag) ? "Pass" : "Fail";
addWBD(Integer.parseInt(cas.getId()), Constants.A_ASSERT_RESULT_CELL_NUM, assertSqlAFlag);
System.out.println("数据库A-sql断言结果:" + sqlAFlag);
//7-2、B段数据库断言
boolean sqlBFlag = assertSql(cas.getbExpectValue(), beforeBSqlResult == null ? null : beforeBSqlResult.toString(), afterBSqlResult == null ? null : afterBSqlResult.toString());
String assertSqlBFlag = (sqlBFlag) ? "Pass" : "Fail";
addWBD(Integer.parseInt(cas.getId()), Constants.B_ASSERT_RESULT_CELL_NUM, assertSqlBFlag);
System.out.println("数据库B-sql断言结果:" + sqlBFlag);
//8、添加断言回写内容
String assertContent = (assertResponseFlag && sqlAFlag && sqlBFlag) ? "Pass" : "Fail";
addWBD(Integer.parseInt(cas.getId()), Constants.ASSERT_RESULT_CELL_NUM, assertContent);
Assert.assertEquals(assertContent, "Pass");
}
@DataProvider(name = "data1")
......@@ -76,30 +125,67 @@ public class ServerCase extends BaseCase {
@Test(dataProvider = "data2", description = "2B:B段sql部分匹配")
@Description("2B:B段sql部分匹配")
public void testManager2B(API api, Case cas) throws Exception {
System.out.println("==================================================接口请求分割线==================================================");
Thread.sleep(1000 * 10);
String params = replace(cas.getParams());
String Bsql = replace(cas.getCheckBSQL());
cas.setParams(params);
cas.setCheckBSQL(Bsql);
Object beforeBSqlResult = SqlUtils.querySingle(cas.getCheckBSQL());
String body = call(api, cas, false);
AuthorizationUtils.storeToken(body);
Object afterASqlResult = null;
Object afterBSqlResult = null;
String assertContent = "Fail";
try {
System.out.println("testManager2B==================================================接口请求分割线==================================================");
Thread.sleep(1000 * 2);
String params = replace(cas.getParams());
String Bsql = replace(cas.getCheckBSQL());
cas.setParams(params);
cas.setCheckBSQL(Bsql);
Object beforeBSqlResult = SqlUtils.querySingle(cas.getCheckBSQL());
String body = call(api, cas, false);
AuthorizationUtils.storeToken(body);
// System.out.println(AuthorizationUtils.env);
boolean assertResponseFlag = assertResponse(cas, body);
addWBD(Integer.parseInt(cas.getId()), Constants.ACTURL_WRITER_BACK_CELL_NUM, body);
System.out.println("B-sql: " + cas.getCheckBSQL());
Thread.sleep(1000 * 30); //验证B段SQL的休眠时间60秒
Object afterBSqlResult = SqlUtils.querySingle(cas.getCheckBSQL());
//7-2、B段数据库断言
boolean sqlBFlag = assertSqlB(beforeBSqlResult == null ? null : beforeBSqlResult.toString(), afterBSqlResult == null ? null : afterBSqlResult.toString());
String assertSqlBFlag = (sqlBFlag) ? "Pass" : "Fail";
addWBD(Integer.parseInt(cas.getId()), Constants.B_ASSERT_RESULT_CELL_NUM, assertSqlBFlag);
System.out.println("数据库B-sql断言结果:" + sqlBFlag);
//8、添加断言回写内容
String assertContent = (assertResponseFlag && sqlBFlag) ? "Pass" : "Fail";
addWBD(Integer.parseInt(cas.getId()), Constants.ASSERT_RESULT_CELL_NUM, assertContent);
Assert.assertEquals(assertContent, "Pass");
boolean assertResponseFlag = assertResponse(cas, body);
addWBD(Integer.parseInt(cas.getId()), Constants.ACTURL_WRITER_BACK_CELL_NUM, body);
System.out.println("B-sql: " + cas.getCheckBSQL());
afterBSqlResult = SqlUtils.querySingle(cas.getCheckBSQL());
// 查不到 ,等待5秒再查
if (null == afterBSqlResult) {
Thread.sleep(1000 * 5);
afterBSqlResult = SqlUtils.querySingle(cas.getCheckBSQL());
// 查不到 ,等待10秒再查
if (null == afterBSqlResult) {
Thread.sleep(1000 * 10);
afterBSqlResult = SqlUtils.querySingle(cas.getCheckBSQL());
}
}
//7-2、B段数据库断言
boolean sqlBFlag = assertSqlB(beforeBSqlResult == null ? null : beforeBSqlResult.toString(), afterBSqlResult == null ? null : afterBSqlResult.toString());
String assertSqlBFlag = (sqlBFlag) ? "Pass" : "Fail";
addWBD(Integer.parseInt(cas.getId()), Constants.B_ASSERT_RESULT_CELL_NUM, assertSqlBFlag);
System.out.println("数据库B-sql断言结果:" + sqlBFlag);
//8、添加断言回写内容
assertContent = (assertResponseFlag && sqlBFlag) ? "Pass" : "Fail";
addWBD(Integer.parseInt(cas.getId()), Constants.ASSERT_RESULT_CELL_NUM, assertContent);
Assert.assertEquals(assertContent, "Pass");
} catch (InterruptedException e) {
throw new RuntimeException(e);
} catch (NumberFormatException e) {
throw new RuntimeException(e);
} finally {
// 同时将测试结果新增到数据库中
TestRecord testRecord = new TestRecord();
testRecord.setCaseId(Integer.valueOf(cas.getId()));
testRecord.setCaseDesc(cas.getDesc());
testRecord.setInquiry(cas.getParams());
testRecord.setExpectContent(cas.getExpectValue());
testRecord.setInputContent(cas.getParams());
testRecord.setOutputContent(null != afterASqlResult ? afterASqlResult.toString() : null + " | " + null != afterBSqlResult ? afterBSqlResult.toString() : null);
testRecord.setState("Pass".equals(assertContent) ? 1 : 0);
testRecord.setTestVersion(DateUtils.formatDate(new Date(), "yyyyMMdd HH:mm"));
testRecord.setCreateTime(new Date());
testRecord.setUpdateTime(new Date());
testRecord.setIsDelete(0);
SqlUtils.insertTestRecord(testRecord);
// 如果失败则发送消息提醒
if (!"Pass".equals(assertContent)) {
sendAlarm(cas.getParams());
}
}
}
// 使用Excel文件传输接口信息和请求信息
......@@ -112,27 +198,60 @@ public class ServerCase extends BaseCase {
@Test(dataProvider = "data3", description = "3C:A段sql完全匹配") // DTO数据传输
@Description("3C:A段sql完全匹配")
public void testManager3C(API api, Case cas) throws Exception {
System.out.println("==================================================接口请求分割线==================================================");
Thread.sleep(1000 * 10);
String params = replace(cas.getParams());
String sql = replace(cas.getCheckSQL());
cas.setParams(params);
cas.setCheckBSQL(sql);
Object beforeASqlResult = SqlUtils.querySingle(cas.getCheckSQL());
String body = call(api, cas, false);
AuthorizationUtils.storeToken(body);
boolean assertResponseFlag = assertResponse(cas, body);
addWBD(Integer.parseInt(cas.getId()), Constants.ACTURL_WRITER_BACK_CELL_NUM, body);
System.out.println("a-sql: " + cas.getCheckSQL());
Thread.sleep(1000 * 30); //A段SQL的休眠时间60秒
Object afterASqlResult = SqlUtils.querySingle(cas.getCheckSQL());
boolean sqlAFlag = assertSql(cas.getaExpectValue(), beforeASqlResult == null ? null : beforeASqlResult.toString(), afterASqlResult == null ? null : afterASqlResult.toString());
String assertSqlAFlag = (sqlAFlag) ? "Pass" : "Fail";
addWBD(Integer.parseInt(cas.getId()), Constants.A_ASSERT_RESULT_CELL_NUM, assertSqlAFlag);
System.out.println("数据库A-sql断言结果:" + sqlAFlag);
String assertContent = (assertResponseFlag && sqlAFlag) ? "Pass" : "Fail";
addWBD(Integer.parseInt(cas.getId()), Constants.ASSERT_RESULT_CELL_NUM, assertContent);
Assert.assertEquals(assertContent, "Pass");
Object afterASqlResult = null;
Object afterBSqlResult = null;
String assertContent = "Fail";
try {
System.out.println("testManager3C==================================================接口请求分割线==================================================");
Thread.sleep(1000 * 2);
String params = replace(cas.getParams());
String sql = replace(cas.getCheckSQL());
cas.setParams(params);
cas.setCheckBSQL(sql);
Object beforeASqlResult = SqlUtils.querySingle(cas.getCheckSQL());
String body = call(api, cas, false);
AuthorizationUtils.storeToken(body);
boolean assertResponseFlag = assertResponse(cas, body);
addWBD(Integer.parseInt(cas.getId()), Constants.ACTURL_WRITER_BACK_CELL_NUM, body);
System.out.println("a-sql: " + cas.getCheckSQL());
afterASqlResult = SqlUtils.querySingle(cas.getCheckSQL());
// 查不到 ,等待5秒再查
if (null == afterASqlResult) {
Thread.sleep(1000 * 5);
afterASqlResult = SqlUtils.querySingle(cas.getCheckSQL());
}
boolean sqlAFlag = assertSql(cas.getaExpectValue(), beforeASqlResult == null ? null : beforeASqlResult.toString(), afterASqlResult == null ? null : afterASqlResult.toString());
String assertSqlAFlag = (sqlAFlag) ? "Pass" : "Fail";
addWBD(Integer.parseInt(cas.getId()), Constants.A_ASSERT_RESULT_CELL_NUM, assertSqlAFlag);
System.out.println("数据库A-sql断言结果:" + sqlAFlag);
assertContent = (assertResponseFlag && sqlAFlag) ? "Pass" : "Fail";
addWBD(Integer.parseInt(cas.getId()), Constants.ASSERT_RESULT_CELL_NUM, assertContent);
Assert.assertEquals(assertContent, "Pass");
} catch (InterruptedException e) {
throw new RuntimeException(e);
} catch (NumberFormatException e) {
throw new RuntimeException(e);
} finally {
// 同时将测试结果新增到数据库中
TestRecord testRecord = new TestRecord();
testRecord.setCaseId(Integer.valueOf(cas.getId()));
testRecord.setCaseDesc(cas.getDesc());
testRecord.setInquiry(cas.getParams());
testRecord.setExpectContent(cas.getExpectValue());
testRecord.setInputContent(cas.getParams());
testRecord.setOutputContent(null != afterASqlResult ? afterASqlResult.toString() : null + " | " + null != afterBSqlResult ? afterBSqlResult.toString() : null);
testRecord.setState("Pass".equals(assertContent) ? 1 : 0);
testRecord.setTestVersion(DateUtils.formatDate(new Date(), "yyyyMMdd HH:mm"));
testRecord.setCreateTime(new Date());
testRecord.setUpdateTime(new Date());
testRecord.setIsDelete(0);
SqlUtils.insertTestRecord(testRecord);
// 如果失败则发送消息提醒
if (!"Pass".equals(assertContent)) {
sendAlarm(cas.getParams());
}
}
}
@DataProvider(name = "data3")
......@@ -145,50 +264,90 @@ public class ServerCase extends BaseCase {
@Test(dataProvider = "data4", description = "4D:A段sql完全匹配,B段sql部分匹配") // DTO数据传输
@Description("4D:A段sql完全匹配,B段sql部分匹配")
public void testManager4D(API api, Case cas) throws Exception {
System.out.println("==================================================接口请求分割线==================================================");
//0、执行前休眠30秒
//1、参数化替换
String params = replace(cas.getParams());
String sql = replace(cas.getCheckSQL());
String Bsql = replace(cas.getCheckBSQL());
cas.setParams(params);
cas.setCheckSQL(sql);
cas.setCheckBSQL(Bsql);
//2、数据库前置查询结果(断言必须在接口执行前后都查询)
Thread.sleep(1000 * 30);
Object beforeASqlResult = SqlUtils.querySingle(cas.getCheckSQL());
Thread.sleep(1000 * 30);
Object beforeBSqlResult = SqlUtils.querySingle(cas.getCheckBSQL());
//3、调用接口
String body = call(api, cas, false);
// 3.1 将登录的token存储(仅限于登录接口特有)
AuthorizationUtils.storeToken(body);
Object afterASqlResult = null;
Object afterBSqlResult = null;
String assertContent = "Fail";
try {
System.out.println("testManager4D==================================================接口请求分割线==================================================");
//0、执行前休眠30秒
//1、参数化替换
String params = replace(cas.getParams());
String sql = replace(cas.getCheckSQL());
String Bsql = replace(cas.getCheckBSQL());
cas.setParams(params);
cas.setCheckSQL(sql);
cas.setCheckBSQL(Bsql);
//2、数据库前置查询结果(断言必须在接口执行前后都查询)
Object beforeASqlResult = SqlUtils.querySingle(cas.getCheckSQL());
Object beforeBSqlResult = SqlUtils.querySingle(cas.getCheckBSQL());
//3、调用接口
String body = call(api, cas, false);
// 3.1 将登录的token存储(仅限于登录接口特有)
AuthorizationUtils.storeToken(body);
// System.out.println(AuthorizationUtils.env);
//4、断言响应结果,Excel中预期响应数据与实际响应数据进行对比
boolean assertResponseFlag = assertResponse(cas, body);
//5、添加接口响应回写excel内容
addWBD(Integer.parseInt(cas.getId()), Constants.ACTURL_WRITER_BACK_CELL_NUM, body);
//6、数据库后置查询结果
System.out.println("A-sql: " + cas.getCheckSQL());
Thread.sleep(1000 * 30);
Object afterASqlResult = SqlUtils.querySingle(cas.getCheckSQL());
System.out.println("B-sql: " + cas.getCheckBSQL());
Thread.sleep(1000 * 30); //验证B段SQL的休眠时间60秒
Object afterBSqlResult = SqlUtils.querySingle(cas.getCheckBSQL());
//7-1、A段数据库断言
boolean sqlAFlag = assertSql(cas.getaExpectValue(), beforeASqlResult == null ? null : beforeASqlResult.toString(), afterASqlResult == null ? null : afterASqlResult.toString());
String assertSqlAFlag = (sqlAFlag) ? "Pass" : "Fail";
addWBD(Integer.parseInt(cas.getId()), Constants.A_ASSERT_RESULT_CELL_NUM, assertSqlAFlag);
System.out.println("数据库A-sql断言结果:" + sqlAFlag);
//7-2、B段数据库断言
boolean sqlBFlag = assertSqlB(beforeBSqlResult == null ? null : beforeBSqlResult.toString(), afterBSqlResult == null ? null : afterBSqlResult.toString());
String assertSqlBFlag = (sqlBFlag) ? "Pass" : "Fail";
addWBD(Integer.parseInt(cas.getId()), Constants.B_ASSERT_RESULT_CELL_NUM, assertSqlBFlag);
System.out.println("数据库B-sql断言结果:" + sqlBFlag);
//8、添加断言回写内容
String assertContent = (assertResponseFlag && sqlAFlag && sqlBFlag) ? "Pass" : "Fail";
addWBD(Integer.parseInt(cas.getId()), Constants.ASSERT_RESULT_CELL_NUM, assertContent);
Assert.assertEquals(assertContent, "Pass");
//4、断言响应结果,Excel中预期响应数据与实际响应数据进行对比
boolean assertResponseFlag = assertResponse(cas, body);
//5、添加接口响应回写excel内容
addWBD(Integer.parseInt(cas.getId()), Constants.ACTURL_WRITER_BACK_CELL_NUM, body);
//6、数据库后置查询结果
System.out.println("A-sql: " + cas.getCheckSQL());
afterASqlResult = SqlUtils.querySingle(cas.getCheckSQL());
// 查不到 ,等待5秒再查
if (null == afterASqlResult) {
Thread.sleep(1000 * 5);
afterASqlResult = SqlUtils.querySingle(cas.getCheckSQL());
}
System.out.println("B-sql: " + cas.getCheckBSQL());
afterBSqlResult = SqlUtils.querySingle(cas.getCheckBSQL());
// 查不到 ,等待5秒再查
if (null == afterBSqlResult) {
Thread.sleep(1000 * 5);
afterBSqlResult = SqlUtils.querySingle(cas.getCheckBSQL());
// 查不到 ,等待10秒再查
if (null == afterBSqlResult) {
Thread.sleep(1000 * 10);
afterBSqlResult = SqlUtils.querySingle(cas.getCheckBSQL());
}
}
//7-1、A段数据库断言
boolean sqlAFlag = assertSql(cas.getaExpectValue(), beforeASqlResult == null ? null : beforeASqlResult.toString(), afterASqlResult == null ? null : afterASqlResult.toString());
String assertSqlAFlag = (sqlAFlag) ? "Pass" : "Fail";
addWBD(Integer.parseInt(cas.getId()), Constants.A_ASSERT_RESULT_CELL_NUM, assertSqlAFlag);
System.out.println("数据库A-sql断言结果:" + sqlAFlag);
//7-2、B段数据库断言
boolean sqlBFlag = assertSqlB(beforeBSqlResult == null ? null : beforeBSqlResult.toString(), afterBSqlResult == null ? null : afterBSqlResult.toString());
String assertSqlBFlag = (sqlBFlag) ? "Pass" : "Fail";
addWBD(Integer.parseInt(cas.getId()), Constants.B_ASSERT_RESULT_CELL_NUM, assertSqlBFlag);
System.out.println("数据库B-sql断言结果:" + sqlBFlag);
//8、添加断言回写内容
assertContent = (assertResponseFlag && sqlAFlag && sqlBFlag) ? "Pass" : "Fail";
addWBD(Integer.parseInt(cas.getId()), Constants.ASSERT_RESULT_CELL_NUM, assertContent);
Assert.assertEquals(assertContent, "Pass");
} catch (InterruptedException e) {
throw new RuntimeException(e);
} catch (NumberFormatException e) {
throw new RuntimeException(e);
} finally {
// 同时将测试结果新增到数据库中
TestRecord testRecord = new TestRecord();
testRecord.setCaseId(Integer.valueOf(cas.getId()));
testRecord.setCaseDesc(cas.getDesc());
testRecord.setInquiry(cas.getParams());
testRecord.setExpectContent(cas.getExpectValue());
testRecord.setInputContent(cas.getParams());
testRecord.setOutputContent(null != afterASqlResult ? afterASqlResult.toString() : null + " | " + null != afterBSqlResult ? afterBSqlResult.toString() : null);
testRecord.setState("Pass".equals(assertContent) ? 1 : 0);
testRecord.setTestVersion(DateUtils.formatDate(new Date(), "yyyyMMdd HH:mm"));
testRecord.setCreateTime(new Date());
testRecord.setUpdateTime(new Date());
testRecord.setIsDelete(0);
SqlUtils.insertTestRecord(testRecord);
// 如果失败则发送消息提醒
if (!"Pass".equals(assertContent)) {
sendAlarm(cas.getParams());
}
}
}
// 使用Excel文件传输接口信息和请求信息
......@@ -202,47 +361,89 @@ public class ServerCase extends BaseCase {
@Test(dataProvider = "data5", description = "5E:A段sql完全匹配,B段sql部分匹配,适用于作品应用")
@Description("5E:A段sql完全匹配,B段sql部分匹配,适用于作品应用")
public void testManager5E(API api, Case cas) throws Exception {
System.out.println("==================================================接口请求分割线==================================================");
//1、参数化替换
String params = replace(cas.getParams());
String sql = replace(cas.getCheckSQL());
String Bsql = replace(cas.getCheckBSQL());
cas.setParams(params);
cas.setCheckSQL(sql);
cas.setCheckBSQL(Bsql);
//2、数据库前置查询结果(断言必须在接口执行前后都查询)
Object beforeASqlResult = SqlUtils.querySingle(cas.getCheckSQL());
Object beforeBSqlResult = SqlUtils.querySingle(cas.getCheckBSQL());
//3、调用接口
String body = call(api, cas, false);
// 3.1 将登录的token存储(仅限于登录接口特有)
AuthorizationUtils.storeToken(body);
Object afterASqlResult = null;
Object afterBSqlResult = null;
String assertContent = "Fail";
try {
System.out.println("testManager5E==================================================接口请求分割线==================================================");
//1、参数化替换
String params = replace(cas.getParams());
String sql = replace(cas.getCheckSQL());
String Bsql = replace(cas.getCheckBSQL());
cas.setParams(params);
cas.setCheckSQL(sql);
cas.setCheckBSQL(Bsql);
//2、数据库前置查询结果(断言必须在接口执行前后都查询)
Object beforeASqlResult = SqlUtils.querySingle(cas.getCheckSQL());
Object beforeBSqlResult = SqlUtils.querySingle(cas.getCheckBSQL());
//3、调用接口
String body = call(api, cas, false);
// 3.1 将登录的token存储(仅限于登录接口特有)
AuthorizationUtils.storeToken(body);
// System.out.println(AuthorizationUtils.env);
//4、断言响应结果,Excel中预期响应数据与实际响应数据进行对比
boolean assertResponseFlag = assertResponse(cas, body);
//5、添加接口响应回写excel内容
addWBD(Integer.parseInt(cas.getId()), Constants.ACTURL_WRITER_BACK_CELL_NUM, body);
//6、数据库后置查询结果
System.out.println("A-sql: " + cas.getCheckSQL());
Thread.sleep(1000 * 30);
Object afterASqlResult = SqlUtils.querySingle(cas.getCheckSQL());
System.out.println("B-sql: " + cas.getCheckBSQL());
Thread.sleep(1000 * 30); //验证B段SQL的休眠时间60秒
Object afterBSqlResult = SqlUtils.querySingle(cas.getCheckBSQL());
//7-1、A段数据库断言
boolean sqlAFlag = assertSql(cas.getaExpectValue(), beforeASqlResult == null ? null : beforeASqlResult.toString(), afterASqlResult == null ? null : afterASqlResult.toString());
String assertSqlAFlag = (sqlAFlag) ? "Pass" : "Fail";
addWBD(Integer.parseInt(cas.getId()), Constants.A_ASSERT_RESULT_CELL_NUM, assertSqlAFlag);
System.out.println("数据库A-sql断言结果:" + sqlAFlag);
//7-2、B段数据库断言
boolean sqlBFlag = assertSqlB(beforeBSqlResult == null ? null : beforeBSqlResult.toString(), afterBSqlResult == null ? null : afterBSqlResult.toString());
String assertSqlBFlag = (sqlBFlag) ? "Pass" : "Fail";
addWBD(Integer.parseInt(cas.getId()), Constants.B_ASSERT_RESULT_CELL_NUM, assertSqlBFlag);
System.out.println("数据库B-sql断言结果:" + sqlBFlag);
//8、添加断言回写内容
String assertContent = (assertResponseFlag && sqlAFlag && sqlBFlag) ? "Pass" : "Fail";
addWBD(Integer.parseInt(cas.getId()), Constants.ASSERT_RESULT_CELL_NUM, assertContent);
Assert.assertEquals(assertContent, "Pass");
//4、断言响应结果,Excel中预期响应数据与实际响应数据进行对比
boolean assertResponseFlag = assertResponse(cas, body);
//5、添加接口响应回写excel内容
addWBD(Integer.parseInt(cas.getId()), Constants.ACTURL_WRITER_BACK_CELL_NUM, body);
//6、数据库后置查询结果
System.out.println("A-sql: " + cas.getCheckSQL());
afterASqlResult = SqlUtils.querySingle(cas.getCheckSQL());
// 查不到 ,等待5秒再查
if (null == afterASqlResult) {
Thread.sleep(1000 * 5);
afterASqlResult = SqlUtils.querySingle(cas.getCheckSQL());
}
System.out.println("B-sql: " + cas.getCheckBSQL());
afterBSqlResult = SqlUtils.querySingle(cas.getCheckBSQL());
// 查不到 ,等待5秒再查
if (null == afterBSqlResult) {
Thread.sleep(1000 * 5);
afterBSqlResult = SqlUtils.querySingle(cas.getCheckBSQL());
// 查不到 ,等待10秒再查
if (null == afterBSqlResult) {
Thread.sleep(1000 * 10);
afterBSqlResult = SqlUtils.querySingle(cas.getCheckBSQL());
}
}
//7-1、A段数据库断言
boolean sqlAFlag = assertSql(cas.getaExpectValue(), beforeASqlResult == null ? null : beforeASqlResult.toString(), afterASqlResult == null ? null : afterASqlResult.toString());
String assertSqlAFlag = (sqlAFlag) ? "Pass" : "Fail";
addWBD(Integer.parseInt(cas.getId()), Constants.A_ASSERT_RESULT_CELL_NUM, assertSqlAFlag);
System.out.println("数据库A-sql断言结果:" + sqlAFlag);
//7-2、B段数据库断言
boolean sqlBFlag = assertSqlB(beforeBSqlResult == null ? null : beforeBSqlResult.toString(), afterBSqlResult == null ? null : afterBSqlResult.toString());
String assertSqlBFlag = (sqlBFlag) ? "Pass" : "Fail";
addWBD(Integer.parseInt(cas.getId()), Constants.B_ASSERT_RESULT_CELL_NUM, assertSqlBFlag);
System.out.println("数据库B-sql断言结果:" + sqlBFlag);
//8、添加断言回写内容
assertContent = (assertResponseFlag && sqlAFlag && sqlBFlag) ? "Pass" : "Fail";
addWBD(Integer.parseInt(cas.getId()), Constants.ASSERT_RESULT_CELL_NUM, assertContent);
Assert.assertEquals(assertContent, "Pass");
} catch (NumberFormatException e) {
throw new RuntimeException(e);
} catch (InterruptedException e) {
throw new RuntimeException(e);
} finally {
// 同时将测试结果新增到数据库中
TestRecord testRecord = new TestRecord();
testRecord.setCaseId(Integer.valueOf(cas.getId()));
testRecord.setCaseDesc(cas.getDesc());
testRecord.setInquiry(cas.getParams());
testRecord.setExpectContent(cas.getExpectValue());
testRecord.setInputContent(cas.getParams());
testRecord.setOutputContent(null != afterASqlResult ? afterASqlResult.toString() : null + " | " + null != afterBSqlResult ? afterBSqlResult.toString() : null);
testRecord.setState("Pass".equals(assertContent) ? 1 : 0);
testRecord.setTestVersion(DateUtils.formatDate(new Date(), "yyyyMMdd HH:mm"));
testRecord.setCreateTime(new Date());
testRecord.setUpdateTime(new Date());
testRecord.setIsDelete(0);
SqlUtils.insertTestRecord(testRecord);
// 如果失败则发送消息提醒
if (!"Pass".equals(assertContent)) {
sendAlarm(cas.getParams());
}
}
}
@DataProvider(name = "data5")
......@@ -254,44 +455,81 @@ public class ServerCase extends BaseCase {
@Test(dataProvider = "data6", description = "6F:B段sql完全匹配") // DTO数据传输
@Description("6F:B段sql完全匹配")
public void testManager6F(API api, Case cas) throws Exception {
System.out.println("==================================================接口请求分割线==================================================");
//0、执行前休眠30秒
Thread.sleep(1000 * 10);
//1、参数化替换
String params = replace(cas.getParams());
String sql = replace(cas.getCheckSQL());
String Bsql = replace(cas.getCheckBSQL());
cas.setParams(params);
cas.setCheckSQL(sql);
cas.setCheckBSQL(Bsql);
//2、数据库前置查询结果(断言必须在接口执行前后都查询)
Object beforeASqlResult = SqlUtils.querySingle(cas.getCheckSQL());
Object beforeBSqlResult = SqlUtils.querySingle(cas.getCheckBSQL());
//3、调用接口
String body = call(api, cas, false);
// 3.1 将登录的token存储(仅限于登录接口特有)
AuthorizationUtils.storeToken(body);
Object afterASqlResult = null;
Object afterBSqlResult = null;
String assertContent = "Fail";
try {
System.out.println("testManager6F==================================================接口请求分割线==================================================");
//0、执行前休眠30秒
Thread.sleep(1000 * 2);
//1、参数化替换
String params = replace(cas.getParams());
String sql = replace(cas.getCheckSQL());
String Bsql = replace(cas.getCheckBSQL());
cas.setParams(params);
cas.setCheckSQL(sql);
cas.setCheckBSQL(Bsql);
//2、数据库前置查询结果(断言必须在接口执行前后都查询)
Object beforeASqlResult = SqlUtils.querySingle(cas.getCheckSQL());
Object beforeBSqlResult = SqlUtils.querySingle(cas.getCheckBSQL());
//3、调用接口
String body = call(api, cas, false);
// 3.1 将登录的token存储(仅限于登录接口特有)
AuthorizationUtils.storeToken(body);
// System.out.println(AuthorizationUtils.env);
//4、断言响应结果,Excel中预期响应数据与实际响应数据进行对比
boolean assertResponseFlag = assertResponse(cas, body);
//5、添加接口响应回写excel内容
addWBD(Integer.parseInt(cas.getId()), Constants.ACTURL_WRITER_BACK_CELL_NUM, body);
//6、数据库后置查询结果
//4、断言响应结果,Excel中预期响应数据与实际响应数据进行对比
boolean assertResponseFlag = assertResponse(cas, body);
//5、添加接口响应回写excel内容
addWBD(Integer.parseInt(cas.getId()), Constants.ACTURL_WRITER_BACK_CELL_NUM, body);
//6、数据库后置查询结果
// System.out.println("A-sql: " + cas.getCheckSQL());
// Object afterASqlResult = SqlUtils.querySingle(cas.getCheckSQL());
System.out.println("B-sql: " + cas.getCheckBSQL());
Thread.sleep(1000 * 30); //验证B段SQL的休眠时间60秒
Object afterBSqlResult = SqlUtils.querySingle(cas.getCheckBSQL());
System.out.println("B-sql: " + cas.getCheckBSQL());
afterBSqlResult = SqlUtils.querySingle(cas.getCheckBSQL());
// 查不到 ,等待5秒再查
if (null == afterBSqlResult) {
Thread.sleep(1000 * 5);
afterBSqlResult = SqlUtils.querySingle(cas.getCheckBSQL());
// 查不到 ,等待10秒再查
if (null == afterBSqlResult) {
Thread.sleep(1000 * 10);
afterBSqlResult = SqlUtils.querySingle(cas.getCheckBSQL());
}
}
//7-2、B段数据库断言
boolean sqlBFlag = assertSql(cas.getbExpectValue(), beforeBSqlResult == null ? null : beforeBSqlResult.toString(), afterBSqlResult == null ? null : afterBSqlResult.toString());
String assertSqlBFlag = (sqlBFlag) ? "Pass" : "Fail";
addWBD(Integer.parseInt(cas.getId()), Constants.B_ASSERT_RESULT_CELL_NUM, assertSqlBFlag);
System.out.println("数据库B-sql断言结果:" + sqlBFlag);
//8、添加断言回写内容
String assertContent = (assertResponseFlag && sqlBFlag) ? "Pass" : "Fail";
addWBD(Integer.parseInt(cas.getId()), Constants.ASSERT_RESULT_CELL_NUM, assertContent);
Assert.assertEquals(assertContent, "Pass");
//7-2、B段数据库断言
boolean sqlBFlag = assertSql(cas.getbExpectValue(), beforeBSqlResult == null ? null : beforeBSqlResult.toString(), afterBSqlResult == null ? null : afterBSqlResult.toString());
String assertSqlBFlag = (sqlBFlag) ? "Pass" : "Fail";
addWBD(Integer.parseInt(cas.getId()), Constants.B_ASSERT_RESULT_CELL_NUM, assertSqlBFlag);
System.out.println("数据库B-sql断言结果:" + sqlBFlag);
//8、添加断言回写内容
assertContent = (assertResponseFlag && sqlBFlag) ? "Pass" : "Fail";
addWBD(Integer.parseInt(cas.getId()), Constants.ASSERT_RESULT_CELL_NUM, assertContent);
Assert.assertEquals(assertContent, "Pass");
} catch (InterruptedException e) {
throw new RuntimeException(e);
} catch (NumberFormatException e) {
throw new RuntimeException(e);
} finally {
// 同时将测试结果新增到数据库中
TestRecord testRecord = new TestRecord();
testRecord.setCaseId(Integer.valueOf(cas.getId()));
testRecord.setCaseDesc(cas.getDesc());
testRecord.setInquiry(cas.getParams());
testRecord.setExpectContent(cas.getExpectValue());
testRecord.setInputContent(cas.getParams());
testRecord.setOutputContent(null != afterASqlResult ? afterASqlResult.toString() : null + " | " + null != afterBSqlResult ? afterBSqlResult.toString() : null);
testRecord.setState("Pass".equals(assertContent) ? 1 : 0);
testRecord.setTestVersion(DateUtils.formatDate(new Date(), "yyyyMMdd HH:mm"));
testRecord.setCreateTime(new Date());
testRecord.setUpdateTime(new Date());
testRecord.setIsDelete(0);
SqlUtils.insertTestRecord(testRecord);
// 如果失败则发送消息提醒
if (!"Pass".equals(assertContent)) {
sendAlarm(cas.getParams());
}
}
}
@DataProvider(name = "data6")
......@@ -300,4 +538,27 @@ public class ServerCase extends BaseCase {
return data;
}
public void sendAlarm(String inquiry) {
Long chatUserId = null;
String msgRemoteId = null;
String origin =null;
Long digitalPersonId =null;
Long bookId = null;
StringBuilder sb = new StringBuilder();
sb.append("案例书自动化测试结果异常告警").append("\n");
sb.append("<@").append("599").append(">\n");
sb.append("> 环境:").append(MessageBuilder.comment("prod")).append("\t").append(MessageBuilder.comment(null)).append("\n");
sb.append("> 用户输入:").append(MessageBuilder.comment(inquiry)).append("\n");
sb.append("> 消息到达的节点描述:").append("案例书_长江作业本").append("\n");
sb.append("> Time:").append(MessageBuilder.comment(MessageBuilder.format(new Date()))).append("\n");
String alarmKey = "7782a9ad-817a-4b7e-9745-b2b173ddc04a";
if (StringUtils.isNotBlank(alarmKey)) {
log.info("发送告警消息:" +sb.toString());
WeworkRobotSender.asyncSend(alarmKey, MessageBuilder.buildMarkdown(sb.toString()));
}
}
}
package com.lemon.utils;
import com.lemon.cases.BaseCase;
import com.lemon.pojo.TestRecord;
import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.ScalarHandler;
import org.apache.commons.lang3.StringUtils;
import org.apache.log4j.Logger;
import java.math.BigInteger;
import java.sql.Connection;
/**
......@@ -12,8 +16,11 @@ import java.sql.Connection;
* @author: ray
* @create: 2022-09-27 14:20
**/
public class SqlUtils {
public static Logger log = Logger.getLogger(BaseCase.class);
/**
* 传入sql语句执行sql查询,并且返回查询结果
*
......@@ -34,7 +41,7 @@ public class SqlUtils {
conn = JDBCUtils.getConnection();
// 查询操作,使用query
result = runner.query(conn, sql, new ScalarHandler<>());
System.out.println(result);
log.info("querySingle:" + sql + "rsult:"+ result);
} catch (Exception e) {
e.printStackTrace();
} finally {
......@@ -44,4 +51,67 @@ public class SqlUtils {
}
public static void insertTestRecord(TestRecord testRecord) {
/**
* public class TestRecord {
* private Long id;
* private String inquiry;
* private String inputContent;
* private String outputContent;
* private Integer state;
* private String testVersion;
* private Date createTime;
* private Date updateTime;
* private Integer isDelete;
* private Long caseId;
* private String caseDesc;
* private String expectContent;
*/
/**
* CREATE TABLE `test_record` (
* `id` bigint(20) NOT NULL COMMENT '主鍵id',
* `inquiry` varchar(255) DEFAULT NULL COMMENT '用户输入',
* `input_content` varchar(2000) DEFAULT NULL COMMENT '接口入参',
* `output_content` varchar(2000) DEFAULT NULL COMMENT '接口出参',
* `state` tinyint(4) DEFAULT NULL COMMENT '用例状态 0 未通过 1通过',
* `test_version` varchar(255) DEFAULT NULL COMMENT '测试版本',
* `create_time` datetime DEFAULT NULL COMMENT '创建时间',
* `update_time` datetime DEFAULT NULL COMMENT '更新时间',
* `is_delete` tinyint(4) DEFAULT NULL COMMENT '删除状态',
* `case_id` bigint(20) DEFAULT NULL COMMENT '案例ID',
* `case_desc` varchar(255) DEFAULT NULL COMMENT '案例描述',
* `expect_content` varchar(2000) DEFAULT NULL COMMENT '预期结果',
* PRIMARY KEY (`id`)
* ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;
*/
Connection conn = null;
try {
//创建queryRunner对象
QueryRunner runner = new QueryRunner();
//调用查询方法,传入数据库连接、SQL语句、返回值类型
conn = JDBCUtils.getConnection();
// 插入操作,使用update
String sql = "INSERT INTO test_record (inquiry, input_content, output_content, state, test_version, create_time, update_time, is_delete, case_id, case_desc, expect_content) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)";
// 插入数据并获取生成的主键
BigInteger generatedId = (BigInteger)runner.insert(conn, sql, new ScalarHandler<BigInteger>(),
testRecord.getInquiry(),
testRecord.getInputContent(),
testRecord.getOutputContent(),
testRecord.getState(),
testRecord.getTestVersion(),
testRecord.getCreateTime(),
testRecord.getUpdateTime(),
testRecord.getIsDelete(),
testRecord.getCaseId(),
testRecord.getCaseDesc(),
testRecord.getExpectContent()
);
// 设置生成的主键到 testRecord 对象
testRecord.setId(generatedId.longValue());
} catch (Exception e) {
e.printStackTrace();
} finally {
JDBCUtils.close(conn);
}
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment