Skip to content

Commit

Permalink
Cleaned up http response handling
Browse files Browse the repository at this point in the history
  • Loading branch information
hylkevds committed Apr 3, 2019
1 parent 83a7994 commit bbd7fce
Show file tree
Hide file tree
Showing 11 changed files with 232 additions and 231 deletions.
Expand Up @@ -5,6 +5,7 @@
import de.fraunhofer.iosb.ilt.statests.util.ControlInformation;
import de.fraunhofer.iosb.ilt.statests.util.EntityType;
import de.fraunhofer.iosb.ilt.statests.util.HTTPMethods;
import de.fraunhofer.iosb.ilt.statests.util.HTTPMethods.HttpResponse;
import de.fraunhofer.iosb.ilt.statests.util.ServiceURLBuilder;
import java.util.ArrayList;
import java.util.HashMap;
Expand Down Expand Up @@ -133,15 +134,15 @@ private void readPropertyOfEntityWithEntityType(EntityType entityType) {
*/
private void checkGetPropertyOfEntity(EntityType entityType, Object id, EntityType.EntityProperty property) {
try {
Map<String, Object> responseMap = getEntity(entityType, id, property.name);
int responseCode = Integer.parseInt(responseMap.get("response-code").toString());
HttpResponse responseMap = getEntity(entityType, id, property.name);
int responseCode = responseMap.code;
if (responseCode == 204) {
// 204 is the proper response for NULL properties.
return;
}
String message = "Reading property \"" + property.name + "\" of the existing " + entityType.name() + " with id " + id + " failed.";
Assert.assertEquals(message, 200, responseCode);
String response = responseMap.get("response").toString();
String response = responseMap.response;
JSONObject entity;
entity = new JSONObject(response);
try {
Expand All @@ -167,8 +168,8 @@ private void checkGetPropertyOfEntity(EntityType entityType, Object id, EntityTy
* @param property The property to get requested
*/
private void checkGetPropertyValueOfEntity(EntityType entityType, Object id, EntityType.EntityProperty property) {
Map<String, Object> responseMap = getEntity(entityType, id, property.name + "/$value");
int responseCode = Integer.parseInt(responseMap.get("response-code").toString());
HttpResponse responseMap = getEntity(entityType, id, property.name + "/$value");
int responseCode = responseMap.code;
if (responseCode != 200 && property.optional) {
// The property is optional, and probably not present.
return;
Expand All @@ -179,7 +180,7 @@ private void checkGetPropertyValueOfEntity(EntityType entityType, Object id, Ent
}
String message = "Reading property value of \"" + property + "\" of the exitixting " + entityType.name() + " with id " + id + " failed.";
Assert.assertEquals(message, 200, responseCode);
String response = responseMap.get("response").toString();
String response = responseMap.response;
if ("object".equalsIgnoreCase(property.jsonType)) {
message = "Reading property value of \"" + property + "\" of \"" + entityType + "\" fails.";
Assert.assertEquals(message, 0, response.indexOf("{"));
Expand Down Expand Up @@ -232,13 +233,13 @@ private void readRelatedEntity(List<String> entityTypes, List<Object> ids) {
EntityType headEntity = EntityType.getForRelation(headName);
boolean isPlural = EntityType.isPlural(headName);
urlString = ServiceURLBuilder.buildURLString(serverSettings.serviceUrl, entityTypes, ids, null);
Map<String, Object> responseMap = HTTPMethods.doGet(urlString);
int code = Integer.valueOf(responseMap.get("response-code").toString());
HttpResponse responseMap = HTTPMethods.doGet(urlString);
int code = responseMap.code;

String message = "Reading relation of the entity failed: " + entityTypes.toString();
Assert.assertEquals(message, 200, code);

String response = responseMap.get("response").toString();
String response = responseMap.response;
Object id;
if (isPlural) {
id = new JSONObject(response).getJSONArray("value").getJSONObject(0).get(ControlInformation.ID);
Expand All @@ -249,11 +250,11 @@ private void readRelatedEntity(List<String> entityTypes, List<Object> ids) {
//check $ref
urlString = ServiceURLBuilder.buildURLString(serverSettings.serviceUrl, entityTypes, ids, "$ref");
responseMap = HTTPMethods.doGet(urlString);
code = Integer.valueOf(responseMap.get("response-code").toString());
code = responseMap.code;

message = "Reading relation of the entity failed: " + entityTypes.toString();
Assert.assertEquals(message, 200, code);
response = responseMap.get("response").toString();
response = responseMap.response;
checkAssociationLinks(response, entityTypes, ids);

if (entityTypes.size() == resourcePathLevel) {
Expand Down Expand Up @@ -333,13 +334,13 @@ private String readEntityWithEntityType(EntityType entityType) {
try {
String response = getEntities(entityType);
Object id = new JSONObject(response).getJSONArray("value").getJSONObject(0).get(ControlInformation.ID);
Map<String, Object> responseMap = getEntity(entityType, id, null);
int responseCode = Integer.parseInt(responseMap.get("response-code").toString());
HttpResponse responseMap = getEntity(entityType, id, null);
int responseCode = responseMap.code;

String message = "Reading existing " + entityType.name() + " with id " + id + " failed.";
Assert.assertEquals(message, 200, responseCode);

response = responseMap.get("response").toString();
response = responseMap.response;
return response;
} catch (JSONException e) {
LOGGER.error("Exception:", e);
Expand All @@ -356,7 +357,7 @@ private String readEntityWithEntityType(EntityType entityType) {
*/
private void readNonexistentEntityWithEntityType(EntityType entityType) {
long id = Long.MAX_VALUE;
int responseCode = Integer.parseInt(getEntity(entityType, id, null).get("response-code").toString());
int responseCode = getEntity(entityType, id, null).code;
String message = "Reading non-existing " + entityType.name() + " with id " + id + " failed.";
Assert.assertEquals(message, 404, responseCode);
}
Expand Down Expand Up @@ -432,9 +433,9 @@ private String getEntities(EntityType entityType) {
if (entityType != null) {
urlString = ServiceURLBuilder.buildURLString(serverSettings.serviceUrl, entityType, null, null, null);
}
Map<String, Object> responseMap = HTTPMethods.doGet(urlString);
String response = responseMap.get("response").toString();
int responseCode = Integer.parseInt(responseMap.get("response-code").toString());
HttpResponse responseMap = HTTPMethods.doGet(urlString);
String response = responseMap.response;
int responseCode = responseMap.code;

String message = "Error during getting entities: " + ((entityType != null) ? entityType.name() : "root URI");
Assert.assertEquals(message, 200, responseCode);
Expand All @@ -458,7 +459,7 @@ private String getEntities(EntityType entityType) {
* @return The response-code and response (body) of the request in Map
* format.
*/
private Map<String, Object> getEntity(EntityType entityType, Object id, String property) {
private HttpResponse getEntity(EntityType entityType, Object id, String property) {
if (id == null) {
return null;
}
Expand Down
Expand Up @@ -3,8 +3,8 @@
import de.fraunhofer.iosb.ilt.statests.ServerSettings;
import de.fraunhofer.iosb.ilt.statests.util.EntityType;
import de.fraunhofer.iosb.ilt.statests.util.HTTPMethods;
import de.fraunhofer.iosb.ilt.statests.util.HTTPMethods.HttpResponse;
import de.fraunhofer.iosb.ilt.statests.util.ServiceURLBuilder;
import java.util.Map;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
Expand Down Expand Up @@ -128,8 +128,8 @@ private static void createTestEntities(String rootUri, boolean actuation) {
+ "}\n"
+ "";
String urlString = ServiceURLBuilder.buildURLString(rootUri, EntityType.THING, null, null, null);
Map<String, Object> responseMap = HTTPMethods.doPost(urlString, urlParameters);
String response = responseMap.get("response").toString();
HttpResponse responseMap = HTTPMethods.doPost(urlString, urlParameters);
String response = responseMap.response;
Object id = HTTPMethods.idFromSelfLink(response);
if (actuation) {
String postContent = "{\n"
Expand Down Expand Up @@ -181,9 +181,9 @@ private static String getEntities(String rootUri, EntityType entityType) {
if (entityType != null) {
urlString = ServiceURLBuilder.buildURLString(rootUri, entityType, null, null, null);
}
Map<String, Object> responseMap = HTTPMethods.doGet(urlString);
String response = responseMap.get("response").toString();
int responseCode = Integer.parseInt(responseMap.get("response-code").toString());
HttpResponse responseMap = HTTPMethods.doGet(urlString);
String response = responseMap.response;
int responseCode = responseMap.code;
Assert.assertEquals("Error during getting entities: " + ((entityType != null) ? entityType.name() : "root URI"), 200, responseCode);
if (entityType != null) {
Assert.assertTrue("The GET entities response for entity type \"" + entityType + "\" does not match SensorThings API : missing \"value\" in response.", response.contains("value"));
Expand Down

0 comments on commit bbd7fce

Please sign in to comment.