|
| 1 | +package uk.nhs.adaptors.gp2gp.ehr.utils; |
| 2 | + |
| 3 | +import static org.assertj.core.api.Assertions.assertThatThrownBy; |
| 4 | +import static org.assertj.core.api.AssertionsForClassTypes.assertThat; |
| 5 | + |
| 6 | +import org.assertj.core.api.ThrowableAssert; |
| 7 | +import org.hl7.fhir.dstu3.model.DateTimeType; |
| 8 | +import org.hl7.fhir.dstu3.model.MedicationRequest; |
| 9 | + |
| 10 | +import org.hl7.fhir.dstu3.model.Period; |
| 11 | +import org.junit.jupiter.api.BeforeEach; |
| 12 | +import org.junit.jupiter.api.Test; |
| 13 | +import uk.nhs.adaptors.gp2gp.ehr.exception.EhrMapperException; |
| 14 | + |
| 15 | +public class StatementTimeMappingUtilsTest { |
| 16 | + |
| 17 | + private static final String MEDICATION_REQUEST_ID = "00000000-0000-4000-8000-000000000001"; |
| 18 | + private MedicationRequest medicationRequest; |
| 19 | + |
| 20 | + @BeforeEach |
| 21 | + void beforeEach() { |
| 22 | + medicationRequest = new MedicationRequest(); |
| 23 | + medicationRequest.setId(MEDICATION_REQUEST_ID); |
| 24 | + } |
| 25 | + |
| 26 | + @Test |
| 27 | + void When_PrepareEffectiveTimeForMedicationRequestWithoutEffectivePeriod_Expect_EhrMapperExceptionContainingIdIsThrown() { |
| 28 | + medicationRequest.setDispenseRequest(new MedicationRequest.MedicationRequestDispenseRequestComponent()); |
| 29 | + |
| 30 | + assertThatEhrMapperExceptionWithIdThrown( |
| 31 | + () -> StatementTimeMappingUtils.prepareEffectiveTimeForMedicationRequest(medicationRequest) |
| 32 | + ); |
| 33 | + } |
| 34 | + |
| 35 | + @Test |
| 36 | + void When_PrepareEffectiveTimeForMedicationRequestWithEffectivePeriodWithoutStart_Expect_EhrMapperExceptionContainingIdIsThrown() { |
| 37 | + medicationRequest.setDispenseRequest( |
| 38 | + new MedicationRequest.MedicationRequestDispenseRequestComponent() |
| 39 | + .setValidityPeriod(new Period()) |
| 40 | + ); |
| 41 | + |
| 42 | + assertThatEhrMapperExceptionWithIdThrown( |
| 43 | + () -> StatementTimeMappingUtils.prepareEffectiveTimeForMedicationRequest(medicationRequest) |
| 44 | + ); |
| 45 | + } |
| 46 | + |
| 47 | + @Test |
| 48 | + void When_PrepareEffectiveTimeForMedicationRequestWithEffectivePeriodWithStartOnly_Expect_LowXmlElementProduced() { |
| 49 | + medicationRequest.setDispenseRequest( |
| 50 | + new MedicationRequest.MedicationRequestDispenseRequestComponent() |
| 51 | + .setValidityPeriod(new Period() |
| 52 | + .setStartElement(new DateTimeType("2024-01-01")) |
| 53 | + ) |
| 54 | + ); |
| 55 | + |
| 56 | + var effectiveTimeXML = StatementTimeMappingUtils.prepareEffectiveTimeForMedicationRequest(medicationRequest); |
| 57 | + |
| 58 | + assertThat(effectiveTimeXML) |
| 59 | + .isEqualTo("<low value=\"20240101\"/>"); |
| 60 | + } |
| 61 | + |
| 62 | + @Test |
| 63 | + void When_PrepareEffectiveTimeForMedicationRequestWithEffectivePeriodWithStartAndEnd_Expect_LowAndHighXmlElementsProduced() { |
| 64 | + medicationRequest.setDispenseRequest( |
| 65 | + new MedicationRequest.MedicationRequestDispenseRequestComponent() |
| 66 | + .setValidityPeriod(new Period() |
| 67 | + .setStartElement(new DateTimeType("2024-01-01")) |
| 68 | + .setEndElement(new DateTimeType("2024-02-02")) |
| 69 | + ) |
| 70 | + ); |
| 71 | + |
| 72 | + var effectiveTimeXML = StatementTimeMappingUtils.prepareEffectiveTimeForMedicationRequest(medicationRequest); |
| 73 | + |
| 74 | + assertThat(effectiveTimeXML) |
| 75 | + .isEqualTo("<low value=\"20240101\"/><high value=\"20240202\"/>"); |
| 76 | + } |
| 77 | + |
| 78 | + @Test |
| 79 | + void When_PrepareAvailabilityTimeForMedicationRequestWithoutEffectivePeriod_Expect_EhrMapperExceptionContainingIdIsThrown() { |
| 80 | + medicationRequest.setDispenseRequest(new MedicationRequest.MedicationRequestDispenseRequestComponent()); |
| 81 | + |
| 82 | + assertThatEhrMapperExceptionWithIdThrown( |
| 83 | + () -> StatementTimeMappingUtils.prepareAvailabilityTimeForMedicationRequest(medicationRequest) |
| 84 | + ); |
| 85 | + } |
| 86 | + |
| 87 | + @Test |
| 88 | + void When_PrepareAvailabilityTimeForMedicationRequestWithEffectivePeriodWithoutStart_Expect_EhrMapperExceptionContainingIdIsThrown() { |
| 89 | + medicationRequest.setDispenseRequest( |
| 90 | + new MedicationRequest.MedicationRequestDispenseRequestComponent() |
| 91 | + .setValidityPeriod(new Period()) |
| 92 | + ); |
| 93 | + |
| 94 | + assertThatEhrMapperExceptionWithIdThrown( |
| 95 | + () -> StatementTimeMappingUtils.prepareAvailabilityTimeForMedicationRequest(medicationRequest) |
| 96 | + ); |
| 97 | + } |
| 98 | + |
| 99 | + @Test |
| 100 | + void When_PrepareAvailabilityTimeForMedicationRequestWithEffectivePeriodWithStart_Expect_AvailabilityTimeXmlElementProduced() { |
| 101 | + medicationRequest.setDispenseRequest( |
| 102 | + new MedicationRequest.MedicationRequestDispenseRequestComponent() |
| 103 | + .setValidityPeriod(new Period() |
| 104 | + .setStartElement(new DateTimeType("2024-01-01")) |
| 105 | + ) |
| 106 | + ); |
| 107 | + |
| 108 | + var effectiveTimeXML = StatementTimeMappingUtils.prepareAvailabilityTimeForMedicationRequest(medicationRequest); |
| 109 | + |
| 110 | + assertThat(effectiveTimeXML) |
| 111 | + .isEqualTo("<availabilityTime value=\"20240101\"/>"); |
| 112 | + } |
| 113 | + |
| 114 | + |
| 115 | + private void assertThatEhrMapperExceptionWithIdThrown(ThrowableAssert.ThrowingCallable callable) { |
| 116 | + assertThatThrownBy(callable) |
| 117 | + .isInstanceOf(EhrMapperException.class) |
| 118 | + .hasMessageContaining(MEDICATION_REQUEST_ID); |
| 119 | + } |
| 120 | +} |
0 commit comments