### Question:
MultiTenantUserDetailsService implements UserDetailsService { @Override public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException { String[] orgTenantUsername = username.split(String.valueOf(Character.LINE_SEPARATOR)); if (orgTenantUsername == null || orgTenantUsername.length != 3) { throw new UsernameNotFoundException("Org, Tenant and Username must be provided."); } User user = userRepository.find(orgTenantUsername[0], orgTenantUsername[1], orgTenantUsername[2]); if (user == null) { throw new UsernameNotFoundException(String.format("Username[%s] not found for Org[%s] Tenant[%s].", orgTenantUsername[2], orgTenantUsername[0], orgTenantUsername[1])); } List<GrantedAuthority> authorities = user.getRoles().stream() .map(authority -> new SimpleGrantedAuthority("ROLE_" + authority.getRole())) .collect(Collectors.toList()); String authPrinciple = user.getOrg().getOrg() + String.valueOf(Character.LINE_SEPARATOR) + user.getTenant().getTenant() + String.valueOf(Character.LINE_SEPARATOR) + user.getUsername(); return new org.springframework.security.core.userdetails.User(authPrinciple, user.getPassword(), user.isEnabled(), true, true, true, authorities); }  @Override UserDetails loadUserByUsername(String username);  }### Answer:
@Test public void shouldThrowUsernameNotFoundExceptionWhenProvidedUsernameDoesNotContainOrgOrTenantOrUsername() { thrown.expect(UsernameNotFoundException.class); thrown.expectMessage("Org, Tenant and Username must be provided."); userDetailsService.loadUserByUsername("MyWithoutOrgTenantUsername"); }
@Test public void shouldThrowUsernameNotFoundExceptionWhenUserNotFoundInDB() { thrown.expect(UsernameNotFoundException.class); thrown.expectMessage("Username[MyUsername] not found for Org[MyOrg] Tenant[MyTenant]."); String providedUsername = "MyOrg" + String.valueOf(Character.LINE_SEPARATOR) + "MyTenant" + String.valueOf(Character.LINE_SEPARATOR) + "MyUsername"; userDetailsService.loadUserByUsername(providedUsername); }
@Test public void shouldReturnUserDetailsWhenUserFoundInDB() { Role role = new Role(); role.setRole("HELLO"); User userFromDB = new User(); userFromDB.setOrg(new Org("MyOrg")); userFromDB.setTenant(new Tenant("MyTenant")); userFromDB.setUsername("MyUsername"); userFromDB.setPassword("MyPass"); userFromDB.setDisplayName("MyName"); userFromDB.setRoles(Collections.singleton(role)); userFromDB.setEnabled(true); when(userRepositoryMock.find(anyString(), anyString(), anyString())).thenReturn(userFromDB); String providedUsername = "MyOrg" + String.valueOf(Character.LINE_SEPARATOR) + "MyTenant" + String.valueOf(Character.LINE_SEPARATOR) + "MyUsername"; UserDetails userDetails = userDetailsService.loadUserByUsername(providedUsername); assertEquals("MyOrg" + String.valueOf(Character.LINE_SEPARATOR) + "MyTenant" + String.valueOf(Character.LINE_SEPARATOR) + "MyUsername", userDetails.getUsername()); assertEquals("MyPass", userDetails.getPassword()); assertEquals("[ROLE_HELLO]", userDetails.getAuthorities().toString()); assertTrue(userDetails.isEnabled()); assertTrue(userDetails.isAccountNonExpired()); assertTrue(userDetails.isAccountNonLocked()); assertTrue(userDetails.isCredentialsNonExpired()); }