Skip to content

Commit

Permalink
GAP-2446: Multiple editors publishing adverts (#249)
Browse files Browse the repository at this point in the history
  • Loading branch information
ryan-tco authored Mar 15, 2024
1 parent c6d416e commit cc7eda5
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
import gov.cabinetoffice.gap.adminbackend.enums.GrantAdvertPageResponseStatus;
import gov.cabinetoffice.gap.adminbackend.enums.GrantAdvertSectionResponseStatus;
import gov.cabinetoffice.gap.adminbackend.enums.GrantAdvertStatus;
import gov.cabinetoffice.gap.adminbackend.exceptions.ConflictException;
import gov.cabinetoffice.gap.adminbackend.exceptions.GrantAdvertException;
import gov.cabinetoffice.gap.adminbackend.exceptions.NotFoundException;
import gov.cabinetoffice.gap.adminbackend.exceptions.UserNotFoundException;
Expand Down Expand Up @@ -168,9 +169,12 @@ public void updatePageResponse(GrantAdvertPageResponseValidationDto pagePatchDto
GrantAdvert grantAdvert = grantAdvertRepository.findById(pagePatchDto.getGrantAdvertId())
.orElseThrow(() -> new NotFoundException(
String.format("GrantAdvert with id %s not found", pagePatchDto.getGrantAdvertId())));

validateAdvertStatus(grantAdvert);
// adds the static opening and closing time to the date question
addStaticTimeToDateQuestion(pagePatchDto);


// if response/section/page does not exist, create it. If it does exist, update it
GrantAdvertResponse response = Optional.ofNullable(grantAdvert.getResponse()).orElseGet(() -> {
GrantAdvertResponse newResponse = GrantAdvertResponse.builder().build();
Expand Down Expand Up @@ -595,4 +599,10 @@ public void removeAdminReferenceBySchemeId(GrantAdmin grantAdmin, Integer scheme
grantAdvertRepository.save(advert);
});
}

public static void validateAdvertStatus(GrantAdvert grantAdvert) {
if (grantAdvert.getStatus() == GrantAdvertStatus.PUBLISHED || grantAdvert.getStatus() == GrantAdvertStatus.SCHEDULED) {
throw new ConflictException("GRANT_ADVERT_MULTIPLE_EDITORS");
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
import gov.cabinetoffice.gap.adminbackend.enums.GrantAdvertPageResponseStatus;
import gov.cabinetoffice.gap.adminbackend.enums.GrantAdvertSectionResponseStatus;
import gov.cabinetoffice.gap.adminbackend.enums.GrantAdvertStatus;
import gov.cabinetoffice.gap.adminbackend.exceptions.ConflictException;
import gov.cabinetoffice.gap.adminbackend.exceptions.NotFoundException;
import gov.cabinetoffice.gap.adminbackend.mappers.GrantAdvertMapper;
import gov.cabinetoffice.gap.adminbackend.mappers.GrantAdvertMapperImpl;
Expand Down Expand Up @@ -609,14 +610,14 @@ void updatePageResponse_DateQuestion() {

final GrantAdvert advert = GrantAdvert.builder().build();

String[] openingMultiResponse = new String[] { "10", "10", "2010", "13:00" };
String[] openingMultiResponse = new String[]{"10", "10", "2010", "13:00"};
GrantAdvertQuestionResponse openingDateQuestion = GrantAdvertQuestionResponse.builder().id(OPENING_DATE_ID)
.multiResponse(openingMultiResponse).build();
String[] closingMultiResponse = new String[] { "12", "12", "2012", "13:00" };
String[] closingMultiResponse = new String[]{"12", "12", "2012", "13:00"};
GrantAdvertQuestionResponse closingDateQuestion = GrantAdvertQuestionResponse.builder().id(CLOSING_DATE_ID)
.multiResponse(closingMultiResponse).build();
String[] expectedOpeningMultiResponse = new String[] { "10", "10", "2010", "13", "00" };
String[] expectedClosingMultiResponse = new String[] { "12", "12", "2012", "13", "00" };
String[] expectedOpeningMultiResponse = new String[]{"10", "10", "2010", "13", "00"};
String[] expectedClosingMultiResponse = new String[]{"12", "12", "2012", "13", "00"};

GrantAdvertPageResponse datePage = GrantAdvertPageResponse.builder().id(pageId)
.status(GrantAdvertPageResponseStatus.COMPLETED)
Expand Down Expand Up @@ -661,6 +662,16 @@ void updatePageResponse_DateQuestion() {
assertThat(closingQuestion.get().getMultiResponse()).isEqualTo(expectedClosingMultiResponse);
}

@Test
void updatePageResponseThrowsConflictException() {
final GrantAdvert advert = GrantAdvert.builder().status(GrantAdvertStatus.PUBLISHED).build();
when(grantAdvertRepository.findById(grantAdvertId)).thenReturn(Optional.of(advert));
GrantAdvertPageResponseValidationDto datePagePatchDto = GrantAdvertPageResponseValidationDto.builder()
.grantAdvertId(grantAdvertId).sectionId(ADVERT_DATES_SECTION_ID).page(samplePageDto).build();

assertThrows(ConflictException.class, () -> grantAdvertService.updatePageResponse(datePagePatchDto));

}
}

// TODO refactor this test and the underlying service methods to be more maintainable
Expand Down

0 comments on commit cc7eda5

Please sign in to comment.