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);  }
@Test public void postUpdateForNullTrip() throws Exception { mTripTableActionAlterations.postUpdate(mTrip1, null) .test() .assertNoValues() .assertNotComplete() .assertError(Exception.class); }
@Test public void postUpdateForTripWithSameName() throws Exception { final String name = "name"; when(mTrip1.getName()).thenReturn(name); when(mTrip2.getName()).thenReturn(name); when(mTrip1.getDirectory()).thenReturn(new File(name)); when(mTrip2.getDirectory()).thenReturn(new File(name)); mTripTableActionAlterations.postUpdate(mTrip1, mTrip2) .test() .assertValue(mTrip2) .assertComplete() .assertNoErrors(); verify(mTrip2).setPrice(mPrice1); verify(mTrip2).setDailySubTotal(mPrice2); verifyZeroInteractions(mStorageManager, mReceiptsTable, mDistanceTable); }
@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); }
@Test public void postUpdateForValidTripButIOFails() 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(name1)); when(mTripsTable.update(mTrip2, mTrip1, new DatabaseOperationMetadata())).thenReturn(Single.just(mTrip1)); mTripTableActionAlterations.postUpdate(mTrip1, mTrip2) .test() .assertNoValues() .assertNotComplete() .assertError(IOException.class); verify(mTrip2).setPrice(mPrice1); verify(mTrip2).setDailySubTotal(mPrice2); verify(mReceiptsTable).updateParentBlocking(mTrip1, mTrip2); verify(mDistanceTable).updateParentBlocking(mTrip1, mTrip2); verify(mReceiptsTable).updateParentBlocking(mTrip2, mTrip1); verify(mDistanceTable).updateParentBlocking(mTrip2, mTrip1); verify(mTripsTable).update(mTrip2, mTrip1, new DatabaseOperationMetadata()); }