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);  }
@Test public void preUpdateWithoutFile() { 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.getDate()).thenReturn(DATE); when(oldReceipt.getTimeZone()).thenReturn(TIMEZONE); when(receipt.getFile()).thenReturn(null); receiptTableActionAlterations.preUpdate(oldReceipt, receipt) .test() .assertValue(receipt) .assertComplete() .assertNoErrors(); }
@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()); }
@Test public void preUpdateWithUpdatedFile() throws Exception { this.file1 = new File(System.getProperty("java.io.tmpdir"), "1_name.jpg"); this.file2 = new File(System.getProperty("java.io.tmpdir"), "12345.jpg"); assertTrue(this.file1.createNewFile()); assertTrue(this.file2.createNewFile()); 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.getDate()).thenReturn(DATE); when(oldReceipt.getTimeZone()).thenReturn(TIMEZONE); when(oldReceipt.getIndex()).thenReturn(1); when(oldReceipt.getName()).thenReturn(name); when(oldReceipt.getFile()).thenReturn(file1); when(receipt.getIndex()).thenReturn(1); when(receipt.getName()).thenReturn(name); when(receipt.getFile()).thenReturn(file2); final List<Receipt> onNextResults = receiptTableActionAlterations.preUpdate(oldReceipt, receipt) .test() .assertComplete() .assertNoErrors() .values(); assertNotNull(onNextResults); assertEquals(1, onNextResults.size()); final Receipt result = onNextResults.get(0); assertNotNull(result.getFile()); assertEquals("1_name.jpg", result.getFile().getName()); verify(picasso).invalidate(file1); }
@Test public void preUpdateWithUpdatedFileAndFileType() throws Exception { this.file1 = new File("1_name.jpg"); this.file2 = new File("12345.pdf"); assertTrue(this.file1.createNewFile()); assertTrue(this.file2.createNewFile()); 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.getIndex()).thenReturn(1); when(oldReceipt.getName()).thenReturn(name); when(oldReceipt.getFile()).thenReturn(file1); when(oldReceipt.getDate()).thenReturn(DATE); when(oldReceipt.getTimeZone()).thenReturn(TIMEZONE); when(receipt.getIndex()).thenReturn(1); when(receipt.getName()).thenReturn(name); when(receipt.getFile()).thenReturn(file2); final List<Receipt> onNextResults = receiptTableActionAlterations.preUpdate(oldReceipt, receipt) .test() .assertComplete() .assertNoErrors() .values(); assertNotNull(onNextResults); assertTrue(onNextResults.size() == 1); final Receipt result = onNextResults.get(0); assertNotNull(result.getFile()); assertEquals("1_name.pdf", result.getFile().getName()); assertTrue(result.getFile().delete()); }
@Test public void preUpdateWithNewIndex() { 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.getIndex()).thenReturn(1); when(oldReceipt.getName()).thenReturn(name); when(oldReceipt.getFile()).thenReturn(new File("1_name.jpg")); 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("1_name.jpg")); final List<Receipt> onNextResults = receiptTableActionAlterations.preUpdate(oldReceipt, receipt) .test() .assertComplete() .assertNoErrors() .values(); assertNotNull(onNextResults); assertTrue(onNextResults.size() == 1); final Receipt result = onNextResults.get(0); assertEquals(new File("4_name.jpg"), result.getFile()); }