added junit test for MensaParser

* updated kotlin 1.3.31 -> 1.3.41
* updated spring boot 2.1.5 -> 2.1.7
* updated coroutines 1.2.1 -> 1.2.2
* reworked MensaParser to make it testable
This commit is contained in:
2019-08-16 13:59:44 +02:00
parent e89201c951
commit a0fceccc2f
5 changed files with 730 additions and 13 deletions

View File

@ -23,6 +23,7 @@
package org.mosad.thecitadelofricks.hsoparser
import org.jsoup.Jsoup
import org.jsoup.nodes.Document
import org.mosad.thecitadelofricks.Meal
import org.mosad.thecitadelofricks.MensaWeek
import org.slf4j.LoggerFactory
@ -33,16 +34,33 @@ class MensaParser {
var logger: org.slf4j.Logger = LoggerFactory.getLogger(MensaParser::class.java)
/**
* returns the mensa menu for a week
* returns the mensa's menu for a week
* @param mensaMenuURL the url to a mensa menu (swfr)
* @return the menu plan found at menuURL or null if the request was not successful
* @return the mensa's menu as MensaWeek found at menuURL or null if the request was not successful
*/
fun getMensaMenu(mensaMenuURL: String): MensaWeek? {
val mealWeekList = MensaWeek()
try {
return try {
val menuHTML = Jsoup.connect(mensaMenuURL).timeout(15000).get()
parseMensaMenu(menuHTML)
} catch (ex: SocketTimeoutException) {
logger.warn("timeout from $mensaMenuURL, updating on next attempt!")
null
} catch (gex: Exception) {
logger.error("general MensaParser error", gex)
null
}
}
menuHTML.select("#speiseplan-tabs").select("div.tab-content").select("div.menu-tagesplan")
/**
* parse the mensa's menu from the html document
* @param htmlDoc the html document containing the menu
* @return the mensa's menu as MensaWeek
*/
fun parseMensaMenu(htmlDoc: Document): MensaWeek? {
val mealWeekList = MensaWeek()
try {
htmlDoc.select("#speiseplan-tabs").select("div.tab-content").select("div.menu-tagesplan")
.forEachIndexed { dayIndex, day ->
val strDay = day.select("h3").text()
@ -56,11 +74,8 @@ class MensaParser {
}
}
} catch (ex: SocketTimeoutException) {
logger.warn("timeout from $mensaMenuURL, updating on next attempt!")
return null
} catch (gex: Exception) {
logger.error("general MensaParser error", gex)
} catch (pex: Exception) {
logger.error("error while parsing the html file", pex)
return null
}