Timeline { public void fetchItems() { int restoreOffset = restoreIfNeeded(); items.addAll( itemProvider.fetchItems( getOldest(), getFetchCount() + restoreOffset ) ); storeTopItem(); } Timeline( ItemProvider itemProvider, SessionStorage sessionStorage  ); void setFetchCount( int fetchCount ); int getFetchCount(); List<Item> getItems(); void fetchItems(); static final int FETCH_COUNT_LOWER_BOUND; static final int FETCH_COUNT_UPPER_BOUND; }
@Test public void fetchItems( FetchItemsEnum data ) { itemProvider.addItems( data.getInput() ); timeline.setFetchCount( data.getFetchCount() ); timeline.fetchItems(); timeline.fetchItems(); List<Item> actual = timeline.getItems(); assertArrayEquals( data.getOutput(), actual.toArray() ); }
@Test public void fetchItemWithExceptionOnStoreTop() throws IOException { IOException cause = new IOException(); doThrow( cause ).when( sessionStorage ).storeTop( any( Item.class ) ); Throwable actual = thrownBy( () -> timeline.fetchItems() ); assertNotNull( actual ); assertTrue( actual instanceof IllegalStateException ); assertSame( cause, actual.getCause() ); assertEquals( Timeline.ERROR_STORE_TOP, actual.getMessage() ); }
@Test public void fetchItemWithRuntimeExceptionOnStoreTop() throws IOException { RuntimeException cause = new RuntimeException(); doThrow( cause ).when( sessionStorage ).storeTop( any( Item.class ) ); Throwable actual = thrownBy( () -> timeline.fetchItems() ); assertNotNull( actual ); assertSame( cause, actual ); }
@Test public void fetchItemWithExceptionOnReadTop() throws IOException { IOException cause = new IOException(); doThrow( cause ).when( sessionStorage ).readTop(); Throwable actual = thrownBy( () -> timeline.fetchItems() ); assertNotNull( actual ); assertTrue( actual instanceof IllegalStateException ); assertSame( cause, actual.getCause() ); assertEquals( Timeline.ERROR_READ_TOP, actual.getMessage() ); }
@Test public void fetchItemWithRuntimeExceptionOnReadTop() throws IOException { RuntimeException cause = new RuntimeException(); doThrow( cause ).when( sessionStorage ).readTop(); Throwable actual = thrownBy( () -> timeline.fetchItems() ); assertNotNull( actual ); assertSame( cause, actual ); }
@Test public void fetchItems() { itemProvider.addItems( FIRST_ITEM, SECOND_ITEM, THIRD_ITEM, FOURTH_ITEM, FIFTH_ITEM, SIXTH_ITEM ); timeline.setFetchCount( 2 ); timeline.fetchItems(); timeline.fetchItems(); List<Item> actual = timeline.getItems(); assertArrayEquals( new Item[] { SIXTH_ITEM, FIFTH_ITEM, FOURTH_ITEM, THIRD_ITEM }, actual.toArray( new Item[ 2 ] ) ); }