-
Notifications
You must be signed in to change notification settings - Fork 10
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
feat: add support for the format parameter #1299
base: develop
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -480,6 +480,47 @@ class EntityHandlerTests { | |
) | ||
} | ||
|
||
@Test | ||
fun `get entity by id should correctly return the representation asked in the format parameter`() { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. better add one or more tests on the |
||
initializeRetrieveEntityMocks() | ||
coEvery { entityQueryService.queryEntity(any(), MOCK_USER_SUB) } returns ExpandedEntity( | ||
mapOf( | ||
"@id" to beehiveId.toString(), | ||
"@type" to listOf("Beehive"), | ||
"https://uri.etsi.org/ngsi-ld/default-context/prop1" to mapOf( | ||
JSONLD_TYPE to NGSILD_PROPERTY_TYPE.uri, | ||
NGSILD_PROPERTY_VALUE to mapOf( | ||
JSONLD_VALUE to "some value" | ||
) | ||
), | ||
"https://uri.etsi.org/ngsi-ld/default-context/rel1" to mapOf( | ||
JSONLD_TYPE to NGSILD_RELATIONSHIP_TYPE.uri, | ||
NGSILD_RELATIONSHIP_OBJECT to mapOf( | ||
JSONLD_ID to "urn:ngsi-ld:Entity:1234" | ||
) | ||
) | ||
) | ||
).right() | ||
|
||
webClient.get() | ||
.uri("/ngsi-ld/v1/entities/$beehiveId?format=keyValues&options=normalized") | ||
.header(HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_JSON_VALUE) | ||
.exchange() | ||
.expectStatus().isOk | ||
.expectBody() | ||
.json( | ||
""" | ||
{ | ||
"id": "$beehiveId", | ||
"type": "Beehive", | ||
"prop1": "some value", | ||
"rel1": "urn:ngsi-ld:Entity:1234", | ||
"@context": "${applicationProperties.contexts.core}" | ||
} | ||
""".trimIndent() | ||
) | ||
} | ||
|
||
@Test | ||
fun `get entity by id should return 404 if the entity has none of the requested attributes`() { | ||
initializeRetrieveEntityMocks() | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,6 @@ | ||
package com.egm.stellio.shared.model | ||
|
||
import com.egm.stellio.shared.queryparameter.FormatValue | ||
import com.egm.stellio.shared.queryparameter.OptionsValue | ||
import com.egm.stellio.shared.queryparameter.QueryParameter | ||
import com.egm.stellio.shared.util.GEO_JSON_MEDIA_TYPE | ||
|
@@ -28,9 +29,14 @@ data class NgsiLdDataRepresentation( | |
acceptMediaType: MediaType | ||
): NgsiLdDataRepresentation { | ||
val optionsParam = queryParams.getOrDefault(QueryParameter.OPTIONS.key, emptyList()) | ||
val formatParam = queryParams.getOrDefault(QueryParameter.FORMAT.key, emptyList()) | ||
val attributeRepresentation = when { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. since you are in it and since it is very related, also add support for the |
||
formatParam.contains(FormatValue.KEY_VALUES.value) -> AttributeRepresentation.SIMPLIFIED | ||
formatParam.contains(FormatValue.NORMALIZED.value) -> AttributeRepresentation.NORMALIZED | ||
optionsParam.contains(FormatValue.KEY_VALUES.value) -> AttributeRepresentation.SIMPLIFIED | ||
else -> AttributeRepresentation.NORMALIZED | ||
} | ||
val includeSysAttrs = optionsParam.contains(OptionsValue.SYS_ATTRS.value) | ||
val attributeRepresentation = optionsParam.contains(OptionsValue.KEY_VALUES.value) | ||
.let { if (it) AttributeRepresentation.SIMPLIFIED else AttributeRepresentation.NORMALIZED } | ||
val languageFilter = queryParams.getFirst(QueryParameter.LANG.key) | ||
val entityRepresentation = EntityRepresentation.forMediaType(acceptMediaType) | ||
val geometryProperty = | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
package com.egm.stellio.shared.queryparameter | ||
|
||
enum class FormatValue(val value: String) { | ||
KEY_VALUES("keyValues"), | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. don't forget to remove it in the |
||
NORMALIZED("normalized"), | ||
TEMPORAL_VALUES("temporalValues"), | ||
AGGREGATED_VALUES("aggregatedValues") | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -168,16 +168,16 @@ internal fun canExpandJsonLdKeyFromCore(contexts: List<String>): Boolean { | |
return expandedType == NGSILD_DATASET_ID_PROPERTY | ||
} | ||
|
||
enum class OptionsParamValue(val value: String) { | ||
enum class QueryParamValue(val value: String) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
TEMPORAL_VALUES("temporalValues"), | ||
AUDIT("audit"), | ||
AGGREGATED_VALUES("aggregatedValues") | ||
} | ||
|
||
fun hasValueInOptionsParam(options: Optional<String>, optionValue: OptionsParamValue): Boolean = | ||
fun hasValueInQueryParam(options: Optional<String>, queryParamValue: QueryParamValue): Boolean = | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
options | ||
.map { it.split(",") } | ||
.filter { it.any { option -> option == optionValue.value } } | ||
.filter { it.any { option -> option == queryParamValue.value } } | ||
.isPresent | ||
|
||
fun parseQueryParameter(queryParam: String?): Set<String> = | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,7 +2,7 @@ package com.egm.stellio.shared.util | |
|
||
import com.egm.stellio.shared.config.ApplicationProperties | ||
import com.egm.stellio.shared.model.BadRequestDataException | ||
import com.egm.stellio.shared.util.OptionsParamValue.TEMPORAL_VALUES | ||
import com.egm.stellio.shared.util.QueryParamValue.TEMPORAL_VALUES | ||
import com.egm.stellio.shared.web.CustomWebFilter | ||
import io.mockk.every | ||
import io.mockk.mockk | ||
|
@@ -33,27 +33,27 @@ class ApiUtilsTests { | |
|
||
@Test | ||
fun `it should not find a value if there is no options query param`() { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. also update the names of the test functions ( |
||
assertFalse(hasValueInOptionsParam(Optional.empty(), TEMPORAL_VALUES)) | ||
assertFalse(hasValueInQueryParam(Optional.empty(), TEMPORAL_VALUES)) | ||
} | ||
|
||
@Test | ||
fun `it should not find a value if it is not in a single value options query param`() { | ||
assertFalse(hasValueInOptionsParam(Optional.of("one"), TEMPORAL_VALUES)) | ||
assertFalse(hasValueInQueryParam(Optional.of("one"), TEMPORAL_VALUES)) | ||
} | ||
|
||
@Test | ||
fun `it should not find a value if it is not in a multi value options query param`() { | ||
assertFalse(hasValueInOptionsParam(Optional.of("one,two"), TEMPORAL_VALUES)) | ||
assertFalse(hasValueInQueryParam(Optional.of("one,two"), TEMPORAL_VALUES)) | ||
} | ||
|
||
@Test | ||
fun `it should find a value if it is in a single value options query param`() { | ||
assertTrue(hasValueInOptionsParam(Optional.of("temporalValues"), TEMPORAL_VALUES)) | ||
assertTrue(hasValueInQueryParam(Optional.of("temporalValues"), TEMPORAL_VALUES)) | ||
} | ||
|
||
@Test | ||
fun `it should find a value if it is in a multi value options query param`() { | ||
assertTrue(hasValueInOptionsParam(Optional.of("one,temporalValues"), TEMPORAL_VALUES)) | ||
assertTrue(hasValueInQueryParam(Optional.of("one,temporalValues"), TEMPORAL_VALUES)) | ||
} | ||
|
||
@Test | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
you could take this opportunity to return an
APIException
if bothtemporalValues
andaggregatedValues
are provided