Skip to content

Commit

Permalink
add more tests
Browse files Browse the repository at this point in the history
  • Loading branch information
CollinBeczak committed Aug 29, 2024
1 parent 241f7b6 commit f22a819
Show file tree
Hide file tree
Showing 2 changed files with 93 additions and 1 deletion.
8 changes: 7 additions & 1 deletion app/org/maproulette/provider/ChallengeProvider.scala
Original file line number Diff line number Diff line change
Expand Up @@ -295,7 +295,13 @@ class ChallengeProvider @Inject() (
* Checks for the first valid ID in the specified list of field names.
*/
def findName(value: JsValue, fields: List[String]): Option[String] = {
fields.iterator.flatMap(fieldName => getNonNullString(value, fieldName)).toList.headOption
fields.iterator
.flatMap { fieldName =>
val result = getNonNullString(value, fieldName)
result
}
.toList
.headOption
}

/**
Expand Down
86 changes: 86 additions & 0 deletions test/org/maproulette/provider/ChallengeProviderSpec.scala
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,60 @@ class ChallengeProviderSpec extends PlaySpec with MockitoSugar {
ChallengeExtra()
)

"getNonNullString" should {
"return Some string for a non-empty string field" in {
val json = Json.obj("field" -> "validString")
repository.getNonNullString(json, "field") mustEqual Some("validString")
}

"return Some string for a numeric field" in {
val json = Json.obj("field" -> 12345)
repository.getNonNullString(json, "field") mustEqual Some("12345")
}

"return None for an empty string field" in {
val json = Json.obj("field" -> "")
repository.getNonNullString(json, "field") mustEqual None
}

"return None for a null field" in {
val json = Json.obj("field" -> JsNull)
repository.getNonNullString(json, "field") mustEqual None
}

"return None for a missing field" in {
val json = Json.obj()
repository.getNonNullString(json, "field") mustEqual None
}

"return None for a non-string and non-numeric field" in {
val json = Json.obj("field" -> Json.obj("nestedField" -> "value"))
repository.getNonNullString(json, "field") mustEqual None
}
}

"findName" should {
"return the first non-null, non-empty string from the list of fields" in {
val json = Json.obj("field1" -> "first", "field2" -> "second")
repository.findName(json, List("field1", "field2")) mustEqual Some("first")
}

"return None if none of the fields have valid values" in {
val json = Json.obj("field1" -> "", "field2" -> JsNull)
repository.findName(json, List("field1", "field2")) mustEqual None
}

"return None if the list of fields is empty" in {
val json = Json.obj("field" -> "value")
repository.findName(json, List()) mustEqual None
}

"return None if the fields do not exist in the JSON" in {
val json = Json.obj()
repository.findName(json, List("field1", "field2")) mustEqual None
}
}

"featureOSMId" should {
"return OSM ID from root if present and specified in challenge" in {
val json = Json.obj("custom_osm_id" -> "singleFeatureId")
Expand Down Expand Up @@ -88,6 +142,22 @@ class ChallengeProviderSpec extends PlaySpec with MockitoSugar {
Json.obj("properties" -> Json.obj("custom_osm_id" -> null, "fillerProperty" -> "string"))
repository.featureOSMId(json, challengeWithoutOsmId) mustEqual None
}

"return None if features array is empty and challenge specifies OSM ID property" in {
val json = Json.obj("features" -> Json.arr())
repository.featureOSMId(json, challengeWithOsmId) mustEqual None
}

"return OSM ID from nested features if specified in challenge" in {
val json = Json.obj(
"features" -> Json.arr(
Json.obj(
"properties" -> Json.obj("custom_osm_id" -> "nestedFeatureId1")
)
)
)
repository.featureOSMId(json, challengeWithOsmId) mustEqual Some("nestedFeatureId1")
}
}

"taskNameFromJsValue" should {
Expand Down Expand Up @@ -136,5 +206,21 @@ class ChallengeProviderSpec extends PlaySpec with MockitoSugar {
)
repository.taskNameFromJsValue(json, challengeWithoutOsmId) mustEqual "testName"
}

"return field from the first feature if the ID field is 'null' and challenge specifies valid feature IDs" in {
val json = Json.obj(
"features" -> Json.arr(
Json.obj("id" -> null, "properties" -> Json.obj("name" -> "featureName")),
Json.obj("id" -> null, "properties" -> Json.obj("name" -> "otherFeatureName"))
)
)
repository.taskNameFromJsValue(json, challengeWithoutOsmId) mustEqual "featureName"
}

"return a UUID if JSON features array has objects with null or empty names" in {
val json = Json.obj("features" -> Json.arr(Json.obj("name" -> ""), Json.obj("name" -> null)))
val result = repository.taskNameFromJsValue(json, challengeWithoutOsmId)
assert(UUID.fromString(result).toString == result)
}
}
}

0 comments on commit f22a819

Please sign in to comment.