-
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 5 commits
873b29e
7863668
98144cc
98e6891
06b7ec4
e89b116
7e46c4e
a9fe227
e4d3857
29fb4b9
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 |
---|---|---|
|
@@ -20,9 +20,10 @@ import com.egm.stellio.search.temporal.model.TemporalQuery.Timerel | |
import com.egm.stellio.shared.config.ApplicationProperties | ||
import com.egm.stellio.shared.model.APIException | ||
import com.egm.stellio.shared.model.BadRequestDataException | ||
import com.egm.stellio.shared.queryparameter.FormatValue | ||
import com.egm.stellio.shared.queryparameter.QueryParameter | ||
import com.egm.stellio.shared.util.OptionsParamValue | ||
import com.egm.stellio.shared.util.hasValueInOptionsParam | ||
import com.egm.stellio.shared.util.QueryParamValue | ||
import com.egm.stellio.shared.util.hasValueInQueryParam | ||
import com.egm.stellio.shared.util.parseTimeParameter | ||
import org.springframework.util.MultiValueMap | ||
import org.springframework.util.MultiValueMapAdapter | ||
|
@@ -42,21 +43,29 @@ fun composeTemporalEntitiesQueryFromGet( | |
requestParams, | ||
contexts | ||
).bind() | ||
|
||
if (inQueryEntities) | ||
var withTemporalValues = false | ||
var withAggregatedValues = false | ||
if (inQueryEntities) { | ||
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. why did you add enclosing 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. detekt automatic fix |
||
entitiesQueryFromGet.validateMinimalQueryEntitiesParameters().bind() | ||
|
||
val withTemporalValues = hasValueInOptionsParam( | ||
Optional.ofNullable(requestParams.getFirst(QueryParameter.OPTIONS.key)), | ||
OptionsParamValue.TEMPORAL_VALUES | ||
) | ||
val withAudit = hasValueInOptionsParam( | ||
Optional.ofNullable(requestParams.getFirst(QueryParameter.OPTIONS.key)), | ||
OptionsParamValue.AUDIT | ||
) | ||
val withAggregatedValues = hasValueInOptionsParam( | ||
} | ||
val optionsParam = Optional.ofNullable(requestParams.getFirst(QueryParameter.OPTIONS.key)) | ||
val formatParam = requestParams.getFirst(QueryParameter.FORMAT.key) | ||
when (formatParam) { | ||
FormatValue.TEMPORAL_VALUES.value -> withTemporalValues = true | ||
FormatValue.AGGREGATED_VALUES.value -> withAggregatedValues = true | ||
else -> { | ||
val hasTemporal = hasValueInQueryParam(optionsParam, QueryParamValue.TEMPORAL_VALUES) | ||
val hasAggregated = hasValueInQueryParam(optionsParam, QueryParamValue.AGGREGATED_VALUES) | ||
if (hasTemporal && hasAggregated) { | ||
return BadRequestDataException("Only one temporal representation can be present").left() | ||
} | ||
withTemporalValues = hasTemporal | ||
withAggregatedValues = hasAggregated | ||
} | ||
} | ||
val withAudit = hasValueInQueryParam( | ||
Optional.ofNullable(requestParams.getFirst(QueryParameter.OPTIONS.key)), | ||
OptionsParamValue.AGGREGATED_VALUES | ||
QueryParamValue.AUDIT | ||
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. it should use |
||
) | ||
val temporalQuery = | ||
buildTemporalQuery(requestParams, defaultPagination, inQueryEntities, withAggregatedValues).bind() | ||
|
@@ -83,17 +92,17 @@ fun composeTemporalEntitiesQueryFromPost( | |
contexts | ||
).bind() | ||
|
||
val withTemporalValues = hasValueInOptionsParam( | ||
val withTemporalValues = hasValueInQueryParam( | ||
Optional.ofNullable(requestParams.getFirst(QueryParameter.OPTIONS.key)), | ||
OptionsParamValue.TEMPORAL_VALUES | ||
QueryParamValue.TEMPORAL_VALUES | ||
) | ||
val withAudit = hasValueInOptionsParam( | ||
val withAudit = hasValueInQueryParam( | ||
Optional.ofNullable(requestParams.getFirst(QueryParameter.OPTIONS.key)), | ||
OptionsParamValue.AUDIT | ||
QueryParamValue.AUDIT | ||
) | ||
val withAggregatedValues = hasValueInOptionsParam( | ||
val withAggregatedValues = hasValueInQueryParam( | ||
Optional.ofNullable(requestParams.getFirst(QueryParameter.OPTIONS.key)), | ||
OptionsParamValue.AGGREGATED_VALUES | ||
QueryParamValue.AGGREGATED_VALUES | ||
) | ||
|
||
val temporalParams = mapOf( | ||
|
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 |
---|---|---|
@@ -0,0 +1,9 @@ | ||
package com.egm.stellio.shared.queryparameter | ||
|
||
enum class FormatValue(val value: String) { | ||
KEY_VALUES("keyValues"), | ||
bobeal marked this conversation as resolved.
Show resolved
Hide resolved
|
||
SIMPLIFIED("simplified"), | ||
NORMALIZED("normalized"), | ||
TEMPORAL_VALUES("temporalValues"), | ||
AGGREGATED_VALUES("aggregatedValues") | ||
bobeal marked this conversation as resolved.
Show resolved
Hide resolved
|
||
} |
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 = | ||
options | ||
fun hasValueInQueryParam(queryParam: Optional<String>, queryParamValue: QueryParamValue): Boolean = | ||
queryParam | ||
.map { it.split(",") } | ||
.filter { it.any { option -> option == optionValue.value } } | ||
.filter { it.any { option -> option == queryParamValue.value } } | ||
.isPresent | ||
|
||
fun parseQueryParameter(queryParam: String?): Set<String> = | ||
|
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.
instead of using these 2 booleans, it will be cleaner to switch to an enum similar to the
AttributeRepresentation
one