Commit eb27ae45 authored by xie.qin's avatar xie.qin

Bug fixed for supporting more sanity tests.

parent d88f2d46
......@@ -343,10 +343,10 @@ public class OpenApiParser {
}
else if (eachRespNodeSchemaValue.has("type") && eachRespNodeSchemaValue.get("type").asText().equalsIgnoreCase("object") && eachRespNodeSchemaValue.has("additionalProperties")) {
if (eachRespNodeSchemaValue.get("additionalProperties").has("format")){
respJsonBody.put("type", "array.object." + eachRespNodeSchemaValue.get("additionalProperties").get("format").asText().toLowerCase());
respJsonBody.put("type", "object." + eachRespNodeSchemaValue.get("additionalProperties").get("format").asText().toLowerCase());
}
else {
respJsonBody.put("type", "array.object." + eachRespNodeSchemaValue.get("type").asText().toLowerCase());
respJsonBody.put("type", "object." + eachRespNodeSchemaValue.get("type").asText().toLowerCase());
}
}
else{
......@@ -589,7 +589,8 @@ public class OpenApiParser {
if (refField.equals(refKey)){
Map<String, String> tmpObj = new HashMap<>();
reqJsonBody.put(property.getKey(), tmpObj);
return reqJsonBody;
//return reqJsonBody;
break;
}
if (definitionNode.has(refField) && definitionNode.get(refField).has("properties")){
Map<String, Object> subReqJsonBody = translatePropertyToJsonBody(refField, definitionNode.get(refField).get("properties"), definitionNode);
......@@ -661,7 +662,8 @@ public class OpenApiParser {
if (refField.equals(refKey)){
List<String> tmpArr = new ArrayList<>();
reqJsonBody.put(property.getKey(), tmpArr);
return reqJsonBody;
//return reqJsonBody;
break;
}
if (definitionNode.has(refField) && definitionNode.get(refField).has("properties")){
Map<String, Object> subReqJsonBody = translatePropertyToJsonBody(refField, definitionNode.get(refField).get("properties"), definitionNode);
......@@ -684,7 +686,8 @@ public class OpenApiParser {
if (refField.equals(refKey)){
List<String> tmpArr = new ArrayList<>();
reqJsonBody.put(property.getKey(), tmpArr);
return reqJsonBody;
//return reqJsonBody;
break;
}
if (definitionNode.has(refField) && definitionNode.get(refField).has("properties")){
Map<String, Object> subReqJsonBody = translatePropertyToJsonBody(refField, definitionNode.get(refField).get("properties"), definitionNode);
......@@ -722,7 +725,8 @@ public class OpenApiParser {
String refField = property.getValue().get("additionalProperties").get("originalRef").asText();
if (refField.equals(refKey)){
respJsonBody.put(property.getKey(), "object");
return respJsonBody;
//return respJsonBody;
break;
}
if (definitionNode.has(refField) && definitionNode.get(refField).has("properties")){
Map<String, Object> objRespJsonBody = produceExpectedResponseBody(refField, definitionNode.get(refField), definitionNode);
......@@ -759,7 +763,8 @@ public class OpenApiParser {
String refField = property.getValue().get("items").get("originalRef").asText();
if (refField.equals(refKey)){
respJsonBody.put(property.getKey(), "array");
return respJsonBody;
//return respJsonBody;
break;
}
if (definitionNode.has(refField) && definitionNode.get(refField).has("properties")){
List<Map<String, Object>> tmpArrayObject = new ArrayList<>();
......
......@@ -17,6 +17,7 @@ import io.restassured.path.json.exception.JsonPathException;
import io.restassured.response.Response;
import io.restassured.specification.MultiPartSpecification;
import lombok.extern.slf4j.Slf4j;
import org.aopalliance.aop.AspectException;
import org.apache.http.client.config.RequestConfig;
import org.apache.http.impl.client.HttpClientBuilder;
import org.springframework.beans.factory.annotation.Autowired;
......@@ -166,12 +167,19 @@ public class RestfulMessageEventListener {
} else {
if (restfulMessageEntity.getReqJsonBody() != null) {
if (restfulMessageEntity.getContentType().equals(ContentType.MULTIPART)) {
MultiPartSpecification multiPartSpecBuilder = null;
if (restfulMessageEntity.getReqJsonBody().get("file") != null) {
String filePathName = restfulMessageEntity.getReqJsonBody().get("file").toString();
restfulMessageEntity.getReqJsonBody().remove("file");
MultiPartSpecification multiPartSpecBuilder = new MultiPartSpecBuilder(restfulMessageEntity.getReqJsonBody(), ObjectMapperType.JACKSON_2)
multiPartSpecBuilder = new MultiPartSpecBuilder(restfulMessageEntity.getReqJsonBody(), ObjectMapperType.JACKSON_2)
.fileName(filePathName)
.build();
}
else {
multiPartSpecBuilder = new MultiPartSpecBuilder(restfulMessageEntity.getReqJsonBody(), ObjectMapperType.JACKSON_2)
.build();
}
response =
given()
.filter(new AllureRestAssured())
......@@ -187,8 +195,7 @@ public class RestfulMessageEventListener {
//.statusCode(restfulMessageEntity.getStatusCode())
.extract()
.response();
final String binaryStrPath = "src" + File.separatorChar + "test" + File.separatorChar + "resources" + File.separatorChar + "testdatacollection" + File.separatorChar + "common" + File.separatorChar + globalProperties.getLatestchain33version() + ".tar.gz";
restfulMessageEntity.getReqJsonBody().put("file", binaryStrPath);
} else {
response =
given()
......@@ -252,17 +259,17 @@ public class RestfulMessageEventListener {
}
}
catch (Exception ex){
log.error("HTTP1.x REST message send failed as error: {}", ex.toString());
ex.printStackTrace();
throwAssertionErrorInFeatureTest(restfulMessageEntity, ex.toString());
log.error("HTTP1.x REST message [{}] send failed as error: {}", tmpTarget, ex);
//ex.printStackTrace();
throwExceptionInFeatureTest(restfulMessageEntity, ex.toString());
return;
}
try {
log.info("received the response, the status_code is: {}, response json_body is: {}", response.getStatusCode(), response.getBody().asString());
}
catch (JsonPathException jsonPathException){
log.error("Failed to parse json_body from response as error {}", jsonPathException.toString());
throwAssertionErrorInFeatureTest(restfulMessageEntity, jsonPathException.toString());
log.error("HTTP1.x REST message [{}] failed to parse json_body from response as error {}", tmpTarget, jsonPathException);
throwExceptionInFeatureTest(restfulMessageEntity, jsonPathException.toString());
return;
}
//response format: {code: "baas.err.success", data: {}, message: "OK", status: "OK"}
......@@ -275,13 +282,13 @@ public class RestfulMessageEventListener {
assertThat("status code in http response is not the expectation.", respCode, equalTo("baas.err.success"));
}
catch (AssertionError err){
log.error("HTTP1.x REST message [{}] failed to verify status_code. Received [{}], but expected [{}]", tmpTarget, response.getStatusCode(), restfulMessageEntity.getStatusCode());
throwAssertionErrorInFeatureTest(restfulMessageEntity, err.toString());
log.warn("HTTP1.x REST message [{}] failed to verify status_code. Received [{}], but expected [{}]", tmpTarget, response.getStatusCode(), restfulMessageEntity.getStatusCode());
throwExceptionInFeatureTest(restfulMessageEntity, err);
return;
}
catch (JsonPathException jsonPathException){
log.error("Failed to parse json_body from response as error {}", jsonPathException.toString());
throwAssertionErrorInFeatureTest(restfulMessageEntity, jsonPathException.toString());
log.error("HTTP1.x REST message [{}] failed to parse json_body from response as error {}", tmpTarget, jsonPathException);
throwExceptionInFeatureTest(restfulMessageEntity, jsonPathException.toString());
return;
}
//response json body only match key, not value
......@@ -300,8 +307,8 @@ public class RestfulMessageEventListener {
}
}
catch (AssertionError err){
log.error("HTTP1.x REST message [{}] failed to verify response_body. Received is [{}], but expected is not.", tmpTarget, jsonPathEvaluator.get("data"));
throwAssertionErrorInFeatureTest(restfulMessageEntity, err.toString());
log.error("HTTP1.x REST message [{}] failed to verify response_body. Error is {}.", tmpTarget, err);
throwExceptionInFeatureTest(restfulMessageEntity, err);
return;
}
......@@ -319,8 +326,8 @@ public class RestfulMessageEventListener {
matchKeyAndDataTypeInRespWithExpectation(objRespBody, restfulMessageEntity.getRespJsonBody());
}
catch (AssertionError error){
log.error("HTTP1.x REST message [{}] failed to verify response_body. Received is [{}], but expected is [{}].", tmpTarget, objRespBody, restfulMessageEntity.getRespJsonBody());
throwAssertionErrorInFeatureTest(restfulMessageEntity, error.toString());
log.error("HTTP1.x REST message [{}] failed to verify response_body. Received is {}, but expected is {}.", tmpTarget, objRespBody, restfulMessageEntity.getRespJsonBody());
throwExceptionInFeatureTest(restfulMessageEntity, error);
return;
}
}
......@@ -356,14 +363,14 @@ public class RestfulMessageEventListener {
matchDataTypeInRespWithExpectation(arrRespBody.get(0), expectedDataType);
}
catch (AssertionError error){
log.error("HTTP1.x REST message [{}] failed to verify data type in response_body array. Received is [{}], but expected is [{}].", tmpTarget, arrRespBody.get(0), restfulMessageEntity.getRespJsonBody().get("type").toString());
throwAssertionErrorInFeatureTest(restfulMessageEntity, error.toString());
log.error("HTTP1.x REST message [{}] failed to verify data type in response_body array. Received is {}, but expected is {}.", tmpTarget, arrRespBody.get(0), restfulMessageEntity.getRespJsonBody().get("type").toString());
throwExceptionInFeatureTest(restfulMessageEntity, error);
return;
}
}
catch (AssertionError error){
log.error("HTTP1.x REST message [{}] failed to verify array type response_body. Received is [{}], but expected is [{}].", tmpTarget, jsonPathEvaluator.get("data"), restfulMessageEntity);
throwAssertionErrorInFeatureTest(restfulMessageEntity, error.toString());
log.error("HTTP1.x REST message [{}] failed to verify array type response_body. Received is {}, but expected is {}.", tmpTarget, jsonPathEvaluator.get("data"), restfulMessageEntity);
throwExceptionInFeatureTest(restfulMessageEntity, error);
return;
}
}catch (ClassCastException ex){
......@@ -379,8 +386,8 @@ public class RestfulMessageEventListener {
}
}
catch (AssertionError error){
log.error("HTTP1.x REST message [{}] failed to verify data type of response_body. Received is [{}], but expected is [{}].", tmpTarget, jsonPathEvaluator.get("data"), restfulMessageEntity);
throwAssertionErrorInFeatureTest(restfulMessageEntity, error.toString());
log.error("HTTP1.x REST message [{}] failed to verify data type of response_body. Received is {}, but expected is {}.", tmpTarget, jsonPathEvaluator.get("data"), restfulMessageEntity);
throwExceptionInFeatureTest(restfulMessageEntity, error);
return;
}
}
......@@ -396,6 +403,10 @@ public class RestfulMessageEventListener {
//how to match: traverse expectedRespBody and found corresponding key in recvRespBody
for (Map.Entry<String, Object> entry : expectedRespBody.entrySet()) {
log.debug("traversing expectedRespBody now, the key is [{}]", entry.getKey());
if (expectedRespBody.size() == 1 && entry.getKey().equalsIgnoreCase("type")){
matchDataTypeInRespWithExpectation(recvRespBody, expectedRespBody.get("type").toString());
return;
}
//to match key
assertThat("Cannot found out same key in Response Body.", true, equalTo(recvRespBody.containsKey(entry.getKey())));
//to match type of value
......@@ -409,17 +420,29 @@ public class RestfulMessageEventListener {
Map<String, Object> matchExpected = expected.get(0);
matchKeyAndDataTypeInRespWithExpectation(matchRecv, matchExpected);
} else if (entry.getValue().getClass().isArray()) {
assertThat("Received data type is not array", recvRespBody.get(entry.getKey()).getClass().isArray(), equalTo(true));
if (Array.getLength(recvRespBody.get(entry.getKey())) > 0) {
int tmpArrayLength = Array.getLength(recvRespBody.get(entry.getKey()));
matchDataTypeInRespWithExpectation(Array.get(recvRespBody.get(entry.getKey()), 0), Array.get(entry.getValue(), 0).toString());
} else if (entry.getValue() instanceof ArrayList) {
assertThat("Received data type is not array", recvRespBody.get(entry.getKey()) instanceof ArrayList, equalTo(true));
if (((ArrayList)((ArrayList<?>) recvRespBody.get(entry.getKey()))).size() > 0) {
for(Iterator ite = ((ArrayList<?>) entry.getValue()).iterator(); ite.hasNext();) {
Object expectedObj = ite.next();
for (Iterator it = ((ArrayList)((ArrayList<?>) recvRespBody.get(entry.getKey()))).iterator(); it.hasNext();){
Object actualObj = it.next();
if (expectedObj instanceof Map){
assertThat("Received data type does not match the expectation: object in array", actualObj instanceof Map, equalTo(true));
matchKeyAndDataTypeInRespWithExpectation((Map<String, Object>)actualObj, (Map<String, Object>)expectedObj);
}
else {
matchDataTypeInRespWithExpectation(actualObj, expectedObj.toString());
}
break;
}
break;
}
}
} else {
matchDataTypeInRespWithExpectation(recvRespBody.get(entry.getKey()), entry.getValue().toString());
}
}
}
private void matchKeyAndDataValueInRespWithExpectation(Map<String, Object> recvRespBody, JsonNode expectedRespBody) {
......@@ -451,15 +474,24 @@ public class RestfulMessageEventListener {
}
private void matchDataTypeInRespWithExpectation(Object recvDataType, String expectedDataType) {
if (recvDataType == null){
return;
}
else {
boolean matched = false;
switch (expectedDataType) {
case "string": case "int64":
if (recvDataType instanceof String){
case "string":
if (recvDataType instanceof String) {
matched = true;
}
break;
case "int64":
if (recvDataType instanceof String || recvDataType instanceof Integer) {
matched = true;
}
break;
case "boolean":
if (recvDataType instanceof Boolean){
if (recvDataType instanceof Boolean) {
matched = true;
}
break;
......@@ -474,11 +506,12 @@ public class RestfulMessageEventListener {
}
break;
case "number":
if (recvDataType instanceof BigDecimal) {
if (recvDataType instanceof BigDecimal || recvDataType instanceof Float) {
matched = true;
}
break;
case "array.string": case "array.int64":
case "array.string":
case "array.int64":
if (recvDataType instanceof String[]) {
matched = true;
}
......@@ -499,25 +532,32 @@ public class RestfulMessageEventListener {
}
break;
case "array.object":
if (recvDataType instanceof Map[]) {
case "array":
case "array.array":
if (recvDataType instanceof ArrayList) {
matched = true;
}
break;
case "object": case "object.object":
if (recvDataType instanceof Map){
matched = true;
}
break;
}
assertThat("response data type cannot match the expectation.", matched, equalTo(true));
/*try {
assertThat("response data type cannot match the expectation.", matched, equalTo(true));
}
catch (AssertionError error){
log.error("failed to match data type [{}]. Expected data type is [{}]", recvDataType, expectedDataType);
throw new AssertionError(error.toString());
}*/
}
private void throwAssertionErrorInFeatureTest(RestfulMessageEntity restfulMessageEntity, String errMsg){
private void throwExceptionInFeatureTest(RestfulMessageEntity restfulMessageEntity, AssertionError errMsg){
if (restfulMessageEntity.getMapperReqJsonBody() != null || restfulMessageEntity.getMapperRespJsonBody() != null){
throw new AssertionError(errMsg);
}
}
private void throwExceptionInFeatureTest(RestfulMessageEntity restfulMessageEntity, String exception){
if (restfulMessageEntity.getMapperReqJsonBody() != null || restfulMessageEntity.getMapperRespJsonBody() != null){
throw new AssertionError(exception);
}
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment