Skip to content

Commit

Permalink
Merge pull request EventFahrplan#621 from EventFahrplan/malformed-sch…
Browse files Browse the repository at this point in the history
…edule-data

Continue to display parsing error when schedule data cannot be parsed.
  • Loading branch information
johnjohndoe authored Feb 15, 2024
2 parents 988abb9 + 0a5eaa5 commit 97ac191
Show file tree
Hide file tree
Showing 5 changed files with 45 additions and 17 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -362,10 +362,11 @@ object AppRepository {
val parsingStatus = if (meta.numDays == 0) InitialParsing else Parsing
mutableLoadScheduleState.tryEmit(parsingStatus)
parseSchedule(
fetchScheduleResult.scheduleXml,
fetchScheduleResult.httpHeader,
onParsingDone,
onLoadingShiftsDone
scheduleXml = fetchScheduleResult.scheduleXml,
httpHeader = fetchScheduleResult.httpHeader,
oldMeta = meta,
onParsingDone = onParsingDone,
onLoadingShiftsDone = onLoadingShiftsDone
)
} else if (fetchResult.isNotModified) {
loadShifts(onLoadingShiftsDone)
Expand All @@ -375,6 +376,7 @@ object AppRepository {

private fun parseSchedule(scheduleXml: String,
httpHeader: HttpHeader,
oldMeta: Meta,
onParsingDone: (parseScheduleResult: ParseResult) -> Unit,
onLoadingShiftsDone: (loadShiftsResult: LoadShiftsResult) -> Unit) {
scheduleNetworkRepository.parseSchedule(scheduleXml, httpHeader,
Expand All @@ -391,9 +393,12 @@ object AppRepository {
val validMeta = meta.validate()
updateMeta(validMeta)
},
onParsingDone = { result: Boolean, version: String ->
val parseResult = ParseScheduleResult(result, version)
val parseScheduleStatus = if (result) ParseSuccess else ParseFailure(parseResult)
onParsingDone = { isSuccess: Boolean, version: String ->
if (!isSuccess) {
updateMeta(oldMeta.copy(httpHeader = HttpHeader(eTag = "", lastModified = "")))
}
val parseResult = ParseScheduleResult(isSuccess, version)
val parseScheduleStatus = if (isSuccess) ParseSuccess else ParseFailure(parseResult)
mutableLoadScheduleState.tryEmit(parseScheduleStatus)
onParsingDone(parseResult)
loadShifts(onLoadingShiftsDone)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -298,6 +298,29 @@ class AppRepositoryLoadAndParseScheduleTest {
testableAppRepository.loadScheduleState.test {
assertThat(awaitItem()).isEqualTo(ParseFailure(ParseScheduleResult(false, "1.0.0")))
}
// Reset ETag &b Last-Modified if parsing failed
verify(metaDatabaseRepository, times(2)).insert(any())
}

@Test
fun `loadScheduleState emits ParseFailure when initial parsing finished with an error`() =
runTest {
whenever(metaDatabaseRepository.query()) doReturn DatabaseMeta(numDays = 0)
val success = createFetchScheduleResult(NetworkHttpStatus.HTTP_OK)
val onParsingDone: OnParsingDone = { result ->
assertThat(result).isEqualTo(ParseScheduleResult(isSuccess = false, "1.0.0"))
}
testableAppRepository.loadSchedule(isUserRequest = false, onParsingDone = onParsingDone)
scheduleNetworkRepository.onFetchScheduleFinished(success)

// onParsingDone
whenever(sharedPreferencesRepository.getEngelsystemShiftsUrl()) doReturn EMPTY_ENGELSYSTEM_URL // early exit to bypass here
scheduleNetworkRepository.onParsingDone(false, "1.0.0")
testableAppRepository.loadScheduleState.test {
assertThat(awaitItem()).isEqualTo(ParseFailure(ParseScheduleResult(false, "1.0.0")))
}
// Reset ETag &b Last-Modified if parsing failed
verify(metaDatabaseRepository, times(2)).insert(any())
}

private fun createFetchScheduleResult(httpStatus: NetworkHttpStatus) =
Expand Down Expand Up @@ -351,7 +374,7 @@ class AppRepositoryLoadAndParseScheduleTest {
httpHeader: HttpHeader,
onUpdateSessions: (sessions: List<NetworkSession>) -> Unit,
onUpdateMeta: (meta: NetworkMeta) -> Unit,
onParsingDone: (result: Boolean, version: String) -> Unit
onParsingDone: (isSuccess: Boolean, version: String) -> Unit
) {
this.onUpdateSessions = onUpdateSessions
this.onUpdateMeta = onUpdateMeta
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,11 +30,11 @@ class RealScheduleNetworkRepository(
httpHeader: HttpHeader,
onUpdateSessions: (sessions: List<Session>) -> Unit,
onUpdateMeta: (meta: Meta) -> Unit,
onParsingDone: (result: Boolean, version: String) -> Unit) {
onParsingDone: (isSuccess: Boolean, version: String) -> Unit) {
parser.setListener(object : FahrplanParser.OnParseCompleteListener {
override fun onUpdateSessions(sessions: List<Session>) = onUpdateSessions.invoke(sessions)
override fun onUpdateMeta(meta: Meta) = onUpdateMeta.invoke(meta)
override fun onParseDone(result: Boolean, version: String) = onParsingDone.invoke(result, version)
override fun onParseDone(isSuccess: Boolean, version: String) = onParsingDone.invoke(isSuccess, version)
})
parser.parse(scheduleXml, httpHeader)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,6 @@ interface ScheduleNetworkRepository {
httpHeader: HttpHeader,
onUpdateSessions: (sessions: List<Session>) -> Unit,
onUpdateMeta: (meta: Meta) -> Unit,
onParsingDone: (result: Boolean, version: String) -> Unit)
onParsingDone: (isSuccess: Boolean, version: String) -> Unit)

}
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ public interface OnParseCompleteListener {

void onUpdateMeta(@NonNull Meta meta);

void onParseDone(Boolean result, String version);
void onParseDone(Boolean isSuccess, String version);
}

@NonNull
Expand Down Expand Up @@ -80,7 +80,7 @@ class ParserTask extends AsyncTask<String, Void, Boolean> {

private boolean completed;

private boolean result;
private boolean isSuccess;

ParserTask(@NonNull Logging logging, FahrplanParser.OnParseCompleteListener listener) {
this.logging = logging;
Expand Down Expand Up @@ -109,17 +109,17 @@ protected Boolean doInBackground(String... args) {
}

private void notifyActivity() {
if (result) {
if (isSuccess) {
listener.onUpdateMeta(meta);
listener.onUpdateSessions(sessions);
}
listener.onParseDone(result, meta.getVersion());
listener.onParseDone(isSuccess, meta.getVersion());
completed = false;
}

protected void onPostExecute(Boolean result) {
protected void onPostExecute(Boolean isSuccess) {
completed = true;
this.result = result;
this.isSuccess = isSuccess;

if (listener != null) {
notifyActivity();
Expand Down

0 comments on commit 97ac191

Please sign in to comment.