@Test public void postUpdateForValidTripWithNewName() throws Exception { final String name1 = "name1"; final String name2 = "name2"; when(mTrip1.getName()).thenReturn(name1); when(mTrip2.getName()).thenReturn(name2); when(mTrip1.getDirectory()).thenReturn(new File(name1)); when(mStorageManager.rename(new File(name1), name2)).thenReturn(new File(name2)); mTripTableActionAlterations.postUpdate(mTrip1, mTrip2) .test() .assertValue(mTrip2) .assertComplete() .assertNoErrors(); verify(mTrip2).setPrice(mPrice1); verify(mTrip2).setDailySubTotal(mPrice2); verify(mReceiptsTable).updateParentBlocking(mTrip1, mTrip2); verify(mDistanceTable).updateParentBlocking(mTrip1, mTrip2); verify(mTripsTable, never()).update(mTrip2, mTrip1, new DatabaseOperationMetadata()); verify(mReceiptsTable, never()).updateParentBlocking(mTrip2, mTrip1); verify(mDistanceTable, never()).updateParentBlocking(mTrip2, mTrip1); }
@NonNull @Override public Single<Trip> postUpdate(@NonNull final Trip oldTrip, @Nullable final Trip newTrip) { return Single.fromCallable(() -> { if (newTrip == null) { throw new Exception("Post update failed due to a null trip"); } newTrip.setPrice(oldTrip.getPrice()); newTrip.setDailySubTotal(oldTrip.getDailySubTotal()); return newTrip; }).doOnSuccess(trip -> { if (!oldTrip.getDirectory().equals(trip.getDirectory())) { mReceiptsTable.updateParentBlocking(oldTrip, trip); mDistanceTable.updateParentBlocking(oldTrip, trip); final File dir = mStorageManager.rename(oldTrip.getDirectory(), trip.getName()); if (dir.equals(oldTrip.getDirectory())) { Logger.error(this, "Failed to re-name the trip directory... Rolling back and throwing an exception"); mTripsTable.update(trip, oldTrip, new DatabaseOperationMetadata()).blockingGet(); mReceiptsTable.updateParentBlocking(trip, oldTrip); mDistanceTable.updateParentBlocking(trip, oldTrip); throw new IOException("Failed to create trip directory"); } } }); }
TripTableActionAlterations extends StubTableActionAlterations<Trip> { @NonNull @Override public Single<Trip> postUpdate(@NonNull final Trip oldTrip, @Nullable final Trip newTrip) { return Single.fromCallable(() -> { if (newTrip == null) { throw new Exception("Post update failed due to a null trip"); } newTrip.setPrice(oldTrip.getPrice()); newTrip.setDailySubTotal(oldTrip.getDailySubTotal()); return newTrip; }).doOnSuccess(trip -> { if (!oldTrip.getDirectory().equals(trip.getDirectory())) { mReceiptsTable.updateParentBlocking(oldTrip, trip); mDistanceTable.updateParentBlocking(oldTrip, trip); final File dir = mStorageManager.rename(oldTrip.getDirectory(), trip.getName()); if (dir.equals(oldTrip.getDirectory())) { Logger.error(this, "Failed to re-name the trip directory... Rolling back and throwing an exception"); mTripsTable.update(trip, oldTrip, new DatabaseOperationMetadata()).blockingGet(); mReceiptsTable.updateParentBlocking(trip, oldTrip); mDistanceTable.updateParentBlocking(trip, oldTrip); throw new IOException("Failed to create trip directory"); } } }); } }
TripTableActionAlterations extends StubTableActionAlterations<Trip> { @NonNull @Override public Single<Trip> postUpdate(@NonNull final Trip oldTrip, @Nullable final Trip newTrip) { return Single.fromCallable(() -> { if (newTrip == null) { throw new Exception("Post update failed due to a null trip"); } newTrip.setPrice(oldTrip.getPrice()); newTrip.setDailySubTotal(oldTrip.getDailySubTotal()); return newTrip; }).doOnSuccess(trip -> { if (!oldTrip.getDirectory().equals(trip.getDirectory())) { mReceiptsTable.updateParentBlocking(oldTrip, trip); mDistanceTable.updateParentBlocking(oldTrip, trip); final File dir = mStorageManager.rename(oldTrip.getDirectory(), trip.getName()); if (dir.equals(oldTrip.getDirectory())) { Logger.error(this, "Failed to re-name the trip directory... Rolling back and throwing an exception"); mTripsTable.update(trip, oldTrip, new DatabaseOperationMetadata()).blockingGet(); mReceiptsTable.updateParentBlocking(trip, oldTrip); mDistanceTable.updateParentBlocking(trip, oldTrip); throw new IOException("Failed to create trip directory"); } } }); } TripTableActionAlterations(@NonNull PersistenceManager persistenceManager);  TripTableActionAlterations(@NonNull DatabaseHelper databaseHelper, @NonNull StorageManager storageManager);  TripTableActionAlterations(@NonNull Table<Trip> tripsTable, @NonNull ReceiptsTable receiptsTable, @NonNull DistanceTable distanceTable,
                                      @NonNull DatabaseHelper databaseHelper, @NonNull StorageManager storageManager); }
TripTableActionAlterations extends StubTableActionAlterations<Trip> { @NonNull @Override public Single<Trip> postUpdate(@NonNull final Trip oldTrip, @Nullable final Trip newTrip) { return Single.fromCallable(() -> { if (newTrip == null) { throw new Exception("Post update failed due to a null trip"); } newTrip.setPrice(oldTrip.getPrice()); newTrip.setDailySubTotal(oldTrip.getDailySubTotal()); return newTrip; }).doOnSuccess(trip -> { if (!oldTrip.getDirectory().equals(trip.getDirectory())) { mReceiptsTable.updateParentBlocking(oldTrip, trip); mDistanceTable.updateParentBlocking(oldTrip, trip); final File dir = mStorageManager.rename(oldTrip.getDirectory(), trip.getName()); if (dir.equals(oldTrip.getDirectory())) { Logger.error(this, "Failed to re-name the trip directory... Rolling back and throwing an exception"); mTripsTable.update(trip, oldTrip, new DatabaseOperationMetadata()).blockingGet(); mReceiptsTable.updateParentBlocking(trip, oldTrip); mDistanceTable.updateParentBlocking(trip, oldTrip); throw new IOException("Failed to create trip directory"); } } }); } TripTableActionAlterations(@NonNull PersistenceManager persistenceManager);  TripTableActionAlterations(@NonNull DatabaseHelper databaseHelper, @NonNull StorageManager storageManager);  TripTableActionAlterations(@NonNull Table<Trip> tripsTable, @NonNull ReceiptsTable receiptsTable, @NonNull DistanceTable distanceTable,
                                      @NonNull DatabaseHelper databaseHelper, @NonNull StorageManager storageManager); @NonNull @Override Single<List<Trip>> postGet(@NonNull final List<Trip> trips); @NonNull @Override Single<Trip> postInsert(@NonNull final Trip postInsertTrip); @NonNull @Override Single<Trip> postUpdate(@NonNull final Trip oldTrip, @Nullable final Trip newTrip); @NonNull @Override Single<Trip> postDelete(@Nullable final Trip trip); }
TripTableActionAlterations extends StubTableActionAlterations<Trip> { @NonNull @Override public Single<Trip> postUpdate(@NonNull final Trip oldTrip, @Nullable final Trip newTrip) { return Single.fromCallable(() -> { if (newTrip == null) { throw new Exception("Post update failed due to a null trip"); } newTrip.setPrice(oldTrip.getPrice()); newTrip.setDailySubTotal(oldTrip.getDailySubTotal()); return newTrip; }).doOnSuccess(trip -> { if (!oldTrip.getDirectory().equals(trip.getDirectory())) { mReceiptsTable.updateParentBlocking(oldTrip, trip); mDistanceTable.updateParentBlocking(oldTrip, trip); final File dir = mStorageManager.rename(oldTrip.getDirectory(), trip.getName()); if (dir.equals(oldTrip.getDirectory())) { Logger.error(this, "Failed to re-name the trip directory... Rolling back and throwing an exception"); mTripsTable.update(trip, oldTrip, new DatabaseOperationMetadata()).blockingGet(); mReceiptsTable.updateParentBlocking(trip, oldTrip); mDistanceTable.updateParentBlocking(trip, oldTrip); throw new IOException("Failed to create trip directory"); } } }); } TripTableActionAlterations(@NonNull PersistenceManager persistenceManager);  TripTableActionAlterations(@NonNull DatabaseHelper databaseHelper, @NonNull StorageManager storageManager);  TripTableActionAlterations(@NonNull Table<Trip> tripsTable, @NonNull ReceiptsTable receiptsTable, @NonNull DistanceTable distanceTable,
                                      @NonNull DatabaseHelper databaseHelper, @NonNull StorageManager storageManager); @NonNull @Override Single<List<Trip>> postGet(@NonNull final List<Trip> trips); @NonNull @Override Single<Trip> postInsert(@NonNull final Trip postInsertTrip); @NonNull @Override Single<Trip> postUpdate(@NonNull final Trip oldTrip, @Nullable final Trip newTrip); @NonNull @Override Single<Trip> postDelete(@Nullable final Trip trip);  }