### Question:
Authenticator { public boolean isAuthenticated(Request request) { requireNonNull(request, "request"); String requestAccessKey = request.headers(HttpConstants.HEADER_NAME_ACCESS_KEY); LOG.debug("Provided access key in header is '{}', required value is '{}'", requestAccessKey, configuration.accessKey()); if (Strings.isNullOrEmpty(configuration.accessKey())) { LOG.debug("No access key, authentication not required."); return true; } return Objects.equals(requestAccessKey, configuration.accessKey()); } @Inject  Authenticator(ServiceWebApiConfiguration configuration); boolean isAuthenticated(Request request);  }### Answer:
@Test public void allowRequestsIfNoAccessKeyConfigured() { when(configuration.accessKey()).thenReturn(""); assertTrue(authenticator.isAuthenticated(aRequestWithAccessKey(null))); assertTrue(authenticator.isAuthenticated(aRequestWithAccessKey(""))); assertTrue(authenticator.isAuthenticated(aRequestWithAccessKey("some random value"))); }
@Test public void allowRequestIfCorrectAccessKeyGiven() { when(configuration.accessKey()).thenReturn("my access key"); assertTrue(authenticator.isAuthenticated(aRequestWithAccessKey("my access key"))); }
@Test public void disallowRequestIfWrongAccessKeyGiven() { when(configuration.accessKey()).thenReturn("my access key"); assertFalse(authenticator.isAuthenticated(aRequestWithAccessKey(null))); assertFalse(authenticator.isAuthenticated(aRequestWithAccessKey(""))); assertFalse(authenticator.isAuthenticated(aRequestWithAccessKey("some random value"))); }