Skip to content

Commit

Permalink
Refactor the duplicate code and reuse (#174)
Browse files Browse the repository at this point in the history
* Refactor the duplcate code and reuse

* Refactor the duplcate code and reuse
  • Loading branch information
sabahirfan authored Feb 2, 2024
1 parent 639fe9d commit 2263001
Show file tree
Hide file tree
Showing 4 changed files with 78 additions and 21 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down Expand Up @@ -89,11 +91,7 @@ public ResponseEntity<List<DefendantAccountEntity>> getDefendantAccountsByBusine
List<DefendantAccountEntity> response = defendantAccountService
.getDefendantAccountsByBusinessUnit(businessUnit);

if (response == null) {
return ResponseEntity.noContent().build();
}

return ResponseEntity.ok(response);
return buildResponse(response);
}

@GetMapping(value = "/details")
Expand Down Expand Up @@ -162,10 +160,6 @@ public ResponseEntity<List<NoteDto>> getNotesForDefendantAccount(@PathVariable S

List<NoteDto> response = noteService.searchNotes(criteria);

if (response == null) {
return ResponseEntity.noContent().build();
}

return ResponseEntity.ok(response);
return buildResponse(response);
}
}
15 changes: 4 additions & 11 deletions src/main/java/uk/gov/hmcts/opal/controllers/NoteController.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@

import java.util.List;

import static uk.gov.hmcts.opal.util.ResponseUtil.buildResponse;


@RestController
@RequestMapping("/api/notes")
Expand Down Expand Up @@ -54,11 +56,7 @@ public ResponseEntity<List<NoteDto>> getNotesByAssociatedRecord(
List<NoteDto> 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)
Expand All @@ -70,13 +68,8 @@ public ResponseEntity<List<NoteDto>> postNotesSearch(
List<NoteDto> response = noteService
.searchNotes(criteria);

if (response == null) {
return ResponseEntity.noContent().build();
}

return ResponseEntity.ok(response);
return buildResponse(response);
}



}
16 changes: 16 additions & 0 deletions src/main/java/uk/gov/hmcts/opal/util/ResponseUtil.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package uk.gov.hmcts.opal.util;

import org.springframework.http.ResponseEntity;

import java.util.List;

public class ResponseUtil {

public static <T> ResponseEntity<List<T>> buildResponse(List<T> contents) {
if (contents == null || contents.isEmpty()) {
return ResponseEntity.noContent().build();
}

return ResponseEntity.ok(contents);
}
}
54 changes: 54 additions & 0 deletions src/test/java/uk/gov/hmcts/opal/util/ResponseUtilTest.java
Original file line number Diff line number Diff line change
@@ -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<String> responseList = Arrays.asList("item1", "item2", "item3");

// Act
ResponseEntity<List<String>> responseEntity = ResponseUtil.buildResponse(responseList);

// Assert
assertEquals(200, responseEntity.getStatusCode().value());
assertEquals(responseList, responseEntity.getBody());
}

@Test
void buildResponse_withEmptyList_returnsNoContentResponse() {
// Arrange
List<String> responseList = Collections.emptyList();

// Act
ResponseEntity<List<String>> responseEntity = ResponseUtil.buildResponse(responseList);

// Assert
assertEquals(204, responseEntity.getStatusCode().value());
assertNull(responseEntity.getBody());
}

@Test
void buildResponse_withNullList_returnsNoContentResponse() {
// Arrange
List<String> responseList = null;

// Act
ResponseEntity<List<String>> responseEntity = ResponseUtil.buildResponse(responseList);

// Assert
assertEquals(204, responseEntity.getStatusCode().value());
assertNull(responseEntity.getBody());
}
}

0 comments on commit 2263001

Please sign in to comment.