Separate method pullImageUrlsFromNetwork to allow testing

This commit is contained in:
inorichi 2015-10-09 13:38:28 +02:00
parent ea8ded549b
commit e7ecfd1e84
3 changed files with 53 additions and 45 deletions

View file

@ -62,21 +62,19 @@ public class ChapterManagerImpl extends BaseManager implements ChapterManager {
subscriber.onCompleted();
});
Observable<Integer> newChaptersObs =
chapterList
.flatMap(dbChapters -> Observable.from(chapters)
.filter(c -> !dbChapters.contains(c))
.toList()
.flatMap(this::insertChapters)
.map(PutResults::numberOfInserts));
Observable<Integer> newChaptersObs = chapterList
.flatMap(dbChapters -> Observable.from(chapters)
.filter(c -> !dbChapters.contains(c))
.toList()
.flatMap(this::insertChapters)
.map(PutResults::numberOfInserts));
Observable<Integer> deletedChaptersObs =
chapterList
.flatMap(dbChapters -> Observable.from(dbChapters)
.filter(c -> !chapters.contains(c))
.toList()
.flatMap(this::deleteChapters)
.map( d -> d.results().size() ));
Observable<Integer> deletedChaptersObs = chapterList
.flatMap(dbChapters -> Observable.from(dbChapters)
.filter(c -> !chapters.contains(c))
.toList()
.flatMap(this::deleteChapters)
.map( d -> d.results().size() ));
return Observable.zip(newChaptersObs, deletedChaptersObs,
(insertions, deletions) -> new PostResult(0, insertions, deletions)

View file

@ -114,20 +114,12 @@ public class Batoto extends Source {
String url = getUrlFromPageNumber(page);
return mNetworkService
.getStringResponse(url, mNetworkService.NULL_CACHE_CONTROL, REQUEST_HEADERS)
.flatMap(response -> Observable.just(parseHtmlToLatestUpdates(response)));
.flatMap(response -> Observable.just(parsePopularMangasFromHtml(response)));
}
private List<Manga> parseHtmlToLatestUpdates(String unparsedHtml) {
private List<Manga> parsePopularMangasFromHtml(String unparsedHtml) {
Document parsedDocument = Jsoup.parse(unparsedHtml);
List<Manga> updatedMangaList = scrapeUpdateMangasFromParsedDocument(parsedDocument);
//updateLibraryInDatabase(updatedMangaList);
return updatedMangaList;
}
private List<Manga> scrapeUpdateMangasFromParsedDocument(Document parsedDocument) {
List<Manga> updatedMangaList = new ArrayList<>();
Elements updatedHtmlBlocks = parsedDocument.select("tr:not([id]):not([class])");
@ -388,30 +380,33 @@ public class Batoto extends Source {
return mCacheManager.getImageUrlsFromDiskCache(chapterUrl)
.onErrorResumeNext(throwable -> {
return mNetworkService
.getStringResponse(chapterUrl, mNetworkService.NULL_CACHE_CONTROL, REQUEST_HEADERS)
.subscribeOn(Schedulers.io())
.flatMap(unparsedHtml -> Observable.from(parseHtmlToPageUrls(unparsedHtml)))
.buffer(3)
.concatMap(batchedPageUrls -> {
List<Observable<String>> imageUrlObservables = new ArrayList<>();
for (String pageUrl : batchedPageUrls) {
Observable<String> temporaryObservable = mNetworkService
.getStringResponse(pageUrl, mNetworkService.NULL_CACHE_CONTROL, REQUEST_HEADERS)
.flatMap(unparsedHtml -> Observable.just(parseHtmlToImageUrl(unparsedHtml)))
.subscribeOn(Schedulers.io());
imageUrlObservables.add(temporaryObservable);
}
return Observable.merge(imageUrlObservables);
})
return getImageUrlsFromNetwork(chapterUrl)
.doOnNext(imageUrl -> temporaryCachedImageUrls.add(imageUrl))
.doOnCompleted(mCacheManager.putImageUrlsToDiskCache(chapterUrl, temporaryCachedImageUrls));
})
.onBackpressureBuffer();
}
public Observable<String> getImageUrlsFromNetwork(final String chapterUrl) {
return mNetworkService
.getStringResponse(chapterUrl, mNetworkService.NULL_CACHE_CONTROL, REQUEST_HEADERS)
.flatMap(unparsedHtml -> Observable.from(parseHtmlToPageUrls(unparsedHtml)))
.buffer(3)
.concatMap(batchedPageUrls -> {
List<Observable<String>> imageUrlObservables = new ArrayList<>();
for (String pageUrl : batchedPageUrls) {
Observable<String> temporaryObservable = mNetworkService
.getStringResponse(pageUrl, mNetworkService.NULL_CACHE_CONTROL, REQUEST_HEADERS)
.flatMap(unparsedHtml -> Observable.just(parseHtmlToImageUrl(unparsedHtml)))
.subscribeOn(Schedulers.io());
imageUrlObservables.add(temporaryObservable);
}
return Observable.merge(imageUrlObservables);
});
}
private List<String> parseHtmlToPageUrls(String unparsedHtml) {
Document parsedDocument = Jsoup.parse(unparsedHtml);

View file

@ -2,6 +2,8 @@ package eu.kanade.mangafeed;
import android.os.Build;
import junit.framework.Assert;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
@ -9,10 +11,15 @@ import org.robolectric.RobolectricGradleTestRunner;
import org.robolectric.RuntimeEnvironment;
import org.robolectric.annotation.Config;
import java.util.List;
import eu.kanade.mangafeed.data.caches.CacheManager;
import eu.kanade.mangafeed.data.helpers.NetworkHelper;
import eu.kanade.mangafeed.data.models.Manga;
import eu.kanade.mangafeed.sources.Batoto;
import rx.android.schedulers.AndroidSchedulers;
import rx.observers.TestSubscriber;
import rx.schedulers.Schedulers;
@Config(constants = BuildConfig.class, sdk = Build.VERSION_CODES.LOLLIPOP)
@RunWith(RobolectricGradleTestRunner.class)
@ -32,9 +39,17 @@ public class BatotoTest {
@Test
public void testImageList() {
TestSubscriber a = new TestSubscriber();
List<String> imageUrls = b.getImageUrlsFromNetwork(chapterUrl)
.toList().toBlocking().single();
b.pullImageUrlsFromNetwork(chapterUrl).subscribe(a);
a.assertNoErrors();
Assert.assertTrue(imageUrls.size() > 5);
}
@Test
public void testMangaList() {
List<Manga> mangaList = b.pullPopularMangasFromNetwork(1)
.toBlocking().first();
Assert.assertTrue(mangaList.size() > 25);
}
}