一、封装登录后POST请求以及GET请求
/**
* 全局运行时环境参数管理器
*/
public static Map
/**
* 全局测试结果数据收集器
*/
public static JSONArray BASE_URI_COLLECTOR;
/**
* 全局请求处理器
*/
public static UcRESTTemplate BASE_REST;
/**
* 运行时日志文件路径
*/
public static String RUNTIM_FILE_PATH = “”;
public static SalesRequestRunTimeVo lendRequest;
public static CollectionRequestRunTimeVo collectionRequest;
import com.uc.qa.utils.encrypt.SJS;
import com.uc.qa.utils.map.TreeMap2MultiValueMap;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.ResponseEntity;
import org.springframework.util.MultiValueMap;
import org.testng.Reporter;
import java.util.TreeMap;
import static com.uc.qa.cases.assistant.BaseParameters.*;
public class BaseRunner {
final static Logger logger = LoggerFactory.getLogger(BaseParameters.class);
/**
* 根据产品服务请求规则处理请求前的UTI
* @param map 原始请求参数
* @return MultiValueMap 处理后的请求报文体
*/
public static TreeMap dealWithUri(TreeMap map){
//secret(根秘钥)已在Login中获取到参数,BASE_GLOBAL_MAP为全局参数收集器
if (BASE_GLOBAL_MAP.get(“product”).equalsIgnoreCase(“当前系统名字”) && null != BASE_GLOBAL_MAP.get(“secret”)){
try {
//SJS在上一篇文章中已说明
map.put(“sign”, new SJS().getSJSInfo(map, BASE_GLOBAL_MAP.get(“secret”)));
} catch (Exception e) {
logger.error(“当前执行产品【当前系统名字】请求加密过程处理失败”);
e.printStackTrace();
}
}
return map;
}
/**
* 发送 Rest Template 请求
* @param map 发送Rest请求前的UTI
* @param uri 发送Rest请求的目的接口
* @return ResponseEntity响应报文体
*/
public static ResponseEntity dealWithPost(TreeMap map, String uri) {
TreeMap2MultiValueMap treeMap2MultiValueMap = new TreeMap2MultiValueMap();
MultiValueMap body = treeMap2MultiValueMap.getMultiValueMap(dealWithUri(map));
//BASE_URI_CLLOCTOR为全局测试结果收集器
BASE_URI_COLLECTOR.add(BASE_GLOBAL_MAP.get(uri));
Reporter.log("Request URL is : " + BASE_GLOBAL_MAP.get(“server”) + BASE_GLOBAL_MAP.get(uri), true);
Reporter.log("Post Request Body is : " + body.toSingleValueMap().toString(), true);
final HttpEntity> request = new HttpEntity<>(body);
//server为域名,uri为接口名
ResponseEntity responseEntity = BASE_REST.postEntity(BASE_GLOBAL_MAP.get(“server”) + BASE_GLOBAL_MAP.get(uri), request);
Reporter.log("Get Response : " + responseEntity.toString(), true);
return responseEntity;
}
/**
* 发送 Rest Template Get 请求
* @param map 发送Rest请求前的UTI
* @param uri 发送Rest请求的目的接口
* @return ResponseEntity响应报文体
*/
public static ResponseEntity dealGetWithHeader(TreeMap map, String uri) {
MultiValueMap body = (new TreeMap2MultiValueMap()).getMultiValueMap(map);
BASE_URI_COLLECTOR.add(BASE_GLOBAL_MAP.get(uri));
Reporter.log("Request URL is : " + BASE_GLOBAL_MAP.get("server") + BASE_GLOBAL_MAP.get(uri), true);
Reporter.log("Post Request Body is : " + body.toSingleValueMap().toString(), true);
HttpHeaders headers = new HttpHeaders();
headers.set("Authorization", "Bearer " + collectionRequest.getToken());
final HttpEntity> request = new HttpEntity<>(body, headers);
ResponseEntity responseEntity = BASE_REST.getEntity(BASE_GLOBAL_MAP.get("server") + BASE_GLOBAL_MAP.get(uri), request, map);
Reporter.log("Get Response : " + responseEntity.toString(), true);
return responseEntity;
}
/**
* 发送 Rest Template Post 请求
* @param map 发送Rest请求前的UTI
* @param uri 发送Rest请求的目的接口
* @return ResponseEntity响应报文体
*/
public static ResponseEntity dealPostWithHeader(TreeMap map, String uri) {
TreeMap2MultiValueMap treeMap2MultiValueMap = new TreeMap2MultiValueMap();
MultiValueMap body = treeMap2MultiValueMap.getMultiValueMap(map);
BASE_URI_COLLECTOR.add(BASE_GLOBAL_MAP.get(uri));
Reporter.log("Request URL is : " + BASE_GLOBAL_MAP.get("server") + BASE_GLOBAL_MAP.get(uri), true);
Reporter.log("Post Request Body is : " + body.toSingleValueMap().toString(), true);
HttpHeaders headers = new HttpHeaders();
headers.set("Authorization", "Bearer " + collectionRequest.getToken());
final HttpEntity> request = new HttpEntity<>(body, headers);
ResponseEntity responseEntity = BASE_REST.postEntity(BASE_GLOBAL_MAP.get("server") + BASE_GLOBAL_MAP.get(uri), request);
Reporter.log("Get Response : " + responseEntity.toString(), true);
return responseEntity;
}
}