Skip to content

Commit 591a8bc

Browse files
Improve exception messages when mapping MedicationRequest AvailabilityTime and EffectiveTime fields (#964)
* Improve exception messages when mapping MedicationRequest `AvailabilityTime` and `EffectiveTime` fields. Improve exception message detailing the reason for the failure and the ID of the resource causing the exception. Add unit tests for the `prepareAvailabilityTimeForMedicationRequest` and `prepareEffectiveTimeForMedicationRequest` methods as none previously existed. * Fix spotbugs issue with \n in multiline strings * Condense exception message, without losing meaning for user --------- Co-authored-by: Adrian Clay <[email protected]>
1 parent 0f82324 commit 591a8bc

File tree

2 files changed

+126
-2
lines changed

2 files changed

+126
-2
lines changed

service/src/main/java/uk/nhs/adaptors/gp2gp/ehr/utils/StatementTimeMappingUtils.java

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -137,7 +137,9 @@ public static String prepareEffectiveTimeForMedicationRequest(MedicationRequest
137137
}
138138
return String.format(EFFECTIVE_TIME_LOW_TEMPLATE, toHl7Format(startElement));
139139
}
140-
throw new EhrMapperException("Could not map Effective Time for Medication Request");
140+
throw new EhrMapperException(
141+
"MedicationRequest/{%s} must contain a dispenseRequest.validityPeriod.start".formatted(medicationRequest.getId())
142+
);
141143
}
142144

143145
public static String prepareAvailabilityTimeForMedicationRequest(MedicationRequest medicationRequest) {
@@ -146,7 +148,9 @@ public static String prepareAvailabilityTimeForMedicationRequest(MedicationReque
146148
return String.format(AVAILABILITY_TIME_VALUE_TEMPLATE, toHl7Format(
147149
medicationRequest.getDispenseRequest().getValidityPeriod().getStartElement()));
148150
}
149-
throw new EhrMapperException("Could not map Availability Time for Medication Request");
151+
throw new EhrMapperException(
152+
"MedicationRequest/{%s} must contain a dispenseRequest.validityPeriod.start".formatted(medicationRequest.getId())
153+
);
150154
}
151155

152156
public static String prepareEffectiveTimeForEhrFolder(EhrFolderEffectiveTime effectiveTime) {
Lines changed: 120 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,120 @@
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

Comments
 (0)