@Test public void preUpdateWithBrandNewFile() { 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)); final Receipt oldReceipt = mock(Receipt.class); when(oldReceipt.getFile()).thenReturn(null); when(oldReceipt.getDate()).thenReturn(DATE); when(oldReceipt.getTimeZone()).thenReturn(TIMEZONE); when(receipt.getIndex()).thenReturn(4); when(receipt.getName()).thenReturn(name); when(receipt.getFile()).thenReturn(new File("12345.jpg")); final List<Receipt> result = receiptTableActionAlterations.preUpdate(oldReceipt, receipt) .test() .assertComplete() .assertNoErrors() .values(); assertEquals(1, result.size()); final Receipt receipt = result.get(0); assertEquals(new File("4_name.jpg"), receipt.getFile()); }
@NonNull @Override public Single<Receipt> preUpdate(@NonNull final Receipt oldReceipt, @NonNull final Receipt newReceipt) { return receiptsTable.get(newReceipt.getTrip()) .map(existingReceipts -> { final ReceiptBuilderFactory factory = receiptBuilderFactoryFactory.build(newReceipt); if (!oldReceipt.getDate().equals(newReceipt.getDate()) || !oldReceipt.getTimeZone().equals(newReceipt.getTimeZone())) { final long customOrderId = ReceiptsOrderer.getCustomOrderId(newReceipt, existingReceipts); factory.setCustomOrderId(customOrderId); } if (newReceipt.getFile() != null) { if (!newReceipt.getFile().equals(oldReceipt.getFile())) { if (oldReceipt.getFile() != null) { final String oldExtension = "." + UriUtils.getExtension(oldReceipt.getFile(), context); final String newExtension = "." + UriUtils.getExtension(newReceipt.getFile(), context); if (newExtension.equals(oldExtension)) { picasso.get().invalidate(oldReceipt.getFile()); if (newReceipt.getFile().renameTo(oldReceipt.getFile())) { factory.setFile(oldReceipt.getFile()); } } else { final String renamedNewFileName = oldReceipt.getFile().getName().replace(oldExtension, newExtension); final String renamedNewFilePath = newReceipt.getFile().getAbsolutePath().replace(newReceipt.getFile().getName(), renamedNewFileName); final File renamedNewFile = new File(renamedNewFilePath); if (newReceipt.getFile().renameTo(renamedNewFile)) { factory.setFile(renamedNewFile); } } return factory.build(); } else { return updateReceiptFileNameBlocking(factory.build()); } } else if (newReceipt.getIndex() != oldReceipt.getIndex()) { return updateReceiptFileNameBlocking(factory.build()); } else { return factory.build(); } } else { return factory.build(); } }); }
ReceiptTableActionAlterations extends StubTableActionAlterations<Receipt> { @NonNull @Override public Single<Receipt> preUpdate(@NonNull final Receipt oldReceipt, @NonNull final Receipt newReceipt) { return receiptsTable.get(newReceipt.getTrip()) .map(existingReceipts -> { final ReceiptBuilderFactory factory = receiptBuilderFactoryFactory.build(newReceipt); if (!oldReceipt.getDate().equals(newReceipt.getDate()) || !oldReceipt.getTimeZone().equals(newReceipt.getTimeZone())) { final long customOrderId = ReceiptsOrderer.getCustomOrderId(newReceipt, existingReceipts); factory.setCustomOrderId(customOrderId); } if (newReceipt.getFile() != null) { if (!newReceipt.getFile().equals(oldReceipt.getFile())) { if (oldReceipt.getFile() != null) { final String oldExtension = "." + UriUtils.getExtension(oldReceipt.getFile(), context); final String newExtension = "." + UriUtils.getExtension(newReceipt.getFile(), context); if (newExtension.equals(oldExtension)) { picasso.get().invalidate(oldReceipt.getFile()); if (newReceipt.getFile().renameTo(oldReceipt.getFile())) { factory.setFile(oldReceipt.getFile()); } } else { final String renamedNewFileName = oldReceipt.getFile().getName().replace(oldExtension, newExtension); final String renamedNewFilePath = newReceipt.getFile().getAbsolutePath().replace(newReceipt.getFile().getName(), renamedNewFileName); final File renamedNewFile = new File(renamedNewFilePath); if (newReceipt.getFile().renameTo(renamedNewFile)) { factory.setFile(renamedNewFile); } } return factory.build(); } else { return updateReceiptFileNameBlocking(factory.build()); } } else if (newReceipt.getIndex() != oldReceipt.getIndex()) { return updateReceiptFileNameBlocking(factory.build()); } else { return factory.build(); } } else { return factory.build(); } }); } }
ReceiptTableActionAlterations extends StubTableActionAlterations<Receipt> { @NonNull @Override public Single<Receipt> preUpdate(@NonNull final Receipt oldReceipt, @NonNull final Receipt newReceipt) { return receiptsTable.get(newReceipt.getTrip()) .map(existingReceipts -> { final ReceiptBuilderFactory factory = receiptBuilderFactoryFactory.build(newReceipt); if (!oldReceipt.getDate().equals(newReceipt.getDate()) || !oldReceipt.getTimeZone().equals(newReceipt.getTimeZone())) { final long customOrderId = ReceiptsOrderer.getCustomOrderId(newReceipt, existingReceipts); factory.setCustomOrderId(customOrderId); } if (newReceipt.getFile() != null) { if (!newReceipt.getFile().equals(oldReceipt.getFile())) { if (oldReceipt.getFile() != null) { final String oldExtension = "." + UriUtils.getExtension(oldReceipt.getFile(), context); final String newExtension = "." + UriUtils.getExtension(newReceipt.getFile(), context); if (newExtension.equals(oldExtension)) { picasso.get().invalidate(oldReceipt.getFile()); if (newReceipt.getFile().renameTo(oldReceipt.getFile())) { factory.setFile(oldReceipt.getFile()); } } else { final String renamedNewFileName = oldReceipt.getFile().getName().replace(oldExtension, newExtension); final String renamedNewFilePath = newReceipt.getFile().getAbsolutePath().replace(newReceipt.getFile().getName(), renamedNewFileName); final File renamedNewFile = new File(renamedNewFilePath); if (newReceipt.getFile().renameTo(renamedNewFile)) { factory.setFile(renamedNewFile); } } return factory.build(); } else { return updateReceiptFileNameBlocking(factory.build()); } } else if (newReceipt.getIndex() != oldReceipt.getIndex()) { return updateReceiptFileNameBlocking(factory.build()); } else { return factory.build(); } } else { return factory.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); }
ReceiptTableActionAlterations extends StubTableActionAlterations<Receipt> { @NonNull @Override public Single<Receipt> preUpdate(@NonNull final Receipt oldReceipt, @NonNull final Receipt newReceipt) { return receiptsTable.get(newReceipt.getTrip()) .map(existingReceipts -> { final ReceiptBuilderFactory factory = receiptBuilderFactoryFactory.build(newReceipt); if (!oldReceipt.getDate().equals(newReceipt.getDate()) || !oldReceipt.getTimeZone().equals(newReceipt.getTimeZone())) { final long customOrderId = ReceiptsOrderer.getCustomOrderId(newReceipt, existingReceipts); factory.setCustomOrderId(customOrderId); } if (newReceipt.getFile() != null) { if (!newReceipt.getFile().equals(oldReceipt.getFile())) { if (oldReceipt.getFile() != null) { final String oldExtension = "." + UriUtils.getExtension(oldReceipt.getFile(), context); final String newExtension = "." + UriUtils.getExtension(newReceipt.getFile(), context); if (newExtension.equals(oldExtension)) { picasso.get().invalidate(oldReceipt.getFile()); if (newReceipt.getFile().renameTo(oldReceipt.getFile())) { factory.setFile(oldReceipt.getFile()); } } else { final String renamedNewFileName = oldReceipt.getFile().getName().replace(oldExtension, newExtension); final String renamedNewFilePath = newReceipt.getFile().getAbsolutePath().replace(newReceipt.getFile().getName(), renamedNewFileName); final File renamedNewFile = new File(renamedNewFilePath); if (newReceipt.getFile().renameTo(renamedNewFile)) { factory.setFile(renamedNewFile); } } return factory.build(); } else { return updateReceiptFileNameBlocking(factory.build()); } } else if (newReceipt.getIndex() != oldReceipt.getIndex()) { return updateReceiptFileNameBlocking(factory.build()); } else { return factory.build(); } } else { return factory.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); }
ReceiptTableActionAlterations extends StubTableActionAlterations<Receipt> { @NonNull @Override public Single<Receipt> preUpdate(@NonNull final Receipt oldReceipt, @NonNull final Receipt newReceipt) { return receiptsTable.get(newReceipt.getTrip()) .map(existingReceipts -> { final ReceiptBuilderFactory factory = receiptBuilderFactoryFactory.build(newReceipt); if (!oldReceipt.getDate().equals(newReceipt.getDate()) || !oldReceipt.getTimeZone().equals(newReceipt.getTimeZone())) { final long customOrderId = ReceiptsOrderer.getCustomOrderId(newReceipt, existingReceipts); factory.setCustomOrderId(customOrderId); } if (newReceipt.getFile() != null) { if (!newReceipt.getFile().equals(oldReceipt.getFile())) { if (oldReceipt.getFile() != null) { final String oldExtension = "." + UriUtils.getExtension(oldReceipt.getFile(), context); final String newExtension = "." + UriUtils.getExtension(newReceipt.getFile(), context); if (newExtension.equals(oldExtension)) { picasso.get().invalidate(oldReceipt.getFile()); if (newReceipt.getFile().renameTo(oldReceipt.getFile())) { factory.setFile(oldReceipt.getFile()); } } else { final String renamedNewFileName = oldReceipt.getFile().getName().replace(oldExtension, newExtension); final String renamedNewFilePath = newReceipt.getFile().getAbsolutePath().replace(newReceipt.getFile().getName(), renamedNewFileName); final File renamedNewFile = new File(renamedNewFilePath); if (newReceipt.getFile().renameTo(renamedNewFile)) { factory.setFile(renamedNewFile); } } return factory.build(); } else { return updateReceiptFileNameBlocking(factory.build()); } } else if (newReceipt.getIndex() != oldReceipt.getIndex()) { return updateReceiptFileNameBlocking(factory.build()); } else { return factory.build(); } } else { return factory.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);  }