UserRepositoryImpl implements UserRepository { @Override public Observable<List<User>> searchUsers(final String searchTerm) { return Observable.defer(() -> githubUserRestService.searchGithubUsers(searchTerm).concatMap( usersList -> Observable.from(usersList.getItems()) .concatMap(user -> githubUserRestService.getUser(user.getLogin())).toList())) .retryWhen(observable -> observable.flatMap(o -> { if (o instanceof IOException) { return Observable.just(null); } return Observable.error(o); })); } UserRepositoryImpl(GithubUserRestService githubUserRestService); @Override Observable<List<User>> searchUsers(final String searchTerm);  }
@Test public void searchUsers_200OkResponse_InvokesCorrectApiCalls() { when(githubUserRestService.searchGithubUsers(anyString())).thenReturn(Observable.just(githubUserList())); when(githubUserRestService.getUser(anyString())) .thenReturn(Observable.just(user1FullDetails()), Observable.just(user2FullDetails())); TestSubscriber<List<User>> subscriber = new TestSubscriber<>(); userRepository.searchUsers(USER_LOGIN_RIGGAROO).subscribe(subscriber); subscriber.awaitTerminalEvent(); subscriber.assertNoErrors(); List<List<User>> onNextEvents = subscriber.getOnNextEvents(); List<User> users = onNextEvents.get(0); Assert.assertEquals(USER_LOGIN_RIGGAROO, users.get(0).getLogin()); Assert.assertEquals(USER_LOGIN_2_REBECCA, users.get(1).getLogin()); verify(githubUserRestService).searchGithubUsers(USER_LOGIN_RIGGAROO); verify(githubUserRestService).getUser(USER_LOGIN_RIGGAROO); verify(githubUserRestService).getUser(USER_LOGIN_2_REBECCA); }
@Test public void searchUsers_IOExceptionThenSuccess_SearchUsersRetried() { when(githubUserRestService.searchGithubUsers(anyString())) .thenReturn(getIOExceptionError(), Observable.just(githubUserList())); when(githubUserRestService.getUser(anyString())) .thenReturn(Observable.just(user1FullDetails()), Observable.just(user2FullDetails())); TestSubscriber<List<User>> subscriber = new TestSubscriber<>(); userRepository.searchUsers(USER_LOGIN_RIGGAROO).subscribe(subscriber); subscriber.awaitTerminalEvent(); subscriber.assertNoErrors(); verify(githubUserRestService, times(2)).searchGithubUsers(USER_LOGIN_RIGGAROO); verify(githubUserRestService).getUser(USER_LOGIN_RIGGAROO); verify(githubUserRestService).getUser(USER_LOGIN_2_REBECCA); }
@Test public void searchUsers_GetUserIOExceptionThenSuccess_SearchUsersRetried() { when(githubUserRestService.searchGithubUsers(anyString())).thenReturn(Observable.just(githubUserList())); when(githubUserRestService.getUser(anyString())) .thenReturn(getIOExceptionError(), Observable.just(user1FullDetails()), Observable.just(user2FullDetails())); TestSubscriber<List<User>> subscriber = new TestSubscriber<>(); userRepository.searchUsers(USER_LOGIN_RIGGAROO).subscribe(subscriber); subscriber.awaitTerminalEvent(); subscriber.assertNoErrors(); verify(githubUserRestService, times(2)).searchGithubUsers(USER_LOGIN_RIGGAROO); verify(githubUserRestService, times(2)).getUser(USER_LOGIN_RIGGAROO); verify(githubUserRestService).getUser(USER_LOGIN_2_REBECCA); }
@Test public void searchUsers_OtherHttpError_SearchTerminatedWithError() { when(githubUserRestService.searchGithubUsers(anyString())).thenReturn(get403ForbiddenError()); TestSubscriber<List<User>> subscriber = new TestSubscriber<>(); userRepository.searchUsers(USER_LOGIN_RIGGAROO).subscribe(subscriber); subscriber.awaitTerminalEvent(); subscriber.assertError(HttpException.class); verify(githubUserRestService).searchGithubUsers(USER_LOGIN_RIGGAROO); verify(githubUserRestService, never()).getUser(USER_LOGIN_RIGGAROO); verify(githubUserRestService, never()).getUser(USER_LOGIN_2_REBECCA); }