diff --git a/src/main/java/gov/cabinetoffice/gap/applybackend/dto/api/SchemeMandatoryQuestionApplicationFormInfosDto.java b/src/main/java/gov/cabinetoffice/gap/applybackend/dto/api/SchemeMandatoryQuestionApplicationFormInfosDto.java index f3370057..50ab9fe1 100644 --- a/src/main/java/gov/cabinetoffice/gap/applybackend/dto/api/SchemeMandatoryQuestionApplicationFormInfosDto.java +++ b/src/main/java/gov/cabinetoffice/gap/applybackend/dto/api/SchemeMandatoryQuestionApplicationFormInfosDto.java @@ -10,6 +10,7 @@ @AllArgsConstructor @Builder public class SchemeMandatoryQuestionApplicationFormInfosDto { + private boolean hasAdvertPublished; private boolean hasInternalApplication; private boolean hasPublishedInternalApplication; } diff --git a/src/main/java/gov/cabinetoffice/gap/applybackend/exception/AdvertNotPublishedException.java b/src/main/java/gov/cabinetoffice/gap/applybackend/exception/AdvertNotPublishedException.java new file mode 100644 index 00000000..ff13e2bb --- /dev/null +++ b/src/main/java/gov/cabinetoffice/gap/applybackend/exception/AdvertNotPublishedException.java @@ -0,0 +1,8 @@ +package gov.cabinetoffice.gap.applybackend.exception; + +public class AdvertNotPublishedException extends RuntimeException { + public AdvertNotPublishedException(String message) { + super(message); + } + +} \ No newline at end of file diff --git a/src/main/java/gov/cabinetoffice/gap/applybackend/service/GrantAdvertService.java b/src/main/java/gov/cabinetoffice/gap/applybackend/service/GrantAdvertService.java index 03debda2..583cd311 100644 --- a/src/main/java/gov/cabinetoffice/gap/applybackend/service/GrantAdvertService.java +++ b/src/main/java/gov/cabinetoffice/gap/applybackend/service/GrantAdvertService.java @@ -34,7 +34,7 @@ public class GrantAdvertService { private final GrantMandatoryQuestionService grantMandatoryQuestionService; private final GrantApplicantService grantApplicantService; - public String getExternalSubmissionUrl(GrantAdvert advert) { + public String getApplyToUrl(GrantAdvert advert) { return advert.getResponse().getSections().stream() .filter(section -> section.getId().equals("howToApply")) .flatMap(section -> section.getPages().stream()) @@ -60,7 +60,7 @@ public GetGrantAdvertDto generateGetGrantAdvertDto(GrantAdvert advert, GetGrantM return GetGrantAdvertDto.builder() .id(advert.getId()) .version(advert.getVersion()) - .externalSubmissionUrl(getExternalSubmissionUrl(advert)) + .externalSubmissionUrl(getApplyToUrl(advert)) .isInternal(isInternal) .grantApplicationId(grantApplicationId) .grantSchemeId(advert.getScheme().getId()) diff --git a/src/main/java/gov/cabinetoffice/gap/applybackend/service/GrantApplicationService.java b/src/main/java/gov/cabinetoffice/gap/applybackend/service/GrantApplicationService.java index 0eeff924..a514e142 100644 --- a/src/main/java/gov/cabinetoffice/gap/applybackend/service/GrantApplicationService.java +++ b/src/main/java/gov/cabinetoffice/gap/applybackend/service/GrantApplicationService.java @@ -35,6 +35,10 @@ public boolean doesSchemeHaveAPublishedApplication(final GrantScheme grantScheme return grantApplicationRepository.findByGrantSchemeAndApplicationStatus(grantScheme, GrantApplicationStatus.PUBLISHED).isPresent(); } + public boolean doesSchemeHaveAnApplication(final GrantScheme grantScheme) { + return grantApplicationRepository.findByGrantScheme(grantScheme).isPresent(); + } + public Integer getGrantApplicationId(final GrantScheme grantScheme) { final Optional grantApplication = grantApplicationRepository.findByGrantScheme(grantScheme); return grantApplication.map(GrantApplication::getId).orElse(null); diff --git a/src/main/java/gov/cabinetoffice/gap/applybackend/web/GrantMandatoryQuestionsController.java b/src/main/java/gov/cabinetoffice/gap/applybackend/web/GrantMandatoryQuestionsController.java index ce9573b7..cae38a30 100644 --- a/src/main/java/gov/cabinetoffice/gap/applybackend/web/GrantMandatoryQuestionsController.java +++ b/src/main/java/gov/cabinetoffice/gap/applybackend/web/GrantMandatoryQuestionsController.java @@ -4,7 +4,10 @@ import gov.cabinetoffice.gap.applybackend.dto.api.GetGrantMandatoryQuestionDto; import gov.cabinetoffice.gap.applybackend.dto.api.JwtPayload; import gov.cabinetoffice.gap.applybackend.dto.api.UpdateGrantMandatoryQuestionDto; +import gov.cabinetoffice.gap.applybackend.enums.GrantAdvertStatus; import gov.cabinetoffice.gap.applybackend.enums.GrantMandatoryQuestionStatus; +import gov.cabinetoffice.gap.applybackend.exception.AdvertNotPublishedException; +import gov.cabinetoffice.gap.applybackend.exception.NotFoundException; import gov.cabinetoffice.gap.applybackend.mapper.GrantMandatoryQuestionMapper; import gov.cabinetoffice.gap.applybackend.model.*; import gov.cabinetoffice.gap.applybackend.service.GrantAdvertService; @@ -54,21 +57,19 @@ public ResponseEntity createMandatoryQuestion(@Req final JwtPayload jwtPayload = (JwtPayload) SecurityContextHolder.getContext().getAuthentication().getPrincipal(); final GrantApplicant applicant = grantApplicantService.getApplicantById(jwtPayload.getSub()); - log.info("Getting scheme with id {}", schemeId); final GrantScheme scheme = grantSchemeService.getSchemeById(schemeId); - log.info("Scheme with id {} found", schemeId); + boolean isForInternalApplication = true; - log.info("Getting Advert associated to scheme with id {}", schemeId); - final GrantAdvert advert = grantAdvertService.getAdvertBySchemeId(schemeId.toString()); - log.info("Advert with id {} found for scheme with id {}", advert.getId(), schemeId); + try { + final String webpageUrl = getApplyingUrlFromAdvert(scheme); + isForInternalApplication = webpageUrl.contains(environmentProperties.getFrontEndUri()); + log.debug("Advert is pointing to an internal application form : {}", isForInternalApplication); - final String webpageUrl = grantAdvertService.getExternalSubmissionUrl(advert); - - log.info("Checking that the advert has link to internal or external application"); - final boolean webPageUrlIsForInternalApplications = webpageUrl.contains(environmentProperties.getFrontEndUri()); - log.info("Advert is pointing to an internal application form : {}", webPageUrlIsForInternalApplications); + } catch (AdvertNotPublishedException | NotFoundException e) { + log.debug(e.getMessage()); + } - final GrantMandatoryQuestions grantMandatoryQuestions = grantMandatoryQuestionService.createMandatoryQuestion(scheme, applicant, webPageUrlIsForInternalApplications); + final GrantMandatoryQuestions grantMandatoryQuestions = grantMandatoryQuestionService.createMandatoryQuestion(scheme, applicant, isForInternalApplication); log.info("Mandatory question with ID {} has been created.", grantMandatoryQuestions.getId()); final GetGrantMandatoryQuestionDto getGrantMandatoryQuestionDto = grantMandatoryQuestionMapper.mapGrantMandatoryQuestionToGetGrantMandatoryQuestionDTO(grantMandatoryQuestions); @@ -76,7 +77,6 @@ public ResponseEntity createMandatoryQuestion(@Req return ResponseEntity.ok(getGrantMandatoryQuestionDto); } - @GetMapping("/{mandatoryQuestionId}") @ApiResponses(value = { @ApiResponse(responseCode = "200", description = "Grant Mandatory found", content = @Content(mediaType = "application/json", schema = @Schema(implementation = GrantMandatoryQuestions.class))), @@ -193,4 +193,18 @@ public ResponseEntity getApplicationStatusByMandatoryQuestionId(@PathVar return ResponseEntity.ok(grantApplication.getApplicationStatus().name()); } + + private String getApplyingUrlFromAdvert(GrantScheme scheme) { + log.debug("Getting Advert associated to scheme with id {}", scheme.getId()); + final GrantAdvert advert = grantAdvertService.getAdvertBySchemeId(scheme.getId().toString()); + log.debug("Advert with id {} found for scheme with id {}", advert.getId(), scheme.getId()); + + final boolean isAdvertPublished = advert.getStatus().equals(GrantAdvertStatus.PUBLISHED); + if (!isAdvertPublished) { + throw new AdvertNotPublishedException("Advert with id " + advert.getId() + " is not published"); + } + + final String webpageUrl = grantAdvertService.getApplyToUrl(advert); + return webpageUrl; + } } diff --git a/src/main/java/gov/cabinetoffice/gap/applybackend/web/GrantSchemeController.java b/src/main/java/gov/cabinetoffice/gap/applybackend/web/GrantSchemeController.java index a207b667..264ee035 100644 --- a/src/main/java/gov/cabinetoffice/gap/applybackend/web/GrantSchemeController.java +++ b/src/main/java/gov/cabinetoffice/gap/applybackend/web/GrantSchemeController.java @@ -3,6 +3,8 @@ import gov.cabinetoffice.gap.applybackend.config.properties.EnvironmentProperties; import gov.cabinetoffice.gap.applybackend.dto.api.*; import gov.cabinetoffice.gap.applybackend.enums.GrantAdvertStatus; +import gov.cabinetoffice.gap.applybackend.exception.AdvertNotPublishedException; +import gov.cabinetoffice.gap.applybackend.exception.NotFoundException; import gov.cabinetoffice.gap.applybackend.mapper.GrantSchemeMapper; import gov.cabinetoffice.gap.applybackend.model.GrantAdvert; import gov.cabinetoffice.gap.applybackend.model.GrantScheme; @@ -67,31 +69,23 @@ public ResponseEntity getGrantSchemeByI @ApiResponse(responseCode = "404", description = "No Grant Scheme found", content = @Content(mediaType = "application/json")), }) - public ResponseEntity schemeHasInternalApplication(@PathVariable Integer grantSchemeId){ - log.debug("Checking if scheme with id {} has internal application form and if it is published",grantSchemeId); + public ResponseEntity schemeHasInternalApplication(@PathVariable Integer grantSchemeId) { + log.debug("Checking if scheme with id {} has internal application form and if it is published", grantSchemeId); final GrantScheme grantScheme = grantSchemeService.getSchemeById(grantSchemeId); log.debug("Scheme with id {} found. Getting associated advert", grantSchemeId); - log.debug("Getting Advert associated to scheme with id {}", grantSchemeId); - final GrantAdvert advert = grantAdvertService.getAdvertBySchemeId(grantSchemeId.toString()); - log.debug("Advert with id {} found for scheme with id {}", advert.getId(), grantSchemeId); - - final String webpageUrl = grantAdvertService.getExternalSubmissionUrl(advert); - - final boolean webPageUrlIsForInternalApplications = webpageUrl.contains(environmentProperties.getFrontEndUri()); - log.debug("Advert is pointing to an internal application form : {}", webPageUrlIsForInternalApplications); + final boolean hasSchemeAnApplication = grantApplicationService.doesSchemeHaveAnApplication(grantScheme); + final boolean hasPublishedApplication = grantApplicationService.doesSchemeHaveAPublishedApplication(grantScheme); + log.debug("Application form associated to scheme with Id {} has PUBLISHED status: {}", grantScheme.getId(), hasPublishedApplication); final SchemeMandatoryQuestionApplicationFormInfosDto dto = SchemeMandatoryQuestionApplicationFormInfosDto.builder() - .hasInternalApplication(webPageUrlIsForInternalApplications) - .hasPublishedInternalApplication(false) + .hasAdvertPublished(false) + .hasInternalApplication(hasSchemeAnApplication) + .hasPublishedInternalApplication(hasPublishedApplication) .build(); - if (webPageUrlIsForInternalApplications) { - final boolean hasPublishedApplication = grantApplicationService.doesSchemeHaveAPublishedApplication(grantScheme); - log.debug("Application form associated to advert with Id {} has PUBLISHED status: {}", advert.getId(), hasPublishedApplication); - dto.setHasPublishedInternalApplication(hasPublishedApplication); - } + checkIfAdvertExistAndHasApplyingUrlPointingToInternalApplication(grantSchemeId, dto); log.debug("Scheme with ID {} is for internal application: {} and has a published application form : {}", grantSchemeId, dto.isHasInternalApplication(), dto.isHasPublishedInternalApplication()); @@ -99,4 +93,28 @@ public ResponseEntity schemeHasI return ResponseEntity.ok(dto); } + + private void checkIfAdvertExistAndHasApplyingUrlPointingToInternalApplication(Integer grantSchemeId, SchemeMandatoryQuestionApplicationFormInfosDto dto) { + try { + log.debug("Getting Advert associated to scheme with id {}", grantSchemeId); + final GrantAdvert advert = grantAdvertService.getAdvertBySchemeId(grantSchemeId.toString()); + log.debug("Advert with id {} found for scheme with id {}", advert.getId(), grantSchemeId); + + final boolean isAdvertPublished = advert.getStatus().equals(GrantAdvertStatus.PUBLISHED); + if (!isAdvertPublished) { + throw new AdvertNotPublishedException("Advert with id " + advert.getId() + " is not published"); + } + dto.setHasAdvertPublished(true); + + final String webpageUrl = grantAdvertService.getApplyToUrl(advert); + + final boolean webPageUrlIsForInternalApplications = webpageUrl.contains(environmentProperties.getFrontEndUri()); + log.debug("Advert is pointing to an internal application form : {}", webPageUrlIsForInternalApplications); + dto.setHasInternalApplication(webPageUrlIsForInternalApplications); + + } catch (AdvertNotPublishedException | NotFoundException e) { + log.debug(e.getMessage()); + } + } + } diff --git a/src/test/java/gov/cabinetoffice/gap/applybackend/service/GrantAdvertServiceTest.java b/src/test/java/gov/cabinetoffice/gap/applybackend/service/GrantAdvertServiceTest.java index e0ae7078..40be478c 100644 --- a/src/test/java/gov/cabinetoffice/gap/applybackend/service/GrantAdvertServiceTest.java +++ b/src/test/java/gov/cabinetoffice/gap/applybackend/service/GrantAdvertServiceTest.java @@ -79,7 +79,7 @@ void getExternalSubmissionUrl() { .response(response) .build(); - final String methodResponse = grantAdvertService.getExternalSubmissionUrl(advert); + final String methodResponse = grantAdvertService.getApplyToUrl(advert); assertThat(methodResponse).isEqualTo("responseUrl"); } @@ -91,7 +91,7 @@ void getExternalSubmissionUrl_returnEmptyString() { .response(response) .build(); - final String methodResponse = grantAdvertService.getExternalSubmissionUrl(advert); + final String methodResponse = grantAdvertService.getApplyToUrl(advert); assertThat(methodResponse).isEmpty(); } diff --git a/src/test/java/gov/cabinetoffice/gap/applybackend/service/GrantApplicationServiceTest.java b/src/test/java/gov/cabinetoffice/gap/applybackend/service/GrantApplicationServiceTest.java index 9586af4f..469ebda0 100644 --- a/src/test/java/gov/cabinetoffice/gap/applybackend/service/GrantApplicationServiceTest.java +++ b/src/test/java/gov/cabinetoffice/gap/applybackend/service/GrantApplicationServiceTest.java @@ -96,7 +96,7 @@ void isGrantApplicationPublished__False() { } @Test - void doesSchemeHaveApplication__True() { + void doesSchemeHavePublishedApplication__True() { final GrantScheme scheme = GrantScheme.builder().id(1).build(); final GrantApplication application = GrantApplication.builder() .grantScheme(scheme) @@ -113,7 +113,7 @@ void doesSchemeHaveApplication__True() { } @Test - void doesSchemeHaveApplication__False() { + void doesSchemeHavePublishedApplication__False() { final GrantScheme scheme = GrantScheme.builder().id(1).build(); when(grantApplicationRepository.findByGrantSchemeAndApplicationStatus(scheme, GrantApplicationStatus.PUBLISHED)).thenReturn(Optional.empty()); @@ -124,6 +124,33 @@ void doesSchemeHaveApplication__False() { assertFalse(response); } + @Test + void doesSchemeHaveApplication__True() { + final GrantScheme scheme = GrantScheme.builder().id(1).build(); + final GrantApplication application = GrantApplication.builder() + .grantScheme(scheme) + .build(); + + when(grantApplicationRepository.findByGrantScheme(scheme)) + .thenReturn(Optional.of(application)); + + final boolean response = serviceUnderTest.doesSchemeHaveAnApplication(scheme); + + verify(grantApplicationRepository).findByGrantScheme(scheme); + assertTrue(response); + } + + @Test + void doesSchemeHaveApplication__False() { + final GrantScheme scheme = GrantScheme.builder().id(1).build(); + + when(grantApplicationRepository.findByGrantScheme(scheme)).thenReturn(Optional.empty()); + + final boolean response = serviceUnderTest.doesSchemeHaveAnApplication(scheme); + + verify(grantApplicationRepository).findByGrantScheme(scheme); + assertFalse(response); + } @Test void getGrantApplicationId__returnsId() { final GrantScheme scheme = GrantScheme.builder().id(1).build(); diff --git a/src/test/java/gov/cabinetoffice/gap/applybackend/web/GrantMandatoryQuestionsControllerTest.java b/src/test/java/gov/cabinetoffice/gap/applybackend/web/GrantMandatoryQuestionsControllerTest.java index 27d55a57..2cbeaf95 100644 --- a/src/test/java/gov/cabinetoffice/gap/applybackend/web/GrantMandatoryQuestionsControllerTest.java +++ b/src/test/java/gov/cabinetoffice/gap/applybackend/web/GrantMandatoryQuestionsControllerTest.java @@ -4,6 +4,7 @@ import gov.cabinetoffice.gap.applybackend.dto.api.GetGrantMandatoryQuestionDto; import gov.cabinetoffice.gap.applybackend.dto.api.JwtPayload; import gov.cabinetoffice.gap.applybackend.dto.api.UpdateGrantMandatoryQuestionDto; +import gov.cabinetoffice.gap.applybackend.enums.GrantAdvertStatus; import gov.cabinetoffice.gap.applybackend.enums.GrantApplicationStatus; import gov.cabinetoffice.gap.applybackend.enums.GrantMandatoryQuestionFundingLocation; import gov.cabinetoffice.gap.applybackend.enums.GrantMandatoryQuestionOrgType; @@ -40,6 +41,7 @@ import static org.assertj.core.api.Assertions.assertThat; import static org.junit.Assert.assertFalse; import static org.junit.Assert.assertTrue; +import static org.mockito.Mockito.never; import static org.mockito.Mockito.verify; import static org.mockito.Mockito.when; @@ -109,13 +111,13 @@ void createMandatoryQuestion_CreatesMandatoryQuestionEntry_AndReturnsItsID() { final GetGrantMandatoryQuestionDto emptyMandatoryQuestionsDto = GetGrantMandatoryQuestionDto.builder() .id(MANDATORY_QUESTION_ID) .build(); - final GrantAdvert grantAdvert = GrantAdvert.builder().scheme(scheme).build(); + final GrantAdvert grantAdvert = GrantAdvert.builder().scheme(scheme).status(GrantAdvertStatus.PUBLISHED).build(); final String webPageUrl = "http://localhost:3000/apply/applicant/internalUrl"; final String frontEndUrl = "http://localhost:3000/apply/applicant"; when(grantAdvertService.getAdvertBySchemeId(schemeId.toString())).thenReturn(grantAdvert); - when(grantAdvertService.getExternalSubmissionUrl(grantAdvert)).thenReturn(webPageUrl); + when(grantAdvertService.getApplyToUrl(grantAdvert)).thenReturn(webPageUrl); when(environmentProperties.getFrontEndUri()).thenReturn(frontEndUrl); when(grantApplicantService.getApplicantById(applicantUserId)) @@ -138,6 +140,40 @@ void createMandatoryQuestion_CreatesMandatoryQuestionEntry_AndReturnsItsID() { } + @Test + void createMandatoryQuestion_AdvertIsNotPublished_CreatesMandatoryQuestionEntry_AndReturnsItsID() { + + final GrantMandatoryQuestions emptyMandatoryQuestions = GrantMandatoryQuestions.builder() + .id(MANDATORY_QUESTION_ID) + .build(); + final GetGrantMandatoryQuestionDto emptyMandatoryQuestionsDto = GetGrantMandatoryQuestionDto.builder() + .id(MANDATORY_QUESTION_ID) + .build(); + final GrantAdvert grantAdvert = GrantAdvert.builder().scheme(scheme).status(GrantAdvertStatus.UNPUBLISHED).build(); + + when(grantAdvertService.getAdvertBySchemeId(schemeId.toString())).thenReturn(grantAdvert); + + when(grantApplicantService.getApplicantById(applicantUserId)) + .thenReturn(applicant); + + when(grantSchemeService.getSchemeById(schemeId)) + .thenReturn(scheme); + + when(grantMandatoryQuestionService.createMandatoryQuestion(scheme, applicant, true)) + .thenReturn(emptyMandatoryQuestions); + + when(grantMandatoryQuestionMapper.mapGrantMandatoryQuestionToGetGrantMandatoryQuestionDTO(emptyMandatoryQuestions)) + .thenReturn(emptyMandatoryQuestionsDto); + + final ResponseEntity methodResponse = controllerUnderTest.createMandatoryQuestion(schemeId); + + verify(grantMandatoryQuestionService).createMandatoryQuestion(scheme, applicant, true); + verify(grantAdvertService, never()).getApplyToUrl(grantAdvert); + assertThat(methodResponse.getStatusCode()).isEqualTo(HttpStatus.OK); + assertThat(methodResponse.getBody()).isEqualTo(emptyMandatoryQuestionsDto); + + } + @Test void getGrantMandatoryQuestionsById_ReturnsExpectedMandatoryQuestions() { final GrantMandatoryQuestionFundingLocation fundingLocation = SCOTLAND; diff --git a/src/test/java/gov/cabinetoffice/gap/applybackend/web/GrantSchemeControllerTest.java b/src/test/java/gov/cabinetoffice/gap/applybackend/web/GrantSchemeControllerTest.java index a7ad6e2a..17b544b4 100644 --- a/src/test/java/gov/cabinetoffice/gap/applybackend/web/GrantSchemeControllerTest.java +++ b/src/test/java/gov/cabinetoffice/gap/applybackend/web/GrantSchemeControllerTest.java @@ -30,6 +30,7 @@ import java.time.Instant; import java.util.Collections; +import static gov.cabinetoffice.gap.applybackend.enums.GrantAdvertStatus.*; import static org.junit.jupiter.api.Assertions.assertEquals; import static org.junit.jupiter.api.Assertions.assertFalse; import static org.junit.jupiter.api.Assertions.assertTrue; @@ -100,7 +101,7 @@ void getGrantSchemeById_ReturnsTheCorrectGrantScheme() { @Test void getGrantSchemeById_ReturnsTheCorrectGrantSchemeWithNoAdverts() { - final GrantAdvert grantAdvert = GrantAdvert.builder().status(GrantAdvertStatus.DRAFT).build(); + final GrantAdvert grantAdvert = GrantAdvert.builder().status(DRAFT).build(); final GrantScheme grantScheme = GrantScheme.builder() .id(SCHEME_ID) .funderId(1) @@ -133,63 +134,89 @@ void getGrantSchemeById_ReturnsTheCorrectGrantSchemeWithNoAdverts() { } @Test - void schemeHasInternalApplicationAndHasPublishedApplicationForm(){ + void schemeHasAdvertPublishedAndHasInternalApplicationAndHasPublishedApplicationForm() { final GrantScheme grantScheme = GrantScheme.builder() .id(SCHEME_ID).build(); - final GrantAdvert grantAdvert = GrantAdvert.builder().scheme(grantScheme).build(); + final GrantAdvert grantAdvert = GrantAdvert.builder().scheme(grantScheme).status(PUBLISHED).build(); final String webPageUrl = "http://localhost:3000/apply/applicant/internalUrl"; final String frontEndUrl = "http://localhost:3000/apply/applicant"; when(grantSchemeService.getSchemeById(SCHEME_ID)).thenReturn(grantScheme); when(grantAdvertService.getAdvertBySchemeId(SCHEME_ID.toString())).thenReturn(grantAdvert); - when(grantAdvertService.getExternalSubmissionUrl(grantAdvert)).thenReturn(webPageUrl); + when(grantAdvertService.getApplyToUrl(grantAdvert)).thenReturn(webPageUrl); when(environmentProperties.getFrontEndUri()).thenReturn(frontEndUrl); when(grantApplicationService.doesSchemeHaveAPublishedApplication(grantScheme)).thenReturn(true); + when(grantApplicationService.doesSchemeHaveAnApplication(grantScheme)).thenReturn(true); final ResponseEntity response = controllerUnderTest.schemeHasInternalApplication(SCHEME_ID); assertTrue(response.getBody().isHasInternalApplication()); assertTrue(response.getBody().isHasPublishedInternalApplication()); + assertTrue(response.getBody().isHasAdvertPublished()); } @Test - void schemeHasInternalApplicationAndHasNotPublishedApplicationForm(){ + void schemeHasAdvertPublishedAndHasInternalApplicationAndHasNotPublishedApplicationForm() { final GrantScheme grantScheme = GrantScheme.builder() .id(SCHEME_ID).build(); - final GrantAdvert grantAdvert = GrantAdvert.builder().scheme(grantScheme).build(); + final GrantAdvert grantAdvert = GrantAdvert.builder().scheme(grantScheme).status(PUBLISHED).build(); final String webPageUrl = "http://localhost:3000/apply/applicant/internalUrl"; final String frontEndUrl = "http://localhost:3000/apply/applicant"; when(grantSchemeService.getSchemeById(SCHEME_ID)).thenReturn(grantScheme); when(grantAdvertService.getAdvertBySchemeId(SCHEME_ID.toString())).thenReturn(grantAdvert); - when(grantAdvertService.getExternalSubmissionUrl(grantAdvert)).thenReturn(webPageUrl); + when(grantAdvertService.getApplyToUrl(grantAdvert)).thenReturn(webPageUrl); when(environmentProperties.getFrontEndUri()).thenReturn(frontEndUrl); when(grantApplicationService.doesSchemeHaveAPublishedApplication(grantScheme)).thenReturn(false); + when(grantApplicationService.doesSchemeHaveAnApplication(grantScheme)).thenReturn(true); final ResponseEntity response = controllerUnderTest.schemeHasInternalApplication(SCHEME_ID); assertTrue(response.getBody().isHasInternalApplication()); assertFalse(response.getBody().isHasPublishedInternalApplication()); + assertTrue(response.getBody().isHasAdvertPublished()); + } @Test - void schemeHasNotInternalApplicationAndHasNotPublishedApplicationForm(){ + void schemeHasAdvertPublishedAndHasNotInternalApplicationAndHasNotPublishedApplicationForm() { final GrantScheme grantScheme = GrantScheme.builder() .id(SCHEME_ID).build(); - final GrantAdvert grantAdvert = GrantAdvert.builder().scheme(grantScheme).build(); + final GrantAdvert grantAdvert = GrantAdvert.builder().scheme(grantScheme).status(PUBLISHED).build(); final String webPageUrl = "http://externalURL"; final String frontEndUrl = "http://localhost:3000/apply/applicant"; when(grantSchemeService.getSchemeById(SCHEME_ID)).thenReturn(grantScheme); when(grantAdvertService.getAdvertBySchemeId(SCHEME_ID.toString())).thenReturn(grantAdvert); - when(grantAdvertService.getExternalSubmissionUrl(grantAdvert)).thenReturn(webPageUrl); + when(grantAdvertService.getApplyToUrl(grantAdvert)).thenReturn(webPageUrl); when(environmentProperties.getFrontEndUri()).thenReturn(frontEndUrl); + when(grantApplicationService.doesSchemeHaveAnApplication(grantScheme)).thenReturn(false); + when(grantApplicationService.doesSchemeHaveAPublishedApplication(grantScheme)).thenReturn(false); final ResponseEntity response = controllerUnderTest.schemeHasInternalApplication(SCHEME_ID); - verify(grantApplicationService, never()).doesSchemeHaveAPublishedApplication(grantScheme); assertFalse(response.getBody().isHasInternalApplication()); assertFalse(response.getBody().isHasPublishedInternalApplication()); + assertTrue(response.getBody().isHasAdvertPublished()); + } + @Test + void schemeHasAdvertNotPublishedAndInternalApplicationAndHasPublishedApplicationForm() { + final GrantScheme grantScheme = GrantScheme.builder() + .id(SCHEME_ID).build(); + final GrantAdvert grantAdvert = GrantAdvert.builder().scheme(grantScheme).status(UNPUBLISHED).build(); + + when(grantSchemeService.getSchemeById(SCHEME_ID)).thenReturn(grantScheme); + when(grantAdvertService.getAdvertBySchemeId(SCHEME_ID.toString())).thenReturn(grantAdvert); + when(grantApplicationService.doesSchemeHaveAnApplication(grantScheme)).thenReturn(true); + when(grantApplicationService.doesSchemeHaveAPublishedApplication(grantScheme)).thenReturn(true); + + final ResponseEntity response = controllerUnderTest.schemeHasInternalApplication(SCHEME_ID); + + verify(grantAdvertService, never()).getApplyToUrl(grantAdvert); + assertTrue(response.getBody().isHasInternalApplication()); + assertTrue(response.getBody().isHasPublishedInternalApplication()); + assertFalse(response.getBody().isHasAdvertPublished()); + } }