Skip to content

Commit

Permalink
first draft in changing code error and adding configurable variables …
Browse files Browse the repository at this point in the history
…for pagination parameters
  • Loading branch information
ranim-n committed Apr 24, 2024
1 parent 05a83b1 commit 24a18dd
Show file tree
Hide file tree
Showing 8 changed files with 25 additions and 16 deletions.
4 changes: 4 additions & 0 deletions .env
Original file line number Diff line number Diff line change
Expand Up @@ -22,3 +22,7 @@ SUBSCRIPTION_STELLIO_URL=http://localhost:8080
APPLICATION_TENANTS_0_ISSUER=https://sso.eglobalmark.com/auth/realms/stellio
APPLICATION_TENANTS_0_NAME=urn:ngsi-ld:tenant:default
APPLICATION_TENANTS_0_DBSCHEMA=public

# Pagination config for query resources endpoints
LIMIT_DEFAULT=30
LIMIT_MAX=100
4 changes: 4 additions & 0 deletions docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,8 @@ services:
- APPLICATION_TENANTS_0_ISSUER=${APPLICATION_TENANTS_0_ISSUER}
- APPLICATION_TENANTS_0_NAME=${APPLICATION_TENANTS_0_NAME}
- APPLICATION_TENANTS_0_DBSCHEMA=${APPLICATION_TENANTS_0_DBSCHEMA}
- LIMIT_DEFAULT=${LIMIT_DEFAULT}
- LIMIT_MAX=${LIMIT_MAX}
ports:
- "8083:8083"
depends_on:
Expand All @@ -82,6 +84,8 @@ services:
- APPLICATION_TENANTS_0_DBSCHEMA=${APPLICATION_TENANTS_0_DBSCHEMA}
- SUBSCRIPTION_ENTITY-SERVICE-URL=${SUBSCRIPTION_ENTITY_SERVICE_URL}
- SUBSCRIPTION_STELLIO_URL=${SUBSCRIPTION_STELLIO_URL}
- LIMIT_DEFAULT=${LIMIT_DEFAULT}
- LIMIT_MAX=${LIMIT_MAX}
ports:
- "8084:8084"
depends_on:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1024,16 +1024,16 @@ class EntityHandlerTests {
}

@Test
fun `get entities should return 400 if limit is greater than the maximum authorized limit`() {
fun `get entities should return 403 if limit is greater than the maximum authorized limit`() {
webClient.get()
.uri("/ngsi-ld/v1/entities/?type=Beehive&limit=200&offset=1")
.exchange()
.expectStatus().isBadRequest
.expectStatus().isForbidden
.expectBody().json(
"""
{
"type":"https://uri.etsi.org/ngsi-ld/errors/BadRequestData",
"title":"The request includes input data which does not meet the requirements of the operation",
"type":"https://uri.etsi.org/ngsi-ld/errors/TooManyResults",
"title":"The query associated to the operation is producing so many results that can exhaust client or server resources. It should be made more restrictive",
"detail":"You asked for 200 results, but the supported maximum limit is 100"
}
""".trimIndent()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1027,11 +1027,11 @@ class TemporalEntityHandlerTests {
}

@Test
fun `query temporal entity should return 400 if limit is greater than the maximum authorized limit`() {
fun `query temporal entity should return 403 if limit is greater than the maximum authorized limit`() {
coEvery { authorizationService.computeAccessRightFilter(any()) } returns { null }
coEvery {
queryService.queryTemporalEntities(any(), any())
} throws BadRequestDataException(
} throws TooManyResultsException(
"You asked for 200 results, but the supported maximum limit is 100"
)

Expand All @@ -1042,12 +1042,12 @@ class TemporalEntityHandlerTests {
"type=BeeHive&limit=200&offset=1"
)
.exchange()
.expectStatus().isBadRequest
.expectStatus().isForbidden
.expectBody().json(
"""
{
"type":"https://uri.etsi.org/ngsi-ld/errors/BadRequestData",
"title":"The request includes input data which does not meet the requirements of the operation",
"type":"https://uri.etsi.org/ngsi-ld/errors/TooManyResults",
"title":"The query associated to the operation is producing so many results that can exhaust client or server resources. It should be made more restrictive",
"detail":"You asked for 200 results, but the supported maximum limit is 100"
}
""".trimIndent()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,10 +40,9 @@ data class TooComplexQueryResponse(override val detail: String) : ErrorResponse(

data class TooManyResultsResponse(override val detail: String) : ErrorResponse(
ErrorType.TOO_MANY_RESULTS.type,
"""
The query associated to the operation is producing so many results that can exhaust client or server resources.
It should be made more restrictive
""",
"The query associated to the operation is producing so many results " +
"that can exhaust client or server resources. " +
"It should be made more restrictive",
detail
)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,8 @@ fun APIException.toErrorResponse(): ResponseEntity<*> =
generateErrorResponse(HttpStatus.NOT_IMPLEMENTED, NotImplementedResponse(this.message))
is LdContextNotAvailableException ->
generateErrorResponse(HttpStatus.SERVICE_UNAVAILABLE, LdContextNotAvailableResponse(this.message))
is TooManyResultsException ->
generateErrorResponse(HttpStatus.FORBIDDEN, TooManyResultsResponse(this.message))
else -> generateErrorResponse(HttpStatus.INTERNAL_SERVER_ERROR, InternalErrorResponse("$cause"))
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -259,7 +259,7 @@ fun parsePaginationParameters(
if (count && (limit < 0 || offset < 0))
return BadRequestDataException("Offset and limit must be greater than zero").left()
if (limit > limitMax)
return BadRequestDataException(
return TooManyResultsException(
"You asked for $limit results, but the supported maximum limit is $limitMax"
).left()
return PaginationQuery(offset, limit, count).right()
Expand Down
4 changes: 2 additions & 2 deletions shared/src/main/resources/shared.properties
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@ application.tenants[0].dbSchema = public

application.authentication.enabled = false
# Pagination config for query resources endpoints
application.pagination.limit-default = 30
application.pagination.limit-max = 100
application.pagination.limit-default = ${LIMIT_DEFAULT}
application.pagination.limit-max = ${LIMIT_MAX}

spring.kafka.bootstrap-servers = localhost:29092
# To ensure we get all past messages when dynamically joining a new topic based on our "cim.entities.*" pattern
Expand Down

0 comments on commit 24a18dd

Please sign in to comment.