Skip to content

Commit bd65bd3

Browse files
committed
FhirParseService refactoring
1 parent bf88ffd commit bd65bd3

36 files changed

+155
-103
lines changed

service/src/intTest/java/uk/nhs/adaptors/gp2gp/common/task/BaseTaskTest.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package uk.nhs.adaptors.gp2gp.common.task;
22

3+
import ca.uhn.fhir.context.FhirContext;
34
import org.junit.jupiter.api.extension.ExtendWith;
45
import org.springframework.boot.test.context.SpringBootTest;
56
import org.springframework.boot.test.mock.mockito.MockBean;
@@ -17,7 +18,7 @@
1718
@SuppressWarnings("checkstyle:VisibilityModifier")
1819
public abstract class BaseTaskTest {
1920
public static final ObjectMapper OBJECT_MAPPER = new ObjectMapper();
20-
public static final FhirParseService FHIR_PARSE_SERVICE = new FhirParseService();
21+
public static final FhirParseService FHIR_PARSE_SERVICE = new FhirParseService(FhirContext.forDstu3());
2122

2223
@MockBean
2324
protected TaskDispatcher taskDispatcher;

service/src/main/java/uk/nhs/adaptors/gp2gp/common/service/FhirParseService.java

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,12 @@
1010

1111
@Service
1212
public class FhirParseService {
13-
private final IParser jsonParser = prepareParser();
13+
14+
private final IParser jsonParser;
15+
16+
public FhirParseService(FhirContext fhirContext) {
17+
this.jsonParser = prepareParser(fhirContext);
18+
}
1419

1520
public <T extends IBaseResource> T parseResource(String body, Class<T> fhirClass) {
1621
try {
@@ -20,10 +25,9 @@ public <T extends IBaseResource> T parseResource(String body, Class<T> fhirClass
2025
}
2126
}
2227

23-
private IParser prepareParser() {
24-
FhirContext ctx = FhirContext.forDstu3();
25-
ctx.newJsonParser();
26-
ctx.setParserErrorHandler(new StrictErrorHandler());
27-
return ctx.newJsonParser();
28+
private IParser prepareParser(FhirContext fhirContext) {
29+
fhirContext.newJsonParser();
30+
fhirContext.setParserErrorHandler(new StrictErrorHandler());
31+
return fhirContext.newJsonParser();
2832
}
2933
}

service/src/main/java/uk/nhs/adaptors/gp2gp/transformJsonToXmlTool/TransformJsonToXml.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
import java.util.List;
1313
import java.util.stream.Collectors;
1414

15+
import ca.uhn.fhir.context.FhirContext;
1516
import org.apache.commons.io.FilenameUtils;
1617
import org.hl7.fhir.dstu3.model.Bundle;
1718
import org.hl7.fhir.dstu3.model.Identifier;
@@ -116,7 +117,7 @@ private List<InputFile> getFiles() throws UnreadableJsonFileException, NoJsonFil
116117
final String mapJsonToXml(String jsonAsStringInput) {
117118
String hl7TranslatedResponse;
118119
try {
119-
final Bundle bundle = new FhirParseService().parseResource(jsonAsStringInput, Bundle.class);
120+
final Bundle bundle = new FhirParseService(FhirContext.forDstu3()).parseResource(jsonAsStringInput, Bundle.class);
120121

121122
messageContext.initialize(bundle);
122123

service/src/test/java/uk/nhs/adaptors/gp2gp/ehr/mapper/AgentDirectoryMapperTest.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010

1111
import java.io.IOException;
1212

13+
import ca.uhn.fhir.context.FhirContext;
1314
import org.hl7.fhir.dstu3.model.Bundle;
1415
import org.hl7.fhir.dstu3.model.ResourceType;
1516
import org.junit.jupiter.api.AfterEach;
@@ -56,7 +57,7 @@ public void setUp() {
5657
lenient().when(agentPersonMapper.mapAgentPerson(any(), any())).thenAnswer(answerWithObjectId());
5758

5859
agentDirectoryMapper = new AgentDirectoryMapper(messageContext, agentPersonMapper);
59-
fhirParseService = new FhirParseService();
60+
fhirParseService = new FhirParseService(FhirContext.forDstu3());
6061
}
6162

6263
private Answer<String> answerWithObjectId() {

service/src/test/java/uk/nhs/adaptors/gp2gp/ehr/mapper/AgentDirectoryTest.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
import java.util.Map;
1010
import java.util.Optional;
1111

12+
import ca.uhn.fhir.context.FhirContext;
1213
import org.hl7.fhir.dstu3.model.Bundle;
1314
import org.hl7.fhir.dstu3.model.ResourceType;
1415
import org.junit.jupiter.api.BeforeEach;
@@ -44,7 +45,7 @@ public void setUp() throws IOException {
4445
lenient().when(randomIdGeneratorService.createNewId()).thenReturn(GENERATED_ID_1, GENERATED_ID_2);
4546

4647
String jsonInput = ResourceTestFileUtils.getFileContent(INPUT_BUNDLE);
47-
inputBundle = new FhirParseService().parseResource(jsonInput, Bundle.class);
48+
inputBundle = new FhirParseService(FhirContext.forDstu3()).parseResource(jsonInput, Bundle.class);
4849
}
4950

5051
@Test

service/src/test/java/uk/nhs/adaptors/gp2gp/ehr/mapper/AgentPersonMapperTest.java

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
import java.io.IOException;
77
import java.util.stream.Stream;
88

9+
import ca.uhn.fhir.context.FhirContext;
910
import org.hl7.fhir.dstu3.model.Bundle;
1011
import org.hl7.fhir.dstu3.model.Organization;
1112
import org.hl7.fhir.dstu3.model.Practitioner;
@@ -52,7 +53,7 @@ public void setUp() {
5253
when(randomIdGeneratorService.createNewId()).thenReturn(TEST_ID);
5354
messageContext = new MessageContext(randomIdGeneratorService);
5455
agentPersonMapper = new AgentPersonMapper(messageContext);
55-
fhirParseService = new FhirParseService();
56+
fhirParseService = new FhirParseService(FhirContext.forDstu3());
5657
}
5758

5859
@ParameterizedTest
@@ -149,12 +150,12 @@ private static Stream<Arguments> readPractitionerRoleTests() {
149150

150151
private static Practitioner getPractitionerResource() throws IOException {
151152
String jsonPractitioner = ResourceTestFileUtils.getFileContent(PRACTITIONER);
152-
return new FhirParseService().parseResource(jsonPractitioner, Practitioner.class);
153+
return new FhirParseService(FhirContext.forDstu3()).parseResource(jsonPractitioner, Practitioner.class);
153154
}
154155

155156
private static Organization getOrganizationResource() throws IOException {
156157
String jsonOrganization = ResourceTestFileUtils.getFileContent(ORGANIZATION);
157-
return new FhirParseService().parseResource(jsonOrganization, Organization.class);
158+
return new FhirParseService(FhirContext.forDstu3()).parseResource(jsonOrganization, Organization.class);
158159
}
159160

160161
@AfterEach

service/src/test/java/uk/nhs/adaptors/gp2gp/ehr/mapper/AllergyStructureMapperTest.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
import java.util.Optional;
1616
import java.util.stream.Stream;
1717

18+
import ca.uhn.fhir.context.FhirContext;
1819
import org.hl7.fhir.dstu3.model.AllergyIntolerance;
1920
import org.hl7.fhir.dstu3.model.Bundle;
2021
import org.hl7.fhir.dstu3.model.CodeableConcept;
@@ -198,7 +199,7 @@ public void setUp() throws IOException {
198199
.thenReturn(Optional.empty());
199200

200201
var bundleInput = ResourceTestFileUtils.getFileContent(INPUT_JSON_BUNDLE);
201-
Bundle bundle = new FhirParseService().parseResource(bundleInput, Bundle.class);
202+
Bundle bundle = new FhirParseService(FhirContext.forDstu3()).parseResource(bundleInput, Bundle.class);
202203
messageContext = new MessageContext(randomIdGeneratorService);
203204
messageContext.initialize(bundle);
204205
List.of(ResourceType.Patient, ResourceType.Device)
@@ -265,7 +266,7 @@ public void When_ConfidentialityServiceReturnsEmptyOptional_Expect_MessageDoesNo
265266

266267
private static AllergyIntolerance parseAllergyIntoleranceFromJsonFile(String filepath) {
267268
final var jsonInput = ResourceTestFileUtils.getFileContent(filepath);
268-
return new FhirParseService().parseResource(jsonInput, AllergyIntolerance.class);
269+
return new FhirParseService(FhirContext.forDstu3()).parseResource(jsonInput, AllergyIntolerance.class);
269270
}
270271

271272
}

service/src/test/java/uk/nhs/adaptors/gp2gp/ehr/mapper/BloodPressureMapperTest.java

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package uk.nhs.adaptors.gp2gp.ehr.mapper;
22

3+
import ca.uhn.fhir.context.FhirContext;
34
import org.hl7.fhir.dstu3.model.Bundle;
45
import org.hl7.fhir.dstu3.model.CodeableConcept;
56
import org.hl7.fhir.dstu3.model.Observation;
@@ -59,6 +60,7 @@ public class BloodPressureMapperTest {
5960
private static final String INPUT_BLOOD_PRESSURE_WITH_CODEABLE_CONCEPTS = "blood-pressure-with-codeable-concepts.json";
6061
private static final String EXPECTED_BLOOD_PRESSURE_WITH_CODEABLE_CONCEPTS = "blood-pressure-with-codeable-concepts.xml";
6162
private static final String INPUT_BLOOD_PRESSURE_WITH_NO_CODEABLE_CONCEPTS = "blood-pressure-with-no-codeable-concepts.json";
63+
private FhirContext fhirCtx = FhirContext.forDstu3();
6264

6365
@Mock
6466
private RandomIdGeneratorService randomIdGeneratorService;
@@ -92,7 +94,7 @@ public void When_MappingEmptyObservation_Expect_CompoundStatementXmlReturned() t
9294
var jsonInput = ResourceTestFileUtils.getFileContent(BLOOD_PRESSURE_FILE_LOCATION + INPUT_EMPTY_OBSERVATION);
9395
var expectedOutput = ResourceTestFileUtils.getFileContent(BLOOD_PRESSURE_FILE_LOCATION + EXPECTED_EMPTY_OBSERVATION);
9496

95-
Observation observation = new FhirParseService().parseResource(jsonInput, Observation.class);
97+
Observation observation = new FhirParseService(fhirCtx).parseResource(jsonInput, Observation.class);
9698
var outputMessage = bloodPressureMapper.mapBloodPressure(observation, false);
9799

98100
assertThat(outputMessage).isEqualTo(expectedOutput);
@@ -106,7 +108,7 @@ public void When_MappingBloodPressureWithNestedTrue_Expect_CompoundStatementXmlR
106108
var jsonInput = ResourceTestFileUtils.getFileContent(BLOOD_PRESSURE_FILE_LOCATION + INPUT_BLOOD_PRESSURE_WITH_DATA);
107109
var expectedOutput = ResourceTestFileUtils.getFileContent(BLOOD_PRESSURE_FILE_LOCATION + EXPECTED_NESTED_BLOOD_PRESSURE);
108110

109-
Observation observation = new FhirParseService().parseResource(jsonInput, Observation.class);
111+
Observation observation = new FhirParseService(fhirCtx).parseResource(jsonInput, Observation.class);
110112
var outputMessage = bloodPressureMapper.mapBloodPressure(observation, true);
111113

112114
assertThat(outputMessage).isEqualToIgnoringWhitespace(expectedOutput);
@@ -121,7 +123,7 @@ public void When_MappingBloodPressure_Expect_CompoundStatementXmlReturned(String
121123
var jsonInput = ResourceTestFileUtils.getFileContent(BLOOD_PRESSURE_FILE_LOCATION + inputJson);
122124
var expectedOutput = ResourceTestFileUtils.getFileContent(BLOOD_PRESSURE_FILE_LOCATION + outputXml);
123125

124-
Observation observation = new FhirParseService().parseResource(jsonInput, Observation.class);
126+
Observation observation = new FhirParseService(fhirCtx).parseResource(jsonInput, Observation.class);
125127
var outputMessage = bloodPressureMapper.mapBloodPressure(observation, false);
126128

127129
assertThat(outputMessage)
@@ -157,7 +159,7 @@ public void When_MappingBloodPressureWithCodeableConcepts_Expect_CompoundStateme
157159
messageContext, randomIdGeneratorService, new StructuredObservationValueMapper(),
158160
codeableConceptCdMapper, new ParticipantMapper());
159161

160-
Observation observation = new FhirParseService().parseResource(jsonInput, Observation.class);
162+
Observation observation = new FhirParseService(fhirCtx).parseResource(jsonInput, Observation.class);
161163
var outputMessage = bloodPressureMapper.mapBloodPressure(observation, true);
162164

163165
assertThat(outputMessage).isEqualToIgnoringWhitespace(expectedOutput);
@@ -172,7 +174,7 @@ public void When_MappingBloodPressureWithNoCodeableConcepts_Expect_Exception() t
172174
messageContext, randomIdGeneratorService, new StructuredObservationValueMapper(),
173175
codeableConceptCdMapper, new ParticipantMapper());
174176

175-
Observation observation = new FhirParseService().parseResource(jsonInput, Observation.class);
177+
Observation observation = new FhirParseService(fhirCtx).parseResource(jsonInput, Observation.class);
176178

177179
assertThrows(EhrMapperException.class, ()
178180
-> bloodPressureMapper.mapBloodPressure(observation, true));

service/src/test/java/uk/nhs/adaptors/gp2gp/ehr/mapper/CodeableConceptCdMapperTest.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
import java.io.IOException;
66
import java.util.stream.Stream;
77

8+
import ca.uhn.fhir.context.FhirContext;
89
import org.hl7.fhir.dstu3.model.AllergyIntolerance;
910
import org.hl7.fhir.dstu3.model.Condition;
1011
import org.hl7.fhir.dstu3.model.Medication;
@@ -68,7 +69,7 @@ private static Stream<Arguments> getTestArgumentsForTopicRelatedProblem() {
6869

6970
@BeforeEach
7071
public void setUp() {
71-
fhirParseService = new FhirParseService();
72+
fhirParseService = new FhirParseService(FhirContext.forDstu3());
7273
codeableConceptCdMapper = new CodeableConceptCdMapper();
7374
}
7475

service/src/test/java/uk/nhs/adaptors/gp2gp/ehr/mapper/ConditionLinkSetMapperTest.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package uk.nhs.adaptors.gp2gp.ehr.mapper;
22

3+
import ca.uhn.fhir.context.FhirContext;
34
import org.hl7.fhir.dstu3.model.Bundle;
45
import org.hl7.fhir.dstu3.model.CodeableConcept;
56
import org.hl7.fhir.dstu3.model.Condition;
@@ -131,7 +132,7 @@ class ConditionLinkSetMapperTest {
131132
@BeforeEach
132133
void setUp() throws IOException {
133134
var bundleInput = ResourceTestFileUtils.getFileContent(TEST_FILES_DIRECTORY + INPUT_JSON_BUNDLE);
134-
final Bundle bundle = new FhirParseService().parseResource(bundleInput, Bundle.class);
135+
final Bundle bundle = new FhirParseService(FhirContext.forDstu3()).parseResource(bundleInput, Bundle.class);
135136
inputBundle = new InputBundle(bundle);
136137

137138
lenient().when(codeableConceptCdMapper.mapCodeableConceptToCd(any(CodeableConcept.class)))

0 commit comments

Comments
 (0)