GetPeople extends RosieUseCase { public PaginatedCollection<Person> getAllPeopleInCache() { Collection<Person> allPeople; try { allPeople = peopleRepository.getAll(ReadPolicy.CACHE_ONLY); } catch (Exception e) { allPeople = new ArrayList<>(); } if (allPeople == null) { allPeople = new ArrayList<>(); } Page page = Page.withOffsetAndLimit(0, allPeople.size()); PaginatedCollection<Person> allPeoplePaginated = new PaginatedCollection<>(allPeople); allPeoplePaginated.setPage(page); allPeoplePaginated.setHasMore(true); return allPeoplePaginated; } @Inject  GetPeople(final PeopleRepository peopleRepository); PaginatedCollection<Person> getAllPeopleInCache(); @UseCase void getPeople(Page page);  }
@Test public void testGetAllPeopleInTheCache_cacheIsEmpty() throws Exception { Collection<Person> emptyCacheList = new ArrayList<>(); PaginatedCollection<Person> expectedPeopleList = PaginatedCollectionUtils.createPaginatedCollection( emptyCacheList); when(peopleRepository.getAll(ReadPolicy.CACHE_ONLY)).thenReturn(emptyCacheList); PaginatedCollection<Person> actualPeopleList = getPeople.getAllPeopleInCache(); assertThatPaginatedListAreEqual(actualPeopleList, expectedPeopleList); }
@Test public void testGetAllPeopleInTheCache_cacheIsNotEmpty() throws Exception { Collection<Person> nonEmptyCacheList = new ArrayList<>(); nonEmptyCacheList.add(new Person(PERSON_NAME, PERSON_GENDER, PERSON_BIRTH_YEAR, PERSON_URL)); PaginatedCollection<Person> expectedPeopleList = PaginatedCollectionUtils.createPaginatedCollection( nonEmptyCacheList); when(peopleRepository.getAll(ReadPolicy.CACHE_ONLY)).thenReturn(nonEmptyCacheList); PaginatedCollection<Person> actualPeopleList = getPeople.getAllPeopleInCache(); assertThatPaginatedListAreEqual(actualPeopleList, expectedPeopleList); }