ReceiptTableActionAlterations extends StubTableActionAlterations<Receipt> { @NonNull @Override public Single<Receipt> postUpdate(@NonNull final Receipt oldReceipt, @Nullable final Receipt newReceipt) { return Single.fromCallable(() -> { if (newReceipt == null) { throw new Exception("Post update failed due to a null receipt"); } if (oldReceipt.getFile() != null) { if (newReceipt.getFile() == null || !newReceipt.getFile().equals(oldReceipt.getFile())) { picasso.get().invalidate(oldReceipt.getFile()); storageManager.delete(oldReceipt.getFile()); } } return newReceipt; }); } ReceiptTableActionAlterations(@NonNull Context context,
                                         @NonNull ReceiptsTable receiptsTable,
                                         @NonNull StorageManager storageManager,
                                         @NonNull Lazy<Picasso> picasso);   ReceiptTableActionAlterations(@NonNull Context context,
                                  @NonNull ReceiptsTable receiptsTable,
                                  @NonNull StorageManager storageManager,
                                  @NonNull BuilderFactory1<Receipt, ReceiptBuilderFactory> receiptBuilderFactoryFactory,
                                  @NonNull Lazy<Picasso> picasso); @NonNull @Override Single<Receipt> preInsert(@NonNull final Receipt receipt); @NonNull @Override Single<Receipt> preUpdate(@NonNull final Receipt oldReceipt, @NonNull final Receipt newReceipt); @NonNull @Override Single<Receipt> postUpdate(@NonNull final Receipt oldReceipt, @Nullable final Receipt newReceipt); @NonNull @Override Single<Receipt> postDelete(@Nullable final Receipt receipt); @NonNull Single<Receipt> preCopy(@NonNull final Receipt receipt, @NonNull final Trip toTrip); void postCopy(@NonNull Receipt oldReceipt, @Nullable Receipt newReceipt); @NonNull Single<Receipt> preMove(@NonNull final Receipt receipt, @NonNull final Trip toTrip); void postMove(@NonNull Receipt oldReceipt, @Nullable Receipt newReceipt);  }
@Test public void postUpdateNull() throws Exception { receiptTableActionAlterations.postUpdate(receipt, null) .test() .assertNoValues() .assertNotComplete() .assertError(Exception.class); verifyZeroInteractions(storageManager); }
@Test public void postUpdateSuccessWithoutFile() throws Exception { final Receipt updatedReceipt = mock(Receipt.class); receiptTableActionAlterations.postUpdate(receipt, updatedReceipt) .test() .assertValue(updatedReceipt) .assertComplete() .assertNoErrors(); verifyZeroInteractions(storageManager); }
@Test public void postUpdateSuccessWithSameFile() throws Exception { final Receipt updatedReceipt = mock(Receipt.class); final File file = new File("abc"); when(receipt.getFile()).thenReturn(file); when(updatedReceipt.getFile()).thenReturn(file); receiptTableActionAlterations.postUpdate(receipt, updatedReceipt) .test() .assertValue(updatedReceipt) .assertComplete() .assertNoErrors(); verify(storageManager, never()).delete(file); }
@Test public void postUpdateSuccessWithNewFile() throws Exception { final Receipt updatedReceipt = mock(Receipt.class); final File file = new File("abc"); final File newFile = new File("efg"); when(receipt.getFile()).thenReturn(file); when(updatedReceipt.getFile()).thenReturn(newFile); receiptTableActionAlterations.postUpdate(receipt, updatedReceipt) .test() .assertValue(updatedReceipt) .assertComplete() .assertNoErrors(); verify(storageManager).delete(file); verify(picasso).invalidate(file); }