@Test public void getAllModulesByCourse() { MutableLiveData<List<ModuleEntity>> dummyModules = new MutableLiveData<>(); dummyModules.setValue(DataDummy.generateDummyModules(courseId)); when(local.getAllModulesByCourse(courseId)).thenReturn(dummyModules); Resource<List<ModuleEntity>> courseEntities = LiveDataTestUtil.getValue(academyRepository.getAllModulesByCourse(courseId)); verify(local).getAllModulesByCourse(courseId); assertNotNull(courseEntities.data); assertEquals(moduleResponses.size(), courseEntities.data.size()); }
@Override public LiveData<Resource<List<ModuleEntity>>> getAllModulesByCourse(String courseId) { return new NetworkBoundResource<List<ModuleEntity>, List<ModuleResponse>>(appExecutors) { @Override protected LiveData<List<ModuleEntity>> loadFromDB() { return localDataSource.getAllModulesByCourse(courseId); } @Override protected Boolean shouldFetch(List<ModuleEntity> modules) { return (modules == null) || (modules.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<List<ModuleEntity>>> getAllModulesByCourse(String courseId) { return new NetworkBoundResource<List<ModuleEntity>, List<ModuleResponse>>(appExecutors) { @Override protected LiveData<List<ModuleEntity>> loadFromDB() { return localDataSource.getAllModulesByCourse(courseId); } @Override protected Boolean shouldFetch(List<ModuleEntity> modules) { return (modules == null) || (modules.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<List<ModuleEntity>>> getAllModulesByCourse(String courseId) { return new NetworkBoundResource<List<ModuleEntity>, List<ModuleResponse>>(appExecutors) { @Override protected LiveData<List<ModuleEntity>> loadFromDB() { return localDataSource.getAllModulesByCourse(courseId); } @Override protected Boolean shouldFetch(List<ModuleEntity> modules) { return (modules == null) || (modules.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<List<ModuleEntity>>> getAllModulesByCourse(String courseId) { return new NetworkBoundResource<List<ModuleEntity>, List<ModuleResponse>>(appExecutors) { @Override protected LiveData<List<ModuleEntity>> loadFromDB() { return localDataSource.getAllModulesByCourse(courseId); } @Override protected Boolean shouldFetch(List<ModuleEntity> modules) { return (modules == null) || (modules.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<List<ModuleEntity>>> getAllModulesByCourse(String courseId) { return new NetworkBoundResource<List<ModuleEntity>, List<ModuleResponse>>(appExecutors) { @Override protected LiveData<List<ModuleEntity>> loadFromDB() { return localDataSource.getAllModulesByCourse(courseId); } @Override protected Boolean shouldFetch(List<ModuleEntity> modules) { return (modules == null) || (modules.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);  }