@Test
 public void descriptorCannotBeCreatedAroundDirectory() {
   // Assert:
   ExceptionAssert.assertThrowsStorageException(
       v -> this.createDescriptor(TEST_FILE_DIRECTORY),
       this.getExceptionClass(),
       this.getExceptionValue(
           StorableEntityStorageException.Code.STORABLE_ENTITY_CANNOT_BE_DIRECTORY.value()));
 }
 @Test
 public void descriptorCannotBeCreatedAroundStorableEntityWithInvalidExtension() {
   // Assert:
   ExceptionAssert.assertThrowsStorageException(
       v -> this.createDescriptor(TEST_FILE_BAD_EXT),
       this.getExceptionClass(),
       this.getExceptionValue(
           StorableEntityStorageException.Code.STORABLE_ENTITY_HAS_INVALID_EXTENSION.value()));
 }
  @Test
  public void openReadThrowsIfModeIsNotRaw() {
    // Arrange:
    final TEntityFileDescriptor descriptor = this.createDescriptor(TEST_FILE);

    // Act + Assert:
    ExceptionAssert.assertThrows(
        v -> descriptor.openRead(StorableEntityReadMode.Decode), IllegalArgumentException.class);
  }
  @Test
  public void openReadCannotOpenFileThatDoesNotExist() {
    // Arrange:
    final File file = new File(TEST_FILE_DIRECTORY, "imaginary-read.bar");
    final TEntityFileDescriptor descriptor = this.createDescriptor(file);

    // Act:
    ExceptionAssert.assertThrowsStorageException(
        v -> descriptor.openRead(),
        this.getExceptionClass(),
        this.getExceptionValue(
            StorableEntityStorageException.Code.STORABLE_ENTITY_DOES_NOT_EXIST.value()));
  }
  @Test
  public void getCannotReturnClosedWallet() {
    // Arrange:
    final TestContext context = new TestContext();

    // Act:
    context.walletServices.open(context.pair);
    context.walletServices.close(context.pair.getName());

    // Act:
    ExceptionAssert.assertThrowsNccException(
        v -> context.walletServices.get(context.pair.getName()),
        NccException.Code.WALLET_IS_NOT_OPEN);
  }
  @Test
  public void openFailsIfCachedWalletPasswordIsIncorrect() {
    // Arrange:
    final TestContext context = new TestContext();
    context.walletServices.open(context.pair); // cache the wallet
    Mockito.when(context.repository.load(context.descriptor))
        .thenThrow(
            new WalletStorageException(WalletStorageException.Code.WALLET_PASSWORD_INCORRECT));

    // Act:
    ExceptionAssert.assertThrowsWalletStorageException(
        v -> context.walletServices.open(context.pair),
        WalletStorageException.Code.WALLET_PASSWORD_INCORRECT);
  }
  @Test
  public void openWriteCannotOpenFileThatIsInvalid() {
    // Arrange:
    final File file = Mockito.mock(File.class);
    Mockito.when(file.getName()).thenReturn("fo\0o.bar");
    Mockito.when(file.getAbsolutePath()).thenReturn("fo\0o.bar");
    final TEntityFileDescriptor descriptor = this.createDescriptor(file);

    // Act:
    ExceptionAssert.assertThrowsStorageException(
        v -> descriptor.openWrite(),
        this.getExceptionClass(),
        this.getExceptionValue(
            StorableEntityStorageException.Code.STORABLE_ENTITY_COULD_NOT_BE_SAVED.value()));
  }
  @Test
  public void deleteRaisesExceptionIfFileCannotBeDeleted() {
    // Arrange:
    final File file = Mockito.mock(File.class);
    Mockito.when(file.getName()).thenReturn("foo.bar");
    Mockito.when(file.getAbsolutePath()).thenReturn("foo.bar");
    final TEntityFileDescriptor descriptor = this.createDescriptor(file);

    Mockito.when(file.delete()).thenReturn(false);

    // Act:
    ExceptionAssert.assertThrowsStorageException(
        v -> descriptor.delete(),
        this.getExceptionClass(),
        this.getExceptionValue(
            StorableEntityStorageException.Code.STORABLE_ENTITY_COULD_NOT_BE_DELETED.value()));
  }
  @Test
  public void renamedWalletIsNotAccessibleByOldName() {
    // Arrange:
    final WalletNamePasswordPair pair2 = createPair("n2", "p");
    final WalletDescriptor descriptor2 = createDescriptor("n2");
    final TestContext context = new TestContext();
    Mockito.when(context.descriptorFactory.createNew(pair2, FILE_EXTENSION))
        .thenReturn(descriptor2);

    // Act:
    context.walletServices.open(context.pair);
    context.walletServices.move(context.pair, pair2);

    // Assert:
    Assert.assertThat(context.walletServices.get(new WalletName("n2")), IsNull.notNullValue());
    ExceptionAssert.assertThrowsNccException(
        v -> context.walletServices.get(new WalletName("n")), NccException.Code.WALLET_IS_NOT_OPEN);
  }