1212import static org .hamcrest .CoreMatchers .is ;
1313import static org .hamcrest .MatcherAssert .assertThat ;
1414import static org .hamcrest .Matchers .equalTo ;
15+ import static org .hamcrest .Matchers .not ;
1516import static org .hl7 .fhir .r4 .model .Patient .SP_IDENTIFIER ;
17+ import static org .junit .Assert .assertNull ;
18+ import static org .junit .Assert .assertTrue ;
19+ import static org .junit .jupiter .api .Assertions .assertNotNull ;
1620import static org .openmrs .module .fhir2 .FhirConstants .ENCOUNTER ;
1721import static org .openmrs .module .fhir2 .FhirConstants .PATIENT ;
1822import static org .openmrs .module .fhir2 .FhirConstants .PRACTITIONER ;
1923import static org .openmrs .module .fhir2 .api .translators .impl .ImmunizationTranslatorImpl .CIEL_1410 ;
2024import static org .openmrs .module .fhir2 .api .translators .impl .ImmunizationTranslatorImpl .CIEL_1418 ;
2125import static org .openmrs .module .fhir2 .api .translators .impl .ImmunizationTranslatorImpl .CIEL_1419 ;
2226import static org .openmrs .module .fhir2 .api .translators .impl .ImmunizationTranslatorImpl .CIEL_1420 ;
27+ import static org .openmrs .module .fhir2 .api .translators .impl .ImmunizationTranslatorImpl .CIEL_161011 ;
2328import static org .openmrs .module .fhir2 .api .translators .impl .ImmunizationTranslatorImpl .CIEL_165907 ;
2429import static org .openmrs .module .fhir2 .api .translators .impl .ImmunizationTranslatorImpl .CIEL_984 ;
2530
31+ import java .nio .charset .StandardCharsets ;
2632import java .util .List ;
2733import java .util .Map ;
34+ import java .util .Objects ;
2835import java .util .Set ;
2936import java .util .stream .Collectors ;
3037
3441import ca .uhn .fhir .rest .param .ReferenceAndListParam ;
3542import ca .uhn .fhir .rest .param .ReferenceOrListParam ;
3643import ca .uhn .fhir .rest .param .ReferenceParam ;
44+ import org .apache .commons .io .IOUtils ;
3745import org .hl7 .fhir .r4 .model .Coding ;
3846import org .hl7 .fhir .r4 .model .DateTimeType ;
3947import org .hl7 .fhir .r4 .model .DateType ;
4351import org .openmrs .Obs ;
4452import org .openmrs .Provider ;
4553import org .openmrs .api .AdministrationService ;
54+ import org .openmrs .api .ConceptService ;
4655import org .openmrs .api .ObsService ;
4756import org .openmrs .module .fhir2 .BaseFhirContextSensitiveTest ;
4857import org .openmrs .module .fhir2 .FhirConstants ;
5160import org .springframework .beans .factory .annotation .Qualifier ;
5261
5362public class FhirImmunizationServiceImplTest extends BaseFhirContextSensitiveTest {
54-
63+
64+ private static final String FREETEXT_COMMENT_CONCEPT_CODE = "161011" ;
65+
66+ private static final String FREETEXT_COMMENT_CONCEPT_SOURCE = "CIEL" ;
67+
5568 private static final String IMMUNIZATIONS_METADATA_XML = "org/openmrs/module/fhir2/Immunization_metadata.xml" ;
5669
5770 private static final String IMMUNIZATIONS_INITIAL_DATA_XML = "org/openmrs/module/fhir2/api/services/impl/FhirImmunizationService_initial_data.xml" ;
@@ -61,6 +74,9 @@ public class FhirImmunizationServiceImplTest extends BaseFhirContextSensitiveTes
6174 @ Autowired
6275 private FhirImmunizationServiceImpl service ;
6376
77+ @ Autowired
78+ private ConceptService conceptService ;
79+
6480 @ Autowired
6581 private ObsService obsService ;
6682
@@ -144,6 +160,43 @@ public void saveImmunization_shouldCreateEncounterAndObsGroupWhenNewImmunization
144160 equalTo (new DateTimeType ("2022-07-31T18:30:00.000Z" ).getValue ()));
145161 }
146162
163+ @ Test
164+ public void saveImmunization_shouldSaveImmunizationWithNoteField () throws Exception {
165+ FhirContext ctx = FhirContext .forR4 ();
166+ IParser parser = ctx .newJsonParser ();
167+ String json = IOUtils .toString (
168+ Objects .requireNonNull (
169+ getClass ().getResourceAsStream ("/org/openmrs/module/fhir2/providers/immunization-note.json" )),
170+ StandardCharsets .UTF_8 );
171+ Immunization newImmunization = parser .parseResource (Immunization .class , json );
172+ Immunization savedImmunization = service .create (newImmunization );
173+ Obs obs = obsService .getObsByUuid (savedImmunization .getId ());
174+ Map <String , Obs > members = helper .getObsMembersMap (obs );
175+ assertThat (members .get (CIEL_161011 ).getValueText (), is ("This is a test immunization note." ));
176+ assertThat (savedImmunization .getNoteFirstRep ().getText (), is ("This is a test immunization note." ));
177+ }
178+
179+ @ Test
180+ public void saveImmunization_shouldNotFailIfNoteConceptIsMissingAndNoteProvided () throws Exception {
181+ // Remove the note concept since @Before loads it
182+ conceptService .purgeConcept (conceptService .getConceptByMapping (FREETEXT_COMMENT_CONCEPT_CODE , FREETEXT_COMMENT_CONCEPT_SOURCE ));
183+ assertNull (conceptService .getConceptByMapping (FREETEXT_COMMENT_CONCEPT_CODE , FREETEXT_COMMENT_CONCEPT_SOURCE ));
184+
185+ FhirContext ctx = FhirContext .forR4 ();
186+ IParser parser = ctx .newJsonParser ();
187+ String json = IOUtils .toString (
188+ Objects .requireNonNull (
189+ getClass ().getResourceAsStream ("/org/openmrs/module/fhir2/providers/immunization-note.json" )),
190+ StandardCharsets .UTF_8 );
191+ Immunization newImmunization = parser .parseResource (Immunization .class , json );
192+ Immunization savedImmunization = service .create (newImmunization );
193+ Obs obs = obsService .getObsByUuid (savedImmunization .getId ());
194+ Map <String , Obs > members = helper .getObsMembersMap (obs );
195+ assertNull (members .get (CIEL_161011 ));
196+ assertTrue (savedImmunization .getNote ().isEmpty () || savedImmunization .getNoteFirstRep ().getText () == null );
197+ assertThat (savedImmunization .getNoteFirstRep ().getText (), is (not ("This is a test immunization note." )));
198+ }
199+
147200 @ Test
148201 public void updateImmunization_shouldUpdateImmunizationAccordingly () {
149202 // setup
@@ -188,6 +241,43 @@ public void updateImmunization_shouldUpdateImmunizationAccordingly() {
188241 assertThat (members .get (CIEL_165907 ).getValueDate (), equalTo (new DateType ("2020-10-08" ).getValue ()));
189242 }
190243
244+ @ Test
245+ public void updateImmunization_shouldUpdateNoteFieldWhenNoteConceptIsAvailable () throws Exception {
246+ FhirContext ctx = FhirContext .forR4 ();
247+ IParser parser = ctx .newJsonParser ();
248+ String createJson = IOUtils .toString (
249+ Objects .requireNonNull (
250+ getClass ().getResourceAsStream ("/org/openmrs/module/fhir2/providers/immunization-note.json" )),
251+ StandardCharsets .UTF_8 );
252+ Immunization created = service .create (parser .parseResource (Immunization .class , createJson ));
253+ assertThat (created .getNoteFirstRep ().getText (), is ("This is a test immunization note." ));
254+
255+ Immunization immunizationToBeUpdated = service .get (created .getId ());
256+ immunizationToBeUpdated .getNote ().clear ();
257+ immunizationToBeUpdated .addNote ().setText ("This is an UPDATED immunization note." );
258+ Immunization updated = service .update (created .getId (), immunizationToBeUpdated );
259+ assertThat (updated .getNoteFirstRep ().getText (), is ("This is an UPDATED immunization note." ));
260+ }
261+
262+ @ Test
263+ public void updateImmunization_shouldNotFailIfNoteConceptIsMissingButNoteIsProvided () throws Exception {
264+ // Remove the note concept since @Before loads it
265+ conceptService .purgeConcept (conceptService .getConceptByMapping (FREETEXT_COMMENT_CONCEPT_CODE , FREETEXT_COMMENT_CONCEPT_SOURCE ));
266+ assertNull (conceptService .getConceptByMapping (FREETEXT_COMMENT_CONCEPT_CODE , FREETEXT_COMMENT_CONCEPT_SOURCE ));
267+
268+ FhirContext ctx = FhirContext .forR4 ();
269+ IParser parser = ctx .newJsonParser ();
270+ String createJson = IOUtils .toString (
271+ Objects .requireNonNull (
272+ getClass ().getResourceAsStream ("/org/openmrs/module/fhir2/providers/immunization-note.json" )),
273+ StandardCharsets .UTF_8 );
274+ Immunization created = service .create (parser .parseResource (Immunization .class , createJson ));
275+ created .getNote ().clear ();
276+ created .addNote ().setText ("This is an UPDATED immunization note." );
277+ Immunization updated = service .update (created .getId (), created );
278+ assertTrue (updated .getNote ().isEmpty () || updated .getNoteFirstRep ().getText () == null );
279+ }
280+
191281 @ Test
192282 public void searchImmunizations_shouldFetchImmunizationsByPatientIdentifier () {
193283 // setup
0 commit comments