Skip to content

Refactor AllergyStructureMapper#1430

Draft
MartinWheelerMT wants to merge 4 commits into
mainfrom
refactor-allergy-structure-mapper
Draft

Refactor AllergyStructureMapper#1430
MartinWheelerMT wants to merge 4 commits into
mainfrom
refactor-allergy-structure-mapper

Conversation

@MartinWheelerMT

@MartinWheelerMT MartinWheelerMT commented Jan 20, 2026

Copy link
Copy Markdown
Collaborator

What

  • Move pertinent information mapping to separate mapper PertinentInformationAllergyMapper, in the same way it is handled for observation values.
  • Remove enum -> primitive string mapping when building the category and use the fhir model enums for comparison instead.
  • Inject new mapper into class and use real implementation in tests as classes.
  • Tidy code for readability.

Why

AllergyStructureMapper was difficult to read and contained code duplication, along with containing a number of pertinent information builders which could be refactored into another component and injected, in the same style as we handle the pertinent information for Observation values.

Type of change

Please delete options that are not relevant.

  • Bug fix (non-breaking change which fixes an issue)
  • New feature (non-breaking change which adds functionality)
  • Internal change (non-breaking change with no effect on the functionality affecting end users)
  • Breaking change (fix or feature that would cause existing functionality to not work as expected)

Checklist:

  • I have performed a self-review of my code
  • I have made corresponding changes to the documentation
  • My changes generate no new warnings
  • I have added tests that prove my fix is effective or that my feature works
  • New and existing unit tests pass locally with my changes
  • I have updated the Changelog with details of my change in the UNRELEASED section if this change will affect end users

* Move pertinent information mapping to separate mapper `PertinentInformationAllergyMapper`, in the same way it is handled for observation values.
* Remove enum -> primitive string mapping when building the category and use the fhir model enums for comparison instead.
* Inject new mapper into class and use real implementation in tests as classes.
* Tidy code for readability.
@sonarqubecloud

Copy link
Copy Markdown

@github-actions

github-actions Bot commented Jun 3, 2026

Copy link
Copy Markdown
  • Surviving mutants in this change: 16
  • Killed mutants in this change: 76
class surviving killed
🧟uk.nhs.adaptors.gp2gp.ehr.mapper.PertinentInformationAllergyMapper 9 40
🧟uk.nhs.adaptors.gp2gp.ehr.mapper.AllergyStructureMapper 7 36

See https://pitest.org

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This PR refactors allergy mapping by extracting “pertinent information” string-building logic out of AllergyStructureMapper into a dedicated PertinentInformationAllergyMapper, and updates the mapper wiring in unit/component/UAT tests accordingly.

Changes:

  • Introduced PertinentInformationAllergyMapper and moved pertinent-information construction logic into it.
  • Refactored AllergyStructureMapper to delegate pertinent-information generation and to compare allergy categories using FHIR enums rather than primitive string values.
  • Updated tests to construct/inject the new mapper when instantiating AllergyStructureMapper.

Reviewed changes

Copilot reviewed 6 out of 6 changed files in this pull request and generated 4 comments.

Show a summary per file
File Description
service/src/main/java/uk/nhs/adaptors/gp2gp/ehr/mapper/PertinentInformationAllergyMapper.java New component containing allergy pertinent-information mapping logic.
service/src/main/java/uk/nhs/adaptors/gp2gp/ehr/mapper/AllergyStructureMapper.java Refactor to delegate pertinent information and use enum-based category checks; constructor updated.
service/src/test/java/uk/nhs/adaptors/gp2gp/uat/EhrExtractUATTest.java Updates test wiring to pass the new mapper into AllergyStructureMapper.
service/src/test/java/uk/nhs/adaptors/gp2gp/ehr/mapper/EncounterComponentsMapperTest.java Updates mapper construction to include PertinentInformationAllergyMapper.
service/src/test/java/uk/nhs/adaptors/gp2gp/ehr/mapper/EhrExtractMapperComponentTest.java Updates mapper construction to include PertinentInformationAllergyMapper.
service/src/test/java/uk/nhs/adaptors/gp2gp/ehr/mapper/AllergyStructureMapperTest.java Updates AllergyStructureMapper instantiation to include PertinentInformationAllergyMapper.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment on lines +96 to +100
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())) {
Comment on lines +128 to +132
if (allergyIntolerance.hasRecorder()) {
IIdType reference = allergyIntolerance.getRecorder().getReferenceElement();
if (reference.getResourceType().equals(ResourceType.Patient.name())) {
return PATIENT_RECORDER;
}
Comment on lines 118 to 122
private Optional<String> buildParticipant(Reference reference, ParticipantType participantType) {
if (reference.getReferenceElement().getResourceType().startsWith(ResourceType.Practitioner.name())) {
var authorReferenceId = messageContext.getAgentDirectory().getAgentId(reference);
return Optional.of(participantMapper.mapToParticipant(authorReferenceId, participantType));
var resourceType = reference.getReferenceElement().getResourceType();
if (!resourceType.startsWith(ResourceType.Practitioner.name())) {
return Optional.empty();
}
Comment on lines 146 to 149
private boolean isValidAsserter(AllergyIntolerance allergyIntolerance) {
return allergyIntolerance.hasAsserter()
&& allergyIntolerance.getAsserter().getReferenceElement().getResourceType().startsWith(ResourceType.Practitioner.name());
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants