diff --git a/app/src/main/java/io/github/wulkanowy/data/db/dao/SemesterDao.kt b/app/src/main/java/io/github/wulkanowy/data/db/dao/SemesterDao.kt index 44de31d85..608a66630 100644 --- a/app/src/main/java/io/github/wulkanowy/data/db/dao/SemesterDao.kt +++ b/app/src/main/java/io/github/wulkanowy/data/db/dao/SemesterDao.kt @@ -1,8 +1,8 @@ package io.github.wulkanowy.data.db.dao import androidx.room.Dao +import androidx.room.Delete import androidx.room.Insert -import androidx.room.OnConflictStrategy.IGNORE import androidx.room.Query import io.github.wulkanowy.data.db.entities.Semester import io.reactivex.Maybe @@ -12,15 +12,12 @@ import javax.inject.Singleton @Dao interface SemesterDao { - @Insert(onConflict = IGNORE) + @Insert fun insertAll(semester: List) + @Delete + fun deleteAll(semester: List) + @Query("SELECT * FROM Semesters WHERE student_id = :studentId") fun loadAll(studentId: Int): Maybe> - - @Query("UPDATE Semesters SET is_current = 1 WHERE semester_id = :semesterId AND diary_id = :diaryId") - fun updateCurrent(semesterId: Int, diaryId: Int) - - @Query("UPDATE Semesters SET is_current = 0 WHERE student_id = :studentId") - fun resetCurrent(studentId: Int) } diff --git a/app/src/main/java/io/github/wulkanowy/data/repositories/semester/SemesterLocal.kt b/app/src/main/java/io/github/wulkanowy/data/repositories/semester/SemesterLocal.kt index 5be82d7f9..37d4a2687 100644 --- a/app/src/main/java/io/github/wulkanowy/data/repositories/semester/SemesterLocal.kt +++ b/app/src/main/java/io/github/wulkanowy/data/repositories/semester/SemesterLocal.kt @@ -11,17 +11,14 @@ import javax.inject.Singleton class SemesterLocal @Inject constructor(private val semesterDb: SemesterDao) { fun saveSemesters(semesters: List) { - return semesterDb.insertAll(semesters) + semesterDb.insertAll(semesters) + } + + fun deleteSemesters(semesters: List) { + semesterDb.deleteAll(semesters) } fun getSemesters(student: Student): Maybe> { return semesterDb.loadAll(student.studentId).filter { !it.isEmpty() } } - - fun setCurrentSemester(semester: Semester) { - semesterDb.run { - resetCurrent(semester.studentId) - updateCurrent(semester.semesterId, semester.diaryId) - } - } } diff --git a/app/src/main/java/io/github/wulkanowy/data/repositories/semester/SemesterRepository.kt b/app/src/main/java/io/github/wulkanowy/data/repositories/semester/SemesterRepository.kt index 975868a22..9735f0290 100644 --- a/app/src/main/java/io/github/wulkanowy/data/repositories/semester/SemesterRepository.kt +++ b/app/src/main/java/io/github/wulkanowy/data/repositories/semester/SemesterRepository.kt @@ -7,6 +7,7 @@ import io.github.wulkanowy.data.db.entities.Semester import io.github.wulkanowy.data.db.entities.Student import io.reactivex.Maybe import io.reactivex.Single +import timber.log.Timber import java.net.UnknownHostException import javax.inject.Inject import javax.inject.Singleton @@ -25,10 +26,17 @@ class SemesterRepository @Inject constructor( .switchIfEmpty(ReactiveNetwork.checkInternetConnectivity(settings) .flatMap { if (it) remote.getSemesters(student) else Single.error(UnknownHostException()) - }.map { newSemesters -> - local.apply { - saveSemesters(newSemesters) - setCurrentSemester(newSemesters.single { it.isCurrent }) + }.flatMap { new -> + val currentSemesters = new.filter { it.isCurrent } + if (currentSemesters.size == 1) { + local.getSemesters(student).toSingle(emptyList()) + .doOnSuccess { old -> + local.deleteSemesters(old - new) + local.saveSemesters(new - old) + } + } else { + Timber.i("Current semesters list:\n${currentSemesters.joinToString(separator = "\n")}") + throw IllegalArgumentException("Current semester can be only one.") } }.flatMap { local.getSemesters(student).toSingle(emptyList()) }) } diff --git a/app/src/test/java/io/github/wulkanowy/data/repositories/UnitTestInternetObservingStrategy.kt b/app/src/test/java/io/github/wulkanowy/data/repositories/UnitTestInternetObservingStrategy.kt new file mode 100644 index 000000000..28abbb993 --- /dev/null +++ b/app/src/test/java/io/github/wulkanowy/data/repositories/UnitTestInternetObservingStrategy.kt @@ -0,0 +1,19 @@ +package io.github.wulkanowy.data.repositories + +import com.github.pwittchen.reactivenetwork.library.rx2.internet.observing.InternetObservingStrategy +import com.github.pwittchen.reactivenetwork.library.rx2.internet.observing.error.ErrorHandler +import io.reactivex.Observable +import io.reactivex.Single + +class UnitTestInternetObservingStrategy : InternetObservingStrategy { + + override fun checkInternetConnectivity(host: String?, port: Int, timeoutInMs: Int, httpResponse: Int, errorHandler: ErrorHandler?): Single { + return Single.just(true) + } + + override fun observeInternetConnectivity(initialIntervalInMs: Int, intervalInMs: Int, host: String?, port: Int, timeoutInMs: Int, httpResponse: Int, errorHandler: ErrorHandler?): Observable { + return Observable.just(true) + } + + override fun getDefaultPingHost() = "localhost" +} diff --git a/app/src/test/java/io/github/wulkanowy/data/repositories/remote/AttendanceRemoteTest.kt b/app/src/test/java/io/github/wulkanowy/data/repositories/attendance/AttendanceRemoteTest.kt similarity index 92% rename from app/src/test/java/io/github/wulkanowy/data/repositories/remote/AttendanceRemoteTest.kt rename to app/src/test/java/io/github/wulkanowy/data/repositories/attendance/AttendanceRemoteTest.kt index 4842d305c..8adfa83bd 100644 --- a/app/src/test/java/io/github/wulkanowy/data/repositories/remote/AttendanceRemoteTest.kt +++ b/app/src/test/java/io/github/wulkanowy/data/repositories/attendance/AttendanceRemoteTest.kt @@ -1,9 +1,8 @@ -package io.github.wulkanowy.data.repositories.remote +package io.github.wulkanowy.data.repositories.attendance import io.github.wulkanowy.api.Api import io.github.wulkanowy.api.attendance.Attendance import io.github.wulkanowy.data.db.entities.Semester -import io.github.wulkanowy.data.repositories.attendance.AttendanceRemote import io.mockk.MockKAnnotations import io.mockk.every import io.mockk.impl.annotations.MockK diff --git a/app/src/test/java/io/github/wulkanowy/data/repositories/remote/CompletedLessonsRemoteTest.kt b/app/src/test/java/io/github/wulkanowy/data/repositories/completedlessons/CompletedLessonsRemoteTest.kt similarity index 91% rename from app/src/test/java/io/github/wulkanowy/data/repositories/remote/CompletedLessonsRemoteTest.kt rename to app/src/test/java/io/github/wulkanowy/data/repositories/completedlessons/CompletedLessonsRemoteTest.kt index f4c161bc4..bb9f52945 100644 --- a/app/src/test/java/io/github/wulkanowy/data/repositories/remote/CompletedLessonsRemoteTest.kt +++ b/app/src/test/java/io/github/wulkanowy/data/repositories/completedlessons/CompletedLessonsRemoteTest.kt @@ -1,9 +1,8 @@ -package io.github.wulkanowy.data.repositories.remote +package io.github.wulkanowy.data.repositories.completedlessons import io.github.wulkanowy.api.Api import io.github.wulkanowy.api.timetable.CompletedLesson import io.github.wulkanowy.data.db.entities.Semester -import io.github.wulkanowy.data.repositories.completedlessons.CompletedLessonsRemote import io.mockk.MockKAnnotations import io.mockk.every import io.mockk.impl.annotations.MockK diff --git a/app/src/test/java/io/github/wulkanowy/data/repositories/remote/ExamRemoteTest.kt b/app/src/test/java/io/github/wulkanowy/data/repositories/exam/ExamRemoteTest.kt similarity index 93% rename from app/src/test/java/io/github/wulkanowy/data/repositories/remote/ExamRemoteTest.kt rename to app/src/test/java/io/github/wulkanowy/data/repositories/exam/ExamRemoteTest.kt index 0cc785482..0c83bed0a 100644 --- a/app/src/test/java/io/github/wulkanowy/data/repositories/remote/ExamRemoteTest.kt +++ b/app/src/test/java/io/github/wulkanowy/data/repositories/exam/ExamRemoteTest.kt @@ -1,9 +1,8 @@ -package io.github.wulkanowy.data.repositories.remote +package io.github.wulkanowy.data.repositories.exam import io.github.wulkanowy.api.Api import io.github.wulkanowy.api.exams.Exam import io.github.wulkanowy.data.db.entities.Semester -import io.github.wulkanowy.data.repositories.exam.ExamRemote import io.mockk.MockKAnnotations import io.mockk.every import io.mockk.impl.annotations.MockK diff --git a/app/src/test/java/io/github/wulkanowy/data/repositories/remote/GradeStatisticsRemoteTest.kt b/app/src/test/java/io/github/wulkanowy/data/repositories/gradestatistics/GradeStatisticsRemoteTest.kt similarity index 91% rename from app/src/test/java/io/github/wulkanowy/data/repositories/remote/GradeStatisticsRemoteTest.kt rename to app/src/test/java/io/github/wulkanowy/data/repositories/gradestatistics/GradeStatisticsRemoteTest.kt index 14381791d..37d3f2ad6 100644 --- a/app/src/test/java/io/github/wulkanowy/data/repositories/remote/GradeStatisticsRemoteTest.kt +++ b/app/src/test/java/io/github/wulkanowy/data/repositories/gradestatistics/GradeStatisticsRemoteTest.kt @@ -1,9 +1,8 @@ -package io.github.wulkanowy.data.repositories.remote +package io.github.wulkanowy.data.repositories.gradestatistics import io.github.wulkanowy.api.Api import io.github.wulkanowy.api.grades.GradeStatistics import io.github.wulkanowy.data.db.entities.Semester -import io.github.wulkanowy.data.repositories.gradestatistics.GradeStatisticsRemote import io.mockk.MockKAnnotations import io.mockk.every import io.mockk.impl.annotations.MockK diff --git a/app/src/test/java/io/github/wulkanowy/data/repositories/remote/LuckyNumberRemoteTest.kt b/app/src/test/java/io/github/wulkanowy/data/repositories/luckynumber/LuckyNumberRemoteTest.kt similarity index 95% rename from app/src/test/java/io/github/wulkanowy/data/repositories/remote/LuckyNumberRemoteTest.kt rename to app/src/test/java/io/github/wulkanowy/data/repositories/luckynumber/LuckyNumberRemoteTest.kt index 054f8890d..67e6c2ef6 100644 --- a/app/src/test/java/io/github/wulkanowy/data/repositories/remote/LuckyNumberRemoteTest.kt +++ b/app/src/test/java/io/github/wulkanowy/data/repositories/luckynumber/LuckyNumberRemoteTest.kt @@ -1,4 +1,4 @@ -package io.github.wulkanowy.data.repositories.remote +package io.github.wulkanowy.data.repositories.luckynumber import io.github.wulkanowy.api.Api import io.github.wulkanowy.data.db.entities.Semester diff --git a/app/src/test/java/io/github/wulkanowy/data/repositories/semester/SemesterRepositoryTest.kt b/app/src/test/java/io/github/wulkanowy/data/repositories/semester/SemesterRepositoryTest.kt new file mode 100644 index 000000000..7856b9370 --- /dev/null +++ b/app/src/test/java/io/github/wulkanowy/data/repositories/semester/SemesterRepositoryTest.kt @@ -0,0 +1,74 @@ +package io.github.wulkanowy.data.repositories.semester + +import com.github.pwittchen.reactivenetwork.library.rx2.internet.observing.InternetObservingSettings +import io.github.wulkanowy.data.ApiHelper +import io.github.wulkanowy.data.db.entities.Semester +import io.github.wulkanowy.data.db.entities.Student +import io.github.wulkanowy.data.repositories.UnitTestInternetObservingStrategy +import io.reactivex.Maybe +import io.reactivex.Single +import org.junit.Before +import org.junit.Test +import org.mockito.Mock +import org.mockito.Mockito.doNothing +import org.mockito.Mockito.doReturn +import org.mockito.Mockito.verify +import org.mockito.MockitoAnnotations + +class SemesterRepositoryTest { + + @Mock + private lateinit var semesterRemote: SemesterRemote + + @Mock + private lateinit var semesterLocal: SemesterLocal + + @Mock + private lateinit var apiHelper: ApiHelper + + @Mock + private lateinit var student: Student + + private lateinit var semesterRepository: SemesterRepository + + private val settings = InternetObservingSettings.builder() + .strategy(UnitTestInternetObservingStrategy()) + .build() + + @Before + fun initTest() { + MockitoAnnotations.initMocks(this) + semesterRepository = SemesterRepository(semesterRemote, semesterLocal, settings, apiHelper) + } + + @Test + fun singleCurrentSemesterTest() { + val semesters = listOf( + createSemesterEntity(false), + createSemesterEntity(true) + ) + + doNothing().`when`(apiHelper).initApi(student) + doReturn(Maybe.empty()).`when`(semesterLocal).getSemesters(student) + doReturn(Single.just(semesters)).`when`(semesterRemote).getSemesters(student) + + semesterRepository.getSemesters(student).blockingGet() + + verify(semesterLocal).deleteSemesters(emptyList()) + verify(semesterLocal).saveSemesters(semesters) + } + + @Test(expected = IllegalArgumentException::class) + fun twoCurrentSemesterTest() { + val semesters = listOf( + createSemesterEntity(true), + createSemesterEntity(true) + ) + + doNothing().`when`(apiHelper).initApi(student) + doReturn(Maybe.empty()).`when`(semesterLocal).getSemesters(student) + doReturn(Single.just(semesters)).`when`(semesterRemote).getSemesters(student) + + semesterRepository.getSemesters(student).blockingGet() + } +} diff --git a/app/src/test/java/io/github/wulkanowy/data/repositories/semester/TestSemesterEnityCreator.kt b/app/src/test/java/io/github/wulkanowy/data/repositories/semester/TestSemesterEnityCreator.kt new file mode 100644 index 000000000..c3377ac51 --- /dev/null +++ b/app/src/test/java/io/github/wulkanowy/data/repositories/semester/TestSemesterEnityCreator.kt @@ -0,0 +1,16 @@ +package io.github.wulkanowy.data.repositories.semester + +import io.github.wulkanowy.data.db.entities.Semester + +fun createSemesterEntity(current: Boolean): Semester { + return Semester( + studentId = 0, + diaryId = 0, + semesterId = 0, + diaryName = "", + classId = 0, + isCurrent = current, + semesterName = 0, + unitId = 0 + ) +} diff --git a/app/src/test/java/io/github/wulkanowy/data/repositories/remote/StudentRemoteTest.kt b/app/src/test/java/io/github/wulkanowy/data/repositories/student/StudentRemoteTest.kt similarity index 87% rename from app/src/test/java/io/github/wulkanowy/data/repositories/remote/StudentRemoteTest.kt rename to app/src/test/java/io/github/wulkanowy/data/repositories/student/StudentRemoteTest.kt index 3a26836de..1244239e8 100644 --- a/app/src/test/java/io/github/wulkanowy/data/repositories/remote/StudentRemoteTest.kt +++ b/app/src/test/java/io/github/wulkanowy/data/repositories/student/StudentRemoteTest.kt @@ -1,8 +1,7 @@ -package io.github.wulkanowy.data.repositories.remote +package io.github.wulkanowy.data.repositories.student import io.github.wulkanowy.api.Api import io.github.wulkanowy.api.register.Student -import io.github.wulkanowy.data.repositories.student.StudentRemote import io.reactivex.Single import org.junit.Assert.assertEquals import org.junit.Before diff --git a/app/src/test/java/io/github/wulkanowy/data/repositories/remote/TimetableRemoteTest.kt b/app/src/test/java/io/github/wulkanowy/data/repositories/timetable/TimetableRemoteTest.kt similarity index 96% rename from app/src/test/java/io/github/wulkanowy/data/repositories/remote/TimetableRemoteTest.kt rename to app/src/test/java/io/github/wulkanowy/data/repositories/timetable/TimetableRemoteTest.kt index 942b28ed9..4bb4091fb 100644 --- a/app/src/test/java/io/github/wulkanowy/data/repositories/remote/TimetableRemoteTest.kt +++ b/app/src/test/java/io/github/wulkanowy/data/repositories/timetable/TimetableRemoteTest.kt @@ -1,4 +1,4 @@ -package io.github.wulkanowy.data.repositories.remote +package io.github.wulkanowy.data.repositories.timetable import io.github.wulkanowy.api.Api import io.github.wulkanowy.api.timetable.Timetable