Browse Source

added junit test for TimetableParser

* this also is release 1.1.4 (api version 1.1.2)
* closes issue #5
pull/18/head 1.1.4
Jannik 3 years ago
parent
commit
3be98f6129
Signed by: Seil0
GPG Key ID: E8459F3723C52C24
  1. 3
      src/main/kotlin/org/mosad/thecitadelofricks/hsoparser/MensaParser.kt
  2. 17
      src/main/kotlin/org/mosad/thecitadelofricks/hsoparser/TimetableParser.kt
  3. 23
      src/test/kotlin/org/mosad/thecitadelofricks/hsoparser/TimetableParserTest.kt
  4. 1
      src/test/resources/expected/Timetable_empty-week_expected.txt
  5. 1
      src/test/resources/expected/Timetable_normal-week_expected.txt
  6. 1189
      src/test/resources/html/Timetable_empty-week.html

3
src/main/kotlin/org/mosad/thecitadelofricks/hsoparser/MensaParser.kt

@ -30,8 +30,7 @@ import org.slf4j.LoggerFactory
import java.net.SocketTimeoutException
class MensaParser {
var logger: org.slf4j.Logger = LoggerFactory.getLogger(MensaParser::class.java)
private var logger: org.slf4j.Logger = LoggerFactory.getLogger(MensaParser::class.java)
/**
* returns the mensa's menu for a week

17
src/main/kotlin/org/mosad/thecitadelofricks/hsoparser/TimetableParser.kt

@ -26,8 +26,10 @@ import org.jsoup.Jsoup
import org.jsoup.nodes.Document
import org.mosad.thecitadelofricks.Lesson
import org.mosad.thecitadelofricks.TimetableWeek
import org.slf4j.LoggerFactory
class TimetableParser {
private var logger: org.slf4j.Logger = LoggerFactory.getLogger(TimetableParser::class.java)
private val days = arrayOf("Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday")
/**
@ -37,10 +39,18 @@ class TimetableParser {
* @param timetableURL the URL of the timetable you want to get
*/
fun getTimeTable(timetableURL: String): TimetableWeek {
return try {
parseTimeTable(Jsoup.connect(timetableURL).get())
} catch (gex: Exception) {
logger.error("general TimetableParser error", gex)
TimetableWeek()
}
}
fun parseTimeTable(htmlDoc: Document): TimetableWeek {
val timetableWeek = TimetableWeek()
val scheduleHTML = Jsoup.connect(timetableURL).get() // TODO add a try catch block to cover timeouts
val rows = htmlDoc.select("table.timetable").select("tr[scope=\"row\"]")
val rows = scheduleHTML.select("table.timetable").select("tr[scope=\"row\"]")
var sDay = -1
var sRow = -1
var sLesson = Lesson("", "", "", "", "")
@ -113,7 +123,8 @@ class TimetableParser {
fun getWeekNumberYear(timetableURL: String): Int {
return try {
parseWeekNumberYear(Jsoup.connect(timetableURL).get())
} catch (ex: Exception) {
} catch (gex: Exception) {
logger.error("general TimetableParser error", gex)
0
}
}

23
src/test/kotlin/org/mosad/thecitadelofricks/hsoparser/TimetableParserTest.kt

@ -31,15 +31,30 @@ class TimetableParserTest {
@Test
fun parseTimetableNormalWeek() {
// TODO
val htmlFile = File(TimetableParserTest::class.java.getResource("/html/Timetable_normal-week.html").path)
val htmlDoc = Jsoup.parse(htmlFile,"UTF-8", "https://www.hs-offenburg.de/")
val actualTimetable = TimetableParser().parseTimeTable(htmlDoc).toString().trim()
val expectedTimetable = File(TimetableParserTest::class.java.getResource("/expected/Timetable_normal-week_expected.txt").toURI()).readText().trim()
Assertions.assertEquals(expectedTimetable, actualTimetable)
}
@Test
fun parseTimetableEmptyWeek() {
val htmlFile = File(TimetableParserTest::class.java.getResource("/html/Timetable_empty-week.html").path)
val htmlDoc = Jsoup.parse(htmlFile,"UTF-8", "https://www.hs-offenburg.de/")
val actualTimetable = TimetableParser().parseTimeTable(htmlDoc).toString().trim()
val expectedTimetable = File(TimetableParserTest::class.java.getResource("/expected/Timetable_empty-week_expected.txt").toURI()).readText().trim()
Assertions.assertEquals(expectedTimetable, actualTimetable)
}
@Test
fun parseWeekNumberYear() {
val htmlFile = File(MensaParserTest::class.java.getResource("/html/Timetable_normal-week.html").path)
val htmlFile = File(TimetableParserTest::class.java.getResource("/html/Timetable_normal-week.html").path)
val htmlDoc = Jsoup.parse(htmlFile,"UTF-8", "https://www.hs-offenburg.de/")
val weekNumberYear = TimetableParser().parseWeekNumberYear(htmlDoc)
val actualWeekNumberYear = TimetableParser().parseWeekNumberYear(htmlDoc)
Assertions.assertEquals(42, weekNumberYear)
Assertions.assertEquals(42, actualWeekNumberYear)
}
}

1
src/test/resources/expected/Timetable_empty-week_expected.txt

@ -0,0 +1 @@
TimetableWeek(days=[TimetableDay(timeslots=[[], [], [], [], [], []]), TimetableDay(timeslots=[[], [], [], [], [], []]), TimetableDay(timeslots=[[], [], [], [], [], []]), TimetableDay(timeslots=[[], [], [], [], [], []]), TimetableDay(timeslots=[[], [], [], [], [], []]), TimetableDay(timeslots=[[], [], [], [], [], []])])

1
src/test/resources/expected/Timetable_normal-week_expected.txt

@ -0,0 +1 @@
TimetableWeek(days=[TimetableDay(timeslots=[[Lesson(lessonID=0.0.0, lessonSubject=, lessonTeacher=, lessonRoom=, lessonRemark=)], [Lesson(lessonID=0.1.0, lessonSubject=, lessonTeacher=, lessonRoom=, lessonRemark=)], [Lesson(lessonID=0.2.0, lessonSubject=, lessonTeacher=, lessonRoom=, lessonRemark=)], [Lesson(lessonID=0.3.0, lessonSubject=Prakt. Computernetze, lessonTeacher=Mayer E., lessonRoom=STB 0.28, A111a, lessonRemark=)], [Lesson(lessonID=0.4.0, lessonSubject=Prakt. Computernetze, lessonTeacher=Mayer E., lessonRoom=STB 0.28, A111a, lessonRemark=)], [Lesson(lessonID=0.5.0, lessonSubject=, lessonTeacher=, lessonRoom=, lessonRemark=)]]), TimetableDay(timeslots=[[Lesson(lessonID=1.0.0, lessonSubject=, lessonTeacher=, lessonRoom=, lessonRemark=)], [Lesson(lessonID=1.1.0, lessonSubject=Systemprogramm., lessonTeacher=Mayer E., lessonRoom=B040, lessonRemark=)], [Lesson(lessonID=1.2.0, lessonSubject=, lessonTeacher=, lessonRoom=, lessonRemark=)], [Lesson(lessonID=1.3.0, lessonSubject=Prakt. Computernetze, lessonTeacher=Mayer E., lessonRoom=STB 0.28, A112, lessonRemark=)], [Lesson(lessonID=1.4.0, lessonSubject=Prakt. Computernetze, lessonTeacher=Mayer E., lessonRoom=STB 0.28, A112, lessonRemark=)], [Lesson(lessonID=1.5.0, lessonSubject=, lessonTeacher=, lessonRoom=, lessonRemark=)]]), TimetableDay(timeslots=[[Lesson(lessonID=2.0.0, lessonSubject=Softw. Engin. 1, lessonTeacher=Dorer, K., lessonRoom=A213, lessonRemark=)], [Lesson(lessonID=2.1.0, lessonSubject=Datenbanksysteme 1, lessonTeacher=Grabowski, H., lessonRoom=B040, lessonRemark=)], [Lesson(lessonID=2.2.0, lessonSubject=, lessonTeacher=, lessonRoom=, lessonRemark=)], [Lesson(lessonID=2.3.0, lessonSubject=Praktikum Internet-Programmierung, lessonTeacher=Orb, J., lessonRoom=B207, lessonRemark=)], [Lesson(lessonID=2.4.0, lessonSubject=Praktikum Internet-Programmierung, lessonTeacher=Orb, J., lessonRoom=B207, lessonRemark=)], [Lesson(lessonID=2.5.0, lessonSubject=, lessonTeacher=, lessonRoom=, lessonRemark=)]]), TimetableDay(timeslots=[[Lesson(lessonID=3.0.0, lessonSubject=Prakt. Datenbanksyst., lessonTeacher=Grabowski, H., lessonRoom=D115, B204b, lessonRemark=)], [Lesson(lessonID=3.1.0, lessonSubject=Zusatzübung C++, lessonTeacher=Behr, A., lessonRoom=D116, lessonRemark=), Lesson(lessonID=3.1.1, lessonSubject=Prakt. Datenbanksyst., lessonTeacher=Grabowski, H., lessonRoom=D115, B204b, lessonRemark=)], [Lesson(lessonID=3.2.0, lessonSubject=Softw. Engin. 1, lessonTeacher=Dorer, K., lessonRoom=B013, lessonRemark=)], [Lesson(lessonID=3.3.0, lessonSubject=, lessonTeacher=, lessonRoom=, lessonRemark=)], [Lesson(lessonID=3.4.0, lessonSubject=, lessonTeacher=, lessonRoom=, lessonRemark=)], [Lesson(lessonID=3.5.0, lessonSubject=, lessonTeacher=, lessonRoom=, lessonRemark=)]]), TimetableDay(timeslots=[[Lesson(lessonID=4.0.0, lessonSubject=Computernetze 1, lessonTeacher=Mayer E., lessonRoom=A213, lessonRemark=)], [Lesson(lessonID=4.1.0, lessonSubject=Internet-Programmierung, lessonTeacher=Orb, J., lessonRoom=A213, lessonRemark=)], [Lesson(lessonID=4.2.0, lessonSubject=Prakt. Systemprogr., lessonTeacher=Mayer E., lessonRoom=STB 0.28, lessonRemark=)], [Lesson(lessonID=4.3.0, lessonSubject=Prakt. Systemprogr., lessonTeacher=Mayer E., lessonRoom=STB 0.28, lessonRemark=)], [Lesson(lessonID=4.4.0, lessonSubject=, lessonTeacher=, lessonRoom=, lessonRemark=)], [Lesson(lessonID=4.5.0, lessonSubject=, lessonTeacher=, lessonRoom=, lessonRemark=)]]), TimetableDay(timeslots=[[Lesson(lessonID=5.0.0, lessonSubject=, lessonTeacher=, lessonRoom=, lessonRemark=)], [Lesson(lessonID=5.1.0, lessonSubject=, lessonTeacher=, lessonRoom=, lessonRemark=)], [Lesson(lessonID=5.2.0, lessonSubject=, lessonTeacher=, lessonRoom=, lessonRemark=)], [Lesson(lessonID=5.3.0, lessonSubject=, lessonTeacher=, lessonRoom=, lessonRemark=)], [Lesson(lessonID=5.4.0, lessonSubject=, lessonTeacher=, lessonRoom=, lessonRemark=)], [Lesson(lessonID=5.5.0, lessonSubject=, lessonTeacher=, lessonRoom=, lessonRemark=)]])])

1189
src/test/resources/html/Timetable_empty-week.html

File diff suppressed because one or more lines are too long
Loading…
Cancel
Save

Du besuchst diese Seite mit einem veralteten IPv4-Internetzugang. Möglicherweise treten in Zukunft Probleme mit der Erreichbarkeit und Performance auf. Bitte frage deinen Internetanbieter oder Netzwerkadministrator nach IPv6-Unterstützung.
You are visiting this site with an outdated IPv4 internet access. You may experience problems with accessibility and performance in the future. Please ask your ISP or network administrator for IPv6 support.
Weitere Infos | More Information
Klicke zum schließen | Click to close