@Test
  public void deleteDeletesUnderlyingStorableEntityFile() throws IOException {
    // Arrange:
    final File file = new File(TEST_FILE_DIRECTORY, "to-be-deleted.bar");
    final TEntityFileDescriptor descriptor = this.createDescriptor(file);
    Assert.assertThat(file.createNewFile(), IsEqual.equalTo(true));

    // Act:
    descriptor.delete();

    // Assert:
    Assert.assertThat(file.exists(), IsEqual.equalTo(false));
  }
    @Test
    public void addressConstantIsConsistentWithNemesisBlock() {
      // Arrange:
      final Block block = this.loadNemesisBlock();
      final Address blockAddress = block.getSigner().getAddress();

      // Assert:
      Assert.assertThat(blockAddress, IsEqual.equalTo(NEMESIS_BLOCK_INFO.getAddress()));
      Assert.assertThat(
          blockAddress.getPublicKey(),
          IsEqual.equalTo(NEMESIS_BLOCK_INFO.getAddress().getPublicKey()));
      Assert.assertThat(blockAddress.getPublicKey(), IsNull.notNullValue());
    }
  @Test
  public void canChangeWalletNameAndPasswordIfWalletIsAlreadyOpen() {
    // Arrange:
    final WalletNamePasswordPair pair2 = createPair("n2", "p2");
    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:
    // - the original is opened and the target is created
    Mockito.verify(context.descriptorFactory, Mockito.times(2))
        .openExisting(context.pair, FILE_EXTENSION);
    Mockito.verify(context.descriptorFactory, Mockito.times(1)).createNew(pair2, FILE_EXTENSION);

    // - the original is loaded
    Mockito.verify(context.repository, Mockito.times(2)).load(context.descriptor);

    // - the target is saved
    final Wallet updatedWallet = context.walletServices.get(new WalletName("n2"));
    Assert.assertThat(updatedWallet.getName(), IsEqual.equalTo(new WalletName("n2")));
    // TODO BR: Fix this
    // Mockito.verify(context.repository, Mockito.times(1)).save(descriptor2, updatedWallet);
    Mockito.verify(context.repository, Mockito.times(1)).save(Mockito.any(), Mockito.any());

    // - the original wallet is deleted
    Mockito.verify(context.descriptor, Mockito.times(1)).delete();
  }
  @Test
  public void copyToCopiesBytesToGivenOutputStream() {
    // Arrange:
    final TestContext context = new TestContext();
    final WalletNamePasswordPair pair = createPair("n", "p");
    final ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
    Mockito.when(context.descriptor.openRead(StorableEntityReadMode.Raw))
        .thenReturn(new ByteArrayInputStream("test".getBytes()));

    // Act:
    context.walletServices.copyTo(pair, outputStream);

    // Assert:
    Assert.assertThat(outputStream.size(), IsEqual.equalTo(4));
    Assert.assertThat(outputStream.toString(), IsEqual.equalTo("test"));
  }
    @Test
    public void nemesisBlockIsVerifiable() {
      // Arrange:
      final Block block = this.loadNemesisBlock();

      // Assert:
      Assert.assertThat(block.verify(), IsEqual.equalTo(true));
    }
  @Test
  public void descriptorCanBeSerialized() {
    // Arrange:
    final File file =
        new File(Paths.get(TEST_FILE_DIRECTORY.toString(), "blah").toString(), "foo.bar");
    final TEntityFileDescriptor descriptor = this.createDescriptor(file);

    // Act:
    final JSONObject jsonObject = JsonSerializer.serializeToJson(descriptor);

    // Assert:
    Assert.assertThat(jsonObject.size(), IsEqual.equalTo(2));
    Assert.assertThat(jsonObject.get(descriptor.getName().getLabel()), IsEqual.equalTo("foo"));
    Assert.assertThat(
        jsonObject.get("location"),
        IsEqual.equalTo(Paths.get(TEST_FILE_DIRECTORY.toString(), "blah", "foo.bar").toString()));
  }
  @Test
  public void descriptorCanBeCreatedAroundStorableEntityWithMixedCaseExtension() {
    // Arrange:
    final File file =
        new File(Paths.get(TEST_FILE_DIRECTORY.toString(), "BlAh").toString(), "FoO.BaR");

    // Act:
    final TEntityFileDescriptor descriptor = this.createDescriptor(file);

    // Assert:
    Assert.assertThat(descriptor.getName(), IsEqual.equalTo(this.createEntityName("FoO")));
    Assert.assertThat(
        descriptor.getFileExtension(), IsEqual.equalTo(this.createEntityFileExtension(".BaR")));
    Assert.assertThat(
        descriptor.getStorableEntityLocation(),
        IsEqual.equalTo(Paths.get(TEST_FILE_DIRECTORY.toString(), "BlAh", "FoO.BaR").toString()));
  }
    @Test
    public void generationHashConstantIsConsistentWithNemesisBlock() {
      // Arrange:
      final Block block = this.loadNemesisBlock();

      // Assert:
      Assert.assertThat(
          block.getGenerationHash(), IsEqual.equalTo(NEMESIS_BLOCK_INFO.getGenerationHash()));
    }
    @Test
    public void nemesisTransactionsAreVerifiable() {
      // Arrange:
      final Block block = this.loadNemesisBlock();

      // Assert:
      for (final Transaction transaction : block.getTransactions()) {
        Assert.assertThat(transaction.verify(), IsEqual.equalTo(true));
      }
    }
  @Test
  public void getCanReturnOpenWallet() {
    // Arrange:
    final TestContext context = new TestContext();

    // Act:
    final Wallet wallet1 = context.walletServices.open(context.pair);
    final Wallet wallet2 = context.walletServices.get(context.pair.getName());

    // Assert:
    Assert.assertThat(wallet2, IsEqual.equalTo(wallet1));
  }
  @Test
  public void descriptorCanBeCreatedAroundUrlEncodedStorableEntityWithValidExtension() {
    // Arrange:
    final File file =
        new File(
            Paths.get(TEST_FILE_DIRECTORY.toString(), "blah").toString(),
            "%C3%B6%C3%A4%C3%BC%40.bar");

    // Act:
    final TEntityFileDescriptor descriptor = this.createDescriptor(file);

    // Assert:
    Assert.assertThat(descriptor.getName(), IsEqual.equalTo(this.createEntityName("öäü@")));
    Assert.assertThat(
        descriptor.getFileExtension(), IsEqual.equalTo(this.createEntityFileExtension(".bar")));
    Assert.assertThat(
        descriptor.getStorableEntityLocation(),
        IsEqual.equalTo(
            Paths.get(TEST_FILE_DIRECTORY.toString(), "blah", "%C3%B6%C3%A4%C3%BC%40.bar")
                .toString()));
  }
  @Test
  public void getStorableEntityLocationReturnsAbsolutePathToStorableEntity() {
    // Arrange:
    final File file =
        new File(Paths.get(TEST_FILE_DIRECTORY.toString(), "blah").toString(), "foo.bar");
    final TEntityFileDescriptor descriptor = this.createDescriptor(file);

    // Act:
    final String path = descriptor.getStorableEntityLocation();

    // Assert:
    Assert.assertThat(path, IsEqual.equalTo(file.getAbsolutePath()));
  }
    @Test
    public void nemesisTransactionsHaveCorrectFees() {
      // Arrange:
      final Block block = this.loadNemesisBlock();

      // Assert:
      for (final Transaction transaction : block.getTransactions()) {
        final Amount expectedFee =
            TransactionTypes.TRANSFER == transaction.getType()
                ? Amount.ZERO
                : NemGlobals.getTransactionFeeCalculator().calculateMinimumFee(transaction);
        Assert.assertThat(transaction.getFee(), IsEqual.equalTo(expectedFee));
      }
    }
    @Test
    public void amountConstantIsConsistentWithNemesisBlock() {
      // Act:
      Amount totalAmount = Amount.ZERO;
      final Block block = this.loadNemesisBlock();
      for (final Transaction transaction : block.getTransactions()) {
        if (transaction instanceof TransferTransaction) {
          totalAmount = totalAmount.add(((TransferTransaction) transaction).getAmount());
        }
      }

      // Assert:
      Assert.assertThat(totalAmount, IsEqual.equalTo(NEMESIS_BLOCK_INFO.getAmount()));
    }
  @Test
  public void canReturnWalletAccountInOpenWallet() {
    // Arrange:
    final WalletAccount account = new WalletAccount();
    final TestContext context = new TestContext();
    context.originalWallet.addOtherAccount(account);

    // Act:
    context.walletServices.open(context.pair);
    final WalletAccount resultAccount =
        context.walletServices.tryFindOpenAccount(account.getAddress());

    // Assert:
    Assert.assertThat(resultAccount, IsEqual.equalTo(account));
  }
    @Test
    public void nemesisBlockCanBeCreated() {
      // Act:
      final Block block = this.loadNemesisBlock();

      // Assert:
      Assert.assertThat(
          block.getSigner().getAddress(), IsEqual.equalTo(NEMESIS_BLOCK_INFO.getAddress()));
      Assert.assertThat(block.getType(), IsEqual.equalTo(-1));
      Assert.assertThat(block.getVersion(), IsEqual.equalTo(EXPECTED_VERSION));
      Assert.assertThat(block.getTimeStamp(), IsEqual.equalTo(TimeInstant.ZERO));

      // 2 multisig aggregate transactions
      Assert.assertThat(
          block.getTotalFee(), IsEqual.equalTo(EXPECTED_MULTISIG_AGGREGATE_FEE.multiply(2)));
      Assert.assertThat(block.getPreviousBlockHash(), IsEqual.equalTo(Hash.ZERO));
      Assert.assertThat(block.getHeight(), IsEqual.equalTo(BlockHeight.ONE));
      Assert.assertThat(block.getTransactions().size(), IsEqual.equalTo(NUM_NEMESIS_TRANSACTIONS));

      Assert.assertThat(block.getDifficulty(), IsEqual.equalTo(BlockDifficulty.INITIAL_DIFFICULTY));
      Assert.assertThat(block.getGenerationHash(), IsNull.notNullValue());
    }
  @Test
  public void openLoadsWalletIfWalletIsNotOpened() {
    // Arrange:
    final TestContext context = new TestContext();

    // Act:
    final Wallet wallet = context.walletServices.open(context.pair);

    // Assert:
    Assert.assertThat(wallet.getName(), IsEqual.equalTo(context.originalWallet.getName()));
    Assert.assertThat(
        context.walletServices.getOpenWalletNames(),
        IsEquivalent.equivalentTo(Collections.singletonList(context.originalWallet.getName())));
    Mockito.verify(context.descriptorFactory, Mockito.times(1))
        .openExisting(context.pair, FILE_EXTENSION);
    Mockito.verify(context.repository, Mockito.times(1)).load(context.descriptor);
  }
    @Test
    public void nemesisAddressesAreValid() {
      // Arrange:
      final Block block = this.loadNemesisBlock();

      // Act:
      final Set<Address> allAddresses =
          block
              .getTransactions()
              .stream()
              .flatMap(t -> t.getAccounts().stream().map(Account::getAddress))
              .collect(Collectors.toSet());

      // Assert:
      for (final Address address : allAddresses) {
        Assert.assertThat(address.toString(), address.isValid(), IsEqual.equalTo(true));
      }
    }
  @Test
  public void renamedWalletPreservesAccountInformation() {
    // Arrange:
    final WalletNamePasswordPair pair2 = createPair("n2", "p");
    final WalletDescriptor descriptor2 = createDescriptor("n2");
    final TestContext context = new TestContext();
    context.originalWallet.addOtherAccount(new WalletAccount());
    context.originalWallet.addOtherAccount(new WalletAccount());
    Mockito.when(context.descriptorFactory.createNew(pair2, FILE_EXTENSION))
        .thenReturn(descriptor2);

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

    // Assert:
    final Wallet updatedWallet = context.walletServices.get(new WalletName("n2"));
    Assert.assertThat(
        updatedWallet.getPrimaryAccount(),
        IsEqual.equalTo(context.originalWallet.getPrimaryAccount()));
    Assert.assertThat(
        updatedWallet.getOtherAccounts(),
        IsEquivalent.equivalentTo(context.originalWallet.getOtherAccounts()));
  }