Search in sources :

Example 6 with Resource

use of in.bugzy.data.model.Resource in project bugzy by cpunq.

the class NetworkBoundResourceTest method dbSuccessWithFetch.

@Test
public void dbSuccessWithFetch() {
    Foo dbFoo = new Foo(1);
    AtomicBoolean saved = new AtomicBoolean();
    shouldFetch = (foo) -> foo == dbFoo;
    saveCallResult = foo -> {
        saved.set(true);
        return null;
    };
    Observer<Resource<Foo>> observer = Mockito.mock(Observer.class);
    networkBoundResource.asLiveData().observeForever(observer);
    MutableLiveData<ApiResponse<Foo>> apiResponseLiveData = new MutableLiveData();
    createCall = (aVoid) -> apiResponseLiveData;
    drain();
    verify(observer).onChanged(Resource.loading(null));
    reset(observer);
    dbData.setValue(dbFoo);
    drain();
    verify(observer).onChanged(Resource.loading(dbFoo));
    ResponseBody body = ResponseBody.create(MediaType.parse("text/html"), "error");
    apiResponseLiveData.setValue(new ApiResponse<>(Response.error(400, body), mGson));
    drain();
    verify(observer).onChanged(Resource.error("Oops! We can't reach Fogbugz", dbFoo));
    Foo dbFoo2 = new Foo(2);
    dbData.setValue(dbFoo2);
    drain();
    verify(observer).onChanged(Resource.error("Oops! We can't reach Fogbugz", dbFoo2));
    verifyNoMoreInteractions(observer);
}
Also used : AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) Resource(in.bugzy.data.model.Resource) MutableLiveData(android.arch.lifecycle.MutableLiveData) ResponseBody(okhttp3.ResponseBody) Test(org.junit.Test)

Aggregations

Resource (in.bugzy.data.model.Resource)6 Test (org.junit.Test)5 ResponseBody (okhttp3.ResponseBody)4 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)3 Error (in.bugzy.data.remote.model.Error)2 Objects (java.util.Objects)2 MutableLiveData (android.arch.lifecycle.MutableLiveData)1 NonNull (android.support.annotation.NonNull)1 SearchResultsResource (in.bugzy.data.model.SearchResultsResource)1 ApiResponse (in.bugzy.data.remote.ApiResponse)1 NetworkBoundResource (in.bugzy.data.remote.NetworkBoundResource)1 NetworkBoundTask (in.bugzy.data.remote.NetworkBoundTask)1 ListCasesData (in.bugzy.data.remote.model.ListCasesData)1 Response (in.bugzy.data.remote.model.Response)1 SearchCasesRequest (in.bugzy.data.remote.model.SearchCasesRequest)1