From 949f52aa751f4253cd0790d2c266837d495006ed Mon Sep 17 00:00:00 2001 From: Ayoub LABIDI <117761394+ayolab@users.noreply.github.com> Date: Mon, 27 Jan 2025 13:31:01 +0100 Subject: [PATCH] Add error handling for report duplication (#105) Signed-off-by: Ayoub LABIDI --- .../gridsuite/report/server/ReportController.java | 13 +++++++++++-- .../report/server/ReportControllerTest.java | 7 +++++++ 2 files changed, 18 insertions(+), 2 deletions(-) diff --git a/src/main/java/org/gridsuite/report/server/ReportController.java b/src/main/java/org/gridsuite/report/server/ReportController.java index a5091a6..5f7420c 100644 --- a/src/main/java/org/gridsuite/report/server/ReportController.java +++ b/src/main/java/org/gridsuite/report/server/ReportController.java @@ -16,11 +16,13 @@ import org.gridsuite.report.server.dto.Report; import org.gridsuite.report.server.dto.ReportLog; import org.springframework.dao.EmptyResultDataAccessException; +import org.springframework.http.HttpStatus; import org.springframework.http.MediaType; import org.springframework.http.ResponseEntity; import org.springframework.web.bind.annotation.*; import java.util.List; +import java.util.NoSuchElementException; import java.util.Set; import java.util.UUID; @@ -87,9 +89,16 @@ public void createReport(@PathVariable("id") UUID id, @RequestBody ReportNode re @PostMapping(value = "reports/{id}/duplicate", consumes = MediaType.APPLICATION_JSON_VALUE) @Operation(summary = "Duplicate a report") - @ApiResponses(value = {@ApiResponse(responseCode = "200", description = "The report has been duplicated")}) + @ApiResponses(value = { + @ApiResponse(responseCode = "200", description = "The report has been duplicated"), + @ApiResponse(responseCode = "404", description = "Report not found") + }) public ResponseEntity duplicateReport(@PathVariable("id") UUID id) { - return ResponseEntity.ok(service.duplicateReport(id)); + try { + return ResponseEntity.ok(service.duplicateReport(id)); + } catch (NoSuchElementException e) { + return ResponseEntity.status(HttpStatus.NOT_FOUND).build(); + } } @DeleteMapping(value = "reports/{id}") diff --git a/src/test/java/org/gridsuite/report/server/ReportControllerTest.java b/src/test/java/org/gridsuite/report/server/ReportControllerTest.java index bb25213..6f3131d 100644 --- a/src/test/java/org/gridsuite/report/server/ReportControllerTest.java +++ b/src/test/java/org/gridsuite/report/server/ReportControllerTest.java @@ -237,6 +237,13 @@ public void testDuplicateReport() throws Exception { assertReportsAreEqualIgnoringIds(resultAfterDuplication, toString(EXPECTED_REPORT_ONE)); } + @Test + public void testDuplicateReportNotFound() throws Exception { + mvc.perform(post(URL_TEMPLATE + "/reports/" + REPORT_UUID + "/duplicate") + .contentType(APPLICATION_JSON)) + .andExpect(status().isNotFound()); + } + @Test public void testDeleteReport() throws Exception { String testReport1 = toString(REPORT_ONE);