import org.springframework.amqp.core.Message; //导入方法依赖的package包/类
@Override
public void onMessage(Message message) {
if (null == message.getBody() || message.getBody().length < 1)
return;
JSONObject jsonObject = JSONObject.parseObject(new String(message.getBody()));
if (null == jsonObject)
return;
Long currentTimeMillis = System.currentTimeMillis();
Project project = projectService.findProjectById(jsonObject.getLong("pId"));
if (null == project)
return;
JSONArray headerJsonArray = JSONObject.parseObject(jsonObject.getString("headers")).getJSONArray("headers");
HashMap headersMap = null;
if (null != headerJsonArray && headerJsonArray.size() > 0) {
headersMap = getParamsOrHeadersMap(headerJsonArray);
}
List functionList = functionService.selectByProjectId(project.getId());
RequestDto requestDto;
powerapi.entity.AutoTest autoBean = new powerapi.entity.AutoTest();
autoBean.setTitle("#" + jsonObject.getString("title") + "-" + project.getTitle());
autoBean.setUserId(jsonObject.getLong("uId"));
autoBean.setTotalcount(functionList.size());
autoBean.setpId(project.getId());
autoTestService.insert(autoBean);
int errorcount = 0;
for (Function function : functionList) {
requestDto = new RequestDto();
JSONObject paramObject = JSONObject.parseObject(function.getParams());
requestDto.setMethod(function.getMethod());
requestDto.setUrl(project.getHostUrl() + function.getModule().getUrl() + function.getUrl());
/**
* 解析header
* 解析参数
*
*/
HashMap paramsMap = getParamsOrHeadersMap(paramObject.getJSONArray("params"));
/**
* 提交测试
*/
UnitTest unitTest = HttpUtil.doRequest(requestDto, paramsMap, headersMap);
if (unitTest.getResponseCode() != 200) {
errorcount++;
}
unitTest.setUserId(jsonObject.getLong("uId"));
unitTest.setFunctionId(function.getId());
unitTest.setParams(function.getParams());
unitTest.setAutoId(autoBean.getId());
unitTestService.insert(unitTest);
}
autoBean.setTotaltime((int) (System.currentTimeMillis() - currentTimeMillis));
autoBean.setErrorcount(errorcount);
autoBean.setStatus(1);
autoTestService.updateById(autoBean);
}