Skip to content
Draft
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

Large diffs are not rendered by default.

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

import lombok.RequiredArgsConstructor;
import org.apache.commons.lang3.StringUtils;
import org.hl7.fhir.dstu3.model.AllergyIntolerance;
import org.hl7.fhir.dstu3.model.Annotation;
import org.hl7.fhir.dstu3.model.Condition;
import org.hl7.fhir.dstu3.model.RelatedPerson;
import org.hl7.fhir.dstu3.model.ResourceType;
import org.hl7.fhir.instance.model.api.IIdType;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import uk.nhs.adaptors.gp2gp.ehr.utils.DateFormatUtil;

import java.util.List;
import java.util.Optional;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.stream.Collectors;
import java.util.stream.Stream;

import static uk.nhs.adaptors.gp2gp.ehr.mapper.AllergyStructureExtractor.extractReaction;
import static uk.nhs.adaptors.gp2gp.ehr.utils.DateFormatUtil.toTextFormat;
import static uk.nhs.adaptors.gp2gp.ehr.utils.ExtensionMappingUtils.filterExtensionByUrl;

@RequiredArgsConstructor(onConstructor = @__(@Autowired))
@Component
public class PertinentInformationAllergyMapper {
private static final String ALLERGY_INTOLERANCE_END_URL =
"https://fhir.nhs.uk/STU3/StructureDefinition/Extension-CareConnect-GPC-AllergyIntoleranceEnd-1";
private static final String END_DATE = "End Date: ";
private static final String STATUS = "Status: ";
private static final String TYPE = "Type: ";
private static final String CRITICALITY = "Criticality: ";
private static final String PATIENT_ASSERTER = "Asserted By Patient";
private static final String RELATED_PERSON_ASSERTER = "Asserted By: Related Person";
private static final String LAST_OCCURRENCE = "Last Occurred: ";
private static final String PATIENT_RECORDER = "Recorded By Patient";
private static final String COMMA = ", ";


private final MessageContext messageContext;

public String buildPertinentInformation(AllergyIntolerance allergyIntolerance) {
return retrievePertinentInformation(allergyIntolerance)
.stream()
.filter(StringUtils::isNotEmpty)
.collect(Collectors.joining(StringUtils.SPACE));
}

private List<String> retrievePertinentInformation(AllergyIntolerance allergyIntolerance) {
return List.of(
buildExtensionReasonEndPertinentInformation(allergyIntolerance),
buildClinicalStatusPertinentInformation(allergyIntolerance),
buildTypePertinentInformation(allergyIntolerance),
buildCriticalityPertinentInformation(allergyIntolerance),
buildAsserterPertinentInformation(allergyIntolerance),
buildLastOccurrencePertinentInformation(allergyIntolerance),
buildRecorderPertinentInformation(allergyIntolerance),
buildReactionPertinentInformation(allergyIntolerance),
buildNotePertinentInformation(allergyIntolerance),
buildEndDatePertinentInformation(allergyIntolerance)
);
}

private String buildExtensionReasonEndPertinentInformation(AllergyIntolerance allergyIntolerance) {
return filterExtensionByUrl(allergyIntolerance, ALLERGY_INTOLERANCE_END_URL)
.map(AllergyStructureExtractor::extractReasonEnd)
.orElse(StringUtils.EMPTY);
}

private String buildClinicalStatusPertinentInformation(AllergyIntolerance allergyIntolerance) {
return allergyIntolerance.hasClinicalStatus()

Check warning on line 72 in service/src/main/java/uk/nhs/adaptors/gp2gp/ehr/mapper/PertinentInformationAllergyMapper.java

View workflow job for this annotation

GitHub Actions / pitest

A change can be made to line 72 without causing a test to fail

removed conditional - replaced equality check with true (covered by 40 tests RemoveConditionalMutator_EQUAL_IF)
? buildClinicalStatusNotePertinentInformation(allergyIntolerance)
: StringUtils.EMPTY;
}

private String buildClinicalStatusNotePertinentInformation(AllergyIntolerance allergyIntolerance) {
return allergyIntolerance.hasNote()

Check warning on line 78 in service/src/main/java/uk/nhs/adaptors/gp2gp/ehr/mapper/PertinentInformationAllergyMapper.java

View workflow job for this annotation

GitHub Actions / pitest

2 different changes can be made to line 78 without causing a test to fail

removed conditional - replaced equality check with false (covered by 40 tests RemoveConditionalMutator_EQUAL_ELSE) removed conditional - replaced equality check with true (covered by 40 tests RemoveConditionalMutator_EQUAL_IF)
? STATUS + StringUtils.capitalize(allergyIntolerance.getClinicalStatus().toCode())
: STATUS + allergyIntolerance.getClinicalStatus().getDisplay();
}

private String buildTypePertinentInformation(AllergyIntolerance allergyIntolerance) {
return allergyIntolerance.hasType()
? TYPE + allergyIntolerance.getType().getDisplay()
: StringUtils.EMPTY;
}

private String buildCriticalityPertinentInformation(AllergyIntolerance allergyIntolerance) {
return allergyIntolerance.hasCriticality()
? CRITICALITY + allergyIntolerance.getCriticality().getDisplay()
: StringUtils.EMPTY;
}

private String buildAsserterPertinentInformation(AllergyIntolerance allergyIntolerance) {
if (allergyIntolerance.hasAsserter()) {
IIdType reference = allergyIntolerance.getAsserter().getReferenceElement();
if (reference.getResourceType().equals(ResourceType.Patient.name())) {
return PATIENT_ASSERTER;
} else if (reference.getResourceType().equals(ResourceType.RelatedPerson.name())) {
return messageContext
.getInputBundleHolder()
.getResource(reference)
.map(RelatedPerson.class::cast)
.filter(RelatedPerson::hasName)

Check warning on line 105 in service/src/main/java/uk/nhs/adaptors/gp2gp/ehr/mapper/PertinentInformationAllergyMapper.java

View workflow job for this annotation

GitHub Actions / pitest

A change can be made to line 105 without causing a test to fail

removed call to filter (covered by 4 tests RemoveFilterMutator)
.map(RelatedPerson::getName)
.filter(names -> !names.isEmpty())

Check warning on line 107 in service/src/main/java/uk/nhs/adaptors/gp2gp/ehr/mapper/PertinentInformationAllergyMapper.java

View workflow job for this annotation

GitHub Actions / pitest

A change can be made to line 107 without causing a test to fail

removed call to filter (covered by 4 tests RemoveFilterMutator)
.map(List::getFirst)
.map(name -> Optional.ofNullable(name.getText())
.filter(StringUtils::isNotBlank)

Check warning on line 110 in service/src/main/java/uk/nhs/adaptors/gp2gp/ehr/mapper/PertinentInformationAllergyMapper.java

View workflow job for this annotation

GitHub Actions / pitest

A change can be made to a lambda on line 110 without causing a test to fail

removed call to filter in 2nd lambda in buildAsserterPertinentInformation (covered by 2 tests RemoveFilterMutator)
.orElseGet(name::getNameAsSingleString))
.filter(StringUtils::isNotBlank)

Check warning on line 112 in service/src/main/java/uk/nhs/adaptors/gp2gp/ehr/mapper/PertinentInformationAllergyMapper.java

View workflow job for this annotation

GitHub Actions / pitest

A change can be made to line 112 without causing a test to fail

removed call to filter (covered by 4 tests RemoveFilterMutator)
.map(name -> RELATED_PERSON_ASSERTER + StringUtils.SPACE + name)
.orElse(RELATED_PERSON_ASSERTER);
}
}
return StringUtils.EMPTY;
}

private String buildLastOccurrencePertinentInformation(AllergyIntolerance allergyIntolerance) {
if (allergyIntolerance.hasLastOccurrence()) {
return LAST_OCCURRENCE + toTextFormat(allergyIntolerance.getLastOccurrenceElement());
}
return StringUtils.EMPTY;
}

private String buildRecorderPertinentInformation(AllergyIntolerance allergyIntolerance) {
if (allergyIntolerance.hasRecorder()) {
IIdType reference = allergyIntolerance.getRecorder().getReferenceElement();
if (reference.getResourceType().equals(ResourceType.Patient.name())) {
return PATIENT_RECORDER;
}
}
return StringUtils.EMPTY;
}

private String buildReactionPertinentInformation(AllergyIntolerance allergyIntolerance) {
AtomicInteger reactionCount = new AtomicInteger(1);
if (allergyIntolerance.hasReaction()) {

Check warning on line 139 in service/src/main/java/uk/nhs/adaptors/gp2gp/ehr/mapper/PertinentInformationAllergyMapper.java

View workflow job for this annotation

GitHub Actions / pitest

A change can be made to line 139 without causing a test to fail

removed conditional - replaced equality check with true (covered by 40 tests RemoveConditionalMutator_EQUAL_IF)
return allergyIntolerance.getReaction()
.stream()
.map(reaction -> extractReaction(reaction, reactionCount))
.collect(Collectors.joining(COMMA));
}
return StringUtils.EMPTY;
}

private String buildNotePertinentInformation(AllergyIntolerance allergyIntolerance) {
return Stream.concat(
messageContext.getInputBundleHolder().getRelatedConditions(allergyIntolerance.getId())
.stream()
.map(Condition::getNote)
.flatMap(List::stream),
allergyIntolerance.hasNote() ? allergyIntolerance.getNote().stream() : Stream.empty()

Check warning on line 154 in service/src/main/java/uk/nhs/adaptors/gp2gp/ehr/mapper/PertinentInformationAllergyMapper.java

View workflow job for this annotation

GitHub Actions / pitest

A change can be made to line 154 without causing a test to fail

removed conditional - replaced equality check with true (covered by 40 tests RemoveConditionalMutator_EQUAL_IF)
)
.map(Annotation::getText)
.collect(Collectors.joining(StringUtils.SPACE));
}

private String buildEndDatePertinentInformation(AllergyIntolerance allergyIntolerance) {
return filterExtensionByUrl(allergyIntolerance, ALLERGY_INTOLERANCE_END_URL)
.map(extension -> AllergyStructureExtractor.extractEndDate(extension, DateFormatUtil::toTextFormat))
.filter(StringUtils::isNotBlank)
.map(endDate -> END_DATE + endDate)
.orElse(StringUtils.EMPTY);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -113,8 +113,6 @@ private static Stream<Arguments> resourceFileParamsWithEnvironmentCategoryAndCon
void setUp() {
when(randomIdGeneratorService.createNewId()).thenReturn(TEST_ID);
when(randomIdGeneratorService.createNewOrUseExistingUUID(anyString())).thenReturn(TEST_ID);
when(confidentialityService.generateConfidentialityCode(any()))
.thenReturn(Optional.empty());

var bundleInput = ResourceTestFileUtils.getFileContent(INPUT_JSON_BUNDLE);
Bundle bundle = new FhirParseService().parseResource(bundleInput, Bundle.class);
Expand All @@ -128,7 +126,8 @@ void setUp() {
messageContext,
codeableConceptCdMapper,
new ParticipantMapper(),
confidentialityService);
confidentialityService,
new PertinentInformationAllergyMapper(messageContext));
}

@AfterEach
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -145,7 +145,9 @@ public void setUp() {

EncounterComponentsMapper encounterComponentsMapper = new EncounterComponentsMapper(
messageContext,
new AllergyStructureMapper(messageContext, codeableConceptCdMapper, participantMapper, confidentialityService),
new AllergyStructureMapper(messageContext, codeableConceptCdMapper, participantMapper,
confidentialityService,
new PertinentInformationAllergyMapper(messageContext)),
new BloodPressureMapper(messageContext, randomIdGeneratorService,
new StructuredObservationValueMapper(), codeableConceptCdMapper,
new ParticipantMapper(), confidentialityService),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -147,7 +147,8 @@ public void setUp() {
messageContext,
codeableConceptCdMapper,
participantMapper,
confidentialityService
confidentialityService,
new PertinentInformationAllergyMapper(messageContext)
);
BloodPressureMapper bloodPressureMapper = new BloodPressureMapper(
messageContext,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@
import uk.nhs.adaptors.gp2gp.ehr.mapper.ObservationToNarrativeStatementMapper;
import uk.nhs.adaptors.gp2gp.ehr.mapper.OutputMessageWrapperMapper;
import uk.nhs.adaptors.gp2gp.ehr.mapper.ParticipantMapper;
import uk.nhs.adaptors.gp2gp.ehr.mapper.PertinentInformationAllergyMapper;
import uk.nhs.adaptors.gp2gp.ehr.mapper.PertinentInformationObservationValueMapper;
import uk.nhs.adaptors.gp2gp.ehr.mapper.RequestStatementMapper;
import uk.nhs.adaptors.gp2gp.ehr.mapper.StructuredObservationValueMapper;
Expand Down Expand Up @@ -177,7 +178,8 @@ public void setUp() {

return new EncounterComponentsMapper(
messageContext,
new AllergyStructureMapper(messageContext, codeableConceptCdMapper, participantMapper, confidentialityService),
new AllergyStructureMapper(messageContext, codeableConceptCdMapper, participantMapper, confidentialityService,
new PertinentInformationAllergyMapper(messageContext)),
new BloodPressureMapper(
messageContext, randomIdGeneratorService, new StructuredObservationValueMapper(),
codeableConceptCdMapper, new ParticipantMapper(), confidentialityService),
Expand Down