Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Po 47 create note #46

Merged
merged 5 commits into from
Nov 21, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 32 additions & 0 deletions src/main/java/uk/gov/hmcts/opal/controllers/NoteController.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
package uk.gov.hmcts.opal.controllers;

import io.swagger.v3.oas.annotations.Operation;
import lombok.RequiredArgsConstructor;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import uk.gov.hmcts.opal.dto.NoteDto;
import uk.gov.hmcts.opal.service.NoteService;



@RestController
@RequestMapping("/api/notes")
@RequiredArgsConstructor
public class NoteController {

private final NoteService noteService;

@PostMapping
@Operation(summary = "Creates a new note in the Opal Fines Notes table assigning an ID.")
public ResponseEntity<NoteDto> createNote(@RequestBody NoteDto noteDto) {
NoteDto savedNoteDto = noteService.saveNote(noteDto);
return new ResponseEntity<>(savedNoteDto, HttpStatus.CREATED);
}



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


import lombok.Builder;
import lombok.Data;

import java.time.LocalDateTime;

@Data
@Builder
public class NoteDto {
private Long noteId;
private String noteType;
private String associatedRecordType;
private String associatedRecordId;
private String noteText;
private LocalDateTime postedDate;
private String postedBy;

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

import jakarta.persistence.Column;
import jakarta.persistence.Entity;
import jakarta.persistence.GeneratedValue;
import jakarta.persistence.GenerationType;
import jakarta.persistence.Id;
import jakarta.persistence.SequenceGenerator;
import jakarta.persistence.Table;
import lombok.Builder;
import lombok.Data;

import java.time.LocalDateTime;

@Entity
@Table(name = "notes")
@Data
@Builder
public class NoteEntity {

@Id
@GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "note_id_seq_generator")
@SequenceGenerator(name = "note_id_seq_generator", sequenceName = "note_id_seq", allocationSize = 1)
private Long noteId;

@Column(name = "note_type", length = 2)
private String noteType;

@Column(name = "associated_record_type", length = 30)
private String associatedRecordType;

@Column(name = "associated_record_id", length = 30)
private String associatedRecordId;

@Column(name = "note_text", columnDefinition = "TEXT")
private String noteText;

@Column(name = "posted_date")
private LocalDateTime postedDate;

@Column(name = "posted_by", length = 20)
private String postedBy;

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

import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.stereotype.Repository;
import uk.gov.hmcts.opal.entity.NoteEntity;

@Repository
public interface NoteRepository extends JpaRepository<NoteEntity, Long> {



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


import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import uk.gov.hmcts.opal.dto.NoteDto;
import uk.gov.hmcts.opal.entity.NoteEntity;
import uk.gov.hmcts.opal.repository.NoteRepository;

import java.time.LocalDateTime;

@Service
@RequiredArgsConstructor
public class NoteService {


private final NoteRepository noteRepository;

public NoteDto saveNote(NoteDto noteDto) {
NoteEntity noteEntity = NoteEntity.builder()
.noteType(noteDto.getNoteType())
.associatedRecordType(noteDto.getAssociatedRecordType())
.associatedRecordId(noteDto.getAssociatedRecordId())
.noteText(noteDto.getNoteText())
.postedDate(noteDto.getPostedDate() == null ? LocalDateTime.now() : noteDto.getPostedDate())
.postedBy(noteDto.getPostedBy())
.build();

NoteEntity savedNoteEntity = noteRepository.save(noteEntity);

return NoteDto.builder()
.noteId(savedNoteEntity.getNoteId()) // This will be the generated ID
.noteType(savedNoteEntity.getNoteType())
.associatedRecordType(savedNoteEntity.getAssociatedRecordType())
.associatedRecordId(savedNoteEntity.getAssociatedRecordId())
.noteText(savedNoteEntity.getNoteText())
.postedDate(savedNoteEntity.getPostedDate())
.postedBy(savedNoteEntity.getPostedBy())
.build();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
package uk.gov.hmcts.opal.controllers;

import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.mockito.InjectMocks;
import org.mockito.Mock;
import org.mockito.junit.jupiter.MockitoExtension;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import uk.gov.hmcts.opal.dto.NoteDto;
import uk.gov.hmcts.opal.service.NoteService;

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.Mockito.times;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;

@ExtendWith(MockitoExtension.class)
class NoteControllerTest {

@Mock
private NoteService noteService;

@InjectMocks
private NoteController noteController;

@Test
void testCreateNote_Success() {
// Arrange
NoteDto noteDtoRequest = NoteDto.builder().build();
NoteDto noteDtoResponse = NoteDto.builder().noteId(1L).build(); //some id assigned by db sequence

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

// Act
ResponseEntity<NoteDto> response = noteController.createNote(noteDtoRequest);

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


}
97 changes: 97 additions & 0 deletions src/test/java/uk/gov/hmcts/opal/service/NoteServiceTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
package uk.gov.hmcts.opal.service;

import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.mockito.InjectMocks;
import org.mockito.Mock;
import org.mockito.junit.jupiter.MockitoExtension;
import uk.gov.hmcts.opal.dto.NoteDto;
import uk.gov.hmcts.opal.entity.NoteEntity;
import uk.gov.hmcts.opal.repository.NoteRepository;

import java.time.LocalDateTime;

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertTrue;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.Mockito.times;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;

@ExtendWith(MockitoExtension.class)
class NoteServiceTest {

@Mock
private NoteRepository noteRepository;

@InjectMocks
private NoteService noteService;

@Test
void testSaveNoteWithPostedDate() {
// Arrange
LocalDateTime postedDate = LocalDateTime.now();

NoteDto noteDto = NoteDto.builder()
.noteType("AC")
.associatedRecordType("defendants_accounts")
.associatedRecordId("123")
.noteText("Sample note")
.postedDate(postedDate)
.postedBy("user123")
.build();

NoteEntity noteEntity = NoteEntity.builder()
.noteId(1L)
.noteType("AC")
.associatedRecordType("defendants_accounts")
.associatedRecordId("123")
.noteText("Sample note")
.postedDate(postedDate)
.postedBy("user123")
.build();

when(noteRepository.save(any(NoteEntity.class))).thenReturn(noteEntity);

// Act
NoteDto savedNoteDto = noteService.saveNote(noteDto);

// Assert
assertEquals(noteEntity.getNoteId(), savedNoteDto.getNoteId());
verify(noteRepository, times(1)).save(any(NoteEntity.class));
}

@Test
void testSaveNoteWithNullPostedDate() {
// Arrange
NoteDto noteDto = NoteDto.builder()
.noteType("AC")
.associatedRecordType("defendants_accounts")
.associatedRecordId("123")
.noteText("Sample note")
.postedDate(null)
.postedBy("user123")
.build();

NoteEntity noteEntity = NoteEntity.builder().noteId(1L).build();

when(noteRepository.save(any(NoteEntity.class))).thenAnswer(invocation -> {
NoteEntity entity = invocation.getArgument(0);
entity.setNoteId(1L); // Set ID to simulate repository behavior
return entity;
});

// Act
NoteDto savedNoteDto = noteService.saveNote(noteDto);

// Assert
Assertions.assertNotNull(savedNoteDto.getPostedDate());
assertTrue(savedNoteDto.getPostedDate().isBefore(LocalDateTime.now().plusMinutes(1))
&& savedNoteDto.getPostedDate().isAfter(LocalDateTime.now().minusMinutes(1)),
"Posted date should be around the current time");
verify(noteRepository, times(1)).save(any(NoteEntity.class));
}


}