@@ -694,6 +694,67 @@ public void saveObs_shouldNotOverwriteFileWhenUpdatingAComplexObs() throws IOExc
694694 }
695695
696696 }
697+
698+ @ Test
699+ public void updateObs_shouldUpdateAComplexObs () throws IOException {
700+ executeDataSet (COMPLEX_OBS_XML );
701+ ObsService os = Context .getObsService ();
702+ ConceptService cs = Context .getConceptService ();
703+
704+ Obs obs = null ;
705+ Obs updatedObs = null ;
706+
707+ File obsFile = null ;
708+ File updatedObsFile = null ;
709+
710+ try {
711+ // the complex data to put onto an obs that will be saved
712+ Reader input2 = new CharArrayReader ("some string" .toCharArray ());
713+ ComplexData complexData = new ComplexData ("nameOfFile" , input2 );
714+
715+ // must fetch the concept instead of just new Concept(8473) because the attributes on concept are checked
716+ // this is a concept mapped to the text handler
717+ Concept questionConcept = cs .getConcept (8474 );
718+
719+ obs = new Obs (new Person (1 ), questionConcept , new Date (), new Location (1 ));
720+
721+ obs .setComplexData (complexData );
722+ os .saveObs (obs , null );
723+
724+ // sanity check, confirm the file exists
725+ obs = os .getObs (obs .getObsId ());
726+ obsFile = new File (OpenmrsUtil .getApplicationDataDirectory () + File .separator + "storage" + File .separator + obs .getValueComplex ().split ("\\ |" )[1 ]);
727+ assertTrue (obsFile .exists ());
728+
729+ // now change the obs
730+ // NOTE: this really should change an actual field instead of voidReason; I originally had this change
731+ // the comment field but, somewhat disconcertingly, this caused an UnchangeableObjectException because it
732+ // tries to flush the original obs (before it is cloned) when it attempts to do a "getGlobalProperty" call.
733+ // This exception doesn't occur in real life, and getGlobalProperty is set to transactional=readOnly, so
734+ // not sure why it causes a flush when testing.
735+ obs .setVoidReason ("some comment" );
736+ updatedObs = os .saveObs (obs , "updating obs" );
737+
738+ // confirm old file has been removed and new one exists
739+ updatedObsFile = new File (OpenmrsUtil .getApplicationDataDirectory () + File .separator + "storage" + File .separator + updatedObs .getValueComplex ().split ("\\ |" )[1 ]);
740+ assertTrue (updatedObsFile .exists ());
741+ assertFalse (obsFile .exists ());
742+
743+ }
744+ finally {
745+ if (obsFile != null && obsFile .exists ()) {
746+ obsFile .delete ();
747+ }
748+ if (updatedObsFile != null && updatedObsFile .exists ()) {
749+ updatedObsFile .delete ();
750+ }
751+ }
752+ }
753+
754+ private void confirmFileWithNameExists (File [] files , String name ) {
755+ assertTrue (Arrays .stream (files ).anyMatch (f -> f .getName ().equals (name )));
756+ }
757+
697758
698759 /**
699760 * @see ObsService#setHandlers(Map)}
0 commit comments