FileBasedUserRepository implements UserRepository<U> { @Override public Optional<U> findUserByName(String name) { return users.get(name); } FileBasedUserRepository(Class<U> userClass, ObjectMapper mapper, U defaultAdmin,
                                   Path usersPath, Path credentialsPath, boolean reloadOnChange); @Override Optional<U> findUserByName(String name); @Override Optional<String> findCredentialByUserName(String userName); @Override boolean isAdminDefined(); @Override U defaultAdmin();  }
@Test public void should_load_users() throws Exception { FileBasedUserRepository<StdUser> repo = newRepo(false); Files.copy(getClass().getResourceAsStream("users.json"), usersPath, StandardCopyOption.REPLACE_EXISTING); assertThat(repo.findUserByName("admin").isPresent()).isFalse(); checkUser(repo.findUserByName("john"), "john", "role1"); }
@Test public void should_reload_users() throws Exception { FileBasedUserRepository<StdUser> repo = newRepo(true); Files.copy(getClass().getResourceAsStream("users.json"), usersPath, StandardCopyOption.REPLACE_EXISTING); checkUser(repo.findUserByName("john"), "john", "role1"); assertThat(repo.findUserByName("jane").isPresent()).isFalse(); Thread.sleep(1000); Files.copy(getClass().getResourceAsStream("users2.json"), usersPath, StandardCopyOption.REPLACE_EXISTING); checkUser(repo.findUserByName("john"), "john", "role2"); checkUser(repo.findUserByName("jane"), "jane", "role1", "role2"); }
@Test public void should_not_reload_users() throws Exception { FileBasedUserRepository<StdUser> repo = newRepo(false); Files.copy(getClass().getResourceAsStream("users.json"), usersPath, StandardCopyOption.REPLACE_EXISTING); checkUser(repo.findUserByName("john"), "john", "role1"); assertThat(repo.findUserByName("jane").isPresent()).isFalse(); Thread.sleep(1000); Files.copy(getClass().getResourceAsStream("users2.json"), usersPath, StandardCopyOption.REPLACE_EXISTING); checkUser(repo.findUserByName("john"), "john", "role1"); assertThat(repo.findUserByName("jane").isPresent()).isFalse(); }