Skip to content

Commit

Permalink
feat: handle @none for default values
Browse files Browse the repository at this point in the history
  • Loading branch information
bobeal committed Apr 7, 2024
1 parent 7a28cf1 commit 05822fb
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 26 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,10 @@ private fun filterLanguageProperty(value: Map<String, Any>, transformationParame
val localeRanges = Locale.LanguageRange.parse(transformationParameters?.get(QUERY_PARAM_LANG)!!)
val propertyLocales = (value[JSONLD_LANGUAGEMAP_TERM] as Map<String, Any>).keys.sorted()
val bestLocaleMatch = Locale.filterTags(localeRanges, propertyLocales)
.getOrElse(0) { _ -> propertyLocales.first() }
.getOrElse(0) { _ ->
// as the list is sorted, @none is the first in the list if it exists
propertyLocales.first()
}
mapOf(
JSONLD_TYPE_TERM to NGSILD_PROPERTY_TERM,
JSONLD_VALUE_TERM to (value[JSONLD_LANGUAGEMAP_TERM] as Map<String, Any>)[bestLocaleMatch],
Expand Down
28 changes: 16 additions & 12 deletions shared/src/main/kotlin/com/egm/stellio/shared/model/NgsiLdEntity.kt
Original file line number Diff line number Diff line change
Expand Up @@ -325,11 +325,11 @@ class NgsiLdJsonPropertyInstance private constructor(
): Either<APIException, NgsiLdJsonPropertyInstance> = either {
val json = values.getMemberValue(NGSILD_JSONPROPERTY_VALUE)
ensureNotNull(json) {
BadRequestDataException("Property $name has an instance without a json member")
BadRequestDataException("JsonProperty $name has an instance without a json member")
}
ensure(json is Map<*, *> || (json is List<*> && json.all { it is Map<*, *> })) {
BadRequestDataException(
"Property $name has a json member that is not a JSON object, nor an array of JSON objects"
"JsonProperty $name has a json member that is not a JSON object, nor an array of JSON objects"
)
}

Expand Down Expand Up @@ -366,10 +366,10 @@ class NgsiLdLanguagePropertyInstance private constructor(
): Either<APIException, NgsiLdLanguagePropertyInstance> = either {
val languageMap = values[NGSILD_LANGUAGEPROPERTY_VALUE]
ensureNotNull(languageMap) {
BadRequestDataException("Property $name has an instance without a languageMap member")
BadRequestDataException("LanguageProperty $name has an instance without a languageMap member")
}
ensure(isValidLanguageMap(languageMap)) {
BadRequestDataException("Property $name has an invalid languageMap member")
BadRequestDataException("LanguageProperty $name has an invalid languageMap member")
}

val observedAt = values.getMemberValueAsDateTime(NGSILD_OBSERVED_AT_PROPERTY)
Expand All @@ -391,16 +391,20 @@ class NgsiLdLanguagePropertyInstance private constructor(
private fun isValidLanguageMap(languageMap: List<Any>): Boolean =
languageMap.all {
it is Map<*, *> &&
it.size == 2 &&
(it.containsKey(JSONLD_VALUE) || it.containsKey(JSONLD_LANGUAGE)) &&
it.values.all { value -> value is String } &&
isValidLanguageTag(it[JSONLD_LANGUAGE] as String)
isValidStructure(it) &&
isValidLangValue(it.values) &&
isValidLanguageTag(it[JSONLD_LANGUAGE] as? String)
}

private fun isValidLanguageTag(tag: String): Boolean {
val locale = Locale.forLanguageTag(tag)
return tag == locale.toLanguageTag()
}
private fun isValidStructure(langEntry: Map<*, *>): Boolean =
(langEntry.size == 2 && langEntry.containsKey(JSONLD_VALUE) && langEntry.containsKey(JSONLD_LANGUAGE)) ||
(langEntry.size == 1 && langEntry.containsKey(JSONLD_VALUE))

private fun isValidLangValue(values: Collection<Any?>): Boolean =
values.all { value -> value is String || value is List<*> }

private fun isValidLanguageTag(tag: String?): Boolean =
tag == null || "und" != Locale.forLanguageTag(tag).toLanguageTag()
}

override fun toString(): String = "NgsiLdLanguagePropertyInstance(languageMap=$languageMap)"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,8 @@ class LanguageFilterTests {
"""
"languageProperty": {
"type": "Property",
"value": "Grand Place",
"lang": "fr"
"value": "Big Place",
"lang": "@none"
}
""".trimIndent()
),
Expand All @@ -40,8 +40,8 @@ class LanguageFilterTests {
"""
"languageProperty": {
"type": "Property",
"value": "Grand Place",
"lang": "fr"
"value": "Big Place",
"lang": "@none"
}
""".trimIndent()
),
Expand Down Expand Up @@ -103,7 +103,8 @@ class LanguageFilterTests {
"type": "LanguageProperty",
"languageMap": {
"fr": "Grand Place",
"nl": "Grote Markt"
"nl": "Grote Markt",
"@none": "Big Place"
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -829,7 +829,7 @@ class NgsiLdEntityTests {
.shouldFail {
assertInstanceOf(BadRequestDataException::class.java, it)
assertEquals(
"Property ${NGSILD_DEFAULT_VOCAB}jsonProperty has an instance without a json member",
"JsonProperty ${NGSILD_DEFAULT_VOCAB}jsonProperty has an instance without a json member",
it.message
)
}
Expand All @@ -853,7 +853,7 @@ class NgsiLdEntityTests {
.shouldFail {
assertInstanceOf(BadRequestDataException::class.java, it)
assertEquals(
"Property ${NGSILD_DEFAULT_VOCAB}jsonProperty has a json member that is not a JSON object, " +
"JsonProperty ${NGSILD_DEFAULT_VOCAB}jsonProperty has a json member that is not a JSON object, " +
"nor an array of JSON objects",
it.message
)
Expand All @@ -870,8 +870,9 @@ class NgsiLdEntityTests {
"languageProperty": {
"type": "LanguageProperty",
"languageMap": {
"fr": "Grand Place",
"nl": "Grote Markt"
"fr": ["Grand Place", "Grande Place"],
"nl": "Grote Markt",
"@none": "Big Place"
}
}
}
Expand All @@ -891,10 +892,17 @@ class NgsiLdEntityTests {
"@language" to "fr",
"@value" to "Grand Place"
),
mapOf(
"@language" to "fr",
"@value" to "Grande Place"
),
mapOf(
"@language" to "nl",
"@value" to "Grote Markt"
),
mapOf(
"@value" to "Big Place"
)
),
languagePropertyInstance.languageMap
)
Expand All @@ -920,7 +928,8 @@ class NgsiLdEntityTests {
.shouldFail {
assertInstanceOf(BadRequestDataException::class.java, it)
assertEquals(
"Property ${NGSILD_DEFAULT_VOCAB}languageProperty has an instance without a languageMap member",
"LanguageProperty ${NGSILD_DEFAULT_VOCAB}languageProperty has an instance " +
"without a languageMap member",
it.message
)
}
Expand All @@ -947,7 +956,7 @@ class NgsiLdEntityTests {
.shouldFail {
assertInstanceOf(BadRequestDataException::class.java, it)
assertEquals(
"Property ${NGSILD_DEFAULT_VOCAB}languageProperty has an invalid languageMap member",
"LanguageProperty ${NGSILD_DEFAULT_VOCAB}languageProperty has an invalid languageMap member",
it.message
)
}
Expand All @@ -963,7 +972,7 @@ class NgsiLdEntityTests {
"languageProperty": {
"type": "LanguageProperty",
"languageMap": {
"invalid-lang-tag": "Grand Place"
"123": "Grand Place"
}
}
}
Expand All @@ -973,7 +982,7 @@ class NgsiLdEntityTests {
.shouldFail {
assertInstanceOf(BadRequestDataException::class.java, it)
assertEquals(
"Property ${NGSILD_DEFAULT_VOCAB}languageProperty has an invalid languageMap member",
"LanguageProperty ${NGSILD_DEFAULT_VOCAB}languageProperty has an invalid languageMap member",
it.message
)
}
Expand Down

0 comments on commit 05822fb

Please sign in to comment.