XkcdStoreImpl implements XkcdStore { @Override public Observable<DomainStripe> stripeWithNum(final long stripeNum) { return mapToDomain(dataStripeWithNum(stripeNum)).cache(); } @Inject  XkcdStoreImpl(final @NonNull XkcdClient client,
      final @NonNull DatabaseEntityMapper databaseEntityMapper,
      final @NonNull DomainEntityMapper entityMapper,
      final @NonNull XkcdDatabaseHandler xkcdDatabaseHandler); @Override Observable<DomainStripe> currentStripe(); @Override Observable<DomainStripe> stripeWithNum(final long stripeNum);  }
@Test public void testGetStripeWithValidNumNoCachedSuccessful() { final DataStripe sourceStripe = generateRandomDataStripe(); final DomainStripe targetStripe = generateRandomDomainStripe(); final long generatedNum; given(mMockClient.getStripeWithId( generatedNum = ValueGenerator.generateLong(ValueGenerator.Value.REGULAR))).willReturn( Observable.just(sourceStripe)); given(mMockDomainEntityMapper.transform(sourceStripe)).willReturn(targetStripe); given(mMockXkcdDatabaseHandler.queryForStripeWithNum(generatedNum)).willReturn(null); final TestSubscriber<DomainStripe> testSubscriber = new TestSubscriber<>(); mSut.stripeWithNum(generatedNum).subscribe(testSubscriber); testSubscriber.assertNoErrors(); testSubscriber.assertReceivedOnNext(Collections.singletonList(targetStripe)); testSubscriber.assertCompleted(); }
@Test public void testGetStripeWithValidNumNoCachedNoConnection() { final Throwable error = generateNoInternetStubThrowable(); final long generatedNum; given(mMockXkcdDatabaseHandler.queryForStripeWithNum( generatedNum = ValueGenerator.generateLong(ValueGenerator.Value.REGULAR))).willReturn(null); given(mMockClient.getStripeWithId((generatedNum))).willReturn( Observable.<DataStripe>error(error)); final TestSubscriber<DomainStripe> testSubscriber = new TestSubscriber<>(); mSut.stripeWithNum(generatedNum).subscribe(testSubscriber); testSubscriber.assertError(error); testSubscriber.assertReceivedOnNext(Collections.<DomainStripe>emptyList()); testSubscriber.assertNotCompleted(); }
@Test public void testGetStripeWithValidNumCached() { final DatabaseStripe sourceStripe = generateRandomDatabaseStripe(); final DataStripe intermediateStripe = generateRandomDataStripe(); final DomainStripe targetStripe = generateRandomDomainStripe(); given(mMockXkcdDatabaseHandler.queryForStripeWithNum(anyLong())).willReturn(sourceStripe); given(mMockDatabaseEntityMapper.transform(sourceStripe)).willReturn(intermediateStripe); given(mMockDomainEntityMapper.transform(intermediateStripe)).willReturn(targetStripe); final TestSubscriber<DomainStripe> testSubscriber = new TestSubscriber<>(); mSut.stripeWithNum(generateLong(ValueGenerator.Value.REGULAR)).subscribe(testSubscriber); testSubscriber.assertNoErrors(); testSubscriber.assertReceivedOnNext(Collections.singletonList(targetStripe)); testSubscriber.assertCompleted(); testSubscriber.awaitTerminalEvent(); verify(mMockClient, never()).getStripeWithId(anyLong()); }
@Test public void testGetStripeWithNotFoundNumNoCachedSuccessful() { final Throwable error = generateNotFoundStubThrowable(); final long generatedNum; given(mMockClient.getStripeWithId( (generatedNum = ValueGenerator.generateLong(ValueGenerator.Value.REGULAR)))).willReturn( Observable.<DataStripe>error(error)); given(mMockXkcdDatabaseHandler.queryForStripeWithNum(generatedNum)).willReturn(null); final TestSubscriber<DomainStripe> testSubscriber = new TestSubscriber<>(); mSut.stripeWithNum(generatedNum).subscribe(testSubscriber); testSubscriber.assertError(error); testSubscriber.assertReceivedOnNext(Collections.<DomainStripe>emptyList()); testSubscriber.assertNotCompleted(); }