BookBorrowingContentProviderMapper { public List<BookBorrowing> transform(Cursor cursor) { List<BookBorrowing> bookBorrowings = new ArrayList<>(); if(cursor != null && cursor.moveToFirst()) { do { long bookBorrowingId = cursor.getLong(cursor.getColumnIndex(LendingEntry.COLUMN_LENDING_ID)); String bookBorrowerContactUri = cursor.getString(cursor.getColumnIndex(LendingEntry.COLUMN_CONTACT_URI)); Date lendingDate = VilibraContract.getDateFromDb( cursor.getString(cursor.getColumnIndex(LendingEntry.COLUMN_LENDING_DATE))); Book borrowedBook = this.bookContentProviderMapper.transform(cursor); BookBorrower bookBorrower = new BookBorrower(bookBorrowerContactUri); BookBorrowing bookBorrowing = new BookBorrowing(bookBorrowingId, borrowedBook, bookBorrower, lendingDate); bookBorrowings.add(bookBorrowing); } while (cursor.moveToNext()); } return bookBorrowings; } BookBorrowingContentProviderMapper(); List<BookBorrowing> transform(Cursor cursor); ContentValues transform(BookBorrowing bookBorrowing);  }
@Test public void testTransformEmptyCursorToBookBorrowing() throws Exception { Cursor cursor = mock(Cursor.class); when(cursor.moveToFirst()).thenReturn(false); List<BookBorrowing> bookBorrowings = this.bookBorrowingContentProviderMapper.transform(cursor); assertThat(bookBorrowings, is(empty())); }
@Test public void testTransformNullCursorToBookBorrowing() throws Exception { Cursor cursor = null; List<BookBorrowing> bookBorrowings = this.bookBorrowingContentProviderMapper.transform(cursor); assertThat(bookBorrowings, is(empty())); }
@Test public void testTransformCursorToBookBorrowing() throws Exception { Cursor cursor = prepareMockCursor(); List<BookBorrowing> bookBorrowings = this.bookBorrowingContentProviderMapper.transform(cursor); assertThat(bookBorrowings.size(), is(1)); BookBorrowing transformedBookBorrowing = bookBorrowings.get(0); assertThat(transformedBookBorrowing, equalTo(this.testBookBorrowing)); }
@Test public void testTransformBookBorrowingToContentValues() throws Exception { ContentValues generatedContentValues = this.bookBorrowingContentProviderMapper.transform(this.testBookBorrowing); long bookId = generatedContentValues.getAsLong(LendingEntry.COLUMN_BOOK_ID); String borrowerContactUri = generatedContentValues.getAsString(LendingEntry.COLUMN_CONTACT_URI); Date borrowingDate = getDateFromDb(generatedContentValues.getAsString(LendingEntry.COLUMN_LENDING_DATE)); assertThat(bookId, equalTo(this.testBookBorrowing.getBorrowedBook().getId())); assertThat(borrowerContactUri, equalTo(this.testBookBorrowing.getBorrower().getContactInformationUri())); assertThat(borrowingDate, equalTo(this.testBookBorrowing.getLendingDate())); }