Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

FhirParseService refactoring #1002

Closed
wants to merge 7 commits into from
Closed
Show file tree
Hide file tree
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
@@ -1,5 +1,6 @@
package uk.nhs.adaptors.gp2gp.common.task;

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

@MockBean
protected TaskDispatcher taskDispatcher;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,15 +1,21 @@
package uk.nhs.adaptors.gp2gp.gpc.configuration;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
package uk.nhs.adaptors.gp2gp.common.configuration;

import ca.uhn.fhir.context.FhirContext;
import ca.uhn.fhir.parser.IParser;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
public class GpcFhirParserConfig {
public class FhirParserConfig {

@Bean
public FhirContext fhirContext() {
return FhirContext.forDstu3();
}

@Bean
public IParser fhirJsonParser() {
return FhirContext.forDstu3().newJsonParser();
}
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,11 @@
@Service
public class FhirParseService {

private final IParser jsonParser = prepareParser();
private final IParser jsonParser;

public FhirParseService(FhirContext fhirContext) {
this.jsonParser = prepareParser(fhirContext);
}

public <T extends IBaseResource> T parseResource(String body, Class<T> fhirClass) {
try {
Expand All @@ -20,14 +24,13 @@ public <T extends IBaseResource> T parseResource(String body, Class<T> fhirClass
}
}

private IParser prepareParser(FhirContext fhirContext) {
fhirContext.newJsonParser();
fhirContext.setParserErrorHandler(new StrictErrorHandler());
return fhirContext.newJsonParser();
}

public String encodeToJson(IBaseResource resource) {
return jsonParser.setPrettyPrint(true).encodeResourceToString(resource);
}

private IParser prepareParser() {
FhirContext ctx = FhirContext.forDstu3();
ctx.newJsonParser();
ctx.setParserErrorHandler(new StrictErrorHandler());
return ctx.newJsonParser();
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
import java.util.List;
import java.util.stream.Collectors;

import ca.uhn.fhir.context.FhirContext;
import org.apache.commons.io.FilenameUtils;
import org.hl7.fhir.dstu3.model.Bundle;
import org.hl7.fhir.dstu3.model.Identifier;
Expand Down Expand Up @@ -116,7 +117,7 @@ private List<InputFile> getFiles() throws UnreadableJsonFileException, NoJsonFil
final String mapJsonToXml(String jsonAsStringInput) {
String hl7TranslatedResponse;
try {
final Bundle bundle = new FhirParseService().parseResource(jsonAsStringInput, Bundle.class);
final Bundle bundle = new FhirParseService(FhirContext.forDstu3()).parseResource(jsonAsStringInput, Bundle.class);

messageContext.initialize(bundle);

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package uk.nhs.adaptors.gp2gp.common.configuration;

import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;

import static org.junit.jupiter.api.Assertions.assertNotNull;

class FhirParserConfigTest {

private FhirParserConfig fhirParserConfig;

@BeforeEach
void setUp() {
fhirParserConfig = new FhirParserConfig();
}

@Test
void shouldCreateValidConfig() {
assertNotNull(fhirParserConfig.fhirContext());
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -61,4 +61,5 @@ private static CodeableConcept getCodeableConcept() {
details.setCoding(List.of(codeableConceptCoding));
return details;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@

import java.io.IOException;

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

agentDirectoryMapper = new AgentDirectoryMapper(messageContext, agentPersonMapper);
fhirParseService = new FhirParseService();
fhirParseService = new FhirParseService(FhirContext.forDstu3());
}

private Answer<String> answerWithObjectId() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
import java.util.Map;
import java.util.Optional;

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

String jsonInput = ResourceTestFileUtils.getFileContent(INPUT_BUNDLE);
inputBundle = new FhirParseService().parseResource(jsonInput, Bundle.class);
inputBundle = new FhirParseService(FhirContext.forDstu3()).parseResource(jsonInput, Bundle.class);
}

@Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import java.io.IOException;
import java.util.stream.Stream;

import ca.uhn.fhir.context.FhirContext;
import org.hl7.fhir.dstu3.model.Bundle;
import org.hl7.fhir.dstu3.model.Organization;
import org.hl7.fhir.dstu3.model.Practitioner;
Expand Down Expand Up @@ -52,7 +53,7 @@ public void setUp() {
when(randomIdGeneratorService.createNewId()).thenReturn(TEST_ID);
messageContext = new MessageContext(randomIdGeneratorService);
agentPersonMapper = new AgentPersonMapper(messageContext);
fhirParseService = new FhirParseService();
fhirParseService = new FhirParseService(FhirContext.forDstu3());
}

@ParameterizedTest
Expand Down Expand Up @@ -149,12 +150,12 @@ private static Stream<Arguments> readPractitionerRoleTests() {

private static Practitioner getPractitionerResource() throws IOException {
String jsonPractitioner = ResourceTestFileUtils.getFileContent(PRACTITIONER);
return new FhirParseService().parseResource(jsonPractitioner, Practitioner.class);
return new FhirParseService(FhirContext.forDstu3()).parseResource(jsonPractitioner, Practitioner.class);
}

private static Organization getOrganizationResource() throws IOException {
String jsonOrganization = ResourceTestFileUtils.getFileContent(ORGANIZATION);
return new FhirParseService().parseResource(jsonOrganization, Organization.class);
return new FhirParseService(FhirContext.forDstu3()).parseResource(jsonOrganization, Organization.class);
}

@AfterEach
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
import java.util.Optional;
import java.util.stream.Stream;

import ca.uhn.fhir.context.FhirContext;
import org.hl7.fhir.dstu3.model.AllergyIntolerance;
import org.hl7.fhir.dstu3.model.Bundle;
import org.hl7.fhir.dstu3.model.CodeableConcept;
Expand Down Expand Up @@ -198,7 +199,7 @@ public void setUp() throws IOException {
.thenReturn(Optional.empty());

var bundleInput = ResourceTestFileUtils.getFileContent(INPUT_JSON_BUNDLE);
Bundle bundle = new FhirParseService().parseResource(bundleInput, Bundle.class);
Bundle bundle = new FhirParseService(FhirContext.forDstu3()).parseResource(bundleInput, Bundle.class);
messageContext = new MessageContext(randomIdGeneratorService);
messageContext.initialize(bundle);
List.of(ResourceType.Patient, ResourceType.Device)
Expand Down Expand Up @@ -265,7 +266,7 @@ public void When_ConfidentialityServiceReturnsEmptyOptional_Expect_MessageDoesNo

private static AllergyIntolerance parseAllergyIntoleranceFromJsonFile(String filepath) {
final var jsonInput = ResourceTestFileUtils.getFileContent(filepath);
return new FhirParseService().parseResource(jsonInput, AllergyIntolerance.class);
return new FhirParseService(FhirContext.forDstu3()).parseResource(jsonInput, AllergyIntolerance.class);
}

}
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package uk.nhs.adaptors.gp2gp.ehr.mapper;

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

@Mock
private RandomIdGeneratorService randomIdGeneratorService;
Expand Down Expand Up @@ -92,7 +94,7 @@ public void When_MappingEmptyObservation_Expect_CompoundStatementXmlReturned() t
var jsonInput = ResourceTestFileUtils.getFileContent(BLOOD_PRESSURE_FILE_LOCATION + INPUT_EMPTY_OBSERVATION);
var expectedOutput = ResourceTestFileUtils.getFileContent(BLOOD_PRESSURE_FILE_LOCATION + EXPECTED_EMPTY_OBSERVATION);

Observation observation = new FhirParseService().parseResource(jsonInput, Observation.class);
Observation observation = new FhirParseService(fhirCtx).parseResource(jsonInput, Observation.class);
var outputMessage = bloodPressureMapper.mapBloodPressure(observation, false);

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

Observation observation = new FhirParseService().parseResource(jsonInput, Observation.class);
Observation observation = new FhirParseService(fhirCtx).parseResource(jsonInput, Observation.class);
var outputMessage = bloodPressureMapper.mapBloodPressure(observation, true);

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

Observation observation = new FhirParseService().parseResource(jsonInput, Observation.class);
Observation observation = new FhirParseService(fhirCtx).parseResource(jsonInput, Observation.class);
var outputMessage = bloodPressureMapper.mapBloodPressure(observation, false);

assertThat(outputMessage)
Expand Down Expand Up @@ -157,7 +159,7 @@ public void When_MappingBloodPressureWithCodeableConcepts_Expect_CompoundStateme
messageContext, randomIdGeneratorService, new StructuredObservationValueMapper(),
codeableConceptCdMapper, new ParticipantMapper());

Observation observation = new FhirParseService().parseResource(jsonInput, Observation.class);
Observation observation = new FhirParseService(fhirCtx).parseResource(jsonInput, Observation.class);
var outputMessage = bloodPressureMapper.mapBloodPressure(observation, true);

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

Observation observation = new FhirParseService().parseResource(jsonInput, Observation.class);
Observation observation = new FhirParseService(fhirCtx).parseResource(jsonInput, Observation.class);

assertThrows(EhrMapperException.class, ()
-> bloodPressureMapper.mapBloodPressure(observation, true));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import java.io.IOException;
import java.util.stream.Stream;

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

@BeforeEach
public void setUp() {
fhirParseService = new FhirParseService();
fhirParseService = new FhirParseService(FhirContext.forDstu3());
codeableConceptCdMapper = new CodeableConceptCdMapper();
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package uk.nhs.adaptors.gp2gp.ehr.mapper;

import ca.uhn.fhir.context.FhirContext;
import org.hl7.fhir.dstu3.model.Bundle;
import org.hl7.fhir.dstu3.model.CodeableConcept;
import org.hl7.fhir.dstu3.model.Condition;
Expand Down Expand Up @@ -131,7 +132,7 @@ class ConditionLinkSetMapperTest {
@BeforeEach
void setUp() throws IOException {
var bundleInput = ResourceTestFileUtils.getFileContent(TEST_FILES_DIRECTORY + INPUT_JSON_BUNDLE);
final Bundle bundle = new FhirParseService().parseResource(bundleInput, Bundle.class);
final Bundle bundle = new FhirParseService(FhirContext.forDstu3()).parseResource(bundleInput, Bundle.class);
inputBundle = new InputBundle(bundle);

lenient().when(codeableConceptCdMapper.mapCodeableConceptToCd(any(CodeableConcept.class)))
Expand Down
Loading
Loading