Search in sources :

Example 1 with TranslationList

use of com.quran.labs.androidquran.dao.translation.TranslationList in project quran_android by quran.

the class TranslationManagerPresenterTest method getRemoteTranslationListObservableIssue.

@Test
public void getRemoteTranslationListObservableIssue() throws Exception {
    MockResponse mockResponse = new MockResponse();
    mockResponse.setResponseCode(500);
    this.mockWebServer.enqueue(mockResponse);
    TestObserver<TranslationList> testObserver = new TestObserver<>();
    this.translationManager.getRemoteTranslationListObservable().subscribe(testObserver);
    testObserver.awaitTerminalEvent();
    testObserver.assertNoValues();
    testObserver.assertError(IOException.class);
}
Also used : MockResponse(okhttp3.mockwebserver.MockResponse) TranslationList(com.quran.labs.androidquran.dao.translation.TranslationList) TestObserver(io.reactivex.observers.TestObserver) Test(org.junit.Test)

Example 2 with TranslationList

use of com.quran.labs.androidquran.dao.translation.TranslationList in project quran_android by quran.

the class TranslationManagerPresenter method getCachedTranslationListObservable.

Observable<TranslationList> getCachedTranslationListObservable(final boolean forceDownload) {
    return Observable.defer(() -> {
        boolean isCacheStale = System.currentTimeMillis() - quranSettings.getLastUpdatedTranslationDate() > Constants.MIN_TRANSLATION_REFRESH_TIME;
        if (forceDownload || isCacheStale) {
            return Observable.empty();
        }
        try {
            File cachedFile = getCachedFile();
            if (cachedFile.exists()) {
                Moshi moshi = new Moshi.Builder().build();
                JsonAdapter<TranslationList> jsonAdapter = moshi.adapter(TranslationList.class);
                return Observable.just(jsonAdapter.fromJson(Okio.buffer(Okio.source(cachedFile))));
            }
        } catch (Exception e) {
            Crashlytics.logException(e);
        }
        return Observable.empty();
    });
}
Also used : Moshi(com.squareup.moshi.Moshi) TranslationList(com.quran.labs.androidquran.dao.translation.TranslationList) File(java.io.File)

Example 3 with TranslationList

use of com.quran.labs.androidquran.dao.translation.TranslationList in project quran_android by quran.

the class TranslationManagerPresenter method writeTranslationList.

void writeTranslationList(TranslationList list) {
    File cacheFile = getCachedFile();
    try {
        File directory = cacheFile.getParentFile();
        boolean directoryExists = directory.mkdirs() || directory.isDirectory();
        if (directoryExists) {
            if (cacheFile.exists()) {
                cacheFile.delete();
            }
            Moshi moshi = new Moshi.Builder().build();
            JsonAdapter<TranslationList> jsonAdapter = moshi.adapter(TranslationList.class);
            BufferedSink sink = Okio.buffer(Okio.sink(cacheFile));
            jsonAdapter.toJson(sink, list);
            sink.close();
            quranSettings.setLastUpdatedTranslationDate(System.currentTimeMillis());
        }
    } catch (Exception e) {
        cacheFile.delete();
        Crashlytics.logException(e);
    }
}
Also used : Moshi(com.squareup.moshi.Moshi) TranslationList(com.quran.labs.androidquran.dao.translation.TranslationList) BufferedSink(okio.BufferedSink) File(java.io.File)

Example 4 with TranslationList

use of com.quran.labs.androidquran.dao.translation.TranslationList in project quran_android by quran.

the class TranslationManagerPresenterTest method getRemoteTranslationListObservable.

@Test
public void getRemoteTranslationListObservable() throws Exception {
    MockResponse mockResponse = new MockResponse();
    File file = new File(CLI_ROOT_DIRECTORY, "translations.json");
    Buffer buffer = new Buffer();
    buffer.writeAll(Okio.source(file));
    mockResponse.setBody(buffer);
    this.mockWebServer.enqueue(mockResponse);
    TestObserver<TranslationList> testObserver = new TestObserver<>();
    this.translationManager.getRemoteTranslationListObservable().subscribe(testObserver);
    testObserver.awaitTerminalEvent();
    testObserver.assertValueCount(1);
    testObserver.assertNoErrors();
    TranslationList list = testObserver.values().get(0);
    assertThat(list.getTranslations()).hasSize(50);
}
Also used : Buffer(okio.Buffer) MockResponse(okhttp3.mockwebserver.MockResponse) TranslationList(com.quran.labs.androidquran.dao.translation.TranslationList) File(java.io.File) TestObserver(io.reactivex.observers.TestObserver) Test(org.junit.Test)

Example 5 with TranslationList

use of com.quran.labs.androidquran.dao.translation.TranslationList in project quran_android by quran.

the class TranslationManagerPresenterTest method setup.

@Before
public void setup() {
    Context mockAppContext = mock(Context.class);
    QuranSettings mockSettings = mock(QuranSettings.class);
    OkHttpClient mockOkHttp = new OkHttpClient.Builder().build();
    mockWebServer = new MockWebServer();
    translationManager = new TranslationManagerPresenter(mockAppContext, mockOkHttp, mockSettings, null, mock(QuranFileUtils.class)) {

        @Override
        void writeTranslationList(TranslationList list) {
        // no op
        }
    };
    translationManager.host = mockWebServer.url("").toString();
}
Also used : Context(android.content.Context) OkHttpClient(okhttp3.OkHttpClient) MockWebServer(okhttp3.mockwebserver.MockWebServer) TranslationList(com.quran.labs.androidquran.dao.translation.TranslationList) QuranSettings(com.quran.labs.androidquran.util.QuranSettings) Before(org.junit.Before)

Aggregations

TranslationList (com.quran.labs.androidquran.dao.translation.TranslationList)5 File (java.io.File)3 Moshi (com.squareup.moshi.Moshi)2 TestObserver (io.reactivex.observers.TestObserver)2 MockResponse (okhttp3.mockwebserver.MockResponse)2 Test (org.junit.Test)2 Context (android.content.Context)1 QuranSettings (com.quran.labs.androidquran.util.QuranSettings)1 OkHttpClient (okhttp3.OkHttpClient)1 MockWebServer (okhttp3.mockwebserver.MockWebServer)1 Buffer (okio.Buffer)1 BufferedSink (okio.BufferedSink)1 Before (org.junit.Before)1