### Question:
DefaultRequestHandler implements RequestHandler<Principal> { @Override public Principal handle(HttpRequestContext request) throws NotAuthorizedException, InternalServerException { try { Credentials credentials = requestDecoder.decode(request); Principal result = authenticator.authenticate(credentials); if (result != null) { return result; } } catch (IllegalArgumentException e) { LOG.info("Error decoding credentials: {}", e.getMessage()); throw new NotAuthorizedException(); } catch (Exception e) { LOG.warn("Error while authenticating credentials", e); throw new InternalServerException(); } throw new NotAuthorizedException(); } DefaultRequestHandler(Authenticator<Principal> authenticator);  DefaultRequestHandler(Authenticator<Principal> authenticator, RequestConfiguration requestConfiguration); @VisibleForTesting  DefaultRequestHandler(RequestDecoder requestDecoder, Authenticator<Principal> authenticator); @Override Principal handle(HttpRequestContext request);  }### Answer:
@Test(expected = NotAuthorizedException.class) public void testHandleWithoutCredentials() { when(decoder.decode(any(HttpRequestContext.class))).thenThrow(new IllegalArgumentException()); handler.handle(request); fail(); }
@Test public void testHandleWithValidCredentials() { HttpRequestContext request = mock(HttpRequestContext.class); when(decoder.decode(any(HttpRequestContext.class))).thenReturn(credentials); when(authenticator.authenticate(any(Credentials.class))).thenReturn(principal); String value = handler.handle(request); assertNotNull(value); assertEquals(principal, value); }
@Test(expected = NotAuthorizedException.class) public void testHandleWithInvalidCredentials() { HttpRequestContext request = mock(HttpRequestContext.class); when(decoder.decode(any(HttpRequestContext.class))).thenReturn(credentials); when(authenticator.authenticate(any(Credentials.class))).thenReturn(null); handler.handle(request); fail(); }
@Test(expected = InternalServerException.class) public void testHandleWithInternalError() { HttpRequestContext request = mock(HttpRequestContext.class); when(decoder.decode(any(HttpRequestContext.class))).thenReturn(credentials); when(authenticator.authenticate(any(Credentials.class))).thenThrow(new NullPointerException()); handler.handle(request); fail(); }