@Test public void getCourseWithModules() { MutableLiveData<CourseWithModule> dummyEntity = new MutableLiveData<>(); dummyEntity.setValue(DataDummy.generateDummyCourseWithModules(DataDummy.generateDummyCourses().get(0), false)); when(local.getCourseWithModules(courseId)).thenReturn(dummyEntity); Resource<CourseWithModule> courseEntities = LiveDataTestUtil.getValue(academyRepository.getCourseWithModules(courseId)); verify(local).getCourseWithModules(courseId); assertNotNull(courseEntities.data); assertNotNull(courseEntities.data.mCourse.getTitle()); assertEquals(courseResponses.get(0).getTitle(), courseEntities.data.mCourse.getTitle()); }
@Override public LiveData<Resource<CourseWithModule>> getCourseWithModules(final String courseId) { return new NetworkBoundResource<CourseWithModule, List<ModuleResponse>>(appExecutors) { @Override protected LiveData<CourseWithModule> loadFromDB() { return localDataSource.getCourseWithModules(courseId); } @Override protected Boolean shouldFetch(CourseWithModule courseWithModule) { return (courseWithModule == null || courseWithModule.mModules == null) || (courseWithModule.mModules.size() == 0); } @Override protected LiveData<ApiResponse<List<ModuleResponse>>> createCall() { return remoteDataSource.getModules(courseId); } @Override protected void saveCallResult(List<ModuleResponse> moduleResponses) { ArrayList<ModuleEntity> moduleList = new ArrayList<>(); for (ModuleResponse response : moduleResponses) { ModuleEntity course = new ModuleEntity(response.getModuleId(), response.getCourseId(), response.getTitle(), response.getPosition(), false); moduleList.add(course); } localDataSource.insertModules(moduleList); } }.asLiveData(); }
AcademyRepository implements AcademyDataSource { @Override public LiveData<Resource<CourseWithModule>> getCourseWithModules(final String courseId) { return new NetworkBoundResource<CourseWithModule, List<ModuleResponse>>(appExecutors) { @Override protected LiveData<CourseWithModule> loadFromDB() { return localDataSource.getCourseWithModules(courseId); } @Override protected Boolean shouldFetch(CourseWithModule courseWithModule) { return (courseWithModule == null || courseWithModule.mModules == null) || (courseWithModule.mModules.size() == 0); } @Override protected LiveData<ApiResponse<List<ModuleResponse>>> createCall() { return remoteDataSource.getModules(courseId); } @Override protected void saveCallResult(List<ModuleResponse> moduleResponses) { ArrayList<ModuleEntity> moduleList = new ArrayList<>(); for (ModuleResponse response : moduleResponses) { ModuleEntity course = new ModuleEntity(response.getModuleId(), response.getCourseId(), response.getTitle(), response.getPosition(), false); moduleList.add(course); } localDataSource.insertModules(moduleList); } }.asLiveData(); } }
AcademyRepository implements AcademyDataSource { @Override public LiveData<Resource<CourseWithModule>> getCourseWithModules(final String courseId) { return new NetworkBoundResource<CourseWithModule, List<ModuleResponse>>(appExecutors) { @Override protected LiveData<CourseWithModule> loadFromDB() { return localDataSource.getCourseWithModules(courseId); } @Override protected Boolean shouldFetch(CourseWithModule courseWithModule) { return (courseWithModule == null || courseWithModule.mModules == null) || (courseWithModule.mModules.size() == 0); } @Override protected LiveData<ApiResponse<List<ModuleResponse>>> createCall() { return remoteDataSource.getModules(courseId); } @Override protected void saveCallResult(List<ModuleResponse> moduleResponses) { ArrayList<ModuleEntity> moduleList = new ArrayList<>(); for (ModuleResponse response : moduleResponses) { ModuleEntity course = new ModuleEntity(response.getModuleId(), response.getCourseId(), response.getTitle(), response.getPosition(), false); moduleList.add(course); } localDataSource.insertModules(moduleList); } }.asLiveData(); } private  AcademyRepository(@NonNull RemoteDataSource remoteDataSource, @NonNull LocalDataSource localDataSource, AppExecutors appExecutors); }
AcademyRepository implements AcademyDataSource { @Override public LiveData<Resource<CourseWithModule>> getCourseWithModules(final String courseId) { return new NetworkBoundResource<CourseWithModule, List<ModuleResponse>>(appExecutors) { @Override protected LiveData<CourseWithModule> loadFromDB() { return localDataSource.getCourseWithModules(courseId); } @Override protected Boolean shouldFetch(CourseWithModule courseWithModule) { return (courseWithModule == null || courseWithModule.mModules == null) || (courseWithModule.mModules.size() == 0); } @Override protected LiveData<ApiResponse<List<ModuleResponse>>> createCall() { return remoteDataSource.getModules(courseId); } @Override protected void saveCallResult(List<ModuleResponse> moduleResponses) { ArrayList<ModuleEntity> moduleList = new ArrayList<>(); for (ModuleResponse response : moduleResponses) { ModuleEntity course = new ModuleEntity(response.getModuleId(), response.getCourseId(), response.getTitle(), response.getPosition(), false); moduleList.add(course); } localDataSource.insertModules(moduleList); } }.asLiveData(); } private  AcademyRepository(@NonNull RemoteDataSource remoteDataSource, @NonNull LocalDataSource localDataSource, AppExecutors appExecutors); static AcademyRepository getInstance(RemoteDataSource remoteData, LocalDataSource localDataSource, AppExecutors appExecutors); @Override LiveData<Resource<PagedList<CourseEntity>>> getAllCourses(); @Override LiveData<PagedList<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<CourseWithModule>> getCourseWithModules(final String courseId) { return new NetworkBoundResource<CourseWithModule, List<ModuleResponse>>(appExecutors) { @Override protected LiveData<CourseWithModule> loadFromDB() { return localDataSource.getCourseWithModules(courseId); } @Override protected Boolean shouldFetch(CourseWithModule courseWithModule) { return (courseWithModule == null || courseWithModule.mModules == null) || (courseWithModule.mModules.size() == 0); } @Override protected LiveData<ApiResponse<List<ModuleResponse>>> createCall() { return remoteDataSource.getModules(courseId); } @Override protected void saveCallResult(List<ModuleResponse> moduleResponses) { ArrayList<ModuleEntity> moduleList = new ArrayList<>(); for (ModuleResponse response : moduleResponses) { ModuleEntity course = new ModuleEntity(response.getModuleId(), response.getCourseId(), response.getTitle(), response.getPosition(), false); moduleList.add(course); } localDataSource.insertModules(moduleList); } }.asLiveData(); } private  AcademyRepository(@NonNull RemoteDataSource remoteDataSource, @NonNull LocalDataSource localDataSource, AppExecutors appExecutors); static AcademyRepository getInstance(RemoteDataSource remoteData, LocalDataSource localDataSource, AppExecutors appExecutors); @Override LiveData<Resource<PagedList<CourseEntity>>> getAllCourses(); @Override LiveData<PagedList<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);  }