Normalize start symbol

Closes #17
This commit is contained in:
Mikołaj Pich 2019-03-31 16:07:27 +02:00
parent e3a8c82d2c
commit 35c10d0d7c
No known key found for this signature in database
GPG key ID: F62B26E36D4C4BAA
2 changed files with 82 additions and 15 deletions

View file

@ -18,6 +18,7 @@ import io.reactivex.Single
import org.jsoup.Jsoup
import org.jsoup.parser.Parser
import java.net.URL
import java.text.Normalizer
class RegisterRepository(
private val startSymbol: String,
@ -32,20 +33,20 @@ class RegisterRepository(
) {
fun getStudents(): Single<List<Student>> {
return getSymbols().flatMapObservable { Observable.fromIterable(it) }.flatMap { symbol ->
loginHelper.sendCertificate(symbol.second, symbol.second.action.replace(startSymbol, symbol.first))
return getSymbols().flatMapObservable { Observable.fromIterable(it) }.flatMap { (symbol, certificate) ->
loginHelper.sendCertificate(certificate, certificate.action.replace(startSymbol.getNormalizedSymbol(), symbol))
.onErrorResumeNext { t ->
if (t is AccountPermissionException) Single.just(SendCertificateResponse())
else Single.error(t)
}
.flatMapObservable { Observable.fromIterable(if (useNewStudent) it.studentSchools else it.oldStudentSchools) }
.flatMapSingle { schoolUrl ->
getLoginType(symbol.first).flatMap { loginType ->
getStudents(symbol.first, schoolUrl).map {
getLoginType(symbol).flatMap { loginType ->
getStudents(symbol, schoolUrl).map {
it.map { student ->
Student(
email = email,
symbol = symbol.first,
symbol = symbol,
studentId = student.id,
studentName = student.name,
schoolSymbol = getExtractedSchoolSymbolFromUrl(schoolUrl),
@ -57,7 +58,11 @@ class RegisterRepository(
}
}
}
}.toList().map { it.flatten().distinctBy { pupil -> listOf(pupil.studentId, pupil.classId, pupil.schoolSymbol) } }
}.toList().map {
it.flatten().distinctBy { pupil ->
listOf(pupil.studentId, pupil.classId, pupil.schoolSymbol)
}
}
}
private fun getStudents(symbol: String, schoolUrl: String): Single<List<StudentAndParentResponse.Student>> {
@ -87,7 +92,7 @@ class RegisterRepository(
}
private fun getSymbols(): Single<List<Pair<String, CertificateResponse>>> {
return getLoginType(startSymbol).map {
return getLoginType(startSymbol.getNormalizedSymbol()).map {
loginHelper.apply { loginType = it }
}.flatMap { login ->
login.sendCredentials(email, password).flatMap { Single.just(it) }.flatMap { cert ->
@ -123,4 +128,12 @@ class RegisterRepository(
return path[2]
}
private fun String.getNormalizedSymbol(): String {
return trim().toLowerCase().replace("default", "").run {
Normalizer.normalize(this, Normalizer.Form.NFD).run {
"\\p{InCombiningDiacriticalMarks}+".toRegex().replace(this, "")
}
}.replace("[^a-z]".toRegex(), "").ifBlank { "Default" }
}
}

View file

@ -14,19 +14,22 @@ import io.github.wulkanowy.api.service.StudentAndParentService
import io.github.wulkanowy.api.service.StudentService
import io.reactivex.observers.TestObserver
import okhttp3.mockwebserver.MockResponse
import org.junit.Assert.assertEquals
import org.junit.Test
import java.net.CookieManager
class RegisterRepositoryTest : BaseLocalTest() {
private val normal by lazy {
RegisterRepository("Default", "jan@fakelog.cf", "jan123", false,
LoginHelper(Api.LoginType.STANDARD, "http", "fakelog.localhost:3000", "Default", CookieManager(),
getService(LoginService::class.java, "http://fakelog.localhost:3000/")),
getService(service = RegisterService::class.java, url = "http://fakelog.localhost:3000/", errorInterceptor = false, noLoggedInInterceptor = false),
getService(StudentAndParentService::class.java),
getService(StudentService::class.java),
ServiceManager.UrlGenerator("http", "fakelog.localhost:3000", "Default", "")
private val normal by lazy { getRegisterRepository("Default") }
private fun getRegisterRepository(symbol: String): RegisterRepository {
return RegisterRepository(symbol, "jan@fakelog.cf", "jan123", false,
LoginHelper(Api.LoginType.STANDARD, "http", "fakelog.localhost:3000", symbol, CookieManager(),
getService(LoginService::class.java, "http://fakelog.localhost:3000/")),
getService(service = RegisterService::class.java, url = "http://fakelog.localhost:3000/", errorInterceptor = false, noLoggedInInterceptor = false),
getService(StudentAndParentService::class.java),
getService(StudentService::class.java),
ServiceManager.UrlGenerator("http", "fakelog.localhost:3000", symbol, "")
)
}
@ -63,4 +66,55 @@ class RegisterRepositoryTest : BaseLocalTest() {
res.subscribe(observer)
observer.assertComplete()
}
@Test
fun normalizeInvalidSymbol_default() {
server.enqueue(MockResponse().setBody(LoginTest::class.java.getResource("LoginPage-standard.html").readText()))
server.enqueue(MockResponse().setBody(LoginTest::class.java.getResource("Logowanie-uonet.html").readText()))
server.enqueue(MockResponse().setBody(LoginTest::class.java.getResource("Logowanie-brak-dostepu.html").readText()))
server.enqueue(MockResponse().setBody(ErrorInterceptorTest::class.java.getResource("Offline.html").readText()))
server.start(3000)
val res = getRegisterRepository("Default").getStudents()
val observer = TestObserver<List<Student>>()
res.subscribe(observer)
observer.assertTerminated()
assertEquals("/Default/Account/LogOn", server.takeRequest().path)
}
@Test
fun normalizeInvalidSymbol_custom() {
server.enqueue(MockResponse().setBody(LoginTest::class.java.getResource("LoginPage-standard.html").readText()))
server.enqueue(MockResponse().setBody(LoginTest::class.java.getResource("Logowanie-uonet.html").readText()))
server.enqueue(MockResponse().setBody(LoginTest::class.java.getResource("Logowanie-brak-dostepu.html").readText()))
server.enqueue(MockResponse().setBody(ErrorInterceptorTest::class.java.getResource("Offline.html").readText()))
server.start(3000)
val res = getRegisterRepository(" Rzeszów + ").getStudents()
val observer = TestObserver<List<Student>>()
res.subscribe(observer)
observer.assertTerminated()
assertEquals("/rzeszow/Account/LogOn", server.takeRequest().path)
}
@Test
fun normalizeInvalidSymbol_emptyFallback() {
server.enqueue(MockResponse().setBody(LoginTest::class.java.getResource("LoginPage-standard.html").readText()))
server.enqueue(MockResponse().setBody(LoginTest::class.java.getResource("Logowanie-uonet.html").readText()))
server.enqueue(MockResponse().setBody(LoginTest::class.java.getResource("Logowanie-brak-dostepu.html").readText()))
server.enqueue(MockResponse().setBody(ErrorInterceptorTest::class.java.getResource("Offline.html").readText()))
server.start(3000)
val res = getRegisterRepository(" + ").getStudents()
val observer = TestObserver<List<Student>>()
res.subscribe(observer)
observer.assertTerminated()
assertEquals("/Default/Account/LogOn", server.takeRequest().path)
}
}