推荐算法接口测试
虽然代码只加了直播类,但是我的代码对其他类型做了判断。算是功能回归
package baiduapi.serverTest.xx;
import baiduapi.baseDeal.fileOperate.FileWrite;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
class rusRespAssert直播 {
private static Logger logger = LoggerFactory.getLogger(rusRespAssert直播.class);
/**
* @param count
* @param request_RECOMMEND
* @param response
* @return 0
*/
// 非沉浸页调用此方法,不传category
// 方法重载 参数个数和参数类型不一样。调用下面的方法也行
public static int responseToParse(int count, String dumisid, JSONObject request_RECOMMEND, JSONObject response) {
// 可以写成return 也可以写成int i
return responseToParse(count, dumisid, null, request_RECOMMEND, response);
}
// 非沉浸页调用此方法,不传category
// 方法的调用。上面重载,这个是方法调用方法
public static int responseToParse非沉浸(int count, String dumisid, JSONObject request_RECOMMEND, JSONObject response) {
int i = responseToParse(count, dumisid, null, request_RECOMMEND, response);
return i;
}
// 沉浸页调用此方法,传category
public static int responseToParse(int count, String dumisid, String categoryReq, JSONObject request_RECOMMEND, JSONObject response) {
try {
// String baseParamPrint = "request_RECOMMEND为==【" + request_RECOMMEND + "】,";
String baseParamPrint = "。";
String bug = "BUG BUG BUG !!!第" + (count + 1) + "个请求,小流量id为=" + dumisid + ",category为=" + categoryReq + baseParamPrint;
String pass = "PASS PASS PASS ~ ~ ~ 第" + (count + 1) + "个请求,小流量id为=" + dumisid + ",category为=" + categoryReq + baseParamPrint;
String request = "第" + (count + 1) + "个请求,小流量id为=" + dumisid + baseParamPrint;
String notExist = "NotExist NotExist NotExist ~ ~ ~ 第" + (count + 1) + "个请求,接口返回中不存在" + baseParamPrint;
// 接口返回不为空判断
if (response != null) {
/**
* request.recommends.subCategories.category为"RECOMMEND"
* 2020年9月14日新增case
*/
// session校验
if (response.containsKey("session")) {
String session = response.getString("session");
System.out.println("session==" + session);
if (!session.equals("{}")) {
System.err.println(bug + "session不为0,实际为:" + session);
} else {
System.out.println(pass + "session为{}");
}
} else {
System.err.println(notExist + "session");
}
// version校验
if (response.containsKey("version")) {
String version = response.getString("version");
// System.out.println("version==" + version);
if (!version.equals("2.0.0")) {
System.err.println(bug + "version不为2.0.0,实际为:" + version);
} else {
System.out.println(pass + "version为2.0.0");
}
} else {
System.err.println(notExist + "version");
}
// response校验,recommendItems数组校验,每个类型读取服务器配置文件
if (response.containsKey("response")) {
JSONObject response_value = response.getJSONObject("response");
// System.out.println("response接口返回字段==" + response_value);
JSONArray resources = response_value.getJSONArray("resources");
if (!resources.isEmpty()) {
JSONArray recommendItems = resources.getJSONObject(0).getJSONObject("payload").getJSONArray("recommendItems");
int size = recommendItems.size();
System.out.println("recommendItems JSONArray数组长度==" + size);
if (size < 100) {
System.out.println(pass + "recommendItems的长度小于100,实际为:" + size);
int fcount = 0;
int count_RECOMMEND = 0;
for (int i = 0; i < size; i++) {
String array = "第" + count_RECOMMEND + "个数组:";
JSONObject data = recommendItems.getJSONObject(i).getJSONObject("data");
String category = data.getString("category");
// System.out.println("category=="+category);
if (!category.isEmpty() && category.equals("LIVE_BROADCAST")) {
System.out.println(pass + array + "category==" + category);
fcount++;
} else {
// System.err.println(bug + array + "category为==" + category);
}
// System.err.println("subCategory=RECOMMEND时,count_RECOMMEND个数为==" + count_RECOMMEND);
count_RECOMMEND++;
}
System.out.println(request + "数组长度==" + size + ",category为LIVE_BROADCAST的个数fcount为==" + fcount);
// 1127需求要求返回的直播数目为6,大于或者小于都不行
if (fcount < 6) {
if (categoryReq.matches("NEWS|VOD|SHORT_VIDEO")) {
System.err.println(pass + "数组长度==" + size + ",category为LIVE_BROADCAST的个数小于6,符合预期!!!fcount==" + fcount);
} else {
System.err.println(bug + "数组长度==" + size + ",category为LIVE_BROADCAST的个数小于6,不符合预期!!!fcount==" + fcount);
}
} else if (fcount == 6) {
System.err.println(pass + "数组长度==" + size + ",category为LIVE_BROADCAST的个数等于6,符合预期!!!fcount==" + fcount);
FileWrite.originLog(pass + "数组长度==" + size + ",category为LIVE_BROADCAST的个数等于6,符合预期!!!" + "fcount==" + fcount);
} else if (fcount > 6) {
System.err.println(bug + "数组长度==" + size + ",category为LIVE_BROADCAST的个数大于6,不符合预期!!!fcount==" + fcount);
}
}
// 用于写入正确日志文件originLog,再做字段拆分,从而写入数据库,记录读取的log日志
FileWrite.originLog(pass + "校验完毕================分隔符=================");
// 用于日志文件回行展示,提高可读性
FileWrite.originLog(" ");
FileWrite.originLog(" ");
FileWrite.originLog(" ");
System.out.println(pass + "校验完毕================分隔符=================");
//
} else {
System.err.println(bug + "resources为空");
}
} else {
System.err.println(notExist + "接口返回字段response");
}
System.err.println("Finished~ RUS接口所有字段校验通过(json长度、value值)~");
System.err.println("----------------------------------------");
} else {
System.err.println("FATAL FATAL FATAL!!!请关注!!!第【" + (count + 1) + "】个请求," +
"rec_type为【" + request_RECOMMEND + "】,接口返回为空");
}
} catch (Exception e) {
e.printStackTrace();
}
return 0;
}
}