diff --git a/src/main/java/uk/gov/hmcts/opal/controllers/DefendantAccountController.java b/src/main/java/uk/gov/hmcts/opal/controllers/DefendantAccountController.java index 8c059a1e7..6729526dc 100644 --- a/src/main/java/uk/gov/hmcts/opal/controllers/DefendantAccountController.java +++ b/src/main/java/uk/gov/hmcts/opal/controllers/DefendantAccountController.java @@ -30,6 +30,8 @@ import java.time.LocalDateTime; import java.util.List; +import static uk.gov.hmcts.opal.util.ResponseUtil.buildResponse; + @RestController @RequestMapping("/api/defendant-account") @Slf4j(topic = "DefendantAccountController") @@ -89,11 +91,7 @@ public ResponseEntity> getDefendantAccountsByBusine List response = defendantAccountService .getDefendantAccountsByBusinessUnit(businessUnit); - if (response == null) { - return ResponseEntity.noContent().build(); - } - - return ResponseEntity.ok(response); + return buildResponse(response); } @GetMapping(value = "/details") @@ -162,10 +160,6 @@ public ResponseEntity> getNotesForDefendantAccount(@PathVariable S List response = noteService.searchNotes(criteria); - if (response == null) { - return ResponseEntity.noContent().build(); - } - - return ResponseEntity.ok(response); + return buildResponse(response); } } diff --git a/src/main/java/uk/gov/hmcts/opal/controllers/NoteController.java b/src/main/java/uk/gov/hmcts/opal/controllers/NoteController.java index 43b9e9875..5040ed950 100644 --- a/src/main/java/uk/gov/hmcts/opal/controllers/NoteController.java +++ b/src/main/java/uk/gov/hmcts/opal/controllers/NoteController.java @@ -19,6 +19,8 @@ import java.util.List; +import static uk.gov.hmcts.opal.util.ResponseUtil.buildResponse; + @RestController @RequestMapping("/api/notes") @@ -54,11 +56,7 @@ public ResponseEntity> getNotesByAssociatedRecord( List response = noteService .searchNotes(criteria); - if (response == null) { - return ResponseEntity.noContent().build(); - } - - return ResponseEntity.ok(response); + return buildResponse(response); } @PostMapping(value = "/search", consumes = MediaType.APPLICATION_JSON_VALUE) @@ -70,13 +68,8 @@ public ResponseEntity> postNotesSearch( List response = noteService .searchNotes(criteria); - if (response == null) { - return ResponseEntity.noContent().build(); - } - - return ResponseEntity.ok(response); + return buildResponse(response); } - } diff --git a/src/main/java/uk/gov/hmcts/opal/util/ResponseUtil.java b/src/main/java/uk/gov/hmcts/opal/util/ResponseUtil.java new file mode 100644 index 000000000..f6baf72b0 --- /dev/null +++ b/src/main/java/uk/gov/hmcts/opal/util/ResponseUtil.java @@ -0,0 +1,16 @@ +package uk.gov.hmcts.opal.util; + +import org.springframework.http.ResponseEntity; + +import java.util.List; + +public class ResponseUtil { + + public static ResponseEntity> buildResponse(List contents) { + if (contents == null || contents.isEmpty()) { + return ResponseEntity.noContent().build(); + } + + return ResponseEntity.ok(contents); + } +} diff --git a/src/test/java/uk/gov/hmcts/opal/util/ResponseUtilTest.java b/src/test/java/uk/gov/hmcts/opal/util/ResponseUtilTest.java new file mode 100644 index 000000000..864a75bcd --- /dev/null +++ b/src/test/java/uk/gov/hmcts/opal/util/ResponseUtilTest.java @@ -0,0 +1,54 @@ +package uk.gov.hmcts.opal.util; + +import org.junit.jupiter.api.Test; +import org.springframework.http.ResponseEntity; + +import java.util.Arrays; +import java.util.Collections; +import java.util.List; + +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertNull; + +class ResponseUtilTest { + + @Test + void buildResponse_withNonNullList_returnsOkResponse() { + // Arrange + List responseList = Arrays.asList("item1", "item2", "item3"); + + // Act + ResponseEntity> responseEntity = ResponseUtil.buildResponse(responseList); + + // Assert + assertEquals(200, responseEntity.getStatusCode().value()); + assertEquals(responseList, responseEntity.getBody()); + } + + @Test + void buildResponse_withEmptyList_returnsNoContentResponse() { + // Arrange + List responseList = Collections.emptyList(); + + // Act + ResponseEntity> responseEntity = ResponseUtil.buildResponse(responseList); + + // Assert + assertEquals(204, responseEntity.getStatusCode().value()); + assertNull(responseEntity.getBody()); + } + + @Test + void buildResponse_withNullList_returnsNoContentResponse() { + // Arrange + List responseList = null; + + // Act + ResponseEntity> responseEntity = ResponseUtil.buildResponse(responseList); + + // Assert + assertEquals(204, responseEntity.getStatusCode().value()); + assertNull(responseEntity.getBody()); + } +} +