BasicAuthenticator implements Authenticator<BasicCredentials, Principal> { @Override public Optional<Principal> authenticate(BasicCredentials credentials) { if (Objects.isNull(credentials)) { return Optional.empty(); } Key key = new Key(credentials.getUsername(), credentials.getPassword()); return allKeys.contains(key) ? Optional.of(key) : Optional.empty(); } @Inject  BasicAuthenticator(List<Key> allKeys); @Override Optional<Principal> authenticate(BasicCredentials credentials);  }
@Test @SuppressWarnings("OptionalGetWithoutIsPresent") void testAuthenticateWithValidCredentials() { BasicCredentials credentials = new BasicCredentials("application", "secret"); Optional<Principal> result = authenticator.authenticate(credentials); assertAll("Assert authentication success", () -> assertTrue(result::isPresent), () -> assertTrue(result.get() instanceof Key), () -> assertEquals(KEY, result.get())); }
@Test void testAuthenticateWithNullCredentials() { Optional<Principal> result = authenticator.authenticate(null); assertFalse(result::isPresent); }
@Test void testAuthenticateWithInvalidCredentials() { BasicCredentials credentials = new BasicCredentials("invalidApplication", "secret"); Optional<Principal> result = authenticator.authenticate(credentials); assertFalse(result::isPresent); }