### Question:
UserManager implements Serializable { public ResultCode confirmRegistration(String email, String registrationControlCode) { User user = databaseModel.getUserByEmail(email); ResultCode checkResult = checkRegistrationConfirmationPreconditions(user, registrationControlCode); if (checkResult != ResultCode.OK) { return checkResult; } boolean ok = databaseModel.confirmUserRegistration(email); if (!ok) { LOGGER.error("DB error: Failed to confirm registration for email {}.", email); return ResultCode.DATABASE_ERROR; } return ResultCode.OK; } UserManager(); ResultCode cancelPasswordResetTokens(int userId); ResultCode cancelRegistration(int userId, String password); ResultCode changePassword(int userId, String oldPassword, String newPassword); PasswordCheckResult checkPassword(String password, String passwordConfirmation); ResultCode confirmManualRegistration(String email, String registrationControlCode, String password); ResultCode confirmRegistration(String email, String registrationControlCode); ResultCode createPasswordResetToken(String email); User getCurrentUser(); int getRegisteredUserCount(Date since); boolean isPasswordResetTokenValid(String email, String tokenKey); boolean isPasswordValid(int userId, String password); ResultCode logIn(String userNameOrEmail, String password, String usersIp); User loginOrRegisterWithSocialAccount(SocialAccountDetails details, String usersIp); ResultCode logInWithoutPassword(String userNameOrEmail); void logOut(); ResultCode registerUser(String email, String name, String password, boolean registrationConfirmed); ResultCode registerUserManually(String email, String name, int rank); ResultCode resendRegistrationEmail(String address); ResultCode resetPassword(String userEmail, String tokenKey, String newPassword); boolean sendTestingEmail(EmailType emailType, String address);  }### Answer:
@Test public void testConfirmRegistrationInvalidEmail() { Mockito.when(databaseModel.getUserByEmail("john@example.com")).thenReturn(null); ResultCode result = userManager.confirmRegistration("john@example.com", storedUser.getRegistrationControlCode()); Assert.assertEquals(ResultCode.NO_SUCH_USER, result); Mockito.verify(databaseModel, Mockito.never()).confirmUserRegistration("john@example.com"); }
@Test public void testConfirmRegistrationInvalidControlCode() { Mockito.when(databaseModel.getUserByEmail("john@example.com")).thenReturn(userWithUnfinishedRegistration); String invalidControlCode = "d9d8172ffa4e21f955e8ad125f9dbc32"; ResultCode result = userManager.confirmRegistration("john@example.com", invalidControlCode); Assert.assertEquals(ResultCode.INVALID_REGISTRATION_CONTROL_CODE, result); Mockito.verify(databaseModel, Mockito.never()).confirmUserRegistration("john@example.com"); }
@Test public void testConfirmRegistrationAlreadyConfirmed() { Mockito.when(databaseModel.getUserByEmail("john@example.com")).thenReturn(storedUser); ResultCode result = userManager.confirmRegistration("john@example.com", storedUser.getRegistrationControlCode()); Assert.assertEquals(ResultCode.REGISTRATION_ALREADY_CONFIRMED, result); Mockito.verify(databaseModel, Mockito.never()).confirmUserRegistration("john@example.com"); }
@Test public void testConfirmRegistration() { Mockito.when(databaseModel.getUserByEmail("john@example.com")).thenReturn(userWithUnfinishedRegistration); Mockito.when(databaseModel.confirmUserRegistration("john@example.com")).thenReturn(true); ResultCode result = userManager.confirmRegistration("john@example.com", userWithUnfinishedRegistration.getRegistrationControlCode()); Assert.assertEquals(ResultCode.OK, result); Mockito.verify(databaseModel).confirmUserRegistration("john@example.com"); }