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

TMI2-697: handle draft adverts #125

Merged
merged 4 commits into from
Mar 6, 2024
Merged
Show file tree
Hide file tree
Changes from 3 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
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package gov.cabinetoffice.gap.applybackend.repository;


import gov.cabinetoffice.gap.applybackend.enums.GrantApplicationStatus;
import gov.cabinetoffice.gap.applybackend.model.GrantApplication;
import gov.cabinetoffice.gap.applybackend.model.GrantScheme;
import org.jetbrains.annotations.NotNull;
Expand All @@ -15,6 +16,7 @@ public interface GrantApplicationRepository extends JpaRepository<GrantApplicati

Optional<GrantApplication> findByGrantScheme(GrantScheme grantScheme);

Optional<GrantApplication> findByGrantSchemeAndApplicationStatus(GrantScheme grantScheme, GrantApplicationStatus applicationStatus);

@NotNull
@EntityGraph(attributePaths = {"grantScheme"})
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ public GrantAdvert getAdvertByContentfulSlug(String contentfulSlug) {
}

public GetGrantAdvertDto generateGetGrantAdvertDto(GrantAdvert advert, GetGrantMandatoryQuestionDto mandatoryQuestions) {
final boolean isInternal = grantApplicationService.doesSchemeHaveApplication(advert.getScheme());
final boolean isInternal = grantApplicationService.doesSchemeHaveAPublishedApplication(advert.getScheme());
final Integer grantApplicationId = grantApplicationService.getGrantApplicationId(advert.getScheme());

return GetGrantAdvertDto.builder()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,8 @@ public boolean isGrantApplicationPublished(final int applicationId) {
return getGrantApplicationById(applicationId).getApplicationStatus().equals(GrantApplicationStatus.PUBLISHED);
}

public boolean doesSchemeHaveApplication(final GrantScheme grantScheme) {
return grantApplicationRepository.findByGrantScheme(grantScheme).isPresent();
public boolean doesSchemeHaveAPublishedApplication(final GrantScheme grantScheme) {
return grantApplicationRepository.findByGrantSchemeAndApplicationStatus(grantScheme, GrantApplicationStatus.PUBLISHED).isPresent();
}

public Integer getGrantApplicationId(final GrantScheme grantScheme) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -134,9 +134,9 @@ public String generateNextPageUrl(String url, UUID mandatoryQuestionId, String a
}

public void addMandatoryQuestionsToSubmissionObject(final GrantMandatoryQuestions mandatoryQuestions) {
if (mandatoryQuestions.getSubmission() != null &&
mandatoryQuestions.getSubmission().getVersion() > 1) {
final Submission submission = mandatoryQuestions.getSubmission();
final Submission submission = mandatoryQuestions.getSubmission();

if (submission != null && submission.getScheme().getVersion() > 1) {

log.info("Adding mandatory question responses to submission " + submission.getId());

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,7 @@ public ResponseEntity<String> validateGrantWebpageUrl(
@Operation(summary = "Get advert scheme version and whether it is an internal application")
public ResponseEntity<GetGrantAdvertSummaryDto> getAdvertSchemeVersion(@PathVariable final String advertSlug) {
GrantAdvert advert = grantAdvertService.getAdvertByContentfulSlug(advertSlug);
boolean isInternalApplication = grantApplicationService.doesSchemeHaveApplication(advert.getScheme());
boolean isInternalApplication = grantApplicationService.doesSchemeHaveAPublishedApplication(advert.getScheme());

GetGrantAdvertSummaryDto advertSummary = GetGrantAdvertSummaryDto.builder()
.schemeVersion(advert.getScheme().getVersion())
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package gov.cabinetoffice.gap.applybackend.web;

import gov.cabinetoffice.gap.applybackend.dto.api.*;
import gov.cabinetoffice.gap.applybackend.enums.GrantAdvertStatus;
import gov.cabinetoffice.gap.applybackend.mapper.GrantSchemeMapper;
import gov.cabinetoffice.gap.applybackend.model.GrantScheme;
import gov.cabinetoffice.gap.applybackend.service.GrantAdvertService;
Expand Down Expand Up @@ -41,7 +42,9 @@ public ResponseEntity<GetGrantSchemeWithApplicationAndAdverts> getGrantSchemeByI
final GetGrantSchemeDto grantSchemeDto = new GetGrantSchemeDto(grantScheme);
final GetGrantApplicationDto grantApplicationDto = grantSchemeMapper.grantSchemeToGetGrantApplicationDto(grantScheme);
final List<GetGrantAdvertDto> grantAdvertDtos = grantScheme.getGrantAdverts().stream()
.map(grantAdvert -> grantAdvertService.grantAdvertToDto(grantAdvert, jwtPayload.getSub(), grantSchemeId))
.map(grantAdvert ->
grantAdvert.getStatus().equals(GrantAdvertStatus.PUBLISHED) ?
grantAdvertService.grantAdvertToDto(grantAdvert, jwtPayload.getSub(), grantSchemeId) : null)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

won't this end up putting null values in the list? Not sure if that matters later on or not but if it does its probably not ideal

.toList();

final GetGrantSchemeWithApplicationAndAdverts getGrantSchemeWithApplicationAndAdverts = GetGrantSchemeWithApplicationAndAdverts.builder()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -140,7 +140,7 @@ void generateGetGrantAdvertDto_createDtoForInternalApplicationAndVersion1() {
.build();
final GetGrantMandatoryQuestionDto mandatoryQuestionDto = GetGrantMandatoryQuestionDto.builder().build();

when(grantApplicationService.doesSchemeHaveApplication(scheme)).thenReturn(true);
when(grantApplicationService.doesSchemeHaveAPublishedApplication(scheme)).thenReturn(true);
when(grantApplicationService.getGrantApplicationId(scheme)).thenReturn(1);

final GetGrantAdvertDto methodResponse = grantAdvertService.generateGetGrantAdvertDto(advert, mandatoryQuestionDto);
Expand Down Expand Up @@ -169,7 +169,7 @@ void generateGetGrantAdvertDto_createDtoForInternalApplicationAndVersion2() {
.build();
final GetGrantMandatoryQuestionDto mandatoryQuestionDto = GetGrantMandatoryQuestionDto.builder().build();

when(grantApplicationService.doesSchemeHaveApplication(scheme)).thenReturn(true);
when(grantApplicationService.doesSchemeHaveAPublishedApplication(scheme)).thenReturn(true);
when(grantApplicationService.getGrantApplicationId(scheme)).thenReturn(1);

final GetGrantAdvertDto methodResponse = grantAdvertService.generateGetGrantAdvertDto(advert, mandatoryQuestionDto);
Expand Down Expand Up @@ -198,7 +198,7 @@ void generateGetGrantAdvertDto_createDtoForExternalApplicationAndVersion2() {
.build();
final GetGrantMandatoryQuestionDto mandatoryQuestionDto = GetGrantMandatoryQuestionDto.builder().build();

when(grantApplicationService.doesSchemeHaveApplication(scheme)).thenReturn(false);
when(grantApplicationService.doesSchemeHaveAPublishedApplication(scheme)).thenReturn(false);
when(grantApplicationService.getGrantApplicationId(scheme)).thenReturn(null);

final GetGrantAdvertDto methodResponse = grantAdvertService.generateGetGrantAdvertDto(advert, mandatoryQuestionDto);
Expand Down Expand Up @@ -227,7 +227,7 @@ void generateGetGrantAdvertDto_createDtoForDraftExternalApplicationAndVersion2()
.build();
final GetGrantMandatoryQuestionDto mandatoryQuestionDto = GetGrantMandatoryQuestionDto.builder().build();

when(grantApplicationService.doesSchemeHaveApplication(scheme)).thenReturn(false);
when(grantApplicationService.doesSchemeHaveAPublishedApplication(scheme)).thenReturn(false);
when(grantApplicationService.getGrantApplicationId(scheme)).thenReturn(null);

final GetGrantAdvertDto methodResponse = grantAdvertService.generateGetGrantAdvertDto(advert, mandatoryQuestionDto);
Expand Down Expand Up @@ -256,7 +256,7 @@ void generateGetGrantAdvertDto_createDtoForUnpublishedExternalApplicationAndVers
.build();
final GetGrantMandatoryQuestionDto mandatoryQuestionDto = GetGrantMandatoryQuestionDto.builder().build();

when(grantApplicationService.doesSchemeHaveApplication(scheme)).thenReturn(false);
when(grantApplicationService.doesSchemeHaveAPublishedApplication(scheme)).thenReturn(false);
when(grantApplicationService.getGrantApplicationId(scheme)).thenReturn(null);

final GetGrantAdvertDto methodResponse = grantAdvertService.generateGetGrantAdvertDto(advert, mandatoryQuestionDto);
Expand Down Expand Up @@ -285,7 +285,7 @@ void generateGetGrantAdvertDto_createDtoForScheduledExternalApplicationAndVersio
.build();
final GetGrantMandatoryQuestionDto mandatoryQuestionDto = GetGrantMandatoryQuestionDto.builder().build();

when(grantApplicationService.doesSchemeHaveApplication(scheme)).thenReturn(false);
when(grantApplicationService.doesSchemeHaveAPublishedApplication(scheme)).thenReturn(false);
when(grantApplicationService.getGrantApplicationId(scheme)).thenReturn(null);

final GetGrantAdvertDto methodResponse = grantAdvertService.generateGetGrantAdvertDto(advert, mandatoryQuestionDto);
Expand Down Expand Up @@ -314,7 +314,7 @@ void generateGetGrantAdvertDto_createDtoForExternalApplicationAndVersion1() {
.build();
final GetGrantMandatoryQuestionDto mandatoryQuestionDto = GetGrantMandatoryQuestionDto.builder().build();

when(grantApplicationService.doesSchemeHaveApplication(scheme)).thenReturn(false);
when(grantApplicationService.doesSchemeHaveAPublishedApplication(scheme)).thenReturn(false);
when(grantApplicationService.getGrantApplicationId(scheme)).thenReturn(null);

final GetGrantAdvertDto methodResponse = grantAdvertService.generateGetGrantAdvertDto(advert, mandatoryQuestionDto);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -98,26 +98,29 @@ void isGrantApplicationPublished__False() {
@Test
void doesSchemeHaveApplication__True() {
final GrantScheme scheme = GrantScheme.builder().id(1).build();
final GrantApplication application = GrantApplication.builder().grantScheme(scheme)
final GrantApplication application = GrantApplication.builder()
.grantScheme(scheme)
.applicationStatus(GrantApplicationStatus.PUBLISHED)
.build();

when(grantApplicationRepository.findByGrantScheme(scheme)).thenReturn(Optional.of(application));
when(grantApplicationRepository.findByGrantSchemeAndApplicationStatus(scheme, GrantApplicationStatus.PUBLISHED))
.thenReturn(Optional.of(application));

final boolean response = serviceUnderTest.doesSchemeHaveApplication(scheme);
final boolean response = serviceUnderTest.doesSchemeHaveAPublishedApplication(scheme);

verify(grantApplicationRepository).findByGrantScheme(scheme);
verify(grantApplicationRepository).findByGrantSchemeAndApplicationStatus(scheme, GrantApplicationStatus.PUBLISHED);
assertTrue(response);
}

@Test
void doesSchemeHaveApplication__False() {
final GrantScheme scheme = GrantScheme.builder().id(1).build();

when(grantApplicationRepository.findByGrantScheme(scheme)).thenReturn(Optional.empty());
when(grantApplicationRepository.findByGrantSchemeAndApplicationStatus(scheme, GrantApplicationStatus.PUBLISHED)).thenReturn(Optional.empty());

final boolean response = serviceUnderTest.doesSchemeHaveApplication(scheme);
final boolean response = serviceUnderTest.doesSchemeHaveAPublishedApplication(scheme);

verify(grantApplicationRepository).findByGrantScheme(scheme);
verify(grantApplicationRepository).findByGrantSchemeAndApplicationStatus(scheme, GrantApplicationStatus.PUBLISHED);
assertFalse(response);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -550,6 +550,7 @@ void testGenerateNextPageUrl_UrlNotInMapper() {
@Nested
class addMandatoryQuestionsToSubmissionObject {
//TODO I think we could maybe write more thorough tests for this method but these should be OK for now
final GrantScheme grantScheme = GrantScheme.builder().version(2).build();

@Test
void doesNothing_IfSubmissionIsNull() {
Expand All @@ -567,10 +568,11 @@ void doesNothing_IfSubmissionIsNull() {
}

@Test
void doesNothing_IfSubmissionIsVersionOne() {

void doesNothing_IfSchemeIsVersionOne() {
final GrantScheme scheme = GrantScheme.builder().version(1).build();
final Submission submission = Submission.builder()
.version(1)
.scheme(scheme)
.build();

final GrantMandatoryQuestions mandatoryQuestions = GrantMandatoryQuestions.builder()
Expand All @@ -594,6 +596,7 @@ void throwsNotFoundException_IfOrganisationDetailsSectionIsNotFound() {
final Submission submission = Submission.builder()
.definition(definition)
.version(2)
.scheme(grantScheme)
.build();

final GrantMandatoryQuestions mandatoryQuestions = GrantMandatoryQuestions.builder()
Expand All @@ -619,6 +622,7 @@ void throwsNotFoundException_IfFundingDetailsSectionIsNotFound() {
final Submission submission = Submission.builder()
.definition(definition)
.version(2)
.scheme(grantScheme)
.build();

final GrantMandatoryQuestions mandatoryQuestions = GrantMandatoryQuestions.builder()
Expand Down Expand Up @@ -652,6 +656,7 @@ void addMandatoryQuestions() {
final Submission submission = Submission.builder()
.definition(definition)
.version(2)
.scheme(grantScheme)
.build();

final GrantMandatoryQuestions mandatoryQuestions = GrantMandatoryQuestions.builder()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -252,7 +252,7 @@ void internalV1_ReturnsAdvertSummaryDto(){

when(grantAdvertService.getAdvertByContentfulSlug(mockInternalV1Advert.getContentfulSlug()))
.thenReturn(mockInternalV1Advert);
when(grantApplicationService.doesSchemeHaveApplication(mockInternalV1Advert.getScheme()))
when(grantApplicationService.doesSchemeHaveAPublishedApplication(mockInternalV1Advert.getScheme()))
.thenReturn(true);

ResponseEntity<GetGrantAdvertSummaryDto> response = grantAdvertController.getAdvertSchemeVersion(mockInternalV1Advert.getContentfulSlug());
Expand All @@ -274,7 +274,7 @@ void externalV1_ReturnsAdvertSummaryDto(){

when(grantAdvertService.getAdvertByContentfulSlug(mockExternalV1Advert.getContentfulSlug()))
.thenReturn(mockExternalV1Advert);
when(grantApplicationService.doesSchemeHaveApplication(mockExternalV1Advert.getScheme()))
when(grantApplicationService.doesSchemeHaveAPublishedApplication(mockExternalV1Advert.getScheme()))
.thenReturn(false);

ResponseEntity<GetGrantAdvertSummaryDto> response = grantAdvertController.getAdvertSchemeVersion(mockExternalV1Advert.getContentfulSlug());
Expand All @@ -296,7 +296,7 @@ void internalV2_ReturnsAdvertSummaryDto(){

when(grantAdvertService.getAdvertByContentfulSlug(mockInternalV2Advert.getContentfulSlug()))
.thenReturn(mockInternalV2Advert);
when(grantApplicationService.doesSchemeHaveApplication(mockInternalV2Advert.getScheme()))
when(grantApplicationService.doesSchemeHaveAPublishedApplication(mockInternalV2Advert.getScheme()))
.thenReturn(true);

ResponseEntity<GetGrantAdvertSummaryDto> response = grantAdvertController.getAdvertSchemeVersion(mockInternalV2Advert.getContentfulSlug());
Expand All @@ -318,7 +318,7 @@ void externalV2_ReturnsAdvertSummaryDto(){

when(grantAdvertService.getAdvertByContentfulSlug(mockExternalV2Advert.getContentfulSlug()))
.thenReturn(mockExternalV2Advert);
when(grantApplicationService.doesSchemeHaveApplication(mockExternalV2Advert.getScheme()))
when(grantApplicationService.doesSchemeHaveAPublishedApplication(mockExternalV2Advert.getScheme()))
.thenReturn(false);

ResponseEntity<GetGrantAdvertSummaryDto> response = grantAdvertController.getAdvertSchemeVersion(mockExternalV2Advert.getContentfulSlug());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,9 @@
import gov.cabinetoffice.gap.applybackend.dto.api.GetGrantSchemeDto;
import gov.cabinetoffice.gap.applybackend.dto.api.GetGrantSchemeWithApplicationAndAdverts;
import gov.cabinetoffice.gap.applybackend.dto.api.JwtPayload;
import gov.cabinetoffice.gap.applybackend.enums.GrantAdvertStatus;
import gov.cabinetoffice.gap.applybackend.mapper.GrantSchemeMapper;
import gov.cabinetoffice.gap.applybackend.model.GrantAdvert;
import gov.cabinetoffice.gap.applybackend.model.GrantApplication;
import gov.cabinetoffice.gap.applybackend.model.GrantScheme;
import gov.cabinetoffice.gap.applybackend.service.GrantSchemeService;
Expand Down Expand Up @@ -81,4 +83,34 @@ void getGrantSchemeById_ReturnsTheCorrectGrantScheme() {
.grantApplication(null)
.build(), response.getBody());
}

@Test
void getGrantSchemeById_ReturnsTheCorrectGrantSchemeWithNoAdverts() {
final GrantAdvert grantAdvert = GrantAdvert.builder().status(GrantAdvertStatus.DRAFT).build();
final GrantScheme grantScheme = GrantScheme.builder()
.id(SCHEME_ID)
.funderId(1)
.version(1)
.lastUpdated(Instant.now())
.lastUpdatedBy(1)
.ggisIdentifier("SCH-000003589")
.name("scheme_name")
.email("[email protected]")
.grantAdverts(Collections.singletonList(grantAdvert))
.grantApplication(GrantApplication.builder().build())
.build();
final GetGrantSchemeDto getGrantSchemeDto = new GetGrantSchemeDto(grantScheme);

when(grantSchemeService.getSchemeByIdWithApplicationAndAdverts(SCHEME_ID)).thenReturn(grantScheme);

ResponseEntity<GetGrantSchemeWithApplicationAndAdverts> response = controllerUnderTest.getGrantSchemeById(SCHEME_ID);

verify(grantSchemeService).getSchemeByIdWithApplicationAndAdverts(SCHEME_ID);
assertEquals(HttpStatus.OK, response.getStatusCode());
assertEquals(GetGrantSchemeWithApplicationAndAdverts.builder()
.grantScheme(getGrantSchemeDto)
.grantAdverts(Collections.singletonList(null))
.grantApplication(null)
.build(), response.getBody());
}
}
Loading