### Question:
DefaultUserService implements UserService { @Override public void setAuthenticatedUser(String userId) { final User user = userRepository.get(userId); if (user == null) { throw new UsernameNotFoundException("User with id '" + userId + "' was not found!"); } SecurityContext securityContext = createContext(user); SecurityContextHolder.setContext(securityContext); } @Autowired  DefaultUserService(PageRepository pageRepository,
                              UserRepository userRepository,
                              WidgetRepository widgetRepository,
                              PageTemplateRepository pageTemplateRepository,
                              CategoryRepository categoryRepository,
                              PersonRepository personRepository); @Override UserDetails loadUserByUsername(String username); @Override User getAuthenticatedUser(); @Override void setAuthenticatedUser(String userId); @Override void clearAuthenticatedUser(); @Override @Transactional void registerNewUser(User user); @Override User getUserById(String id); @Override User getUserByUsername(String userName); @Override User getUserByEmail(String userEmail); @Override User getUserByOpenId(String openId); @Override @Transactional void updateUserProfile(User user); @Override SearchResult<User> getAll(); @Override SearchResult<User> getLimitedList(int offset, int pageSize); @Override SearchResult<Person> getLimitedListOfPersons(int offset, int pageSize); @Override SearchResult<User> getUsersByFreeTextSearch(String searchTerm, int offset, int pageSize); @Override SearchResult<Person> getPersonsByFreeTextSearch(String searchTerm, int offset, int pageSize); @Override List<Person> getAllPeople(); @Override @Transactional // TODO RAVE-300: add security check that is is called by admin or the user itself void deleteUser(String userId); @Override List<Person> getAllByAddedWidget(String widgetId); @Override void updatePassword(User newUser); @Override void sendUserNameReminder(User newUser); @Override void sendPasswordReminder(User newUser); @Override boolean isValidReminderRequest(String forgotPasswordHash, int nrOfMinutesValid); @Override @Transactional boolean addFriend(String friendUsername, String username); @Override @Transactional void removeFriend(String friendUsername, String username); @Override HashMap<String, List<Person>> getFriendsAndRequests(String username); @Override List<Person> getFriendRequestsReceived(String username); @Override @Transactional boolean acceptFriendRequest(String friendUsername, String username); @Override UserDetails loadUserDetails(OpenIDAuthenticationToken token);  }### Answer:
@Test public void setAuthenticatedUser_valid() { final User authUser = new UserImpl(USER_ID); expect(userRepository.get(USER_ID)).andReturn(authUser).anyTimes(); replay(userRepository); service.setAuthenticatedUser(USER_ID); assertThat((User) SecurityContextHolder.getContext().getAuthentication().getPrincipal(), is(sameInstance(authUser))); verify(userRepository); }
@Test public void setAuthenticatedUser_validRole() { final User authUser = new UserImpl(USER_ID); final Authority userRole = new AuthorityImpl(); userRole.setAuthority("admin"); authUser.addAuthority(userRole); expect(userRepository.get(USER_ID)).andReturn(authUser).anyTimes(); replay(userRepository); service.setAuthenticatedUser(USER_ID); assertThat((User) SecurityContextHolder.getContext().getAuthentication().getPrincipal(), is(sameInstance(authUser))); final GrantedAuthority grantedAuthority = SecurityContextHolder.getContext().getAuthentication().getAuthorities().iterator().next(); assertEquals("has authority admin", "admin", grantedAuthority.getAuthority()); verify(userRepository); }
@Test(expected = UsernameNotFoundException.class) public void setAuthenticatedUser_invalid_null() { expect(userRepository.get(USER_ID)).andReturn(null).anyTimes(); replay(userRepository); service.setAuthenticatedUser(USER_ID); verify(userRepository); }
@Test(expected = UsernameNotFoundException.class) public void loadByUsername_invalid_null() { expect(userRepository.get(USER_ID)).andReturn(null).anyTimes(); replay(userRepository); service.setAuthenticatedUser(USER_ID); verify(userRepository); }