Skip to content

Commit

Permalink
S28 2965: Undelete Recording Cascade (#514)
Browse files Browse the repository at this point in the history
  • Loading branch information
lucas-phillips28 authored May 23, 2024
1 parent 60b4ee5 commit 9416ea1
Show file tree
Hide file tree
Showing 8 changed files with 93 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -327,6 +327,7 @@ void createBookingWithParticipantNotInCase() throws JsonProcessingException {
@DisplayName("Scenario: Restore booking")
@Test
void undeleteBooking() throws JsonProcessingException {
// create booking
var caseEntity = createCase();
var participants = Set.of(
createParticipant(ParticipantType.WITNESS),
Expand All @@ -345,14 +346,19 @@ void undeleteBooking() throws JsonProcessingException {
var putResponse = putBooking(booking);
assertResponseCode(putResponse, 201);
assertBookingExists(booking.getId(), true);
assertCaseExists(caseEntity.getId(), true);

var deleteResponse = doDeleteRequest(BOOKINGS_ENDPOINT + "/" + booking.getId(), true);
assertResponseCode(deleteResponse, 204);
// delete case (and associated booking)
var deleteResponse = doDeleteRequest(CASES_ENDPOINT + "/" + caseEntity.getId(), true);
assertResponseCode(deleteResponse, 200);
assertBookingExists(booking.getId(), false);
assertCaseExists(caseEntity.getId(), false);

// undelete booking
var undeleteResponse = doPostRequest(BOOKINGS_ENDPOINT + "/" + booking.getId() + "/undelete", true);
assertResponseCode(undeleteResponse, 200);
assertBookingExists(booking.getId(), true);
assertCaseExists(caseEntity.getId(), true);
}

private CreateBookingDTO createBooking(UUID caseId, Set<CreateParticipantDTO> participants) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import io.restassured.response.Response;
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test;
import uk.gov.hmcts.reform.preapi.dto.BookingDTO;
import uk.gov.hmcts.reform.preapi.dto.CreateCaptureSessionDTO;
import uk.gov.hmcts.reform.preapi.enums.RecordingOrigin;
import uk.gov.hmcts.reform.preapi.enums.RecordingStatus;
Expand Down Expand Up @@ -114,18 +115,28 @@ void shouldCreateCaptureSession() throws JsonProcessingException {
@DisplayName("Scenario: Restore capture session")
@Test
void undeleteCaptureSession() throws JsonProcessingException {
// create capture session
var dto = createCaptureSession();
var putResponse = putCaptureSession(dto);
assertResponseCode(putResponse, 201);
assertCaptureSessionExists(dto.getId(), true);
var bookingResponse = assertBookingExists(dto.getBookingId(), true);
var caseId = bookingResponse.as(BookingDTO.class).getCaseDTO().getId();
assertCaseExists(caseId, true);

var deleteResponse = doDeleteRequest(CAPTURE_SESSIONS_ENDPOINT + "/" + dto.getId(), true);
// delete case (and associated bookings + capture session)
var deleteResponse = doDeleteRequest(CASES_ENDPOINT + "/" + caseId, true);
assertResponseCode(deleteResponse, 200);
assertCaptureSessionExists(dto.getId(), false);
assertBookingExists(dto.getBookingId(), false);
assertCaseExists(caseId, false);

// undelete capture session
var undeleteResponse = doPostRequest(CAPTURE_SESSIONS_ENDPOINT + "/" + dto.getId() + "/undelete", true);
assertResponseCode(undeleteResponse, 200);
assertCaptureSessionExists(dto.getId(), true);
assertBookingExists(dto.getBookingId(), true);
assertCaseExists(caseId, true);
}

private CreateCaptureSessionDTO createCaptureSession(UUID bookingId) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
import static org.assertj.core.api.Assertions.assertThat;

public class RecordingControllerFT extends FunctionalTestBase {
private record CreateRecordingResponse(UUID bookingId, UUID captureSessionId, UUID recordingId) {
private record CreateRecordingResponse(UUID caseId, UUID bookingId, UUID captureSessionId, UUID recordingId) {
}

@DisplayName("Scenario: Restore recording")
Expand Down Expand Up @@ -80,6 +80,40 @@ void deleteRecordingThatDoesntExist() {
assertResponseCode(deleteResponse, 404);
}

@DisplayName("Undelete a recording should cascade to associated capture sessions, bookings and cases")
@Test
void shouldUndeleteRecording() throws JsonProcessingException {
// create recording
var recordingDetails = createRecording();
assertRecordingExists(recordingDetails.recordingId, true);
assertCaptureSessionExists(recordingDetails.captureSessionId, true);
assertBookingExists(recordingDetails.bookingId, true);
assertCaseExists(recordingDetails.caseId, true);

// must delete all recordings associated to case before deleting case
var deleteRecording = doDeleteRequest(RECORDINGS_ENDPOINT + "/" + recordingDetails.recordingId, true);
assertResponseCode(deleteRecording, 200);

// delete case (deleting associated bookings + capture sessions)
var deleteCase = doDeleteRequest(CASES_ENDPOINT + "/" + recordingDetails.caseId, true);
assertResponseCode(deleteCase, 200);
assertRecordingExists(recordingDetails.recordingId, false);
assertCaptureSessionExists(recordingDetails.captureSessionId, false);
assertBookingExists(recordingDetails.bookingId, false);
assertCaseExists(recordingDetails.caseId, false);

// undelete recording (and associated capture session, booking, case)
var undeleteRecording = doPostRequest(
RECORDINGS_ENDPOINT + "/" + recordingDetails.recordingId + "/undelete",
true
);
assertResponseCode(undeleteRecording, 200);
assertRecordingExists(recordingDetails.recordingId, true);
assertCaptureSessionExists(recordingDetails.captureSessionId, true);
assertBookingExists(recordingDetails.bookingId, true);
assertCaseExists(recordingDetails.caseId, true);
}

private CreateRecordingDTO createRecording(UUID captureSessionId) {
var dto = new CreateRecordingDTO();
dto.setId(UUID.randomUUID());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -258,6 +258,7 @@ public ResponseEntity<Map<String, String>> shouldDeleteRecordingsForBooking() {

var response = new HashMap<String, String>() {
{
put("caseId", caseEntity.getId().toString());
put("bookingId", booking.getId().toString());
put("recordingId", recording.getId().toString());
put("captureSessionId", captureSession.getId().toString());
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package uk.gov.hmcts.reform.preapi.services;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Lazy;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.security.access.prepost.PreAuthorize;
Expand Down Expand Up @@ -33,24 +34,26 @@
@SuppressWarnings("PMD.SingularField")
public class BookingService {


private final BookingRepository bookingRepository;
private final ParticipantRepository participantRepository;
private final CaseRepository caseRepository;
private final CaptureSessionService captureSessionService;
private final ShareBookingService shareBookingService;
private final CaseService caseService;

@Autowired
public BookingService(final BookingRepository bookingRepository,
final CaseRepository caseRepository,
final ParticipantRepository participantRepository,
final CaptureSessionService captureSessionService,
final ShareBookingService shareBookingService) {
final ShareBookingService shareBookingService,
@Lazy CaseService caseService) {
this.bookingRepository = bookingRepository;
this.participantRepository = participantRepository;
this.caseRepository = caseRepository;
this.captureSessionService = captureSessionService;
this.shareBookingService = shareBookingService;
this.caseService = caseService;
}

@PreAuthorize("@authorisationService.hasBookingAccess(authentication, #id)")
Expand Down Expand Up @@ -171,10 +174,11 @@ public void markAsDeleted(UUID id) {
bookingRepository.deleteById(id);
}

@Transactional
@Transactional(propagation = Propagation.REQUIRED, rollbackFor = Exception.class)
@PreAuthorize("@authorisationService.hasBookingAccess(authentication, #id)")
public void undelete(UUID id) {
var entity = bookingRepository.findById(id).orElseThrow(() -> new NotFoundException("Booking: " + id));
caseService.undelete(entity.getCaseId().getId());
if (!entity.isDeleted()) {
return;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package uk.gov.hmcts.reform.preapi.services;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Lazy;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.security.access.prepost.PreAuthorize;
Expand Down Expand Up @@ -34,16 +35,19 @@ public class CaptureSessionService {
private final CaptureSessionRepository captureSessionRepository;
private final BookingRepository bookingRepository;
private final UserRepository userRepository;
private final BookingService bookingService;

@Autowired
public CaptureSessionService(RecordingService recordingService,
CaptureSessionRepository captureSessionRepository,
BookingRepository bookingRepository,
UserRepository userRepository) {
UserRepository userRepository,
@Lazy BookingService bookingService) {
this.recordingService = recordingService;
this.captureSessionRepository = captureSessionRepository;
this.bookingRepository = bookingRepository;
this.userRepository = userRepository;
this.bookingService = bookingService;
}

@Transactional
Expand Down Expand Up @@ -159,6 +163,7 @@ public UpsertResult upsert(CreateCaptureSessionDTO createCaptureSessionDTO) {
public void undelete(UUID id) {
var entity =
captureSessionRepository.findById(id).orElseThrow(() -> new NotFoundException("Capture Session: " + id));
bookingService.undelete(entity.getBooking().getId());
if (!entity.isDeleted()) {
return;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,9 @@ class BookingServiceTest {
@MockBean
private ShareBookingService shareBookingService;

@MockBean
private CaseService caseService;

@Autowired
private BookingService bookingService;

Expand Down Expand Up @@ -439,29 +442,38 @@ void deleteCascadeSuccess() {
@DisplayName("Should undelete a booking successfully when booking is marked as deleted")
@Test
void undeleteSuccess() {
var aCase = new Case();
aCase.setId(UUID.randomUUID());
aCase.setDeletedAt(Timestamp.from(Instant.now()));
var booking = new Booking();
booking.setId(UUID.randomUUID());
booking.setDeletedAt(Timestamp.from(Instant.now()));
booking.setCaseId(aCase);

when(bookingRepository.findById(booking.getId())).thenReturn(Optional.of(booking));

bookingService.undelete(booking.getId());

verify(bookingRepository, times(1)).findById(booking.getId());
verify(caseService, times(1)).undelete(aCase.getId());
verify(bookingRepository, times(1)).save(booking);
}

@DisplayName("Should do nothing when booking is not deleted")
@Test
void undeleteNotDeletedSuccess() {
var aCase = new Case();
aCase.setId(UUID.randomUUID());
var booking = new Booking();
booking.setId(UUID.randomUUID());
booking.setCaseId(aCase);

when(bookingRepository.findById(booking.getId())).thenReturn(Optional.of(booking));

bookingService.undelete(booking.getId());

verify(bookingRepository, times(1)).findById(booking.getId());
verify(caseService, times(1)).undelete(aCase.getId());
verify(bookingRepository,never()).save(booking);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,9 @@ public class CaptureSessionServiceTest {
@MockBean
private UserRepository userRepository;

@MockBean
private BookingService bookingService;

@Autowired
private CaptureSessionService captureSessionService;

Expand Down Expand Up @@ -447,29 +450,38 @@ void createCaptureSessionFinishedByNotFound() {
@DisplayName("Should undelete a capture session successfully when capture session is marked as deleted")
@Test
void undeleteSuccess() {
var booking = new Booking();
booking.setId(UUID.randomUUID());
booking.setDeletedAt(Timestamp.from(Instant.now()));
var captureSession = new CaptureSession();
captureSession.setId(UUID.randomUUID());
captureSession.setDeletedAt(Timestamp.from(Instant.now()));
captureSession.setBooking(booking);

when(captureSessionRepository.findById(captureSession.getId())).thenReturn(Optional.of(captureSession));

captureSessionService.undelete(captureSession.getId());

verify(captureSessionRepository, times(1)).findById(captureSession.getId());
verify(bookingService, times(1)).undelete(booking.getId());
verify(captureSessionRepository, times(1)).save(captureSession);
}

@DisplayName("Should do nothing when capture session is not deleted")
@Test
void undeleteNotDeletedSuccess() {
var booking = new Booking();
booking.setId(UUID.randomUUID());
var captureSession = new CaptureSession();
captureSession.setId(UUID.randomUUID());
captureSession.setBooking(booking);

when(captureSessionRepository.findById(captureSession.getId())).thenReturn(Optional.of(captureSession));

captureSessionService.undelete(captureSession.getId());

verify(captureSessionRepository, times(1)).findById(captureSession.getId());
verify(bookingService, times(1)).undelete(booking.getId());
verify(captureSessionRepository, never()).save(captureSession);
}

Expand Down

0 comments on commit 9416ea1

Please sign in to comment.