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 dd702b3e..68946bee 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 @@ -4,6 +4,7 @@ import com.github.pwittchen.reactivenetwork.library.rx2.ReactiveNetwork import com.github.pwittchen.reactivenetwork.library.rx2.internet.observing.InternetObservingSettings import io.github.wulkanowy.data.db.entities.Semester import io.github.wulkanowy.data.db.entities.Student +import io.github.wulkanowy.sdk.Sdk import io.github.wulkanowy.utils.getCurrentOrLast import io.github.wulkanowy.utils.isCurrent import io.github.wulkanowy.utils.uniqueSubtract @@ -20,10 +21,12 @@ class SemesterRepository @Inject constructor( ) { fun getSemesters(student: Student, forceRefresh: Boolean = false, refreshOnNoCurrent: Boolean = false): Single> { - return local.getSemesters(student).filter { !forceRefresh }.filter { - if (refreshOnNoCurrent) { - it.any { semester -> semester.isCurrent } - } else true + return local.getSemesters(student).filter { !forceRefresh }.filter { semesters -> + when { + Sdk.Mode.valueOf(student.loginMode) != Sdk.Mode.API -> semesters.firstOrNull { it.isCurrent }?.diaryId != 0 + refreshOnNoCurrent -> semesters.any { semester -> semester.isCurrent } + else -> true + } }.switchIfEmpty(ReactiveNetwork.checkInternetConnectivity(settings) .flatMap { if (it) remote.getSemesters(student) diff --git a/app/src/main/java/io/github/wulkanowy/data/repositories/student/StudentLocal.kt b/app/src/main/java/io/github/wulkanowy/data/repositories/student/StudentLocal.kt index c7a46e9d..06b9bb4a 100644 --- a/app/src/main/java/io/github/wulkanowy/data/repositories/student/StudentLocal.kt +++ b/app/src/main/java/io/github/wulkanowy/data/repositories/student/StudentLocal.kt @@ -29,7 +29,7 @@ class StudentLocal @Inject constructor( fun getStudents(decryptPass: Boolean): Maybe> { return studentDb.loadAll() - .map { list -> list.map { it.apply { if (decryptPass) password = decrypt(password) } } } + .map { list -> list.map { it.apply { if (decryptPass && Sdk.Mode.valueOf(loginMode) != Sdk.Mode.API) password = decrypt(password) } } } .filter { it.isNotEmpty() } } 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 index af872c57..58c94ab6 100644 --- 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 @@ -8,6 +8,7 @@ import io.github.wulkanowy.data.repositories.createSemesterEntity import io.reactivex.Maybe import io.reactivex.Single import org.junit.Assert.assertEquals +import org.junit.Assert.assertNotEquals import org.junit.Before import org.junit.Test import org.mockito.Mock @@ -37,13 +38,14 @@ class SemesterRepositoryTest { fun initTest() { MockitoAnnotations.initMocks(this) semesterRepository = SemesterRepository(semesterRemote, semesterLocal, settings) + doReturn("SCRAPPER").`when`(student).loginMode } @Test fun getSemesters_noSemesters() { val semesters = listOf( - createSemesterEntity(0, 0, now().minusMonths(6), now().minusMonths(3)), - createSemesterEntity(0, 0, now().minusMonths(3), now()) + createSemesterEntity(1, 1, now().minusMonths(6), now().minusMonths(3)), + createSemesterEntity(1, 2, now().minusMonths(3), now()) ) doReturn(Maybe.empty()).`when`(semesterLocal).getSemesters(student) @@ -55,11 +57,47 @@ class SemesterRepositoryTest { verify(semesterLocal).saveSemesters(semesters) } + @Test + fun getSemesters_invalidDiary_api() { + doReturn("API").`when`(student).loginMode + val badSemesters = listOf( + createSemesterEntity(0, 1, now().minusMonths(6), now().minusMonths(3)), + createSemesterEntity(0, 2, now().minusMonths(3), now()) + ) + + doReturn(Maybe.just(badSemesters)).`when`(semesterLocal).getSemesters(student) + + val items = semesterRepository.getSemesters(student).blockingGet() + assertEquals(2, items.size) + assertEquals(0, items[0].diaryId) + } + + @Test + fun getSemesters_invalidDiary_scrapper() { + doReturn("SCRAPPER").`when`(student).loginMode + val badSemesters = listOf( + createSemesterEntity(0, 1, now().minusMonths(6), now().minusMonths(3)), + createSemesterEntity(0, 2, now().minusMonths(3), now()) + ) + + val goodSemesters = listOf( + createSemesterEntity(1, 1, now().minusMonths(6), now().minusMonths(3)), + createSemesterEntity(1, 2, now().minusMonths(3), now()) + ) + + doReturn(Maybe.just(badSemesters), Maybe.just(badSemesters), Maybe.just(goodSemesters)).`when`(semesterLocal).getSemesters(student) + doReturn(Single.just(goodSemesters)).`when`(semesterRemote).getSemesters(student) + + val items = semesterRepository.getSemesters(student).blockingGet() + assertEquals(2, items.size) + assertNotEquals(0, items[0].diaryId) + } + @Test fun getSemesters_noCurrent() { val semesters = listOf( - createSemesterEntity(0, 0, now().minusMonths(12), now().minusMonths(6)), - createSemesterEntity(0, 0, now().minusMonths(6), now().minusMonths(1)) + createSemesterEntity(1, 1, now().minusMonths(12), now().minusMonths(6)), + createSemesterEntity(1, 2, now().minusMonths(6), now().minusMonths(1)) ) doReturn(Maybe.just(semesters)).`when`(semesterLocal).getSemesters(student) @@ -71,8 +109,8 @@ class SemesterRepositoryTest { @Test fun getSemesters_oneCurrent() { val semesters = listOf( - createSemesterEntity(0, 0, now().minusMonths(6), now().minusMonths(3)), - createSemesterEntity(0, 0, now().minusMonths(3), now()) + createSemesterEntity(1, 1, now().minusMonths(6), now().minusMonths(3)), + createSemesterEntity(1, 2, now().minusMonths(3), now()) ) doReturn(Maybe.just(semesters)).`when`(semesterLocal).getSemesters(student) @@ -84,8 +122,8 @@ class SemesterRepositoryTest { @Test fun getSemesters_doubleCurrent() { val semesters = listOf( - createSemesterEntity(0, 0, now(), now()), - createSemesterEntity(0, 0, now(), now()) + createSemesterEntity(1, 1, now(), now()), + createSemesterEntity(1, 2, now(), now()) ) doReturn(Maybe.just(semesters)).`when`(semesterLocal).getSemesters(student) @@ -97,8 +135,8 @@ class SemesterRepositoryTest { @Test fun getSemesters_noSemesters_refreshOnNoCurrent() { val semesters = listOf( - createSemesterEntity(0, 0, now().minusMonths(6), now().minusMonths(3)), - createSemesterEntity(0, 0, now().minusMonths(3), now()) + createSemesterEntity(1, 1, now().minusMonths(6), now().minusMonths(3)), + createSemesterEntity(1, 2, now().minusMonths(3), now()) ) doReturn(Maybe.empty()).`when`(semesterLocal).getSemesters(student) @@ -113,8 +151,8 @@ class SemesterRepositoryTest { @Test fun getSemesters_noCurrent_refreshOnNoCurrent() { val semesters = listOf( - createSemesterEntity(0, 0, now().minusMonths(12), now().minusMonths(6)), - createSemesterEntity(0, 0, now().minusMonths(6), now().minusMonths(1)) + createSemesterEntity(1, 1, now().minusMonths(12), now().minusMonths(6)), + createSemesterEntity(1, 2, now().minusMonths(6), now().minusMonths(1)) ) doReturn(Maybe.just(semesters)).`when`(semesterLocal).getSemesters(student) @@ -127,8 +165,8 @@ class SemesterRepositoryTest { @Test fun getSemesters_doubleCurrent_refreshOnNoCurrent() { val semesters = listOf( - createSemesterEntity(0, 0, now(), now()), - createSemesterEntity(0, 0, now(), now()) + createSemesterEntity(1, 1, now(), now()), + createSemesterEntity(1, 2, now(), now()) ) doReturn(Maybe.just(semesters)).`when`(semesterLocal).getSemesters(student) @@ -140,8 +178,8 @@ class SemesterRepositoryTest { @Test(expected = IllegalArgumentException::class) fun getCurrentSemester_doubleCurrent() { val semesters = listOf( - createSemesterEntity(0, 0, now(), now()), - createSemesterEntity(0, 0, now(), now()) + createSemesterEntity(1, 1, now(), now()), + createSemesterEntity(1, 1, now(), now()) ) doReturn(Maybe.just(semesters)).`when`(semesterLocal).getSemesters(student)