diff --git a/api/src/main/java/org/openmrs/module/fhir2/api/translators/impl/AllergyIntoleranceReactionComponentTranslatorImpl.java b/api/src/main/java/org/openmrs/module/fhir2/api/translators/impl/AllergyIntoleranceReactionComponentTranslatorImpl.java index 8f91b9b2f..38e270d92 100644 --- a/api/src/main/java/org/openmrs/module/fhir2/api/translators/impl/AllergyIntoleranceReactionComponentTranslatorImpl.java +++ b/api/src/main/java/org/openmrs/module/fhir2/api/translators/impl/AllergyIntoleranceReactionComponentTranslatorImpl.java @@ -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; @@ -101,8 +103,11 @@ private List getManifestation(List 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); } } } diff --git a/api/src/main/java/org/openmrs/module/fhir2/api/translators/impl/AllergyIntoleranceTranslatorImpl.java b/api/src/main/java/org/openmrs/module/fhir2/api/translators/impl/AllergyIntoleranceTranslatorImpl.java index a27a57d7f..d15622d4b 100644 --- a/api/src/main/java/org/openmrs/module/fhir2/api/translators/impl/AllergyIntoleranceTranslatorImpl.java +++ b/api/src/main/java/org/openmrs/module/fhir2/api/translators/impl/AllergyIntoleranceTranslatorImpl.java @@ -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())); } @@ -82,7 +83,6 @@ 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)); @@ -90,6 +90,17 @@ public AllergyIntolerance toFhirResource(@Nonnull Allergy 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"); diff --git a/api/src/test/java/org/openmrs/module/fhir2/api/translators/impl/AllergyIntoleranceReactionComponentTranslatorImplTest.java b/api/src/test/java/org/openmrs/module/fhir2/api/translators/impl/AllergyIntoleranceReactionComponentTranslatorImplTest.java index 873e1c1b2..17e706e08 100644 --- a/api/src/test/java/org/openmrs/module/fhir2/api/translators/impl/AllergyIntoleranceReactionComponentTranslatorImplTest.java +++ b/api/src/test/java/org/openmrs/module/fhir2/api/translators/impl/AllergyIntoleranceReactionComponentTranslatorImplTest.java @@ -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; @@ -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; @@ -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"; @@ -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 @@ -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)); } @@ -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 @@ -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(); diff --git a/api/src/test/java/org/openmrs/module/fhir2/api/translators/impl/AllergyIntoleranceTranslatorImplTest.java b/api/src/test/java/org/openmrs/module/fhir2/api/translators/impl/AllergyIntoleranceTranslatorImplTest.java index ae9ff0499..078abb84c 100644 --- a/api/src/test/java/org/openmrs/module/fhir2/api/translators/impl/AllergyIntoleranceTranslatorImplTest.java +++ b/api/src/test/java/org/openmrs/module/fhir2/api/translators/impl/AllergyIntoleranceTranslatorImplTest.java @@ -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; @@ -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 practitionerReferenceTranslator; @@ -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); } @@ -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); @@ -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(); diff --git a/pom.xml b/pom.xml index 0c161b8cc..09fd024df 100644 --- a/pom.xml +++ b/pom.xml @@ -250,6 +250,10 @@ org.slf4j slf4j-api + + org.simplejavamail + simple-java-mail +