From bc0243e67833b85c67af4012c40f6312d77ae6eb Mon Sep 17 00:00:00 2001 From: Viet Nguyen Date: Wed, 27 Nov 2024 12:43:38 -0600 Subject: [PATCH] fix: NPE when delete eventVisualization (#19315) --- .../dhis/preheat/DefaultPreheatService.java | 15 +++++++++++-- .../EventVisualizationControllerTest.java | 22 +++++++++++++++++++ 2 files changed, 35 insertions(+), 2 deletions(-) diff --git a/dhis-2/dhis-services/dhis-service-core/src/main/java/org/hisp/dhis/preheat/DefaultPreheatService.java b/dhis-2/dhis-services/dhis-service-core/src/main/java/org/hisp/dhis/preheat/DefaultPreheatService.java index 111b0a12bc39..a7ac63029f23 100644 --- a/dhis-2/dhis-services/dhis-service-core/src/main/java/org/hisp/dhis/preheat/DefaultPreheatService.java +++ b/dhis-2/dhis-services/dhis-service-core/src/main/java/org/hisp/dhis/preheat/DefaultPreheatService.java @@ -855,7 +855,13 @@ private void collectScanTargets(Map, List> targets) { } objects.forEach( - o -> list.addAll(ReflectionUtils.invokeMethod(o, property.getGetterMethod()))); + o -> { + Collection propertyValue = + ReflectionUtils.invokeMethod(o, property.getGetterMethod()); + if (!org.apache.commons.collections4.CollectionUtils.isEmpty(propertyValue)) { + list.addAll(propertyValue); + } + }); targets.put(property.getItemKlass(), list); } else { List list = new ArrayList<>(); @@ -865,7 +871,12 @@ private void collectScanTargets(Map, List> targets) { } objects.forEach( - o -> list.add(ReflectionUtils.invokeMethod(o, property.getGetterMethod()))); + o -> { + Object item = ReflectionUtils.invokeMethod(o, property.getGetterMethod()); + if (item != null) { + list.add(item); + } + }); targets.put(property.getKlass(), list); } } diff --git a/dhis-2/dhis-test-web-api/src/test/java/org/hisp/dhis/webapi/controller/EventVisualizationControllerTest.java b/dhis-2/dhis-test-web-api/src/test/java/org/hisp/dhis/webapi/controller/EventVisualizationControllerTest.java index 46bc6a0354a8..de9afb45426f 100644 --- a/dhis-2/dhis-test-web-api/src/test/java/org/hisp/dhis/webapi/controller/EventVisualizationControllerTest.java +++ b/dhis-2/dhis-test-web-api/src/test/java/org/hisp/dhis/webapi/controller/EventVisualizationControllerTest.java @@ -32,6 +32,7 @@ import static org.hamcrest.MatcherAssert.assertThat; import static org.hisp.dhis.web.HttpStatus.BAD_REQUEST; import static org.hisp.dhis.web.HttpStatus.CREATED; +import static org.hisp.dhis.web.HttpStatus.OK; import static org.hisp.dhis.web.WebClientUtils.assertStatus; import static org.junit.jupiter.api.Assertions.assertEquals; @@ -108,6 +109,27 @@ void testPostForSingleEventDate() { assertThat(nodeMap.get("filters").toString(), not(containsString(eventDateDimension))); } + @Test + void testDelete() { + // Given + String eventDateDimension = "eventDate"; + String eventDate = "2021-07-21_2021-08-01"; + String dimensionBody = + "{'dimension': '" + eventDateDimension + "', 'items': [{'id': '" + eventDate + "'}]}"; + String body = + "{'name': 'Name Test', 'type': 'STACKED_COLUMN','eventRepetitions':null, 'program': {'id':'" + + mockProgram.getUid() + + "'}, 'columns': [" + + dimensionBody + + "]}"; + + // When + String uid = assertStatus(CREATED, POST("/eventVisualizations/", body)); + + // Then + DELETE("/eventVisualizations/" + uid).content(OK); + } + @Test @SuppressWarnings("unchecked") void testPostForMultiEventDates() {