### Question:
DatastoreRealm extends AuthorizingRealm { @Override protected AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken token) throws AuthenticationException { if (!(token instanceof UsernamePasswordToken)) { throw new AuthenticationException( "The provided token is not a UsernamePasswordToken"); } UsernamePasswordToken usernamePasswordToken = (UsernamePasswordToken) token; String username = usernamePasswordToken.getUsername(); if (username == null) { throw new AuthenticationException( "The provided token does not contain a username"); } User user = getUserByUsernameOrEmail(username); if (user == null) { LOG.warn("User with the specified username does not exist: " + username); throw new AuthenticationException("The username/password was invalid"); } if (!user.getEnabled()) { throw new AuthenticationException("The user account is disabled"); } SimpleAccount info = new SimpleAccount(username, user.getPassword(), new SimpleByteSource(user.getPasswordSalt()), getName()); SimplePrincipalCollection principals = new SimplePrincipalCollection(); principals.add(user.getId(), getName()); principals.add(user.getUsername(), getName()); principals.add(user.getEmail(), getName()); info.setPrincipals(principals); return info; } @Inject  DatastoreRealm(UserService userService, RoleService roleService,
            CredentialsMatcher credentialsMatcher); @Override CredentialsMatcher getCredentialsMatcher(); void destroy();  }### Answer:
@Test public void invalidAuthenticationTokenType() { expectedEx.expect(AuthenticationException.class); expectedEx.expectMessage("The provided token is not a UsernamePasswordToken"); realm.doGetAuthenticationInfo(null); }
@Test public void missingAuthenticationCredentials() { expectedEx.expect(AuthenticationException.class); expectedEx.expectMessage("The provided token does not contain a username"); realm.doGetAuthenticationInfo(new UsernamePasswordToken()); }
@Test public void invalidAuthenticationCredentials() { expectedEx.expect(AuthenticationException.class); expectedEx.expectMessage("The username/password was invalid"); realm.doGetAuthenticationInfo(new UsernamePasswordToken("unknownUser", "password")); }
@Test public void disabledUserAuthentication() { mockedUser.setEnabled(false); expectedEx.expect(AuthenticationException.class); expectedEx.expectMessage("The user account is disabled"); realm.doGetAuthenticationInfo(new UsernamePasswordToken( mockedUser.getUsername(), mockedUser.getPassword())); }
@Test public void validAuthenticationCredentialsUsingUsername() { AuthenticationInfo authInfo = realm.doGetAuthenticationInfo( new UsernamePasswordToken(mockedUser.getUsername(), mockedUser.getPassword())); assertNotNull("Auth info should not be null with valid username", authInfo); assertTrue("Users ID, username, and email should be include in the auth info principals", authInfo.getPrincipals().asSet().containsAll(Sets.newHashSet( mockedUser.getId(), mockedUser.getUsername(), mockedUser.getEmail()))); }
@Test public void validAuthenticationCredentialsUsingEmail() { AuthenticationInfo authInfo = realm.doGetAuthenticationInfo( new UsernamePasswordToken(mockedUser.getUsername(), mockedUser.getPassword())); assertNotNull("Auth info should not be null with valid email", authInfo); assertTrue("Users ID, username, and email should be include in the auth info principals", authInfo.getPrincipals().asSet().containsAll(Sets.newHashSet( mockedUser.getId(), mockedUser.getUsername(), mockedUser.getEmail()))); }