diff --git a/gp2gp-translator/src/test/java/uk/nhs/adaptors/pss/translator/mapper/medication/MedicationStatementMapperTest.java b/gp2gp-translator/src/test/java/uk/nhs/adaptors/pss/translator/mapper/medication/MedicationStatementMapperTest.java index 21e8a74f6..df246a253 100644 --- a/gp2gp-translator/src/test/java/uk/nhs/adaptors/pss/translator/mapper/medication/MedicationStatementMapperTest.java +++ b/gp2gp-translator/src/test/java/uk/nhs/adaptors/pss/translator/mapper/medication/MedicationStatementMapperTest.java @@ -21,6 +21,7 @@ import java.util.List; import java.util.Objects; import java.util.Optional; +import java.util.stream.Stream; import jakarta.xml.bind.JAXBException; import org.hl7.fhir.dstu3.model.CodeableConcept; @@ -42,6 +43,9 @@ import org.hl7.v3.RCMRMT030101UKMedicationStatement; import org.junit.jupiter.api.Test; import org.junit.jupiter.api.extension.ExtendWith; +import org.junit.jupiter.params.ParameterizedTest; +import org.junit.jupiter.params.provider.Arguments; +import org.junit.jupiter.params.provider.MethodSource; import org.mockito.ArgumentCaptor; import org.mockito.Captor; import org.mockito.InjectMocks; @@ -126,105 +130,27 @@ void When_MappingPrescribedWithUnknownEhrSupplyType_Expect_MedStatementMappedToD assertThat(exception.getMessage()).contains("Unsupported prescribing agency: unknown EhrSupplyType"); } - @Test - void When_MappingPrescribedByAnotherOrganization_Expect_AllFieldsToBeMappedCorrectly() throws JAXBException { - final File file = FileFactory.getXmlFileFor("MedicationStatement", "ehrExtract3.xml"); - final RCMRMT030101UKEhrExtract ehrExtract = unmarshallFile(file, RCMRMT030101UKEhrExtract.class); - final RCMRMT030101UKEhrComposition ehrComposition = GET_EHR_COMPOSITION.apply(ehrExtract); - final RCMRMT030101UKMedicationStatement medicationStatement = - unmarshallMedicationStatement("medicationStatementAuthoriseAllOptionals_MedicationStatement.xml"); - final Optional authorise = medicationStatement.getComponent() - .stream() - .filter(RCMRMT030101UKComponent2::hasEhrSupplyAuthorise) - .map(RCMRMT030101UKComponent2::getEhrSupplyAuthorise) - .findFirst(); - authorise.get().getCode().setDisplayName(PRESCRIPTION_BY_ANOTHER_ORGANISATION); - - final MedicationStatement result = medicationStatementMapper.mapToMedicationStatement( - ehrExtract, - ehrComposition, - medicationStatement, - authorise.get(), - PRACTISE_CODE, - new DateTimeType()); - - assertAll( - () -> assertEquals(PRESCRIBED_BY_ANOTHER_ORGANISATION_CODE, - ((CodeableConcept) result.getExtension().get(0).getValue()).getCoding().get(0).getCode()), - () -> assertEquals(PRESCRIBED_BY_ANOTHER_ORGANISATION_DISPLAY, - ((CodeableConcept) result.getExtension().get(0).getValue()).getCoding().get(0).getDisplay())); - } + @ParameterizedTest(name = "EhrSupplyType \"{0}\" maps to code \"{1}\" and display \"{2}\"") + @MethodSource("ehrSupplyTypeMappings") + void When_MappingPrescribedResources_Expect_CorrectPrescribingAgencyMapped( + String ehrSupplyDisplay, + String expectedCode, + String expectedDisplay) throws JAXBException { - @Test - void When_MappingPrescribedByPersonalAdministration_Expect_AllFieldsToBeMappedCorrectly() throws JAXBException { final File file = FileFactory.getXmlFileFor("MedicationStatement", "ehrExtract3.xml"); final RCMRMT030101UKEhrExtract ehrExtract = unmarshallFile(file, RCMRMT030101UKEhrExtract.class); final RCMRMT030101UKEhrComposition ehrComposition = GET_EHR_COMPOSITION.apply(ehrExtract); final RCMRMT030101UKMedicationStatement medicationStatement = unmarshallMedicationStatement("medicationStatementAuthoriseAllOptionals_MedicationStatement.xml"); - final Optional authorise = medicationStatement.getComponent() - .stream() - .filter(RCMRMT030101UKComponent2::hasEhrSupplyAuthorise) - .map(RCMRMT030101UKComponent2::getEhrSupplyAuthorise) - .findFirst(); - authorise.get().getCode().setDisplayName(PERSONAL_ADMINISTRATION); - final MedicationStatement result = medicationStatementMapper.mapToMedicationStatement( - ehrExtract, - ehrComposition, - medicationStatement, - authorise.get(), - PRACTISE_CODE, - new DateTimeType()); - - assertAll( - () -> assertEquals(PRESCRIBED_BY_ANOTHER_ORGANISATION_CODE, - ((CodeableConcept) result.getExtension().get(0).getValue()).getCoding().get(0).getCode()), - () -> assertEquals(PRESCRIBED_BY_ANOTHER_ORGANISATION_DISPLAY, - ((CodeableConcept) result.getExtension().get(0).getValue()).getCoding().get(0).getDisplay())); - } - - @Test - void When_MappingPrescribedByNhsResource_Expect_AllFieldsToBeMappedCorrectly() throws JAXBException { - final File file = FileFactory.getXmlFileFor("MedicationStatement", "ehrExtract3.xml"); - final RCMRMT030101UKEhrExtract ehrExtract = unmarshallFile(file, RCMRMT030101UKEhrExtract.class); - final RCMRMT030101UKEhrComposition ehrComposition = GET_EHR_COMPOSITION.apply(ehrExtract); - final RCMRMT030101UKMedicationStatement medicationStatement = - unmarshallMedicationStatement("medicationStatementAuthoriseAllOptionals_MedicationStatement.xml"); final Optional authorise = medicationStatement.getComponent() .stream() .filter(RCMRMT030101UKComponent2::hasEhrSupplyAuthorise) .map(RCMRMT030101UKComponent2::getEhrSupplyAuthorise) .findFirst(); - authorise.get().getCode().setDisplayName(PRESCRIPTION); - - final MedicationStatement result = medicationStatementMapper.mapToMedicationStatement( - ehrExtract, - ehrComposition, - medicationStatement, - authorise.get(), - PRACTISE_CODE, - new DateTimeType()); - assertAll( - () -> assertEquals(PRESCRIBED_CODE, ((CodeableConcept) result.getExtension().get(0).getValue()).getCoding().get(0).getCode()), - () -> assertEquals(PRESCRIBED_DISPLAY, - ((CodeableConcept) result.getExtension().get(0).getValue()).getCoding().get(0).getDisplay())); - } - - @Test - void When_MappingPrescribedByLowercaseNhsResource_Expect_AllFieldsToBeMappedCorrectly() throws JAXBException { - final File file = FileFactory.getXmlFileFor("MedicationStatement", "ehrExtract3.xml"); - final RCMRMT030101UKEhrExtract ehrExtract = unmarshallFile(file, RCMRMT030101UKEhrExtract.class); - final RCMRMT030101UKEhrComposition ehrComposition = GET_EHR_COMPOSITION.apply(ehrExtract); - final RCMRMT030101UKMedicationStatement medicationStatement = - unmarshallMedicationStatement("medicationStatementAuthoriseAllOptionals_MedicationStatement.xml"); - final Optional authorise = medicationStatement.getComponent() - .stream() - .filter(RCMRMT030101UKComponent2::hasEhrSupplyAuthorise) - .map(RCMRMT030101UKComponent2::getEhrSupplyAuthorise) - .findFirst(); - authorise.get().getCode().setDisplayName(PRESCRIPTION.toLowerCase()); + assertThat(authorise).isPresent(); + authorise.get().getCode().setDisplayName(ehrSupplyDisplay); final MedicationStatement result = medicationStatementMapper.mapToMedicationStatement( ehrExtract, @@ -234,14 +160,16 @@ void When_MappingPrescribedByLowercaseNhsResource_Expect_AllFieldsToBeMappedCorr PRACTISE_CODE, new DateTimeType()); + CodeableConcept prescribingAgency = (CodeableConcept) result.getExtension().get(0).getValue(); + assertAll( - () -> assertEquals(PRESCRIBED_CODE, ((CodeableConcept) result.getExtension().get(0).getValue()).getCoding().get(0).getCode()), - () -> assertEquals(PRESCRIBED_DISPLAY, - ((CodeableConcept) result.getExtension().get(0).getValue()).getCoding().get(0).getDisplay())); + () -> assertEquals(expectedCode, prescribingAgency.getCoding().get(0).getCode()), + () -> assertEquals(expectedDisplay, prescribingAgency.getCoding().get(0).getDisplay()) + ); } @Test - void When_MappingPrescribeResourceWithAnotherOrg_Expect_AllFieldsToBeMappedCorrectly() throws JAXBException { + void When_MappingPrescribedByAnotherOrganization_Expect_AllFieldsToBeMappedCorrectly() throws JAXBException { final File file = FileFactory.getXmlFileFor("MedicationStatement", "ehrExtract3.xml"); final RCMRMT030101UKEhrExtract ehrExtract = unmarshallFile(file, RCMRMT030101UKEhrExtract.class); final RCMRMT030101UKEhrComposition ehrComposition = GET_EHR_COMPOSITION.apply(ehrExtract); @@ -252,7 +180,7 @@ void When_MappingPrescribeResourceWithAnotherOrg_Expect_AllFieldsToBeMappedCorre .filter(RCMRMT030101UKComponent2::hasEhrSupplyAuthorise) .map(RCMRMT030101UKComponent2::getEhrSupplyAuthorise) .findFirst(); - authorise.get().getCode().setDisplayName(PRESCRIBED_BY_ANOTHER_ORGANISATION_DISPLAY); + authorise.get().getCode().setDisplayName(PRESCRIPTION_BY_ANOTHER_ORGANISATION); final MedicationStatement result = medicationStatementMapper.mapToMedicationStatement( ehrExtract, @@ -270,7 +198,7 @@ void When_MappingPrescribeResourceWithAnotherOrg_Expect_AllFieldsToBeMappedCorre } @Test - void When_MappingPrescribeWithPreviousPracticeResource_Expect_AllFieldsToBeMappedCorrectly() throws JAXBException { + void When_MappingPrescribedByPersonalAdministration_Expect_AllFieldsToBeMappedCorrectly() throws JAXBException { final File file = FileFactory.getXmlFileFor("MedicationStatement", "ehrExtract3.xml"); final RCMRMT030101UKEhrExtract ehrExtract = unmarshallFile(file, RCMRMT030101UKEhrExtract.class); final RCMRMT030101UKEhrComposition ehrComposition = GET_EHR_COMPOSITION.apply(ehrExtract); @@ -281,7 +209,7 @@ void When_MappingPrescribeWithPreviousPracticeResource_Expect_AllFieldsToBeMappe .filter(RCMRMT030101UKComponent2::hasEhrSupplyAuthorise) .map(RCMRMT030101UKComponent2::getEhrSupplyAuthorise) .findFirst(); - authorise.get().getCode().setDisplayName(PRESCRIBED_BY_PREVIOUS_PRACTICE_DISPLAY); + authorise.get().getCode().setDisplayName(PERSONAL_ADMINISTRATION); final MedicationStatement result = medicationStatementMapper.mapToMedicationStatement( ehrExtract, @@ -292,14 +220,14 @@ void When_MappingPrescribeWithPreviousPracticeResource_Expect_AllFieldsToBeMappe new DateTimeType()); assertAll( - () -> assertEquals(PRESCRIBED_BY_PREVIOUS_PRACTICE_CODE, + () -> assertEquals(PRESCRIBED_BY_ANOTHER_ORGANISATION_CODE, ((CodeableConcept) result.getExtension().get(0).getValue()).getCoding().get(0).getCode()), - () -> assertEquals(PRESCRIBED_BY_PREVIOUS_PRACTICE_DISPLAY, + () -> assertEquals(PRESCRIBED_BY_ANOTHER_ORGANISATION_DISPLAY, ((CodeableConcept) result.getExtension().get(0).getValue()).getCoding().get(0).getDisplay())); } @Test - void When_MappingPrescribeResourceWithOtcSale_Expect_AllFieldsToBeMappedCorrectly() throws JAXBException { + void When_MappingPrescribedByLowercaseNhsResource_Expect_AllFieldsToBeMappedCorrectly() throws JAXBException { final File file = FileFactory.getXmlFileFor("MedicationStatement", "ehrExtract3.xml"); final RCMRMT030101UKEhrExtract ehrExtract = unmarshallFile(file, RCMRMT030101UKEhrExtract.class); final RCMRMT030101UKEhrComposition ehrComposition = GET_EHR_COMPOSITION.apply(ehrExtract); @@ -310,7 +238,7 @@ void When_MappingPrescribeResourceWithOtcSale_Expect_AllFieldsToBeMappedCorrectl .filter(RCMRMT030101UKComponent2::hasEhrSupplyAuthorise) .map(RCMRMT030101UKComponent2::getEhrSupplyAuthorise) .findFirst(); - authorise.get().getCode().setDisplayName(OTC_SALE); + authorise.get().getCode().setDisplayName(PRESCRIPTION.toLowerCase()); final MedicationStatement result = medicationStatementMapper.mapToMedicationStatement( ehrExtract, @@ -321,9 +249,8 @@ void When_MappingPrescribeResourceWithOtcSale_Expect_AllFieldsToBeMappedCorrectl new DateTimeType()); assertAll( - () -> assertEquals(PRESCRIBED_BY_ANOTHER_ORGANISATION_CODE, - ((CodeableConcept) result.getExtension().get(0).getValue()).getCoding().get(0).getCode()), - () -> assertEquals(PRESCRIBED_BY_ANOTHER_ORGANISATION_DISPLAY, + () -> assertEquals(PRESCRIBED_CODE, ((CodeableConcept) result.getExtension().get(0).getValue()).getCoding().get(0).getCode()), + () -> assertEquals(PRESCRIBED_DISPLAY, ((CodeableConcept) result.getExtension().get(0).getValue()).getCoding().get(0).getDisplay())); } @@ -650,6 +577,24 @@ void When_MapToMedicationStatement_With_ConfidentialityCodeInEhrComposition_Expe ); } + private static Stream ehrSupplyTypeMappings() { + return Stream.of( + Arguments.of(PRESCRIPTION, + PRESCRIBED_CODE, + PRESCRIBED_DISPLAY), + Arguments.of(PRESCRIBED_BY_ANOTHER_ORGANISATION_DISPLAY, + PRESCRIBED_BY_ANOTHER_ORGANISATION_CODE, + PRESCRIBED_BY_ANOTHER_ORGANISATION_DISPLAY), + Arguments.of(PRESCRIBED_BY_PREVIOUS_PRACTICE_DISPLAY, + PRESCRIBED_BY_PREVIOUS_PRACTICE_CODE, + PRESCRIBED_BY_PREVIOUS_PRACTICE_DISPLAY), + Arguments.of(OTC_SALE, + PRESCRIBED_BY_ANOTHER_ORGANISATION_CODE, + PRESCRIBED_BY_ANOTHER_ORGANISATION_DISPLAY) + ); + } + + @SneakyThrows private RCMRMT030101UKMedicationStatement unmarshallMedicationStatement(String fileName) { final File file = FileFactory.getXmlFileFor(TEST_FILE_DIRECTORY, fileName);