Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -37,10 +37,8 @@ public class TopicManagementResponse {
// Server error codes as defined in https://developers.google.com/instance-id/reference/server
// TODO: Should we handle other error codes here (e.g. PERMISSION_DENIED)?
private static final Map<String, String> ERROR_CODES = ImmutableMap.<String, String>builder()
.put("INVALID_ARGUMENT", "invalid-argument")
.put("NOT_FOUND", "registration-token-not-registered")
.put("INTERNAL", "internal-error")
.put("TOO_MANY_TOPICS", "too-many-topics")
.build();

private final int successCount;
Expand Down Expand Up @@ -101,8 +99,11 @@ public static class Error {

private Error(int index, String reason) {
this.index = index;
this.reason = ERROR_CODES.containsKey(reason)
? ERROR_CODES.get(reason) : UNKNOWN_ERROR;
if (reason == null || reason.trim().isEmpty()) {
this.reason = UNKNOWN_ERROR;
} else {
this.reason = ERROR_CODES.getOrDefault(reason, reason.toLowerCase().replace('_', '-'));
}
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -404,7 +404,30 @@ public void testTopicManagementResponseWithEmptyList() {

@Test
public void testTopicManagementResponseErrorToString() {
GenericJson json = new GenericJson().set("error", "test error");
GenericJson json = new GenericJson().set("error", "INVALID_ARGUMENT");
ImmutableList<GenericJson> jsonList = ImmutableList.of(json);

TopicManagementResponse topicManagementResponse = new TopicManagementResponse(jsonList);

String expected = "[Error{index=0, reason=invalid-argument}]";
assertEquals(expected, topicManagementResponse.getErrors().toString());
}

@Test
public void testTopicManagementResponseErrorNotInErrorCodes() {
String myError = "MY_ERROR";
GenericJson json = new GenericJson().set("error", myError);
ImmutableList<GenericJson> jsonList = ImmutableList.of(json);

TopicManagementResponse topicManagementResponse = new TopicManagementResponse(jsonList);

String expected = "[Error{index=0, reason=my-error}]";
assertEquals(expected, topicManagementResponse.getErrors().toString());
}

@Test
public void testTopicManagementResponseErrorUnknown() {
GenericJson json = new GenericJson().set("error", "");
ImmutableList<GenericJson> jsonList = ImmutableList.of(json);

TopicManagementResponse topicManagementResponse = new TopicManagementResponse(jsonList);
Expand All @@ -413,6 +436,29 @@ public void testTopicManagementResponseErrorToString() {
assertEquals(expected, topicManagementResponse.getErrors().toString());
}

@Test
public void testTopicManagementResponseErrorResourceExhausted() {
GenericJson json = new GenericJson().set("error", "RESOURCE_EXHAUSTED");
ImmutableList<GenericJson> jsonList = ImmutableList.of(json);

TopicManagementResponse topicManagementResponse = new TopicManagementResponse(jsonList);

String expected = "[Error{index=0, reason=resource-exhausted}]";
assertEquals(expected, topicManagementResponse.getErrors().toString());
}

@Test
public void testTopicManagementResponseErrorTooManyTopics() {
GenericJson json = new GenericJson().set("error", "TOO_MANY_TOPICS");
ImmutableList<GenericJson> jsonList = ImmutableList.of(json);

TopicManagementResponse topicManagementResponse = new TopicManagementResponse(jsonList);

String expected = "[Error{index=0, reason=too-many-topics}]";
assertEquals(expected, topicManagementResponse.getErrors().toString());
}


private static InstanceIdClientImpl initInstanceIdClient(
final MockLowLevelHttpResponse mockResponse,
final HttpResponseInterceptor interceptor) {
Expand All @@ -432,7 +478,7 @@ private void checkTopicManagementRequest(
assertEquals(1, result.getFailureCount());
assertEquals(1, result.getErrors().size());
assertEquals(1, result.getErrors().get(0).getIndex());
assertEquals("unknown-error", result.getErrors().get(0).getReason());
assertEquals("error-reason", result.getErrors().get(0).getReason());

ByteArrayOutputStream out = new ByteArrayOutputStream();
request.getContent().writeTo(out);
Expand Down