### Question:
ErrorResponseUtil { public static ErrorCodes getErrorCode(ResponseException responseException) throws ResponseException { String errorContent = responseException.getContent(); if (errorContent == null) { throw responseException; } try { ErrorResponseTemplate errorResponse = JsonTemplateMapper.readJson(errorContent, ErrorResponseTemplate.class); List<ErrorEntryTemplate> errors = errorResponse.getErrors(); if (errors.size() == 1) { String errorCodeString = errors.get(0).getCode(); if (errorCodeString != null) { return ErrorCodes.valueOf(errorCodeString); } } } catch (IOException | IllegalArgumentException ex) { } throw responseException; } private  ErrorResponseUtil(); static ErrorCodes getErrorCode(ResponseException responseException);  }### Answer:
@Test public void testGetErrorCode_knownErrorCode() throws ResponseException { Mockito.when(responseException.getContent()) .thenReturn( "{\"errors\":[{\"code\":\"MANIFEST_INVALID\",\"message\":\"manifest invalid\",\"detail\":{}}]}"); Assert.assertSame( ErrorCodes.MANIFEST_INVALID, ErrorResponseUtil.getErrorCode(responseException)); }
@Test public void testGetErrorCode_unknownErrorCode() { Mockito.when(responseException.getContent()) .thenReturn( "{\"errors\":[{\"code\":\"INVALID_ERROR_CODE\",\"message\":\"invalid code\",\"detail\":{}}]}"); try { ErrorResponseUtil.getErrorCode(responseException); Assert.fail(); } catch (ResponseException ex) { Assert.assertSame(responseException, ex); } }
@Test public void testGetErrorCode_multipleErrors() { Mockito.when(responseException.getContent()) .thenReturn( "{\"errors\":[" + "{\"code\":\"MANIFEST_INVALID\",\"message\":\"message 1\",\"detail\":{}}," + "{\"code\":\"TAG_INVALID\",\"message\":\"message 2\",\"detail\":{}}" + "]}"); try { ErrorResponseUtil.getErrorCode(responseException); Assert.fail(); } catch (ResponseException ex) { Assert.assertSame(responseException, ex); } }
@Test public void testGetErrorCode_invalidErrorObject() { Mockito.when(responseException.getContent()) .thenReturn("{\"type\":\"other\",\"message\":\"some other object\"}"); try { ErrorResponseUtil.getErrorCode(responseException); Assert.fail(); } catch (ResponseException ex) { Assert.assertSame(responseException, ex); } }