Auth { User authenticate() throws ServiceException { Iterable<Authenticator> authenticators = getAuthenticatorInstances(); User user = null; if (authenticators != null) { for (Authenticator authenticator : authenticators) { user = authenticator.authenticate(request); if (user != null) { break; } } } return user; } @VisibleForTesting  Auth(HttpServletRequest request); static Authenticator instantiateAuthenticator(Class<? extends Authenticator> clazz);  }
@Test public void testAuthenticate_pass() throws Exception { when(config.getAuthenticators()).thenReturn( ImmutableList.<Class<? extends Authenticator>>of(PassAuthenticator.class)); assertEquals(PassAuthenticator.USER, auth.authenticate()); }
@Test public void testAuthenticate_fail() throws Exception { when(config.getAuthenticators()).thenReturn( ImmutableList.<Class<? extends Authenticator>>of(FailAuthenticator.class)); assertNull(auth.authenticate()); }
@Test public void testAuthenticate_passThenFail() throws Exception { when(config.getAuthenticators()).thenReturn( ImmutableList.of(PassAuthenticator.class, FailAuthenticator.class)); assertEquals(PassAuthenticator.USER, auth.authenticate()); }
@Test public void testAuthenticate_failThenPass() throws Exception { when(config.getAuthenticators()).thenReturn( ImmutableList.of(FailAuthenticator.class, PassAuthenticator.class)); assertEquals(PassAuthenticator.USER, auth.authenticate()); }