### Question:
GatewayLoginProvider implements AuthenticationProvider { @Override public Authentication authenticate(Authentication authentication) { String username = authentication.getPrincipal().toString(); Optional<String> token = gatewaySecurityService.login(username, authentication.getCredentials().toString()); if (!token.isPresent()) { throw new BadCredentialsException("Username or password are invalid."); } TokenAuthentication tokenAuthentication = new TokenAuthentication(username, token.get()); tokenAuthentication.setAuthenticated(true); return tokenAuthentication; }  @Override Authentication authenticate(Authentication authentication); @Override boolean supports(Class<?> auth);  }### Answer:
@Test public void shouldAuthenticateValidUsernamePassword() { when(gatewaySecurityService.login(USER, VALID_PASSWORD)).thenReturn(Optional.of(VALID_TOKEN)); Authentication auth = new UsernamePasswordAuthenticationToken(USER, VALID_PASSWORD); Authentication processedAuthentication = gatewayLoginProvider.authenticate(auth); assertTrue(processedAuthentication instanceof TokenAuthentication); assertTrue(processedAuthentication.isAuthenticated()); assertEquals(VALID_TOKEN, processedAuthentication.getCredentials()); assertEquals(USER, processedAuthentication.getName()); }
@Test(expected = BadCredentialsException.class) public void shouldThrowWithInvalidUsernamePassword() { when(gatewaySecurityService.login(USER, INVALID_PASSWORD)).thenReturn(Optional.empty()); Authentication auth = new UsernamePasswordAuthenticationToken(USER, INVALID_PASSWORD); gatewayLoginProvider.authenticate(auth); }