@Test public void getAllCourses() { doAnswer(invocation -> { ((RemoteDataSource.LoadCoursesCallback) invocation.getArguments()[0]) .onAllCoursesReceived(courseResponses); return null; }).when(remote).getAllCourses(any(RemoteDataSource.LoadCoursesCallback.class)); List<CourseEntity> courseEntities = LiveDataTestUtil.getValue(academyRepository.getAllCourses()); verify(remote).getAllCourses(any(RemoteDataSource.LoadCoursesCallback.class)); assertNotNull(courseEntities); assertEquals(courseResponses.size(), courseEntities.size()); }
@Override public LiveData<Resource<List<CourseEntity>>> getAllCourses() { return new NetworkBoundResource<List<CourseEntity>, List<CourseResponse>>(appExecutors) { @Override public LiveData<List<CourseEntity>> loadFromDB() { return localDataSource.getAllCourses(); } @Override public Boolean shouldFetch(List<CourseEntity> data) { return (data == null) || (data.size() == 0); } @Override public LiveData<ApiResponse<List<CourseResponse>>> createCall() { return remoteDataSource.getAllCourses(); } @Override public void saveCallResult(List<CourseResponse> courseResponses) { ArrayList<CourseEntity> courseList = new ArrayList<>(); for (CourseResponse response : courseResponses) { CourseEntity course = new CourseEntity(response.getId(), response.getTitle(), response.getDescription(), response.getDate(), false, response.getImagePath()); courseList.add(course); } localDataSource.insertCourses(courseList); } }.asLiveData(); }
AcademyRepository implements AcademyDataSource { @Override public LiveData<Resource<List<CourseEntity>>> getAllCourses() { return new NetworkBoundResource<List<CourseEntity>, List<CourseResponse>>(appExecutors) { @Override public LiveData<List<CourseEntity>> loadFromDB() { return localDataSource.getAllCourses(); } @Override public Boolean shouldFetch(List<CourseEntity> data) { return (data == null) || (data.size() == 0); } @Override public LiveData<ApiResponse<List<CourseResponse>>> createCall() { return remoteDataSource.getAllCourses(); } @Override public void saveCallResult(List<CourseResponse> courseResponses) { ArrayList<CourseEntity> courseList = new ArrayList<>(); for (CourseResponse response : courseResponses) { CourseEntity course = new CourseEntity(response.getId(), response.getTitle(), response.getDescription(), response.getDate(), false, response.getImagePath()); courseList.add(course); } localDataSource.insertCourses(courseList); } }.asLiveData(); } }
AcademyRepository implements AcademyDataSource { @Override public LiveData<Resource<List<CourseEntity>>> getAllCourses() { return new NetworkBoundResource<List<CourseEntity>, List<CourseResponse>>(appExecutors) { @Override public LiveData<List<CourseEntity>> loadFromDB() { return localDataSource.getAllCourses(); } @Override public Boolean shouldFetch(List<CourseEntity> data) { return (data == null) || (data.size() == 0); } @Override public LiveData<ApiResponse<List<CourseResponse>>> createCall() { return remoteDataSource.getAllCourses(); } @Override public void saveCallResult(List<CourseResponse> courseResponses) { ArrayList<CourseEntity> courseList = new ArrayList<>(); for (CourseResponse response : courseResponses) { CourseEntity course = new CourseEntity(response.getId(), response.getTitle(), response.getDescription(), response.getDate(), false, response.getImagePath()); courseList.add(course); } localDataSource.insertCourses(courseList); } }.asLiveData(); } private  AcademyRepository(@NonNull RemoteDataSource remoteDataSource, @NonNull LocalDataSource localDataSource, AppExecutors appExecutors); }
AcademyRepository implements AcademyDataSource { @Override public LiveData<Resource<List<CourseEntity>>> getAllCourses() { return new NetworkBoundResource<List<CourseEntity>, List<CourseResponse>>(appExecutors) { @Override public LiveData<List<CourseEntity>> loadFromDB() { return localDataSource.getAllCourses(); } @Override public Boolean shouldFetch(List<CourseEntity> data) { return (data == null) || (data.size() == 0); } @Override public LiveData<ApiResponse<List<CourseResponse>>> createCall() { return remoteDataSource.getAllCourses(); } @Override public void saveCallResult(List<CourseResponse> courseResponses) { ArrayList<CourseEntity> courseList = new ArrayList<>(); for (CourseResponse response : courseResponses) { CourseEntity course = new CourseEntity(response.getId(), response.getTitle(), response.getDescription(), response.getDate(), false, response.getImagePath()); courseList.add(course); } localDataSource.insertCourses(courseList); } }.asLiveData(); } private  AcademyRepository(@NonNull RemoteDataSource remoteDataSource, @NonNull LocalDataSource localDataSource, AppExecutors appExecutors); static AcademyRepository getInstance(RemoteDataSource remoteData, LocalDataSource localDataSource, AppExecutors appExecutors); @Override LiveData<Resource<List<CourseEntity>>> getAllCourses(); @Override LiveData<List<CourseEntity>> getBookmarkedCourses(); @Override LiveData<Resource<CourseWithModule>> getCourseWithModules(final String courseId); @Override LiveData<Resource<List<ModuleEntity>>> getAllModulesByCourse(String courseId); @Override LiveData<Resource<ModuleEntity>> getContent(String moduleId); @Override void setCourseBookmark(CourseEntity course, boolean state); @Override void setReadModule(ModuleEntity module); }
AcademyRepository implements AcademyDataSource { @Override public LiveData<Resource<List<CourseEntity>>> getAllCourses() { return new NetworkBoundResource<List<CourseEntity>, List<CourseResponse>>(appExecutors) { @Override public LiveData<List<CourseEntity>> loadFromDB() { return localDataSource.getAllCourses(); } @Override public Boolean shouldFetch(List<CourseEntity> data) { return (data == null) || (data.size() == 0); } @Override public LiveData<ApiResponse<List<CourseResponse>>> createCall() { return remoteDataSource.getAllCourses(); } @Override public void saveCallResult(List<CourseResponse> courseResponses) { ArrayList<CourseEntity> courseList = new ArrayList<>(); for (CourseResponse response : courseResponses) { CourseEntity course = new CourseEntity(response.getId(), response.getTitle(), response.getDescription(), response.getDate(), false, response.getImagePath()); courseList.add(course); } localDataSource.insertCourses(courseList); } }.asLiveData(); } private  AcademyRepository(@NonNull RemoteDataSource remoteDataSource, @NonNull LocalDataSource localDataSource, AppExecutors appExecutors); static AcademyRepository getInstance(RemoteDataSource remoteData, LocalDataSource localDataSource, AppExecutors appExecutors); @Override LiveData<Resource<List<CourseEntity>>> getAllCourses(); @Override LiveData<List<CourseEntity>> getBookmarkedCourses(); @Override LiveData<Resource<CourseWithModule>> getCourseWithModules(final String courseId); @Override LiveData<Resource<List<ModuleEntity>>> getAllModulesByCourse(String courseId); @Override LiveData<Resource<ModuleEntity>> getContent(String moduleId); @Override void setCourseBookmark(CourseEntity course, boolean state); @Override void setReadModule(ModuleEntity module);  }