From 07d68f54258b3951ea8ef4a4371cc5e618098947 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Miko=C5=82aj=20Pich?= Date: Sat, 15 Apr 2023 22:39:36 +0200 Subject: [PATCH] Remove getStudents() API --- .gitignore | 2 + sdk-scrapper/build.gradle | 7 + .../github/wulkanowy/sdk/scrapper/Scrapper.kt | 3 - .../sdk/scrapper/register/Student.kt | 24 ---- .../scrapper/repository/RegisterRepository.kt | 48 +------ .../repository/StudentStartRepository.kt | 2 +- .../wulkanowy/sdk/scrapper/HostsRemoteTest.kt | 2 +- .../sdk/scrapper/ScrapperRemoteTest.kt | 31 +++-- .../sdk/scrapper/register/RegisterTest.kt | 47 ++++--- .../repository/RegisterRepositoryTest.kt | 128 ++++++++++-------- .../scrapper/service/ServiceManagerTest.kt | 2 +- .../kotlin/io/github/wulkanowy/sdk/Sdk.kt | 24 ++-- .../wulkanowy/sdk/mapper/StudentsMapper.kt | 87 ++++++++---- .../io/github/wulkanowy/sdk/pojo/Student.kt | 53 +++++--- .../io/github/wulkanowy/sdk/SdkRemoteTest.kt | 5 +- 15 files changed, 243 insertions(+), 222 deletions(-) delete mode 100644 sdk-scrapper/src/main/kotlin/io/github/wulkanowy/sdk/scrapper/register/Student.kt diff --git a/.gitignore b/.gitignore index d962a9c9..a3a3d937 100644 --- a/.gitignore +++ b/.gitignore @@ -75,3 +75,5 @@ classes .idea/kotlinc.xml .idea/jpa-buddy.xml .idea/kotlinScripting.xml +Snap.*.trc +javacore.*.txt diff --git a/sdk-scrapper/build.gradle b/sdk-scrapper/build.gradle index 49ac87a7..d178d8a8 100644 --- a/sdk-scrapper/build.gradle +++ b/sdk-scrapper/build.gradle @@ -19,3 +19,10 @@ dependencies { testImplementation "io.mockk:mockk-jvm:1.13.4" } + +tasks.withType(Test) { + /** + * fix for retrofit https://github.com/square/retrofit/issues/3341 + */ + jvmArgs = ["--add-opens", "java.base/java.lang.invoke=ALL-UNNAMED"] +} diff --git a/sdk-scrapper/src/main/kotlin/io/github/wulkanowy/sdk/scrapper/Scrapper.kt b/sdk-scrapper/src/main/kotlin/io/github/wulkanowy/sdk/scrapper/Scrapper.kt index d7d6ab28..bcc67d24 100644 --- a/sdk-scrapper/src/main/kotlin/io/github/wulkanowy/sdk/scrapper/Scrapper.kt +++ b/sdk-scrapper/src/main/kotlin/io/github/wulkanowy/sdk/scrapper/Scrapper.kt @@ -27,7 +27,6 @@ import io.github.wulkanowy.sdk.scrapper.mobile.TokenResponse import io.github.wulkanowy.sdk.scrapper.notes.Note import io.github.wulkanowy.sdk.scrapper.register.RegisterUser import io.github.wulkanowy.sdk.scrapper.register.Semester -import io.github.wulkanowy.sdk.scrapper.register.Student import io.github.wulkanowy.sdk.scrapper.repository.AccountRepository import io.github.wulkanowy.sdk.scrapper.repository.HomepageRepository import io.github.wulkanowy.sdk.scrapper.repository.MessagesRepository @@ -264,8 +263,6 @@ class Scrapper { return account.sendPasswordResetRequest(registerBaseUrl, symbol, email.trim(), captchaCode) } - suspend fun getStudents(): List = register.getStudents() - suspend fun getUserSubjects(): RegisterUser = register.getUserSubjects() suspend fun getSemesters(): List = studentStart.getSemesters() diff --git a/sdk-scrapper/src/main/kotlin/io/github/wulkanowy/sdk/scrapper/register/Student.kt b/sdk-scrapper/src/main/kotlin/io/github/wulkanowy/sdk/scrapper/register/Student.kt deleted file mode 100644 index f73f8640..00000000 --- a/sdk-scrapper/src/main/kotlin/io/github/wulkanowy/sdk/scrapper/register/Student.kt +++ /dev/null @@ -1,24 +0,0 @@ -package io.github.wulkanowy.sdk.scrapper.register - -import io.github.wulkanowy.sdk.scrapper.Scrapper - -data class Student( - val email: String, - val userName: String, - val userLogin: String, - val userLoginId: Int, - val symbol: String, - val studentId: Int, - val studentName: String, - val studentSecondName: String, - val studentSurname: String, - val schoolSymbol: String, - val schoolShortName: String, - val schoolName: String, - val className: String, - val classId: Int, - val baseUrl: String, - val loginType: Scrapper.LoginType, - val isParent: Boolean, - val semesters: List, -) diff --git a/sdk-scrapper/src/main/kotlin/io/github/wulkanowy/sdk/scrapper/repository/RegisterRepository.kt b/sdk-scrapper/src/main/kotlin/io/github/wulkanowy/sdk/scrapper/repository/RegisterRepository.kt index c63f4739..2922df2a 100644 --- a/sdk-scrapper/src/main/kotlin/io/github/wulkanowy/sdk/scrapper/repository/RegisterRepository.kt +++ b/sdk-scrapper/src/main/kotlin/io/github/wulkanowy/sdk/scrapper/repository/RegisterRepository.kt @@ -2,14 +2,11 @@ package io.github.wulkanowy.sdk.scrapper.repository import com.migcomponents.migbase64.Base64 import io.github.wulkanowy.sdk.scrapper.Scrapper -import io.github.wulkanowy.sdk.scrapper.exception.AccountInactiveException import io.github.wulkanowy.sdk.scrapper.exception.ScrapperException import io.github.wulkanowy.sdk.scrapper.exception.StudentGraduateException -import io.github.wulkanowy.sdk.scrapper.exception.TemporarilyDisabledException import io.github.wulkanowy.sdk.scrapper.getNormalizedSymbol import io.github.wulkanowy.sdk.scrapper.getScriptParam import io.github.wulkanowy.sdk.scrapper.interceptor.handleErrors -import io.github.wulkanowy.sdk.scrapper.login.AccountPermissionException import io.github.wulkanowy.sdk.scrapper.login.CertificateResponse import io.github.wulkanowy.sdk.scrapper.login.InvalidSymbolException import io.github.wulkanowy.sdk.scrapper.login.LoginHelper @@ -24,7 +21,6 @@ import io.github.wulkanowy.sdk.scrapper.register.RegisterStudent import io.github.wulkanowy.sdk.scrapper.register.RegisterSymbol import io.github.wulkanowy.sdk.scrapper.register.RegisterUnit import io.github.wulkanowy.sdk.scrapper.register.RegisterUser -import io.github.wulkanowy.sdk.scrapper.register.Student import io.github.wulkanowy.sdk.scrapper.register.toSemesters import io.github.wulkanowy.sdk.scrapper.repository.AccountRepository.Companion.SELECTOR_ADFS import io.github.wulkanowy.sdk.scrapper.repository.AccountRepository.Companion.SELECTOR_ADFS_CARDS @@ -51,7 +47,7 @@ class RegisterRepository( private val url: UrlGenerator, ) { - companion object { + private companion object { @JvmStatic private val logger = LoggerFactory.getLogger(this::class.java) } @@ -60,48 +56,6 @@ class RegisterRepository( ignoreUnknownKeys = true } - suspend fun getStudents(): List { - val user = getUserSubjects() - - return user.symbols.flatMap { symbol -> - symbol.error?.takeIf { - val isAccountNotRegistered = it is AccountPermissionException - val isInvalidSymbol = it is InvalidSymbolException - val isGraduated = it is StudentGraduateException - val isInactive = it is AccountInactiveException - - (!isAccountNotRegistered && !isInvalidSymbol && !isGraduated && !isInactive) - }?.let { throw it } - - symbol.schools.flatMap { unit -> - unit.error?.takeIf { it !is TemporarilyDisabledException }?.let { throw it } - - unit.subjects.filterIsInstance().map { student -> - Student( - email = user.login, // for compatibility - userLogin = user.login, - userName = symbol.userName, - userLoginId = unit.userLoginId, - symbol = symbol.symbol, - studentId = student.studentId, - studentName = student.studentName, - studentSecondName = student.studentSecondName, - studentSurname = student.studentSurname, - schoolSymbol = unit.schoolId, - schoolShortName = unit.schoolShortName, - schoolName = unit.schoolName, - className = student.className, - classId = student.classId, - baseUrl = user.baseUrl, - loginType = user.loginType, - isParent = student.isParent, - semesters = student.semesters, - ) - } - } - } - } - suspend fun getUserSubjects(): RegisterUser { val symbolLoginType = getLoginType(startSymbol.getNormalizedSymbol()) val certificateResponse = getCert(symbolLoginType) diff --git a/sdk-scrapper/src/main/kotlin/io/github/wulkanowy/sdk/scrapper/repository/StudentStartRepository.kt b/sdk-scrapper/src/main/kotlin/io/github/wulkanowy/sdk/scrapper/repository/StudentStartRepository.kt index 89b5eb19..97f7ee1f 100644 --- a/sdk-scrapper/src/main/kotlin/io/github/wulkanowy/sdk/scrapper/repository/StudentStartRepository.kt +++ b/sdk-scrapper/src/main/kotlin/io/github/wulkanowy/sdk/scrapper/repository/StudentStartRepository.kt @@ -23,7 +23,7 @@ class StudentStartRepository( return diaries?.toSemesters(studentId, classId, unitId).orEmpty() .sortedByDescending { it.semesterId } .ifEmpty { - logger.debug("Student $studentId, class $classId not found in diaries: $diaries") + logger.debug("Student {}, class {} not found in diaries: {}", studentId, classId, diaries) emptyList() } } diff --git a/sdk-scrapper/src/test/kotlin/io/github/wulkanowy/sdk/scrapper/HostsRemoteTest.kt b/sdk-scrapper/src/test/kotlin/io/github/wulkanowy/sdk/scrapper/HostsRemoteTest.kt index 65c896ea..c01c2bd4 100644 --- a/sdk-scrapper/src/test/kotlin/io/github/wulkanowy/sdk/scrapper/HostsRemoteTest.kt +++ b/sdk-scrapper/src/test/kotlin/io/github/wulkanowy/sdk/scrapper/HostsRemoteTest.kt @@ -35,7 +35,7 @@ class HostsRemoteTest : BaseTest() { fun loginTest() = runBlocking { knownHosts.forEach { (host, symbol) -> println("$host/$symbol") - val res = runCatching { getScrapper(host, symbol).getStudents() } + val res = runCatching { getScrapper(host, symbol).getUserSubjects() } requireNotNull(res.exceptionOrNull()).cause!!.printStackTrace() assert(res.exceptionOrNull() is BadCredentialsException) println() diff --git a/sdk-scrapper/src/test/kotlin/io/github/wulkanowy/sdk/scrapper/ScrapperRemoteTest.kt b/sdk-scrapper/src/test/kotlin/io/github/wulkanowy/sdk/scrapper/ScrapperRemoteTest.kt index 151c6200..c871c342 100644 --- a/sdk-scrapper/src/test/kotlin/io/github/wulkanowy/sdk/scrapper/ScrapperRemoteTest.kt +++ b/sdk-scrapper/src/test/kotlin/io/github/wulkanowy/sdk/scrapper/ScrapperRemoteTest.kt @@ -2,7 +2,10 @@ package io.github.wulkanowy.sdk.scrapper import io.github.wulkanowy.sdk.scrapper.attendance.AttendanceCategory import io.github.wulkanowy.sdk.scrapper.messages.Folder +import io.github.wulkanowy.sdk.scrapper.register.RegisterStudent +import kotlinx.coroutines.ExperimentalCoroutinesApi import kotlinx.coroutines.runBlocking +import kotlinx.coroutines.test.runTest import okhttp3.logging.HttpLoggingInterceptor import org.junit.Assert.assertEquals import org.junit.Assert.assertTrue @@ -12,6 +15,7 @@ import org.junit.Test import java.time.LocalDate import java.time.Month +@OptIn(ExperimentalCoroutinesApi::class) @Ignore class ScrapperRemoteTest : BaseTest() { @@ -67,20 +71,21 @@ class ScrapperRemoteTest : BaseTest() { } @Test - fun studentsTest() { - val students = runBlocking { api.getStudents() } + fun studentsTest() = runTest { + val user = api.getUserSubjects() + val symbol = user.symbols[0] + val school = symbol.schools[0] + val student = school.subjects[0] as RegisterStudent - students[0].run { - assertEquals("powiatwulkanowy", symbol) - assertEquals("jan@fakelog.cf", email) - assertEquals("Jan", studentName) - assertEquals("Kowalski", studentSurname) - assertEquals("123456", schoolSymbol) - assertEquals(1, studentId) - assertEquals(1, classId) - assertEquals("A", className) - assertEquals("Publiczna szkoła Wulkanowego nr 1 w fakelog.cf", schoolName) - } + assertEquals("powiatwulkanowy", symbol.symbol) + assertEquals("jan@fakelog.cf", user.email) + assertEquals("Jan", student.studentName) + assertEquals("Kowalski", student.studentSurname) + assertEquals("123456", school.schoolId) + assertEquals(1, student.studentId) + assertEquals(1, student.classId) + assertEquals("A", student.className) + assertEquals("Publiczna szkoła Wulkanowego nr 1 w fakelog.cf", school.schoolName) } @Test diff --git a/sdk-scrapper/src/test/kotlin/io/github/wulkanowy/sdk/scrapper/register/RegisterTest.kt b/sdk-scrapper/src/test/kotlin/io/github/wulkanowy/sdk/scrapper/register/RegisterTest.kt index 06fe0e66..aacd0886 100644 --- a/sdk-scrapper/src/test/kotlin/io/github/wulkanowy/sdk/scrapper/register/RegisterTest.kt +++ b/sdk-scrapper/src/test/kotlin/io/github/wulkanowy/sdk/scrapper/register/RegisterTest.kt @@ -9,11 +9,13 @@ import io.github.wulkanowy.sdk.scrapper.repository.RegisterRepository import io.github.wulkanowy.sdk.scrapper.service.LoginService import io.github.wulkanowy.sdk.scrapper.service.RegisterService import io.github.wulkanowy.sdk.scrapper.service.StudentService -import kotlinx.coroutines.runBlocking +import kotlinx.coroutines.ExperimentalCoroutinesApi +import kotlinx.coroutines.test.runTest import org.junit.Assert.assertEquals import org.junit.Test import java.net.CookieManager +@OptIn(ExperimentalCoroutinesApi::class) class RegisterTest : BaseLocalTest() { private val login by lazy { @@ -57,7 +59,7 @@ class RegisterTest : BaseLocalTest() { } @Test - fun filterStudentsByClass() { + fun filterStudentsByClass() = runTest { with(server) { enqueue("LoginPage-standard.html", LoginTest::class.java) enqueue("Logowanie-uonet.html", LoginTest::class.java) @@ -72,7 +74,10 @@ class RegisterTest : BaseLocalTest() { start(3000) } - val res = runBlocking { registerStudent.getStudents() } + val res = registerStudent.getUserSubjects().symbols + .flatMap { it.schools } + .flatMap { it.subjects } + .filterIsInstance() assertEquals(2, res.size) @@ -81,7 +86,7 @@ class RegisterTest : BaseLocalTest() { assertEquals("Jan", studentName) assertEquals("Kowalski", studentSurname) assertEquals(121, classId) - assertEquals("Publiczna szkoła Wulkanowego nr 1 w fakelog.cf", schoolName) + // assertEquals("Publiczna szkoła Wulkanowego nr 1 w fakelog.cf", schoolName) assertEquals("Te", className) } @@ -90,13 +95,13 @@ class RegisterTest : BaseLocalTest() { assertEquals("Jan", studentName) assertEquals("Kowalski", studentSurname) assertEquals(119, classId) - assertEquals("Publiczna szkoła Wulkanowego nr 1 w fakelog.cf", schoolName) + // assertEquals("Publiczna szkoła Wulkanowego nr 1 w fakelog.cf", schoolName) assertEquals("Ti", className) } } @Test - fun getStudents_kindergartenDiaries() { + fun getStudents_kindergartenDiaries() = runTest { with(server) { enqueue("LoginPage-standard.html", LoginTest::class.java) enqueue("Logowanie-uonet.html", LoginTest::class.java) @@ -114,7 +119,10 @@ class RegisterTest : BaseLocalTest() { start(3000) } - val res = runBlocking { registerStudent.getStudents() } + val res = registerStudent.getUserSubjects().symbols + .flatMap { it.schools } + .flatMap { it.subjects } + .filterIsInstance() assertEquals(1, res.size) @@ -123,17 +131,17 @@ class RegisterTest : BaseLocalTest() { assertEquals("Jan", studentName) assertEquals("Kowalski", studentSurname) assertEquals(0, classId) // always 0 for kindergarten - assertEquals("Publiczna szkoła Wulkanowego nr 1 w fakelog.cf", schoolName) - assertEquals("123456", schoolSymbol) - assertEquals(654321, userLoginId) - assertEquals("Jan Kowalski", userName) + // assertEquals("Publiczna szkoła Wulkanowego nr 1 w fakelog.cf", schoolName) + // assertEquals("123456", schoolSymbol) + // assertEquals(654321, userLoginId) + // assertEquals("Jan Kowalski", fullname) // todo assertEquals(2016, semesters[0].schoolYear) assertEquals(2017, semesters[1].schoolYear) } } @Test - fun getStudents_filterNoDiares() { + fun getStudents_filterNoDiares() = runTest { with(server) { enqueue("LoginPage-standard.html", LoginTest::class.java) enqueue("Logowanie-uonet.html", LoginTest::class.java) @@ -151,13 +159,15 @@ class RegisterTest : BaseLocalTest() { start(3000) } - val res = runBlocking { registerStudent.getStudents() } + val res = registerStudent.getUserSubjects().symbols + .flatMap { it.schools } + .flatMap { it.subjects } assertEquals(0, res.size) } @Test - fun getStudents_classNameOrder() { + fun getStudents_classNameOrder() = runTest { with(server) { enqueue("LoginPage-standard.html", LoginTest::class.java) enqueue("Logowanie-uonet.html", LoginTest::class.java) @@ -173,7 +183,10 @@ class RegisterTest : BaseLocalTest() { start(3000) } - val res = runBlocking { registerStudent.getStudents() } + val res = registerStudent.getUserSubjects().symbols + .flatMap { it.schools } + .flatMap { it.subjects } + .filterIsInstance() assertEquals(2, res.size) @@ -183,7 +196,7 @@ class RegisterTest : BaseLocalTest() { assertEquals("Kowalski", studentSurname) assertEquals(1, classId) assertEquals("A", className) - assertEquals("Publiczna szkoła Wulkanowego nr 1 w fakelog.cf", schoolName) + // assertEquals("Publiczna szkoła Wulkanowego nr 1 w fakelog.cf", schoolName) } res[1].run { @@ -192,7 +205,7 @@ class RegisterTest : BaseLocalTest() { assertEquals("Czerwińska", studentSurname) assertEquals(2, classId) assertEquals("A", className) - assertEquals("Publiczna szkoła Wulkanowego nr 1 w fakelog.cf", schoolName) + // assertEquals("Publiczna szkoła Wulkanowego nr 1 w fakelog.cf", schoolName) // todo } } } diff --git a/sdk-scrapper/src/test/kotlin/io/github/wulkanowy/sdk/scrapper/repository/RegisterRepositoryTest.kt b/sdk-scrapper/src/test/kotlin/io/github/wulkanowy/sdk/scrapper/repository/RegisterRepositoryTest.kt index 11575dfe..363ea445 100644 --- a/sdk-scrapper/src/test/kotlin/io/github/wulkanowy/sdk/scrapper/repository/RegisterRepositoryTest.kt +++ b/sdk-scrapper/src/test/kotlin/io/github/wulkanowy/sdk/scrapper/repository/RegisterRepositoryTest.kt @@ -7,12 +7,12 @@ import io.github.wulkanowy.sdk.scrapper.interceptor.ErrorInterceptorTest import io.github.wulkanowy.sdk.scrapper.login.LoginHelper import io.github.wulkanowy.sdk.scrapper.login.LoginTest import io.github.wulkanowy.sdk.scrapper.login.UrlGenerator +import io.github.wulkanowy.sdk.scrapper.register.RegisterStudent import io.github.wulkanowy.sdk.scrapper.register.RegisterTest import io.github.wulkanowy.sdk.scrapper.service.LoginService import io.github.wulkanowy.sdk.scrapper.service.RegisterService import io.github.wulkanowy.sdk.scrapper.service.StudentService import kotlinx.coroutines.ExperimentalCoroutinesApi -import kotlinx.coroutines.runBlocking import kotlinx.coroutines.test.runTest import org.junit.Assert.assertEquals import org.junit.Assert.assertTrue @@ -48,7 +48,7 @@ class RegisterRepositoryTest : BaseLocalTest() { } @Test - fun normalLogin_one() { + fun normalLogin_one() = runTest { with(server) { enqueue("LoginPage-standard.html", LoginTest::class.java) enqueue("Logowanie-uonet.html", LoginTest::class.java) @@ -65,18 +65,20 @@ class RegisterRepositoryTest : BaseLocalTest() { start(3000) } - val students = runBlocking { getRegisterRepository("Default").getStudents() } + val user = getRegisterRepository("Default").getUserSubjects() + val school = user.symbols[0].schools[0] + val students = school.subjects.filterIsInstance() - assertEquals(1, students.size) - with(students[0]) { - assertEquals("123456", schoolSymbol) + with(school) { + assertEquals("123456", schoolId) assertEquals("Fake123456", schoolShortName) - assertEquals(2, semesters.size) } + assertEquals(1, students.size) + assertEquals(2, students[0].semesters.size) } @Test - fun normalLogin_semesters() { + fun normalLogin_semesters() = runTest { with(server) { enqueue("LoginPage-standard.html", LoginTest::class.java) enqueue("Logowanie-uonet.html", LoginTest::class.java) @@ -93,19 +95,21 @@ class RegisterRepositoryTest : BaseLocalTest() { start(3000) } - val students = runBlocking { getRegisterRepository("Default").getStudents() } + val user = getRegisterRepository("Default").getUserSubjects() + val school = user.symbols[0].schools[0] + val students = school.subjects.filterIsInstance() - assertEquals(2, students.size) - with(students[0]) { - assertEquals("123456", schoolSymbol) + with(school) { + assertEquals("123456", schoolId) assertEquals("Fake123456", schoolShortName) - assertEquals(6, semesters.size) } + assertEquals(6, students[0].semesters.size) + assertEquals(2, students.size) assertEquals(6, students[1].semesters.size) } @Test - fun normalLogin_triple() { + fun normalLogin_triple() = runTest { with(server) { enqueue("LoginPage-standard.html", LoginTest::class.java) enqueue("Logowanie-uonet.html", LoginTest::class.java) @@ -124,21 +128,22 @@ class RegisterRepositoryTest : BaseLocalTest() { start(3000) } - val students = runBlocking { getRegisterRepository("Default").getStudents() } - assertEquals(3, students.size) + val user = getRegisterRepository("Default").getUserSubjects() + val schools = user.symbols[0].schools + assertEquals(3, schools.size) - with(students[0]) { - assertEquals("000788", schoolSymbol) + with(schools[0]) { + assertEquals("000788", schoolId) assertEquals("ZST-CKZiU", schoolShortName) } - with(students[1]) { - assertEquals("004355", schoolSymbol) + with(schools[1]) { + assertEquals("004355", schoolId) assertEquals("ZSET", schoolShortName) } - with(students[2]) { - assertEquals("016636", schoolSymbol) + with(schools[2]) { + assertEquals("016636", schoolId) assertEquals("G7 Wulkanowo", schoolShortName) } } @@ -165,7 +170,12 @@ class RegisterRepositoryTest : BaseLocalTest() { start(3000) } - val students = getRegisterRepository("Default").getStudents() + val user = getRegisterRepository("Default").getUserSubjects() + val students = user.symbols + .flatMap { it.schools } + .flatMap { it.subjects } + .filterIsInstance() + assertEquals(2, students.size) } @@ -174,25 +184,26 @@ class RegisterRepositoryTest : BaseLocalTest() { with(server) { enqueue("LoginPage-standard.html", LoginTest::class.java) enqueue("Logowanie-uonet.html", LoginTest::class.java) + enqueue("Logowanie-brak-dostepu.html", LoginTest::class.java) enqueue("Offline.html", ErrorInterceptorTest::class.java) - enqueue("Logowanie-brak-dostepu.html", LoginTest::class.java) enqueue("Logowanie-brak-dostepu.html", LoginTest::class.java) enqueue("Logowanie-brak-dostepu.html", LoginTest::class.java) start(3000) } - val res = runCatching { normal.getStudents() } + val res = normal.getUserSubjects().symbols + assertEquals(5, res.size) assertEquals( "Wystąpił nieoczekiwany błąd. Wystąpił błąd aplikacji. Prosimy zalogować się ponownie. Jeśli problem będzie się powtarzał, prosimy o kontakt z serwisem.", - res.exceptionOrNull()?.message, + res[1].error?.message, ) - assertEquals(VulcanException::class.java, res.exceptionOrNull()!!::class.java) + assertEquals(VulcanException::class.java, res[1].error!!::class.java) } @Test - fun filterSymbolsWithSpaces() { + fun filterSymbolsWithSpaces() = runTest { with(server) { enqueue("LoginPage-standard.html", LoginTest::class.java) enqueue("Logowanie-uonet.html", LoginTest::class.java) @@ -204,18 +215,21 @@ class RegisterRepositoryTest : BaseLocalTest() { start(3000) } - val students = runBlocking { normal.getStudents() } - assertEquals(0, students.size) + val user = normal.getUserSubjects().symbols + .flatMap { it.schools } + .flatMap { it.subjects } + + assertEquals(0, user.size) } @Test - fun normalizeInvalidSymbol_default() { + fun normalizeInvalidSymbol_default() = runTest { with(server) { enqueue("LoginPage-standard.html", LoginTest::class.java) enqueue("Logowanie-uonet.html", LoginTest::class.java) + enqueue("Logowanie-brak-dostepu.html", LoginTest::class.java) enqueue("Offline.html", ErrorInterceptorTest::class.java) - enqueue("Logowanie-brak-dostepu.html", LoginTest::class.java) enqueue("Logowanie-brak-dostepu.html", LoginTest::class.java) enqueue("Logowanie-brak-dostepu.html", LoginTest::class.java) @@ -223,23 +237,24 @@ class RegisterRepositoryTest : BaseLocalTest() { start(3000) } - val res = runCatching { runBlocking { getRegisterRepository("Default").getStudents() } } + val res = getRegisterRepository("Default").getUserSubjects() + assertEquals(5, res.symbols.size) assertEquals( "Wystąpił nieoczekiwany błąd. Wystąpił błąd aplikacji. Prosimy zalogować się ponownie. Jeśli problem będzie się powtarzał, prosimy o kontakt z serwisem.", - res.exceptionOrNull()?.message, + res.symbols[1].error?.message, ) - assertTrue(res.exceptionOrNull() is VulcanException) + assertTrue(res.symbols[1].error is VulcanException) assertEquals("/Default/Account/LogOn", server.takeRequest().path) } @Test - fun normalizeInvalidSymbol_custom() { + fun normalizeInvalidSymbol_custom() = runTest { with(server) { enqueue("LoginPage-standard.html", LoginTest::class.java) enqueue("Logowanie-uonet.html", LoginTest::class.java) + enqueue("Logowanie-brak-dostepu.html", LoginTest::class.java) enqueue("Offline.html", ErrorInterceptorTest::class.java) - enqueue("Logowanie-brak-dostepu.html", LoginTest::class.java) enqueue("Logowanie-brak-dostepu.html", LoginTest::class.java) enqueue("Logowanie-brak-dostepu.html", LoginTest::class.java) @@ -247,23 +262,24 @@ class RegisterRepositoryTest : BaseLocalTest() { start(3000) } - val res = runCatching { runBlocking { getRegisterRepository(" Rzeszów + ").getStudents() } } - assertTrue(res.exceptionOrNull() is VulcanException) + val res = getRegisterRepository(" Rzeszów + ").getUserSubjects() + assertEquals(5, res.symbols.size) + assertTrue(res.symbols[1].error is VulcanException) assertEquals( "Wystąpił nieoczekiwany błąd. Wystąpił błąd aplikacji. Prosimy zalogować się ponownie. Jeśli problem będzie się powtarzał, prosimy o kontakt z serwisem.", - res.exceptionOrNull()?.message, + res.symbols[1].error?.message, ) assertEquals("/rzeszow/Account/LogOn", server.takeRequest().path) } @Test - fun normalizeInvalidSymbol_trimMultipleSpaces() { + fun normalizeInvalidSymbol_trimMultipleSpaces() = runTest { with(server) { enqueue("LoginPage-standard.html", LoginTest::class.java) enqueue("Logowanie-uonet.html", LoginTest::class.java) + enqueue("Logowanie-brak-dostepu.html", LoginTest::class.java) enqueue("Offline.html", ErrorInterceptorTest::class.java) - enqueue("Logowanie-brak-dostepu.html", LoginTest::class.java) enqueue("Logowanie-brak-dostepu.html", LoginTest::class.java) enqueue("Logowanie-brak-dostepu.html", LoginTest::class.java) @@ -271,24 +287,24 @@ class RegisterRepositoryTest : BaseLocalTest() { start(3000) } - val res = runCatching { runBlocking { getRegisterRepository(" Niepoprawny symbol no ale + ").getStudents() } } - assertTrue(res.exceptionOrNull() is VulcanException) + val res = getRegisterRepository(" Niepoprawny symbol no ale + ").getUserSubjects() + assertTrue(res.symbols[1].error is VulcanException) assertEquals( "Wystąpił nieoczekiwany błąd. Wystąpił błąd aplikacji. Prosimy zalogować się ponownie. Jeśli problem będzie się powtarzał, prosimy o kontakt z serwisem.", - res.exceptionOrNull()?.message, + res.symbols[1].error?.message, ) assertEquals("/niepoprawnysymbolnoale/Account/LogOn", server.takeRequest().path) } @Test - fun normalizeInvalidSymbol_emptyFallback() { + fun normalizeInvalidSymbol_emptyFallback() = runTest { with(server) { enqueue("LoginPage-standard.html", LoginTest::class.java) enqueue("Logowanie-uonet.html", LoginTest::class.java) + enqueue("Logowanie-brak-dostepu.html", LoginTest::class.java) enqueue("Offline.html", ErrorInterceptorTest::class.java) - enqueue("Logowanie-brak-dostepu.html", LoginTest::class.java) enqueue("Logowanie-brak-dostepu.html", LoginTest::class.java) enqueue("Logowanie-brak-dostepu.html", LoginTest::class.java) @@ -296,23 +312,24 @@ class RegisterRepositoryTest : BaseLocalTest() { start(3000) } - val res = runCatching { runBlocking { getRegisterRepository(" + ").getStudents() } } - assertTrue(res.exceptionOrNull() is VulcanException) + val res = getRegisterRepository(" + ").getUserSubjects() + assertEquals(5, res.symbols.size) + assertTrue(res.symbols[1].error is VulcanException) assertEquals( "Wystąpił nieoczekiwany błąd. Wystąpił błąd aplikacji. Prosimy zalogować się ponownie. Jeśli problem będzie się powtarzał, prosimy o kontakt z serwisem.", - res.exceptionOrNull()?.message, + res.symbols[1].error?.message, ) assertEquals("/Default/Account/LogOn", server.takeRequest().path) } @Test - fun normalizeInvalidSymbol_digits() { + fun normalizeInvalidSymbol_digits() = runTest { with(server) { enqueue("LoginPage-standard.html", LoginTest::class.java) enqueue("Logowanie-uonet.html", LoginTest::class.java) + enqueue("Logowanie-brak-dostepu.html", LoginTest::class.java) enqueue("Offline.html", ErrorInterceptorTest::class.java) - enqueue("Logowanie-brak-dostepu.html", LoginTest::class.java) enqueue("Logowanie-brak-dostepu.html", LoginTest::class.java) enqueue("Logowanie-brak-dostepu.html", LoginTest::class.java) @@ -320,11 +337,12 @@ class RegisterRepositoryTest : BaseLocalTest() { start(3000) } - val res = runCatching { runBlocking { getRegisterRepository("Default").getStudents() } } - assertTrue(res.exceptionOrNull() is VulcanException) + val res = getRegisterRepository("Default").getUserSubjects() + assertEquals(5, res.symbols.size) + assertTrue(res.symbols[1].error is VulcanException) assertEquals( "Wystąpił nieoczekiwany błąd. Wystąpił błąd aplikacji. Prosimy zalogować się ponownie. Jeśli problem będzie się powtarzał, prosimy o kontakt z serwisem.", - res.exceptionOrNull()?.message, + res.symbols[1].error?.message, ) assertEquals("/Default/Account/LogOn", server.takeRequest().path) assertEquals(true, server.takeRequest().path?.startsWith("/Account/LogOn?ReturnUrl=%2FDefault")) diff --git a/sdk-scrapper/src/test/kotlin/io/github/wulkanowy/sdk/scrapper/service/ServiceManagerTest.kt b/sdk-scrapper/src/test/kotlin/io/github/wulkanowy/sdk/scrapper/service/ServiceManagerTest.kt index 00cb1ec5..2378a6de 100644 --- a/sdk-scrapper/src/test/kotlin/io/github/wulkanowy/sdk/scrapper/service/ServiceManagerTest.kt +++ b/sdk-scrapper/src/test/kotlin/io/github/wulkanowy/sdk/scrapper/service/ServiceManagerTest.kt @@ -108,7 +108,7 @@ class ServiceManagerTest : BaseLocalTest() { } try { - runBlocking { api.getStudents() } + runBlocking { api.getUserSubjects() } } catch (e: Throwable) { assertTrue(e is ScrapperException) } diff --git a/sdk/src/main/kotlin/io/github/wulkanowy/sdk/Sdk.kt b/sdk/src/main/kotlin/io/github/wulkanowy/sdk/Sdk.kt index cca36dd4..ed77f52a 100644 --- a/sdk/src/main/kotlin/io/github/wulkanowy/sdk/Sdk.kt +++ b/sdk/src/main/kotlin/io/github/wulkanowy/sdk/Sdk.kt @@ -22,13 +22,13 @@ import io.github.wulkanowy.sdk.mapper.mapSchool import io.github.wulkanowy.sdk.mapper.mapScrapperMessage import io.github.wulkanowy.sdk.mapper.mapSemesters import io.github.wulkanowy.sdk.mapper.mapStudent -import io.github.wulkanowy.sdk.mapper.mapStudents import io.github.wulkanowy.sdk.mapper.mapSubjects import io.github.wulkanowy.sdk.mapper.mapTeachers import io.github.wulkanowy.sdk.mapper.mapTimetableFull import io.github.wulkanowy.sdk.mapper.mapToScrapperAbsent import io.github.wulkanowy.sdk.mapper.mapToUnits import io.github.wulkanowy.sdk.mapper.mapToken +import io.github.wulkanowy.sdk.mapper.mapUser import io.github.wulkanowy.sdk.pojo.Absent import io.github.wulkanowy.sdk.pojo.Attendance import io.github.wulkanowy.sdk.pojo.AttendanceSummary @@ -51,9 +51,9 @@ import io.github.wulkanowy.sdk.pojo.MessageDetails import io.github.wulkanowy.sdk.pojo.MessageReplayDetails import io.github.wulkanowy.sdk.pojo.Note import io.github.wulkanowy.sdk.pojo.Recipient +import io.github.wulkanowy.sdk.pojo.RegisterUser import io.github.wulkanowy.sdk.pojo.School import io.github.wulkanowy.sdk.pojo.Semester -import io.github.wulkanowy.sdk.pojo.Student import io.github.wulkanowy.sdk.pojo.StudentInfo import io.github.wulkanowy.sdk.pojo.StudentPhoto import io.github.wulkanowy.sdk.pojo.Subject @@ -61,7 +61,6 @@ import io.github.wulkanowy.sdk.pojo.Teacher import io.github.wulkanowy.sdk.pojo.Timetable import io.github.wulkanowy.sdk.pojo.Token import io.github.wulkanowy.sdk.scrapper.Scrapper -import io.github.wulkanowy.sdk.scrapper.register.RegisterUser import kotlinx.coroutines.Dispatchers import kotlinx.coroutines.withContext import okhttp3.Interceptor @@ -225,23 +224,18 @@ class Sdk { scrapper.sendPasswordResetRequest(registerBaseUrl, symbol, email, captchaCode) } - suspend fun getStudentsFromScrapper(email: String, password: String, scrapperBaseUrl: String, symbol: String = "Default"): List = withContext(Dispatchers.IO) { + suspend fun getUserSubjectsFromScrapper( + email: String, + password: String, + scrapperBaseUrl: String, + symbol: String = "Default", + ): RegisterUser = withContext(Dispatchers.IO) { scrapper.let { it.baseUrl = scrapperBaseUrl it.email = email it.password = password it.symbol = symbol - it.getStudents().mapStudents() - } - } - - suspend fun getUserSubjectsFromScrapper(email: String, password: String, scrapperBaseUrl: String, symbol: String = "Default"): RegisterUser = withContext(Dispatchers.IO) { - scrapper.let { - it.baseUrl = scrapperBaseUrl - it.email = email - it.password = password - it.symbol = symbol - it.getUserSubjects() + it.getUserSubjects().mapUser() } } diff --git a/sdk/src/main/kotlin/io/github/wulkanowy/sdk/mapper/StudentsMapper.kt b/sdk/src/main/kotlin/io/github/wulkanowy/sdk/mapper/StudentsMapper.kt index 4a1fc853..3cddc3cd 100644 --- a/sdk/src/main/kotlin/io/github/wulkanowy/sdk/mapper/StudentsMapper.kt +++ b/sdk/src/main/kotlin/io/github/wulkanowy/sdk/mapper/StudentsMapper.kt @@ -1,31 +1,64 @@ package io.github.wulkanowy.sdk.mapper -import io.github.wulkanowy.sdk.Sdk -import io.github.wulkanowy.sdk.pojo.Student -import io.github.wulkanowy.sdk.scrapper.register.Student as ScrapperStudent +import io.github.wulkanowy.sdk.pojo.RegisterEmployee +import io.github.wulkanowy.sdk.pojo.RegisterStudent +import io.github.wulkanowy.sdk.pojo.RegisterSubject +import io.github.wulkanowy.sdk.pojo.RegisterSymbol +import io.github.wulkanowy.sdk.pojo.RegisterUnit +import io.github.wulkanowy.sdk.pojo.RegisterUser +import io.github.wulkanowy.sdk.scrapper.register.RegisterEmployee as ScrapperRegisterEmploye +import io.github.wulkanowy.sdk.scrapper.register.RegisterStudent as ScrapperRegisterStudent +import io.github.wulkanowy.sdk.scrapper.register.RegisterSubject as ScrapperRegisterSubject +import io.github.wulkanowy.sdk.scrapper.register.RegisterSymbol as SdkRegisterSymbol +import io.github.wulkanowy.sdk.scrapper.register.RegisterUnit as ScrapperRegisterUnit +import io.github.wulkanowy.sdk.scrapper.register.RegisterUser as ScrapperRegisterUser -fun List.mapStudents() = map { - Student( - email = it.email, - userName = it.userName, - userLogin = it.userLogin, - userLoginId = it.userLoginId, - isParent = it.isParent, - className = it.className, - classId = it.classId, - studentId = it.studentId, - symbol = it.symbol, - loginType = Sdk.ScrapperLoginType.valueOf(it.loginType.name), - schoolName = it.schoolName, - schoolShortName = it.schoolShortName, - schoolSymbol = it.schoolSymbol, - studentName = it.studentName, - studentSurname = it.studentSurname, - loginMode = Sdk.Mode.SCRAPPER, - scrapperBaseUrl = it.baseUrl, - mobileBaseUrl = "", - certificateKey = "", - privateKey = "", - semesters = it.semesters.mapSemesters(), - ) +fun ScrapperRegisterUser.mapUser(): RegisterUser = RegisterUser( + email = email, + login = login, + baseUrl = baseUrl, + loginType = loginType, + symbols = symbols.map { it.mapSymbol() }, +) + +fun SdkRegisterSymbol.mapSymbol(): RegisterSymbol = RegisterSymbol( + symbol = symbol, + userName = userName, + error = error, + schools = schools.map { it.mapUnit() }, +) + +fun ScrapperRegisterUnit.mapUnit(): RegisterUnit = RegisterUnit( + userLoginId = userLoginId, + schoolId = schoolId, + schoolName = schoolName, + schoolShortName = schoolShortName, + parentIds = parentIds, + studentIds = studentIds, + employeeIds = employeeIds, + error = error, + subjects = subjects.map { it.mapSubject() }, +) + +fun ScrapperRegisterSubject.mapSubject(): RegisterSubject { + return when (this) { + is ScrapperRegisterStudent -> mapStudent() + is ScrapperRegisterEmploye -> mapEmployee() + } } + +fun ScrapperRegisterEmploye.mapEmployee(): RegisterEmployee = RegisterEmployee( + employeeId = employeeId, + employeeName = employeeName, +) + +fun ScrapperRegisterStudent.mapStudent(): RegisterStudent = RegisterStudent( + studentId = studentId, + studentName = studentName, + studentSecondName = studentSecondName, + studentSurname = studentSurname, + className = className, + classId = classId, + isParent = isParent, + semesters = semesters.mapSemesters(), +) diff --git a/sdk/src/main/kotlin/io/github/wulkanowy/sdk/pojo/Student.kt b/sdk/src/main/kotlin/io/github/wulkanowy/sdk/pojo/Student.kt index f4a020fd..b455daa5 100644 --- a/sdk/src/main/kotlin/io/github/wulkanowy/sdk/pojo/Student.kt +++ b/sdk/src/main/kotlin/io/github/wulkanowy/sdk/pojo/Student.kt @@ -1,27 +1,48 @@ package io.github.wulkanowy.sdk.pojo -import io.github.wulkanowy.sdk.Sdk +import io.github.wulkanowy.sdk.scrapper.Scrapper -data class Student( +data class RegisterUser( val email: String, - val userName: String, - val userLogin: String, - val userLoginId: Int, + val login: String, // may be the same as email + val baseUrl: String, + val loginType: Scrapper.LoginType?, + val symbols: List, +) + +data class RegisterSymbol( val symbol: String, - val isParent: Boolean, + val error: Throwable?, + val userName: String, + val schools: List, +) + +data class RegisterUnit( + val userLoginId: Int, + val schoolId: String, + val schoolName: String, + val schoolShortName: String, + val parentIds: List, + val studentIds: List, + val employeeIds: List, + val error: Throwable?, + val subjects: List, +) + +sealed interface RegisterSubject + +data class RegisterEmployee( + val employeeId: Int, + val employeeName: String, +) : RegisterSubject + +data class RegisterStudent( val studentId: Int, val studentName: String, + val studentSecondName: String, val studentSurname: String, - val schoolSymbol: String, - val schoolShortName: String, - val schoolName: String, val className: String, val classId: Int, - val loginType: Sdk.ScrapperLoginType, - val loginMode: Sdk.Mode, - val scrapperBaseUrl: String, - val mobileBaseUrl: String, - val certificateKey: String, - val privateKey: String, + val isParent: Boolean, val semesters: List, -) +) : RegisterSubject diff --git a/sdk/src/test/kotlin/io/github/wulkanowy/sdk/SdkRemoteTest.kt b/sdk/src/test/kotlin/io/github/wulkanowy/sdk/SdkRemoteTest.kt index d4609721..ea447ece 100644 --- a/sdk/src/test/kotlin/io/github/wulkanowy/sdk/SdkRemoteTest.kt +++ b/sdk/src/test/kotlin/io/github/wulkanowy/sdk/SdkRemoteTest.kt @@ -26,8 +26,9 @@ class SdkRemoteTest { // mode = Sdk.Mode.SCRAPPER } - val students = - runBlocking { sdk.getStudentsFromScrapper(email = "jan@fakelog.cf", password = "jan123", scrapperBaseUrl = "http://fakelog.cf", symbol = "powiatwulkanowy") } + val students = runBlocking { + sdk.getUserSubjectsFromScrapper(email = "jan@fakelog.cf", password = "jan123", scrapperBaseUrl = "http://fakelog.cf", symbol = "powiatwulkanowy") + }.symbols.flatMap { it.schools }.flatMap { it.subjects } assertEquals(6, students.size) }