Skip to content

Commit

Permalink
Add new 'addNote' endpoint for DefendantAccounts
Browse files Browse the repository at this point in the history
  • Loading branch information
RustyHMCTS committed Jan 30, 2024
1 parent dd56f1e commit d11984b
Show file tree
Hide file tree
Showing 3 changed files with 95 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
Expand All @@ -18,9 +19,13 @@
import uk.gov.hmcts.opal.dto.AccountEnquiryDto;
import uk.gov.hmcts.opal.dto.AccountSearchDto;
import uk.gov.hmcts.opal.dto.AccountSearchResultsDto;
import uk.gov.hmcts.opal.dto.AddNoteDto;
import uk.gov.hmcts.opal.dto.NoteDto;
import uk.gov.hmcts.opal.entity.DefendantAccountEntity;
import uk.gov.hmcts.opal.service.DefendantAccountServiceInterface;
import uk.gov.hmcts.opal.service.NoteServiceInterface;

import java.time.LocalDateTime;
import java.util.List;

@RestController
Expand All @@ -29,11 +34,18 @@
@Tag(name = "Defendant Account Controller")
public class DefendantAccountController {

public static final String NOTE_ASSOC_REC_TYPE = "defendant_accounts";

private final DefendantAccountServiceInterface defendantAccountService;

public DefendantAccountController(@Qualifier("defendantAccountServiceProxy")
DefendantAccountServiceInterface defendantAccountService) {
private final NoteServiceInterface noteService;

public DefendantAccountController(
@Qualifier("defendantAccountServiceProxy") DefendantAccountServiceInterface defendantAccountService,
@Qualifier("noteServiceProxy") NoteServiceInterface noteService) {

this.defendantAccountService = defendantAccountService;
this.noteService = noteService;
}

@GetMapping
Expand Down Expand Up @@ -110,4 +122,28 @@ public ResponseEntity<AccountSearchResultsDto> postDefendantAccountSearch(

return ResponseEntity.ok(response);
}

@PostMapping(value = "/addNote", consumes = MediaType.APPLICATION_JSON_VALUE)
@Operation(summary = "Adds a single note associated with the defendant account")
public ResponseEntity<NoteDto> addNote(
@RequestBody AddNoteDto addNote) {
log.info(":POST:addNote: {}", addNote.toPrettyJson());

NoteDto noteDto = NoteDto.builder()
.associatedRecordId(addNote.getAssociatedRecordId())
.noteText(addNote.getNoteText())
.associatedRecordType(NOTE_ASSOC_REC_TYPE)
.noteType("AA") // TODO - This will probably need to part of the AddNoteDto in future
.postedBy("USER") // TODO - need to get this from the logged in user
.postedDate(LocalDateTime.now())
.build();

NoteDto response = noteService.saveNote(noteDto);

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

return new ResponseEntity<>(response, HttpStatus.CREATED);
}
}
18 changes: 18 additions & 0 deletions src/main/java/uk/gov/hmcts/opal/dto/AddNoteDto.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package uk.gov.hmcts.opal.dto;


import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;
import lombok.extern.jackson.Jacksonized;

@Data
@Builder
@NoArgsConstructor
@AllArgsConstructor
@Jacksonized
public class AddNoteDto implements ToJsonString {
private String associatedRecordId;
private String noteText;
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,11 @@
import uk.gov.hmcts.opal.dto.AccountEnquiryDto;
import uk.gov.hmcts.opal.dto.AccountSearchDto;
import uk.gov.hmcts.opal.dto.AccountSearchResultsDto;
import uk.gov.hmcts.opal.dto.AddNoteDto;
import uk.gov.hmcts.opal.dto.NoteDto;
import uk.gov.hmcts.opal.entity.DefendantAccountEntity;
import uk.gov.hmcts.opal.service.DefendantAccountService;
import uk.gov.hmcts.opal.service.NoteService;

import java.util.List;

Expand All @@ -28,6 +31,9 @@ class DefendantAccountControllerTest {
@Mock
private DefendantAccountService defendantAccountService;

@Mock
private NoteService noteService;

@InjectMocks
private DefendantAccountController defendantAccountController;

Expand Down Expand Up @@ -139,4 +145,37 @@ public void testPostDefendantAccountSearch_Success() {
verify(defendantAccountService, times(1)).searchDefendantAccounts(any(
AccountSearchDto.class));
}

@Test
public void testAddNote_Success() {
// Arrange
NoteDto mockResponse = new NoteDto();

when(noteService.saveNote(any(NoteDto.class))).thenReturn(mockResponse);

// Act
AddNoteDto addNote = AddNoteDto.builder().build();
ResponseEntity<NoteDto> responseEntity = defendantAccountController.addNote(addNote);

// Assert
assertEquals(HttpStatus.CREATED, responseEntity.getStatusCode());
assertEquals(mockResponse, responseEntity.getBody());
verify(noteService, times(1)).saveNote(any(
NoteDto.class));
}

@Test
public void testAddNote_NoContent() {

when(noteService.saveNote(any(NoteDto.class))).thenReturn(null);

// Act
AddNoteDto addNote = AddNoteDto.builder().build();
ResponseEntity<NoteDto> responseEntity = defendantAccountController.addNote(addNote);

// Assert
assertEquals(HttpStatus.NO_CONTENT, responseEntity.getStatusCode());
verify(noteService, times(1)).saveNote(any(
NoteDto.class));
}
}

0 comments on commit d11984b

Please sign in to comment.