Java Code Examples for rx.observers.TestSubscriber#assertValue()

The following examples show how to use rx.observers.TestSubscriber#assertValue() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: EnergyServiceTest.java    From openwebnet-android with MIT License 6 votes vote down vote up
@Test
public void energyService_findById() {
    String ENERGY_UUID = "myUuid";
    EnergyModel energyModel = new EnergyModel();
    energyModel.setUuid(ENERGY_UUID);

    when(energyRepository.findById(ENERGY_UUID)).thenReturn(Observable.just(energyModel));

    TestSubscriber<EnergyModel> tester = new TestSubscriber<>();
    energyService.findById(ENERGY_UUID).subscribe(tester);

    verify(energyRepository).findById(ENERGY_UUID);

    tester.assertValue(energyModel);
    tester.assertCompleted();
    tester.assertNoErrors();
}
 
Example 2
Source File: RxAggregateOperatorsUnitTest.java    From tutorials with MIT License 6 votes vote down vote up
@Test
public void givenObservable_whenUsingToSortedList_thenObtainedASortedList() {
    // given
    Observable<Integer> sourceObservable = Observable.range(10, 5);
    TestSubscriber<List> subscriber = TestSubscriber.create();

    // when
    Observable<List<Integer>> listObservable = sourceObservable.toSortedList();
    listObservable.subscribe(subscriber);

    // then
    subscriber.assertCompleted();
    subscriber.assertNoErrors();
    subscriber.assertValueCount(1);
    subscriber.assertValue(Arrays.asList(10, 11, 12, 13, 14));
}
 
Example 3
Source File: RxAggregateOperatorsUnitTest.java    From tutorials with MIT License 6 votes vote down vote up
@Test
public void givenObservable_whenCounting_thenObtainingNumberOfElements() {
    // given
    List<String> lettersList = Arrays.asList("A", "B", "C", "D", "E", "F", "G");

    TestSubscriber<Integer> subscriber = TestSubscriber.create();

    // when
    Observable<Integer> sourceObservable = Observable.from(lettersList)
      .count();
    sourceObservable.subscribe(subscriber);

    // then
    subscriber.assertCompleted();
    subscriber.assertNoErrors();
    subscriber.assertValueCount(1);
    subscriber.assertValue(7);
}
 
Example 4
Source File: CacheHelperTest.java    From aptoide-client-v8 with GNU General Public License v3.0 6 votes vote down vote up
@Test public void shouldReturnZeroWhenCacheNotExpiredButSizeNotExceeded() {
  List<CacheHelper.FolderToManage> folders = new LinkedList<>();
  CacheHelper.FolderToManage mockedFolder = mock(CacheHelper.FolderToManage.class);
  File mockedFile = mock(File.class);
  when(mockedFolder.getFolder()).thenReturn(mockedFile);
  when(mockedFolder.getCacheTime()).thenReturn(1000L);
  folders.add(mockedFolder);
  FileUtils fileUtilsMock = mock(FileUtils.class);
  when(fileUtilsMock.dirSize(mockedFile)).thenReturn(10L);
  cacheHelper = new CacheHelper(11L, folders, fileUtilsMock);

  TestSubscriber subscriber = TestSubscriber.create();
  cacheHelper.cleanCache()
      .subscribe(subscriber);
  subscriber.awaitTerminalEvent();
  subscriber.assertCompleted();
  subscriber.assertNoErrors();
  subscriber.assertValueCount(1);
  subscriber.assertValue(0L);
}
 
Example 5
Source File: CacheHelperTest.java    From aptoide-client-v8 with GNU General Public License v3.0 6 votes vote down vote up
@Test public void shouldReturnZeroBytesWhenCacheFoldersHasNoFilesEmpty() {
  List<CacheHelper.FolderToManage> folders = new LinkedList<>();
  CacheHelper.FolderToManage mockedFolder = mock(CacheHelper.FolderToManage.class);
  File mockedFile = mock(File.class);
  when(mockedFolder.getFolder()).thenReturn(mockedFile);
  folders.add(mockedFolder);
  FileUtils fileUtilsMock = mock(FileUtils.class);
  when(fileUtilsMock.dirSize(mockedFile)).thenReturn(0L);
  cacheHelper = new CacheHelper(0, folders, fileUtilsMock);

  TestSubscriber subscriber = TestSubscriber.create();
  cacheHelper.cleanCache()
      .subscribe(subscriber);
  subscriber.awaitTerminalEvent();
  subscriber.assertCompleted();
  subscriber.assertNoErrors();
  subscriber.assertValueCount(1);
  subscriber.assertValue(0L);
}
 
Example 6
Source File: IpcamServiceTest.java    From openwebnet-android with MIT License 6 votes vote down vote up
@Test
public void ipcamService_findById() {
    String IPCAM_UUID = "myUuid";
    IpcamModel ipcamModel = new IpcamModel();
    ipcamModel.setUuid(IPCAM_UUID);

    when(ipcamRepository.findById(IPCAM_UUID)).thenReturn(Observable.just(ipcamModel));

    TestSubscriber<IpcamModel> tester = new TestSubscriber<>();
    ipcamService.findById(IPCAM_UUID).subscribe(tester);

    verify(ipcamRepository).findById(IPCAM_UUID);

    tester.assertValue(ipcamModel);
    tester.assertCompleted();
    tester.assertNoErrors();
}
 
Example 7
Source File: DataManagerTest.java    From ribot-app-android with Apache License 2.0 6 votes vote down vote up
@Test
public void signInSuccessful() {
    // Stub GoogleAuthHelper and RibotService mocks
    RibotService.SignInResponse signInResponse = new RibotService.SignInResponse();
    signInResponse.ribot = MockModelFabric.newRibot();
    signInResponse.accessToken = MockModelFabric.randomString();
    Account account = new Account("[email protected]", "google.com");
    String googleAccessCode = MockModelFabric.randomString();
    doReturn(Observable.just(googleAccessCode))
            .when(mMockGoogleAuthHelper)
            .retrieveAuthTokenAsObservable(account);
    doReturn(Observable.just(signInResponse))
            .when(mMockRibotsService)
            .signIn(any(RibotService.SignInRequest.class));

    // Test the sign in Observable
    TestSubscriber<Ribot> testSubscriber = new TestSubscriber<>();
    mDataManager.signIn(account).subscribe(testSubscriber);
    testSubscriber.assertValue(signInResponse.ribot);
    testSubscriber.assertCompleted();
    testSubscriber.assertNoErrors();

    verify(mMockPreferencesHelper).putAccessToken(signInResponse.accessToken);
    verify(mMockPreferencesHelper).putSignedInRibot(signInResponse.ribot);
}
 
Example 8
Source File: AutomationServiceTest.java    From openwebnet-android with MIT License 5 votes vote down vote up
@Test
public void automationService_moveDown() {
    AutomationModel automation = mockAutomationModel();
    mockClient();

    TestSubscriber<AutomationModel> tester = new TestSubscriber<>();
    automationService.moveDown(automation).subscribe(tester);

    verify(commonService).findClient(GATEWAY_UUID);

    tester.assertValue(automation);
    tester.assertCompleted();
    tester.assertNoErrors();
}
 
Example 9
Source File: SimpleUploadDataStoreTest.java    From RxUploader with Apache License 2.0 5 votes vote down vote up
@SuppressLint("ApplySharedPref")
@Test
public void delete() throws Exception {
    final Job test = createTestJob();
    final String json = gson.toJson(test);
    final String key = SimpleUploadDataStore.jobIdKey(test.id());
    final Set<String> keys = new HashSet<>();
    keys.add(key);

    final SharedPreferences.Editor editor = sharedPreferences.edit();
    editor.putStringSet(SimpleUploadDataStore.KEY_JOB_IDS, keys);
    editor.putString(key, json);
    editor.commit();

    final TestSubscriber<Job> ts = TestSubscriber.create();
    dataStore.delete(test.id()).subscribe(ts);

    ts.awaitTerminalEvent(1, TimeUnit.SECONDS);
    ts.assertNoErrors();
    ts.assertValueCount(1);
    ts.assertValue(test);

    final TestSubscriber<Job> ts2 = TestSubscriber.create();
    dataStore.getAll().subscribe(ts2);

    ts2.awaitTerminalEvent(1, TimeUnit.SECONDS);
    ts2.assertNoErrors();
    ts2.assertCompleted();
    ts2.assertNoValues();
}
 
Example 10
Source File: GatewayServiceTest.java    From openwebnet-android with MIT License 5 votes vote down vote up
@Ignore // Caused by: javassist.NotFoundException: io.reactivex.Flowable
@Test
public void gatewayService_add() {
    String GATEWAY_UUID = "gatewayUuid";
    String GATEWAY_HOST = "1.1.1.1";
    Integer GATEWAY_PORT = 88;
    String GATEWAY_PASSWORD = "PASSWORD";

    GatewayModel gateway = new GatewayModel();
    gateway.setUuid(GATEWAY_UUID);
    gateway.setHost(GATEWAY_HOST);
    gateway.setPort(GATEWAY_PORT);
    gateway.setPassword(GATEWAY_PASSWORD);

    PowerMockito.mockStatic(GatewayModel.class);
    when(GatewayModel.newGateway(GATEWAY_HOST, GATEWAY_PORT, GATEWAY_PASSWORD)).thenReturn(gateway);
    when(gatewayRepository.add(gateway)).thenReturn(Observable.just(GATEWAY_UUID));

    TestSubscriber<String> tester = new TestSubscriber<>();
    gatewayService.add(gateway).subscribe(tester);

    verify(gatewayRepository).add(gateway);

    tester.assertValue(GATEWAY_UUID);
    tester.assertCompleted();
    tester.assertNoErrors();
}
 
Example 11
Source File: RxPaperBookTest.java    From RxPaper with MIT License 5 votes vote down vote up
@Test
public void testUpdatesUnchecked() throws Exception {
    RxPaperBook book = RxPaperBook.with("UPDATES_UNCH", Schedulers.immediate());
    final String key = "hello";
    final ComplexObject value = ComplexObject.random();
    final TestSubscriber<ComplexObject> updatesSubscriber = TestSubscriber.create();
    book.<ComplexObject> observeUnsafe(key).subscribe(updatesSubscriber);
    updatesSubscriber.assertValueCount(0);
    book.write(key, value).subscribe();
    updatesSubscriber.assertValueCount(1);
    updatesSubscriber.assertValue(value);
    final ComplexObject newValue = ComplexObject.random();
    book.write(key, newValue).subscribe();
    updatesSubscriber.assertValueCount(2);
    updatesSubscriber.assertValues(value, newValue);
    // Error value
    final int wrongValue = 3;
    book.<ComplexObject> observeUnsafe(key).subscribe(new Subscriber<ComplexObject>() {
        @Override
        public void onCompleted() {
            Assert.fail("Expected ClassCastException");
        }

        @Override
        public void onError(Throwable e) {
            if (!(e instanceof ClassCastException)) {
                Assert.fail(e.getMessage());
            }
        }

        @Override
        public void onNext(ComplexObject complexObject) {
            Assert.fail("Expected ClassCastException");
        }
    });
    book.write(key, wrongValue).subscribe();
}
 
Example 12
Source File: RxStoresTest.java    From grox with Apache License 2.0 5 votes vote down vote up
@Test
public void states_should_observeInitialState() {
  //GIVEN
  Store<Integer> store = new Store<>(0);
  TestSubscriber<Integer> testSubscriber = new TestSubscriber<>();

  //WHEN
  states(store).subscribe(testSubscriber);

  //THEN
  testSubscriber.assertNoTerminalEvent();
  testSubscriber.assertValue(0);
}
 
Example 13
Source File: MapObservableProviderTest.java    From RxGoogleMaps with Apache License 2.0 5 votes vote down vote up
@Test
public void shouldReturnMapFromSupportFragment() {
    TestSubscriber<GoogleMap> subscriber = new TestSubscriber<>();
    new MapObservableProvider(supportMapFragment)
            .getMapReadyObservable()
            .subscribe(subscriber);
    verify(supportMapFragment).getMapAsync(argumentCaptor.capture());
    argumentCaptor.getValue().onMapReady(googleMap);
    subscriber.assertNoErrors();
    subscriber.assertValue(googleMap);
}
 
Example 14
Source File: RxJavaFilterOperatorsIntegrationTest.java    From tutorials with MIT License 5 votes vote down vote up
@Test
public void givenRangeObservable_whenFilteringWithLast_thenOnlyLastItemIsEmitted() {

    Observable<Integer> sourceObservable = Observable.range(1, 10);
    TestSubscriber<Integer> subscriber = new TestSubscriber();

    Observable<Integer> filteredObservable = sourceObservable.last(i -> i % 2 != 0);

    filteredObservable.subscribe(subscriber);

    subscriber.assertCompleted();
    subscriber.assertNoErrors();
    subscriber.assertValueCount(1);
    subscriber.assertValue(9);
}
 
Example 15
Source File: LightServiceTest.java    From openwebnet-android with MIT License 5 votes vote down vote up
@Test
public void lightService_findFavourites() {
    List<LightModel> lights = new ArrayList<>();

    when(lightRepository.findFavourites()).thenReturn(Observable.just(lights));

    TestSubscriber<List<LightModel>> tester = new TestSubscriber<>();
    lightService.findFavourites().subscribe(tester);

    verify(lightRepository).findFavourites();

    tester.assertValue(lights);
    tester.assertCompleted();
    tester.assertNoErrors();
}
 
Example 16
Source File: CacheHelperTest.java    From aptoide-client-v8 with GNU General Public License v3.0 5 votes vote down vote up
@Test public void shouldReturnZeroBytesWhenCacheFoldersEmpty() {
  List<CacheHelper.FolderToManage> folders = new LinkedList<>();
  FileUtils fileUtilsMock = mock(FileUtils.class);
  cacheHelper = new CacheHelper(0, folders, fileUtilsMock);

  TestSubscriber subscriber = TestSubscriber.create();
  cacheHelper.cleanCache()
      .subscribe(subscriber);
  subscriber.awaitTerminalEvent();
  subscriber.assertCompleted();
  subscriber.assertNoErrors();
  subscriber.assertValueCount(1);
  subscriber.assertValue(0L);
}
 
Example 17
Source File: TemperatureServiceTest.java    From openwebnet-android with MIT License 5 votes vote down vote up
@Test
public void temperatureService_requestByEnvironment() {
    Integer ENVIRONMENT = 108;
    List<TemperatureModel> temperatures = new ArrayList<>();

    mockClient();
    when(temperatureRepository.findByEnvironment(ENVIRONMENT)).thenReturn(Observable.just(temperatures));

    TestSubscriber<List<TemperatureModel>> tester = new TestSubscriber<>();
    temperatureService.requestByEnvironment(ENVIRONMENT).subscribe(tester);

    tester.assertValue(temperatures);
    tester.assertCompleted();
    tester.assertNoErrors();
}
 
Example 18
Source File: BitmapSnapshotFuncTest.java    From RxGoogleMaps with Apache License 2.0 5 votes vote down vote up
@Test
public void shouldProvideBitmap() throws Exception {
    TestSubscriber<Bitmap> testSubscriber = new TestSubscriber<>();
    new BitmapSnapshotFunc(null).call(googleMap)
            .subscribe(testSubscriber);
    verify(googleMap).snapshot(argumentCaptor.capture(), bitmapArgumentCaptor.capture());
    argumentCaptor.getValue().onSnapshotReady(null);
    testSubscriber.assertNoErrors();
    testSubscriber.assertValueCount(1);
    testSubscriber.assertValue(null);
}
 
Example 19
Source File: RxJavaFilterOperatorsIntegrationTest.java    From tutorials with MIT License 5 votes vote down vote up
@Test
public void givenRangeObservable_whenFilteringWithLastAndDefault_thenOnlyDefaultIsEmitted() {

    Observable<Integer> sourceObservable = Observable.range(1, 10);
    TestSubscriber<Integer> subscriber = new TestSubscriber();

    Observable<Integer> filteredObservable = sourceObservable.lastOrDefault(-1, i -> i > 10);

    filteredObservable.subscribe(subscriber);

    subscriber.assertCompleted();
    subscriber.assertNoErrors();
    subscriber.assertValueCount(1);
    subscriber.assertValue(-1);
}
 
Example 20
Source File: EpicMiddlewareTest.java    From reductor with Apache License 2.0 5 votes vote down vote up
@Test
public void testPropagateActionsToEpic() {
    store = Store.create(reducer, epicMiddleware);

    TestSubscriber<Action> subscriber = TestSubscriber.create();
    actionsCaptor.getValue().subscribe(subscriber);

    Action testAction = Action.create("TEST");
    store.dispatch(testAction);

    subscriber.assertValue(testAction);
}