@Test public void testJsonErrorResponseReturnsNullIfExceptionIsNull() throws Exception { List<JsonErrorUnmarshaller> exceptionUnmarshallers = new ArrayList<JsonErrorUnmarshaller>(); exceptionUnmarshallers.add(new JsonErrorUnmarshaller() { @Override public AmazonServiceException unmarshall(JsonErrorResponse error) throws Exception { return null; } }); handler = new JsonErrorResponseHandler(exceptionUnmarshallers); response = HttpResponse.builder() .statusCode(403) .content(new ByteArrayInputStream("{}".getBytes(StringUtils.UTF8))) .build(); assertNull(handler.handle(response)); }
@Test public void testJsonErrorResponse() throws Exception { String body = "{\"__type\":\"InvalidSignatureException\",\"message\":\"Clock is skewed\"}"; ByteArrayInputStream content = new ByteArrayInputStream(body.getBytes(StringUtils.UTF8)); response = HttpResponse.builder() .content(content) .statusCode(403) .build(); AmazonServiceException ase = handler.handle(response); assertEquals(ase.getErrorCode(), "InvalidSignatureException"); }
@Test public void testRestJsonErrorResponse() throws Exception { String body = "{\"message\":\"Clock is skewed\"}"; ByteArrayInputStream content = new ByteArrayInputStream(body.getBytes(StringUtils.UTF8)); response = HttpResponse.builder() .content(content) .statusCode(403) .header("x-amzn-ErrorType", "InvalidSignatureException") .build(); AmazonServiceException ase = handler.handle(response); assertEquals(ase.getErrorCode(), "InvalidSignatureException"); }
@Test public void testJsonErrorResponseReturnsServiceErrorTypeIfErrorStatus5XX() throws Exception { List<JsonErrorUnmarshaller> exceptionUnmarshallers = new ArrayList<JsonErrorUnmarshaller>(); exceptionUnmarshallers.add(new JsonErrorUnmarshaller() { @Override public AmazonServiceException unmarshall(JsonErrorResponse error) throws Exception { return new AmazonServiceException("TestException"); } }); handler = new JsonErrorResponseHandler(exceptionUnmarshallers); response = HttpResponse.builder() .statusCode(500) .content(new ByteArrayInputStream("{}".getBytes(StringUtils.UTF8))) .build(); AmazonServiceException returnedException = handler.handle(response); assertEquals(returnedException.getErrorType(), ErrorType.Service); }
@Test public void testJsonErrorResponseReturnsXAmzRequestId() throws Exception { List<JsonErrorUnmarshaller> exceptionUnmarshallers = new ArrayList<JsonErrorUnmarshaller>(); exceptionUnmarshallers.add(new JsonErrorUnmarshaller() { @Override public AmazonServiceException unmarshall(JsonErrorResponse error) throws Exception { return new AmazonServiceException("TestException"); } }); handler = new JsonErrorResponseHandler(exceptionUnmarshallers); response = HttpResponse.builder() .statusCode(500) .header("X-Amzn-RequestId", "55") .content(new ByteArrayInputStream("{}".getBytes(StringUtils.UTF8))) .build(); AmazonServiceException returnedException = handler.handle(response); assertEquals(returnedException.getRequestId(), "55"); } }