use of in.bugzy.data.model.Resource in project bugzy by cpunq.
the class NetworkBoundTaskTest method failureFromNetwork.
@Test
public void failureFromNetwork() {
ResponseBody body = ResponseBody.create(MediaType.parse("text/html"), "error");
createCall = (aVoid) -> Calls.response(Response.error(400, body));
Observer<Resource<Foo>> observer = Mockito.mock(Observer.class);
mNetworkBoundTask.asLiveData().observeForever(observer);
// Execute the task
appExecutors.networkIO().execute(mNetworkBoundTask);
verify(observer).onChanged(Resource.loading(null));
verify(observer).onChanged(Resource.error("Oops! We can't reach Fogbugz", null));
}
use of in.bugzy.data.model.Resource in project bugzy by cpunq.
the class NetworkBoundTaskTest method failureFromFogbugz.
@Test
public void failureFromFogbugz() {
Error k = new Error("fook", null, "11");
String content = "{\"errors\"=[" + mGson.toJson(k) + "]}";
ResponseBody body = ResponseBody.create(MediaType.parse("application/json"), content);
createCall = (aVoid) -> Calls.response(Response.error(500, body));
Observer<Resource<Foo>> observer = Mockito.mock(Observer.class);
mNetworkBoundTask.asLiveData().observeForever(observer);
// Execute the task
appExecutors.networkIO().execute(mNetworkBoundTask);
verify(observer).onChanged(Resource.loading(null));
verify(observer).onChanged(Resource.error("fook", null));
}
use of in.bugzy.data.model.Resource in project bugzy by cpunq.
the class CasesRepository method searchCases.
public LiveData<SearchResultsResource<ListCasesData>> searchCases(final String query) {
this.recordResearch(query);
NetworkBoundTask<Response<ListCasesData>> task = new NetworkBoundTask<Response<ListCasesData>>(mAppExecutors, mGson) {
@Override
public void saveCallResult(@NonNull Response<ListCasesData> result) {
}
@NonNull
@Override
protected Call<Response<ListCasesData>> createCall() {
return mApiService.searchCasesCall(new SearchCasesRequest(mColsForCaseList, query));
}
};
mAppExecutors.networkIO().execute(task);
return Transformations.map(task.asLiveData(), v -> {
Resource<ListCasesData> resource = new Resource(v.status, v.data == null ? null : v.data.getData(), v.message);
return new SearchResultsResource<>(query, resource);
});
// return Transformations.map(new NetworkBoundResource<List<Case>, Response<ListCasesData>>(mAppExecutors) {
// List<Case> mCases = null;
//
// @Override
// protected void saveCallResult(@NonNull Response<ListCasesData> item) {
// mCases = item.getData().getCases();
// }
//
// @Override
// protected boolean shouldFetch(@Nullable List<Case> data) {
// // Fetch always
// return true;
// }
//
// @NonNull
// @Override
// protected LiveData<List<Case>> loadFromDb() {
// return new LiveData<List<Case>>() {
// @Override
// protected void onActive() {
// super.onActive();
// setValue(mCases);
// }
// };
// }
//
// @NonNull
// @Override
// protected LiveData<ApiResponse<Response<ListCasesData>>> createCall() {
// return mApiService.searchCases(new SearchCasesRequest(mColsForCaseList, query));
// }
// }.asLiveData(), v -> {
// return new SearchResultsResource<>(query, v);
// });
}
use of in.bugzy.data.model.Resource in project bugzy by cpunq.
the class NetworkBoundResourceTest method dbSuccessWithoutNetwork.
@Test
public void dbSuccessWithoutNetwork() {
AtomicBoolean saved = new AtomicBoolean();
shouldFetch = Objects::isNull;
saveCallResult = foo -> {
saved.set(true);
return null;
};
Observer<Resource<Foo>> observer = Mockito.mock(Observer.class);
networkBoundResource.asLiveData().observeForever(observer);
drain();
verify(observer).onChanged(Resource.loading(null));
reset(observer);
Foo dbFoo = new Foo(1);
dbData.setValue(dbFoo);
drain();
verify(observer).onChanged(Resource.success(dbFoo));
assertThat(saved.get(), is(false));
Foo dbFoo2 = new Foo(2);
dbData.setValue(dbFoo2);
drain();
verify(observer).onChanged(Resource.success(dbFoo2));
verifyNoMoreInteractions(observer);
}
use of in.bugzy.data.model.Resource in project bugzy by cpunq.
the class NetworkBoundResourceTest method failureFromNetwork.
@Test
public void failureFromNetwork() {
AtomicBoolean saved = new AtomicBoolean();
shouldFetch = Objects::isNull;
Foo fetchedDbValue = new Foo(1);
saveCallResult = foo -> {
saved.set(true);
dbData.setValue(fetchedDbValue);
return null;
};
Error k = new Error("foo", null, "11");
String content = "{\"errors\"=[" + mGson.toJson(k) + "]}";
ResponseBody body = ResponseBody.create(MediaType.parse("application/json"), content);
createCall = (aVoid) -> ApiUtil.createCall(Response.error(500, body));
Observer<Resource<Foo>> observer = Mockito.mock(Observer.class);
networkBoundResource.asLiveData().observeForever(observer);
// Drain always before verifying
drain();
verify(observer).onChanged(Resource.loading(null));
reset(observer);
dbData.setValue(null);
drain();
assertThat(saved.get(), is(false));
verify(observer).onChanged(Resource.error("foo", null));
}
Aggregations