JsonErrorCodeParser implements ErrorCodeParser { public String parseErrorCode(HttpResponse response, JsonContent jsonContent) { String errorCodeFromHeader = parseErrorCodeFromHeader(response.getHeaders()); if (errorCodeFromHeader != null) { return errorCodeFromHeader; } else if (jsonContent != null) { return parseErrorCodeFromContents(jsonContent.getJsonNode()); } else { return null; } } JsonErrorCodeParser();  JsonErrorCodeParser(String errorCodeFieldName); String parseErrorCode(HttpResponse response, JsonContent jsonContent);  }
@Test public void parseErrorType_ErrorTypeInHeadersTakesPrecedence_NoSuffix() { String actualErrorType = parser.parseErrorCode( httpResponseWithHeaders(JsonErrorCodeParser.X_AMZN_ERROR_TYPE, HEADER_ERROR_TYPE), toJsonContent(JSON_ERROR_TYPE)); assertEquals(HEADER_ERROR_TYPE, actualErrorType); }
@Test public void parseErrorType_ErrorTypeInHeadersTakesPrecedence_SuffixIgnored() { String actualErrorType = parser.parseErrorCode( httpResponseWithHeaders(JsonErrorCodeParser.X_AMZN_ERROR_TYPE, String.format("%s:%s", HEADER_ERROR_TYPE, "someSuffix")), toJsonContent(JSON_ERROR_TYPE)); assertEquals(HEADER_ERROR_TYPE, actualErrorType); }
@Test public void parseErrorType_ErrorTypeInContent_NoPrefix() { String actualErrorType = parser.parseErrorCode(httpResponseWithoutHeaders(), toJsonContent(JSON_ERROR_TYPE)); assertEquals(JSON_ERROR_TYPE, actualErrorType); }
@Test public void parseErrorType_ErrorTypeInContent_PrefixIgnored() { String actualErrorType = parser.parseErrorCode(httpResponseWithoutHeaders(), toJsonContent(String.format("%s#%s", "somePrefix", JSON_ERROR_TYPE))); assertEquals(JSON_ERROR_TYPE, actualErrorType); }
@Test public void parseErrorType_NotPresentInHeadersAndNullContent_ReturnsNull() { assertNull(parser.parseErrorCode(httpResponseWithoutHeaders(), null)); }
@Test public void parseErrorType_NotPresentInHeadersAndEmptyContent_ReturnsNull() { assertNull(parser.parseErrorCode(httpResponseWithoutHeaders(), new JsonContent(null, new ObjectMapper().createObjectNode()))); }