From cdb6c4b0d8de313796867536d620995e5f6a2dc7 Mon Sep 17 00:00:00 2001 From: kiramarstonTCO <107405237+kiramarstonTCO@users.noreply.github.com> Date: Thu, 16 Nov 2023 13:22:19 +0000 Subject: [PATCH] TMI2-390 - Login to Spotlight from DD checks page (#72) * TMI2-390 - Added call to check if scheme has an internal application form * TMI2-390 - Added missing tests * TMI2-390 - Fixed formatting --- .../controllers/SchemeController.java | 31 ++++++++++++++++ .../controllers/SchemeControllerTest.java | 37 +++++++++++++++++++ 2 files changed, 68 insertions(+) diff --git a/src/main/java/gov/cabinetoffice/gap/adminbackend/controllers/SchemeController.java b/src/main/java/gov/cabinetoffice/gap/adminbackend/controllers/SchemeController.java index a2b27f4c..7ed3143b 100644 --- a/src/main/java/gov/cabinetoffice/gap/adminbackend/controllers/SchemeController.java +++ b/src/main/java/gov/cabinetoffice/gap/adminbackend/controllers/SchemeController.java @@ -5,6 +5,7 @@ import gov.cabinetoffice.gap.adminbackend.dtos.schemes.SchemeDTO; import gov.cabinetoffice.gap.adminbackend.dtos.schemes.SchemePatchDTO; import gov.cabinetoffice.gap.adminbackend.dtos.schemes.SchemePostDTO; +import gov.cabinetoffice.gap.adminbackend.entities.ApplicationFormEntity; import gov.cabinetoffice.gap.adminbackend.entities.GrantAdmin; import gov.cabinetoffice.gap.adminbackend.services.ApplicationFormService; import gov.cabinetoffice.gap.adminbackend.services.GrantAdvertService; @@ -38,6 +39,7 @@ import javax.validation.constraints.NotNull; import java.util.Collections; import java.util.List; +import java.util.NoSuchElementException; import java.util.Optional; @Tag(name = "Schemes", description = "API for handling grant schemes.") @@ -213,4 +215,33 @@ public ResponseEntity> getAdminsSchemes(final @PathVariable Stri return ResponseEntity.ok().body(Collections.emptyList()); } + @GetMapping("/{schemeId}/hasInternalApplicationForm") + @Operation(summary = "Retrieve grant scheme which matches the given id.") + @ApiResponses(value = { + @ApiResponse(responseCode = "200", description = "Found scheme which matched the given id.", + content = @Content(mediaType = "application/json", + schema = @Schema(implementation = SchemeDTO.class))), + @ApiResponse(responseCode = "404", description = "No scheme found with matching id.", + content = @Content(mediaType = "application/json")), + @ApiResponse(responseCode = "403", description = "You do not have permissions to access this scheme.", + content = @Content(mediaType = "application/json")) }) + public ResponseEntity hasInternalApplicationForm(@PathVariable final Integer schemeId) { + SchemeDTO scheme = null; + ApplicationFormEntity application = null; + try { + scheme = this.schemeService.getSchemeBySchemeId(schemeId); + } + catch (EntityNotFoundException enfe) { + return ResponseEntity.notFound().build(); + } + + try { + application = this.applicationFormService.getApplicationFromSchemeId(schemeId); + return ResponseEntity.ok(true); + } + catch (NoSuchElementException exception) { + return ResponseEntity.ok(false); + } + } + } diff --git a/src/test/java/gov/cabinetoffice/gap/adminbackend/controllers/SchemeControllerTest.java b/src/test/java/gov/cabinetoffice/gap/adminbackend/controllers/SchemeControllerTest.java index 53422273..559b2420 100644 --- a/src/test/java/gov/cabinetoffice/gap/adminbackend/controllers/SchemeControllerTest.java +++ b/src/test/java/gov/cabinetoffice/gap/adminbackend/controllers/SchemeControllerTest.java @@ -23,7 +23,9 @@ import org.springframework.data.domain.PageRequest; import org.springframework.data.domain.Pageable; import org.springframework.data.domain.Sort; +import org.springframework.http.HttpStatus; import org.springframework.http.MediaType; +import org.springframework.http.ResponseEntity; import org.springframework.security.access.AccessDeniedException; import org.springframework.test.context.ContextConfiguration; import org.springframework.test.web.servlet.MockMvc; @@ -33,10 +35,13 @@ import javax.servlet.http.Cookie; import javax.servlet.http.HttpSession; import java.util.Collections; +import java.util.NoSuchElementException; import java.util.Optional; +import static gov.cabinetoffice.gap.adminbackend.testdata.ApplicationFormTestData.SAMPLE_APPLICATION_FORM_ENTITY; import static gov.cabinetoffice.gap.adminbackend.testdata.SchemeTestData.*; import static org.assertj.core.api.Assertions.assertThat; +import static org.junit.jupiter.api.Assertions.*; import static org.mockito.ArgumentMatchers.any; import static org.mockito.Mockito.*; import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.*; @@ -403,4 +408,36 @@ void updateGrantOwnership() throws Exception { } + @Test + void hasInternalApplicationForm_HappyPath() throws Exception { + when(schemeService.getSchemeBySchemeId(SAMPLE_SCHEME_ID)).thenReturn(SCHEME_DTO_EXAMPLE); + when(applicationFormService.getApplicationFromSchemeId(SAMPLE_SCHEME_ID)) + .thenReturn(SAMPLE_APPLICATION_FORM_ENTITY); + mockMvc.perform(get("/schemes/1/hasInternalApplicationForm")).andExpect(status().isOk()) + .andExpect(content().string("true")); + } + + @Test + void hasInternalApplicationForm_HappyPathNoInternalApplicationForm() throws Exception { + when(schemeService.getSchemeBySchemeId(SAMPLE_SCHEME_ID)).thenReturn(SCHEME_DTO_EXAMPLE); + when(applicationFormService.getApplicationFromSchemeId(SAMPLE_SCHEME_ID)) + .thenThrow(new NoSuchElementException()); + mockMvc.perform(get("/schemes/1/hasInternalApplicationForm")).andExpect(status().isOk()) + .andExpect(content().string("false")); + } + + @Test + void hasInternalApplicationForm_SchemeNotFound() throws Exception { + when(schemeService.getSchemeBySchemeId(SAMPLE_SCHEME_ID)).thenThrow(new EntityNotFoundException()); + mockMvc.perform(get("/schemes/1/hasInternalApplicationForm")).andExpect(status().isNotFound()) + .andExpect(content().string("")); + } + + @Test + void hasInternalApplicationForm_NoPermission() throws Exception { + when(schemeService.getSchemeBySchemeId(SAMPLE_SCHEME_ID)).thenThrow(new AccessDeniedException("")); + mockMvc.perform(get("/schemes/1/hasInternalApplicationForm")).andExpect(status().isForbidden()) + .andExpect(content().string("{\"error\":{\"message\":\"\"}}")); + } + }