ReceiptTableActionAlterations extends StubTableActionAlterations<Receipt> { @NonNull @Override public Single<Receipt> preInsert(@NonNull final Receipt receipt) { return receiptsTable.get(receipt.getTrip()) .map(existingReceipts -> { final ReceiptBuilderFactory factory = receiptBuilderFactoryFactory.build(receipt).setIndex(getNextReceiptIndex(receipt)); final long customOrderId = ReceiptsOrderer.getCustomOrderId(receipt, existingReceipts); return updateReceiptFileNameBlocking(factory.setCustomOrderId(customOrderId).build()); }); } 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 preInsertWithoutFile() { final String name = "name"; final List<Receipt> receiptsInTrip = Arrays.asList(mock(Receipt.class), mock(Receipt.class), mock(Receipt.class)); when(receiptsTable.get(trip)).thenReturn(Single.just(receiptsInTrip)); when(receipt.getName()).thenReturn(name); receiptTableActionAlterations.preInsert(receipt) .test() .assertValue(receipt) .assertComplete() .assertNoErrors(); assertNull(receipt.getFile()); verify(receiptBuilderFactory).setIndex(4); verify(receiptBuilderFactory).setCustomOrderId(CUSTOM_ORDER_ID); }
@Test public void preInsertWithFile() { final String name = "name"; final List<Receipt> receiptsInTrip = Arrays.asList(mock(Receipt.class), mock(Receipt.class), mock(Receipt.class)); when(receiptsTable.get(trip)).thenReturn(Single.just(receiptsInTrip)); when(receipt.getName()).thenReturn(name); when(receipt.getFile()).thenReturn(new File("12345.jpg")); receiptTableActionAlterations.preInsert(receipt) .test() .assertValue(receipt) .assertComplete() .assertNoErrors(); assertEquals(new File("4_name.jpg"), receipt.getFile()); verify(receiptBuilderFactory).setIndex(4); verify(receiptBuilderFactory).setCustomOrderId(CUSTOM_ORDER_ID); }
@Test public void preInsertWithIllegalCharactersInName() { final String name = "before_|\\\\?*<\\:>+[]/'\n\r\t\0\f_after"; final List<Receipt> receiptsInTrip = Arrays.asList(mock(Receipt.class), mock(Receipt.class), mock(Receipt.class)); when(receiptsTable.get(trip)).thenReturn(Single.just(receiptsInTrip)); when(receipt.getName()).thenReturn(name); when(receipt.getFile()).thenReturn(new File("12345.jpg")); receiptTableActionAlterations.preInsert(receipt) .test() .assertValue(receipt) .assertComplete() .assertNoErrors(); assertEquals(new File("4_before__after.jpg"), receipt.getFile()); verify(receiptBuilderFactory).setIndex(4); verify(receiptBuilderFactory).setCustomOrderId(CUSTOM_ORDER_ID); }