From 944685761022ffe6e74affb706c48ecb2313e958 Mon Sep 17 00:00:00 2001 From: ORybak5 <12736698+ORybak5@users.noreply.github.com> Date: Thu, 22 Jan 2026 12:51:46 +0000 Subject: [PATCH 1/2] test refactoring --- .../MedicationStatementMapperTest.java | 173 +++++------------- 1 file changed, 47 insertions(+), 126 deletions(-) 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..722266b94 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 @@ -7,7 +7,6 @@ import static org.hl7.fhir.dstu3.model.MedicationStatement.MedicationStatementTaken.UNK; import static org.junit.jupiter.api.Assertions.assertAll; import static org.junit.jupiter.api.Assertions.assertEquals; -import static org.junit.jupiter.api.Assertions.assertThrows; import static org.mockito.ArgumentMatchers.any; import static org.mockito.ArgumentMatchers.eq; import static org.mockito.Mockito.when; @@ -21,6 +20,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 +42,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; @@ -100,103 +103,27 @@ public void registerDefaultDependencies(Object... dependencies) { } } - @Test - void When_MappingPrescribedWithUnknownEhrSupplyType_Expect_MedStatementMappedToDefaultEhrSupplyType() 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("unknown EhrSupplyType"); - - Exception exception = assertThrows(IllegalArgumentException.class, - () -> medicationStatementMapper.mapToMedicationStatement( - ehrExtract, - ehrComposition, - medicationStatement, - authorise.get(), - PRACTISE_CODE, - new DateTimeType())); - - assertThat(exception.getMessage()).contains("Unsupported prescribing agency: unknown EhrSupplyType"); - } + @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_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())); - } - - @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); + assertThat(authorise).isPresent(); + authorise.get().getCode().setDisplayName(ehrSupplyDisplay); final MedicationStatement result = medicationStatementMapper.mapToMedicationStatement( ehrExtract, @@ -206,42 +133,16 @@ void When_MappingPrescribedByNhsResource_Expect_AllFieldsToBeMappedCorrectly() t 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()); - - final MedicationStatement result = medicationStatementMapper.mapToMedicationStatement( - ehrExtract, - ehrComposition, - medicationStatement, - authorise.get(), - 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 +153,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 +171,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 +182,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 +193,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 +211,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 +222,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 +550,27 @@ void When_MapToMedicationStatement_With_ConfidentialityCodeInEhrComposition_Expe ); } + private static Stream ehrSupplyTypeMappings() { + return Stream.of( + Arguments.of("unknown EhrSupplyType", + PRESCRIBED_CODE, + PRESCRIBED_DISPLAY), + 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); From 8e8017863b791cde4bf3c2c6a7e7ed185b24549d Mon Sep 17 00:00:00 2001 From: ORybak5 <12736698+ORybak5@users.noreply.github.com> Date: Fri, 23 Jan 2026 10:11:28 +0000 Subject: [PATCH 2/2] adding missing tests --- .../MedicationStatementMapperTest.java | 30 +++++++++++++++++-- 1 file changed, 27 insertions(+), 3 deletions(-) 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 722266b94..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 @@ -7,6 +7,7 @@ import static org.hl7.fhir.dstu3.model.MedicationStatement.MedicationStatementTaken.UNK; import static org.junit.jupiter.api.Assertions.assertAll; import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertThrows; import static org.mockito.ArgumentMatchers.any; import static org.mockito.ArgumentMatchers.eq; import static org.mockito.Mockito.when; @@ -103,6 +104,32 @@ public void registerDefaultDependencies(Object... dependencies) { } } + @Test + void When_MappingPrescribedWithUnknownEhrSupplyType_Expect_MedStatementMappedToDefaultEhrSupplyType() 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("unknown EhrSupplyType"); + + Exception exception = assertThrows(IllegalArgumentException.class, + () -> medicationStatementMapper.mapToMedicationStatement( + ehrExtract, + ehrComposition, + medicationStatement, + authorise.get(), + PRACTISE_CODE, + new DateTimeType())); + + assertThat(exception.getMessage()).contains("Unsupported prescribing agency: unknown EhrSupplyType"); + } + @ParameterizedTest(name = "EhrSupplyType \"{0}\" maps to code \"{1}\" and display \"{2}\"") @MethodSource("ehrSupplyTypeMappings") void When_MappingPrescribedResources_Expect_CorrectPrescribingAgencyMapped( @@ -552,9 +579,6 @@ void When_MapToMedicationStatement_With_ConfidentialityCodeInEhrComposition_Expe private static Stream ehrSupplyTypeMappings() { return Stream.of( - Arguments.of("unknown EhrSupplyType", - PRESCRIBED_CODE, - PRESCRIBED_DISPLAY), Arguments.of(PRESCRIPTION, PRESCRIBED_CODE, PRESCRIBED_DISPLAY),