### Question:
EntitlementsExceptionMappingHandler implements ExceptionMappingHandler<EntitlementException, ResourceException> { @Override public ResourceException handleError(Context context, Request request, EntitlementException error) { return handleError(context, null, request, error); } @Inject  EntitlementsExceptionMappingHandler(
            @Named(RESOURCE_ERROR_MAPPING) Map<Integer, Integer> errorCodeMapping,
            @Named(REQUEST_TYPE_ERROR_OVERRIDES) Map<RequestType, Map<Integer, Integer>> errorCodeOverrides,
            @Named(DEBUG_TYPE_OVERRIDES) Map<Integer, Integer> errorDebugMap);  EntitlementsExceptionMappingHandler(Map<Integer, Integer> errorCodeMapping); @Override ResourceException handleError(Context context, Request request, EntitlementException error); @Override ResourceException handleError(Context context, String msg, Request request, EntitlementException error); @Override ResourceException handleError(String msg, Request request, EntitlementException error); @Override ResourceException handleError(Request request, EntitlementException error); @Override ResourceException handleError(EntitlementException error); static final String RESOURCE_ERROR_MAPPING; static final String REQUEST_TYPE_ERROR_OVERRIDES; static final String DEBUG_TYPE_OVERRIDES; }### Answer:
@Test public void shouldMapKnownErrorsAsConfigured() { EntitlementsExceptionMappingHandler errorHandler = new EntitlementsExceptionMappingHandler( Collections.singletonMap(ERROR_CODE, ResourceException.NOT_FOUND)); EntitlementException error = exception(ERROR_CODE, ERROR_MESSAGE); ResourceException result = errorHandler.handleError(error); assertThat(result).isInstanceOf(NotFoundException.class) .hasMessage(ERROR_MESSAGE); }
@Test public void shouldMapUnknownErrorsAsServerErrors() { EntitlementsExceptionMappingHandler errorHandler = new EntitlementsExceptionMappingHandler( Collections.<Integer, Integer>emptyMap()); EntitlementException error = exception(ERROR_CODE, ERROR_MESSAGE); ResourceException result = errorHandler.handleError(error); assertThat(result).isInstanceOf(InternalServerErrorException.class) .hasMessage(ERROR_MESSAGE); }
@Test public void shouldApplyRequestTypeOverrides() { Map<RequestType, Map<Integer, Integer>> overrides = new HashMap<RequestType, Map<Integer, Integer>>(); RequestType requestType = RequestType.CREATE; overrides.put(requestType, Collections.singletonMap(ResourceException.NOT_FOUND, ResourceException.BAD_REQUEST)); EntitlementsExceptionMappingHandler errorHandler = new EntitlementsExceptionMappingHandler( Collections.singletonMap(ERROR_CODE, ResourceException.NOT_FOUND), overrides, Collections.<Integer, Integer>emptyMap() ); EntitlementException error = exception(ERROR_CODE, ERROR_MESSAGE); Request request = mock(Request.class); given(request.getRequestType()).willReturn(requestType); ResourceException result = errorHandler.handleError(request, error); assertThat(result).isInstanceOf(BadRequestException.class) .hasMessage(ERROR_MESSAGE); }
@Test public void shouldGetExceptionMessageAsEnglish() throws Exception { EntitlementsExceptionMappingHandler errorHandler = new EntitlementsExceptionMappingHandler(Collections.singletonMap(EntitlementException.EMPTY_PRIVILEGE_NAME, ResourceException.BAD_REQUEST)); EntitlementException error = new EntitlementException(EntitlementException.EMPTY_PRIVILEGE_NAME); ResourceException result = errorHandler.handleError(getHttpServerContext("en"), null, error); assertThat(result.getMessage()).isEqualTo("Policy name cannot be empty."); }
@Test public void shouldGetExceptionMessageAsFrench() throws Exception { EntitlementsExceptionMappingHandler errorHandler = new EntitlementsExceptionMappingHandler( Collections.singletonMap(EntitlementException.SUBJECT_REQUIRED, ResourceException.BAD_REQUEST)); EntitlementException error = new EntitlementException(EntitlementException.SUBJECT_REQUIRED); ResourceException result = errorHandler.handleError(getHttpServerContext("fr"), null, error); assertThat(result.getMessage()).isEqualTo("Les objets sont obligatoires."); }