TripTableActionAlterations extends StubTableActionAlterations<Trip> { @NonNull @Override public Single<Trip> postInsert(@NonNull final Trip postInsertTrip) { if (postInsertTrip == null) { return Single.error(new Exception("Post insert failed due to a null trip")); } return makeTripDirectory(postInsertTrip) .doOnError(throwable -> { mTripsTable.delete(postInsertTrip, new DatabaseOperationMetadata(OperationFamilyType.Rollback)) .subscribe(); }) .andThen(Single.just(postInsertTrip)) .doOnSuccess(trip -> backUpDatabase()); } 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 postInsertForNullTrip() throws Exception { mTripTableActionAlterations.postInsert(null) .test() .assertNoValues() .assertNotComplete() .assertError(Exception.class); verify(mTripsTable, never()).delete(mTrip1, new DatabaseOperationMetadata()); }
@Test public void postInsertForValidTrip() throws Exception { final String name = "name"; when(mTrip1.getName()).thenReturn(name); when(mStorageManager.mkdir(name)).thenReturn(new File(name)); mTripTableActionAlterations.postInsert(mTrip1) .test() .assertValue(mTrip1) .assertComplete() .assertNoErrors(); verify(mTripsTable, never()).delete(mTrip1, new DatabaseOperationMetadata()); }
@Test public void postInsertForValidTripButIOFails() throws Exception { final DatabaseOperationMetadata metadata = new DatabaseOperationMetadata(OperationFamilyType.Rollback); final String name = "name"; when(mTrip1.getName()).thenReturn(name); when(mStorageManager.mkdir(name)).thenReturn(null); when(mTripsTable.delete(mTrip1, metadata)).thenReturn(Single.just(mTrip1)); mTripTableActionAlterations.postInsert(mTrip1) .test() .assertNoValues() .assertNotComplete() .assertError(IOException.class); verify(mTripsTable).delete(mTrip1, metadata); }