Skip to content

Commit ae45d01

Browse files
authored
Merge pull request #117 from egecansen/api_assured_method_improvement
Api assured method improvement
2 parents 2db0bd0 + eaddf73 commit ae45d01

File tree

2 files changed

+61
-16
lines changed

2 files changed

+61
-16
lines changed

src/main/java/api_assured/Caller.java

Lines changed: 26 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -2,20 +2,22 @@
22

33
import api_assured.exceptions.FailedCallException;
44
import com.fasterxml.jackson.core.JsonProcessingException;
5+
import okhttp3.ResponseBody;
56
import okio.Buffer;
67
import properties.PropertyUtilities;
78
import retrofit2.Call;
89
import retrofit2.Response;
910
import utils.*;
10-
import utils.mapping.MappingUtilities;
11+
import utils.reflection.ReflectionUtilities;
1112

1213
import java.io.IOException;
1314
import java.nio.charset.StandardCharsets;
1415
import java.util.Objects;
1516

16-
import static utils.mapping.MappingUtilities.Json.*;
17+
import static utils.MappingUtilities.Json.*;
1718
import static utils.reflection.ReflectionUtilities.getPreviousMethodName;
1819
import static utils.StringUtilities.Color.*;
20+
import static utils.reflection.ReflectionUtilities.isOfType;
1921

2022
/**
2123
* This abstract class represents a caller that performs API calls, logs the results and returns objects in response bodies.
@@ -32,7 +34,7 @@ public abstract class Caller {
3234
/**
3335
* A static boolean variable that determines whether logs should be kept for API calls.
3436
*/
35-
public static boolean keepLogs;
37+
protected static boolean keepLogs;
3638

3739
/**
3840
* A Printer object for logging.
@@ -143,16 +145,18 @@ private static <T> Response<T> getResponse(Call<T> call, boolean printBody) thro
143145
if (keepLogs) log.success("The response code is: " + response.code());
144146
if (keepLogs && !response.message().isEmpty()) log.info(response.message());
145147
if (printBody && printableResponse) log.info("The response body is: \n" + getJsonString(body));
146-
assert body != null;
147148
return Response.success(body, response.raw());
148149
}
149150
else {
150151
log.warning("The response code is: " + response.code());
151-
if (!response.message().isEmpty()) log.warning(response.message());
152-
assert response.errorBody() != null;
153-
if (printBody) {
154-
Object errorBody = getJsonString(getErrorObject(response, Object.class));
155-
String errorLog = errorBody.equals("null") ? "The error body is empty." : "The error body is: \n" + errorBody;
152+
if (response.message().length()>0) log.warning(response.message());
153+
if (response.errorBody() != null && printBody) {
154+
Object errorBody = isOfType(response, Object.class) ?
155+
getJsonString(getErrorObject(response, Object.class)) :
156+
response.raw();
157+
String errorLog = errorBody.equals("null") ?
158+
"The error body is empty." :
159+
"The error body is: \n" + errorBody;
156160
log.warning(errorLog);
157161
}
158162
return Response.error(response.errorBody(), response.raw());
@@ -173,12 +177,18 @@ private static <T> Response<T> getResponse(Call<T> call, boolean printBody) thro
173177
*
174178
* @see MappingUtilities.Json#fromJsonString(String, Class)
175179
*/
180+
@SuppressWarnings("unchecked")
176181
private static <ErrorModel> ErrorModel getErrorObject(Response<?> response, Class<ErrorModel> errorModel) throws JsonProcessingException {
177182
assert response.errorBody() != null;
183+
if (errorModel.isAssignableFrom(ResponseBody.class))
184+
return (ErrorModel) response.errorBody();
178185
try (Buffer errorBuffer = response.errorBody().source().getBuffer().clone()) {
179186
String bodyString = errorBuffer.readString(StandardCharsets.UTF_8);
180-
if (!StringUtilities.isBlank(bodyString))
181-
return fromJsonString(bodyString, errorModel);
187+
if (!StringUtilities.isBlank(bodyString)) {
188+
if (errorModel.isAssignableFrom(String.class))
189+
return (ErrorModel) bodyString;
190+
else return fromJsonString(bodyString, errorModel);
191+
}
182192
else
183193
return null;
184194
}
@@ -249,9 +259,12 @@ private static <ResponseModel> Response<ResponseModel> call(
249259
*/
250260
@SuppressWarnings("unchecked")
251261
private static <ErrorModel> ErrorModel getErrorBody(Response<?> response, Class<?>... errorModels){
252-
for (Class<?> errorModel:errorModels){
262+
for (Class<?> errorClass:errorModels){
253263
try {
254-
return (ErrorModel) fromJsonString(getJsonStringFor(getErrorObject(response, errorModel)), errorModel);
264+
ErrorModel errorModel = (ErrorModel) getErrorObject(response, errorClass);
265+
assert errorModel != null;
266+
if (errorClass.isAssignableFrom(errorModel.getClass()))
267+
return errorModel;
255268
}
256269
catch (JsonProcessingException ignored) {}
257270
}

src/main/java/utils/reflection/ReflectionUtilities.java

Lines changed: 35 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -516,7 +516,7 @@ public static <T> JsonObject getJsonObject(Class<T> clazz, JsonObject json, Stri
516516
field.setAccessible(true);
517517
if (Arrays.stream(exceptions).noneMatch(exception -> exception.equals(field.getName()))) {
518518
boolean isMember = field.getType().isMemberClass();
519-
boolean isList = isOfType(field, "List");
519+
boolean isList = fieldIsOfType(field, "List");
520520
if (isMember)
521521
json.add(field.getName(), getJsonObject(field.getType(), new JsonObject(), exceptions));
522522
else if (isList)
@@ -547,7 +547,7 @@ public static <T> JsonObject getJsonFromObject(T object, JsonObject json, String
547547
field.setAccessible(true);
548548
if (Arrays.stream(exceptions).noneMatch(exception -> exception.equals(field.getName()))) {
549549
boolean isMember = field.getType().isMemberClass();
550-
boolean isList = isOfType(field, "List");
550+
boolean isList = fieldIsOfType(field, "List");
551551

552552
if (isMember)
553553
json.add(field.getName(), getJsonFromObject(field, new JsonObject(), exceptions));
@@ -567,10 +567,42 @@ else if (isList)
567567
* @param expectedType expected field type
568568
* @return true or false
569569
*/
570-
public static boolean isOfType(Field field, String expectedType) {
570+
public static boolean fieldIsOfType(Field field, String expectedType) {
571571
return field.getType().getTypeName().contains(expectedType);
572572
}
573573

574+
/**
575+
* Verifies type of given object
576+
*
577+
* @param object target object
578+
* @param type expected object type
579+
* @return true or false
580+
*/
581+
public static <Type> boolean isOfType(Type object, Class<Type> type) {
582+
try {
583+
String jsonString = MappingUtilities.Json.getJsonStringFor(object);
584+
return isOfType(jsonString, type);
585+
} catch (IllegalArgumentException e) {
586+
return false;
587+
}
588+
}
589+
590+
/**
591+
* Verifies type of given object
592+
*
593+
* @param objectString expected object string
594+
* @param type expected object type
595+
* @return true or false
596+
*/
597+
public static <Type> boolean isOfType(String objectString, Class<Type> type) {
598+
try {
599+
MappingUtilities.Json.fromJsonString(objectString, type);
600+
return true;
601+
} catch (JsonProcessingException | IllegalArgumentException e) {
602+
return false;
603+
}
604+
}
605+
574606
/**
575607
* Acquires the type of given field
576608
*

0 commit comments

Comments
 (0)