Skip to content

Commit 2d75381

Browse files
author
Adrian Clay
authored
Simplify the SpecimenMapper observation filtering (#1008)
Now that the filtering of which Observations belong to which Specimen happens within the DiagnosticReportMapper as of commit 7907883 there is no longer a need to perform the same filtering within the SpecimenMapper By moving all grouping, fakery generation and filtering into the DiagnosticReportMapper this should simplify the complexity of this part of the adaptor.
1 parent 364e902 commit 2d75381

File tree

4 files changed

+7
-122
lines changed

4 files changed

+7
-122
lines changed

service/src/main/java/uk/nhs/adaptors/gp2gp/ehr/mapper/diagnosticreport/SpecimenMapper.java

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

3-
import static uk.nhs.adaptors.gp2gp.ehr.mapper.diagnosticreport.DiagnosticReportMapper.DUMMY_OBSERVATION_ID_PREFIX;
4-
import static uk.nhs.adaptors.gp2gp.ehr.mapper.diagnosticreport.DiagnosticReportMapper.DUMMY_SPECIMEN_ID_PREFIX;
53
import static uk.nhs.adaptors.gp2gp.ehr.mapper.diagnosticreport.ObservationMapper.NARRATIVE_STATEMENT_TEMPLATE;
64
import static uk.nhs.adaptors.gp2gp.ehr.utils.TextUtils.newLine;
75
import static uk.nhs.adaptors.gp2gp.ehr.utils.TextUtils.withSpace;
@@ -64,9 +62,11 @@ public class SpecimenMapper {
6462
private final RandomIdGeneratorService randomIdGeneratorService;
6563
private final ConfidentialityService confidentialityService;
6664

67-
public String mapSpecimenToCompoundStatement(Specimen specimen, List<Observation> observations, DiagnosticReport diagnosticReport) {
65+
public String mapSpecimenToCompoundStatement(
66+
Specimen specimen, List<Observation> observationsAssociatedWithSpecimen, DiagnosticReport diagnosticReport
67+
) {
6868
String availabilityTimeElement = StatementTimeMappingUtils.prepareAvailabilityTime(diagnosticReport.getIssuedElement());
69-
String mappedObservations = mapObservationsAssociatedWithSpecimen(specimen, observations);
69+
String mappedObservations = mapObservationsToCompoundStatements(observationsAssociatedWithSpecimen);
7070

7171
var specimenCompoundStatementTemplateParameters = SpecimenCompoundStatementTemplateParameters.builder()
7272
.compoundStatementId(messageContext.getIdMapper().getOrNew(ResourceType.Specimen, specimen.getIdElement()))
@@ -135,28 +135,12 @@ private Optional<String> buildSpecimenMaterialType(Specimen specimen) {
135135
return Optional.empty();
136136
}
137137

138-
private String mapObservationsAssociatedWithSpecimen(Specimen specimen, List<Observation> observations) {
139-
List<Observation> observationsAssociatedWithSpecimen;
140-
141-
if (dummySpecimenOrObservationExists(specimen, observations)) {
142-
observationsAssociatedWithSpecimen = observations;
143-
} else {
144-
observationsAssociatedWithSpecimen = observations.stream()
145-
.filter(Observation::hasSpecimen)
146-
.filter(observation -> observation.getSpecimen().getReference().equals(specimen.getId()))
147-
.collect(Collectors.toList());
148-
}
149-
150-
return observationsAssociatedWithSpecimen.stream()
138+
private String mapObservationsToCompoundStatements(List<Observation> observations) {
139+
return observations.stream()
151140
.map(observationMapper::mapObservationToCompoundStatement)
152141
.collect(Collectors.joining());
153142
}
154143

155-
private boolean dummySpecimenOrObservationExists(Specimen specimen, List<Observation> observations) {
156-
return specimen.getIdElement().getIdPart().contains(DUMMY_SPECIMEN_ID_PREFIX)
157-
|| (!observations.isEmpty() && observations.getFirst().getIdElement().getIdPart().contains(DUMMY_OBSERVATION_ID_PREFIX));
158-
}
159-
160144
private Optional<String> buildSpecimenNarrativeStatement(Specimen specimen, String availabilityTimeElement,
161145
Boolean emptyMappedObservations) {
162146
SpecimenNarrativeStatementCommentBuilder specimenNarrativeStatementCommentBuilder = new SpecimenNarrativeStatementCommentBuilder();

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

Lines changed: 1 addition & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,6 @@ class SpecimenMapperTest {
5050
private static final String DIAGNOSTIC_REPORT_DATE = "2020-10-12T13:33:44Z";
5151
private static final String FHIR_INPUT_BUNDLE = BASE_DIRECTORY + "fhir_bundle.json";
5252
private static final String INPUT_OBSERVATION_RELATED_TO_SPECIMEN = "input-observation-related-to-specimen.json";
53-
private static final String INPUT_OBSERVATION_NOT_RELATED_TO_SPECIMEN = "input-observation-not-related-to-specimen.json";
5453
private static final String TEST_ID = "5E496953-065B-41F2-9577-BE8F2FBD0757";
5554
private static final String ID_FROM_ID_MAPPER = "some-id";
5655
private static final DiagnosticReport DIAGNOSTIC_REPORT = new DiagnosticReport().setIssuedElement(
@@ -87,8 +86,7 @@ void setUp() {
8786
when(randomIdGeneratorService.createNewId()).thenReturn(TEST_ID);
8887

8988
observations = List.of(
90-
getObservationResourceFromJson(INPUT_OBSERVATION_RELATED_TO_SPECIMEN),
91-
getObservationResourceFromJson(INPUT_OBSERVATION_NOT_RELATED_TO_SPECIMEN)
89+
getObservationResourceFromJson(INPUT_OBSERVATION_RELATED_TO_SPECIMEN)
9290
);
9391
}
9492

@@ -126,22 +124,6 @@ public void When_MappingDefaultSpecimenWithDefaultObservation_Expect_DefaultXmlO
126124
assertThat(actualXml).isEqualTo(expectedXml);
127125
}
128126

129-
@Test
130-
void When_MappingDefaultSpecimenWithObservation_Expect_DefaultSpecimenAndObservationXmlOutput() {
131-
final Specimen specimen = getDefaultSpecimen();
132-
final String expectedXml = ResourceTestFileUtils.getFileContent(
133-
SPECIMEN_TEST_FILES_DIRECTORY + "expected_output_default_specimen_with_observation.xml");
134-
135-
when(idMapper.getOrNew(any(ResourceType.class), any(IdType.class)))
136-
.thenReturn(ID_FROM_ID_MAPPER);
137-
when(observationMapper.mapObservationToCompoundStatement(any())).thenAnswer(mockObservationMapping());
138-
139-
final String actualXml = specimenMapper.mapSpecimenToCompoundStatement(
140-
specimen, observations, DIAGNOSTIC_REPORT);
141-
142-
assertThat(actualXml).isEqualToIgnoringWhitespace(expectedXml);
143-
}
144-
145127
@Test
146128
void When_MappingDefaultSpecimenWithNoMappableObservations_Expect_EmptySpecimenXmlOutput() {
147129
final Specimen specimen = getDefaultSpecimen();

service/src/test/resources/ehr/mapper/diagnosticreport/observation/input-observation-not-related-to-specimen.json

Lines changed: 0 additions & 45 deletions
This file was deleted.

service/src/test/resources/ehr/mapper/diagnosticreport/specimen/expected_output_default_specimen_with_observation.xml

Lines changed: 0 additions & 36 deletions
This file was deleted.

0 commit comments

Comments
 (0)