ObtainUserAction { public Observable<Optional<SevibusUser>> obtainUser() { return userDataSource.getCurrentUser() .map(this::associateWithCrashReporting) .map(Optional::of) .switchIfEmpty(Observable.just(Optional.absent())); } ObtainUserAction(UserDataSource userDataSource, CrashReportingTool crashReportingTool); Observable<Optional<SevibusUser>> obtainUser();  }
@Test public void returns_user_in_datasource_when_present() throws Exception { when(userDataSource.getCurrentUser()).thenReturn(Observable.just(STUB_USER)); Optional<SevibusUser> result = obtainCurrentUser.obtainUser().toBlocking().single(); assertThat(result.isPresent()).isTrue(); assertThat(result.get()).isEqualTo(STUB_USER); }
@Test public void returns_absent_when_user_not_in_datasource() throws Exception { when(userDataSource.getCurrentUser()).thenReturn(Observable.empty()); Optional<SevibusUser> result = obtainCurrentUser.obtainUser().toBlocking().single(); assertThat(result.isPresent()).isFalse(); }
@Test public void associate_user_with_crash_reporting_tool() throws Exception { when(userDataSource.getCurrentUser()).thenReturn(Observable.just(STUB_USER)); obtainCurrentUser.obtainUser() .toBlocking().single(); verify(crashReportingTool).associateUser(any(SevibusUser.class)); }