DistanceDatabaseAdapter implements SelectionBackedDatabaseAdapter<Distance, Trip> { @NonNull @Override public ContentValues write(@NonNull Distance distance, @NonNull DatabaseOperationMetadata databaseOperationMetadata) { final ContentValues values = new ContentValues(); values.put(DistanceTable.COLUMN_PARENT_TRIP_ID, distance.getTrip().getId()); values.put(DistanceTable.COLUMN_LOCATION, distance.getLocation().trim()); values.put(DistanceTable.COLUMN_DISTANCE, distance.getDistance().doubleValue()); values.put(DistanceTable.COLUMN_TIMEZONE, distance.getTimeZone().getID()); values.put(DistanceTable.COLUMN_DATE, distance.getDate().getTime()); values.put(DistanceTable.COLUMN_RATE, distance.getRate().doubleValue()); values.put(DistanceTable.COLUMN_RATE_CURRENCY, distance.getPrice().getCurrencyCode()); values.put(DistanceTable.COLUMN_COMMENT, distance.getComment().trim()); values.put(DistanceTable.COLUMN_UUID, distance.getUuid().toString()); values.put(DistanceTable.COLUMN_PAYMENT_METHOD_ID, distance.getPaymentMethod().getId()); values.put(DistanceTable.COLUMN_LOCATION_HIDDEN_AUTO_COMPLETE, distance.getAutoCompleteMetadata().isLocationHiddenFromAutoComplete()); values.put(DistanceTable.COLUMN_COMMENT_HIDDEN_AUTO_COMPLETE, distance.getAutoCompleteMetadata().isCommentHiddenFromAutoComplete()); if (databaseOperationMetadata.getOperationFamilyType() == OperationFamilyType.Sync) { values.putAll(mSyncStateAdapter.write(distance.getSyncState())); } else { values.putAll(mSyncStateAdapter.writeUnsynced(distance.getSyncState())); } return values; } DistanceDatabaseAdapter(@NonNull Table<Trip> tripsTable, @NonNull Table<PaymentMethod> paymentMethodTable);  DistanceDatabaseAdapter(@NonNull Table<Trip> tripsTable, @NonNull Table<PaymentMethod> paymentMethodTable,
                                   @NonNull SyncStateAdapter syncStateAdapter); @NonNull @Override Distance read(@NonNull Cursor cursor); @NonNull @Override Distance readForSelection(@NonNull Cursor cursor, @NonNull Trip trip, boolean isDescending); @NonNull @Override ContentValues write(@NonNull Distance distance, @NonNull DatabaseOperationMetadata databaseOperationMetadata); @NonNull @Override Distance build(@NonNull Distance distance, int primaryKey, @NonNull UUID uuid,
                          @NonNull DatabaseOperationMetadata databaseOperationMetadata);  }
@Test public void writeUnsynced() throws Exception { final String sync = "sync"; final ContentValues syncValues = new ContentValues(); syncValues.put(sync, sync); when(mSyncStateAdapter.writeUnsynced(mSyncState)).thenReturn(syncValues); final ContentValues contentValues = mDistanceDatabaseAdapter.write(mDistance, new DatabaseOperationMetadata()); assertTrue(PARENT_ID == contentValues.getAsInteger(DistanceTable.COLUMN_PARENT_TRIP_ID)); assertEquals(DISTANCE, contentValues.getAsDouble(DistanceTable.COLUMN_DISTANCE), 0.0001d); assertEquals(LOCATION, contentValues.getAsString(DistanceTable.COLUMN_LOCATION)); assertEquals(DATE, (long) contentValues.getAsLong(DistanceTable.COLUMN_DATE)); assertEquals(TIMEZONE, contentValues.getAsString(DistanceTable.COLUMN_TIMEZONE)); assertEquals(COMMENT, contentValues.getAsString(DistanceTable.COLUMN_COMMENT)); assertEquals(RATE, contentValues.getAsDouble(DistanceTable.COLUMN_RATE), 0.0001d); assertEquals(CURRENCY_CODE, contentValues.getAsString(DistanceTable.COLUMN_RATE_CURRENCY)); assertEquals(DIST_UUID.toString(), contentValues.getAsString(DistanceTable.COLUMN_UUID)); assertEquals(sync, contentValues.getAsString(sync)); assertEquals(PAYMENT_METHOD_ID, (int) contentValues.getAsInteger(DistanceTable.COLUMN_PAYMENT_METHOD_ID)); assertEquals(LOCATION_HIDDEN_FROM_AUTO_COMPLETE, contentValues.getAsBoolean(DistanceTable.COLUMN_LOCATION_HIDDEN_AUTO_COMPLETE)); assertEquals(COMMENT_HIDDEN_FROM_AUTO_COMPLETE, contentValues.getAsBoolean(DistanceTable.COLUMN_COMMENT_HIDDEN_AUTO_COMPLETE)); assertFalse(contentValues.containsKey(DistanceTable.COLUMN_ID)); }
@Test public void write() throws Exception { final String sync = "sync"; final ContentValues syncValues = new ContentValues(); syncValues.put(sync, sync); when(mSyncStateAdapter.write(mSyncState)).thenReturn(syncValues); final ContentValues contentValues = mDistanceDatabaseAdapter.write(mDistance, new DatabaseOperationMetadata(OperationFamilyType.Sync)); assertTrue(PARENT_ID == contentValues.getAsInteger(DistanceTable.COLUMN_PARENT_TRIP_ID)); assertEquals(DISTANCE, contentValues.getAsDouble(DistanceTable.COLUMN_DISTANCE), 0.0001d); assertEquals(LOCATION, contentValues.getAsString(DistanceTable.COLUMN_LOCATION)); assertEquals(DATE, (long) contentValues.getAsLong(DistanceTable.COLUMN_DATE)); assertEquals(TIMEZONE, contentValues.getAsString(DistanceTable.COLUMN_TIMEZONE)); assertEquals(COMMENT, contentValues.getAsString(DistanceTable.COLUMN_COMMENT)); assertEquals(RATE, contentValues.getAsDouble(DistanceTable.COLUMN_RATE), 0.0001d); assertEquals(CURRENCY_CODE, contentValues.getAsString(DistanceTable.COLUMN_RATE_CURRENCY)); assertEquals(DIST_UUID.toString(), contentValues.getAsString(DistanceTable.COLUMN_UUID)); assertEquals(sync, contentValues.getAsString(sync)); assertEquals(PAYMENT_METHOD_ID, (int) contentValues.getAsInteger(DistanceTable.COLUMN_PAYMENT_METHOD_ID)); assertEquals(LOCATION_HIDDEN_FROM_AUTO_COMPLETE, contentValues.getAsBoolean(DistanceTable.COLUMN_LOCATION_HIDDEN_AUTO_COMPLETE)); assertEquals(COMMENT_HIDDEN_FROM_AUTO_COMPLETE, contentValues.getAsBoolean(DistanceTable.COLUMN_COMMENT_HIDDEN_AUTO_COMPLETE)); assertFalse(contentValues.containsKey(DistanceTable.COLUMN_ID)); }