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

The following examples show how to use rx.observers.TestSubscriber#assertCompleted() . 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: AwaitableEventSubscriberDecoratorTest.java    From mesos-rxjava with Apache License 2.0 6 votes vote down vote up
@Test
public void awaitEventWorks_onCompleted() throws Exception {
    final TestSubscriber<String> testSubscriber = new TestSubscriber<>();
    final AwaitableEventSubscriberDecorator<String> sub = new AwaitableEventSubscriberDecorator<>(testSubscriber);

    final BehaviorSubject<String> subject = BehaviorSubject.create();

    final Subscription subscription = subject.subscribe(sub);

    async.run(subject::onCompleted);

    sub.awaitEvent();
    testSubscriber.assertNoValues();
    testSubscriber.assertCompleted();
    subscription.unsubscribe();
}
 
Example 2
Source File: RxFirebaseUserTests.java    From RxFirebase with Apache License 2.0 6 votes vote down vote up
@Test
public void reauthenticate()  {

    TestSubscriber<Void> testSubscriber = new TestSubscriber<>();
    RxFirebaseUser.reauthenticate(mockUser, credential)
            .subscribeOn(Schedulers.immediate())
            .subscribe(testSubscriber);

    testOnSuccessListener.getValue().onSuccess(mockRes);
    testOnCompleteListener.getValue().onComplete(mockTask);

    verify(mockUser).reauthenticate(credential);

    testSubscriber.assertNoErrors();
    testSubscriber.assertValueCount(1);
    testSubscriber.assertReceivedOnNext(Collections.singletonList(mockRes));
    testSubscriber.assertCompleted();
    testSubscriber.unsubscribe();
}
 
Example 3
Source File: RxStringOperatorsUnitTest.java    From tutorials with MIT License 6 votes vote down vote up
@Test
public void givenStringObservable_whenEncodingString_ThenSuccessfullObtainingByteStream()
{
    //given
    Observable<String> sourceObservable = Observable.just("Lorem ipsum loream");
    TestSubscriber<byte[]> subscriber = TestSubscriber.create();

    // when
    StringObservable.encode(sourceObservable, StandardCharsets.UTF_8)
        .subscribe(subscriber);

    // then
    subscriber.assertCompleted();
    subscriber.assertNoErrors();
    subscriber.assertValueCount(1);
    subscriber.getOnNextEvents()
        .stream()
        .forEach(bytes -> Assert.assertTrue(Arrays.equals(bytes, "Lorem ipsum loream".getBytes(StandardCharsets.UTF_8))));
}
 
Example 4
Source File: DatabaseHelperTest.java    From ribot-app-android with Apache License 2.0 6 votes vote down vote up
@Test
public void findRegisteredBeacon() {
    RegisteredBeacon beaconToFind = MockModelFabric.newRegisteredBeacon();
    RegisteredBeacon anotherBeacon = MockModelFabric.newRegisteredBeacon();
    mDatabaseHelper.getBriteDb().insert(Db.BeaconTable.TABLE_NAME,
            Db.BeaconTable.toContentValues(beaconToFind));
    mDatabaseHelper.getBriteDb().insert(Db.BeaconTable.TABLE_NAME,
            Db.BeaconTable.toContentValues(anotherBeacon));

    TestSubscriber<RegisteredBeacon> testSubscriber = new TestSubscriber<>();
    mDatabaseHelper.findRegisteredBeacon(beaconToFind.uuid,
            beaconToFind.major, beaconToFind.minor)
            .subscribe(testSubscriber);

    //zone is not saved in DB so we won't expect it to be returned
    beaconToFind.zone = null;
    testSubscriber.assertNoErrors();
    testSubscriber.assertValue(beaconToFind);
    testSubscriber.assertCompleted();
}
 
Example 5
Source File: RxFirebaseUserTests.java    From RxFirebase with Apache License 2.0 6 votes vote down vote up
@Test
public void updateProfile()  {

    TestSubscriber<Void> testSubscriber = new TestSubscriber<>();
    RxFirebaseUser.updateProfile(mockUser, userProfileChangeRequest)
            .subscribeOn(Schedulers.immediate())
            .subscribe(testSubscriber);

    testOnSuccessListener.getValue().onSuccess(mockRes);
    testOnCompleteListener.getValue().onComplete(mockTask);

    verify(mockUser).updateProfile(userProfileChangeRequest);

    testSubscriber.assertNoErrors();
    testSubscriber.assertValueCount(1);
    testSubscriber.assertReceivedOnNext(Collections.singletonList(mockRes));
    testSubscriber.assertCompleted();
    testSubscriber.unsubscribe();
}
 
Example 6
Source File: TransformerStateMachineTest.java    From rxjava-extras with Apache License 2.0 6 votes vote down vote up
@Test
public void testForCompletionWithinStateMachine() {
    Func0<Integer> initialState = Functions.constant0(1);
    Func3<Integer, Integer, Subscriber<Integer>, Integer> transition = new Func3<Integer, Integer, Subscriber<Integer>, Integer>() {

        @Override
        public Integer call(Integer collection, Integer t, Subscriber<Integer> subscriber) {
            subscriber.onNext(123);
            // complete from within transition
            subscriber.onCompleted();
            return 1;
        }

    };
    Func2<? super Integer, ? super Subscriber<Integer>, Boolean> completion = Functions
            .alwaysTrue2();
    Transformer<Integer, Integer> transformer = Transformers.stateMachine(initialState,
            transition, completion);
    TestSubscriber<Integer> ts = new TestSubscriber<Integer>();
    final AtomicInteger count = new AtomicInteger(0);
    Observable.just(1, 2, 3).doOnNext(Actions.increment1(count)).compose(transformer)
            .subscribe(ts);
    ts.assertValues(123);
    ts.assertCompleted();
    assertEquals(1, count.get());
}
 
Example 7
Source File: HystrixBuilderTest.java    From feign with Apache License 2.0 6 votes vote down vote up
@Test
public void rxCompletableWithBody() {
  server.enqueue(new MockResponse().setBody("foo"));

  final TestInterface api = target();

  final Completable completable = api.completable();

  assertThat(completable).isNotNull();
  assertThat(server.getRequestCount()).isEqualTo(0);

  final TestSubscriber<String> testSubscriber = new TestSubscriber<String>();
  completable.subscribe(testSubscriber);
  testSubscriber.awaitTerminalEvent();

  testSubscriber.assertCompleted();
  testSubscriber.assertNoErrors();
}
 
Example 8
Source File: RxMathematicalOperatorsUnitTest.java    From tutorials with MIT License 6 votes vote down vote up
@Test
public void givenRangeNumericObservable_whenCalculatingSum_ThenSuccessfull() {
    // given
    Observable<Integer> sourceObservable = Observable.range(1, 20);
    TestSubscriber<Integer> subscriber = TestSubscriber.create();

    // when
    MathObservable.sumInteger(sourceObservable)
        .subscribe(subscriber);

    // then
    subscriber.assertCompleted();
    subscriber.assertNoErrors();
    subscriber.assertValueCount(1);
    subscriber.assertValue(210);

}
 
Example 9
Source File: DatabaseHelperTest.java    From ribot-app-android with Apache License 2.0 6 votes vote down vote up
@Test
public void setRegisteredBeaconsWithEmptyListClearsData() {
    List<RegisteredBeacon> existingBeacons = MockModelFabric.newRegisteredBeaconList(10);
    mDatabaseHelper.setRegisteredBeacons(existingBeacons).subscribe();

    TestSubscriber<Void> testSubscriber = new TestSubscriber<>();
    mDatabaseHelper.setRegisteredBeacons(new ArrayList<RegisteredBeacon>())
            .subscribe(testSubscriber);

    testSubscriber.assertNoErrors();
    testSubscriber.assertNoValues();
    testSubscriber.assertCompleted();

    Cursor cursor = mDatabaseHelper.getBriteDb()
            .query("SELECT * FROM " + Db.BeaconTable.TABLE_NAME);
    assertEquals(0, cursor.getCount());
}
 
Example 10
Source File: SoundServiceTest.java    From openwebnet-android with MIT License 6 votes vote down vote up
@Test
public void soundService_findByEnvironment() {
    Integer ENVIRONMENT = 108;
    List<SoundModel> sounds = new ArrayList<>();

    when(soundRepository.findByEnvironment(ENVIRONMENT)).thenReturn(Observable.just(sounds));

    TestSubscriber<List<SoundModel>> tester = new TestSubscriber<>();
    soundService.findByEnvironment(ENVIRONMENT).subscribe(tester);

    verify(soundRepository).findByEnvironment(ENVIRONMENT);

    tester.assertValue(sounds);
    tester.assertCompleted();
    tester.assertNoErrors();
}
 
Example 11
Source File: ScenarioServiceTest.java    From openwebnet-android with MIT License 5 votes vote down vote up
@Test
public void scenarioService_delete() {
    String SCENARIO_UUID = "myUuid";

    when(scenarioRepository.delete(SCENARIO_UUID)).thenReturn(Observable.just(null));

    TestSubscriber<Void> tester = new TestSubscriber<>();
    scenarioService.delete(SCENARIO_UUID).subscribe(tester);

    verify(scenarioRepository).delete(SCENARIO_UUID);

    tester.assertCompleted();
    tester.assertNoErrors();
}
 
Example 12
Source File: CharacterNetworkDataSourceTest.java    From Game-of-Thrones with Apache License 2.0 5 votes vote down vote up
@Test
public void
get_all_got_characters() {
  TestSubscriber<List<GoTCharacter>> testSubscriber = new TestSubscriber<>();

  networkDataSource.getAll().subscribe(testSubscriber);

  testSubscriber.assertCompleted();
  testSubscriber.assertNoErrors();
}
 
Example 13
Source File: RxJavaFilterOperatorsIntegrationTest.java    From tutorials with MIT License 5 votes vote down vote up
@Test
public void givenRangeObservable_whenFilteringItems_thenOddItemsAreFiltered() {

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

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

    filteredObservable.subscribe(subscriber);

    subscriber.assertCompleted();
    subscriber.assertNoErrors();
    subscriber.assertValueCount(5);
    subscriber.assertValues(1, 3, 5, 7, 9);
}
 
Example 14
Source File: EnergyServiceTest.java    From openwebnet-android with MIT License 5 votes vote down vote up
@Test
public void energyService_findFavourites() {
    List<EnergyModel> energies = new ArrayList<>();

    when(energyRepository.findFavourites()).thenReturn(Observable.just(energies));

    TestSubscriber<List<EnergyModel>> tester = new TestSubscriber<>();
    energyService.findFavourites().subscribe(tester);

    verify(energyRepository).findFavourites();

    tester.assertValue(energies);
    tester.assertCompleted();
    tester.assertNoErrors();
}
 
Example 15
Source File: RxJavaFilterOperatorsIntegrationTest.java    From tutorials with MIT License 5 votes vote down vote up
@Test
public void givenRangeObservable_whenFilteringWithFirst_thenOnlyFirstThreeItemsAreEmitted() {

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

    Observable<Integer> filteredObservable = sourceObservable.first();

    filteredObservable.subscribe(subscriber);

    subscriber.assertCompleted();
    subscriber.assertNoErrors();
    subscriber.assertValueCount(1);
    subscriber.assertValue(1);
}
 
Example 16
Source File: TransformerStateMachineTest.java    From rxjava-extras with Apache License 2.0 5 votes vote down vote up
@Test
public void testDoesNotRequestMaxValueOfUpstreamIfBackpressureBufferOptionSelected() {
    final AtomicLong requested = new AtomicLong();
    Observable<Integer> o = Observable.range(1, 200) //
            .doOnRequest(new Action1<Long>() {
                @Override
                public void call(Long n) {
                    BackpressureUtils.getAndAddRequest(requested, n);
                }
            });
    TestSubscriber<Integer> ts = TestSubscriber.create(0);
    o //
            .compose(Transformers.stateMachine().initialState(null)
                    .transition(new Transition<Object, Integer, Integer>() {

                        @Override
                        public Object call(Object state, Integer value,
                                Subscriber<Integer> subscriber) {
                            // subscriber.onNext(value);
                            return state;
                        }
                    }).build())
            // get as list
            .subscribe(ts);
    ts.requestMore(4);
    assertEquals(201, requested.get());
    ts.assertCompleted();
    ts.assertNoValues();
}
 
Example 17
Source File: TemperatureServiceTest.java    From openwebnet-android with MIT License 5 votes vote down vote up
@Test
public void temperatureService_delete() {
    String TEMPERATURE_UUID = "myUuid";

    when(temperatureRepository.delete(TEMPERATURE_UUID)).thenReturn(Observable.just(null));

    TestSubscriber<Void> tester = new TestSubscriber<>();
    temperatureService.delete(TEMPERATURE_UUID).subscribe(tester);

    verify(temperatureRepository).delete(TEMPERATURE_UUID);

    tester.assertCompleted();
    tester.assertNoErrors();
}
 
Example 18
Source File: RxJavaSkipOperatorsIntegrationTest.java    From tutorials with MIT License 5 votes vote down vote up
@Test
public void givenRangeObservable_whenSkippingLast_thenLastFiveItemsAreSkipped() {

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

    Observable<Integer> filteredObservable = sourceObservable.skipLast(5);

    filteredObservable.subscribe(subscriber);

    subscriber.assertCompleted();
    subscriber.assertNoErrors();
    subscriber.assertValueCount(5);
    subscriber.assertValues(1, 2, 3, 4, 5);
}
 
Example 19
Source File: ObserveOnTest.java    From akarnokd-misc with Apache License 2.0 4 votes vote down vote up
@Test
public void completeExactRequest() {
    TestSubscriber<Integer> ts = TestSubscriber.create(0);

    TestScheduler test = Schedulers.test();

    Observable.range(1, 10).observeOn(test).subscribe(ts);

    test.advanceTimeBy(1, TimeUnit.SECONDS);

    ts.assertNoValues();
    ts.assertNoErrors();
    ts.assertNotCompleted();

    ts.requestMore(10);

    test.advanceTimeBy(1, TimeUnit.SECONDS);

    ts.assertValueCount(10);
    ts.assertNoErrors();
    ts.assertCompleted();
}
 
Example 20
Source File: TransformerOnTerminateResumeTest.java    From rxjava-extras with Apache License 2.0 3 votes vote down vote up
@Test
public void mainCompletesBackpressure() {
    
    Transformer<Integer, Integer> op = new TransformerOnTerminateResume<Integer>(new Func1<Throwable, Observable<Integer>>() {
        @Override
        public Observable<Integer> call(Throwable e) {
            return Observable.just(11);
        }
    }, Observable.just(12));
    
    TestSubscriber<Integer> ts = TestSubscriber.create(0);
    
    Observable.range(1, 10)
    .compose(op)
    .subscribe(ts);
    
    ts.assertNoValues();
    
    ts.requestMore(2);

    ts.assertValues(1, 2);

    ts.requestMore(5);

    ts.assertValues(1, 2, 3, 4, 5, 6, 7);

    ts.requestMore(3);

    ts.assertValues(1, 2, 3, 4, 5, 6, 7, 8, 9, 10);

    ts.requestMore(1);

    ts.assertValues(1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 12);
    ts.assertNoErrors();
    ts.assertCompleted();
}