UserServiceImpl implements UserService { @Override public UserIndex completePhoneNumberRegistration(UserIndex userIndex, String registrationCode) { UserRegistration registration = _userIndexRegistrationService.getRegistrationForUserIndexKey(userIndex.getId()); if (registration == null) return null; String expectedCode = registration.getRegistrationCode(); if (!expectedCode.equals(registrationCode)) return null; _userIndexRegistrationService.clearRegistrationForUserIndexKey(userIndex.getId()); User targetUser = _userDao.getUserForId(registration.getUserId()); if (targetUser == null) return null; User phoneUser = userIndex.getUser(); if (phoneUser.equals(targetUser)) return userIndex; if (phoneUser.getUserIndices().size() == 1) { mergeUsers(userIndex.getUser(), targetUser); } else { userIndex.setUser(targetUser); targetUser.getUserIndices().add(userIndex); phoneUser.getUserIndices().remove(userIndex); _userDao.saveOrUpdateUsers(phoneUser, targetUser); } return getUserIndexForId(userIndex.getId()); }  @Autowired void setUserDao(UserDao dao); @Autowired void setAuthoritiesService(
      StandardAuthoritiesService authoritiesService); @Autowired void setUserPropertiesService(
      UserPropertiesService userPropertiesService); @Autowired void setUserIndexRegistrationService(
      UserIndexRegistrationService userIndexRegistrationService); @Autowired void setPasswordEncoder(PasswordEncoder passwordEncoder); @PostConstruct void start(); @PreDestroy void stop(); @Override int getNumberOfUsers(); @Override List<Integer> getAllUserIds(); @Override List<Integer> getAllUserIdsInRange(int offset, int limit); @Override int getNumberOfAdmins(); @Override User getUserForId(int userId); @Override UserBean getUserAsBean(User user); @Override UserBean getAnonymousUser(); @Override void resetUser(User user); @Override void deleteUser(User user); @Override boolean isAnonymous(User user); @Override boolean isAdministrator(User user); @Override void enableAdminRoleForUser(User user, boolean onlyIfNoOtherAdmins); void disableAdminRoleForUser(User user, boolean onlyIfOtherAdmins); @Override List<String> getUserIndexKeyValuesForKeyType(String keyType); @Override Integer getApiKeyCount(); @Override List<User> getApiKeys(final int start, final int maxResults); @Override UserIndex getOrCreateUserForIndexKey(UserIndexKey key,
      String credentials, boolean isAnonymous); @Override UserIndex getOrCreateUserForUsernameAndPassword(String username,
      String password); @Override UserIndex getUserIndexForId(UserIndexKey key); @Override UserIndex getUserIndexForUsername(String username); @Override UserIndex addUserIndexToUser(User user, UserIndexKey key,
      String credentials); @Override void removeUserIndexForUser(User user, UserIndexKey key); @Override void setCredentialsForUserIndex(UserIndex userIndex, String credentials); @Override void setPasswordForUsernameUserIndex(UserIndex userIndex,
      String password); @Override void mergeUsers(User sourceUser, User targetUser); @Override String registerPhoneNumber(User user, String phoneNumber); @Override boolean hasPhoneNumberRegistration(UserIndexKey userIndexKey); @Override UserIndex completePhoneNumberRegistration(UserIndex userIndex,
      String registrationCode); @Override void clearPhoneNumberRegistration(UserIndexKey userIndexKey); @Override void startUserPropertiesMigration(); @Override UserPropertiesMigrationStatus getUserPropertiesMigrationStatus(); @Override void deleteStaleUsers(); @Override boolean isDeletingStaleUsers(); @Override void cancelDeleteStaleUsers(); @Override long getNumberOfStaleUsers(); @Cacheable @Transactional @Override Long getMinApiRequestIntervalForKey(String key,
      @CacheableArgument(cacheRefreshIndicator = true) boolean forceRefresh);  }
@Test public void testCompletePhoneNumberRegistration() { User userA = createUser(1234); UserIndexKey key = new UserIndexKey(UserIndexTypes.PHONE_NUMBER, "12065551234"); UserDao userDao = Mockito.mock(UserDao.class); _service.setUserDao(userDao); Mockito.when(userDao.getUserForId(1234)).thenReturn(userA); UserIndex migratedIndex = new UserIndex(); migratedIndex.setId(key); migratedIndex.setUser(userA); migratedIndex.setCredentials(""); Mockito.when(userDao.getUserIndexForId(key)).thenReturn(migratedIndex); UserIndexRegistrationService registrationService = Mockito.mock(UserIndexRegistrationService.class); _service.setUserIndexRegistrationService(registrationService); UserRegistration registration = new UserRegistration(1234, "5555"); Mockito.when(registrationService.getRegistrationForUserIndexKey(key)).thenReturn( registration); UserPropertiesService userPropertiesService = Mockito.mock(UserPropertiesService.class); _service.setUserPropertiesService(userPropertiesService); User userB = createUser(1235); UserIndex index = createUserIndex(key.getType(), key.getValue(), userB); UserIndex updated = _service.completePhoneNumberRegistration(index, "5554"); assertTrue(updated == null); updated = _service.completePhoneNumberRegistration(index, "5555"); assertTrue(updated != null); Mockito.verify(userPropertiesService).mergeProperties(userB, userA); }