Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Show parsing error when receiving empty form list #5526

Merged
merged 3 commits into from
Mar 28, 2023
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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,24 @@ 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 doc = StringReader("").use { reader ->
seadowg marked this conversation as resolved.
Show resolved Hide resolved
val parser = KXmlParser()
parser.setInput(reader)
parser.setFeature(XmlPullParser.FEATURE_PROCESS_NAMESPACES, true)
Document().also { it.parse(parser) }
}

val formList = OpenRosaResponseParserImpl().parseFormList(doc)
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 +50,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 +72,17 @@ class OpenRosaResponseParserImplTest {
val mediaFiles = OpenRosaResponseParserImpl().parseManifest(doc)
assertThat(mediaFiles, equalTo(null))
}

@Test
fun `parseManifest() when document is empty, returns null`() {
val doc = StringReader("").use { reader ->
seadowg marked this conversation as resolved.
Show resolved Hide resolved
val parser = KXmlParser()
parser.setInput(reader)
parser.setFeature(XmlPullParser.FEATURE_PROCESS_NAMESPACES, true)
Document().also { it.parse(parser) }
}

val formList = OpenRosaResponseParserImpl().parseManifest(doc)
assertThat(formList, equalTo(null))
}
}