### Question:
RepositorySearchTaskV5_6Plus extends RepositorySearchTask { @NotNull @Override public List<Resource> nextLookup() throws ServiceException { if (mEndReached || mInitialCriteria.getLimit() == 0){ return EMPTY_RESPONSE; } if (mInternalOffset == UNDEFINED) { defineInternalOffset(); } return Collections.unmodifiableList(performLookup()); } RepositorySearchTaskV5_6Plus(InternalCriteria criteria, SearchUseCase searchUseCase); @NotNull @Override List<Resource> nextLookup(); @Override boolean hasNext(); static final List<Resource> EMPTY_RESPONSE; }### Answer:
@Test public void shouldMakeImmediateCallOnApiForUserOffsetZero() throws Exception { InternalCriteria searchCriteria = new InternalCriteria.Builder().offset(0).create(); RepositorySearchTask strategy = new RepositorySearchTaskV5_6Plus(searchCriteria, mSearchUseCase); strategy.nextLookup(); InternalCriteria internalCriteria = new InternalCriteria.Builder() .offset(0) .forceFullPage(true) .create(); verify(mSearchUseCase).performSearch(internalCriteria); }
@Test public void makesAdditionalCallOnApiIfUserOffsetNotZero() throws Exception { InternalCriteria searchCriteria = new InternalCriteria.Builder().offset(5).create(); RepositorySearchTask strategy = new RepositorySearchTaskV5_6Plus(searchCriteria, mSearchUseCase); strategy.nextLookup(); InternalCriteria internalCriteria = new InternalCriteria.Builder() .limit(5) .forceFullPage(true) .create(); verify(mSearchUseCase).performSearch(internalCriteria); }
@Test public void secondSearchLookupShouldUseNextOffset() throws Exception { InternalCriteria searchCriteria = new InternalCriteria.Builder().offset(0).create(); RepositorySearchTask strategy = new RepositorySearchTaskV5_6Plus(searchCriteria, mSearchUseCase); when(mResponse.getNextOffset()).thenReturn(133); strategy.nextLookup(); when(mResponse.getNextOffset()).thenReturn(233); strategy.nextLookup(); InternalCriteria internalCriteria = new InternalCriteria.Builder() .offset(133) .limit(100) .forceFullPage(true) .create(); verify(mResponse, times(2)).getNextOffset(); verify(mSearchUseCase).performSearch(internalCriteria); }
@Test public void shouldReturnEmptyCollectionForZeroLimit() throws Exception { InternalCriteria userCriteria = new InternalCriteria.Builder().limit(0).offset(5).create(); RepositorySearchTask strategy = new RepositorySearchTaskV5_6Plus(userCriteria, mSearchUseCase); Collection<Resource> result = strategy.nextLookup(); assertThat(result, Matchers.is(Matchers.empty())); verifyZeroInteractions(mSearchUseCase); }