}
@Test
@Ignore
public void testInvalidGMGPayloads() throws Exception {
ErrorMessage error = jsonToObject(makeRequest(imgBaseUrl, "POST", new JSONObject(), apiKey,
AuthTokenType.GATEWAY),
TypeFactory.defaultInstance().constructType(ErrorMessage.class));
// Invalid message because the message or metrics attributes were missing
assertTrue(error.getErrorMessage().equals("You must supply at least a 'message' or 'metrics' attribute in " +
"the payload."));
// Invalid message because hashtags is a non-array
JSONObject json = new JSONObject();
json.put("message", "This message doesn't matter.");
json.put("hashtags", 12345);
error = jsonToObject(makeRequest(imgBaseUrl, "POST", json, apiKey,
AuthTokenType.GATEWAY),
TypeFactory.defaultInstance().constructType(ErrorMessage.class));
assertTrue(error.getErrorMessage().equals("'hashtags' must be a string array."));
// Invalid message because hashtags is an array with non-string values
JSONArray jsonArray = new JSONArray();
json = new JSONObject();
jsonArray.add(12345);
json.put("message", "This message doesn't matter.");
json.put("hashtags", jsonArray);
error = jsonToObject(makeRequest(imgBaseUrl, "POST", json, apiKey,
AuthTokenType.GATEWAY),
TypeFactory.defaultInstance().constructType(ErrorMessage.class));
assertTrue(error.getErrorMessage()
.equals("All hashtags specified in the 'hashtags' attribute must be strings."));
// Invalid message because metrics is a non-array
json = new JSONObject();
json.put("message", "This message doesn't matter.");
json.put("metrics", 12345);
error = jsonToObject(makeRequest(imgBaseUrl, "POST", json, apiKey,
AuthTokenType.GATEWAY),
TypeFactory.defaultInstance().constructType(ErrorMessage.class));
assertTrue(error.getErrorMessage().equals("'metrics' must be an object array."));
// Invalid message because metrics is an array with non-object values
json = new JSONObject();
jsonArray = new JSONArray();
jsonArray.add(12345);
json.put("message", "This message doesn't matter.");
json.put("metrics", jsonArray);
error = jsonToObject(makeRequest(imgBaseUrl, "POST", json, apiKey,
AuthTokenType.GATEWAY),
TypeFactory.defaultInstance().constructType(ErrorMessage.class));
assertTrue(error.getErrorMessage()
.equals("All metrics specified in the 'metrics' attribute must be objects."));
// Invalid message because metric in metrics doesn't contain name
JSONObject metric = new JSONObject();
json = new JSONObject();
jsonArray = new JSONArray();
jsonArray.add(metric);
json.put("message", "This message doesn't matter.");
json.put("metrics", jsonArray);
error = jsonToObject(makeRequest(imgBaseUrl, "POST", json, apiKey,
AuthTokenType.GATEWAY),
TypeFactory.defaultInstance().constructType(ErrorMessage.class));
assertTrue(error.getErrorMessage()
.equals("'name' is required for each metric in the 'metrics' attribute."));
// Invalid message because metric in metrics doesn't contain type
metric = new JSONObject();
json = new JSONObject();
jsonArray = new JSONArray();
metric.put("name", "This name doesn't matter.");
jsonArray.add(metric);
json.put("message", "This message doesn't matter.");
json.put("metrics", jsonArray);
error = jsonToObject(makeRequest(imgBaseUrl, "POST", json, apiKey,
AuthTokenType.GATEWAY),
TypeFactory.defaultInstance().constructType(ErrorMessage.class));
assertTrue(error.getErrorMessage()
.equals("'type' is required for each metric in the 'metrics' attribute and must be " +
"either 'ABSOLUTE' or 'DELTA'."));
// Invalid message because metric in metrics doesn't contain valid type
metric = new JSONObject();
json = new JSONObject();
jsonArray = new JSONArray();
metric.put("name", "This name doesn't matter.");
metric.put("type", "FAKE");
jsonArray.add(metric);
json.put("message", "This message doesn't matter.");
json.put("metrics", jsonArray);
error = jsonToObject(makeRequest(imgBaseUrl, "POST", json, apiKey,
AuthTokenType.GATEWAY),
TypeFactory.defaultInstance().constructType(ErrorMessage.class));
assertTrue(error.getErrorMessage()
.equals("'type' is required for each metric in the 'metrics' attribute and must be " +
"either 'ABSOLUTE' or 'DELTA'."));
// Invalid message because metric in metrics doesn't contain value
metric = new JSONObject();
json = new JSONObject();
jsonArray = new JSONArray();
metric.put("name", "This name doesn't matter.");
metric.put("type", "ABSOLUTE");
jsonArray.add(metric);
json.put("message", "This message doesn't matter.");
json.put("metrics", jsonArray);
error = jsonToObject(makeRequest(imgBaseUrl, "POST", json, apiKey,
AuthTokenType.GATEWAY),
TypeFactory.defaultInstance().constructType(ErrorMessage.class));
assertTrue(error.getErrorMessage()
.equals("'value' is required for each metric in the 'metrics' attribute and must " +
"be a valid numerical value."));
// Invalid message because metric in metrics doesn't contain valid value
metric = new JSONObject();
json = new JSONObject();
jsonArray = new JSONArray();
metric.put("name", "This name doesn't matter.");
metric.put("type", "ABSOLUTE");
metric.put("value", "fake");
jsonArray.add(metric);
json.put("message", "This message doesn't matter.");
json.put("metrics", jsonArray);
error = jsonToObject(makeRequest(imgBaseUrl, "POST", json, apiKey,
AuthTokenType.GATEWAY),
TypeFactory.defaultInstance().constructType(ErrorMessage.class));
assertTrue(error.getErrorMessage()
.equals("'value' for each metric in the 'metrics' attribute must be a numerical value."));
// Invalid because it's a multi-entry request but the data attribute isn't a JSONArray
json = new JSONObject();
json.put("data", "This should be a JSONArray.");
error = jsonToObject(makeRequest(imgBaseUrl, "POST", json, apiKey,
AuthTokenType.GATEWAY),
TypeFactory.defaultInstance().constructType(ErrorMessage.class));
assertTrue(error.getErrorMessage().equals("'data' must be an object array."));
// Invalid because it's a multi-entry request but the data entry isn't an object
json = new JSONObject();
jsonArray = new JSONArray();
jsonArray.add("This should be a JSONObject.");
json.put("data", jsonArray);
error = jsonToObject(makeRequest(imgBaseUrl, "POST", json, apiKey,
AuthTokenType.GATEWAY),
TypeFactory.defaultInstance().constructType(ErrorMessage.class));
assertTrue(error.getErrorMessage().equals("Every object in the 'data' array should be an object."));
// Invalid because dateGenerated is not a number
json = new JSONObject();
json.put("message", "This is a test message.");
json.put("dateGenerated", "This should be a number.");
error = jsonToObject(makeRequest(imgBaseUrl, "POST", json, apiKey,
AuthTokenType.GATEWAY),
TypeFactory.defaultInstance().constructType(ErrorMessage.class));
assertTrue(error.getErrorMessage().equals("'dateGenerated' must be an number."));
}