@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 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 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 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 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 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())); }
@Test public void cannotReturnWalletAccountInClosedWallet() { // Arrange: final WalletAccount account = new WalletAccount(); final TestContext context = new TestContext(); context.originalWallet.addOtherAccount(account); // Act: context.walletServices.open(context.pair); context.walletServices.close(context.pair.getName()); final WalletAccount resultAccount = context.walletServices.tryFindOpenAccount(account.getAddress()); // Assert: Assert.assertThat(resultAccount, IsNull.nullValue()); }
@Test public void closeRemovesWalletFromOpenedList() { // Arrange: final TestContext context = new TestContext(); // Act: context.walletServices.open(context.pair); context.walletServices.close(context.pair.getName()); // Assert: Assert.assertThat( context.walletServices.getOpenWalletNames(), IsEquivalent.equivalentTo(new WalletName[] {})); 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 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); }