AwsServiceHelper { public void handleAmazonException(AmazonServiceException amazonServiceException, String message) throws IllegalArgumentException, ObjectNotFoundException { if (amazonServiceException.getStatusCode() == HttpStatus.SC_BAD_REQUEST) { throw new IllegalArgumentException(message + " Reason: " + amazonServiceException.getMessage(), amazonServiceException); } else if (amazonServiceException.getStatusCode() == HttpStatus.SC_NOT_FOUND) { throw new ObjectNotFoundException(message + " Reason: " + amazonServiceException.getMessage(), amazonServiceException); } throw amazonServiceException; }  void handleAmazonException(AmazonServiceException amazonServiceException, String message);  }
@Test public void testHandleAmazonException() { try { AmazonServiceException amazonServiceException = new AmazonServiceException(ERROR_MESSAGE); awsServiceHelper.handleAmazonException(amazonServiceException, MESSAGE_TEXT); fail(); } catch (AmazonServiceException amazonServiceException) { assertThat("Error message not correct.", amazonServiceException.getMessage().contains(ERROR_MESSAGE), is(true)); } }
@Test public void testHandleAmazonExceptionWithBadRequestException() { try { AmazonServiceException amazonServiceException = new AmazonServiceException(ERROR_MESSAGE); amazonServiceException.setStatusCode(HttpStatus.SC_BAD_REQUEST); awsServiceHelper.handleAmazonException(amazonServiceException, MESSAGE_TEXT); fail(); } catch (IllegalArgumentException illegalArgumentException) { assertThat("Error message not correct.", illegalArgumentException.getMessage().contains(MESSAGE_TEXT), is(true)); } }
@Test public void testHandleAmazonExceptionWithNotFoundException() { try { AmazonServiceException amazonServiceException = new AmazonServiceException(ERROR_MESSAGE); amazonServiceException.setStatusCode(HttpStatus.SC_NOT_FOUND); awsServiceHelper.handleAmazonException(amazonServiceException, MESSAGE_TEXT); fail(); } catch (ObjectNotFoundException objectNotFoundException) { assertThat("Error message not correct.", objectNotFoundException.getMessage().contains(MESSAGE_TEXT), is(true)); } }