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
app/src
main/java/eu/kanade/mangafeed
test/java/eu/kanade/mangafeed

View File

@ -62,16 +62,14 @@ public class ChapterManagerImpl extends BaseManager implements ChapterManager {
subscriber.onCompleted(); subscriber.onCompleted();
}); });
Observable<Integer> newChaptersObs = Observable<Integer> newChaptersObs = chapterList
chapterList
.flatMap(dbChapters -> Observable.from(chapters) .flatMap(dbChapters -> Observable.from(chapters)
.filter(c -> !dbChapters.contains(c)) .filter(c -> !dbChapters.contains(c))
.toList() .toList()
.flatMap(this::insertChapters) .flatMap(this::insertChapters)
.map(PutResults::numberOfInserts)); .map(PutResults::numberOfInserts));
Observable<Integer> deletedChaptersObs = Observable<Integer> deletedChaptersObs = chapterList
chapterList
.flatMap(dbChapters -> Observable.from(dbChapters) .flatMap(dbChapters -> Observable.from(dbChapters)
.filter(c -> !chapters.contains(c)) .filter(c -> !chapters.contains(c))
.toList() .toList()

View File

@ -114,20 +114,12 @@ public class Batoto extends Source {
String url = getUrlFromPageNumber(page); String url = getUrlFromPageNumber(page);
return mNetworkService return mNetworkService
.getStringResponse(url, mNetworkService.NULL_CACHE_CONTROL, REQUEST_HEADERS) .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); 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<>(); List<Manga> updatedMangaList = new ArrayList<>();
Elements updatedHtmlBlocks = parsedDocument.select("tr:not([id]):not([class])"); Elements updatedHtmlBlocks = parsedDocument.select("tr:not([id]):not([class])");
@ -388,9 +380,16 @@ public class Batoto extends Source {
return mCacheManager.getImageUrlsFromDiskCache(chapterUrl) return mCacheManager.getImageUrlsFromDiskCache(chapterUrl)
.onErrorResumeNext(throwable -> { .onErrorResumeNext(throwable -> {
return getImageUrlsFromNetwork(chapterUrl)
.doOnNext(imageUrl -> temporaryCachedImageUrls.add(imageUrl))
.doOnCompleted(mCacheManager.putImageUrlsToDiskCache(chapterUrl, temporaryCachedImageUrls));
})
.onBackpressureBuffer();
}
public Observable<String> getImageUrlsFromNetwork(final String chapterUrl) {
return mNetworkService return mNetworkService
.getStringResponse(chapterUrl, mNetworkService.NULL_CACHE_CONTROL, REQUEST_HEADERS) .getStringResponse(chapterUrl, mNetworkService.NULL_CACHE_CONTROL, REQUEST_HEADERS)
.subscribeOn(Schedulers.io())
.flatMap(unparsedHtml -> Observable.from(parseHtmlToPageUrls(unparsedHtml))) .flatMap(unparsedHtml -> Observable.from(parseHtmlToPageUrls(unparsedHtml)))
.buffer(3) .buffer(3)
.concatMap(batchedPageUrls -> { .concatMap(batchedPageUrls -> {
@ -405,11 +404,7 @@ public class Batoto extends Source {
} }
return Observable.merge(imageUrlObservables); return Observable.merge(imageUrlObservables);
}) });
.doOnNext(imageUrl -> temporaryCachedImageUrls.add(imageUrl))
.doOnCompleted(mCacheManager.putImageUrlsToDiskCache(chapterUrl, temporaryCachedImageUrls));
})
.onBackpressureBuffer();
} }
private List<String> parseHtmlToPageUrls(String unparsedHtml) { private List<String> parseHtmlToPageUrls(String unparsedHtml) {

View File

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