Skip to content

Commit

Permalink
FM2-629: Address non-coded Values Handling in AllergyIntolerance Reso…
Browse files Browse the repository at this point in the history
…urce (openmrs#531)
  • Loading branch information
jayasanka-sack authored Jan 16, 2024
1 parent b4dcbe2 commit b52d021
Show file tree
Hide file tree
Showing 4 changed files with 162 additions and 27 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,9 @@ private CodeableConcept getAllergySubstance(Allergen allergen) {

if (allergen.getCodedAllergen() != null) {
allergySubstance = conceptTranslator.toFhirResource(allergen.getCodedAllergen());
allergySubstance.setText(allergen.getNonCodedAllergen());
if (allergen.getNonCodedAllergen() != null) {
allergySubstance.setText(allergen.getNonCodedAllergen());
}
}

return allergySubstance;
Expand All @@ -101,8 +103,11 @@ private List<CodeableConcept> getManifestation(List<AllergyReaction> reactions)
if (reactions != null) {
for (AllergyReaction reaction : reactions) {
if (reaction.getReaction() != null) {
manifestations.add(
conceptTranslator.toFhirResource(reaction.getReaction()).setText(reaction.getReactionNonCoded()));
CodeableConcept manifestation = conceptTranslator.toFhirResource(reaction.getReaction());
if (reaction.getReactionNonCoded() != null) {
manifestation.setText(reaction.getReactionNonCoded());
}
manifestations.add(manifestation);
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@ public AllergyIntolerance toFhirResource(@Nonnull Allergy omrsAllergy) {

AllergyIntolerance allergy = new AllergyIntolerance();
allergy.setId(omrsAllergy.getUuid());
allergy.setCode(toCodeableConcept(omrsAllergy.getAllergen()));
if (omrsAllergy.getAllergen() != null) {
allergy.addCategory(categoryTranslator.toFhirResource(omrsAllergy.getAllergen().getAllergenType()));
}
Expand All @@ -82,14 +83,24 @@ public AllergyIntolerance toFhirResource(@Nonnull Allergy omrsAllergy) {
allergy.setCriticality(
criticalityTranslator.toFhirResource(severityTranslator.toFhirResource(omrsAllergy.getSeverity())));
allergy.addReaction(reactionComponentTranslator.toFhirResource(omrsAllergy));
allergy.setCode(allergy.getReactionFirstRep().getSubstance());

allergy.getMeta().setLastUpdated(getLastUpdated(omrsAllergy));
allergy.getMeta().setVersionId(getVersionId(omrsAllergy));

return allergy;
}

protected CodeableConcept toCodeableConcept(Allergen allergen) {
CodeableConcept code = new CodeableConcept();
if (allergen != null) {
code = conceptTranslator.toFhirResource(allergen.getCodedAllergen());
if (allergen.getNonCodedAllergen() != null) {
code.setText(allergen.getNonCodedAllergen());
}
}
return code;
}

@Override
public Allergy toOpenmrsType(@Nonnull AllergyIntolerance fhirAllergy) {
notNull(fhirAllergy, "The AllergyIntolerance object should not be null");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@

import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.equalTo;
import static org.hamcrest.Matchers.greaterThanOrEqualTo;
import static org.hamcrest.Matchers.hasSize;
import static org.hamcrest.Matchers.greaterThanOrEqualTo;
import static org.hamcrest.Matchers.notNullValue;
import static org.hamcrest.Matchers.nullValue;
import static org.mockito.Mockito.when;
Expand All @@ -32,6 +32,7 @@
import org.openmrs.Allergy;
import org.openmrs.AllergyReaction;
import org.openmrs.Concept;
import org.openmrs.ConceptName;
import org.openmrs.module.fhir2.FhirConstants;
import org.openmrs.module.fhir2.api.translators.AllergyIntoleranceSeverityTranslator;
import org.openmrs.module.fhir2.api.translators.ConceptTranslator;
Expand All @@ -47,8 +48,6 @@ public class AllergyIntoleranceReactionComponentTranslatorImplTest {

private AllergyIntoleranceReactionComponentTranslatorImpl reactionComponentTranslator;

private static final String NON_CODED_REACTION = "Test Reaction";

private static final String GLOBAL_PROPERTY_MILD_VALUE = "102553AAAAAAAAAAAAAAAAAAAAAAAAAAAAAA";

private static final String GLOBAL_PROPERTY_SEVERE_VALUE = "202553AAAAAAAAAAAAAAAAAAAAAAAAAAAAAA";
Expand All @@ -61,6 +60,16 @@ public class AllergyIntoleranceReactionComponentTranslatorImplTest {

private static final String CONCEPT_UUID = "162553AAAAAAAAAAAAAAAAAAAAAAAAAAAAAA";

private static final String CODED_ALLERGEN = "Coded Allergen";

private static final String NON_CODED_ALLERGEN = "Non coded Allergen";

private static final String CODED_REACTION = "Coded Reaction";

private static final String NON_CODED_REACTION = "Non coded Reaction";

private static final String OTHER_CONCEPT_NAME = "Other";

private Allergy omrsAllergy;

@Before
Expand All @@ -79,24 +88,53 @@ public void setUp() {
public void toFhirResource_shouldTranslateReactionToManifestation() {
Concept concept = new Concept();
concept.setUuid(CONCEPT_UUID);
concept.addName(new ConceptName(CODED_REACTION, null));

AllergyReaction reaction = new AllergyReaction();
reaction.setUuid(ALLERGY_REACTION_UUID);
reaction.setReaction(concept);
reaction.setAllergy(omrsAllergy);
reaction.setReactionNonCoded(NON_CODED_REACTION);
omrsAllergy.setReactions(Collections.singletonList(reaction));

CodeableConcept codeableConcept = new CodeableConcept();
codeableConcept.addCoding(new Coding().setCode(CONCEPT_UUID));
codeableConcept.setText(NON_CODED_REACTION);
codeableConcept.setText(CODED_REACTION);
when(conceptTranslator.toFhirResource(concept)).thenReturn(codeableConcept);

AllergyIntolerance.AllergyIntoleranceReactionComponent reactionComponent = reactionComponentTranslator
.toFhirResource(omrsAllergy);

assertThat(reactionComponent, notNullValue());
assertThat(reactionComponent.getManifestation(), hasSize(greaterThanOrEqualTo(1)));
assertThat(reactionComponent.getManifestation().get(0).getCoding(), hasSize(greaterThanOrEqualTo(1)));
assertThat(reactionComponent.getManifestation().size(), greaterThanOrEqualTo(1));
assertThat(reactionComponent.getManifestation(), hasSize(equalTo(1)));
assertThat(reactionComponent.getManifestation().get(0).getCoding(), hasSize(equalTo(1)));
assertThat(reactionComponent.getManifestation().get(0).getCoding().get(0).getCode(), equalTo(CONCEPT_UUID));
assertThat(reactionComponent.getManifestation().get(0).getText(), equalTo(CODED_REACTION));
}

@Test
public void toFhirResource_shouldTranslateNonCodedReactionToManifestation() {

Concept otherConcept = new Concept();
otherConcept.setUuid(GLOBAL_PROPERTY_OTHER_VALUE);
otherConcept.addName(new ConceptName(OTHER_CONCEPT_NAME, null));

AllergyReaction reaction = new AllergyReaction();
reaction.setUuid(ALLERGY_REACTION_UUID);
reaction.setReaction(otherConcept);
reaction.setAllergy(omrsAllergy);
reaction.setReactionNonCoded(NON_CODED_REACTION);
omrsAllergy.setReactions(Collections.singletonList(reaction));

CodeableConcept codeableConcept = new CodeableConcept();
codeableConcept.addCoding(new Coding().setCode(CONCEPT_UUID));
when(conceptTranslator.toFhirResource(otherConcept)).thenReturn(codeableConcept);

AllergyIntolerance.AllergyIntoleranceReactionComponent reactionComponent = reactionComponentTranslator
.toFhirResource(omrsAllergy);

assertThat(reactionComponent, notNullValue());
assertThat(reactionComponent.getManifestation(), hasSize(equalTo(1)));
assertThat(reactionComponent.getManifestation().get(0).getCoding(), hasSize(equalTo(1)));
assertThat(reactionComponent.getManifestation().get(0).getCoding().get(0).getCode(), equalTo(CONCEPT_UUID));
assertThat(reactionComponent.getManifestation().get(0).getText(), equalTo(NON_CODED_REACTION));
}
Expand Down Expand Up @@ -163,18 +201,47 @@ public void toFhirResource_shouldTranslateReactionOtherToNull() {
public void toFhirResource_shouldTranslateAllergenToAllergySubstance() {
Concept concept = new Concept();
concept.setUuid(CONCEPT_UUID);
concept.addName(new ConceptName(CODED_ALLERGEN, null));

Allergen allergen = new Allergen();
allergen.setCodedAllergen(concept);
allergen.setAllergenType(AllergenType.FOOD);
Allergen allergen = new Allergen(AllergenType.FOOD, concept, null);
omrsAllergy.setAllergen(allergen);

when(conceptTranslator.toFhirResource(concept))
.thenReturn(new CodeableConcept().addCoding(new Coding("", CONCEPT_UUID, "")));
CodeableConcept codeableConcept = new CodeableConcept();
codeableConcept.addCoding(new Coding().setCode(CONCEPT_UUID).setDisplay(CODED_ALLERGEN));
codeableConcept.setText(CODED_ALLERGEN);
when(conceptTranslator.toFhirResource(concept)).thenReturn(codeableConcept);

AllergyIntolerance.AllergyIntoleranceReactionComponent reactionComponent = reactionComponentTranslator
.toFhirResource(omrsAllergy);
assertThat(reactionComponent, notNullValue());
assertThat(reactionComponent.getSubstance().getCodingFirstRep().getCode(), equalTo(CONCEPT_UUID));
assertThat(reactionComponent.getSubstance().getCodingFirstRep().getDisplay(), equalTo(CODED_ALLERGEN));
assertThat(reactionComponent.getSubstance().getText(), equalTo(CODED_ALLERGEN));
}

@Test
public void toFhirResource_shouldTranslateNonCodedAllergenToAllergySubstance() {

Concept otherConcept = new Concept();
otherConcept.setUuid(GLOBAL_PROPERTY_OTHER_VALUE);
otherConcept.addName(new ConceptName(OTHER_CONCEPT_NAME, null));

Allergen allergen = new Allergen(AllergenType.OTHER, otherConcept, NON_CODED_ALLERGEN);
omrsAllergy.setAllergen(allergen);

CodeableConcept codeableConcept = new CodeableConcept();
codeableConcept.addCoding(new Coding().setCode(GLOBAL_PROPERTY_OTHER_VALUE).setDisplay(OTHER_CONCEPT_NAME));
codeableConcept.setText(OTHER_CONCEPT_NAME);
when(conceptTranslator.toFhirResource(otherConcept)).thenReturn(codeableConcept);

AllergyIntolerance.AllergyIntoleranceReactionComponent reactionComponent = reactionComponentTranslator
.toFhirResource(omrsAllergy);

assertThat(reactionComponent, notNullValue());
assertThat(reactionComponent.getSubstance().getCodingFirstRep().getCode(), equalTo(GLOBAL_PROPERTY_OTHER_VALUE));
assertThat(reactionComponent.getSubstance().getCodingFirstRep().getDisplay(), equalTo(OTHER_CONCEPT_NAME));
assertThat(reactionComponent.getSubstance().getText(), equalTo(NON_CODED_ALLERGEN));

}

@Test
Expand Down Expand Up @@ -275,7 +342,7 @@ public void toOpenmrsType_shouldTranslateManifestationToReaction() {
reactionComponent.setSeverity(AllergyIntolerance.AllergyIntoleranceSeverity.MODERATE);

CodeableConcept manifestation = new CodeableConcept()
.addCoding(new Coding(FhirConstants.CLINICAL_FINDINGS_SYSTEM_URI, CONCEPT_UUID, "Test Reaction"));
.addCoding(new Coding(FhirConstants.CLINICAL_FINDINGS_SYSTEM_URI, CONCEPT_UUID, CODED_REACTION));
reactionComponent.addManifestation(manifestation);

Concept codedReaction = new Concept();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@
import org.openmrs.AllergenType;
import org.openmrs.Allergy;
import org.openmrs.Concept;
import org.openmrs.ConceptName;
import org.openmrs.Patient;
import org.openmrs.User;
import org.openmrs.module.fhir2.FhirTestConstants;
Expand Down Expand Up @@ -68,6 +69,16 @@ public class AllergyIntoleranceTranslatorImplTest {

private static final String GLOBAL_PROPERTY_OTHER_VALUE = "402553AAAAAAAAAAAAAAAAAAAAAAAAAAAAAA";

private static final String CODED_ALLERGEN = "Coded Allergen";

private static final String NON_CODED_ALLERGEN = "Non coded Allergen";

private static final String CODED_REACTION = "Coded Reaction";

private static final String NON_CODED_REACTION = "Non coded Reaction";

private static final String OTHER_CONCEPT_NAME = "Other";

@Mock
private PractitionerReferenceTranslator<User> practitionerReferenceTranslator;

Expand Down Expand Up @@ -105,7 +116,10 @@ public void setUp() {
allergyIntoleranceTranslator.setReactionComponentTranslator(reactionComponentTranslator);

omrsAllergy = new Allergy();
Allergen allergen = new Allergen(AllergenType.FOOD, null, "Test allergen");
Concept allergenConcept = new Concept();
allergenConcept.setUuid(CONCEPT_UUID);
allergenConcept.addName(new ConceptName(CODED_ALLERGEN, null));
Allergen allergen = new Allergen(AllergenType.FOOD, allergenConcept, null);
omrsAllergy.setAllergen(allergen);
}

Expand All @@ -123,6 +137,52 @@ public void toFhirResource_shouldTranslateUuidToId() {
assertThat(allergyIntolerance.getId(), equalTo(ALLERGY_UUID));
}

@Test
public void toFhirResource_shouldTranslateCodedAllergen() {
Concept concept = new Concept();
concept.setUuid(CONCEPT_UUID);
concept.addName(new ConceptName(CODED_ALLERGEN, null));

Allergen allergen = new Allergen(AllergenType.FOOD, concept, null);
omrsAllergy.setAllergen(allergen);

CodeableConcept codeableConcept = new CodeableConcept();
codeableConcept.addCoding(new Coding().setCode(CONCEPT_UUID).setDisplay(CODED_ALLERGEN));
codeableConcept.setText(CODED_ALLERGEN);
when(conceptTranslator.toFhirResource(concept)).thenReturn(codeableConcept);

AllergyIntolerance allergyIntolerance = allergyIntoleranceTranslator.toFhirResource(omrsAllergy);

assertThat(allergyIntolerance, notNullValue());
assertThat(allergyIntolerance.getCode(), notNullValue());
assertThat(allergyIntolerance.getCode().getText(), equalTo(CODED_ALLERGEN));

Coding coding = allergyIntolerance.getCode().getCoding().get(0);
assertThat(coding.getCode(), equalTo(CONCEPT_UUID));
assertThat(coding.getDisplay(), equalTo(CODED_ALLERGEN));
}

@Test
public void toFhirResource_shouldTranslateNonCodedAllergen() {
Concept otherConcept = new Concept();
otherConcept.setUuid(CONCEPT_UUID);
otherConcept.addName(new ConceptName(OTHER_CONCEPT_NAME, null));

Allergen allergen = new Allergen(AllergenType.OTHER, otherConcept, NON_CODED_ALLERGEN);
omrsAllergy.setAllergen(allergen);

CodeableConcept codeableConcept = new CodeableConcept();
codeableConcept.addCoding(new Coding().setCode(CONCEPT_UUID).setDisplay(OTHER_CONCEPT_NAME));
codeableConcept.setText(OTHER_CONCEPT_NAME);
when(conceptTranslator.toFhirResource(otherConcept)).thenReturn(codeableConcept);

AllergyIntolerance allergyIntolerance = allergyIntoleranceTranslator.toFhirResource(omrsAllergy);

assertThat(allergyIntolerance, notNullValue());
assertThat(allergyIntolerance.getCode(), notNullValue());
assertThat(allergyIntolerance.getCode().getText(), equalTo(NON_CODED_ALLERGEN));
}

@Test
public void toFhirResource_shouldTranslateNullUuidToNullId() {
omrsAllergy.setUuid(null);
Expand Down Expand Up @@ -275,14 +335,6 @@ public void toFhirResource_shouldTranslateToAllergyType() {
assertThat(allergyIntolerance.getType(), equalTo(AllergyIntolerance.AllergyIntoleranceType.ALLERGY));
}

@Test
public void toFhirResource_shouldReturnNullSeverityIfSeverityConceptIsNull() {
omrsAllergy.setSeverity(null);

AllergyIntolerance allergyIntolerance = allergyIntoleranceTranslator.toFhirResource(omrsAllergy);
assertThat(allergyIntolerance.getReaction().get(0).getSeverity(), nullValue());
}

@Test
public void toFhirResource_shouldTranslateToHighCriticality() {
Concept severeConcept = new Concept();
Expand Down

0 comments on commit b52d021

Please sign in to comment.