From 84a928bb1ce28d6c1cb59a86e7cab25ebb87766c Mon Sep 17 00:00:00 2001 From: "gayan.weerakutti" Date: Sun, 24 Mar 2024 21:56:36 +0900 Subject: [PATCH] TRUNK-5721 Fix ObsService.getObs error when Obs.valueComplex null --- .../java/org/openmrs/api/ObsServiceTest.java | 41 +++++++++++++++++++ 1 file changed, 41 insertions(+) diff --git a/api/src/test/java/org/openmrs/api/ObsServiceTest.java b/api/src/test/java/org/openmrs/api/ObsServiceTest.java index efa5eda86be9..e17ff6b2b941 100644 --- a/api/src/test/java/org/openmrs/api/ObsServiceTest.java +++ b/api/src/test/java/org/openmrs/api/ObsServiceTest.java @@ -55,6 +55,7 @@ import org.openmrs.api.impl.ObsServiceImpl; import org.openmrs.obs.ComplexData; import org.openmrs.obs.ComplexObsHandler; +import org.openmrs.obs.handler.AbstractHandler; import org.openmrs.obs.handler.BinaryDataHandler; import org.openmrs.obs.handler.ImageHandler; import org.openmrs.obs.handler.TextHandler; @@ -812,6 +813,46 @@ public void getObs_shouldGetObsMatchingGivenObsId() { assertEquals(5089, obs.getConcept().getId().intValue()); } + /** + * This is a test to verify the behavior reported in TRUNK-5721. + * The test name and the content will be refactored once the expected behavior is known + * + * @see ObsService#saveObs(Obs,String) + * @see ObsService#getObs(Integer) + * @see ComplexObsHandler#saveObs(Obs) + */ + @Test + public void getObs_shouldGetObsWithComplexDataNull() throws IOException { + // load and retrieve a complex concept from COMPLEX_OBS_XML + executeDataSet(COMPLEX_OBS_XML); + Concept complexConcept = Context.getConceptService().getConcept(8474); + + // construct complex observation, but with complex data field set to null + Obs complexObs = new Obs(new Person(1), complexConcept, new Date(), new Location(1)); + ComplexData complexData = new ComplexData("title", null); + complexObs.setComplexData(complexData); + assertTrue(complexObs.isComplex()); + + // delete the complex data file if already exists + File complexDataFile = new AbstractHandler().getOutputFileToWrite(complexObs); + if (complexDataFile.exists()) { + complexDataFile.delete(); + } + + try { + Context.getObsService().saveObs(complexObs, null); + + // Verify that data file not exist + assertFalse(complexDataFile.exists()); + + // Assert that ObsService.getObs fails if data file not exist + assertThrows(NullPointerException.class, () -> obsService.getObs(complexObs.getObsId())); + } + finally { + complexDataFile.delete(); + } + } + /** * @see ObsService#getObservations(List,List,List,List,List,List,List,Integer,Integer,Date,Date,boolean) * @see ObsService#getObservations(List,List,List,List,List,List,List,Integer,Integer,Date,Date,boolean,String)