Skip to content

Commit

Permalink
[NIAD-3144] Checkstyle violations fixed, enhance SpecimenMapperTest.java
Browse files Browse the repository at this point in the history
  • Loading branch information
martin-nhs committed Aug 19, 2024
1 parent 8053607 commit 2727494
Show file tree
Hide file tree
Showing 5 changed files with 85 additions and 81 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ public class DiagnosticReportMapper {
private final ParticipantMapper participantMapper;
private final RandomIdGeneratorService randomIdGeneratorService;
private final ConfidentialityService confidentialityService;

public String mapDiagnosticReportToCompoundStatement(DiagnosticReport diagnosticReport) {
List<Specimen> specimens = fetchSpecimens(diagnosticReport);
List<Observation> observations = fetchObservations(diagnosticReport);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -189,7 +189,8 @@ public void setUp() {
);
}

private @NotNull SpecimenMapper getSpecimenMapper(StructuredObservationValueMapper structuredObservationValueMapper, ParticipantMapper participantMapper) {
private @NotNull SpecimenMapper getSpecimenMapper(StructuredObservationValueMapper structuredObservationValueMapper,
ParticipantMapper participantMapper) {
MultiStatementObservationHolderFactory multiStatementObservationHolderFactory =
new MultiStatementObservationHolderFactory(messageContext, randomIdGeneratorService);
ObservationMapper specimenObservationMapper = new ObservationMapper(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,8 @@ public void setUp() throws IOException {

when(randomIdGeneratorService.createNewId()).thenReturn(TEST_ID);

mapper = new DiagnosticReportMapper(messageContext, specimenMapper, new ParticipantMapper(), randomIdGeneratorService, confidentialityService);
mapper = new DiagnosticReportMapper(
messageContext, specimenMapper, new ParticipantMapper(), randomIdGeneratorService, confidentialityService);
}

@AfterEach
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
import static org.mockito.Mockito.lenient;
import static org.mockito.Mockito.when;

import java.io.IOException;
import java.util.Collections;
import java.util.List;
import java.util.stream.Stream;
Expand All @@ -24,10 +23,14 @@
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.Arguments;
import org.junit.jupiter.params.provider.MethodSource;
import org.mockito.ArgumentCaptor;
import org.mockito.Captor;
import org.mockito.Mock;
import org.mockito.junit.jupiter.MockitoExtension;
import org.mockito.stubbing.Answer;

import uk.nhs.adaptors.gp2gp.TestUtility;
import uk.nhs.adaptors.gp2gp.common.service.ConfidentialityService;
import uk.nhs.adaptors.gp2gp.common.service.FhirParseService;
import uk.nhs.adaptors.gp2gp.common.service.RandomIdGeneratorService;
import uk.nhs.adaptors.gp2gp.ehr.mapper.AgentDirectory;
Expand All @@ -37,37 +40,39 @@
import uk.nhs.adaptors.gp2gp.utils.ResourceTestFileUtils;

@ExtendWith(MockitoExtension.class)
public class SpecimenMapperTest {

private static final String DIAGNOSTIC_REPORT_TEST_FILE_DIRECTORY = "/ehr/mapper/diagnosticreport/";
class SpecimenMapperTest {
private static final String BASE_DIRECTORY = "/ehr/mapper/diagnosticreport/";
private static final String SPECIMEN_TEST_FILES_DIRECTORY = BASE_DIRECTORY + "/specimen/";
private static final String OBSERVATION_TEST_FILES_DIRECTORY = BASE_DIRECTORY + "/observation/";
private static final String DIAGNOSTIC_REPORT_DATE = "2020-10-12T13:33:44Z";
private static final String FHIR_INPUT_BUNDLE = DIAGNOSTIC_REPORT_TEST_FILE_DIRECTORY + "fhir_bundle.json";

private static final String FHIR_INPUT_BUNDLE = BASE_DIRECTORY + "fhir_bundle.json";
private static final String INPUT_OBSERVATION_RELATED_TO_SPECIMEN = "input-observation-related-to-specimen.json";
private static final String INPUT_OBSERVATION_NOT_RELATED_TO_SPECIMEN = "input-observation-not-related-to-specimen.json";

private static final String TEST_ID = "5E496953-065B-41F2-9577-BE8F2FBD0757";
private static final DiagnosticReport DIAGNOSTIC_REPORT = new DiagnosticReport().setIssuedElement(
new InstantType(DIAGNOSTIC_REPORT_DATE)
);

private SpecimenMapper specimenMapper;
private List<Observation> observations;

@Mock
private MessageContext messageContext;

@Mock
private IdMapper idMapper;

@Mock
private AgentDirectory agentDirectory;

@Mock
private ObservationMapper observationMapper;

@Mock
private RandomIdGeneratorService randomIdGeneratorService;
@Mock
private ConfidentialityService confidentialityService;
@Captor
private ArgumentCaptor<Specimen> specimenArgumentCaptor;

@BeforeEach
public void setUp() throws IOException {
void setUp() {
var inputBundleString = ResourceTestFileUtils.getFileContent(FHIR_INPUT_BUNDLE);
var inputBundle = new FhirParseService().parseResource(inputBundleString, Bundle.class);
lenient().when(messageContext.getIdMapper()).thenReturn(idMapper);
Expand All @@ -79,97 +84,98 @@ public void setUp() throws IOException {
when(randomIdGeneratorService.createNewId()).thenReturn(TEST_ID);

observations = List.of(
parseObservation(INPUT_OBSERVATION_RELATED_TO_SPECIMEN),
parseObservation(INPUT_OBSERVATION_NOT_RELATED_TO_SPECIMEN)
getObservationResourceFromJson(INPUT_OBSERVATION_RELATED_TO_SPECIMEN),
getObservationResourceFromJson(INPUT_OBSERVATION_NOT_RELATED_TO_SPECIMEN)
);

specimenMapper = new SpecimenMapper(messageContext, observationMapper, randomIdGeneratorService);
}

@ParameterizedTest
@MethodSource("testData")
public void When_MappingSpecimen_Expect_XmlOutput(String inputPath, String expectedPath) throws IOException {
var input = ResourceTestFileUtils.getFileContent(DIAGNOSTIC_REPORT_TEST_FILE_DIRECTORY + "specimen/" + inputPath);
var specimen = new FhirParseService().parseResource(input, Specimen.class);
void When_MappingSpecimen_Expect_XmlOutput(String inputPath, String expectedPath) {
final Specimen specimen = getSpecimenResourceFromJson(inputPath);
final String expectedXml = ResourceTestFileUtils.getFileContent(SPECIMEN_TEST_FILES_DIRECTORY + expectedPath);

var diagnosticReport = new DiagnosticReport().setIssuedElement(new InstantType(DIAGNOSTIC_REPORT_DATE));
when(observationMapper.mapObservationToCompoundStatement(any(Observation.class)))
.thenAnswer(mockObservationMapping());

var expected = ResourceTestFileUtils.getFileContent(DIAGNOSTIC_REPORT_TEST_FILE_DIRECTORY + "specimen/" + expectedPath);
final String actualXml = specimenMapper.mapSpecimenToCompoundStatement(specimen, observations, DIAGNOSTIC_REPORT);

when(observationMapper.mapObservationToCompoundStatement(any())).thenAnswer(mockObservationMapping());

String outputMessage = specimenMapper.mapSpecimenToCompoundStatement(specimen, observations, diagnosticReport);

assertThat(outputMessage).isEqualTo(expected);
assertThat(actualXml).isEqualTo(expectedXml);
}

@Test
public void When_MappingDefaultSpecimenWithDefaultObservation_Expect_DefaultXmlOutput() throws IOException {
String defaultSpecimenJson = ResourceTestFileUtils.getFileContent(
DIAGNOSTIC_REPORT_TEST_FILE_DIRECTORY + "specimen/" + "input_default_specimen.json"
);
Specimen specimen = new FhirParseService().parseResource(defaultSpecimenJson, Specimen.class);
public void When_MappingDefaultSpecimenWithDefaultObservation_Expect_DefaultXmlOutput() {
final Specimen specimen = getDefaultSpecimen();
final Observation observation = getDefaultObservation();
final String expectedXml = ResourceTestFileUtils.getFileContent(
SPECIMEN_TEST_FILES_DIRECTORY + "expected_output_default_specimen_and_default_observation.xml");

String defaultObservationJson = ResourceTestFileUtils.getFileContent(
DIAGNOSTIC_REPORT_TEST_FILE_DIRECTORY + "observation/" + "input_default_observation.json"
);
Observation observation = new FhirParseService().parseResource(defaultObservationJson, Observation.class);
when(idMapper.getOrNew(any(ResourceType.class), any(IdType.class)))
.thenReturn("some-id");

String expectedXmlOutput = ResourceTestFileUtils.getFileContent(
DIAGNOSTIC_REPORT_TEST_FILE_DIRECTORY + "specimen/" + "expected_output_default_specimen_and_default_observation.xml"
);
var diagnosticReport = new DiagnosticReport().setIssuedElement(new InstantType(DIAGNOSTIC_REPORT_DATE));

when(idMapper.getOrNew(any(ResourceType.class), any(IdType.class))).thenReturn("some-id");
when(observationMapper.mapObservationToCompoundStatement(observation)).thenAnswer(mockObservationMapping());
when(observationMapper.mapObservationToCompoundStatement(observation))
.thenAnswer(mockObservationMapping());

String compoundStatementXml = specimenMapper.mapSpecimenToCompoundStatement(
specimen, Collections.singletonList(observation), diagnosticReport
final String actualXml = specimenMapper.mapSpecimenToCompoundStatement(
specimen, Collections.singletonList(observation), DIAGNOSTIC_REPORT
);

assertThat(compoundStatementXml).isEqualTo(expectedXmlOutput);
assertThat(actualXml).isEqualTo(expectedXml);
}

@Test
public void When_MappingDefaultSpecimenWithObservations_Expect_DefaultSpecimenAndObservationsXmlOutput() throws IOException {
String defaultSpecimenJson = ResourceTestFileUtils.getFileContent(
DIAGNOSTIC_REPORT_TEST_FILE_DIRECTORY + "specimen/" + "input_default_specimen.json"
);
Specimen specimen = new FhirParseService().parseResource(defaultSpecimenJson, Specimen.class);
void When_MappingDefaultSpecimenWithObservations_Expect_DefaultSpecimenAndObservationsXmlOutput() {
final Specimen specimen = getDefaultSpecimen();
final String expectedXml = ResourceTestFileUtils.getFileContent(
SPECIMEN_TEST_FILES_DIRECTORY + "expected_output_default_specimen_and_default_observation.xml");

String expectedXmlOutput = ResourceTestFileUtils.getFileContent(
DIAGNOSTIC_REPORT_TEST_FILE_DIRECTORY + "specimen/" + "expected_output_default_specimen_with_observations.xml"
);
var diagnosticReport = new DiagnosticReport().setIssuedElement(new InstantType(DIAGNOSTIC_REPORT_DATE));
when(idMapper.getOrNew(any(ResourceType.class), any(IdType.class)))
.thenReturn("some-id");

when(idMapper.getOrNew(any(ResourceType.class), any(IdType.class))).thenReturn("some-id");
when(observationMapper.mapObservationToCompoundStatement(any())).thenAnswer(mockObservationMapping());
when(observationMapper.mapObservationToCompoundStatement(any(Observation.class)))
.thenAnswer(mockObservationMapping());

String compoundStatementXml = specimenMapper.mapSpecimenToCompoundStatement(
specimen, observations, diagnosticReport
);
final String actualXml = specimenMapper.mapSpecimenToCompoundStatement(
specimen, observations, DIAGNOSTIC_REPORT);

assertThat(compoundStatementXml).isEqualTo(expectedXmlOutput);
assertThat(actualXml).isEqualTo(expectedXml);
}

@Test
public void When_MappingDefaultSpecimenWithNoMappableObservations_Expect_EmptySpecimenXmlOutput() throws IOException {
String defaultSpecimenJson = ResourceTestFileUtils.getFileContent(
DIAGNOSTIC_REPORT_TEST_FILE_DIRECTORY + "specimen/" + "input_default_specimen.json"
void When_MappingDefaultSpecimenWithNoMappableObservations_Expect_EmptySpecimenXmlOutput() {
final Specimen specimen = getDefaultSpecimen();
final String expectedXmlOutput = ResourceTestFileUtils.getFileContent(
SPECIMEN_TEST_FILES_DIRECTORY + "expected_output_default_empty_specimen.xml"
);
Specimen specimen = new FhirParseService().parseResource(defaultSpecimenJson, Specimen.class);
String expectedXmlOutput = ResourceTestFileUtils.getFileContent(
DIAGNOSTIC_REPORT_TEST_FILE_DIRECTORY + "specimen/" + "expected_output_default_empty_specimen.xml"
);
var diagnosticReport = new DiagnosticReport().setIssuedElement(new InstantType(DIAGNOSTIC_REPORT_DATE));

when(idMapper.getOrNew(any(ResourceType.class), any(IdType.class))).thenReturn("some-id");
when(idMapper.getOrNew(any(ResourceType.class), any(IdType.class)))
.thenReturn("some-id");

String compoundStatementXml = specimenMapper.mapSpecimenToCompoundStatement(
specimen, Collections.emptyList(), diagnosticReport
final String actualXml = specimenMapper.mapSpecimenToCompoundStatement(
specimen, Collections.emptyList(), DIAGNOSTIC_REPORT
);

assertThat(compoundStatementXml).isEqualTo(expectedXmlOutput);
assertThat(actualXml).isEqualTo(expectedXmlOutput);
}

private Specimen getDefaultSpecimen() {
return getSpecimenResourceFromJson("input_default_specimen.json");
}

private Observation getDefaultObservation() {
return getObservationResourceFromJson("input_default_observation.json");
}

private Observation getObservationResourceFromJson(String filename) {
final String filePath = OBSERVATION_TEST_FILES_DIRECTORY + filename;
return TestUtility.parseResourceFromJsonFile(filePath, Observation.class);
}

private Specimen getSpecimenResourceFromJson(String filename) {
final String filePath = SPECIMEN_TEST_FILES_DIRECTORY + filename;
return TestUtility.parseResourceFromJsonFile(filePath, Specimen.class);
}

private static Stream<Arguments> testData() {
Expand Down Expand Up @@ -214,10 +220,4 @@ private Answer<String> mockObservationMapping() {
return String.format("<!-- Mapped Observation with id: %s -->", observation.getId());
};
}

private Observation parseObservation(String path) throws IOException {
String fileContent = ResourceTestFileUtils.getFileContent(DIAGNOSTIC_REPORT_TEST_FILE_DIRECTORY + "observation/" + path);

return new FhirParseService().parseResource(fileContent, Observation.class);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -146,7 +146,8 @@ public void setUp() {
messageContext = new MessageContext(randomIdGeneratorService);

CodeableConceptCdMapper codeableConceptCdMapper = new CodeableConceptCdMapper();
final EncounterComponentsMapper encounterComponentsMapper = getEncounterComponentsMapper(randomIdGeneratorService, codeableConceptCdMapper);
final EncounterComponentsMapper encounterComponentsMapper =
getEncounterComponentsMapper(randomIdGeneratorService, codeableConceptCdMapper);

AgentPersonMapper agentPersonMapper
= new AgentPersonMapper(messageContext);
Expand All @@ -163,7 +164,8 @@ public void setUp() {
.thenReturn(Optional.empty());
}

private @NotNull EncounterComponentsMapper getEncounterComponentsMapper(RandomIdGeneratorService randomIdGeneratorService, CodeableConceptCdMapper codeableConceptCdMapper) {
private @NotNull EncounterComponentsMapper getEncounterComponentsMapper(RandomIdGeneratorService randomIdGeneratorService,
CodeableConceptCdMapper codeableConceptCdMapper) {
StructuredObservationValueMapper structuredObservationValueMapper = new StructuredObservationValueMapper();
ParticipantMapper participantMapper = new ParticipantMapper();
MultiStatementObservationHolderFactory multiStatementObservationHolderFactory =
Expand Down

0 comments on commit 2727494

Please sign in to comment.