Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
Expand Down Expand Up @@ -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<RCMRMT030101UKAuthorise> 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<RCMRMT030101UKAuthorise> 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<RCMRMT030101UKAuthorise> 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<RCMRMT030101UKAuthorise> 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,
Expand All @@ -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);
Expand All @@ -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,
Expand All @@ -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);
Expand All @@ -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,
Expand All @@ -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);
Expand All @@ -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,
Expand All @@ -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()));
}

Expand Down Expand Up @@ -650,6 +577,24 @@ void When_MapToMedicationStatement_With_ConfidentialityCodeInEhrComposition_Expe
);
}

private static Stream<Arguments> 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);
Expand Down