Bean包文件
AsyncMonitorRequest类代码
public class AsyncMonitorRequest {
//请求方法
private String method;
//请求的ID
private String requestId;
public String getRequestId() {
return requestId;
}
public void setRequestId(String requestId) {
this.requestId = requestId;
}
public String getMethod() {
return method;
}
public void setMethod(String method) {
this.method = method;
}
}
Oauth2CerdCode用户认证代码
public class Oauth2CerdCode {
private String client_id;
private String client_secret;
private String response_type;
private String username;
private String userType;
private String systemCode;
private String nonceStr;
private String timeStamp;
private String redirect_uri;
private String loginSign;
public String getClient_id() {
return client_id;
}
public void setClient_id(String client_id) {
this.client_id = client_id;
}
public String getResponse_type() {
return response_type;
}
public void setResponse_type(String response_type) {
this.response_type = response_type;
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getUserType() {
return userType;
}
public void setUserType(String userType) {
this.userType = userType;
}
public String getSystemCode() {
return systemCode;
}
public void setSystemCode(String systemCode) {
this.systemCode = systemCode;
}
public String getNonceStr() {
return nonceStr;
}
public void setNonceStr(String nonceStr) {
this.nonceStr = nonceStr;
}
public String getTimeStamp() {
return timeStamp;
}
public void setTimeStamp(String timeStamp) {
this.timeStamp = timeStamp;
}
public String getRedirect_uri() {
return redirect_uri;
}
public void setRedirect_uri(String redirect_uri) {
this.redirect_uri = redirect_uri;
}
public String getLoginSign() {
return loginSign;
}
public void setLoginSign(String loginSign) {
this.loginSign = loginSign;
}
public String getClient_secret() {
return client_secret;
}
public void setClient_secret(String client_secret) {
this.client_secret = client_secret;
}
}
TestCase测试用例
public class TestCase {
private String caseID;// 案例编号
private String caseName;// 案例名称
private String apiName;// 接口名称
private String apiCode;// 接口号
private Map<String, String> header;// 请求头
private String contentType;// 报文格式类型
private String characterSet;// 报文字符集类型
private String requestHead;// 请求报文头(百望云接口用)
private String requestBody;// 请求报文内容
private String requestType;// 请求类型
private String securityType;// 请求加密类型,默认为securityDefault
private String requestValue;// 请求报文
private Map<String, String> requestParameter;// 请求参数
private String responseExp;// 报文返回格式
private Map<String, String> checkPoint;// 检查点预期结果
private String responseValue;// 响应报文
private String responseTime; // 响应时间
private String caseResult;// 案例执行状态
private String apiURL;// 接口URL
private String testFilePath;// 测试案例路径
private String testResultFilePath;// 测试结果文件目录
private Map<String, Object> userParameter = new HashMap<String, Object>();// 用户自定义参数
private Map<String, String> outPutParameter;// 输出参数
private long sleepTime; // 延迟执行时间,默认为0
private String status;// 运行状态,0为不运行、1为运行
private String times;// 运行次数
public String getContentType() {
return contentType;
}
public void setContentType(String contentType) {
this.contentType = contentType;
}
public String getCharacterSet() {
return characterSet;
}
public void setCharacterSet(String characterSet) {
this.characterSet = characterSet;
}
public Map<String, String> getHeader() {
return header;
}
public void setHeader(Map<String, String> header) {
this.header = header;
}
public void setSecurityType(String securityType) {
this.securityType = securityType;
}
public String getCaseID() {
return caseID;
}
public void setCaseID(String caseID) {
this.caseID = caseID;
}
public String getCaseName() {
return caseName;
}
public void setCaseName(String caseName) {
this.caseName = caseName;
}
public String getApiName() {
return apiName;
}
public void setApiName(String apiName) {
this.apiName = apiName;
}
public String getApiCode() {
return apiCode;
}
public void setApiCode(String apiCode) {
this.apiCode = apiCode;
}
public String getRequestHead() {
return requestHead;
}
public void setRequestHead(String requestHead) {
this.requestHead = requestHead;
}
public String getRequestBody() {
return requestBody;
}
public void setRequestBody(String requestBody) {
this.requestBody = requestBody;
}
public String getRequestType() {
return requestType;
}
public void setRequestType(String requestType) {
this.requestType = requestType;
}
public String getRequestValue() {
return requestValue;
}
public void setRequestValue(String requestValue) {
this.requestValue = requestValue;
}
public Map<String, String> getRequestParameter() {
return requestParameter;
}
public void setRequestParameter(Map<String, String> map) {
this.requestParameter = map;
try {
this.sleepTime = Long.valueOf((String) map.get("SLEEP"));
} catch (Exception e) {
this.sleepTime = 0;
}
}
public String getResponseExp() {
return responseExp;
}
public void setResponseExp(String responseExp) {
this.responseExp = responseExp;
}
public Map<String, String> getCheckPoint() {
return checkPoint;
}
public void setCheckPoint(Map<String, String> map) {
this.checkPoint = map;
}
public String getResponseValue() {
return responseValue;
}
public void setResponseValue(String responseValue) {
this.responseValue = responseValue;
}
public String getResponseTime() {
return responseTime;
}
public void setResponseTime(String responseTime) {
this.responseTime = responseTime;
}
public String getCaseResult() {
return caseResult;
}
public void setCaseResult(String caseResult) {
this.caseResult = caseResult;
}
public String getApiURL() {
return apiURL;
}
public void setApiURL(String apiURL) {
this.apiURL = apiURL;
}
public Map<String, Object> getUserParameter() {
return userParameter;
}
public void setUserParameter(Map<String, Object> userParameter) {
this.userParameter = userParameter;
}
public String getTestFilePath() {
return testFilePath;
}
public void setTestFilePath(String testFilePath) {
this.testFilePath = testFilePath;
}
public String getTestResultFilePath() {
return testResultFilePath;
}
public void setTestResultFilePath(String testResultFilePath) {
this.testResultFilePath = testResultFilePath;
}
public Map<String, String> getOutPutParameter() {
return outPutParameter;
}
public void setOutPutParameter(Map<String, String> outPutParameter) {
this.outPutParameter = outPutParameter;
}
public String getSecurityType() {
return securityType;
}
public long getSleepTime() {
return sleepTime;
}
public void setSleepTime(long sleepTime) {
this.sleepTime = sleepTime;
}
public String getStatus() {
return status;
}
public void setStatus(String status) {
this.status = status;
}
public String getTimes() {
return times;
}
public void setTimes(String times) {
this.times = times;
}
}
客户端包
AutoTestClient
package com.baiwang.Client;
import com.baiwang.Bean.TestCase;
public abstract class AutoTestClient {
//自动测试客户端方法
public abstract String execute(TestCase testCase);
}
Http客户端测试
HttpAutoTestClientAWT
package com.baiwang.Client;
import java.io.IOException;
import java.net.ConnectException;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Map.Entry;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.ContentType;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.util.EntityUtils;
import com.baiwang.Bean.TestCase;
public class HttpAutoTestClientAWT extends AutoTestClient {
@Override
public String execute(TestCase testCase) {
String response = null;
//如歌请求方法为Post
if (testCase.getRequestType().equals("POST")) {
response = toRunByPost(testCase);
//如歌请求方法为APIpost
} else if (testCase.getRequestType().equals("BaiwangAPI-POST")) {
response = toRunByPost2(testCase);
}
return response;
}
//post放啊返回数据
private String toRunByPost(TestCase testCase) {
String response = null;
try {
HttpClient httpclient = null;
HttpResponse httpresponse = null;
httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost(testCase.getApiURL());
Iterator<Entry<String, String>> it = testCase.getHeader().entrySet().iterator();
while (it.hasNext()) {
Entry<String, String> entry = it.next();
httppost.addHeader(entry.getKey(), entry.getValue());
}
httppost.setEntity(new StringEntity(testCase.getRequestValue(),
ContentType.create(testCase.getContentType(),
testCase.getCharacterSet())));
httpresponse = httpclient.execute(httppost);
response = EntityUtils.toString(httpresponse.getEntity());
} catch (ConnectException e) {
response = "错误:" + e.toString();
e.printStackTrace();
} catch (ClientProtocolException e) {
response = "错误:" + e.toString();
e.printStackTrace();
} catch (IOException e) {
response = "错误:" + e.toString();
e.printStackTrace();
}
return response;
}
//post2返回的结果
private String toRunByPost2(TestCase testCase) {
String apiUrl = testCase.getApiURL(); // 请求路径
String nsrsbh = (String) testCase.getRequestParameter().get("NSRSBH"); // 纳税人识别号码
String inXml = testCase.getRequestValue();// 获取请求的报文
String responseStr = null;
// System.out.println("接口请求报文===" + inXml);
@SuppressWarnings("resource")
HttpClient httpCLient = new DefaultHttpClient();
HttpPost httpPost = new HttpPost(apiUrl);
List<BasicNameValuePair> nvps = new ArrayList<>();
nvps.add(new BasicNameValuePair("METHOD", "rpc"));
nvps.add(new BasicNameValuePair("NSRSBH", nsrsbh));
nvps.add(new BasicNameValuePair("DATA", inXml));
try {
httpPost.setEntity(new UrlEncodedFormEntity(nvps, "UTF-8"));
HttpResponse response = httpCLient.execute(httpPost);
HttpEntity entity = response.getEntity();
if (entity != null) {
String resultString = EntityUtils.toString(entity, "UTF-8");
if ((resultString == null) || (resultString.trim().equals(""))) {
responseStr = "远程调用异常,返回的数据非法,请核对远程访问路径是否正常";
} else {
// System.out.println("接口返回报文===" + resultString);
responseStr = resultString.substring(resultString.indexOf("<?xml"), resultString.length());
}
}
} catch (Exception e) {
e.printStackTrace();
}
return responseStr;
}
}
Https客户端测试
package com.baiwang.Client;
import java.io.IOException;
import java.net.ConnectException;
import java.security.KeyManagementException;
import java.security.NoSuchAlgorithmException;
import java.util.Iterator;
import java.util.Map.Entry;
import org.apache.http.HttpResponse;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.ContentType;
import org.apache.http.entity.StringEntity;
import org.apache.http.util.EntityUtils;
import com.baiwang.Bean.TestCase;
public class HttpsAutoTestClientAWT extends AutoTestClient {
@Override
public String execute(TestCase testCase) {
String response = null;
if (testCase.getRequestType().equals("POST")) {
response = toRunByPost(testCase);
}
return response;
}
private String toRunByPost(TestCase testCase) {
String response = null;
try {
HttpClient httpclient = new SSLClient();
HttpResponse httpresponse = null;
HttpPost httppost = new HttpPost(testCase.getApiURL());
Iterator<Entry<String, String>> it = testCase.getHeader().entrySet().iterator();
while (it.hasNext()) {
Entry<String, String> entry = it.next();
httppost.addHeader(entry.getKey(), entry.getValue());
}
httppost.setEntity(new StringEntity(testCase.getRequestValue(), ContentType.create(testCase.getContentType(), testCase.getCharacterSet())));
httpresponse = httpclient.execute(httppost);
response = EntityUtils.toString(httpresponse.getEntity());
} catch (ConnectException e) {
response = "错误:" + e.toString();
e.printStackTrace();
} catch (ClientProtocolException e) {
response = "错误:" + e.toString();
e.printStackTrace();
} catch (IOException e) {
response = "错误:" + e.toString();
e.printStackTrace();
} catch (KeyManagementException e) {
// TODO Auto-generated catch block
response = "错误:" + e.toString();
e.printStackTrace();
} catch (NoSuchAlgorithmException e) {
// TODO Auto-generated catch block
response = "错误:" + e.toString();
e.printStackTrace();
}
return response;
}
}
package com.baiwang.Client;
import java.security.KeyManagementException;
import java.security.NoSuchAlgorithmException;
import java.security.cert.CertificateException;
import java.security.cert.X509Certificate;
import javax.net.ssl.SSLContext;
import javax.net.ssl.TrustManager;
import javax.net.ssl.X509TrustManager;
import org.apache.http.conn.;ClientConnectionManager
import org.apache.http.conn.scheme.;Scheme
import org.apache.http.conn.scheme.;SchemeRegistry
import org.apache.http.conn.ssl.;SSLSocketFactory
import org.apache.http.impl.client.;DefaultHttpClient
//用于进行Https请求的HttpClient
@SuppressWarnings("deprecation")
public class SSLClient extends {DefaultHttpClient
public SSLClient() throws NoSuchAlgorithmException, KeyManagementException {
super();
SSLContext ctx = SSLContext.getInstance("TLS");
X509TrustManager tm = new X509TrustManager() {
@Override
public void checkClientTrusted(X509Certificate[] chain, String authType) throws CertificateException {
}
@Override
public void checkServerTrusted(X509Certificate[] chain, String authType) throws CertificateException {
}
@Override
public X509Certificate[] getAcceptedIssuers() {
return null;
}
};
ctx.init(null, new TrustManager[] { tm }, null);
ssf = new SSLSocketFactory(ctx, SSLSocketFactory.SSLSocketFactory);ALLOW_ALL_HOSTNAME_VERIFIER
ccm = this.ClientConnectionManager();getConnectionManager
sr = ccm.SchemeRegistry();getSchemeRegistry
sr.(new register("https", 443, ssf));Scheme
}
}