提交 22e81adc authored 作者: 黄夏豪's avatar 黄夏豪

refactor(base): 补充了异常情况的处理

上级 047c7faf
...@@ -5,6 +5,7 @@ import lombok.AllArgsConstructor; ...@@ -5,6 +5,7 @@ import lombok.AllArgsConstructor;
import lombok.Data; import lombok.Data;
import lombok.NoArgsConstructor; import lombok.NoArgsConstructor;
import org.apache.http.client.methods.CloseableHttpResponse; import org.apache.http.client.methods.CloseableHttpResponse;
import org.springframework.http.HttpStatus;
@Data @Data
@AllArgsConstructor @AllArgsConstructor
...@@ -18,7 +19,7 @@ public class HttpResponseDetail { ...@@ -18,7 +19,7 @@ public class HttpResponseDetail {
private String responseBody = "error request"; private String responseBody = "error request";
@ApiModelProperty("响应状态,例如:200、401、500") @ApiModelProperty("响应状态,例如:200、401、500")
private Integer statusCode = 500; private HttpStatus statusCode = HttpStatus.INTERNAL_SERVER_ERROR;
@ApiModelProperty("响应时间,单位为 ms ") @ApiModelProperty("响应时间,单位为 ms ")
private Long responseTime = 0l; private Long responseTime = 0l;
......
package org.matrix.exception;
/**
* @author huangxiahao
*/
public class CheckPointException extends RuntimeException {
public CheckPointException(String message){
super(message);
}
}
package org.matrix.exception;
/**
* @author huangxiahao
*/
public class HttpRequestException extends RuntimeException {
public HttpRequestException(String message){
super(message);
}
}
...@@ -13,6 +13,7 @@ import org.apache.commons.io.IOUtils; ...@@ -13,6 +13,7 @@ import org.apache.commons.io.IOUtils;
import org.apache.commons.lang3.StringUtils; import org.apache.commons.lang3.StringUtils;
import org.matrix.entity.checkPoint.*; import org.matrix.entity.checkPoint.*;
import org.matrix.entity.httpRequest.HttpResponseDetail; import org.matrix.entity.httpRequest.HttpResponseDetail;
import org.matrix.exception.CheckPointException;
import org.springframework.core.io.ClassPathResource; import org.springframework.core.io.ClassPathResource;
import org.springframework.http.HttpStatus; import org.springframework.http.HttpStatus;
...@@ -28,7 +29,7 @@ import java.util.regex.Pattern; ...@@ -28,7 +29,7 @@ import java.util.regex.Pattern;
* 数据检测点工具 * 数据检测点工具
* todo 打印LOG * todo 打印LOG
* *
* @author Administrator * @author huangxiahao
*/ */
public class CheckPointUtil { public class CheckPointUtil {
...@@ -39,8 +40,7 @@ public class CheckPointUtil { ...@@ -39,8 +40,7 @@ public class CheckPointUtil {
try { try {
this.baseJs = IOUtils.toString(cpr.getInputStream(), StandardCharsets.UTF_8); this.baseJs = IOUtils.toString(cpr.getInputStream(), StandardCharsets.UTF_8);
} catch (IOException e) { } catch (IOException e) {
//todo 抛出异常 初始JS加载失败 throw new CheckPointException("初始JS加载失败");
e.printStackTrace();
} }
} }
...@@ -59,7 +59,6 @@ public class CheckPointUtil { ...@@ -59,7 +59,6 @@ public class CheckPointUtil {
for (ContainCheckPoint containCheckPoint : checkPoint.getContainCheckPoints()) { for (ContainCheckPoint containCheckPoint : checkPoint.getContainCheckPoints()) {
checkPointResult.addCheckPointResultDetail(containCheck(containCheckPoint, httpResponseDetail.getResponseBody())); checkPointResult.addCheckPointResultDetail(containCheck(containCheckPoint, httpResponseDetail.getResponseBody()));
} }
//todo 数据库参数检测
//JsonPath检查点检测 //JsonPath检查点检测
if (checkPoint.getJsonPathCheckPoints().size() > 0) { if (checkPoint.getJsonPathCheckPoints().size() > 0) {
Object jsonObject = Configuration.defaultConfiguration().jsonProvider().parse(httpResponseDetail.getResponseBody()); Object jsonObject = Configuration.defaultConfiguration().jsonProvider().parse(httpResponseDetail.getResponseBody());
...@@ -106,13 +105,8 @@ public class CheckPointUtil { ...@@ -106,13 +105,8 @@ public class CheckPointUtil {
); );
} }
} catch (ScriptException e) { } catch (ScriptException e) {
//todo 抛出异常初始化js引擎失败 throw new CheckPointException("初始化js引擎失败");
e.printStackTrace();
} }
return new CheckPointResultDetail(
false,
"JsonPath检查点,检查未通过,空的表达式"
);
} }
public ScriptEngine getScriptEngine() throws ScriptException { public ScriptEngine getScriptEngine() throws ScriptException {
...@@ -136,39 +130,6 @@ public class CheckPointUtil { ...@@ -136,39 +130,6 @@ public class CheckPointUtil {
return result; return result;
} }
public static void main(String[] args) {
String json = "{\n" +
" \"school\": [\n" +
" {\n" +
" \"className\": \"一班\",\n" +
" \"student\":[\n" +
" \"一班的张三\",\"一班的李四\",\"一班的王五\",\"一班的赵六\",\"一班的田七\"\n" +
" ]\n" +
" },\n" +
" {\n" +
" \"className\": \"二班\",\n" +
" \"student\":[\n" +
" \"二班的张三\",\"二班的李四\",\"二班的王五\",\"二班的赵六\",\"二班的田七\"\n" +
" ]\n" +
" },\n" +
" {\n" +
" \"className\": \"三班\",\n" +
" \"student\":[\n" +
" \"三班的张三\",\"三班的李四\",\"三班的王五\",\"三班的赵六\",\"三班的田七\"\n" +
" ]\n" +
" }\n" +
" ]\n" +
"}";
Object jsonObject = Configuration.defaultConfiguration().jsonProvider().parse(json);
CheckPointUtil checkPointUtil = new CheckPointUtil("baseJS.js");
CheckPointResultDetail checkPointResultDetail = checkPointUtil.jsonPathCheck(new JsonPathCheckPoint(
"contains({$..className},\"一班\")"
),
jsonObject);
System.out.println(checkPointResultDetail);
}
public CheckPointResultDetail containCheck(ContainCheckPoint containCheckPoint, String responseBody) { public CheckPointResultDetail containCheck(ContainCheckPoint containCheckPoint, String responseBody) {
if (containCheckPoint.getIsContain()) { if (containCheckPoint.getIsContain()) {
if (responseBody.contains(containCheckPoint.getValue())) { if (responseBody.contains(containCheckPoint.getValue())) {
...@@ -222,14 +183,14 @@ public class CheckPointUtil { ...@@ -222,14 +183,14 @@ public class CheckPointUtil {
} else { } else {
return new CheckPointResultDetail( return new CheckPointResultDetail(
true, true,
"异常检查点,检查通过" "不为空检查点,检查通过"
); );
} }
} }
public CheckPointResultDetail exceptionCheck(HttpResponseDetail httpResponseDetail) { public CheckPointResultDetail exceptionCheck(HttpResponseDetail httpResponseDetail) {
if (httpResponseDetail.getStatusCode() == HttpStatus.OK.value()) { if (httpResponseDetail.getStatusCode().value() == HttpStatus.OK.value()) {
return new CheckPointResultDetail( return new CheckPointResultDetail(
true, true,
"异常检查点,检查通过" "异常检查点,检查通过"
......
...@@ -19,7 +19,9 @@ import org.apache.http.impl.client.CloseableHttpClient; ...@@ -19,7 +19,9 @@ import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.message.BasicNameValuePair; import org.apache.http.message.BasicNameValuePair;
import org.apache.http.util.EntityUtils; import org.apache.http.util.EntityUtils;
import org.matrix.entity.httpRequest.*; import org.matrix.entity.httpRequest.*;
import org.matrix.exception.HttpRequestException;
import org.springframework.http.HttpMethod; import org.springframework.http.HttpMethod;
import org.springframework.http.HttpStatus;
import java.io.File; import java.io.File;
import java.io.FileInputStream; import java.io.FileInputStream;
...@@ -32,12 +34,10 @@ import java.security.NoSuchAlgorithmException; ...@@ -32,12 +34,10 @@ import java.security.NoSuchAlgorithmException;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Date; import java.util.Date;
import java.util.List; import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
/** /**
* HttpClient调用封装 * HttpClient调用封装
* * todo 打印LOG
* @author HuangXiahao * @author HuangXiahao
**/ **/
public class HttpClientUtil { public class HttpClientUtil {
...@@ -98,12 +98,11 @@ public class HttpClientUtil { ...@@ -98,12 +98,11 @@ public class HttpClientUtil {
return new HttpResponseDetail( return new HttpResponseDetail(
response, response,
EntityUtils.toString(response.getEntity(), "UTF-8"), EntityUtils.toString(response.getEntity(), "UTF-8"),
response.getStatusLine().getStatusCode(), HttpStatus.valueOf(response.getStatusLine().getStatusCode()),
endTime.getTime() - startTime.getTime() endTime.getTime() - startTime.getTime()
); );
} catch (IOException e) { } catch (IOException e) {
e.printStackTrace(); throw new HttpRequestException(String.format("解析返回值失败,本次请求详细参数如下: %s ",httpRequestDetail));
return null;
} finally { } finally {
//关闭请求request //关闭请求request
closeRequest(requestBase); closeRequest(requestBase);
...@@ -202,28 +201,25 @@ public class HttpClientUtil { ...@@ -202,28 +201,25 @@ public class HttpClientUtil {
public String initUrlString(HttpRequestDetail httpRequestDetail) { public String initUrlString(HttpRequestDetail httpRequestDetail) {
String url = httpRequestDetail.getUrl(); String url = httpRequestDetail.getUrl();
try { try {
switch (httpRequestDetail.getRequestType()) { if (httpRequestDetail.getRequestType() == HttpRequestType.QUERY) {
case QUERY: if (httpRequestDetail.getMethod().equals(HttpMethod.GET)) {
if (httpRequestDetail.getMethod().equals(HttpMethod.GET)) { URIBuilder uriBuilder;
URIBuilder uriBuilder ; uriBuilder = new URIBuilder(url);
uriBuilder = new URIBuilder(url); for (RequestBody requestBody : httpRequestDetail.getRequestBodies()) {
for (RequestBody requestBody : httpRequestDetail.getRequestBodies()) { switch (requestBody.getType()) {
switch (requestBody.getType()) { case TEXT:
case TEXT: uriBuilder.setParameter(requestBody.getKey(), requestBody.getValue());
uriBuilder.setParameter(requestBody.getKey(), requestBody.getValue()); break;
break; case FILE:
case FILE: throw new NullPointerException("QUERY请求不能传入文件流");
throw new NullPointerException("QUERY请求不能传入文件流"); default:
default:
}
} }
url = uriBuilder.build().toString();
} }
break; url = uriBuilder.build().toString();
default: }
} }
} catch (URISyntaxException e) { } catch (URISyntaxException e) {
e.printStackTrace(); throw new HttpRequestException(String.format("URL格式不正确,不正确的URL为: %s",url));
} }
return url; return url;
} }
...@@ -269,6 +265,7 @@ public class HttpClientUtil { ...@@ -269,6 +265,7 @@ public class HttpClientUtil {
response.close(); response.close();
} catch (IOException e) { } catch (IOException e) {
e.printStackTrace(); e.printStackTrace();
throw new HttpRequestException(String.format("关闭返回流失败"));
} }
} }
} }
......
package org.matrix; package org.matrix;
import static org.junit.Assert.assertTrue;
import org.junit.Test; import org.junit.jupiter.api.Test;
import static org.junit.jupiter.api.Assertions.assertTrue;
/** /**
* Unit test for simple App. * Unit test for simple App.
......
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论