Skip to content

Commit

Permalink
Multiple editors - handle editing recently deleted section (#265)
Browse files Browse the repository at this point in the history
* throw multiple editors exception on retrieve question
  • Loading branch information
john-tco authored Mar 27, 2024
1 parent 2eb1c5b commit ee1b0b0
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@
import gov.cabinetoffice.gap.adminbackend.enums.ResponseTypeEnum;
import gov.cabinetoffice.gap.adminbackend.enums.SessionObjectEnum;
import gov.cabinetoffice.gap.adminbackend.exceptions.ApplicationFormException;
import gov.cabinetoffice.gap.adminbackend.exceptions.ConflictException;
import gov.cabinetoffice.gap.adminbackend.exceptions.FieldViolationException;
import gov.cabinetoffice.gap.adminbackend.exceptions.NotFoundException;
import gov.cabinetoffice.gap.adminbackend.mappers.ApplicationFormMapper;
Expand Down Expand Up @@ -238,13 +237,8 @@ public String addQuestionToApplicationForm(Integer applicationId, String section
String questionId = UUID.randomUUID().toString();

this.applicationFormRepository.findById(applicationId).ifPresentOrElse(applicationForm -> {
ApplicationFormSectionDTO sectionDTO;
try {
sectionDTO = applicationForm.getDefinition().getSectionById(sectionId);
} catch (NotFoundException e) {
//If the section does not exist here it must have been recently deleted by another editor.
throw new ConflictException("MULTIPLE_EDITORS_SECTION_DELETED");
}
ApplicationFormSectionDTO sectionDTO =
ApplicationFormUtils.verifyAndGetApplicationFormSection(applicationForm, sectionId);

ApplicationFormQuestionDTO applicationFormQuestionDTO;
QuestionAbstractPostDTO questionAbstractPostDTO = validatePostQuestion(question);
Expand Down Expand Up @@ -328,7 +322,9 @@ public ApplicationFormQuestionDTO retrieveQuestion(Integer applicationId, String
ApplicationFormEntity applicationForm = this.applicationFormRepository.findById(applicationId)
.orElseThrow(() -> new NotFoundException("Application with id " + applicationId + " does not exist"));

return applicationForm.getDefinition().getSectionById(sectionId).getQuestionById(questionId);
ApplicationFormSectionDTO sectionDTO = ApplicationFormUtils.verifyAndGetApplicationFormSection(applicationForm, sectionId);

return sectionDTO.getQuestionById(questionId);

}

Expand Down Expand Up @@ -374,10 +370,11 @@ public void updateQuestionOrder(final Integer applicationId, final String sectio
.orElseThrow(() -> new NotFoundException(
"Application with id " + applicationId + " does not exist or insufficient permissions"));

final ApplicationFormSectionDTO section = ApplicationFormUtils.verifyAndGetApplicationFormSection(applicationForm, sectionId);
ApplicationFormUtils.verifyApplicationFormVersion(version, applicationForm);

final List<ApplicationFormSectionDTO> sections = applicationForm.getDefinition().getSections();
final ApplicationFormSectionDTO section = applicationForm.getDefinition().getSectionById(sectionId);

final List<ApplicationFormQuestionDTO> questions = section.getQuestions();
final ApplicationFormQuestionDTO question = section.getQuestionById(questionId);

Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
package gov.cabinetoffice.gap.adminbackend.utils;

import gov.cabinetoffice.gap.adminbackend.dtos.application.ApplicationFormSectionDTO;
import gov.cabinetoffice.gap.adminbackend.entities.ApplicationFormEntity;
import gov.cabinetoffice.gap.adminbackend.exceptions.ConflictException;
import gov.cabinetoffice.gap.adminbackend.exceptions.NotFoundException;
import gov.cabinetoffice.gap.adminbackend.models.AdminSession;

import java.time.Instant;
Expand All @@ -23,4 +25,14 @@ public static void verifyApplicationFormVersion(Integer version, ApplicationForm
}
}

public static ApplicationFormSectionDTO verifyAndGetApplicationFormSection(ApplicationFormEntity applicationForm, String sectionId) {
ApplicationFormSectionDTO sectionDTO;
try {
sectionDTO = applicationForm.getDefinition().getSectionById(sectionId);
} catch (NotFoundException e) {
// If the section is not found it must have recently been deleted by another editor.
throw new ConflictException("MULTIPLE_EDITORS_SECTION_DELETED");
}
return sectionDTO;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -403,6 +403,9 @@ void addNewQuestionValuesGenericHappyPathTest() {
.thenReturn(Optional.of(SAMPLE_APPLICATION_FORM_ENTITY));
doReturn(form).when(applicationFormService).save(any());

utilMock.when(() -> ApplicationFormUtils.verifyAndGetApplicationFormSection(any(), any()))
.thenReturn(SAMPLE_APPLICATION_FORM_ENTITY.getDefinition().getSections().get(0));

String questionId = applicationFormService.addQuestionToApplicationForm(
SAMPLE_APPLICATION_ID, SAMPLE_SECTION_ID, SAMPLE_QUESTION_GENERIC_POST_DTO, new MockHttpSession());

Expand Down Expand Up @@ -432,6 +435,9 @@ void addNewQuestionValuesOptionsHappyPathTest() {
.thenReturn(Optional.of(SAMPLE_APPLICATION_FORM_ENTITY));
doReturn(form).when(applicationFormService).save(any());

utilMock.when(() -> ApplicationFormUtils.verifyAndGetApplicationFormSection(any(), any()))
.thenReturn(SAMPLE_APPLICATION_FORM_ENTITY.getDefinition().getSections().get(0));

String questionId = applicationFormService.addQuestionToApplicationForm(
SAMPLE_APPLICATION_ID, SAMPLE_SECTION_ID, SAMPLE_QUESTION_OPTIONS_POST_DTO, new MockHttpSession());

Expand Down Expand Up @@ -513,6 +519,9 @@ void addNewQuestionOptionValueConflictExceptionTest() {
when(ApplicationFormServiceTest.this.applicationFormRepository.findById(SAMPLE_APPLICATION_ID))
.thenReturn(Optional.of(SAMPLE_EMPTY_APPLICATION_FORM_ENTITY));

utilMock.when(() -> ApplicationFormUtils.verifyAndGetApplicationFormSection(any(), any()))
.thenThrow(new ConflictException("MULTIPLE_EDITORS_SECTION_DELETED"));

assertThatThrownBy(() -> applicationFormService
.addQuestionToApplicationForm(SAMPLE_APPLICATION_ID, SAMPLE_SECTION_ID,
SAMPLE_QUESTION_OPTIONS_CONTENT_INVALID_POST_DTO, session))
Expand Down Expand Up @@ -652,8 +661,8 @@ void getQuestionSectionNotFoundTest() {

assertThatThrownBy(() -> ApplicationFormServiceTest.this.applicationFormService
.retrieveQuestion(SAMPLE_APPLICATION_ID, "differentId", SAMPLE_QUESTION_ID))
.isInstanceOf(NotFoundException.class)
.hasMessage("Section with id differentId does not exist");
.isInstanceOf(ConflictException.class)
.hasMessage("MULTIPLE_EDITORS_SECTION_DELETED");

}

Expand Down Expand Up @@ -884,6 +893,7 @@ void updateQuestionOrderOutdatedVersion() {
ArgumentCaptor<ApplicationFormEntity> argument = ArgumentCaptor.forClass(ApplicationFormEntity.class);

ApplicationFormEntity testApplicationFormEntity = randomApplicationFormEntity().build();
String sectionId = testApplicationFormEntity.getDefinition().getSections().get(0).getSectionId();

final Integer applicationId = testApplicationFormEntity.getGrantApplicationId();
final Integer increment = 1;
Expand All @@ -894,11 +904,31 @@ void updateQuestionOrderOutdatedVersion() {
doReturn(testApplicationFormEntity).when(applicationFormService).save(any());

assertThatThrownBy(() -> ApplicationFormServiceTest.this.applicationFormService
.updateQuestionOrder(applicationId, SAMPLE_SECTION_ID, SAMPLE_QUESTION_ID, increment, SAMPLE_VERSION - 1))
.updateQuestionOrder(applicationId,sectionId, SAMPLE_QUESTION_ID, increment, SAMPLE_VERSION - 1))
.isInstanceOf(ConflictException.class)
.hasMessage("MULTIPLE_EDITORS");
}

@Test
void updateQuestionOrderSectionDeleted() {
ArgumentCaptor<ApplicationFormEntity> argument = ArgumentCaptor.forClass(ApplicationFormEntity.class);

ApplicationFormEntity testApplicationFormEntity = randomApplicationFormEntity().build();

final Integer applicationId = testApplicationFormEntity.getGrantApplicationId();
final Integer increment = 1;

when(applicationFormRepository.findById(applicationId))
.thenReturn(Optional.of(testApplicationFormEntity));

doReturn(testApplicationFormEntity).when(applicationFormService).save(any());

assertThatThrownBy(() -> ApplicationFormServiceTest.this.applicationFormService
.updateQuestionOrder(applicationId,SAMPLE_SECTION_ID, SAMPLE_QUESTION_ID, increment, SAMPLE_VERSION - 1))
.isInstanceOf(ConflictException.class)
.hasMessage("MULTIPLE_EDITORS_SECTION_DELETED");
}

}

@Nested
Expand Down

0 comments on commit ee1b0b0

Please sign in to comment.