From 9a8457ff343b8579d385b8155b69beaf52b79b3f Mon Sep 17 00:00:00 2001 From: conor Date: Tue, 12 Mar 2024 10:38:06 +0000 Subject: [PATCH] GAP-2462 | implement versioning for section reorder --- .../controllers/ApplicationFormSectionsController.java | 2 +- .../dtos/application/ApplicationSectionOrderPatchDto.java | 2 ++ .../services/ApplicationFormSectionService.java | 4 +++- .../controllers/ApplicationFormSectionsControllerTest.java | 4 ++-- .../services/ApplicationFormSectionServiceTest.java | 6 +++--- 5 files changed, 11 insertions(+), 7 deletions(-) diff --git a/src/main/java/gov/cabinetoffice/gap/adminbackend/controllers/ApplicationFormSectionsController.java b/src/main/java/gov/cabinetoffice/gap/adminbackend/controllers/ApplicationFormSectionsController.java index 8d8d5605..d9f1329e 100644 --- a/src/main/java/gov/cabinetoffice/gap/adminbackend/controllers/ApplicationFormSectionsController.java +++ b/src/main/java/gov/cabinetoffice/gap/adminbackend/controllers/ApplicationFormSectionsController.java @@ -202,7 +202,7 @@ public ResponseEntity updateSectionOrder(final HttpServletRequest reques final @RequestBody ApplicationSectionOrderPatchDto sectionOrderPatchDto) { try { this.applicationFormSectionService.updateSectionOrder(applicationId, sectionOrderPatchDto.getSectionId(), - sectionOrderPatchDto.getIncrement()); + sectionOrderPatchDto.getIncrement(), sectionOrderPatchDto.getVersion()); logApplicationUpdatedEvent(request.getSession().getId(), applicationId); return ResponseEntity.ok().build(); } diff --git a/src/main/java/gov/cabinetoffice/gap/adminbackend/dtos/application/ApplicationSectionOrderPatchDto.java b/src/main/java/gov/cabinetoffice/gap/adminbackend/dtos/application/ApplicationSectionOrderPatchDto.java index 4aac828c..9ac15852 100644 --- a/src/main/java/gov/cabinetoffice/gap/adminbackend/dtos/application/ApplicationSectionOrderPatchDto.java +++ b/src/main/java/gov/cabinetoffice/gap/adminbackend/dtos/application/ApplicationSectionOrderPatchDto.java @@ -13,4 +13,6 @@ public class ApplicationSectionOrderPatchDto { private Integer increment; + private Integer version; + } diff --git a/src/main/java/gov/cabinetoffice/gap/adminbackend/services/ApplicationFormSectionService.java b/src/main/java/gov/cabinetoffice/gap/adminbackend/services/ApplicationFormSectionService.java index 32d1f4d1..ec8b9cda 100644 --- a/src/main/java/gov/cabinetoffice/gap/adminbackend/services/ApplicationFormSectionService.java +++ b/src/main/java/gov/cabinetoffice/gap/adminbackend/services/ApplicationFormSectionService.java @@ -108,11 +108,13 @@ public void updateSectionTitle(final Integer applicationId, final String section this.applicationFormRepository.save(applicationForm); } - public void updateSectionOrder(final Integer applicationId, final String sectionId, final Integer increment) { + public void updateSectionOrder(final Integer applicationId, final String sectionId, final Integer increment, final Integer version) { ApplicationFormEntity applicationForm = this.applicationFormRepository.findById(applicationId) .orElseThrow(() -> new NotFoundException( "Application with id " + applicationId + " does not exist or insufficient permissions")); + ApplicationFormUtils.verifyApplicationFormVersion(version, applicationForm); + List sections = applicationForm.getDefinition().getSections(); ApplicationFormSectionDTO section = applicationForm.getDefinition().getSectionById(sectionId); int index = sections.indexOf(section); diff --git a/src/test/java/gov/cabinetoffice/gap/adminbackend/controllers/ApplicationFormSectionsControllerTest.java b/src/test/java/gov/cabinetoffice/gap/adminbackend/controllers/ApplicationFormSectionsControllerTest.java index c4ec5b61..c31f5cd7 100644 --- a/src/test/java/gov/cabinetoffice/gap/adminbackend/controllers/ApplicationFormSectionsControllerTest.java +++ b/src/test/java/gov/cabinetoffice/gap/adminbackend/controllers/ApplicationFormSectionsControllerTest.java @@ -351,7 +351,7 @@ class updateSectionOrder { void updateSectionOrderHappyPathTest() throws Exception { doNothing().when(ApplicationFormSectionsControllerTest.this.applicationFormSectionService) - .updateSectionOrder(SAMPLE_APPLICATION_ID, "A-random-uuid", 1); + .updateSectionOrder(SAMPLE_APPLICATION_ID, "A-random-uuid", 1, SAMPLE_VERSION); ApplicationSectionOrderPatchDto applicationSectionOrderPatchDto = ApplicationSectionOrderPatchDto.builder() .sectionId("test").increment(1).build(); @@ -375,7 +375,7 @@ void updateSectionOrder_AccessDeniedTest() throws Exception { doThrow(new AccessDeniedException("Error message")) .when(ApplicationFormSectionsControllerTest.this.applicationFormSectionService) - .updateSectionOrder(SAMPLE_APPLICATION_ID, "test", 1); + .updateSectionOrder(SAMPLE_APPLICATION_ID, "test", 1, SAMPLE_VERSION); ApplicationSectionOrderPatchDto applicationSectionOrderPatchDto = ApplicationSectionOrderPatchDto.builder() .sectionId("test").increment(1).build(); diff --git a/src/test/java/gov/cabinetoffice/gap/adminbackend/services/ApplicationFormSectionServiceTest.java b/src/test/java/gov/cabinetoffice/gap/adminbackend/services/ApplicationFormSectionServiceTest.java index 4c16c9a0..88a079fb 100644 --- a/src/test/java/gov/cabinetoffice/gap/adminbackend/services/ApplicationFormSectionServiceTest.java +++ b/src/test/java/gov/cabinetoffice/gap/adminbackend/services/ApplicationFormSectionServiceTest.java @@ -375,7 +375,7 @@ void updateSectionOrderSuccessful() { .thenReturn(Optional.of(testApplicationFormEntity)); ApplicationFormSectionServiceTest.this.applicationFormSectionService.updateSectionOrder(applicationId, - sectionId, increment); + sectionId, increment, SAMPLE_VERSION); Mockito.verify(ApplicationFormSectionServiceTest.this.applicationFormRepository).save(argument.capture()); utilMock.close(); @@ -402,7 +402,7 @@ void updateSectionOrderOutsideOfRange() { .thenReturn(Optional.of(testApplicationFormEntity)); assertThatThrownBy(() -> ApplicationFormSectionServiceTest.this.applicationFormSectionService - .updateSectionOrder(applicationId, sectionId, increment)) + .updateSectionOrder(applicationId, sectionId, increment, SAMPLE_VERSION)) .isInstanceOf(FieldViolationException.class).hasMessage("Section is already at the bottom"); utilMock.close(); @@ -417,7 +417,7 @@ void updateSectionOrderUnauthorised() { final Integer increment = 1; assertThatThrownBy(() -> ApplicationFormSectionServiceTest.this.applicationFormSectionService - .updateSectionOrder(applicationId, sectionId, increment)).isInstanceOf(NotFoundException.class) + .updateSectionOrder(applicationId, sectionId, increment, SAMPLE_VERSION)).isInstanceOf(NotFoundException.class) .hasMessage("Application with id " + applicationId + " does not exist or insufficient permissions");