diff --git a/gp2gp-translator/src/test/java/uk/nhs/adaptors/pss/translator/mapper/medication/MedicationRequestMapperTest.java b/gp2gp-translator/src/test/java/uk/nhs/adaptors/pss/translator/mapper/medication/MedicationRequestMapperTest.java index 26f320b68..6a7e046b5 100644 --- a/gp2gp-translator/src/test/java/uk/nhs/adaptors/pss/translator/mapper/medication/MedicationRequestMapperTest.java +++ b/gp2gp-translator/src/test/java/uk/nhs/adaptors/pss/translator/mapper/medication/MedicationRequestMapperTest.java @@ -25,7 +25,10 @@ import org.hl7.v3.RCMRMT030101UKMedicationStatement; import org.hl7.v3.RCMRMT030101UKPrescribe; import org.jetbrains.annotations.NotNull; +import org.junit.jupiter.api.BeforeAll; import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.DisplayName; +import org.junit.jupiter.api.Nested; import org.junit.jupiter.api.Test; import org.junit.jupiter.api.extension.ExtendWith; import org.mockito.InjectMocks; @@ -179,11 +182,11 @@ public void When_MappingMedicationRequestWithAvailabilityTimeInMedicationStateme assertThat(resources).hasSize(EXPECTED_RESOURCES_MAPPED); var medicationRequest = resources - .stream() - .filter(resource -> ResourceType.MedicationRequest.equals(resource.getResourceType())) - .map(MedicationRequest.class::cast) - .findFirst() - .get(); + .stream() + .filter(resource -> ResourceType.MedicationRequest.equals(resource.getResourceType())) + .map(MedicationRequest.class::cast) + .findFirst() + .get(); assertThat(medicationRequest.getAuthoredOnElement().getValue()).isEqualTo(expectedAuthoredOn.getValue()); } @@ -338,399 +341,361 @@ public void When_MappingMedicationRequestWithAuthoredOnValidDateInExtractOnly_Ex } @Test - void When_MultipleOrdersAreBasedOnOneRepeatPlan_Expect_NoAdditionalPlanCreated() { - setupMultipleOrdersToOnePlanStubs(REPEAT_PRESCRIPTION_EXTENSION); - var ehrExtract = unmarshallEhrExtract( - "ehrExtract_MultipleSupplyPrescribeInFulfilmentOfSingleNonAcuteSupplyAuthorise.xml" - ); - - var resources = medicationRequestMapper - .mapResources(ehrExtract, (Patient) new Patient().setId(PATIENT_ID), List.of(), PRACTISE_CODE - ); + public void When_MedicationRequestMapperThrowsException_Expect_MedicationMapperContextToBeReset() { + var ehrExtract = unmarshallEhrExtract("ehrExtract1.xml"); - var planMedicationRequests = resources.stream() - .filter(MedicationRequest.class::isInstance) - .map(MedicationRequest.class::cast) - .filter(medicationRequest -> MedicationRequestIntent.PLAN.equals(medicationRequest.getIntent())); + when(medicationRequestOrderMapper.mapToOrderMedicationRequest(any(), any(), any(), any(), any())) + .thenThrow(NullPointerException.class); - var orderMedicationRequests = resources.stream() - .filter(MedicationRequest.class::isInstance) - .map(MedicationRequest.class::cast) - .filter(medicationRequest -> MedicationRequestIntent.ORDER.equals(medicationRequest.getIntent())) - .toList(); + assertThrows( + NullPointerException.class, + () -> medicationRequestMapper.mapResources( + ehrExtract, + (Patient) new Patient().setId(PATIENT_ID), + List.of(), + PRACTISE_CODE)); - assertAll( - () -> assertThat(planMedicationRequests).as("Plans").hasSize(1), - () -> assertThat(orderMedicationRequests).as("Orders").hasSize(2) - ); + verify(medicationMapperContext).reset(); } @Test - void When_MultipleOrdersAreBasedOnOneAcutePlan_Expect_AdditionalPlanCreated() { - setupMultipleOrdersToOnePlanStubs(ACUTE_PRESCRIPTION_EXTENSION); - var ehrExtract = unmarshallEhrExtract( - "ehrExtract_MultipleSupplyPrescribeInFulfilmentOfSingleAcuteSupplyAuthorise.xml" - ); - - var resources = medicationRequestMapper - .mapResources(ehrExtract, (Patient) new Patient().setId(PATIENT_ID), List.of(), PRACTISE_CODE - ); - - var planMedicationRequests = resources.stream() - .filter(MedicationRequest.class::isInstance) - .map(MedicationRequest.class::cast) - .filter(medicationRequest -> MedicationRequestIntent.PLAN.equals(medicationRequest.getIntent())); + public void When_MedicationRequestMapperCompletesSuccessfully_Expect_MedicationMapperContextToBeReset() { + var ehrExtract = unmarshallEhrExtract("ehrExtract1.xml"); - var orderMedicationRequests = resources.stream() - .filter(MedicationRequest.class::isInstance) - .map(MedicationRequest.class::cast) - .filter(medicationRequest -> MedicationRequestIntent.ORDER.equals(medicationRequest.getIntent())) - .toList(); + medicationRequestMapper + .mapResources(ehrExtract, (Patient) new Patient().setId(PATIENT_ID), List.of(), PRACTISE_CODE); - assertAll( - () -> assertThat(planMedicationRequests).as("Plans").hasSize(2), - () -> assertThat(orderMedicationRequests).as("Orders").hasSize(2) - ); + verify(medicationMapperContext).reset(); } - @Test - void When_MultipleOrdersAreBasedOnOneAcutePlan_Expect_TheGeneratedPlanHasIdAndIdentityUpdatedToGeneratedId() { - setupMultipleOrdersToOnePlanStubs(ACUTE_PRESCRIPTION_EXTENSION); - var ehrExtract = unmarshallEhrExtract( - "ehrExtract_MultipleSupplyPrescribeInFulfilmentOfSingleAcuteSupplyAuthorise.xml" - ); - - var resources = medicationRequestMapper - .mapResources(ehrExtract, (Patient) new Patient().setId(PATIENT_ID), List.of(), PRACTISE_CODE - ); + @Nested() + @DisplayName("WhenTwoEhrSupplyPrescribeReferenceOneRepeatEhrSupplyAuthorise") + class TwoEhrSupplyPrescribeReferenceOneRepeatEhrSupplyAuthorise { - var generatedPlan = getMedicationRequestById(resources, GENERATED_PLAN_ID); + private static RCMRMT030101UKEhrExtract ehrExtract; - assertAll( - () -> assertThat(generatedPlan.getId()) - .isEqualTo(GENERATED_PLAN_ID), - () -> assertThat(generatedPlan.getIdentifierFirstRep().getValue()) - .isEqualTo(GENERATED_PLAN_ID) - ); - } - - @Test - void When_MultipleOrdersAreBasedOnOneAcutePlan_Expect_TheEarliestOrderReferencesTheOriginalPlan() { - setupMultipleOrdersToOnePlanStubs(ACUTE_PRESCRIPTION_EXTENSION); - var ehrExtract = unmarshallEhrExtract( - "ehrExtract_MultipleSupplyPrescribeInFulfilmentOfSingleAcuteSupplyAuthorise.xml" - ); - - var resources = medicationRequestMapper - .mapResources(ehrExtract, (Patient) new Patient().setId(PATIENT_ID), List.of(), PRACTISE_CODE + @BeforeAll + static void beforeAll() { + ehrExtract = unmarshallEhrExtract( + "ehrExtract_MultipleSupplyPrescribeInFulfilmentOfSingleNonAcuteSupplyAuthorise.xml" ); + } - var earliestOrder = getMedicationRequestById(resources, EARLIEST_ORDER_ID); + @BeforeEach + void beforeEach() { + setupMultipleOrdersToOnePlanStubs(REPEAT_PRESCRIPTION_EXTENSION); + } - assertAll( - () -> assertThat(earliestOrder.getBasedOn().getFirst().getReferenceElement().getIdPart()) - .isEqualTo(INITIAL_PLAN_ID), - () -> assertThat(earliestOrder.getBasedOn().getFirst().getReferenceElement().getResourceType()) - .isEqualTo(ResourceType.MedicationRequest.name()) - ); - } - - @Test - void When_MultipleOrdersAreBasedOnOneAcutePlan_Expect_TheLatestOrderReferencesTheGeneratedPlan() { - setupMultipleOrdersToOnePlanStubs(ACUTE_PRESCRIPTION_EXTENSION); - var ehrExtract = unmarshallEhrExtract( - "ehrExtract_MultipleSupplyPrescribeInFulfilmentOfSingleAcuteSupplyAuthorise.xml" - ); + @Test + void expectNoAdditionalPlanCreated() { + var resources = medicationRequestMapper + .mapResources(ehrExtract, (Patient) new Patient().setId(PATIENT_ID), List.of(), PRACTISE_CODE + ); - var resources = medicationRequestMapper - .mapResources(ehrExtract, (Patient) new Patient().setId(PATIENT_ID), List.of(), PRACTISE_CODE); + var planMedicationRequests = resources.stream() + .filter(MedicationRequest.class::isInstance) + .map(MedicationRequest.class::cast) + .filter(medicationRequest -> MedicationRequestIntent.PLAN.equals(medicationRequest.getIntent())); - var latestOrder = getMedicationRequestById(resources, LATEST_ORDER_ID); - var generatedPlan = getMedicationRequestById(resources, GENERATED_PLAN_ID); + var orderMedicationRequests = resources.stream() + .filter(MedicationRequest.class::isInstance) + .map(MedicationRequest.class::cast) + .filter(medicationRequest -> MedicationRequestIntent.ORDER.equals(medicationRequest.getIntent())) + .toList(); - assertAll( - () -> assertThat(latestOrder.getBasedOn().getFirst().getReferenceElement().getIdPart()) - .isEqualTo(generatedPlan.getId()), - () -> assertThat(latestOrder.getBasedOn().getFirst().getReferenceElement().getResourceType()) - .isEqualTo(ResourceType.MedicationRequest.name()) - ); - } + assertAll( + () -> assertThat(planMedicationRequests).as("Plans").hasSize(1), + () -> assertThat(orderMedicationRequests).as("Orders").hasSize(2) + ); + } - @Test - void When_MultipleOrdersAreBasedOnOneAcutePlan_Expect_TheLatestOrderPriorPrescriptionReferencesTheOriginalPlan() { - setupMultipleOrdersToOnePlanStubs(ACUTE_PRESCRIPTION_EXTENSION); - var ehrExtract = unmarshallEhrExtract( - "ehrExtract_MultipleSupplyPrescribeInFulfilmentOfSingleAcuteSupplyAuthorise.xml" - ); + @Test + void expectNoAdditionalMedicationStatementCreated() { + var resources = medicationRequestMapper + .mapResources(ehrExtract, (Patient) new Patient().setId(PATIENT_ID), List.of(), PRACTISE_CODE + ); - var resources = medicationRequestMapper - .mapResources(ehrExtract, (Patient) new Patient().setId(PATIENT_ID), List.of(), PRACTISE_CODE); + var medicationStatements = resources.stream() + .filter(MedicationStatement.class::isInstance) + .map(MedicationStatement.class::cast); - var generatedPlanReferenceElement = getMedicationRequestById(resources, GENERATED_PLAN_ID) - .getPriorPrescription() - .getReferenceElement(); + var orderMedicationRequests = resources.stream() + .filter(MedicationRequest.class::isInstance) + .map(MedicationRequest.class::cast) + .filter(medicationRequest -> MedicationRequestIntent.ORDER.equals(medicationRequest.getIntent())) + .toList(); - assertAll( - () -> assertThat(generatedPlanReferenceElement.getResourceType()) - .isEqualTo(ResourceType.MedicationRequest.name()), - () -> assertThat(generatedPlanReferenceElement.getIdPart()) - .isEqualTo(INITIAL_PLAN_ID) - ); + assertAll( + () -> assertThat(medicationStatements).as("MedicationStatements").hasSize(1), + () -> assertThat(orderMedicationRequests).as("Orders").hasSize(2) + ); + } } - @Test - void When_MultipleOrdersAreBasedOnOneAcutePlan_Expect_GeneratedPlanDispenseRequestValidityPeriodIsCopiedFromTheLatestOrder() { - setupMultipleOrdersToOnePlanStubs(ACUTE_PRESCRIPTION_EXTENSION); - var ehrExtract = unmarshallEhrExtract( - "ehrExtract_MultipleSupplyPrescribeInFulfilmentOfSingleAcuteSupplyAuthorise.xml" - ); - - var resources = medicationRequestMapper - .mapResources(ehrExtract, (Patient) new Patient().setId(PATIENT_ID), List.of(), PRACTISE_CODE); + @Nested() + @DisplayName("WhenTwoEhrSupplyPrescribeReferenceOneAcuteEhrSupplyAuthorise") + class TwoEhrSupplyPrescribeReferenceOneAcuteEhrSupplyAuthorise { - var latestOrderValidityPeriod = getMedicationRequestById(resources, LATEST_ORDER_ID) - .getDispenseRequest() - .getValidityPeriod(); + private static RCMRMT030101UKEhrExtract ehrExtract; - var generatedPlanValidityPeriod = getMedicationRequestById(resources, GENERATED_PLAN_ID) - .getDispenseRequest() - .getValidityPeriod(); + @BeforeAll + static void beforeAll() { + ehrExtract = unmarshallEhrExtract( + "ehrExtract_MultipleSupplyPrescribeInFulfilmentOfSingleAcuteSupplyAuthorise.xml" + ); + } + @BeforeEach + void beforeEach() { + setupMultipleOrdersToOnePlanStubs(ACUTE_PRESCRIPTION_EXTENSION); + } + + @Test + void expectAdditionalPlanCreated() { + var resources = medicationRequestMapper + .mapResources(ehrExtract, (Patient) new Patient().setId(PATIENT_ID), List.of(), PRACTISE_CODE + ); + + var planMedicationRequests = resources.stream() + .filter(MedicationRequest.class::isInstance) + .map(MedicationRequest.class::cast) + .filter(medicationRequest -> MedicationRequestIntent.PLAN.equals(medicationRequest.getIntent())); - assertThat(generatedPlanValidityPeriod) - .usingRecursiveComparison() - .isEqualTo(latestOrderValidityPeriod); - } + var orderMedicationRequests = resources.stream() + .filter(MedicationRequest.class::isInstance) + .map(MedicationRequest.class::cast) + .filter(medicationRequest -> MedicationRequestIntent.ORDER.equals(medicationRequest.getIntent())) + .toList(); - @Test - void When_MultipleOrdersAreBasedOnOneAcutePlan_Expect_TheLatestOrderUnchangedPropertiesAreCopiedToGeneratedPlan() { - setupMultipleOrdersToOnePlanStubs(ACUTE_PRESCRIPTION_EXTENSION); - var ehrExtract = unmarshallEhrExtract( - "ehrExtract_MultipleSupplyPrescribeInFulfilmentOfSingleAcuteSupplyAuthorise.xml" - ); + assertAll( + () -> assertThat(planMedicationRequests).as("Plans").hasSize(2), + () -> assertThat(orderMedicationRequests).as("Orders").hasSize(2) + ); + } - var resources = medicationRequestMapper - .mapResources(ehrExtract, (Patient) new Patient().setId(PATIENT_ID), List.of(), PRACTISE_CODE); + @Test + void expectTheGeneratedPlanHasIdAndIdentityUpdatedToGeneratedId() { + var resources = medicationRequestMapper + .mapResources(ehrExtract, (Patient) new Patient().setId(PATIENT_ID), List.of(), PRACTISE_CODE + ); - var originalPlan = getMedicationRequestById(resources, INITIAL_PLAN_ID); - var generatedPlan = getMedicationRequestById(resources, GENERATED_PLAN_ID); + var generatedPlan = getMedicationRequestById(resources, GENERATED_PLAN_ID); - assertAll( - () -> assertThat(originalPlan.getDosageInstructionFirstRep().getText()) - .isEqualTo(generatedPlan.getDosageInstructionFirstRep().getText()), - () -> assertThat(originalPlan.getDispenseRequest().getId()) - .isEqualTo(generatedPlan.getDispenseRequest().getId()), - () -> assertThat(originalPlan.getExtension().getFirst()) - .usingRecursiveComparison() - .isEqualTo(generatedPlan.getExtension().getFirst()), - () -> assertThat(originalPlan.getStatus()) - .isEqualTo(generatedPlan.getStatus()), - () -> assertThat(originalPlan.getNoteFirstRep().getText()) - .isEqualTo(generatedPlan.getNoteFirstRep().getText()), - () -> assertThat(originalPlan.getMedicationReference().getReference()) - .isEqualTo(generatedPlan.getMedicationReference().getReference()), - () -> assertThat(originalPlan.getMeta()) - .usingRecursiveComparison() - .isEqualTo(generatedPlan.getMeta()) - ); - } - - @Test - void When_MultipleOrdersAreBasedOnOneRepeatPlan_Expect_NoAdditionalMedicationStatementCreated() { - setupMultipleOrdersToOnePlanStubs(REPEAT_PRESCRIPTION_EXTENSION); - var ehrExtract = unmarshallEhrExtract( - "ehrExtract_MultipleSupplyPrescribeInFulfilmentOfSingleNonAcuteSupplyAuthorise.xml" - ); - - var resources = medicationRequestMapper - .mapResources(ehrExtract, (Patient) new Patient().setId(PATIENT_ID), List.of(), PRACTISE_CODE + assertAll( + () -> assertThat(generatedPlan.getId()) + .isEqualTo(GENERATED_PLAN_ID), + () -> assertThat(generatedPlan.getIdentifierFirstRep().getValue()) + .isEqualTo(GENERATED_PLAN_ID) ); + } - var medicationStatements = resources.stream() - .filter(MedicationStatement.class::isInstance) - .map(MedicationStatement.class::cast); + @Test + void expectTheEarliestOrderBasedOnReferencesTheOriginalPlan() { + var resources = medicationRequestMapper + .mapResources(ehrExtract, (Patient) new Patient().setId(PATIENT_ID), List.of(), PRACTISE_CODE + ); - var orderMedicationRequests = resources.stream() - .filter(MedicationRequest.class::isInstance) - .map(MedicationRequest.class::cast) - .filter(medicationRequest -> MedicationRequestIntent.ORDER.equals(medicationRequest.getIntent())) - .toList(); + var earliestOrder = getMedicationRequestById(resources, EARLIEST_ORDER_ID); - assertAll( - () -> assertThat(medicationStatements).as("MedicationStatements").hasSize(1), - () -> assertThat(orderMedicationRequests).as("Orders").hasSize(2) - ); - } - - @Test - void When_MultipleOrdersAreBasedOnOneAcutePlan_Expect_AdditionalMedicationStatementCreated() { - setupMultipleOrdersToOnePlanStubs(ACUTE_PRESCRIPTION_EXTENSION); - var ehrExtract = unmarshallEhrExtract( - "ehrExtract_MultipleSupplyPrescribeInFulfilmentOfSingleAcuteSupplyAuthorise.xml" - ); - - var resources = medicationRequestMapper - .mapResources(ehrExtract, (Patient) new Patient().setId(PATIENT_ID), List.of(), PRACTISE_CODE + assertAll( + () -> assertThat(earliestOrder.getBasedOn().getFirst().getReferenceElement().getIdPart()) + .isEqualTo(INITIAL_PLAN_ID), + () -> assertThat(earliestOrder.getBasedOn().getFirst().getReferenceElement().getResourceType()) + .isEqualTo(ResourceType.MedicationRequest.name()) ); + } - var medicationStatements = resources.stream() - .filter(MedicationStatement.class::isInstance) - .map(MedicationStatement.class::cast); - - var orderMedicationRequests = resources.stream() - .filter(MedicationRequest.class::isInstance) - .map(MedicationRequest.class::cast) - .filter(medicationRequest -> MedicationRequestIntent.ORDER.equals(medicationRequest.getIntent())) - .toList(); - - assertAll( - () -> assertThat(medicationStatements).as("MedicationStatements").hasSize(2), - () -> assertThat(orderMedicationRequests).as("Orders").hasSize(2) - ); - } + @Test + void expectTheLatestOrderBasedOnReferencesTheGeneratedPlan() { + var resources = medicationRequestMapper + .mapResources(ehrExtract, (Patient) new Patient().setId(PATIENT_ID), List.of(), PRACTISE_CODE); - @Test - void When_MultipleOrdersAreBasedOnOneAcutePlan_Expect_GeneratedMedicationStatementHasIdAndIdentityUpdatedToGeneratedPlan() { - setupMultipleOrdersToOnePlanStubs(ACUTE_PRESCRIPTION_EXTENSION); - var ehrExtract = unmarshallEhrExtract( - "ehrExtract_MultipleSupplyPrescribeInFulfilmentOfSingleAcuteSupplyAuthorise.xml" - ); + var latestOrder = getMedicationRequestById(resources, LATEST_ORDER_ID); + var generatedPlan = getMedicationRequestById(resources, GENERATED_PLAN_ID); - var resources = medicationRequestMapper - .mapResources(ehrExtract, (Patient) new Patient().setId(PATIENT_ID), List.of(), PRACTISE_CODE + assertAll( + () -> assertThat(latestOrder.getBasedOn().getFirst().getReferenceElement().getIdPart()) + .isEqualTo(generatedPlan.getId()), + () -> assertThat(latestOrder.getBasedOn().getFirst().getReferenceElement().getResourceType()) + .isEqualTo(ResourceType.MedicationRequest.name()) ); + } - var generatedMedicationStatement = getMedicationStatementById(resources, GENERATED_MEDICATION_STATEMENT_ID); + @Test + void expectTheLatestOrderPriorPrescriptionReferencesTheOriginalPlan() { + var resources = medicationRequestMapper + .mapResources(ehrExtract, (Patient) new Patient().setId(PATIENT_ID), List.of(), PRACTISE_CODE); - assertAll( - () -> assertThat(generatedMedicationStatement.getId()) - .isEqualTo(GENERATED_PLAN_ID + "-MS"), - () -> assertThat(generatedMedicationStatement.getIdentifierFirstRep().getValue()) - .isEqualTo(GENERATED_PLAN_ID + "-MS") - ); - } + var generatedPlanReferenceElement = getMedicationRequestById(resources, GENERATED_PLAN_ID) + .getPriorPrescription() + .getReferenceElement(); - @Test - void When_MultipleOrdersAreBasedOnOneAcutePlan_Expect_GeneratedMedicationStatementReferencesTheGeneratedPlan() { - setupMultipleOrdersToOnePlanStubs(ACUTE_PRESCRIPTION_EXTENSION); - var ehrExtract = unmarshallEhrExtract( - "ehrExtract_MultipleSupplyPrescribeInFulfilmentOfSingleAcuteSupplyAuthorise.xml" - ); - - var resources = medicationRequestMapper - .mapResources(ehrExtract, (Patient) new Patient().setId(PATIENT_ID), List.of(), PRACTISE_CODE + assertAll( + () -> assertThat(generatedPlanReferenceElement.getResourceType()) + .isEqualTo(ResourceType.MedicationRequest.name()), + () -> assertThat(generatedPlanReferenceElement.getIdPart()) + .isEqualTo(INITIAL_PLAN_ID) ); + } - var generatedMedicationStatement = getMedicationStatementById(resources, GENERATED_MEDICATION_STATEMENT_ID); + @Test + void expectGeneratedPlanDispenseRequestValidityPeriodIsCopiedFromTheLatestOrder() { + var resources = medicationRequestMapper + .mapResources(ehrExtract, (Patient) new Patient().setId(PATIENT_ID), List.of(), PRACTISE_CODE); - assertAll( - () -> assertThat(generatedMedicationStatement.getBasedOn().getFirst().getReferenceElement().getIdPart()) - .isEqualTo(GENERATED_PLAN_ID), - () -> assertThat(generatedMedicationStatement.getBasedOn().getFirst().getReferenceElement().getResourceType()) - .isEqualTo(ResourceType.MedicationRequest.name()) - ); - } + var latestOrderValidityPeriod = getMedicationRequestById(resources, LATEST_ORDER_ID) + .getDispenseRequest() + .getValidityPeriod(); - @Test - void When_MultipleOrdersAreBasedOnOneAcutePlan_Expect_GeneratedMedicationStatementEffectivePeriodSetToOrderValidityPeriod() { - setupMultipleOrdersToOnePlanStubs(ACUTE_PRESCRIPTION_EXTENSION); - var ehrExtract = unmarshallEhrExtract( - "ehrExtract_MultipleSupplyPrescribeInFulfilmentOfSingleAcuteSupplyAuthorise.xml" - ); + var generatedPlanValidityPeriod = getMedicationRequestById(resources, GENERATED_PLAN_ID) + .getDispenseRequest() + .getValidityPeriod(); - var resources = medicationRequestMapper - .mapResources(ehrExtract, (Patient) new Patient().setId(PATIENT_ID), List.of(), PRACTISE_CODE - ); - - var latestOrder = getMedicationRequestById(resources, LATEST_ORDER_ID); - var generatedMedicationStatement = getMedicationStatementById(resources, GENERATED_MEDICATION_STATEMENT_ID); - - assertAll( - () -> assertThat(generatedMedicationStatement.getEffectivePeriod()) + assertThat(generatedPlanValidityPeriod) .usingRecursiveComparison() - .isEqualTo(latestOrder.getDispenseRequest().getValidityPeriod()) - ); - } + .isEqualTo(latestOrderValidityPeriod); + } - @Test - void When_MultipleOrdersAreBasedOnOneAcutePlan_Expect_GeneratedMedicationStatementLastIssueDateExtensionSetToValidityPeriodStart() { - setupMultipleOrdersToOnePlanStubs(ACUTE_PRESCRIPTION_EXTENSION); - var ehrExtract = unmarshallEhrExtract( - "ehrExtract_MultipleSupplyPrescribeInFulfilmentOfSingleAcuteSupplyAuthorise.xml" - ); + @Test + void expectTheLatestOrderUnchangedPropertiesAreCopiedToGeneratedPlan() { + var resources = medicationRequestMapper + .mapResources(ehrExtract, (Patient) new Patient().setId(PATIENT_ID), List.of(), PRACTISE_CODE); - var resources = medicationRequestMapper - .mapResources(ehrExtract, (Patient) new Patient().setId(PATIENT_ID), List.of(), PRACTISE_CODE + var originalPlan = getMedicationRequestById(resources, INITIAL_PLAN_ID); + var generatedPlan = getMedicationRequestById(resources, GENERATED_PLAN_ID); + + assertAll( + () -> assertThat(originalPlan.getDosageInstructionFirstRep().getText()) + .isEqualTo(generatedPlan.getDosageInstructionFirstRep().getText()), + () -> assertThat(originalPlan.getDispenseRequest().getId()) + .isEqualTo(generatedPlan.getDispenseRequest().getId()), + () -> assertThat(originalPlan.getExtension().getFirst()) + .usingRecursiveComparison() + .isEqualTo(generatedPlan.getExtension().getFirst()), + () -> assertThat(originalPlan.getStatus()) + .isEqualTo(generatedPlan.getStatus()), + () -> assertThat(originalPlan.getNoteFirstRep().getText()) + .isEqualTo(generatedPlan.getNoteFirstRep().getText()), + () -> assertThat(originalPlan.getMedicationReference().getReference()) + .isEqualTo(generatedPlan.getMedicationReference().getReference()), + () -> assertThat(originalPlan.getMeta()) + .usingRecursiveComparison() + .isEqualTo(generatedPlan.getMeta()) ); + } - var latestOrder = getMedicationRequestById(resources, LATEST_ORDER_ID); - var generatedMedicationStatement = getMedicationStatementById(resources, GENERATED_MEDICATION_STATEMENT_ID); + @Test + void expectAdditionalMedicationStatementCreated() { + var resources = medicationRequestMapper + .mapResources(ehrExtract, (Patient) new Patient().setId(PATIENT_ID), List.of(), PRACTISE_CODE + ); - assertThat(generatedMedicationStatement.getExtensionByUrl(MEDICATION_STATEMENT_LAST_ISSUE_DATE_URL).getValue()) - .isEqualTo(latestOrder.getDispenseRequest().getValidityPeriod().getStartElement()); - } + var medicationStatements = resources.stream() + .filter(MedicationStatement.class::isInstance) + .map(MedicationStatement.class::cast); - @Test - void When_MultipleOrdersAreBasedOnOneAcutePlan_Expect_OriginalMedicationStatementUnchangedPropertiesAreCopiedToGeneratedPlan() { - setupMultipleOrdersToOnePlanStubs(ACUTE_PRESCRIPTION_EXTENSION); - var ehrExtract = unmarshallEhrExtract( - "ehrExtract_MultipleSupplyPrescribeInFulfilmentOfSingleAcuteSupplyAuthorise.xml" - ); + var orderMedicationRequests = resources.stream() + .filter(MedicationRequest.class::isInstance) + .map(MedicationRequest.class::cast) + .filter(medicationRequest -> MedicationRequestIntent.ORDER.equals(medicationRequest.getIntent())) + .toList(); - var resources = medicationRequestMapper - .mapResources(ehrExtract, (Patient) new Patient().setId(PATIENT_ID), List.of(), PRACTISE_CODE + assertAll( + () -> assertThat(medicationStatements).as("MedicationStatements").hasSize(2), + () -> assertThat(orderMedicationRequests).as("Orders").hasSize(2) ); + } - var originalMedicationStatement = getMedicationStatementById(resources, INITIAL_MEDICATION_STATEMENT_ID); - var generatedMedicationStatement = getMedicationStatementById(resources, GENERATED_MEDICATION_STATEMENT_ID); + @Test + void expectGeneratedMedicationStatementHasIdAndIdentityUpdatedToGeneratedPlanWithSuffix() { + var resources = medicationRequestMapper + .mapResources(ehrExtract, (Patient) new Patient().setId(PATIENT_ID), List.of(), PRACTISE_CODE + ); - assertAll( - () -> assertThat(generatedMedicationStatement.getTaken()) - .isEqualTo(originalMedicationStatement.getTaken()), - () -> assertThat(generatedMedicationStatement.getDosage()) - .usingRecursiveComparison() - .isEqualTo(originalMedicationStatement.getDosage()), - () -> assertThat(generatedMedicationStatement.getExtension().getFirst()) - .isEqualTo(generatedMedicationStatement.getExtension().getFirst()), - () -> assertThat(generatedMedicationStatement.getMeta()) - .usingRecursiveComparison() - .isEqualTo(originalMedicationStatement.getMeta()), - () -> assertThat(generatedMedicationStatement.getMedicationReference().getReferenceElement()) - .isEqualTo(originalMedicationStatement.getMedicationReference().getReferenceElement()), - () -> assertThat(generatedMedicationStatement.getStatus()) - .isEqualTo(originalMedicationStatement.getStatus()) - ); - } + var generatedMedicationStatement = getMedicationStatementById(resources, GENERATED_MEDICATION_STATEMENT_ID); - @Test - public void When_MedicationRequestMapperThrowsException_Expect_MedicationMapperContextToBeReset() { - var ehrExtract = unmarshallEhrExtract("ehrExtract1.xml"); + assertAll( + () -> assertThat(generatedMedicationStatement.getId()) + .isEqualTo(GENERATED_PLAN_ID + "-MS"), + () -> assertThat(generatedMedicationStatement.getIdentifierFirstRep().getValue()) + .isEqualTo(GENERATED_PLAN_ID + "-MS") + ); + } - when(medicationRequestOrderMapper.mapToOrderMedicationRequest(any(), any(), any(), any(), any())) - .thenThrow(NullPointerException.class); + @Test + void expectGeneratedMedicationStatementBasedOnReferencesTheGeneratedPlan() { + var resources = medicationRequestMapper + .mapResources(ehrExtract, (Patient) new Patient().setId(PATIENT_ID), List.of(), PRACTISE_CODE + ); - assertThrows( - NullPointerException.class, - () -> medicationRequestMapper.mapResources( - ehrExtract, - (Patient) new Patient().setId(PATIENT_ID), - List.of(), - PRACTISE_CODE)); + var generatedMedicationStatement = getMedicationStatementById(resources, GENERATED_MEDICATION_STATEMENT_ID); - verify(medicationMapperContext).reset(); - } + assertAll( + () -> assertThat(generatedMedicationStatement.getBasedOn().getFirst().getReferenceElement().getIdPart()) + .isEqualTo(GENERATED_PLAN_ID), + () -> assertThat(generatedMedicationStatement.getBasedOn().getFirst().getReferenceElement().getResourceType()) + .isEqualTo(ResourceType.MedicationRequest.name()) + ); + } - @Test - public void When_MedicationRequestMapperCompletesSuccessfully_Expect_MedicationMapperContextToBeReset() { - var ehrExtract = unmarshallEhrExtract("ehrExtract1.xml"); + @Test + void expectGeneratedMedicationStatementEffectivePeriodSetToOrderValidityPeriod() { + var resources = medicationRequestMapper + .mapResources(ehrExtract, (Patient) new Patient().setId(PATIENT_ID), List.of(), PRACTISE_CODE + ); - medicationRequestMapper - .mapResources(ehrExtract, (Patient) new Patient().setId(PATIENT_ID), List.of(), PRACTISE_CODE); + var latestOrder = getMedicationRequestById(resources, LATEST_ORDER_ID); + var generatedMedicationStatement = getMedicationStatementById(resources, GENERATED_MEDICATION_STATEMENT_ID); - verify(medicationMapperContext).reset(); + assertAll( + () -> assertThat(generatedMedicationStatement.getEffectivePeriod()) + .usingRecursiveComparison() + .isEqualTo(latestOrder.getDispenseRequest().getValidityPeriod()) + ); + } + + @Test + void expectGeneratedMedicationStatementLastIssueDateExtensionSetToValidityPeriodStart() { + var resources = medicationRequestMapper + .mapResources(ehrExtract, (Patient) new Patient().setId(PATIENT_ID), List.of(), PRACTISE_CODE + ); + + var latestOrder = getMedicationRequestById(resources, LATEST_ORDER_ID); + var generatedMedicationStatement = getMedicationStatementById(resources, GENERATED_MEDICATION_STATEMENT_ID); + + assertThat(generatedMedicationStatement.getExtensionByUrl(MEDICATION_STATEMENT_LAST_ISSUE_DATE_URL).getValue()) + .isEqualTo(latestOrder.getDispenseRequest().getValidityPeriod().getStartElement()); + } + + @Test + void expectOriginalMedicationStatementUnchangedPropertiesAreCopiedToGeneratedMedicationStatement() { + var resources = medicationRequestMapper + .mapResources(ehrExtract, (Patient) new Patient().setId(PATIENT_ID), List.of(), PRACTISE_CODE + ); + + var originalMedicationStatement = getMedicationStatementById(resources, INITIAL_MEDICATION_STATEMENT_ID); + var generatedMedicationStatement = getMedicationStatementById(resources, GENERATED_MEDICATION_STATEMENT_ID); + + assertAll( + () -> assertThat(generatedMedicationStatement.getTaken()) + .isEqualTo(originalMedicationStatement.getTaken()), + () -> assertThat(generatedMedicationStatement.getDosage()) + .usingRecursiveComparison() + .isEqualTo(originalMedicationStatement.getDosage()), + () -> assertThat(generatedMedicationStatement.getExtension().getFirst()) + .isEqualTo(generatedMedicationStatement.getExtension().getFirst()), + () -> assertThat(generatedMedicationStatement.getMeta()) + .usingRecursiveComparison() + .isEqualTo(originalMedicationStatement.getMeta()), + () -> assertThat(generatedMedicationStatement.getMedicationReference().getReferenceElement()) + .isEqualTo(originalMedicationStatement.getMedicationReference().getReferenceElement()), + () -> assertThat(generatedMedicationStatement.getStatus()) + .isEqualTo(originalMedicationStatement.getStatus()) + ); + } } @SneakyThrows - private RCMRMT030101UKEhrExtract unmarshallEhrExtract(String fileName) { + private static RCMRMT030101UKEhrExtract unmarshallEhrExtract(String fileName) { return unmarshallFile(getFile("classpath:" + XML_RESOURCES_BASE + fileName), RCMRMT030101UKEhrExtract.class); }