Skip to content

Commit 9fd7818

Browse files
eesa456axelkrastek1-nhs
authored andcommitted
NRL-1554 sonar qube fixes
1 parent 4ed5144 commit 9fd7818

File tree

3 files changed

+55
-26
lines changed

3 files changed

+55
-26
lines changed

layer/nrlf/core/constants.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -677,6 +677,7 @@ def coding_value(self):
677677
CONTENT_STABILITY_EXTENSION_URL = (
678678
"https://fhir.nhs.uk/England/StructureDefinition/Extension-England-ContentStability"
679679
)
680+
CONTENT_RETRIEVAL_EXTENSION_URL = "https://fhir.nhs.uk/England/StructureDefinition/Extension-England-RetrievalMechanism"
680681
CONTENT_STABILITY_SYSTEM_URL = (
681682
"https://fhir.nhs.uk/England/CodeSystem/England-NRLContentStability"
682683
)

layer/nrlf/core/validators.py

Lines changed: 36 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -507,14 +507,14 @@ def _validate_content_extension(self, model: DocumentReference):
507507
Validate the content.extension field contains an appropriate coding.
508508
"""
509509
logger.log(LogReference.VALIDATOR001, step="content_extension")
510-
511510
logger.debug("Validating extension")
511+
512512
for i, content in enumerate(model.content):
513-
if len(content.extension) == 0:
513+
if not content.extension:
514514
self.result.add_error(
515515
issue_code="business-rule",
516516
error_code="UNPROCESSABLE_ENTITY",
517-
diagnostics=f"Invalid content extension: Extension must have at least one value",
517+
diagnostics="Invalid content extension: Extension must have at least one value",
518518
field=f"content[{i}].extension",
519519
)
520520
return
@@ -525,44 +525,55 @@ def _validate_content_extension(self, model: DocumentReference):
525525
extension.url
526526
== "https://fhir.nhs.uk/England/StructureDefinition/Extension-England-ContentStability"
527527
):
528-
coding = extension.valueCodeableConcept.coding[0]
529-
if coding.code != coding.display.lower() or coding.display not in [
530-
"Static",
531-
"Dynamic",
532-
]:
533-
self.result.add_error(
534-
issue_code="business-rule",
535-
error_code="UNPROCESSABLE_ENTITY",
536-
diagnostics=f"Invalid content extension display: {coding.display} Extension display must be the same as code either 'Static' or 'Dynamic'",
537-
field=f"content[{i}].extension[{j}].valueCodeableConcept.coding[0].display",
538-
)
528+
if not self._validate_content_stability_extension(extension, i, j):
539529
return
540530
has_content_stability = True
541-
542531
elif (
543532
extension.url
544533
== "https://fhir.nhs.uk/England/StructureDefinition/Extension-England-RetrievalMechanism"
545534
):
546-
coding = extension.valueCodeableConcept.coding[0]
547-
retrievalDisplay = CONTENT_RETRIEVAL_CODE_MAP.get(coding.code)
548-
if coding.display != retrievalDisplay:
549-
self.result.add_error(
550-
issue_code="business-rule",
551-
error_code="UNPROCESSABLE_ENTITY",
552-
diagnostics=f"Invalid content extension display: {coding.display} Expected display is '{retrievalDisplay}'",
553-
field=f"content[{i}].extension[{j}].valueCodeableConcept.coding[0].display",
554-
)
535+
if not self._validate_retrieval_mechanism_extension(
536+
extension, i, j
537+
):
555538
return
556539

557540
if not has_content_stability:
558541
self.result.add_error(
559542
issue_code="business-rule",
560543
error_code="UNPROCESSABLE_ENTITY",
561-
diagnostics=f"Invalid content extension: Extension must have one content stability extension see value set ('https://fhir.nhs.uk/England/CodeSystem/England-NRLContentStability')",
544+
diagnostics="Invalid content extension: Extension must have one content stability extension see value set ('https://fhir.nhs.uk/England/CodeSystem/England-NRLContentStability')",
562545
field=f"content[{i}].extension",
563546
)
564547
return
565548

549+
def _validate_content_stability_extension(self, extension, i, j):
550+
coding = extension.valueCodeableConcept.coding[0]
551+
if coding.code != coding.display.lower() or coding.display not in [
552+
"Static",
553+
"Dynamic",
554+
]:
555+
self.result.add_error(
556+
issue_code="business-rule",
557+
error_code="UNPROCESSABLE_ENTITY",
558+
diagnostics=f"Invalid content extension display: {coding.display} Extension display must be the same as code either 'Static' or 'Dynamic'",
559+
field=f"content[{i}].extension[{j}].valueCodeableConcept.coding[0].display",
560+
)
561+
return False
562+
return True
563+
564+
def _validate_retrieval_mechanism_extension(self, extension, i, j):
565+
coding = extension.valueCodeableConcept.coding[0]
566+
expected_retrieval_display = CONTENT_RETRIEVAL_CODE_MAP.get(coding.code)
567+
if coding.display != expected_retrieval_display:
568+
self.result.add_error(
569+
issue_code="business-rule",
570+
error_code="UNPROCESSABLE_ENTITY",
571+
diagnostics=f"Invalid content extension display: {coding.display} Expected display is '{expected_retrieval_display}'",
572+
field=f"content[{i}].extension[{j}].valueCodeableConcept.coding[0].display",
573+
)
574+
return False
575+
return True
576+
566577
def _validate_author(self, model: DocumentReference):
567578
"""
568579
Validate the author field contains an appropriate coding system and code.

tests/features/utils/data.py

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
from layer.nrlf.core.constants import (
22
CATEGORY_ATTRIBUTES,
33
CONTENT_FORMAT_CODE_URL,
4+
CONTENT_RETRIEVAL_EXTENSION_URL,
5+
CONTENT_RETRIEVAL_SYSTEM_URL,
46
CONTENT_STABILITY_EXTENSION_URL,
57
CONTENT_STABILITY_SYSTEM_URL,
68
SNOMED_PRACTICE_SETTINGS,
@@ -22,6 +24,9 @@
2224
NRLCoding,
2325
NRLFormatCode,
2426
Reference,
27+
RetrievalMechanismExtension,
28+
RetrievalMechanismExtensionCoding,
29+
RetrievalMechanismExtensionValueCodeableConcept,
2530
)
2631
from tests.features.utils.constants import (
2732
DEFAULT_TEST_AUTHOR,
@@ -75,7 +80,19 @@ def create_test_document_reference(items: dict) -> DocumentReference:
7580
)
7681
]
7782
),
78-
)
83+
),
84+
RetrievalMechanismExtension(
85+
url=CONTENT_RETRIEVAL_EXTENSION_URL,
86+
valueCodeableConcept=RetrievalMechanismExtensionValueCodeableConcept(
87+
coding=[
88+
RetrievalMechanismExtensionCoding(
89+
system=CONTENT_RETRIEVAL_SYSTEM_URL,
90+
code="Direct",
91+
display="Direct",
92+
)
93+
]
94+
),
95+
),
7996
],
8097
)
8198
],

0 commit comments

Comments
 (0)