From 2c8e21a116be9ba1b6f693be336790009026b636 Mon Sep 17 00:00:00 2001 From: Ole <12736698+ole4ryb@users.noreply.github.com> Date: Fri, 2 Aug 2024 12:03:54 +0100 Subject: [PATCH] Niad-3005: Code optimization (AllergyIntollerance) (#740) * [NIAD-3130] Add failing unit test for ReferralRequestMapperTest.java * [NIAD-3130] Address test discrepancies * [NIAD-3130] Address test discrepancies * [NIAD-3130] Modified production ReferralRequestMapper to make unit tests pass * [NIAD-3130] Add test to verify behavior of NOPAT at ehrComposition level to ReferralRequestMapperTest * [NIAD-3130] Add test to verify behavior of NOSCRUB confidentiality code within ReferralRequestMapperTest * [NIAD-3130] Update CHANGELOG.md * small code optimization --------- Co-authored-by: martin-nhs <127403254+martin-nhs@users.noreply.github.com> --- .../mapper/AllergyIntoleranceMapper.java | 46 ++++++++++------- .../mapper/AllergyIntoleranceMapperTest.java | 31 ++---------- .../mapper/ReferralRequestMapperTest.java | 28 ++++++++++- ...mple_with_noscrub_confidentiality_code.xml | 50 +++++++++++++++++++ 4 files changed, 108 insertions(+), 47 deletions(-) create mode 100644 gp2gp-translator/src/test/resources/xml/RequestStatement/full_valid_data_example_with_noscrub_confidentiality_code.xml diff --git a/gp2gp-translator/src/main/java/uk/nhs/adaptors/pss/translator/mapper/AllergyIntoleranceMapper.java b/gp2gp-translator/src/main/java/uk/nhs/adaptors/pss/translator/mapper/AllergyIntoleranceMapper.java index edfddf5f7..86766c2a1 100644 --- a/gp2gp-translator/src/main/java/uk/nhs/adaptors/pss/translator/mapper/AllergyIntoleranceMapper.java +++ b/gp2gp-translator/src/main/java/uk/nhs/adaptors/pss/translator/mapper/AllergyIntoleranceMapper.java @@ -85,16 +85,27 @@ private AllergyIntolerance mapAllergyIntolerance(RCMRMT030101UKEhrComposition eh var allergyIntolerance = new AllergyIntolerance(); var observationStatement = extractObservationStatement(compoundStatement); - - var id = observationStatement - .getId() - .getRoot(); + var id = observationStatement.getId().getRoot(); final Meta meta = confidentialityService.createMetaAndAddSecurityIfConfidentialityCodesPresent( META_PROFILE, ehrComposition.getConfidentialityCode(), - observationStatement.getConfidentialityCode() - ); + observationStatement.getConfidentialityCode()); + + initializeAllergyIntolerance(allergyIntolerance, compoundStatement, ehrComposition, patient, practiseCode, id, meta); + buildAdditionalFields(allergyIntolerance, compoundStatement, ehrComposition, encounters, observationStatement); + + return allergyIntolerance; + } + + private void initializeAllergyIntolerance( + AllergyIntolerance allergyIntolerance, + RCMRMT030101UKCompoundStatement compoundStatement, + RCMRMT030101UKEhrComposition ehrComposition, + Patient patient, + String practiseCode, + String id, + Meta meta) { allergyIntolerance .addCategory(getCategory(compoundStatement)) @@ -105,6 +116,14 @@ private AllergyIntolerance mapAllergyIntolerance(RCMRMT030101UKEhrComposition eh .addIdentifier(buildIdentifier(id, practiseCode)) .setMeta(meta) .setId(id); + } + + private void buildAdditionalFields( + AllergyIntolerance allergyIntolerance, + RCMRMT030101UKCompoundStatement compoundStatement, + RCMRMT030101UKEhrComposition ehrComposition, + List encounters, + RCMRMT030101UKObservationStatement observationStatement) { buildOnset(compoundStatement, allergyIntolerance); buildParticipantReferences(ehrComposition, compoundStatement, allergyIntolerance); @@ -114,19 +133,8 @@ private AllergyIntolerance mapAllergyIntolerance(RCMRMT030101UKEhrComposition eh var codeableConceptFromValue = getCodeableConceptFromNonEgtonCodeValue(observationStatement); var compoundCode = compoundStatement.getCode().getCode(); - buildCode(allergyIntolerance, - observationStatement, - compoundCode, - codeableConceptFromCode, - codeableConceptFromValue); - - buildNote(allergyIntolerance, - observationStatement, - compoundCode, - codeableConceptFromCode, - codeableConceptFromValue); - - return allergyIntolerance; + buildCode(allergyIntolerance, observationStatement, compoundCode, codeableConceptFromCode, codeableConceptFromValue); + buildNote(allergyIntolerance, observationStatement, compoundCode, codeableConceptFromCode, codeableConceptFromValue); } private void buildParticipantReferences(RCMRMT030101UKEhrComposition ehrComposition, diff --git a/gp2gp-translator/src/test/java/uk/nhs/adaptors/pss/translator/mapper/AllergyIntoleranceMapperTest.java b/gp2gp-translator/src/test/java/uk/nhs/adaptors/pss/translator/mapper/AllergyIntoleranceMapperTest.java index 2e3053756..dbd98a324 100644 --- a/gp2gp-translator/src/test/java/uk/nhs/adaptors/pss/translator/mapper/AllergyIntoleranceMapperTest.java +++ b/gp2gp-translator/src/test/java/uk/nhs/adaptors/pss/translator/mapper/AllergyIntoleranceMapperTest.java @@ -7,6 +7,7 @@ import static org.hl7.fhir.dstu3.model.AllergyIntolerance.AllergyIntoleranceVerificationStatus.UNCONFIRMED; import static org.junit.jupiter.api.Assertions.assertAll; import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertNull; import static org.mockito.ArgumentMatchers.any; import static org.mockito.Mockito.times; import static org.mockito.Mockito.verify; @@ -326,9 +327,9 @@ void testGivenAllergyWithNoOptionalData() { assertThat(allergyIntolerance.getCode().getCodingFirstRep()).isEqualTo(DegradedCodeableConcepts.DEGRADED_DRUG_ALLERGY); assertThat(allergyIntolerance.getCode().getCoding().get(1).getDisplay()).isEqualTo(CODING_DISPLAY_1); assertThat(allergyIntolerance.getAssertedDateElement().asStringValue()).isEqualTo("2019-07-08T13:35:00+00:00"); - assertThat(allergyIntolerance.getRecorder().getReference()).isNull(); // this is added later in the UnknownPractitionerHandler - assertThat(allergyIntolerance.getAsserter().getReference()).isNull(); - assertThat(allergyIntolerance.getOnset()).isNull(); + assertNull(allergyIntolerance.getRecorder().getReference()); // this is added later in the UnknownPractitionerHandler + assertNull(allergyIntolerance.getAsserter().getReference()); + assertNull(allergyIntolerance.getOnset()); assertThat(allergyIntolerance.getNote()).isEmpty(); verifyConfidentialityServiceCalled(1, Optional.empty(), Optional.empty()); } @@ -389,28 +390,6 @@ void testGivenAllergyIntoleranceWithNopatConfidentialityCodePresentWithinEhrComp verifyConfidentialityServiceCalled(1, ehrComposition.getConfidentialityCode(), Optional.empty()); } - @Test - void testGivenAllergyIntoleranceWithNoscrubConfidentialityCodePresentWithinEhrCompositionExpectMetaSecurityAdded() { - final Meta stubbedMeta = MetaFactory.getMetaFor(META_WITH_SECURITY, META_PROFILE); - final RCMRMT030101UKEhrExtract ehrExtract = - unmarshallEhrExtract("allergy-structure-with-ehr-composition-nopat-confidentiality-code.xml"); - - Mockito - .lenient() - .when(confidentialityService.createMetaAndAddSecurityIfConfidentialityCodesPresent( - any(String.class), any(Optional.class), any(Optional.class) - )).thenReturn(stubbedMeta); - - final RCMRMT030101UKEhrComposition ehrComposition = TestUtility.GET_EHR_COMPOSITION.apply(ehrExtract); - final List allergyIntolerance = allergyIntoleranceMapper - .mapResources(ehrExtract, getPatient(), getEncounterList(), PRACTISE_CODE); - - final Meta meta = allergyIntolerance.get(0).getMeta(); - - assertMetaSecurityPresent(meta); - verifyConfidentialityServiceCalled(1, ehrComposition.getConfidentialityCode(), Optional.empty()); - } - @Test void testGivenAllergyIntoleranceWithNopatConfidentialityCodePresentWithinObservationStatementExpectMetaSecurityAdded() { final Meta stubbedMeta = MetaFactory.getMetaFor(META_WITH_SECURITY, META_PROFILE); @@ -442,7 +421,7 @@ void testGivenAllergyIntoleranceWithNoscrubConfidentialityCodePresentWithinObser final List allergyIntolerance = allergyIntoleranceMapper .mapResources(ehrExtract, getPatient(), getEncounterList(), PRACTISE_CODE); - assertThat(allergyIntolerance.get(0).getMeta().getSecurity()).hasSize(0); + assertThat(allergyIntolerance.get(0).getMeta().getSecurity()).isEmpty(); verifyConfidentialityServiceCalled(1, Optional.empty(), observationStatement.getConfidentialityCode()); } diff --git a/gp2gp-translator/src/test/java/uk/nhs/adaptors/pss/translator/mapper/ReferralRequestMapperTest.java b/gp2gp-translator/src/test/java/uk/nhs/adaptors/pss/translator/mapper/ReferralRequestMapperTest.java index ff4d2f648..1c0fe8a13 100644 --- a/gp2gp-translator/src/test/java/uk/nhs/adaptors/pss/translator/mapper/ReferralRequestMapperTest.java +++ b/gp2gp-translator/src/test/java/uk/nhs/adaptors/pss/translator/mapper/ReferralRequestMapperTest.java @@ -69,6 +69,9 @@ class ReferralRequestMapperTest { private static final String UNEXPECTED_PRIORITY_DISPLAY = "Delayed priority"; private static final String PRIORITY_NOTE_PREPENDAGE = "Priority: "; private static final String NOPAT = "NOPAT"; + + private static final String NOSCRUB = "NOSCRUB"; + private static final String TEST_DIRECTORY_NAME = "RequestStatement"; @Mock @@ -477,7 +480,7 @@ void When_MapToReferralRequest_With_NopatConfidentialityCode_Expect_MetaFromConf final CV confidentialityCode = confidentialityCodeCaptor .getAllValues() - .get(0) // requestStatement.getConfidentialityCode() + .get(0) // First value .orElseThrow(); assertAll( @@ -505,7 +508,7 @@ void When_MapToReferralRequest_With_NopatConfidentialityCodeWithinEhrComposition final CV confidentialityCode = confidentialityCodeCaptor .getAllValues() - .get(1) // ehrComposition.getConfidentialityCode() + .get(1) // Second value (i.e. ehrComposition.getConfidentialityCode()) .orElseThrow(); assertAll( @@ -515,6 +518,27 @@ void When_MapToReferralRequest_With_NopatConfidentialityCodeWithinEhrComposition ); } + @Test + void When_MapToReferralRequest_With_NoscrubConfidentialityCode_Expect_MetaFromConfidentialityServiceWithoutSecurity() { + final Meta metaWithoutSecurity = MetaFactory.getMetaFor(META_WITHOUT_SECURITY, META_PROFILE); + final RCMRMT030101UKEhrExtract ehrExtract = + unmarshallEhrExtractElement("full_valid_data_example_with_noscrub_confidentiality_code.xml"); + final RCMRMT030101UKEhrComposition ehrComposition = GET_EHR_COMPOSITION.apply(ehrExtract); + + final ReferralRequest result = mapReferralRequest(ehrExtract, ehrComposition, + composition -> composition.getComponent().get(0).getRequestStatement()); + + final CV confidentialityCode = confidentialityCodeCaptor + .getAllValues() + .get(0) // First value + .orElseThrow(); + + assertAll( + () -> assertThat(result.getMeta()).usingRecursiveComparison().isEqualTo(metaWithoutSecurity), + () -> assertThat(confidentialityCode.getCode()).isEqualTo(NOSCRUB) + ); + } + private RCMRMT030101UKRequestStatement getNestedRequestStatement(RCMRMT030101UKEhrComposition ehrComposition) { return ehrComposition.getComponent() .get(0) diff --git a/gp2gp-translator/src/test/resources/xml/RequestStatement/full_valid_data_example_with_noscrub_confidentiality_code.xml b/gp2gp-translator/src/test/resources/xml/RequestStatement/full_valid_data_example_with_noscrub_confidentiality_code.xml new file mode 100644 index 000000000..795344a43 --- /dev/null +++ b/gp2gp-translator/src/test/resources/xml/RequestStatement/full_valid_data_example_with_noscrub_confidentiality_code.xml @@ -0,0 +1,50 @@ + + + + + + + + + + + + + + + + + + + + + Test request statement text New line + + +
+ + + + Routine + + + + + + + + + + + + + + + + + + + \ No newline at end of file