### Question:
FileRoleRepository extends AbstractRoleRepository implements FileRepository { @Override public void start() throws Throwable { clear(); FileRepository.assertNotMigrated( roleFile, fileSystem, log ); ListSnapshot<RoleRecord> onDiskRoles = readPersistedRoles(); if ( onDiskRoles != null ) { setRoles( onDiskRoles ); } } FileRoleRepository( FileSystemAbstraction fileSystem, File file, LogProvider logProvider ); @Override void start(); @Override ListSnapshot<RoleRecord> getPersistedSnapshot(); @Override void purge(); @Override void markAsMigrated();  }### Answer:
@Test public void shouldPersistRoles() throws Throwable { RoleRecord role = new RoleRecord( "admin", "craig", "karl" ); roleRepository.create( role ); roleRepository = new FileRoleRepository( fs, roleFile, logProvider ); roleRepository.start(); RoleRecord resultByName = roleRepository.getRoleByName( role.name() ); assertThat( resultByName, equalTo( role ) ); }
@Test public void shouldRecoverIfCrashedDuringMove() throws Throwable { final IOException exception = new IOException( "simulated IO Exception on create" ); FileSystemAbstraction crashingFileSystem = new DelegatingFileSystemAbstraction( fs ) { @Override public void renameFile( File oldLocation, File newLocation, CopyOption... copyOptions ) throws IOException { if ( roleFile.getName().equals( newLocation.getName() ) ) { throw exception; } super.renameFile( oldLocation, newLocation, copyOptions ); } }; roleRepository = new FileRoleRepository( crashingFileSystem, roleFile, logProvider ); roleRepository.start(); RoleRecord role = new RoleRecord( "admin", "jake" ); try { roleRepository.create( role ); fail( "Expected an IOException" ); } catch ( IOException e ) { assertSame( exception, e ); } assertFalse( crashingFileSystem.fileExists( roleFile ) ); assertThat( crashingFileSystem.listFiles( roleFile.getParentFile() ).length, equalTo( 0 ) ); }
@Test public void shouldFailOnReadingInvalidEntries() throws Throwable { AssertableLogProvider logProvider = new AssertableLogProvider(); fs.mkdirs( roleFile.getParentFile() ); FileRepositorySerializer.writeToFile( fs, roleFile, UTF8.encode( "neo4j:admin\n" + "admin:admin:\n" ) ); roleRepository = new FileRoleRepository( fs, roleFile, logProvider ); thrown.expect( IllegalStateException.class ); thrown.expectMessage( startsWith( "Failed to read role file '" ) ); try { roleRepository.start(); } catch ( IllegalStateException e ) { assertThat( roleRepository.numberOfRoles(), equalTo( 0 ) ); logProvider.assertExactly( AssertableLogProvider.inLog( FileRoleRepository.class ).error( "Failed to read role file \"%s\" (%s)", roleFile.getAbsolutePath(), "wrong number of line fields [line 2]" ) ); throw e; } }
@Test public void shouldNotAddEmptyUserToRole() throws Throwable { fs.mkdirs( roleFile.getParentFile() ); FileRepositorySerializer.writeToFile( fs, roleFile, UTF8.encode( "admin:neo4j\nreader:\n" ) ); roleRepository = new FileRoleRepository( fs, roleFile, logProvider ); roleRepository.start(); RoleRecord role = roleRepository.getRoleByName( "admin" ); assertTrue( "neo4j should be assigned to 'admin'", role.users().contains( "neo4j" ) ); assertEquals( "only one admin should exist", 1, role.users().size() ); role = roleRepository.getRoleByName( "reader" ); assertTrue( "no users should be assigned to 'reader'", role.users().isEmpty() ); }