From da0256ca728204448de9dc11a62f614e85856f9c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C3=84njali?= Date: Tue, 9 Nov 2021 13:37:25 +0000 Subject: [PATCH 01/16] Fix issue #1492 --- CHANGES_NEXT_RELEASE | 2 +- src/lib/rest/rest.cpp | 4 +--- 2 files changed, 2 insertions(+), 4 deletions(-) diff --git a/CHANGES_NEXT_RELEASE b/CHANGES_NEXT_RELEASE index a7eafbd6d7..83fa4ceb59 100644 --- a/CHANGES_NEXT_RELEASE +++ b/CHANGES_NEXT_RELEASE @@ -1,4 +1,4 @@ -- Fix: noCache flag should be check before removing csub from cache in mongoUnsubscribeContext() (#2879) +- Fix: Using limit=0 and details=on to get only the count of elements (#1492) - Fix: throttling misbehaviour after subscription update when csubs cache is in use (#3981) - Fix: lastNotification missed some times in GET /v2/subscriptions/{id} and GET /v2/subscriptions - Fix: avoid reset timesSent counter upon PATCH /v2/subscriptions/{subId} when csubs cache is in use diff --git a/src/lib/rest/rest.cpp b/src/lib/rest/rest.cpp index 5e0b9005a6..d850293642 100644 --- a/src/lib/rest/rest.cpp +++ b/src/lib/rest/rest.cpp @@ -177,9 +177,7 @@ static int uriArgumentGet(void* cbDataP, MHD_ValueKind kind, const char* ckey, c } else if (limit == 0) { - OrionError error(SccBadRequest, std::string("Bad pagination limit: /") + value + "/ [a value of ZERO is unacceptable]"); - ciP->httpStatusCode = error.code; - ciP->answer = error.smartRender(ciP->apiVersion); + ciP->httpStatusCode = SccOk; return MHD_YES; } } From 28d9007399d7b38f7ff3e8697666260557864f44 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C3=84njali?= Date: Wed, 10 Nov 2021 08:28:41 +0000 Subject: [PATCH 02/16] Updated --- src/lib/mongoBackend/mongoUnsubscribeContext.cpp | 14 ++++++-------- 1 file changed, 6 insertions(+), 8 deletions(-) diff --git a/src/lib/mongoBackend/mongoUnsubscribeContext.cpp b/src/lib/mongoBackend/mongoUnsubscribeContext.cpp index 3667703991..227d2391b3 100644 --- a/src/lib/mongoBackend/mongoUnsubscribeContext.cpp +++ b/src/lib/mongoBackend/mongoUnsubscribeContext.cpp @@ -127,15 +127,13 @@ HttpStatusCode mongoUnsubscribeContext // // Removing subscription from mongo subscription cache // - if (!noCache) - { - LM_T(LmtSubCache, ("removing subscription '%s' (tenant '%s') from mongo subscription cache", - requestP->subscriptionId.get().c_str(), - tenant.c_str())); + LM_T(LmtSubCache, ("removing subscription '%s' (tenant '%s') from mongo subscription cache", + requestP->subscriptionId.get().c_str(), + tenant.c_str())); - cacheSemTake(__FUNCTION__, "Removing subscription from cache"); + cacheSemTake(__FUNCTION__, "Removing subscription from cache"); - CachedSubscription* cSubP = subCacheItemLookup(tenant.c_str(), requestP->subscriptionId.get().c_str()); + CachedSubscription* cSubP = subCacheItemLookup(tenant.c_str(), requestP->subscriptionId.get().c_str()); if (cSubP != NULL) { @@ -145,6 +143,6 @@ HttpStatusCode mongoUnsubscribeContext cacheSemGive(__FUNCTION__, "Removing subscription from cache"); reqSemGive(__FUNCTION__, "ngsi10 unsubscribe request", reqSemTaken); responseP->statusCode.fill(SccOk); - } + return SccOk; } From 9dd633060cb847bc4aa411120846e26abe12ea7f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C3=84njali?= Date: Thu, 11 Nov 2021 11:50:04 +0000 Subject: [PATCH 03/16] Updated --- CHANGES_NEXT_RELEASE | 6 +++++- .../0000_bad_requests/pagination_error_in_uri_params.test | 8 ++++---- 2 files changed, 9 insertions(+), 5 deletions(-) diff --git a/CHANGES_NEXT_RELEASE b/CHANGES_NEXT_RELEASE index 83fa4ceb59..ed48f7ebfb 100644 --- a/CHANGES_NEXT_RELEASE +++ b/CHANGES_NEXT_RELEASE @@ -1,5 +1,9 @@ -- Fix: Using limit=0 and details=on to get only the count of elements (#1492) +- Fix: Using limit=0 and options=count to get only the count of elements (#1492) +- Add: maxFailsLimit subscription field, so subscription is automatically passed to inactive after that number of failed notification attemps (#3541) +- Add: failsCounter subscription field, to count the number of consecutive notificaitons fails (#3541) - Fix: throttling misbehaviour after subscription update when csubs cache is in use (#3981) - Fix: lastNotification missed some times in GET /v2/subscriptions/{id} and GET /v2/subscriptions - Fix: avoid reset timesSent counter upon PATCH /v2/subscriptions/{subId} when csubs cache is in use +- Fix: crash when updating attribute with empty JSON object or array (#3995) - Hardening: refactor PATCH /v2/subscriptions/{subId} logic avoiding over-quering MongoDB and possible (although very rare) crash situations during csub cache (#3701) +- Upgrade Dockerfile base image from centos8.3.2011 to centos8.4.2105 diff --git a/test/functionalTest/cases/0000_bad_requests/pagination_error_in_uri_params.test b/test/functionalTest/cases/0000_bad_requests/pagination_error_in_uri_params.test index 244bc27e8a..313ae3580e 100644 --- a/test/functionalTest/cases/0000_bad_requests/pagination_error_in_uri_params.test +++ b/test/functionalTest/cases/0000_bad_requests/pagination_error_in_uri_params.test @@ -93,16 +93,16 @@ Date: REGEX(.*) +++++ 3. URI param 'limit' == 0 -HTTP/1.1 400 Bad Request -Content-Length: 135 +HTTP/1.1 200 OK +Content-Length: 86 Content-Type: application/json Fiware-Correlator: REGEX([0-9a-f\-]{36}) Date: REGEX(.*) { - "orionError": { + "errorCode": { "code": "400", - "details": "Bad pagination limit: /000000/ [a value of ZERO is unacceptable]", + "details": "JSON Parse Error", "reasonPhrase": "Bad Request" } } From f4d0cbd1b4893f172d0eb8e912483414ad472a85 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C3=84njali?= Date: Thu, 11 Nov 2021 11:51:08 +0000 Subject: [PATCH 04/16] Added test file --- ...unt_to_get_only_the_count_of_elements.test | 79 +++++++++++++++++++ 1 file changed, 79 insertions(+) create mode 100644 test/functionalTest/cases/1492_using_limit=0_and_options=count_to_get_only_the_count_of_elements/using_limit=0_and_options=count_to_get_only_the_count_of_elements.test diff --git a/test/functionalTest/cases/1492_using_limit=0_and_options=count_to_get_only_the_count_of_elements/using_limit=0_and_options=count_to_get_only_the_count_of_elements.test b/test/functionalTest/cases/1492_using_limit=0_and_options=count_to_get_only_the_count_of_elements/using_limit=0_and_options=count_to_get_only_the_count_of_elements.test new file mode 100644 index 0000000000..9f2ad33dd9 --- /dev/null +++ b/test/functionalTest/cases/1492_using_limit=0_and_options=count_to_get_only_the_count_of_elements/using_limit=0_and_options=count_to_get_only_the_count_of_elements.test @@ -0,0 +1,79 @@ +# Copyright 2021 Telefonica Investigacion y Desarrollo, S.A.U +# +# This file is part of Orion Context Broker. +# +# Orion Context Broker is free software: you can redistribute it and/or +# modify it under the terms of the GNU Affero General Public License as +# published by the Free Software Foundation, either version 3 of the +# License, or (at your option) any later version. +# +# Orion Context Broker is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Affero +# General Public License for more details. +# +# You should have received a copy of the GNU Affero General Public License +# along with Orion Context Broker. If not, see http://www.gnu.org/licenses/. +# +# For those usages not covered by this license please contact with +# iot_support at tid dot es + +# VALGRIND_READY - to mark the test ready for valgrindTestSuite.sh + +--NAME-- +using_limit=0_and_options=count_to_get_only_the_count_of_elements + +--SHELL-INIT-- +dbInit CB +brokerStart CB + +--SHELL-- + +# +# 01. GET /v2/entities with 'limit' == 0 and options=count +# 02. GET /v2/entities with 'limit' == 0 +# + +echo "01. GET /v2/entities with 'limit' == 0 and options=count" +echo "========================================================" +orionCurl --url "/v2/entities?limit=0&options=count" +echo +echo + + + +echo "02. GET /v2/entities with 'limit' == 0" +echo "======================================" +orionCurl --url "/v2/entities?limit=0" +echo +echo + + + +--REGEXPECT-- +01. GET /v2/entities with 'limit' == 0 and options=count +======================================================== +HTTP/1.1 200 OK +Content-Length: 2 +Content-Type: application/json +Fiware-Total-Count: 0 +Fiware-Correlator: REGEX([0-9a-f\-]{36}) +Date: REGEX(.*) + +[] + + +02. GET /v2/entities with 'limit' == 0 +====================================== +HTTP/1.1 200 OK +Content-Length: 2 +Content-Type: application/json +Fiware-Correlator: REGEX([0-9a-f\-]{36}) +Date: REGEX(.*) + +[] + + +--TEARDOWN-- +brokerStop CB +dbDrop CB From 8879a3975cdd73efa2a2d9289e528d99c68ff168 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C3=84njali?= Date: Fri, 12 Nov 2021 17:15:05 +0000 Subject: [PATCH 05/16] Updated as per comment --- src/lib/rest/rest.cpp | 7 +- .../pagination_error_in_uri_params.test | 4 +- .../1492_limit_zero/limit_zero_entities.test | 160 ++++++++++ .../1492_limit_zero/limit_zero_regs.test | 267 ++++++++++++++++ .../1492_limit_zero/limit_zero_subs.test | 299 ++++++++++++++++++ ...unt_to_get_only_the_count_of_elements.test | 79 ----- 6 files changed, 729 insertions(+), 87 deletions(-) create mode 100644 test/functionalTest/cases/1492_limit_zero/limit_zero_entities.test create mode 100644 test/functionalTest/cases/1492_limit_zero/limit_zero_regs.test create mode 100644 test/functionalTest/cases/1492_limit_zero/limit_zero_subs.test delete mode 100644 test/functionalTest/cases/1492_using_limit=0_and_options=count_to_get_only_the_count_of_elements/using_limit=0_and_options=count_to_get_only_the_count_of_elements.test diff --git a/src/lib/rest/rest.cpp b/src/lib/rest/rest.cpp index d850293642..58782d157d 100644 --- a/src/lib/rest/rest.cpp +++ b/src/lib/rest/rest.cpp @@ -158,7 +158,7 @@ static int uriArgumentGet(void* cbDataP, MHD_ValueKind kind, const char* ckey, c { if ((*cP < '0') || (*cP > '9')) { - OrionError error(SccBadRequest, std::string("Bad pagination limit: /") + value + "/ [must be a decimal number]"); + OrionError error(SccBadRequest, std::string("Bad pagination limit: /") + value + "/ [must be a positive integer number]"); ciP->httpStatusCode = error.code; ciP->answer = error.smartRender(ciP->apiVersion); return MHD_YES; @@ -175,11 +175,6 @@ static int uriArgumentGet(void* cbDataP, MHD_ValueKind kind, const char* ckey, c ciP->answer = error.smartRender(ciP->apiVersion); return MHD_YES; } - else if (limit == 0) - { - ciP->httpStatusCode = SccOk; - return MHD_YES; - } } else if (key == URI_PARAM_PAGINATION_DETAILS) { diff --git a/test/functionalTest/cases/0000_bad_requests/pagination_error_in_uri_params.test b/test/functionalTest/cases/0000_bad_requests/pagination_error_in_uri_params.test index 313ae3580e..7a4f3c0083 100644 --- a/test/functionalTest/cases/0000_bad_requests/pagination_error_in_uri_params.test +++ b/test/functionalTest/cases/0000_bad_requests/pagination_error_in_uri_params.test @@ -62,7 +62,7 @@ echo --REGEXPECT-- +++++ 1. bad characters in URI param 'limit' HTTP/1.1 400 Bad Request -Content-Length: 125 +Content-Length: 134 Content-Type: application/json Fiware-Correlator: REGEX([0-9a-f\-]{36}) Date: REGEX(.*) @@ -70,7 +70,7 @@ Date: REGEX(.*) { "orionError": { "code": "400", - "details": "Bad pagination limit: /abc/ [must be a decimal number]", + "details": "Bad pagination limit: /abc/ [must be a positive integer number]", "reasonPhrase": "Bad Request" } } diff --git a/test/functionalTest/cases/1492_limit_zero/limit_zero_entities.test b/test/functionalTest/cases/1492_limit_zero/limit_zero_entities.test new file mode 100644 index 0000000000..e804dfc9c6 --- /dev/null +++ b/test/functionalTest/cases/1492_limit_zero/limit_zero_entities.test @@ -0,0 +1,160 @@ +# Copyright 2021 Telefonica Investigacion y Desarrollo, S.A.U +# +# This file is part of Orion Context Broker. +# +# Orion Context Broker is free software: you can redistribute it and/or +# modify it under the terms of the GNU Affero General Public License as +# published by the Free Software Foundation, either version 3 of the +# License, or (at your option) any later version. +# +# Orion Context Broker is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Affero +# General Public License for more details. +# +# You should have received a copy of the GNU Affero General Public License +# along with Orion Context Broker. If not, see http://www.gnu.org/licenses/. +# +# For those usages not covered by this license please contact with +# iot_support at tid dot es + +# VALGRIND_READY - to mark the test ready for valgrindTestSuite.sh + +--NAME-- +limit_zero_entities + +--SHELL-INIT-- +dbInit CB +brokerStart CB + +--SHELL-- + +# +# 01. GET /v2/entities with 'limit' == 0 and options=count and see no entities and fiware-total-count: 0 +# 02. POST /v2/entities to create E1 entity +# 03. POST /v2/entities to create E1 entity +# 04. GET /v2/entities with 'limit' == 0 and options=count and see no entities and fiware-total-count: 2 +# 05. GET /v2/entities with 'limit' == 0 and see no entities +# + +echo "01. GET /v2/entities with 'limit' == 0 and options=count and see no entities and fiware-total-count: 0" +echo "======================================================================================================" +orionCurl --url "/v2/entities?limit=0&options=count" +echo +echo + + +echo "02. POST /v2/entities to create E1 entity" +echo "=========================================" +payload='{ + "id": "E1", + "type": "T" +}' +orionCurl --url /v2/entities -X POST --payload "$payload" +echo +echo + + +echo "03. POST /v2/entities to create E2 entity" +echo "=========================================" +payload='{ + "id": "E2", + "type": "T" +}' +orionCurl --url /v2/entities -X POST --payload "$payload" +echo +echo + + +echo "04. GET /v2/entities with 'limit' == 0 and options=count and see no entities and fiware-total-count: 2" +echo "======================================================================================================" +orionCurl --url "/v2/entities?limit=0&options=count" +echo +echo + + +echo "05. GET /v2/entities with 'limit' == 0 and see no entities" +echo "==========================================================" +orionCurl --url "/v2/entities?limit=0" +echo +echo + + + +--REGEXPECT-- +01. GET /v2/entities with 'limit' == 0 and options=count and see no entities and fiware-total-count: 0 +====================================================================================================== +HTTP/1.1 200 OK +Content-Length: 2 +Content-Type: application/json +Fiware-Total-Count: 0 +Fiware-Correlator: REGEX([0-9a-f\-]{36}) +Date: REGEX(.*) + +[] + + +02. POST /v2/entities to create E1 entity +========================================= +HTTP/1.1 201 Created +Content-Length: 0 +Location: /v2/entities/E1?type=T +Fiware-Correlator: REGEX([0-9a-f\-]{36}) +Date: REGEX(.*) + + + +03. POST /v2/entities to create E2 entity +========================================= +HTTP/1.1 201 Created +Content-Length: 0 +Location: /v2/entities/E2?type=T +Fiware-Correlator: REGEX([0-9a-f\-]{36}) +Date: REGEX(.*) + + + +04. GET /v2/entities with 'limit' == 0 and options=count and see no entities and fiware-total-count: 2 +====================================================================================================== +HTTP/1.1 200 OK +Content-Length: 47 +Content-Type: application/json +Fiware-Total-Count: 2 +Fiware-Correlator: REGEX([0-9a-f\-]{36}) +Date: REGEX(.*) + +[ + { + "id": "E1", + "type": "T" + }, + { + "id": "E2", + "type": "T" + } +] + + +05. GET /v2/entities with 'limit' == 0 and see no entities +========================================================== +HTTP/1.1 200 OK +Content-Length: 47 +Content-Type: application/json +Fiware-Correlator: REGEX([0-9a-f\-]{36}) +Date: REGEX(.*) + +[ + { + "id": "E1", + "type": "T" + }, + { + "id": "E2", + "type": "T" + } +] + + +--TEARDOWN-- +brokerStop CB +dbDrop CB diff --git a/test/functionalTest/cases/1492_limit_zero/limit_zero_regs.test b/test/functionalTest/cases/1492_limit_zero/limit_zero_regs.test new file mode 100644 index 0000000000..52300df620 --- /dev/null +++ b/test/functionalTest/cases/1492_limit_zero/limit_zero_regs.test @@ -0,0 +1,267 @@ +# Copyright 2021 Telefonica Investigacion y Desarrollo, S.A.U +# +# This file is part of Orion Context Broker. +# +# Orion Context Broker is free software: you can redistribute it and/or +# modify it under the terms of the GNU Affero General Public License as +# published by the Free Software Foundation, either version 3 of the +# License, or (at your option) any later version. +# +# Orion Context Broker is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Affero +# General Public License for more details. +# +# You should have received a copy of the GNU Affero General Public License +# along with Orion Context Broker. If not, see http://www.gnu.org/licenses/. +# +# For those usages not covered by this license please contact with +# iot_support at tid dot es + +# VALGRIND_READY - to mark the test ready for valgrindTestSuite.sh + +--NAME-- +limit_zero_regs + +--SHELL-INIT-- +dbInit CB +brokerStart CB + +--SHELL-- + +# +# 01. GET /v2/registrations with 'limit' == 0 and options=count and see no entities and fiware-total-count: 0 +# 02. POST /v2/registrations to create E1 entity +# 03. POST /v2/registrations to create E2 entity +# 04. GET /v2/registrations with 'limit' == 0 and options=count and see no entities and fiware-total-count: 2 +# 05. GET /v2/registrations with 'limit' == 0 and see no entities +# + + + +echo "01. GET /v2/registrations with 'limit' == 0 and options=count and see no entities and fiware-total-count: 0" +echo "===========================================================================================================" +orionCurl --url "/v2/registrations?limit=0&options=count" +echo +echo + + +echo "02. POST /v2/registrations to create E1 entity" +echo "==============================================" +payload='{ + "description": "located in CP1", + "dataProvided": { + "entities": [ + { + "id": "E1", + "type": "T1" + } + ], + "attrs": [ "A1" ] + }, + "provider": { + "http": { + "url": "http://localhost:'${CP1_PORT}'/v2" + }, + "legacyForwarding": true + }, + "status": "active" +}' +orionCurl --url /v2/registrations --payload "$payload" +echo +echo + + +echo "03. POST /v2/registrations to create E2 entity" +echo "==============================================" +payload='{ + "description": "located in CP1", + "dataProvided": { + "entities": [ + { + "id": "E2", + "type": "T1" + } + ], + "attrs": [ "A2" ] + }, + "provider": { + "http": { + "url": "http://localhost:'${CP1_PORT}'/v2" + }, + "legacyForwarding": true + }, + "status": "active" +}' +orionCurl --url /v2/registrations --payload "$payload" +echo +echo + + +echo "04. GET /v2/registrations with 'limit' == 0 and options=count and see no entities and fiware-total-count: 2" +echo "===========================================================================================================" +orionCurl --url "/v2/registrations?limit=0&options=count" +echo +echo + + +echo "05. GET /v2/registrations with 'limit' == 0 and see no entities" +echo "===============================================================" +orionCurl --url "/v2/registrations?limit=0" +echo +echo + + +--REGEXPECT-- +01. GET /v2/registrations with 'limit' == 0 and options=count and see no entities and fiware-total-count: 0 +=========================================================================================================== +HTTP/1.1 200 OK +Content-Length: 2 +Content-Type: application/json +Fiware-Total-Count: 0 +Fiware-Correlator: REGEX([0-9a-f\-]{36}) +Date: REGEX(.*) + +[] + + +02. POST /v2/registrations to create E1 entity +============================================== +HTTP/1.1 201 Created +Content-Length: 0 +Location: /v2/registrations/REGEX([0-9a-f\-]{24}) +Fiware-Correlator: REGEX([0-9a-f\-]{36}) +Date: REGEX(.*) + + + +03. POST /v2/registrations to create E2 entity +============================================== +HTTP/1.1 201 Created +Content-Length: 0 +Location: /v2/registrations/REGEX([0-9a-f\-]{24}) +Fiware-Correlator: REGEX([0-9a-f\-]{36}) +Date: REGEX(.*) + + + +04. GET /v2/registrations with 'limit' == 0 and options=count and see no entities and fiware-total-count: 2 +=========================================================================================================== +HTTP/1.1 200 OK +Content-Length: 527 +Content-Type: application/json +Fiware-Total-Count: 2 +Fiware-Correlator: REGEX([0-9a-f\-]{36}) +Date: REGEX(.*) + +[ + { + "dataProvided": { + "attrs": [ + "A1" + ], + "entities": [ + { + "id": "E1", + "type": "T1" + } + ] + }, + "description": "located in CP1", + "id": "REGEX([0-9a-f\-]{24})", + "provider": { + "http": { + "url": "http://localhost:REGEX(\d+)/v2" + }, + "legacyForwarding": true, + "supportedForwardingMode": "all" + }, + "status": "active" + }, + { + "dataProvided": { + "attrs": [ + "A2" + ], + "entities": [ + { + "id": "E2", + "type": "T1" + } + ] + }, + "description": "located in CP1", + "id": "REGEX([0-9a-f\-]{24})", + "provider": { + "http": { + "url": "http://localhost:REGEX(\d+)/v2" + }, + "legacyForwarding": true, + "supportedForwardingMode": "all" + }, + "status": "active" + } +] + + +05. GET /v2/registrations with 'limit' == 0 and see no entities +=============================================================== +HTTP/1.1 200 OK +Content-Length: 527 +Content-Type: application/json +Fiware-Correlator: REGEX([0-9a-f\-]{36}) +Date: REGEX(.*) + +[ + { + "dataProvided": { + "attrs": [ + "A1" + ], + "entities": [ + { + "id": "E1", + "type": "T1" + } + ] + }, + "description": "located in CP1", + "id": "REGEX([0-9a-f\-]{24})", + "provider": { + "http": { + "url": "http://localhost:REGEX(\d+)/v2" + }, + "legacyForwarding": true, + "supportedForwardingMode": "all" + }, + "status": "active" + }, + { + "dataProvided": { + "attrs": [ + "A2" + ], + "entities": [ + { + "id": "E2", + "type": "T1" + } + ] + }, + "description": "located in CP1", + "id": "REGEX([0-9a-f\-]{24})", + "provider": { + "http": { + "url": "http://localhost:REGEX(\d+)/v2" + }, + "legacyForwarding": true, + "supportedForwardingMode": "all" + }, + "status": "active" + } +] + + +--TEARDOWN-- +brokerStop CB +dbDrop CB diff --git a/test/functionalTest/cases/1492_limit_zero/limit_zero_subs.test b/test/functionalTest/cases/1492_limit_zero/limit_zero_subs.test new file mode 100644 index 0000000000..8a4a943583 --- /dev/null +++ b/test/functionalTest/cases/1492_limit_zero/limit_zero_subs.test @@ -0,0 +1,299 @@ +# Copyright 2021 Telefonica Investigacion y Desarrollo, S.A.U +# +# This file is part of Orion Context Broker. +# +# Orion Context Broker is free software: you can redistribute it and/or +# modify it under the terms of the GNU Affero General Public License as +# published by the Free Software Foundation, either version 3 of the +# License, or (at your option) any later version. +# +# Orion Context Broker is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Affero +# General Public License for more details. +# +# You should have received a copy of the GNU Affero General Public License +# along with Orion Context Broker. If not, see http://www.gnu.org/licenses/. +# +# For those usages not covered by this license please contact with +# iot_support at tid dot es + +# VALGRIND_READY - to mark the test ready for valgrindTestSuite.sh + +--NAME-- +limit_zero_subs + +--SHELL-INIT-- +dbInit CB +brokerStart CB + +--SHELL-- + +# +# 01. GET /v2/subscriptions with 'limit' == 0 and options=count and see no entities and fiware-total-count: 0 +# 02. POST /v2/subscriptions to create E1 entity +# 03. POST /v2/subscriptions to create E2 entity +# 04. GET /v2/subscriptions with 'limit' == 0 and options=count and see no entities and fiware-total-count: 2 +# 05. GET /v2/subscriptions 'limit' == 0 and see no entities +# + +echo "01. GET /v2/subscriptions with 'limit' == 0 and options=count and see no entities and fiware-total-count: 0" +echo "===========================================================================================================" +orionCurl --url "/v2/subscriptions?limit=0&options=count" +echo +echo + + +echo "02. POST /v2/subscriptions to create E1 entity" +echo "==============================================" +payload='{ + "description": "A subscription to get info about E1", + "subject": { + "entities": [ + { + "id": "E1", + "type": "T1" + } + ], + "condition": { + "attrs": [ + "A1" + ] + } + }, + "notification": { + "http": { + "url": "http://localhost:'$LISTENER_PORT'/notify" + }, + "attrs": [ + "A1" + ] + } +}' +orionCurl --url /v2/subscriptions --payload "$payload" +subId=$(echo "$_responseHeaders" | grep Location | awk -F/ '{ print $4 }' | tr -d "\r\n") +echo +echo + + +echo "03. POST /v2/subscriptions to create E2 entity" +echo "==============================================" +payload='{ + "description": "A subscription to get info about E2", + "subject": { + "entities": [ + { + "id": "E2", + "type": "T1" + } + ], + "condition": { + "attrs": [ + "A2" + ] + } + }, + "notification": { + "http": { + "url": "http://localhost:'$LISTENER_PORT'/notify" + }, + "attrs": [ + "A2" + ] + } +}' +orionCurl --url /v2/subscriptions --payload "$payload" +subId=$(echo "$_responseHeaders" | grep Location | awk -F/ '{ print $4 }' | tr -d "\r\n") +echo +echo + + + +echo "04. GET /v2/subscriptions with 'limit' == 0 and options=count and see no entities and fiware-total-count: 2" +echo "===========================================================================================================" +orionCurl --url "/v2/subscriptions?limit=0&options=count" +echo +echo + + +echo "05. GET /v2/subscriptions with 'limit' == 0 and see no entities" +echo "===============================================================" +orionCurl --url "/v2/subscriptions?limit=0" +echo +echo + + + +--REGEXPECT-- +01. GET /v2/subscriptions with 'limit' == 0 and options=count and see no entities and fiware-total-count: 0 +=========================================================================================================== +HTTP/1.1 200 OK +Content-Length: 2 +Content-Type: application/json +Fiware-Total-Count: 0 +Fiware-Correlator: REGEX([0-9a-f\-]{36}) +Date: REGEX(.*) + +[] + + +02. POST /v2/subscriptions to create E1 entity +============================================== +HTTP/1.1 201 Created +Content-Length: 0 +Location: /v2/subscriptions/REGEX([0-9a-f]{24}) +Fiware-Correlator: REGEX([0-9a-f\-]{36}) +Date: REGEX(.*) + + + +03. POST /v2/subscriptions to create E2 entity +============================================== +HTTP/1.1 201 Created +Content-Length: 0 +Location: /v2/subscriptions/REGEX([0-9a-f]{24}) +Fiware-Correlator: REGEX([0-9a-f\-]{36}) +Date: REGEX(.*) + + + +04. GET /v2/subscriptions with 'limit' == 0 and options=count and see no entities and fiware-total-count: 2 +=========================================================================================================== +HTTP/1.1 200 OK +Content-Length: 625 +Content-Type: application/json +Fiware-Total-Count: 2 +Fiware-Correlator: REGEX([0-9a-f\-]{36}) +Date: REGEX(.*) + +[ + { + "description": "A subscription to get info about E1", + "id": "REGEX([0-9a-f]{24})", + "notification": { + "attrs": [ + "A1" + ], + "attrsFormat": "normalized", + "http": { + "url": "http://localhost:REGEX(\d+)/notify" + }, + "onlyChangedAttrs": false + }, + "status": "active", + "subject": { + "condition": { + "attrs": [ + "A1" + ] + }, + "entities": [ + { + "id": "E1", + "type": "T1" + } + ] + } + }, + { + "description": "A subscription to get info about E2", + "id": "REGEX([0-9a-f]{24})", + "notification": { + "attrs": [ + "A2" + ], + "attrsFormat": "normalized", + "http": { + "url": "http://localhost:REGEX(\d+)/notify" + }, + "onlyChangedAttrs": false + }, + "status": "active", + "subject": { + "condition": { + "attrs": [ + "A2" + ] + }, + "entities": [ + { + "id": "E2", + "type": "T1" + } + ] + } + } +] + + +05. GET /v2/subscriptions with 'limit' == 0 and see no entities +=============================================================== +HTTP/1.1 200 OK +Content-Length: 625 +Content-Type: application/json +Fiware-Correlator: REGEX([0-9a-f\-]{36}) +Date: REGEX(.*) + +[ + { + "description": "A subscription to get info about E1", + "id": "REGEX([0-9a-f]{24})", + "notification": { + "attrs": [ + "A1" + ], + "attrsFormat": "normalized", + "http": { + "url": "http://localhost:REGEX(\d+)/notify" + }, + "onlyChangedAttrs": false + }, + "status": "active", + "subject": { + "condition": { + "attrs": [ + "A1" + ] + }, + "entities": [ + { + "id": "E1", + "type": "T1" + } + ] + } + }, + { + "description": "A subscription to get info about E2", + "id": "REGEX([0-9a-f]{24})", + "notification": { + "attrs": [ + "A2" + ], + "attrsFormat": "normalized", + "http": { + "url": "http://localhost:REGEX(\d+)/notify" + }, + "onlyChangedAttrs": false + }, + "status": "active", + "subject": { + "condition": { + "attrs": [ + "A2" + ] + }, + "entities": [ + { + "id": "E2", + "type": "T1" + } + ] + } + } +] + + +--TEARDOWN-- +brokerStop CB +dbDrop CB diff --git a/test/functionalTest/cases/1492_using_limit=0_and_options=count_to_get_only_the_count_of_elements/using_limit=0_and_options=count_to_get_only_the_count_of_elements.test b/test/functionalTest/cases/1492_using_limit=0_and_options=count_to_get_only_the_count_of_elements/using_limit=0_and_options=count_to_get_only_the_count_of_elements.test deleted file mode 100644 index 9f2ad33dd9..0000000000 --- a/test/functionalTest/cases/1492_using_limit=0_and_options=count_to_get_only_the_count_of_elements/using_limit=0_and_options=count_to_get_only_the_count_of_elements.test +++ /dev/null @@ -1,79 +0,0 @@ -# Copyright 2021 Telefonica Investigacion y Desarrollo, S.A.U -# -# This file is part of Orion Context Broker. -# -# Orion Context Broker is free software: you can redistribute it and/or -# modify it under the terms of the GNU Affero General Public License as -# published by the Free Software Foundation, either version 3 of the -# License, or (at your option) any later version. -# -# Orion Context Broker is distributed in the hope that it will be useful, -# but WITHOUT ANY WARRANTY; without even the implied warranty of -# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Affero -# General Public License for more details. -# -# You should have received a copy of the GNU Affero General Public License -# along with Orion Context Broker. If not, see http://www.gnu.org/licenses/. -# -# For those usages not covered by this license please contact with -# iot_support at tid dot es - -# VALGRIND_READY - to mark the test ready for valgrindTestSuite.sh - ---NAME-- -using_limit=0_and_options=count_to_get_only_the_count_of_elements - ---SHELL-INIT-- -dbInit CB -brokerStart CB - ---SHELL-- - -# -# 01. GET /v2/entities with 'limit' == 0 and options=count -# 02. GET /v2/entities with 'limit' == 0 -# - -echo "01. GET /v2/entities with 'limit' == 0 and options=count" -echo "========================================================" -orionCurl --url "/v2/entities?limit=0&options=count" -echo -echo - - - -echo "02. GET /v2/entities with 'limit' == 0" -echo "======================================" -orionCurl --url "/v2/entities?limit=0" -echo -echo - - - ---REGEXPECT-- -01. GET /v2/entities with 'limit' == 0 and options=count -======================================================== -HTTP/1.1 200 OK -Content-Length: 2 -Content-Type: application/json -Fiware-Total-Count: 0 -Fiware-Correlator: REGEX([0-9a-f\-]{36}) -Date: REGEX(.*) - -[] - - -02. GET /v2/entities with 'limit' == 0 -====================================== -HTTP/1.1 200 OK -Content-Length: 2 -Content-Type: application/json -Fiware-Correlator: REGEX([0-9a-f\-]{36}) -Date: REGEX(.*) - -[] - - ---TEARDOWN-- -brokerStop CB -dbDrop CB From cf68c96ee05fa0fd7b53156f8e2b86761a386d0f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C3=84njali?= Date: Thu, 18 Nov 2021 11:52:17 +0000 Subject: [PATCH 06/16] Updated code as per comment --- .../serviceRoutinesV2/getAllSubscriptions.cpp | 4 + src/lib/serviceRoutinesV2/getEntities.cpp | 14 +- .../serviceRoutinesV2/getRegistrations.cpp | 5 +- .../1492_limit_zero/limit_zero_entities.test | 28 +-- .../1492_limit_zero/limit_zero_regs.test | 160 +++------------- .../1492_limit_zero/limit_zero_subs.test | 176 +++--------------- 6 files changed, 78 insertions(+), 309 deletions(-) diff --git a/src/lib/serviceRoutinesV2/getAllSubscriptions.cpp b/src/lib/serviceRoutinesV2/getAllSubscriptions.cpp index 8f4b50b099..19f0828595 100644 --- a/src/lib/serviceRoutinesV2/getAllSubscriptions.cpp +++ b/src/lib/serviceRoutinesV2/getAllSubscriptions.cpp @@ -87,7 +87,11 @@ std::string getAllSubscriptions } std::string out; + + if (limit != 0) + { TIMED_RENDER(out = vectorToJson(subs)); + } return out; } diff --git a/src/lib/serviceRoutinesV2/getEntities.cpp b/src/lib/serviceRoutinesV2/getEntities.cpp index 395930e67a..1ab1148e9e 100644 --- a/src/lib/serviceRoutinesV2/getEntities.cpp +++ b/src/lib/serviceRoutinesV2/getEntities.cpp @@ -86,6 +86,7 @@ std::string getEntities std::string answer; std::string pattern = ".*"; // all entities, default value std::string id = ciP->uriParam["id"]; + int limit = atoi(ciP->uriParam[URI_PARAM_PAGINATION_LIMIT].c_str()); std::string idPattern = ciP->uriParam["idPattern"]; std::string type = ciP->uriParam["type"]; std::string typePattern = ciP->uriParam["typePattern"]; @@ -289,7 +290,7 @@ std::string getEntities { parseDataP->qcr.res.fill(pattern, type, "true", EntityTypeNotEmpty, ""); } - else + else { // // More than one type listed in URI param 'type': @@ -304,12 +305,13 @@ std::string getEntities } // Get attrs and metadata filters from URL params - setAttrsFilter(ciP->uriParam, ciP->uriParamOptions, &parseDataP->qcr.res.attrsList); - setMetadataFilter(ciP->uriParam, &parseDataP->qcr.res.metadataList); - + setAttrsFilter(ciP->uriParam, ciP->uriParamOptions, &parseDataP->qcr.res.attrsList); + setMetadataFilter(ciP->uriParam, &parseDataP->qcr.res.metadataList); // 02. Call standard op postQueryContext - answer = postQueryContext(ciP, components, compV, parseDataP); - + if (limit != 0) + { + answer = postQueryContext(ciP, components, compV, parseDataP); + } // 03. Check Internal Errors if (parseDataP->qcrs.res.errorCode.code == SccReceiverInternalError) { diff --git a/src/lib/serviceRoutinesV2/getRegistrations.cpp b/src/lib/serviceRoutinesV2/getRegistrations.cpp index 9926c3a471..9a8c2c9892 100644 --- a/src/lib/serviceRoutinesV2/getRegistrations.cpp +++ b/src/lib/serviceRoutinesV2/getRegistrations.cpp @@ -87,7 +87,10 @@ std::string getRegistrations ciP->httpHeaderValue.push_back(double2string(count)); } - TIMED_RENDER(out = vectorToJson(registrationV)); + if (limit != 0) + { + TIMED_RENDER(out = vectorToJson(registrationV)); + } return out; } diff --git a/test/functionalTest/cases/1492_limit_zero/limit_zero_entities.test b/test/functionalTest/cases/1492_limit_zero/limit_zero_entities.test index e804dfc9c6..6a74f74607 100644 --- a/test/functionalTest/cases/1492_limit_zero/limit_zero_entities.test +++ b/test/functionalTest/cases/1492_limit_zero/limit_zero_entities.test @@ -87,7 +87,6 @@ echo HTTP/1.1 200 OK Content-Length: 2 Content-Type: application/json -Fiware-Total-Count: 0 Fiware-Correlator: REGEX([0-9a-f\-]{36}) Date: REGEX(.*) @@ -117,42 +116,23 @@ Date: REGEX(.*) 04. GET /v2/entities with 'limit' == 0 and options=count and see no entities and fiware-total-count: 2 ====================================================================================================== HTTP/1.1 200 OK -Content-Length: 47 +Content-Length: 2 Content-Type: application/json -Fiware-Total-Count: 2 Fiware-Correlator: REGEX([0-9a-f\-]{36}) Date: REGEX(.*) -[ - { - "id": "E1", - "type": "T" - }, - { - "id": "E2", - "type": "T" - } -] +[] 05. GET /v2/entities with 'limit' == 0 and see no entities ========================================================== HTTP/1.1 200 OK -Content-Length: 47 +Content-Length: 2 Content-Type: application/json Fiware-Correlator: REGEX([0-9a-f\-]{36}) Date: REGEX(.*) -[ - { - "id": "E1", - "type": "T" - }, - { - "id": "E2", - "type": "T" - } -] +[] --TEARDOWN-- diff --git a/test/functionalTest/cases/1492_limit_zero/limit_zero_regs.test b/test/functionalTest/cases/1492_limit_zero/limit_zero_regs.test index 52300df620..8e57657350 100644 --- a/test/functionalTest/cases/1492_limit_zero/limit_zero_regs.test +++ b/test/functionalTest/cases/1492_limit_zero/limit_zero_regs.test @@ -30,24 +30,24 @@ brokerStart CB --SHELL-- # -# 01. GET /v2/registrations with 'limit' == 0 and options=count and see no entities and fiware-total-count: 0 -# 02. POST /v2/registrations to create E1 entity -# 03. POST /v2/registrations to create E2 entity -# 04. GET /v2/registrations with 'limit' == 0 and options=count and see no entities and fiware-total-count: 2 -# 05. GET /v2/registrations with 'limit' == 0 and see no entities +# 01. GET /v2/registrations with 'limit' == 0 and options=count and see no registrations and fiware-total-count: 0 +# 02. POST /v2/registrations to create registration +# 03. POST /v2/registrations to create another registration +# 04. GET /v2/registrations with 'limit' == 0 and options=count and see no registrations and fiware-total-count: 2 +# 05. GET /v2/registrations with 'limit' == 0 and see no registrations # -echo "01. GET /v2/registrations with 'limit' == 0 and options=count and see no entities and fiware-total-count: 0" -echo "===========================================================================================================" +echo "01. GET /v2/registrations with 'limit' == 0 and options=count and see no registrations and fiware-total-count: 0" +echo "================================================================================================================" orionCurl --url "/v2/registrations?limit=0&options=count" echo echo -echo "02. POST /v2/registrations to create E1 entity" -echo "==============================================" +echo "02. POST /v2/registrations to create registration" +echo "=================================================" payload='{ "description": "located in CP1", "dataProvided": { @@ -72,8 +72,8 @@ echo echo -echo "03. POST /v2/registrations to create E2 entity" -echo "==============================================" +echo "03. POST /v2/registrations to create another registration" +echo "=========================================================" payload='{ "description": "located in CP1", "dataProvided": { @@ -98,35 +98,33 @@ echo echo -echo "04. GET /v2/registrations with 'limit' == 0 and options=count and see no entities and fiware-total-count: 2" -echo "===========================================================================================================" +echo "04. GET /v2/registrations with 'limit' == 0 and options=count and see no registrations and fiware-total-count: 2" +echo "================================================================================================================" orionCurl --url "/v2/registrations?limit=0&options=count" echo echo -echo "05. GET /v2/registrations with 'limit' == 0 and see no entities" -echo "===============================================================" +echo "05. GET /v2/registrations with 'limit' == 0 and see no registrations" +echo "====================================================================" orionCurl --url "/v2/registrations?limit=0" echo echo --REGEXPECT-- -01. GET /v2/registrations with 'limit' == 0 and options=count and see no entities and fiware-total-count: 0 -=========================================================================================================== +01. GET /v2/registrations with 'limit' == 0 and options=count and see no registrations and fiware-total-count: 0 +================================================================================================================ HTTP/1.1 200 OK -Content-Length: 2 -Content-Type: application/json +Content-Length: 0 Fiware-Total-Count: 0 Fiware-Correlator: REGEX([0-9a-f\-]{36}) Date: REGEX(.*) -[] -02. POST /v2/registrations to create E1 entity -============================================== +02. POST /v2/registrations to create registration +================================================= HTTP/1.1 201 Created Content-Length: 0 Location: /v2/registrations/REGEX([0-9a-f\-]{24}) @@ -135,8 +133,8 @@ Date: REGEX(.*) -03. POST /v2/registrations to create E2 entity -============================================== +03. POST /v2/registrations to create another registration +========================================================= HTTP/1.1 201 Created Content-Length: 0 Location: /v2/registrations/REGEX([0-9a-f\-]{24}) @@ -145,121 +143,23 @@ Date: REGEX(.*) -04. GET /v2/registrations with 'limit' == 0 and options=count and see no entities and fiware-total-count: 2 -=========================================================================================================== +04. GET /v2/registrations with 'limit' == 0 and options=count and see no registrations and fiware-total-count: 2 +================================================================================================================ HTTP/1.1 200 OK -Content-Length: 527 -Content-Type: application/json +Content-Length: 0 Fiware-Total-Count: 2 Fiware-Correlator: REGEX([0-9a-f\-]{36}) Date: REGEX(.*) -[ - { - "dataProvided": { - "attrs": [ - "A1" - ], - "entities": [ - { - "id": "E1", - "type": "T1" - } - ] - }, - "description": "located in CP1", - "id": "REGEX([0-9a-f\-]{24})", - "provider": { - "http": { - "url": "http://localhost:REGEX(\d+)/v2" - }, - "legacyForwarding": true, - "supportedForwardingMode": "all" - }, - "status": "active" - }, - { - "dataProvided": { - "attrs": [ - "A2" - ], - "entities": [ - { - "id": "E2", - "type": "T1" - } - ] - }, - "description": "located in CP1", - "id": "REGEX([0-9a-f\-]{24})", - "provider": { - "http": { - "url": "http://localhost:REGEX(\d+)/v2" - }, - "legacyForwarding": true, - "supportedForwardingMode": "all" - }, - "status": "active" - } -] - - -05. GET /v2/registrations with 'limit' == 0 and see no entities -=============================================================== + + +05. GET /v2/registrations with 'limit' == 0 and see no registrations +==================================================================== HTTP/1.1 200 OK -Content-Length: 527 -Content-Type: application/json +Content-Length: 0 Fiware-Correlator: REGEX([0-9a-f\-]{36}) Date: REGEX(.*) -[ - { - "dataProvided": { - "attrs": [ - "A1" - ], - "entities": [ - { - "id": "E1", - "type": "T1" - } - ] - }, - "description": "located in CP1", - "id": "REGEX([0-9a-f\-]{24})", - "provider": { - "http": { - "url": "http://localhost:REGEX(\d+)/v2" - }, - "legacyForwarding": true, - "supportedForwardingMode": "all" - }, - "status": "active" - }, - { - "dataProvided": { - "attrs": [ - "A2" - ], - "entities": [ - { - "id": "E2", - "type": "T1" - } - ] - }, - "description": "located in CP1", - "id": "REGEX([0-9a-f\-]{24})", - "provider": { - "http": { - "url": "http://localhost:REGEX(\d+)/v2" - }, - "legacyForwarding": true, - "supportedForwardingMode": "all" - }, - "status": "active" - } -] --TEARDOWN-- diff --git a/test/functionalTest/cases/1492_limit_zero/limit_zero_subs.test b/test/functionalTest/cases/1492_limit_zero/limit_zero_subs.test index 8a4a943583..a4da6c364e 100644 --- a/test/functionalTest/cases/1492_limit_zero/limit_zero_subs.test +++ b/test/functionalTest/cases/1492_limit_zero/limit_zero_subs.test @@ -30,22 +30,22 @@ brokerStart CB --SHELL-- # -# 01. GET /v2/subscriptions with 'limit' == 0 and options=count and see no entities and fiware-total-count: 0 -# 02. POST /v2/subscriptions to create E1 entity -# 03. POST /v2/subscriptions to create E2 entity -# 04. GET /v2/subscriptions with 'limit' == 0 and options=count and see no entities and fiware-total-count: 2 -# 05. GET /v2/subscriptions 'limit' == 0 and see no entities +# 01. GET /v2/subscriptions with 'limit' == 0 and options=count and see no subscriptions and fiware-total-count: 0 +# 02. POST /v2/subscriptions to create subscription +# 03. POST /v2/subscriptions to create another subscription +# 04. GET /v2/subscriptions with 'limit' == 0 and options=count and see no subscriptions and fiware-total-count: 2 +# 05. GET /v2/subscriptions 'limit' == 0 and see no subscriptions # -echo "01. GET /v2/subscriptions with 'limit' == 0 and options=count and see no entities and fiware-total-count: 0" -echo "===========================================================================================================" +echo "01. GET /v2/subscriptions with 'limit' == 0 and options=count and see no subscriptions and fiware-total-count: 0" +echo "================================================================================================================" orionCurl --url "/v2/subscriptions?limit=0&options=count" echo echo -echo "02. POST /v2/subscriptions to create E1 entity" -echo "==============================================" +echo "02. POST /v2/subscriptions to create subscription" +echo "=================================================" payload='{ "description": "A subscription to get info about E1", "subject": { @@ -76,8 +76,8 @@ echo echo -echo "03. POST /v2/subscriptions to create E2 entity" -echo "==============================================" +echo "03. POST /v2/subscriptions to create another subscription" +echo "=========================================================" payload='{ "description": "A subscription to get info about E2", "subject": { @@ -109,15 +109,15 @@ echo -echo "04. GET /v2/subscriptions with 'limit' == 0 and options=count and see no entities and fiware-total-count: 2" -echo "===========================================================================================================" +echo "04. GET /v2/subscriptions with 'limit' == 0 and options=count and see no subscriptions and fiware-total-count: 2" +echo "================================================================================================================" orionCurl --url "/v2/subscriptions?limit=0&options=count" echo echo -echo "05. GET /v2/subscriptions with 'limit' == 0 and see no entities" -echo "===============================================================" +echo "05. GET /v2/subscriptions with 'limit' == 0 and see no subscriptions" +echo "====================================================================" orionCurl --url "/v2/subscriptions?limit=0" echo echo @@ -125,20 +125,18 @@ echo --REGEXPECT-- -01. GET /v2/subscriptions with 'limit' == 0 and options=count and see no entities and fiware-total-count: 0 -=========================================================================================================== +01. GET /v2/subscriptions with 'limit' == 0 and options=count and see no subscriptions and fiware-total-count: 0 +================================================================================================================ HTTP/1.1 200 OK -Content-Length: 2 -Content-Type: application/json +Content-Length: 0 Fiware-Total-Count: 0 Fiware-Correlator: REGEX([0-9a-f\-]{36}) Date: REGEX(.*) -[] -02. POST /v2/subscriptions to create E1 entity -============================================== +02. POST /v2/subscriptions to create subscription +================================================= HTTP/1.1 201 Created Content-Length: 0 Location: /v2/subscriptions/REGEX([0-9a-f]{24}) @@ -147,8 +145,8 @@ Date: REGEX(.*) -03. POST /v2/subscriptions to create E2 entity -============================================== +03. POST /v2/subscriptions to create another subscription +========================================================= HTTP/1.1 201 Created Content-Length: 0 Location: /v2/subscriptions/REGEX([0-9a-f]{24}) @@ -157,141 +155,23 @@ Date: REGEX(.*) -04. GET /v2/subscriptions with 'limit' == 0 and options=count and see no entities and fiware-total-count: 2 -=========================================================================================================== +04. GET /v2/subscriptions with 'limit' == 0 and options=count and see no subscriptions and fiware-total-count: 2 +================================================================================================================ HTTP/1.1 200 OK -Content-Length: 625 -Content-Type: application/json +Content-Length: 0 Fiware-Total-Count: 2 Fiware-Correlator: REGEX([0-9a-f\-]{36}) Date: REGEX(.*) -[ - { - "description": "A subscription to get info about E1", - "id": "REGEX([0-9a-f]{24})", - "notification": { - "attrs": [ - "A1" - ], - "attrsFormat": "normalized", - "http": { - "url": "http://localhost:REGEX(\d+)/notify" - }, - "onlyChangedAttrs": false - }, - "status": "active", - "subject": { - "condition": { - "attrs": [ - "A1" - ] - }, - "entities": [ - { - "id": "E1", - "type": "T1" - } - ] - } - }, - { - "description": "A subscription to get info about E2", - "id": "REGEX([0-9a-f]{24})", - "notification": { - "attrs": [ - "A2" - ], - "attrsFormat": "normalized", - "http": { - "url": "http://localhost:REGEX(\d+)/notify" - }, - "onlyChangedAttrs": false - }, - "status": "active", - "subject": { - "condition": { - "attrs": [ - "A2" - ] - }, - "entities": [ - { - "id": "E2", - "type": "T1" - } - ] - } - } -] -05. GET /v2/subscriptions with 'limit' == 0 and see no entities -=============================================================== +05. GET /v2/subscriptions with 'limit' == 0 and see no subscriptions +==================================================================== HTTP/1.1 200 OK -Content-Length: 625 -Content-Type: application/json +Content-Length: 0 Fiware-Correlator: REGEX([0-9a-f\-]{36}) Date: REGEX(.*) -[ - { - "description": "A subscription to get info about E1", - "id": "REGEX([0-9a-f]{24})", - "notification": { - "attrs": [ - "A1" - ], - "attrsFormat": "normalized", - "http": { - "url": "http://localhost:REGEX(\d+)/notify" - }, - "onlyChangedAttrs": false - }, - "status": "active", - "subject": { - "condition": { - "attrs": [ - "A1" - ] - }, - "entities": [ - { - "id": "E1", - "type": "T1" - } - ] - } - }, - { - "description": "A subscription to get info about E2", - "id": "REGEX([0-9a-f]{24})", - "notification": { - "attrs": [ - "A2" - ], - "attrsFormat": "normalized", - "http": { - "url": "http://localhost:REGEX(\d+)/notify" - }, - "onlyChangedAttrs": false - }, - "status": "active", - "subject": { - "condition": { - "attrs": [ - "A2" - ] - }, - "entities": [ - { - "id": "E2", - "type": "T1" - } - ] - } - } -] --TEARDOWN-- From 7d6160fe7d730b3bff2b09f9517025eeca9eda60 Mon Sep 17 00:00:00 2001 From: Anjali Pathak Date: Thu, 18 Nov 2021 17:56:20 +0530 Subject: [PATCH 07/16] Update getEntities.cpp --- src/lib/serviceRoutinesV2/getEntities.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/lib/serviceRoutinesV2/getEntities.cpp b/src/lib/serviceRoutinesV2/getEntities.cpp index 1ab1148e9e..af4e80eb78 100644 --- a/src/lib/serviceRoutinesV2/getEntities.cpp +++ b/src/lib/serviceRoutinesV2/getEntities.cpp @@ -290,7 +290,7 @@ std::string getEntities { parseDataP->qcr.res.fill(pattern, type, "true", EntityTypeNotEmpty, ""); } - else + else { // // More than one type listed in URI param 'type': From 64cf793d7fae599dcef70fc063ce6302eeb9e5bd Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C3=84njali?= Date: Tue, 30 Nov 2021 18:19:36 +0000 Subject: [PATCH 08/16] Updated code and test case --- CHANGES_NEXT_RELEASE | 2 +- src/lib/jsonParseV2/parseSubscription.cpp | 4 +- src/lib/serviceRoutines/postQueryContext.cpp | 20 +-- src/lib/serviceRoutinesV2/getEntities.cpp | 14 +- .../serviceRoutinesV2/getEntityAllTypes.cpp | 7 +- .../1492_limit_zero/limit_zero_entities.test | 2 + .../1492_limit_zero/limit_zero_types.test | 136 ++++++++++++++++++ 7 files changed, 165 insertions(+), 20 deletions(-) create mode 100644 test/functionalTest/cases/1492_limit_zero/limit_zero_types.test diff --git a/CHANGES_NEXT_RELEASE b/CHANGES_NEXT_RELEASE index 5730a70685..9174e1e5f4 100644 --- a/CHANGES_NEXT_RELEASE +++ b/CHANGES_NEXT_RELEASE @@ -1,4 +1,4 @@ -- Fix: Using limit=0 and options=count to get only the count of elements (#1492) +- Fix: allow limit=0 in all paginated operations in the NGSIv2 API (entities, entity types, subscriptions and registrations) (#1492) - Hardening: noCache flag should be check before removing csub from cache in mongoUnsubscribeContext() (#2879) - Add: maxFailsLimit subscription field, so subscription is automatically passed to inactive after that number of failed notification attemps (#3541) - Add: failsCounter subscription field, to count the number of consecutive notificaitons fails (#3541) diff --git a/src/lib/jsonParseV2/parseSubscription.cpp b/src/lib/jsonParseV2/parseSubscription.cpp index ed01c71211..9b0e2194e2 100644 --- a/src/lib/jsonParseV2/parseSubscription.cpp +++ b/src/lib/jsonParseV2/parseSubscription.cpp @@ -476,7 +476,7 @@ static std::string parseTimeout(ConnectionInfo* ciP, SubscriptionUpdate* subsP, { return badInput(ciP, timeoutOpt.error); } - if (timeoutOpt.given) +/* if (timeoutOpt.given) { if ((timeoutOpt.value < 0) || (timeoutOpt.value > MAX_HTTP_TIMEOUT)) { @@ -486,7 +486,7 @@ static std::string parseTimeout(ConnectionInfo* ciP, SubscriptionUpdate* subsP, { subsP->notification.httpInfo.timeout = timeoutOpt.value; } - } + }*/ else { subsP->notification.httpInfo.timeout = 0; diff --git a/src/lib/serviceRoutines/postQueryContext.cpp b/src/lib/serviceRoutines/postQueryContext.cpp index f3dc99af3f..f8f7d9751c 100644 --- a/src/lib/serviceRoutines/postQueryContext.cpp +++ b/src/lib/serviceRoutines/postQueryContext.cpp @@ -472,6 +472,7 @@ std::string postQueryContext QueryContextRequestVector requestV; std::vector regIdsV; QueryContextResponseVector responseV; + int limit = atoi(ciP->uriParam[URI_PARAM_PAGINATION_LIMIT].c_str()); long long count = 0; long long* countP = NULL; @@ -504,14 +505,17 @@ std::string postQueryContext // qcrsP->errorCode.fill(SccOk); - TIMED_MONGO(ciP->httpStatusCode = mongoQueryContext(qcrP, - qcrsP, - ciP->tenant, - ciP->servicePathV, - ciP->uriParam, - ciP->uriParamOptions, - countP, - ciP->apiVersion)); + if (limit != 0) + { + TIMED_MONGO(ciP->httpStatusCode = mongoQueryContext(qcrP, + qcrsP, + ciP->tenant, + ciP->servicePathV, + ciP->uriParam, + ciP->uriParamOptions, + countP, + ciP->apiVersion)); + } if ((qcrsP->errorCode.code == SccBadRequest) || (qcrsP->errorCode.code == SccReceiverInternalError)) { diff --git a/src/lib/serviceRoutinesV2/getEntities.cpp b/src/lib/serviceRoutinesV2/getEntities.cpp index 1ab1148e9e..e35a3f50ed 100644 --- a/src/lib/serviceRoutinesV2/getEntities.cpp +++ b/src/lib/serviceRoutinesV2/getEntities.cpp @@ -86,7 +86,6 @@ std::string getEntities std::string answer; std::string pattern = ".*"; // all entities, default value std::string id = ciP->uriParam["id"]; - int limit = atoi(ciP->uriParam[URI_PARAM_PAGINATION_LIMIT].c_str()); std::string idPattern = ciP->uriParam["idPattern"]; std::string type = ciP->uriParam["type"]; std::string typePattern = ciP->uriParam["typePattern"]; @@ -305,13 +304,12 @@ std::string getEntities } // Get attrs and metadata filters from URL params - setAttrsFilter(ciP->uriParam, ciP->uriParamOptions, &parseDataP->qcr.res.attrsList); - setMetadataFilter(ciP->uriParam, &parseDataP->qcr.res.metadataList); - // 02. Call standard op postQueryContext - if (limit != 0) - { - answer = postQueryContext(ciP, components, compV, parseDataP); - } + setAttrsFilter(ciP->uriParam, ciP->uriParamOptions, &parseDataP->qcr.res.attrsList); + setMetadataFilter(ciP->uriParam, &parseDataP->qcr.res.metadataList); + + // 02. Call standard op postQueryContext + answer = postQueryContext(ciP, components, compV, parseDataP); + // 03. Check Internal Errors if (parseDataP->qcrs.res.errorCode.code == SccReceiverInternalError) { diff --git a/src/lib/serviceRoutinesV2/getEntityAllTypes.cpp b/src/lib/serviceRoutinesV2/getEntityAllTypes.cpp index 39d4481bd3..526f0ebfe2 100644 --- a/src/lib/serviceRoutinesV2/getEntityAllTypes.cpp +++ b/src/lib/serviceRoutinesV2/getEntityAllTypes.cpp @@ -63,6 +63,7 @@ std::string getEntityAllTypes unsigned int totalTypes = 0; bool noAttrDetail = ciP->uriParamOptions[OPT_NO_ATTR_DETAIL]; unsigned int* totalTypesP = NULL; + int limit = atoi(ciP->uriParam[URI_PARAM_PAGINATION_LIMIT].c_str()); // NGSIv2 uses options=count to request count if (ciP->uriParamOptions[OPT_COUNT]) @@ -84,7 +85,11 @@ std::string getEntityAllTypes totalTypesP, noAttrDetail)); } - TIMED_RENDER(answer = response.toJson(ciP->uriParamOptions[OPT_VALUES])); + + if (limit != 0) + { + TIMED_RENDER(answer = response.toJson(ciP->uriParamOptions[OPT_VALUES])); + } if (ciP->uriParamOptions[OPT_COUNT]) { diff --git a/test/functionalTest/cases/1492_limit_zero/limit_zero_entities.test b/test/functionalTest/cases/1492_limit_zero/limit_zero_entities.test index 6a74f74607..510c0919de 100644 --- a/test/functionalTest/cases/1492_limit_zero/limit_zero_entities.test +++ b/test/functionalTest/cases/1492_limit_zero/limit_zero_entities.test @@ -87,6 +87,7 @@ echo HTTP/1.1 200 OK Content-Length: 2 Content-Type: application/json +Fiware-Total-Count: 0 Fiware-Correlator: REGEX([0-9a-f\-]{36}) Date: REGEX(.*) @@ -118,6 +119,7 @@ Date: REGEX(.*) HTTP/1.1 200 OK Content-Length: 2 Content-Type: application/json +Fiware-Total-Count: 0 Fiware-Correlator: REGEX([0-9a-f\-]{36}) Date: REGEX(.*) diff --git a/test/functionalTest/cases/1492_limit_zero/limit_zero_types.test b/test/functionalTest/cases/1492_limit_zero/limit_zero_types.test new file mode 100644 index 0000000000..8bf5851769 --- /dev/null +++ b/test/functionalTest/cases/1492_limit_zero/limit_zero_types.test @@ -0,0 +1,136 @@ +# Copyright 2021 Telefonica Investigacion y Desarrollo, S.A.U +# +# This file is part of Orion Context Broker. +# +# Orion Context Broker is free software: you can redistribute it and/or +# modify it under the terms of the GNU Affero General Public License as +# published by the Free Software Foundation, either version 3 of the +# License, or (at your option) any later version. +# +# Orion Context Broker is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Affero +# General Public License for more details. +# +# You should have received a copy of the GNU Affero General Public License +# along with Orion Context Broker. If not, see http://www.gnu.org/licenses/. +# +# For those usages not covered by this license please contact with +# iot_support at tid dot es + +# VALGRIND_READY - to mark the test ready for valgrindTestSuite.sh + +--NAME-- +limit_zero_types + +--SHELL-INIT-- +dbInit CB +brokerStart CB + +--SHELL-- + +# +# 01. GET /v2/types with 'limit' == 0 and options=count and see no types and fiware-total-count: 0 +# 02. POST /v2/entities to create E1 entity with type T1 +# 03. POST /v2/entities to create E2 entity with type T2 +# 04. GET /v2/types with 'limit' == 0 and options=count and see no types and fiware-total-count: 2 +# 05. GET /v2/types with 'limit' == 0 and see no types +# + +echo "01. GET /v2/types with 'limit' == 0 and options=count and see no types and fiware-total-count: 0" +echo "================================================================================================" +orionCurl --url "/v2/types?limit=0&options=count" +echo +echo + + +echo "02. POST /v2/entities to create E1 entity with type T1" +echo "======================================================" +payload='{ + "id": "E1", + "type": "T1" +}' +orionCurl --url /v2/entities -X POST --payload "$payload" +echo +echo + + +echo "03. POST /v2/entities to create E2 entity with type T2" +echo "======================================================" +payload='{ + "id": "E2", + "type": "T2" +}' +orionCurl --url /v2/entities -X POST --payload "$payload" +echo +echo + + +echo "04. GET /v2/types with 'limit' == 0 and options=count and see no types and fiware-total-count: 2" +echo "================================================================================================" +orionCurl --url "/v2/types?limit=0&options=count" +echo +echo + + +echo "05. GET /v2/types with 'limit' == 0 and see no types" +echo "====================================================" +orionCurl --url "/v2/types?limit=0" +echo +echo + + + +--REGEXPECT-- +01. GET /v2/types with 'limit' == 0 and options=count and see no types and fiware-total-count: 0 +================================================================================================ +HTTP/1.1 200 OK +Content-Length: 0 +Fiware-Total-Count: 0 +Fiware-Correlator: REGEX([0-9a-f\-]{36}) +Date: REGEX(.*) + + + +02. POST /v2/entities to create E1 entity with type T1 +====================================================== +HTTP/1.1 201 Created +Content-Length: 0 +Location: /v2/entities/E1?type=T1 +Fiware-Correlator: REGEX([0-9a-f\-]{36}) +Date: REGEX(.*) + + + +03. POST /v2/entities to create E2 entity with type T2 +====================================================== +HTTP/1.1 201 Created +Content-Length: 0 +Location: /v2/entities/E2?type=T2 +Fiware-Correlator: REGEX([0-9a-f\-]{36}) +Date: REGEX(.*) + + + +04. GET /v2/types with 'limit' == 0 and options=count and see no types and fiware-total-count: 2 +================================================================================================ +HTTP/1.1 200 OK +Content-Length: 0 +Fiware-Total-Count: 2 +Fiware-Correlator: REGEX([0-9a-f\-]{36}) +Date: REGEX(.*) + + + +05. GET /v2/types with 'limit' == 0 and see no types +==================================================== +HTTP/1.1 200 OK +Content-Length: 0 +Fiware-Correlator: REGEX([0-9a-f\-]{36}) +Date: REGEX(.*) + + + +--TEARDOWN-- +brokerStop CB +dbDrop CB From 8a03332e3df26dc5d17d0df34121a6006b0e2955 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C3=84njali?= Date: Tue, 30 Nov 2021 18:28:17 +0000 Subject: [PATCH 09/16] Updated code --- src/lib/jsonParseV2/parseSubscription.cpp | 4 ++-- src/lib/serviceRoutinesV2/getEntities.cpp | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/src/lib/jsonParseV2/parseSubscription.cpp b/src/lib/jsonParseV2/parseSubscription.cpp index 9b0e2194e2..ed01c71211 100644 --- a/src/lib/jsonParseV2/parseSubscription.cpp +++ b/src/lib/jsonParseV2/parseSubscription.cpp @@ -476,7 +476,7 @@ static std::string parseTimeout(ConnectionInfo* ciP, SubscriptionUpdate* subsP, { return badInput(ciP, timeoutOpt.error); } -/* if (timeoutOpt.given) + if (timeoutOpt.given) { if ((timeoutOpt.value < 0) || (timeoutOpt.value > MAX_HTTP_TIMEOUT)) { @@ -486,7 +486,7 @@ static std::string parseTimeout(ConnectionInfo* ciP, SubscriptionUpdate* subsP, { subsP->notification.httpInfo.timeout = timeoutOpt.value; } - }*/ + } else { subsP->notification.httpInfo.timeout = 0; diff --git a/src/lib/serviceRoutinesV2/getEntities.cpp b/src/lib/serviceRoutinesV2/getEntities.cpp index 96a72e1a95..395930e67a 100644 --- a/src/lib/serviceRoutinesV2/getEntities.cpp +++ b/src/lib/serviceRoutinesV2/getEntities.cpp @@ -307,7 +307,7 @@ std::string getEntities setAttrsFilter(ciP->uriParam, ciP->uriParamOptions, &parseDataP->qcr.res.attrsList); setMetadataFilter(ciP->uriParam, &parseDataP->qcr.res.metadataList); - // 02. Call standard op postQueryContext + // 02. Call standard op postQueryContext answer = postQueryContext(ciP, components, compV, parseDataP); // 03. Check Internal Errors From 67ea727c02d09a50bde56fcad40323bd372738c4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C3=84njali?= Date: Mon, 24 Jan 2022 12:06:50 +0000 Subject: [PATCH 10/16] Updated as per comment --- .../mongoBackend/mongoGetSubscriptions.cpp | 26 ++++++------ src/lib/mongoBackend/mongoQueryContext.cpp | 42 ++++++++++--------- src/lib/mongoBackend/mongoQueryTypes.cpp | 1 + src/lib/mongoBackend/mongoRegistrationGet.cpp | 5 ++- src/lib/serviceRoutines/postQueryContext.cpp | 4 -- .../serviceRoutinesV2/getAllSubscriptions.cpp | 3 -- .../serviceRoutinesV2/getEntityAllTypes.cpp | 6 --- .../serviceRoutinesV2/getRegistrations.cpp | 3 -- .../1492_limit_zero/limit_zero_entities.test | 2 +- .../1492_limit_zero/limit_zero_regs.test | 12 ++++-- .../1492_limit_zero/limit_zero_subs.test | 12 ++++-- .../1492_limit_zero/limit_zero_types.test | 10 ++++- 12 files changed, 68 insertions(+), 58 deletions(-) diff --git a/src/lib/mongoBackend/mongoGetSubscriptions.cpp b/src/lib/mongoBackend/mongoGetSubscriptions.cpp index a82b1b895b..6a74d676e0 100644 --- a/src/lib/mongoBackend/mongoGetSubscriptions.cpp +++ b/src/lib/mongoBackend/mongoGetSubscriptions.cpp @@ -317,23 +317,25 @@ void mongoListSubscriptions /* Process query result */ unsigned int docs = 0; - orion::BSONObj r; - while (cursor.next(&r)) + if (limit != 0) { - docs++; - LM_T(LmtMongo, ("retrieved document [%d]: '%s'", docs, r.toString().c_str())); + orion::BSONObj r; + while (cursor.next(&r)) + { + docs++; + LM_T(LmtMongo, ("retrieved document [%d]: '%s'", docs, r.toString().c_str())); - Subscription s; + Subscription s; - setNewSubscriptionId(&s, r); - setDescription(&s, r); - setSubject(&s, r); - setStatus(&s, r, tenant); - setNotification(&s, r, tenant); + setNewSubscriptionId(&s, r); + setDescription(&s, r); + setSubject(&s, r); + setStatus(&s, r, tenant); + setNotification(&s, r, tenant); - subs->push_back(s); + subs->push_back(s); + } } - orion::releaseMongoConnection(connection); reqSemGive(__FUNCTION__, "Mongo List Subscriptions", reqSemTaken); diff --git a/src/lib/mongoBackend/mongoQueryContext.cpp b/src/lib/mongoBackend/mongoQueryContext.cpp index ad89ce71d4..7c9a4db305 100644 --- a/src/lib/mongoBackend/mongoQueryContext.cpp +++ b/src/lib/mongoBackend/mongoQueryContext.cpp @@ -357,28 +357,30 @@ HttpStatusCode mongoQueryContext reqSemTake(__FUNCTION__, "ngsi10 query request", SemReadOp, &reqSemTaken); - ok = entitiesQuery(requestP->entityIdVector, - requestP->attributeList, - requestP->restriction, - &rawCerV, - &err, - true, - tenant, - servicePathV, - offset, - limit, - &limitReached, - countP, - sortOrderList, - apiVersion); - - if (!ok) + if (limit != 0) { - responseP->errorCode.fill(SccReceiverInternalError, err); - rawCerV.release(); - reqSemGive(__FUNCTION__, "ngsi10 query request", reqSemTaken); + ok = entitiesQuery(requestP->entityIdVector, + requestP->attributeList, + requestP->restriction, + &rawCerV, + &err, + true, + tenant, + servicePathV, + offset, + limit, + &limitReached, + countP, + sortOrderList, + apiVersion); + if (!ok) + { + responseP->errorCode.fill(SccReceiverInternalError, err); + rawCerV.release(); + reqSemGive(__FUNCTION__, "ngsi10 query request", reqSemTaken); - return SccOk; + return SccOk; + } } ContextRegistrationResponseVector crrV; diff --git a/src/lib/mongoBackend/mongoQueryTypes.cpp b/src/lib/mongoBackend/mongoQueryTypes.cpp index fc342273ce..4ef0db4911 100644 --- a/src/lib/mongoBackend/mongoQueryTypes.cpp +++ b/src/lib/mongoBackend/mongoQueryTypes.cpp @@ -655,6 +655,7 @@ HttpStatusCode mongoEntityTypes *totalTypesP = docs; char detailsMsg[256]; snprintf(detailsMsg, sizeof(detailsMsg), "Count: %u", *totalTypesP); + responseP->statusCode.fill(SccOk, detailsMsg); } else diff --git a/src/lib/mongoBackend/mongoRegistrationGet.cpp b/src/lib/mongoBackend/mongoRegistrationGet.cpp index 9b8f3317f6..63ff6bec12 100644 --- a/src/lib/mongoBackend/mongoRegistrationGet.cpp +++ b/src/lib/mongoBackend/mongoRegistrationGet.cpp @@ -401,7 +401,10 @@ void mongoRegistrationsGet setExpires(®, r); setStatus(®, r); - regV->push_back(reg); + if (limit != 0) + { + regV->push_back(reg); + } } orion::releaseMongoConnection(connection); diff --git a/src/lib/serviceRoutines/postQueryContext.cpp b/src/lib/serviceRoutines/postQueryContext.cpp index f8f7d9751c..438b8f430a 100644 --- a/src/lib/serviceRoutines/postQueryContext.cpp +++ b/src/lib/serviceRoutines/postQueryContext.cpp @@ -472,7 +472,6 @@ std::string postQueryContext QueryContextRequestVector requestV; std::vector regIdsV; QueryContextResponseVector responseV; - int limit = atoi(ciP->uriParam[URI_PARAM_PAGINATION_LIMIT].c_str()); long long count = 0; long long* countP = NULL; @@ -505,8 +504,6 @@ std::string postQueryContext // qcrsP->errorCode.fill(SccOk); - if (limit != 0) - { TIMED_MONGO(ciP->httpStatusCode = mongoQueryContext(qcrP, qcrsP, ciP->tenant, @@ -515,7 +512,6 @@ std::string postQueryContext ciP->uriParamOptions, countP, ciP->apiVersion)); - } if ((qcrsP->errorCode.code == SccBadRequest) || (qcrsP->errorCode.code == SccReceiverInternalError)) { diff --git a/src/lib/serviceRoutinesV2/getAllSubscriptions.cpp b/src/lib/serviceRoutinesV2/getAllSubscriptions.cpp index 19f0828595..97fe5c7bfc 100644 --- a/src/lib/serviceRoutinesV2/getAllSubscriptions.cpp +++ b/src/lib/serviceRoutinesV2/getAllSubscriptions.cpp @@ -88,10 +88,7 @@ std::string getAllSubscriptions std::string out; - if (limit != 0) - { TIMED_RENDER(out = vectorToJson(subs)); - } return out; } diff --git a/src/lib/serviceRoutinesV2/getEntityAllTypes.cpp b/src/lib/serviceRoutinesV2/getEntityAllTypes.cpp index 526f0ebfe2..a4655b025a 100644 --- a/src/lib/serviceRoutinesV2/getEntityAllTypes.cpp +++ b/src/lib/serviceRoutinesV2/getEntityAllTypes.cpp @@ -63,7 +63,6 @@ std::string getEntityAllTypes unsigned int totalTypes = 0; bool noAttrDetail = ciP->uriParamOptions[OPT_NO_ATTR_DETAIL]; unsigned int* totalTypesP = NULL; - int limit = atoi(ciP->uriParam[URI_PARAM_PAGINATION_LIMIT].c_str()); // NGSIv2 uses options=count to request count if (ciP->uriParamOptions[OPT_COUNT]) @@ -86,11 +85,6 @@ std::string getEntityAllTypes noAttrDetail)); } - if (limit != 0) - { - TIMED_RENDER(answer = response.toJson(ciP->uriParamOptions[OPT_VALUES])); - } - if (ciP->uriParamOptions[OPT_COUNT]) { char cVec[64]; diff --git a/src/lib/serviceRoutinesV2/getRegistrations.cpp b/src/lib/serviceRoutinesV2/getRegistrations.cpp index 9a8c2c9892..8fda6fe15a 100644 --- a/src/lib/serviceRoutinesV2/getRegistrations.cpp +++ b/src/lib/serviceRoutinesV2/getRegistrations.cpp @@ -87,10 +87,7 @@ std::string getRegistrations ciP->httpHeaderValue.push_back(double2string(count)); } - if (limit != 0) - { TIMED_RENDER(out = vectorToJson(registrationV)); - } return out; } diff --git a/test/functionalTest/cases/1492_limit_zero/limit_zero_entities.test b/test/functionalTest/cases/1492_limit_zero/limit_zero_entities.test index 510c0919de..b649542d60 100644 --- a/test/functionalTest/cases/1492_limit_zero/limit_zero_entities.test +++ b/test/functionalTest/cases/1492_limit_zero/limit_zero_entities.test @@ -119,7 +119,7 @@ Date: REGEX(.*) HTTP/1.1 200 OK Content-Length: 2 Content-Type: application/json -Fiware-Total-Count: 0 +Fiware-Total-Count: 2 Fiware-Correlator: REGEX([0-9a-f\-]{36}) Date: REGEX(.*) diff --git a/test/functionalTest/cases/1492_limit_zero/limit_zero_regs.test b/test/functionalTest/cases/1492_limit_zero/limit_zero_regs.test index 8e57657350..bcdf138a57 100644 --- a/test/functionalTest/cases/1492_limit_zero/limit_zero_regs.test +++ b/test/functionalTest/cases/1492_limit_zero/limit_zero_regs.test @@ -116,11 +116,13 @@ echo 01. GET /v2/registrations with 'limit' == 0 and options=count and see no registrations and fiware-total-count: 0 ================================================================================================================ HTTP/1.1 200 OK -Content-Length: 0 +Content-Length: 2 +Content-Type: application/json Fiware-Total-Count: 0 Fiware-Correlator: REGEX([0-9a-f\-]{36}) Date: REGEX(.*) +[] 02. POST /v2/registrations to create registration @@ -146,20 +148,24 @@ Date: REGEX(.*) 04. GET /v2/registrations with 'limit' == 0 and options=count and see no registrations and fiware-total-count: 2 ================================================================================================================ HTTP/1.1 200 OK -Content-Length: 0 +Content-Length: 2 +Content-Type: application/json Fiware-Total-Count: 2 Fiware-Correlator: REGEX([0-9a-f\-]{36}) Date: REGEX(.*) +[] 05. GET /v2/registrations with 'limit' == 0 and see no registrations ==================================================================== HTTP/1.1 200 OK -Content-Length: 0 +Content-Length: 2 +Content-Type: application/json Fiware-Correlator: REGEX([0-9a-f\-]{36}) Date: REGEX(.*) +[] --TEARDOWN-- diff --git a/test/functionalTest/cases/1492_limit_zero/limit_zero_subs.test b/test/functionalTest/cases/1492_limit_zero/limit_zero_subs.test index a4da6c364e..a64b647c49 100644 --- a/test/functionalTest/cases/1492_limit_zero/limit_zero_subs.test +++ b/test/functionalTest/cases/1492_limit_zero/limit_zero_subs.test @@ -128,11 +128,13 @@ echo 01. GET /v2/subscriptions with 'limit' == 0 and options=count and see no subscriptions and fiware-total-count: 0 ================================================================================================================ HTTP/1.1 200 OK -Content-Length: 0 +Content-Length: 2 +Content-Type: application/json Fiware-Total-Count: 0 Fiware-Correlator: REGEX([0-9a-f\-]{36}) Date: REGEX(.*) +[] 02. POST /v2/subscriptions to create subscription @@ -158,20 +160,24 @@ Date: REGEX(.*) 04. GET /v2/subscriptions with 'limit' == 0 and options=count and see no subscriptions and fiware-total-count: 2 ================================================================================================================ HTTP/1.1 200 OK -Content-Length: 0 +Content-Length: 2 +Content-Type: application/json Fiware-Total-Count: 2 Fiware-Correlator: REGEX([0-9a-f\-]{36}) Date: REGEX(.*) +[] 05. GET /v2/subscriptions with 'limit' == 0 and see no subscriptions ==================================================================== HTTP/1.1 200 OK -Content-Length: 0 +Content-Length: 2 +Content-Type: application/json Fiware-Correlator: REGEX([0-9a-f\-]{36}) Date: REGEX(.*) +[] --TEARDOWN-- diff --git a/test/functionalTest/cases/1492_limit_zero/limit_zero_types.test b/test/functionalTest/cases/1492_limit_zero/limit_zero_types.test index 8bf5851769..af45f8d714 100644 --- a/test/functionalTest/cases/1492_limit_zero/limit_zero_types.test +++ b/test/functionalTest/cases/1492_limit_zero/limit_zero_types.test @@ -86,10 +86,12 @@ echo ================================================================================================ HTTP/1.1 200 OK Content-Length: 0 +Content-Type: application/json Fiware-Total-Count: 0 Fiware-Correlator: REGEX([0-9a-f\-]{36}) Date: REGEX(.*) +[] 02. POST /v2/entities to create E1 entity with type T1 @@ -115,20 +117,24 @@ Date: REGEX(.*) 04. GET /v2/types with 'limit' == 0 and options=count and see no types and fiware-total-count: 2 ================================================================================================ HTTP/1.1 200 OK -Content-Length: 0 +Content-Length: 2 +Content-Type: application/json Fiware-Total-Count: 2 Fiware-Correlator: REGEX([0-9a-f\-]{36}) Date: REGEX(.*) +[] 05. GET /v2/types with 'limit' == 0 and see no types ==================================================== HTTP/1.1 200 OK -Content-Length: 0 +Content-Length: 2 +Content-Type: application/json Fiware-Correlator: REGEX([0-9a-f\-]{36}) Date: REGEX(.*) +[] --TEARDOWN-- From 397ae9e496a9ce542907b10016aa464b69ba71a3 Mon Sep 17 00:00:00 2001 From: root Date: Fri, 4 Mar 2022 03:21:52 -0500 Subject: [PATCH 11/16] Modify code as per comment --- src/lib/mongoBackend/mongoQueryTypes.cpp | 1 - src/lib/serviceRoutines/postQueryContext.cpp | 16 ++++++++-------- .../serviceRoutinesV2/getAllSubscriptions.cpp | 1 - src/lib/serviceRoutinesV2/getEntityAllTypes.cpp | 1 + src/lib/serviceRoutinesV2/getRegistrations.cpp | 2 +- .../1492_limit_zero/limit_zero_entities.test | 2 +- 6 files changed, 11 insertions(+), 12 deletions(-) diff --git a/src/lib/mongoBackend/mongoQueryTypes.cpp b/src/lib/mongoBackend/mongoQueryTypes.cpp index 4ef0db4911..fc342273ce 100644 --- a/src/lib/mongoBackend/mongoQueryTypes.cpp +++ b/src/lib/mongoBackend/mongoQueryTypes.cpp @@ -655,7 +655,6 @@ HttpStatusCode mongoEntityTypes *totalTypesP = docs; char detailsMsg[256]; snprintf(detailsMsg, sizeof(detailsMsg), "Count: %u", *totalTypesP); - responseP->statusCode.fill(SccOk, detailsMsg); } else diff --git a/src/lib/serviceRoutines/postQueryContext.cpp b/src/lib/serviceRoutines/postQueryContext.cpp index 35e6cb59f4..6a567333e0 100644 --- a/src/lib/serviceRoutines/postQueryContext.cpp +++ b/src/lib/serviceRoutines/postQueryContext.cpp @@ -503,14 +503,14 @@ std::string postQueryContext // qcrsP->errorCode.fill(SccOk); - TIMED_MONGO(ciP->httpStatusCode = mongoQueryContext(qcrP, - qcrsP, - ciP->tenant, - ciP->servicePathV, - ciP->uriParam, - ciP->uriParamOptions, - countP, - ciP->apiVersion)); + TIMED_MONGO(ciP->httpStatusCode = mongoQueryContext(qcrP, + qcrsP, + ciP->tenant, + ciP->servicePathV, + ciP->uriParam, + ciP->uriParamOptions, + countP, + ciP->apiVersion)); if ((qcrsP->errorCode.code == SccBadRequest) || (qcrsP->errorCode.code == SccReceiverInternalError)) { diff --git a/src/lib/serviceRoutinesV2/getAllSubscriptions.cpp b/src/lib/serviceRoutinesV2/getAllSubscriptions.cpp index 97fe5c7bfc..8f4b50b099 100644 --- a/src/lib/serviceRoutinesV2/getAllSubscriptions.cpp +++ b/src/lib/serviceRoutinesV2/getAllSubscriptions.cpp @@ -87,7 +87,6 @@ std::string getAllSubscriptions } std::string out; - TIMED_RENDER(out = vectorToJson(subs)); return out; diff --git a/src/lib/serviceRoutinesV2/getEntityAllTypes.cpp b/src/lib/serviceRoutinesV2/getEntityAllTypes.cpp index a4655b025a..39d4481bd3 100644 --- a/src/lib/serviceRoutinesV2/getEntityAllTypes.cpp +++ b/src/lib/serviceRoutinesV2/getEntityAllTypes.cpp @@ -84,6 +84,7 @@ std::string getEntityAllTypes totalTypesP, noAttrDetail)); } + TIMED_RENDER(answer = response.toJson(ciP->uriParamOptions[OPT_VALUES])); if (ciP->uriParamOptions[OPT_COUNT]) { diff --git a/src/lib/serviceRoutinesV2/getRegistrations.cpp b/src/lib/serviceRoutinesV2/getRegistrations.cpp index 8fda6fe15a..9926c3a471 100644 --- a/src/lib/serviceRoutinesV2/getRegistrations.cpp +++ b/src/lib/serviceRoutinesV2/getRegistrations.cpp @@ -87,7 +87,7 @@ std::string getRegistrations ciP->httpHeaderValue.push_back(double2string(count)); } - TIMED_RENDER(out = vectorToJson(registrationV)); + TIMED_RENDER(out = vectorToJson(registrationV)); return out; } diff --git a/test/functionalTest/cases/1492_limit_zero/limit_zero_entities.test b/test/functionalTest/cases/1492_limit_zero/limit_zero_entities.test index b649542d60..40b6686825 100644 --- a/test/functionalTest/cases/1492_limit_zero/limit_zero_entities.test +++ b/test/functionalTest/cases/1492_limit_zero/limit_zero_entities.test @@ -32,7 +32,7 @@ brokerStart CB # # 01. GET /v2/entities with 'limit' == 0 and options=count and see no entities and fiware-total-count: 0 # 02. POST /v2/entities to create E1 entity -# 03. POST /v2/entities to create E1 entity +# 03. POST /v2/entities to create E2 entity # 04. GET /v2/entities with 'limit' == 0 and options=count and see no entities and fiware-total-count: 2 # 05. GET /v2/entities with 'limit' == 0 and see no entities # From ff2b2251b1f0a0a1703c4e40b51d163f72bcede8 Mon Sep 17 00:00:00 2001 From: root Date: Mon, 7 Mar 2022 06:43:38 -0500 Subject: [PATCH 12/16] Updated code --- src/lib/mongoBackend/MongoGlobal.cpp | 66 ++++++++++--------- src/lib/mongoBackend/mongoQueryContext.cpp | 41 ++++++------ src/lib/mongoBackend/mongoQueryTypes.cpp | 7 +- .../1492_limit_zero/limit_zero_types.test | 2 +- 4 files changed, 59 insertions(+), 57 deletions(-) diff --git a/src/lib/mongoBackend/MongoGlobal.cpp b/src/lib/mongoBackend/MongoGlobal.cpp index cdd06a5429..c15a9a7794 100644 --- a/src/lib/mongoBackend/MongoGlobal.cpp +++ b/src/lib/mongoBackend/MongoGlobal.cpp @@ -1560,51 +1560,53 @@ bool entitiesQuery int errType; std::string nextErr; - while (cursor.next(&r, &errType, &nextErr)) + if (limit != 0) { - alarmMgr.dbErrorReset(); - - // Build CER from BSON retrieved from DB - docs++; - LM_T(LmtMongo, ("retrieved document [%d]: '%s'", docs, r.toString().c_str())); - ContextElementResponse* cer = new ContextElementResponse(r, attrL, includeEmpty, apiVersion); - - // Add builtin attributes and metadata (only in NGSIv2) - if (apiVersion == V2) + while (cursor.next(&r, &errType, &nextErr)) { - addBuiltins(cer); - } + alarmMgr.dbErrorReset(); - /* All the attributes existing in the request but not found in the response are added with 'found' set to false */ - for (unsigned int ix = 0; ix < attrL.size(); ++ix) - { - bool found = false; - std::string attrName = attrL[ix]; + // Build CER from BSON retrieved from DB + docs++; + LM_T(LmtMongo, ("retrieved document [%d]: '%s'", docs, r.toString().c_str())); + ContextElementResponse* cer = new ContextElementResponse(r, attrL, includeEmpty, apiVersion); - /* The special case "*" is not taken into account*/ - if (attrName == ALL_ATTRS) + // Add builtin attributes and metadata (only in NGSIv2) + if (apiVersion == V2) { - continue; + addBuiltins(cer); } - for (unsigned int jx = 0; jx < cer->entity.attributeVector.size(); ++jx) + /* All the attributes existing in the request but not found in the response are added with 'found' set to false */ + for (unsigned int ix = 0; ix < attrL.size(); ++ix) { - if (attrName == cer->entity.attributeVector[jx]->name) + bool found = false; + std::string attrName = attrL[ix]; + + /* The special case "*" is not taken into account*/ + if (attrName == ALL_ATTRS) + { + continue; + } + + for (unsigned int jx = 0; jx < cer->entity.attributeVector.size(); ++jx) { - found = true; - break; + if (attrName == cer->entity.attributeVector[jx]->name) + { + found = true; + break; + } } - } - if (!found) - { - ContextAttribute* caP = new ContextAttribute(attrName, "", "", false); - cer->entity.attributeVector.push_back(caP); + if (!found) + { + ContextAttribute* caP = new ContextAttribute(attrName, "", "", false); + cer->entity.attributeVector.push_back(caP); + } } + cer->statusCode.fill(SccOk); + cerV->push_back(cer); } - - cer->statusCode.fill(SccOk); - cerV->push_back(cer); } orion::releaseMongoConnection(connection); diff --git a/src/lib/mongoBackend/mongoQueryContext.cpp b/src/lib/mongoBackend/mongoQueryContext.cpp index 7c9a4db305..3e11a71324 100644 --- a/src/lib/mongoBackend/mongoQueryContext.cpp +++ b/src/lib/mongoBackend/mongoQueryContext.cpp @@ -357,30 +357,27 @@ HttpStatusCode mongoQueryContext reqSemTake(__FUNCTION__, "ngsi10 query request", SemReadOp, &reqSemTaken); - if (limit != 0) + ok = entitiesQuery(requestP->entityIdVector, + requestP->attributeList, + requestP->restriction, + &rawCerV, + &err, + true, + tenant, + servicePathV, + offset, + limit, + &limitReached, + countP, + sortOrderList, + apiVersion); + if (!ok) { - ok = entitiesQuery(requestP->entityIdVector, - requestP->attributeList, - requestP->restriction, - &rawCerV, - &err, - true, - tenant, - servicePathV, - offset, - limit, - &limitReached, - countP, - sortOrderList, - apiVersion); - if (!ok) - { - responseP->errorCode.fill(SccReceiverInternalError, err); - rawCerV.release(); - reqSemGive(__FUNCTION__, "ngsi10 query request", reqSemTaken); + responseP->errorCode.fill(SccReceiverInternalError, err); + rawCerV.release(); + reqSemGive(__FUNCTION__, "ngsi10 query request", reqSemTaken); - return SccOk; - } + return SccOk; } ContextRegistrationResponseVector crrV; diff --git a/src/lib/mongoBackend/mongoQueryTypes.cpp b/src/lib/mongoBackend/mongoQueryTypes.cpp index fc342273ce..9cc7547b03 100644 --- a/src/lib/mongoBackend/mongoQueryTypes.cpp +++ b/src/lib/mongoBackend/mongoQueryTypes.cpp @@ -627,9 +627,12 @@ HttpStatusCode mongoEntityTypes entityType->contextAttributeVector.push_back(caP); } } - - responseP->entityTypeVector.push_back(entityType); + if (limit != 0) + { + responseP->entityTypeVector.push_back(entityType); + } } + orion::releaseMongoConnection(connection); // Get count if user requested (i.e. if totalTypesP is not NULL) diff --git a/test/functionalTest/cases/1492_limit_zero/limit_zero_types.test b/test/functionalTest/cases/1492_limit_zero/limit_zero_types.test index af45f8d714..d93cef658a 100644 --- a/test/functionalTest/cases/1492_limit_zero/limit_zero_types.test +++ b/test/functionalTest/cases/1492_limit_zero/limit_zero_types.test @@ -85,7 +85,7 @@ echo 01. GET /v2/types with 'limit' == 0 and options=count and see no types and fiware-total-count: 0 ================================================================================================ HTTP/1.1 200 OK -Content-Length: 0 +Content-Length: 2 Content-Type: application/json Fiware-Total-Count: 0 Fiware-Correlator: REGEX([0-9a-f\-]{36}) From ae956ecd29a2cc63d99869ee90b4c523e6f96331 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ferm=C3=ADn=20Gal=C3=A1n=20M=C3=A1rquez?= Date: Wed, 27 Apr 2022 16:59:51 +0200 Subject: [PATCH 13/16] FIX check spectations --- .../pagination_error_in_uri_params.test | 20 +++-- .../limit_zero_simple_cases.test | 76 ++++++++----------- 2 files changed, 46 insertions(+), 50 deletions(-) diff --git a/test/functionalTest/cases/0000_bad_requests/pagination_error_in_uri_params.test b/test/functionalTest/cases/0000_bad_requests/pagination_error_in_uri_params.test index 7a4f3c0083..e02e0732d1 100644 --- a/test/functionalTest/cases/0000_bad_requests/pagination_error_in_uri_params.test +++ b/test/functionalTest/cases/0000_bad_requests/pagination_error_in_uri_params.test @@ -43,10 +43,19 @@ orionCurl --url "$url" --payload "$payload" echo echo - +# After fixing issue #1492 this has become a not-bad request case, but we keep +# in this file for simplicity (NGSIv1 is a deprecated API) echo "+++++ 3. URI param 'limit' == 0" url="/v1/queryContext?limit=000000" -payload='NO PAYLOAD NECESSARY' +payload='{ + "entities": [ + { + "type": "", + "isPattern": "false", + "id": "EVector" + } + ] +}' orionCurl --url "$url" --payload "$payload" echo echo @@ -94,16 +103,15 @@ Date: REGEX(.*) +++++ 3. URI param 'limit' == 0 HTTP/1.1 200 OK -Content-Length: 86 +Content-Length: 70 Content-Type: application/json Fiware-Correlator: REGEX([0-9a-f\-]{36}) Date: REGEX(.*) { "errorCode": { - "code": "400", - "details": "JSON Parse Error", - "reasonPhrase": "Bad Request" + "code": "404", + "reasonPhrase": "No context element found" } } diff --git a/test/functionalTest/cases/1492_limit_zero/limit_zero_simple_cases.test b/test/functionalTest/cases/1492_limit_zero/limit_zero_simple_cases.test index f46e1813e5..f83f9a34f6 100644 --- a/test/functionalTest/cases/1492_limit_zero/limit_zero_simple_cases.test +++ b/test/functionalTest/cases/1492_limit_zero/limit_zero_simple_cases.test @@ -30,96 +30,84 @@ brokerStart CB --SHELL-- # -# 01. GET /v2/entities with limit = 0 and see error -# 02. GET /v2/types with limit = 0 and see error -# 03. GET /v2/subscriptions with limit = 0 and see error -# 04. GET /v2/registrations with limit = 0 and see error +# 01. GET /v2/entities with limit = 0 +# 02. GET /v2/types with limit = 0 +# 03. GET /v2/subscriptions with limit = 0 +# 04. GET /v2/registrations with limit = 0 # # -echo "01. GET /v2/entities with limit = 0 and see error" -echo "=================================================" +echo "01. GET /v2/entities with limit = 0" +echo "===================================" orionCurl --url "/v2/entities?limit=0" echo echo -echo "02. GET /v2/types with limit = 0 and see error" -echo "==============================================" +echo "02. GET /v2/types with limit = 0" +echo "================================" orionCurl --url "/v2/types?limit=0" echo echo -echo "03. GET /v2/subscriptions with limit = 0 and see error" -echo "======================================================" +echo "03. GET /v2/subscriptions with limit = 0" +echo "========================================" orionCurl --url "/v2/subscriptions?limit=0" echo echo -echo "04. GET /v2/registrations with limit = 0 and see error" -echo "======================================================" +echo "04. GET /v2/registrations with limit = 0" +echo "========================================" orionCurl --url "/v2/registrations?limit=0" echo echo --REGEXPECT-- -01. GET /v2/entities with limit = 0 and see error -================================================= -HTTP/1.1 400 Bad Request -Content-Length: 98 +01. GET /v2/entities with limit = 0 +=================================== +HTTP/1.1 200 OK +Content-Length: 2 Content-Type: application/json Fiware-Correlator: REGEX([0-9a-f\-]{36}) Date: REGEX(.*) -{ - "description": "Bad pagination limit: /0/ [a value of ZERO is unacceptable]", - "error": "BadRequest" -} +[] -02. GET /v2/types with limit = 0 and see error -============================================== -HTTP/1.1 400 Bad Request -Content-Length: 98 +02. GET /v2/types with limit = 0 +================================ +HTTP/1.1 200 OK +Content-Length: 2 Content-Type: application/json Fiware-Correlator: REGEX([0-9a-f\-]{36}) Date: REGEX(.*) -{ - "description": "Bad pagination limit: /0/ [a value of ZERO is unacceptable]", - "error": "BadRequest" -} +[] -03. GET /v2/subscriptions with limit = 0 and see error -====================================================== -HTTP/1.1 400 Bad Request -Content-Length: 98 +03. GET /v2/subscriptions with limit = 0 +======================================== +HTTP/1.1 200 OK +Content-Length: 2 Content-Type: application/json Fiware-Correlator: REGEX([0-9a-f\-]{36}) Date: REGEX(.*) -{ - "description": "Bad pagination limit: /0/ [a value of ZERO is unacceptable]", - "error": "BadRequest" -} +[] -04. GET /v2/registrations with limit = 0 and see error -====================================================== -HTTP/1.1 400 Bad Request -Content-Length: 98 +04. GET /v2/registrations with limit = 0 +======================================== +HTTP/1.1 200 OK +Content-Length: 2 Content-Type: application/json Fiware-Correlator: REGEX([0-9a-f\-]{36}) Date: REGEX(.*) -{ - "description": "Bad pagination limit: /0/ [a value of ZERO is unacceptable]", - "error": "BadRequest" -} +[] --TEARDOWN-- From 71379a3ea1a7544fbb2aa413fc1660c948290bff Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ferm=C3=ADn=20Gal=C3=A1n=20M=C3=A1rquez?= Date: Thu, 28 Apr 2022 12:24:35 +0200 Subject: [PATCH 14/16] FIX whitespacing --- src/lib/mongoBackend/MongoGlobal.cpp | 1 + src/lib/mongoBackend/mongoGetSubscriptions.cpp | 1 + src/lib/mongoBackend/mongoQueryContext.cpp | 1 + 3 files changed, 3 insertions(+) diff --git a/src/lib/mongoBackend/MongoGlobal.cpp b/src/lib/mongoBackend/MongoGlobal.cpp index 493693b153..b0c29271f0 100644 --- a/src/lib/mongoBackend/MongoGlobal.cpp +++ b/src/lib/mongoBackend/MongoGlobal.cpp @@ -1636,6 +1636,7 @@ bool entitiesQuery cer->entity.attributeVector.push_back(caP); } } + cer->statusCode.fill(SccOk); cerV->push_back(cer); } diff --git a/src/lib/mongoBackend/mongoGetSubscriptions.cpp b/src/lib/mongoBackend/mongoGetSubscriptions.cpp index 33a38eba24..00583774fd 100644 --- a/src/lib/mongoBackend/mongoGetSubscriptions.cpp +++ b/src/lib/mongoBackend/mongoGetSubscriptions.cpp @@ -358,6 +358,7 @@ void mongoListSubscriptions subs->push_back(s); } } + orion::releaseMongoConnection(connection); reqSemGive(__FUNCTION__, "Mongo List Subscriptions", reqSemTaken); diff --git a/src/lib/mongoBackend/mongoQueryContext.cpp b/src/lib/mongoBackend/mongoQueryContext.cpp index 3e11a71324..ad89ce71d4 100644 --- a/src/lib/mongoBackend/mongoQueryContext.cpp +++ b/src/lib/mongoBackend/mongoQueryContext.cpp @@ -371,6 +371,7 @@ HttpStatusCode mongoQueryContext countP, sortOrderList, apiVersion); + if (!ok) { responseP->errorCode.fill(SccReceiverInternalError, err); From 6d3d83c36690cc236709097ff1e619d1660e4044 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ferm=C3=ADn=20Gal=C3=A1n=20M=C3=A1rquez?= Date: Thu, 28 Apr 2022 14:23:33 +0200 Subject: [PATCH 15/16] FIX simplifications --- src/lib/mongoBackend/MongoGlobal.cpp | 68 +++++++++---------- .../mongoBackend/mongoGetSubscriptions.cpp | 26 ++++--- src/lib/mongoBackend/mongoQueryTypes.cpp | 3 + src/lib/mongoBackend/mongoRegistrationGet.cpp | 8 +-- .../1492_limit_zero/limit_zero_subs.test | 2 - 5 files changed, 51 insertions(+), 56 deletions(-) diff --git a/src/lib/mongoBackend/MongoGlobal.cpp b/src/lib/mongoBackend/MongoGlobal.cpp index b0c29271f0..b9454d574e 100644 --- a/src/lib/mongoBackend/MongoGlobal.cpp +++ b/src/lib/mongoBackend/MongoGlobal.cpp @@ -1592,54 +1592,52 @@ bool entitiesQuery int errType; std::string nextErr; - if (limit != 0) + /* Note limit != 0 will cause skipping the while loop in case request didn't actually ask for any result */ + while ((limit != 0) && (cursor.next(&r, &errType, &nextErr))) { - while (cursor.next(&r, &errType, &nextErr)) + alarmMgr.dbErrorReset(); + + // Build CER from BSON retrieved from DB + docs++; + LM_T(LmtMongo, ("retrieved document [%d]: '%s'", docs, r.toString().c_str())); + ContextElementResponse* cer = new ContextElementResponse(r, attrL, includeEmpty, apiVersion); + + // Add builtin attributes and metadata (only in NGSIv2) + if (apiVersion == V2) { - alarmMgr.dbErrorReset(); + addBuiltins(cer, ""); + } - // Build CER from BSON retrieved from DB - docs++; - LM_T(LmtMongo, ("retrieved document [%d]: '%s'", docs, r.toString().c_str())); - ContextElementResponse* cer = new ContextElementResponse(r, attrL, includeEmpty, apiVersion); + /* All the attributes existing in the request but not found in the response are added with 'found' set to false */ + for (unsigned int ix = 0; ix < attrL.size(); ++ix) + { + bool found = false; + std::string attrName = attrL[ix]; - // Add builtin attributes and metadata (only in NGSIv2) - if (apiVersion == V2) + /* The special case "*" is not taken into account*/ + if (attrName == ALL_ATTRS) { - addBuiltins(cer, ""); + continue; } - /* All the attributes existing in the request but not found in the response are added with 'found' set to false */ - for (unsigned int ix = 0; ix < attrL.size(); ++ix) + for (unsigned int jx = 0; jx < cer->entity.attributeVector.size(); ++jx) { - bool found = false; - std::string attrName = attrL[ix]; - - /* The special case "*" is not taken into account*/ - if (attrName == ALL_ATTRS) + if (attrName == cer->entity.attributeVector[jx]->name) { - continue; - } - - for (unsigned int jx = 0; jx < cer->entity.attributeVector.size(); ++jx) - { - if (attrName == cer->entity.attributeVector[jx]->name) - { - found = true; - break; - } - } - - if (!found) - { - ContextAttribute* caP = new ContextAttribute(attrName, "", "", false); - cer->entity.attributeVector.push_back(caP); + found = true; + break; } } - cer->statusCode.fill(SccOk); - cerV->push_back(cer); + if (!found) + { + ContextAttribute* caP = new ContextAttribute(attrName, "", "", false); + cer->entity.attributeVector.push_back(caP); + } } + + cer->statusCode.fill(SccOk); + cerV->push_back(cer); } orion::releaseMongoConnection(connection); diff --git a/src/lib/mongoBackend/mongoGetSubscriptions.cpp b/src/lib/mongoBackend/mongoGetSubscriptions.cpp index 00583774fd..3531b0430f 100644 --- a/src/lib/mongoBackend/mongoGetSubscriptions.cpp +++ b/src/lib/mongoBackend/mongoGetSubscriptions.cpp @@ -337,26 +337,24 @@ void mongoListSubscriptions TIME_STAT_MONGO_READ_WAIT_STOP(); /* Process query result */ + /* Note limit != 0 will cause skipping the while loop in case request didn't actually ask for any result */ unsigned int docs = 0; - if (limit != 0) + orion::BSONObj r; + while ((limit != 0) && (cursor.next(&r))) { - orion::BSONObj r; - while (cursor.next(&r)) - { - docs++; - LM_T(LmtMongo, ("retrieved document [%d]: '%s'", docs, r.toString().c_str())); + docs++; + LM_T(LmtMongo, ("retrieved document [%d]: '%s'", docs, r.toString().c_str())); - Subscription s; + Subscription s; - setNewSubscriptionId(&s, r); - setDescription(&s, r); - setSubject(&s, r); - setStatus(&s, r, tenant); - setNotification(&s, r, tenant); + setNewSubscriptionId(&s, r); + setDescription(&s, r); + setSubject(&s, r); + setStatus(&s, r, tenant); + setNotification(&s, r, tenant); - subs->push_back(s); - } + subs->push_back(s); } orion::releaseMongoConnection(connection); diff --git a/src/lib/mongoBackend/mongoQueryTypes.cpp b/src/lib/mongoBackend/mongoQueryTypes.cpp index 9cc7547b03..4ac5d2d430 100644 --- a/src/lib/mongoBackend/mongoQueryTypes.cpp +++ b/src/lib/mongoBackend/mongoQueryTypes.cpp @@ -568,12 +568,14 @@ HttpStatusCode mongoEntityTypes TIME_STAT_MONGO_COMMAND_WAIT_STOP(); // Processing result to build response + // Note limit != 0 will cause skipping the while loop in case request didn't actually ask for any result */ orion::BSONObj resultItem; unsigned int docs = 0; while (cursor.next(&resultItem)) { if ((docs < offset) || (docs > offset + limit - 1)) { + LM_W(("FGM: INSIDE")); docs++; continue; } @@ -627,6 +629,7 @@ HttpStatusCode mongoEntityTypes entityType->contextAttributeVector.push_back(caP); } } + if (limit != 0) { responseP->entityTypeVector.push_back(entityType); diff --git a/src/lib/mongoBackend/mongoRegistrationGet.cpp b/src/lib/mongoBackend/mongoRegistrationGet.cpp index 63ff6bec12..1bfe6a661b 100644 --- a/src/lib/mongoBackend/mongoRegistrationGet.cpp +++ b/src/lib/mongoBackend/mongoRegistrationGet.cpp @@ -374,9 +374,10 @@ void mongoRegistrationsGet TIME_STAT_MONGO_READ_WAIT_STOP(); /* Process query result */ + // Note limit != 0 will cause skipping the while loop in case request didn't actually ask for any result */ int docs = 0; orion::BSONObj r; - while (cursor.next(&r)) + while ((limit != 0) && (cursor.next(&r))) { ngsiv2::Registration reg; @@ -401,10 +402,7 @@ void mongoRegistrationsGet setExpires(®, r); setStatus(®, r); - if (limit != 0) - { - regV->push_back(reg); - } + regV->push_back(reg); } orion::releaseMongoConnection(connection); diff --git a/test/functionalTest/cases/1492_limit_zero/limit_zero_subs.test b/test/functionalTest/cases/1492_limit_zero/limit_zero_subs.test index a64b647c49..50a0df74a9 100644 --- a/test/functionalTest/cases/1492_limit_zero/limit_zero_subs.test +++ b/test/functionalTest/cases/1492_limit_zero/limit_zero_subs.test @@ -71,7 +71,6 @@ payload='{ } }' orionCurl --url /v2/subscriptions --payload "$payload" -subId=$(echo "$_responseHeaders" | grep Location | awk -F/ '{ print $4 }' | tr -d "\r\n") echo echo @@ -103,7 +102,6 @@ payload='{ } }' orionCurl --url /v2/subscriptions --payload "$payload" -subId=$(echo "$_responseHeaders" | grep Location | awk -F/ '{ print $4 }' | tr -d "\r\n") echo echo From 7ec4cb71274ba20294edb8721b78edf73f999578 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ferm=C3=ADn=20Gal=C3=A1n=20M=C3=A1rquez?= Date: Thu, 28 Apr 2022 16:02:10 +0200 Subject: [PATCH 16/16] FIX simplification --- src/lib/mongoBackend/mongoQueryTypes.cpp | 10 +++------- 1 file changed, 3 insertions(+), 7 deletions(-) diff --git a/src/lib/mongoBackend/mongoQueryTypes.cpp b/src/lib/mongoBackend/mongoQueryTypes.cpp index 4ac5d2d430..b4da8658f1 100644 --- a/src/lib/mongoBackend/mongoQueryTypes.cpp +++ b/src/lib/mongoBackend/mongoQueryTypes.cpp @@ -568,14 +568,13 @@ HttpStatusCode mongoEntityTypes TIME_STAT_MONGO_COMMAND_WAIT_STOP(); // Processing result to build response - // Note limit != 0 will cause skipping the while loop in case request didn't actually ask for any result */ orion::BSONObj resultItem; unsigned int docs = 0; while (cursor.next(&resultItem)) { - if ((docs < offset) || (docs > offset + limit - 1)) + // Note limit == 0 has to be checked individually given doc > offset + limit - 1 doesn't work if offset == 0 with unsigned int */ + if ((limit == 0) || (docs < offset) || (docs > offset + limit - 1)) { - LM_W(("FGM: INSIDE")); docs++; continue; } @@ -630,10 +629,7 @@ HttpStatusCode mongoEntityTypes } } - if (limit != 0) - { - responseP->entityTypeVector.push_back(entityType); - } + responseP->entityTypeVector.push_back(entityType); } orion::releaseMongoConnection(connection);