Skip to content

Commit 5cb3f5f

Browse files
updated the test of NGSIv2 payloads
1 parent 9cabc8b commit 5cb3f5f

1 file changed

Lines changed: 46 additions & 4 deletions

File tree

test_data_model/tests/test_valid_ngsiv2.py

Lines changed: 46 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,10 +14,14 @@
1414
# limitations under the License. #
1515
# Author: Alberto Abella #
1616
#################################################################################
17-
# version 26/02/25 - 1
17+
# version 26/02/25 - 2
1818

1919
import json
2020

21+
# NGSI-LD attribute types that are invalid in NGSIv2 normalized format
22+
NGSILD_ATTRIBUTE_TYPES = {"Property", "Relationship", "GeoProperty"}
23+
24+
2125
def validate_entity(entity):
2226
"""
2327
Validate that the 'id' is a URI and 'type' is a string.
@@ -52,6 +56,37 @@ def validate_entity(entity):
5256
return success, messages
5357

5458

59+
def check_ngsild_attribute_types(entity):
60+
"""
61+
Check if any attribute in the entity uses NGSI-LD-specific type values
62+
(Property, Relationship, GeoProperty), which are invalid in NGSIv2.
63+
64+
Parameters:
65+
entity (dict): The JSON object to inspect.
66+
67+
Returns:
68+
tuple: (success: bool, messages: list)
69+
"""
70+
success = True
71+
messages = []
72+
73+
required_fields = ["id", "type"]
74+
75+
for attr_name, attr_value in entity.items():
76+
if attr_name in required_fields:
77+
continue
78+
if isinstance(attr_value, dict):
79+
attr_type = attr_value.get("type")
80+
if attr_type in NGSILD_ATTRIBUTE_TYPES:
81+
success = False
82+
messages.append(
83+
f"*** Attribute '{attr_name}' has type '{attr_type}' which is an "
84+
f"NGSI-LD type and is NOT valid in NGSIv2 normalized format"
85+
)
86+
87+
return success, messages
88+
89+
5590
def test_valid_ngsiv2(repo_path, options):
5691
"""
5792
Validate if the example-normalized.json file is a valid NGSI v2 file in normalized format.
@@ -91,17 +126,24 @@ def test_valid_ngsiv2(repo_path, options):
91126
success = False
92127
output.append(f"*** {entity} have incomplete structure")
93128
else:
94-
if "type" not in data [entity]:
129+
if "type" not in data[entity]:
95130
success = False
96131
output.append(f"*** {entity} has not type")
97-
if "value" not in data [entity]:
132+
if "value" not in data[entity]:
98133
success = False
99134
output.append(f"*** {entity} has not value")
135+
136+
# Check for NGSI-LD-specific attribute types that are invalid in NGSIv2
137+
ngsild_success, ngsild_messages = check_ngsild_attribute_types(data)
138+
if not ngsild_success:
139+
success = False
140+
output.extend(ngsild_messages)
141+
100142
except json.JSONDecodeError:
101143
success = False
102144
output.append("*** example-normalized.json is not a valid JSON file")
103145
except FileNotFoundError:
104146
success = False
105147
output.append("*** example-normalized.json file not found")
106148

107-
return test_name, success, output
149+
return test_name, success, output

0 commit comments

Comments
 (0)