@Test
public void testExternalUserShouldBeReplacedWhenExpired()
{

    // Given: сached external user
    UserDto user = new UserDto();
    user.setUsername(TEST_DUPLICATE_USER);
    user.setPassword("foobarpasswrod");
    user.setSourceId("someExternalUserSourceId");
    user.setLastUpdate(new Date());
    strongboxUserManager.cacheExternalUserDetails("someExternalUserSourceId", new StrongboxUserDetails(user));
    
    // Check that external user cached
    UserDetails userDetails = userDetailsService.loadUserByUsername(TEST_DUPLICATE_USER);
    assertThat(userDetails).isNotNull();
    assertThat(userDetails).isInstanceOf(SpringSecurityUser.class);
    assertThat(((SpringSecurityUser)userDetails).getSourceId()).isEqualTo("someExternalUserSourceId");
    
    // Make this user expired
    expireUser(TEST_DUPLICATE_USER);
    // Expired user should be replaced with user from yaml
    userDetails = userDetailsService.loadUserByUsername(TEST_DUPLICATE_USER);
    assertThat(userDetails).isNotNull();
    assertThat(userDetails.getUsername()).isEqualTo(TEST_DUPLICATE_USER);
    assertThat(userDetails).isInstanceOf(SpringSecurityUser.class);
    assertThat(((SpringSecurityUser)userDetails).getSourceId()).isEqualTo("yamlUserDetailService");
}