Skip to content

Commit e93c245

Browse files
authored
TRUNK-6463: Problem editing existing Complex Obs with the new Local F… (#5458) (#5463)
* TRUNK-6463: Problem editing existing Complex Obs with the new Local File Storage Service TRUNK-6464: Test Context should not create new application data directory for each test
1 parent 98e81b6 commit e93c245

File tree

3 files changed

+79
-17
lines changed

3 files changed

+79
-17
lines changed

api/src/main/java/org/openmrs/api/impl/ObsServiceImpl.java

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -105,9 +105,7 @@ public Obs saveObs(Obs obs, String changeMessage) throws APIException {
105105
if(obs.getId() != null && changeMessage == null){
106106
throw new APIException("Obs.error.ChangeMessage.required", (Object[]) null);
107107
}
108-
109-
handleExistingObsWithComplexConcept(obs);
110-
108+
111109
ensureRequirePrivilege(obs);
112110

113111
//Should allow updating a voided Obs, it seems to be pointless to restrict it,
@@ -162,7 +160,7 @@ private Obs saveExistingObs(Obs obs, String changeMessage) {
162160
Obs newObs = Obs.newInstance(obs);
163161

164162
unsetVoidedAndCreationProperties(newObs,obs);
165-
163+
handleObsWithComplexConcept(newObs);
166164
Obs.Status originalStatus = dao.getSavedStatus(obs);
167165
updateStatusIfNecessary(newObs, originalStatus);
168166

@@ -220,6 +218,7 @@ private Obs saveObsNotDirty(Obs obs, String changeMessage) {
220218
}
221219

222220
private Obs saveNewOrVoidedObs(Obs obs, String changeMessage) {
221+
handleObsWithComplexConcept(obs);
223222
Obs ret = dao.saveObs(obs);
224223
saveObsGroup(ret,changeMessage);
225224
return ret;
@@ -250,7 +249,7 @@ private void saveObsGroup(Obs obs, String changeMessage){
250249
}
251250
}
252251

253-
private void handleExistingObsWithComplexConcept(Obs obs) {
252+
private void handleObsWithComplexConcept(Obs obs) {
254253
ComplexData complexData = obs.getComplexData();
255254
Concept concept = obs.getConcept();
256255
if (null != concept && concept.isComplex()

api/src/test/java/org/openmrs/api/ObsServiceTest.java

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -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)}

api/src/test/java/org/openmrs/test/jupiter/BaseContextSensitiveTest.java

Lines changed: 14 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -354,18 +354,20 @@ public Properties getRuntimeProperties() {
354354
runtimeProperties.setProperty(Environment.HBM2DDL_AUTO, "update");
355355
}
356356

357-
try {
358-
File tempappdir = File.createTempFile("appdir-for-unit-tests-", "");
359-
tempappdir.delete(); // so we can make it into a directory
360-
tempappdir.mkdir(); // turn it into a directory
361-
tempappdir.deleteOnExit(); // clean up when we're done with tests
362-
363-
runtimeProperties.setProperty(OpenmrsConstants.APPLICATION_DATA_DIRECTORY_RUNTIME_PROPERTY, tempappdir
364-
.getAbsolutePath());
365-
OpenmrsUtil.setApplicationDataDirectory(tempappdir.getAbsolutePath());
366-
}
367-
catch (IOException e) {
368-
log.error("Unable to create temp dir", e);
357+
String appDataDir = OpenmrsUtil.getApplicationDataDirectory();
358+
if (appDataDir == null || !appDataDir.contains("appdir-for-unit-tests-")) {
359+
try {
360+
File tempappdir = File.createTempFile("appdir-for-unit-tests-", "");
361+
tempappdir.delete(); // so we can make it into a directory
362+
tempappdir.mkdir(); // turn it into a directory
363+
tempappdir.deleteOnExit(); // clean up when we're done with tests
364+
365+
runtimeProperties.setProperty(OpenmrsConstants.APPLICATION_DATA_DIRECTORY_RUNTIME_PROPERTY, tempappdir
366+
.getAbsolutePath());
367+
OpenmrsUtil.setApplicationDataDirectory(tempappdir.getAbsolutePath());
368+
} catch (IOException e) {
369+
log.error("Unable to create temp dir", e);
370+
}
369371
}
370372

371373
return runtimeProperties;

0 commit comments

Comments
 (0)