From 036b5a3e9ad73bc30e30bd18ac201582604900ac Mon Sep 17 00:00:00 2001 From: Benoit Orihuela Date: Wed, 18 Dec 2024 19:29:08 +0100 Subject: [PATCH] feat: add tests for replace entity wrt types and scopes --- .../UpsertBatchOfEntities/004_03.robot | 6 +++ .../Entities/ReplaceEntity/054_03.robot | 52 +++++++++++++++++++ data/entities/building-with-one-scope.jsonld | 8 +++ doc/analysis/requests.py | 31 +++++++++++ .../ContextInformation/Provision/004_03.json | 46 ++++++++++++++-- .../ContextInformation/Provision/054_03.json | 43 +++++++++++++++ .../ContextInformationProvision.resource | 24 +++++++++ 7 files changed, 205 insertions(+), 5 deletions(-) create mode 100644 TP/NGSI-LD/ContextInformation/Provision/Entities/ReplaceEntity/054_03.robot create mode 100644 data/entities/building-with-one-scope.jsonld create mode 100644 doc/files/ContextInformation/Provision/054_03.json diff --git a/TP/NGSI-LD/ContextInformation/Provision/BatchEntities/UpsertBatchOfEntities/004_03.robot b/TP/NGSI-LD/ContextInformation/Provision/BatchEntities/UpsertBatchOfEntities/004_03.robot index 014cf660..ac887633 100644 --- a/TP/NGSI-LD/ContextInformation/Provision/BatchEntities/UpsertBatchOfEntities/004_03.robot +++ b/TP/NGSI-LD/ContextInformation/Provision/BatchEntities/UpsertBatchOfEntities/004_03.robot @@ -25,6 +25,12 @@ ${building_id_prefix}= urn:ngsi-ld:Building: 004_03_03 EntityWithRelationshipsProperties [Tags] be-upsert 5_6_8 building-relationship-of-property.jsonld +004_03_04 EntityWithScope + [Tags] be-upsert 4_18 5_6_8 + building-with-one-scope.jsonld +004_03_05 EntityWithTypes + [Tags] be-upsert 4_16 5_6_8 + building-with-two-types.jsonld *** Keywords *** diff --git a/TP/NGSI-LD/ContextInformation/Provision/Entities/ReplaceEntity/054_03.robot b/TP/NGSI-LD/ContextInformation/Provision/Entities/ReplaceEntity/054_03.robot new file mode 100644 index 00000000..19e2b34f --- /dev/null +++ b/TP/NGSI-LD/ContextInformation/Provision/Entities/ReplaceEntity/054_03.robot @@ -0,0 +1,52 @@ +*** Settings *** +Documentation Check that one can replace an existing entity and that scopes are replaced + +Resource ${EXECDIR}/resources/ApiUtils/ContextInformationProvision.resource +Resource ${EXECDIR}/resources/ApiUtils/ContextInformationConsumption.resource +Resource ${EXECDIR}/resources/AssertionUtils.resource + +Test Setup Setup Initial Entity +Test Teardown Delete Initial Entity + + +*** Variables *** +${building_id_prefix} urn:ngsi-ld:Building: +${entity_filename} building-minimal-with-one-scope.json +${entity_replacement_filename} building-minimal-with-many-scopes.json +${expectation_filename} building-minimal-with-two-scopes.json + + +*** Test Cases *** +054_03_01 Replace an existing entity having scopes + [Documentation] Check that one can replace an existing entity and that scopes are replaced + [Tags] e-replace 4_18 5_6_18 6_5_3_3 since_v1.6.1 + ${response}= Replace Entity + ... entity_id=${entity_id} + ... filename=${entity_replacement_filename} + ... content_type=${CONTENT_TYPE_JSON} + ... context=${ngsild_test_suite_context} + Check Response Status Code 204 ${response.status_code} + + ${response1}= Retrieve Entity by Id + ... id=${entity_id} + ... accept=${CONTENT_TYPE_JSON} + ... context=${ngsild_test_suite_context} + Check Response Body Containing Entity element + ... expectation_filename=${expectation_filename} + ... entity_id=${entity_id} + ... response_body=${response1.json()} + + +*** Keywords *** +Setup Initial Entity + ${entity_id}= Generate Random Entity Id ${building_id_prefix} + Set Test Variable ${entity_id} + ${response}= Create Entity Selecting Content Type + ... ${entity_filename} + ... ${entity_id} + ... ${CONTENT_TYPE_JSON} + ... ${ngsild_test_suite_context} + Check Response Status Code 201 ${response.status_code} + +Delete Initial Entity + Delete Entity by Id ${entity_id} diff --git a/data/entities/building-with-one-scope.jsonld b/data/entities/building-with-one-scope.jsonld new file mode 100644 index 00000000..b91cbdf3 --- /dev/null +++ b/data/entities/building-with-one-scope.jsonld @@ -0,0 +1,8 @@ +{ + "id": "urn:ngsi-ld:Building:randomUUID", + "type": "Building", + "scope": "/Madrid/Gardens/ParqueNorte", + "@context": [ + "https://forge.etsi.org/rep/cim/ngsi-ld-test-suite/-/raw/develop/resources/jsonld-contexts/ngsi-ld-test-suite-compound.jsonld" + ] +} \ No newline at end of file diff --git a/doc/analysis/requests.py b/doc/analysis/requests.py index 1252c8ed..513d4ae7 100644 --- a/doc/analysis/requests.py +++ b/doc/analysis/requests.py @@ -102,6 +102,10 @@ class Requests: 'positions': [0], 'params': ['id'] }, + 'Replace Entity': { + 'positions': [0, 1, 2, 3], + 'params': ['entity_id', 'filename', 'content_type', 'context'] + }, 'Replace Entity Selecting Content Type': { 'positions': [0, 1, 2, 3], 'params': ['entity_id', 'entity_fragment', 'content_type', 'context'] @@ -318,6 +322,8 @@ class Requests: Requests.query_entities, 'Delete Entity by Id': Requests.delete_entity_by_id, + 'Replace Entity': + Requests.replace_entity, 'Replace Entity Selecting Content Type': Requests.replace_entity_selecting_content_type, 'Replace Attribute Selecting Content Type': @@ -1364,6 +1370,31 @@ class Requests: if 'id' in kwargs: return f"Delete Entity Request with id set to '{kwargs['id']}'" + @staticmethod + def replace_entity(kwargs) -> str: + expected_parameters = ['entity_id', 'filename', 'content_type', 'context'] + + if 'context' not in kwargs: + kwargs['context'] = '${EMPTY}' + + result = [x for x in kwargs if x not in expected_parameters] + response = "Replace Entity" + for key, value in kwargs.items(): + match key: + case 'entity_id': + response = f"{response} and\n Query Parameter: entity_id set to '{value}'" + case 'filename': + response = f"{response} and\n Query Parameter: filename set to '{value}'" + case 'content_type': + response = f"{response} and\n Query Parameter: content_type set to '{value}'" + case 'context': + response = f"{response} and\n Query Parameter: context set to '{value}'" + case _: + raise Exception(f"ERROR: unexpected attribute {result}, the attributes expected are " + f"{expected_parameters}, but received: {kwargs}") + + return response + @staticmethod def replace_entity_selecting_content_type(kwargs) -> str: expected_parameters = ['entity_id', 'entity_fragment', 'content_type', 'context'] diff --git a/doc/files/ContextInformation/Provision/004_03.json b/doc/files/ContextInformation/Provision/004_03.json index 6e024155..d6ca2d88 100644 --- a/doc/files/ContextInformation/Provision/004_03.json +++ b/doc/files/ContextInformation/Provision/004_03.json @@ -1,10 +1,12 @@ { "tp_id": "TP/NGSI-LD/CI/Prov/BE/004_03", - "test_objective": "Check that you can upsert a batch of existing entities and they will be replaced", - "reference": "ETSI GS CIM 009 V1.3.1 [], clause 5.6.8", + "test_objective": "Check that one can upsert a batch of existing entities and they will be replaced", + "reference": "ETSI GS CIM 009 V1.3.1 [], clauses 4.16, 4.18, 5.6.8", "config_id": "", "parent_release": "v1.3.1", "clauses": [ + "4.16", + "4.18", "5.6.8" ], "pics_selection": "", @@ -19,7 +21,7 @@ { "name": "004_03_01 EntityWithSimpleProperties", "permutation_tp_id": "TP/NGSI-LD/CI/Prov/BE/004_03_01", - "doc": "Check that you can upsert a batch of existing entities", + "doc": "Check that one can upsert a batch of existing entities", "tags": [ "5_6_8", "be-upsert" @@ -35,7 +37,7 @@ { "name": "004_03_02 EntityWithSimpleRelationships", "permutation_tp_id": "TP/NGSI-LD/CI/Prov/BE/004_03_02", - "doc": "Check that you can upsert a batch of existing entities", + "doc": "Check that one can upsert a batch of existing entities", "tags": [ "5_6_8", "be-upsert" @@ -51,7 +53,7 @@ { "name": "004_03_03 EntityWithRelationshipsProperties", "permutation_tp_id": "TP/NGSI-LD/CI/Prov/BE/004_03_03", - "doc": "Check that you can upsert a batch of existing entities", + "doc": "Check that one can upsert a batch of existing entities", "tags": [ "5_6_8", "be-upsert" @@ -63,6 +65,40 @@ "when": "when {\n the SUT receives a Request from the client containing:\n URL set to '/ngsi-ld/v1/entityOperations/upsert?options=${update_option}'\n method set to 'POST'\n Request batch upsert operation over entity from filename '@{entities_to_be_upserted}' with update_option set to 'replace' and Content-Type set to 'application/ld+json'\n}", "http_verb": "POST", "endpoint": "entityOperations/upsert?options=${update_option}" + }, + { + "name": "004_03_04 EntityWithScope", + "permutation_tp_id": "TP/NGSI-LD/CI/Prov/BE/004_03_04", + "doc": "Check that one can upsert a batch of existing entities", + "tags": [ + "4_18", + "5_6_8", + "be-upsert" + ], + "setup": "Setup Initial Entities", + "teardown": "Delete Initial Entities", + "template": "Batch Upsert Existing Entities Scenarios", + "then": "then {\n the SUT sends a valid Response for the operations:\n Batch Upsert Entities with Response Status Code set to 204 and\n Query Entities with Updated Entities set to '${entities_to_be_upserted}' valid entities\n}", + "when": "when {\n the SUT receives a Request from the client containing:\n URL set to '/ngsi-ld/v1/entityOperations/upsert?options=${update_option}'\n method set to 'POST'\n Request batch upsert operation over entity from filename '@{entities_to_be_upserted}' with update_option set to 'replace' and Content-Type set to 'application/ld+json'\n}", + "http_verb": "POST", + "endpoint": "entityOperations/upsert?options=${update_option}" + }, + { + "name": "004_03_05 EntityWithTypes", + "permutation_tp_id": "TP/NGSI-LD/CI/Prov/BE/004_03_05", + "doc": "Check that one can upsert a batch of existing entities", + "tags": [ + "4_16", + "5_6_8", + "be-upsert" + ], + "setup": "Setup Initial Entities", + "teardown": "Delete Initial Entities", + "template": "Batch Upsert Existing Entities Scenarios", + "then": "then {\n the SUT sends a valid Response for the operations:\n Batch Upsert Entities with Response Status Code set to 204 and\n Query Entities with Updated Entities set to '${entities_to_be_upserted}' valid entities\n}", + "when": "when {\n the SUT receives a Request from the client containing:\n URL set to '/ngsi-ld/v1/entityOperations/upsert?options=${update_option}'\n method set to 'POST'\n Request batch upsert operation over entity from filename '@{entities_to_be_upserted}' with update_option set to 'replace' and Content-Type set to 'application/ld+json'\n}", + "http_verb": "POST", + "endpoint": "entityOperations/upsert?options=${update_option}" } ], "permutations": [], diff --git a/doc/files/ContextInformation/Provision/054_03.json b/doc/files/ContextInformation/Provision/054_03.json new file mode 100644 index 00000000..777c0a51 --- /dev/null +++ b/doc/files/ContextInformation/Provision/054_03.json @@ -0,0 +1,43 @@ +{ + "tp_id": "TP/NGSI-LD/CI/Prov/E/054_03", + "test_objective": "Check that one can replace an existing entity and that scopes are replaced", + "reference": "ETSI GS CIM 009 V1.6.1 [], clauses 4.18, 5.6.18, 6.5.3.3", + "config_id": "", + "parent_release": "v1.6.1", + "clauses": [ + "4.18", + "5.6.18", + "6.5.3.3" + ], + "pics_selection": "", + "keywords": [ + "Setup Initial Entity", + "Delete Initial Entity" + ], + "teardown": "None", + "initial_condition": "with {\n the SUT being in the \"initial state\" and\n the SUT containing an initial Entity ${entity} \n with an id set to ${entityId} \n}", + "test_cases": [ + { + "name": "054_03_01 Replace an existing entity having scopes", + "permutation_tp_id": "TP/NGSI-LD/CI/Prov/E/054_03_01", + "doc": "Check that one can replace an existing entity and that scopes are replaced", + "tags": [ + "4_18", + "5_6_18", + "6_5_3_3", + "e-replace", + "since_v1.6.1" + ], + "setup": "Setup Initial Entity", + "teardown": "Delete Initial Entity", + "template": null, + "then": "then {\n the SUT sends a valid Response for the operations:\n Replace Entity with Response Status Code set to 204 and\n Retrieve Entity by Id with Response Body containing en entity element with id set to 'entity_id=${entity_id}' and body content set to 'expectation_filename=${expectation_filename}'\n}", + "when": "when {\n the SUT receives a Request from the client containing:\n URL set to '/ngsi-ld/v1/entities/{entity_id}'\n method set to 'PUT'\n Replace Entity and\n Query Parameter: entity_id set to '${entity_id}' and\n Query Parameter: filename set to '${entity_replacement_filename}' and\n Query Parameter: content_type set to 'application/json' and\n Query Parameter: context set to 'https://forge.etsi.org/rep/cim/ngsi-ld-test-suite/-/raw/develop/resources/jsonld-contexts/ngsi-ld-test-suite-compound.jsonld'\n}", + "http_verb": "PUT", + "endpoint": "entities/{entity_id}" + } + ], + "permutations": [], + "robotpath": "ContextInformation/Provision/Entities/ReplaceEntity", + "robotfile": "054_03" +} \ No newline at end of file diff --git a/resources/ApiUtils/ContextInformationProvision.resource b/resources/ApiUtils/ContextInformationProvision.resource index e68f678e..efd7796a 100755 --- a/resources/ApiUtils/ContextInformationProvision.resource +++ b/resources/ApiUtils/ContextInformationProvision.resource @@ -5,6 +5,7 @@ Library RequestsLibrary Library OperatingSystem Library Collections Library JSONLibrary +Resource ${EXECDIR}/resources/JsonUtils.resource *** Variables *** @@ -279,6 +280,29 @@ Update Entity Attributes Output ${response} Update Entity Attributes RETURN ${response} +Replace Entity + [Arguments] + ... ${entity_id} + ... ${filename} + ... ${content_type} + ... ${context}=${EMPTY} + ${entity}= Load Entity + ... entity_file_name=${filename} + ... entity_id=${entity_id} + &{headers}= Create Dictionary Content-Type=${content_type} + IF '${context}'!='' + Set To Dictionary + ... ${headers} + ... Link=<${context}>; rel="http://www.w3.org/ns/json-ld#context";type="application/ld+json" + END + ${response}= PUT + ... url=${url}/${ENTITIES_ENDPOINT_PATH}${entity_id} + ... headers=${headers} + ... json=${entity} + ... expected_status=any + Output ${response} Replace Entity + RETURN ${response} + Replace Entity Selecting Content Type [Arguments] ... ${entity_id} -- GitLab