Skip to content

Commit

Permalink
Merge pull request #5526 from seadowg/empty-response
Browse files Browse the repository at this point in the history
Show parsing error when receiving empty form list
  • Loading branch information
grzesiek2010 authored Mar 28, 2023
2 parents 1db696c + 869b33b commit 39e7310
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,17 @@ import org.kxml2.kdom.Element
import org.odk.collect.forms.FormListItem
import org.odk.collect.forms.MediaFile
import org.odk.collect.shared.strings.StringUtils.isBlank
import java.util.ArrayList

class OpenRosaResponseParserImpl : OpenRosaResponseParser {

override fun parseFormList(document: Document): List<FormListItem>? {
// Attempt OpenRosa 1.0 parsing
val xformsElement = document.rootElement
val xformsElement = try {
document.rootElement
} catch (e: RuntimeException) {
return null
}

if (xformsElement.name != "xforms") {
return null
}
Expand Down Expand Up @@ -122,7 +126,12 @@ class OpenRosaResponseParserImpl : OpenRosaResponseParser {

override fun parseManifest(document: Document): List<MediaFile>? {
// Attempt OpenRosa 1.0 parsing
val manifestElement = document.rootElement
val manifestElement = try {
document.rootElement
} catch (e: RuntimeException) {
return null
}

if (manifestElement.name != "manifest") {
return null
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,17 @@ import org.kxml2.io.KXmlParser
import org.kxml2.kdom.Document
import org.xmlpull.v1.XmlPullParser
import java.io.StringReader
import java.lang.StringBuilder

class OpenRosaResponseParserImplTest {

@Test
fun `when xform hash is missing prefix, parseFormList returns null hash for item`() {
fun `parseFormList() when document is empty, returns null`() {
val formList = OpenRosaResponseParserImpl().parseFormList(Document())
assertThat(formList, equalTo(null))
}

@Test
fun `parseFormList() when xform hash is missing prefix, returns null hash for item`() {
val response = StringBuilder()
.appendLine("<?xml version='1.0' encoding='UTF-8' ?>")
.appendLine("<xforms xmlns=\"http://openrosa.org/xforms/xformsList\">")
Expand All @@ -38,7 +43,7 @@ class OpenRosaResponseParserImplTest {
}

@Test
fun `when media file hash is empty, parseManifest returns null`() {
fun `parseManifest() when media file hash is empty, returns null`() {
val response = StringBuilder()
.appendLine("<?xml version='1.0' encoding='UTF-8' ?>")
.appendLine("<manifest xmlns=\"http://openrosa.org/xforms/xformsManifest\">")
Expand All @@ -60,4 +65,10 @@ class OpenRosaResponseParserImplTest {
val mediaFiles = OpenRosaResponseParserImpl().parseManifest(doc)
assertThat(mediaFiles, equalTo(null))
}

@Test
fun `parseManifest() when document is empty, returns null`() {
val formList = OpenRosaResponseParserImpl().parseManifest(Document())
assertThat(formList, equalTo(null))
}
}

0 comments on commit 39e7310

Please sign in to comment.