-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add controller for spotlightSubmission (#75)
* add controller for spotlightSubmission * run javaformat apply
- Loading branch information
Showing
4 changed files
with
147 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
52 changes: 52 additions & 0 deletions
52
...in/java/gov/cabinetoffice/gap/adminbackend/controllers/SpotlightSubmissionController.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
package gov.cabinetoffice.gap.adminbackend.controllers; | ||
|
||
import gov.cabinetoffice.gap.adminbackend.annotations.SpotlightPublisherHeaderValidator; | ||
import gov.cabinetoffice.gap.adminbackend.entities.SpotlightSubmission; | ||
import gov.cabinetoffice.gap.adminbackend.services.SpotlightSubmissionService; | ||
import io.swagger.v3.oas.annotations.Operation; | ||
import io.swagger.v3.oas.annotations.media.Content; | ||
import io.swagger.v3.oas.annotations.media.Schema; | ||
import io.swagger.v3.oas.annotations.responses.ApiResponse; | ||
import io.swagger.v3.oas.annotations.responses.ApiResponses; | ||
import io.swagger.v3.oas.annotations.tags.Tag; | ||
import lombok.RequiredArgsConstructor; | ||
import lombok.extern.log4j.Log4j2; | ||
import org.springframework.http.ResponseEntity; | ||
import org.springframework.web.bind.annotation.GetMapping; | ||
import org.springframework.web.bind.annotation.PathVariable; | ||
import org.springframework.web.bind.annotation.RequestMapping; | ||
import org.springframework.web.bind.annotation.RestController; | ||
|
||
import java.util.UUID; | ||
|
||
@Log4j2 | ||
@RestController | ||
@RequestMapping("/spotlight-submissions") | ||
@Tag(name = "Spotlight Submission", description = "API for handling spotlight submissions") | ||
@RequiredArgsConstructor | ||
public class SpotlightSubmissionController { | ||
|
||
private final SpotlightSubmissionService spotlightSubmissionService; | ||
|
||
@GetMapping("/{spotlightSubmissionId}") | ||
@Operation(summary = "Get spotlight submission by id") | ||
@ApiResponses(value = { | ||
@ApiResponse(responseCode = "200", description = "Successfully retrieved spotlight submission by id", | ||
content = @Content(mediaType = "application/json", | ||
schema = @Schema(implementation = Boolean.class))), | ||
@ApiResponse(responseCode = "404", | ||
description = "Spotlight spotlight submission for the given id does not exist", | ||
content = @Content(mediaType = "application/json")), | ||
@ApiResponse(responseCode = "403", description = "Insufficient permissions to get spotlight submission", | ||
content = @Content(mediaType = "application/json")), | ||
@ApiResponse(responseCode = "400", description = "Bad request", | ||
content = @Content(mediaType = "application/json")) }) | ||
@SpotlightPublisherHeaderValidator | ||
public ResponseEntity<SpotlightSubmission> getSpotlightSubmissionById( | ||
@PathVariable final UUID spotlightSubmissionId) { | ||
|
||
log.info("Getting spotlight submission with id {}", spotlightSubmissionId); | ||
return ResponseEntity.ok().body(spotlightSubmissionService.getSpotlightSubmission(spotlightSubmissionId)); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
82 changes: 82 additions & 0 deletions
82
...ava/gov/cabinetoffice/gap/adminbackend/controllers/SpotlightSubmissionControllerTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,82 @@ | ||
package gov.cabinetoffice.gap.adminbackend.controllers; | ||
|
||
import gov.cabinetoffice.gap.adminbackend.config.SpotlightPublisherInterceptor; | ||
import gov.cabinetoffice.gap.adminbackend.entities.SpotlightSubmission; | ||
import gov.cabinetoffice.gap.adminbackend.exceptions.NotFoundException; | ||
import gov.cabinetoffice.gap.adminbackend.mappers.ValidationErrorMapper; | ||
import gov.cabinetoffice.gap.adminbackend.security.interceptors.AuthorizationHeaderInterceptor; | ||
import gov.cabinetoffice.gap.adminbackend.services.SpotlightSubmissionService; | ||
import org.junit.jupiter.api.Nested; | ||
import org.junit.jupiter.api.Test; | ||
import org.springframework.beans.factory.annotation.Autowired; | ||
import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc; | ||
import org.springframework.boot.test.autoconfigure.web.servlet.WebMvcTest; | ||
import org.springframework.boot.test.mock.mockito.MockBean; | ||
import org.springframework.http.HttpHeaders; | ||
import org.springframework.test.context.ContextConfiguration; | ||
import org.springframework.test.web.servlet.MockMvc; | ||
|
||
import java.time.Instant; | ||
import java.util.UUID; | ||
|
||
import static org.mockito.Mockito.when; | ||
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get; | ||
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.jsonPath; | ||
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status; | ||
|
||
@WebMvcTest(SchemeController.class) | ||
@AutoConfigureMockMvc(addFilters = false) | ||
@ContextConfiguration(classes = { SpotlightSubmissionController.class, ControllerExceptionHandler.class, | ||
SpotlightPublisherInterceptor.class }) | ||
class SpotlightSubmissionControllerTest { | ||
|
||
private final String LAMBDA_AUTH_HEADER = "topSecretKey"; | ||
|
||
@Autowired | ||
private MockMvc mockMvc; | ||
|
||
@MockBean | ||
private SpotlightSubmissionService mockSpotlightSubmissionService; | ||
|
||
@MockBean | ||
private AuthorizationHeaderInterceptor mockAuthorizationHeaderInterceptor; | ||
|
||
@MockBean | ||
private ValidationErrorMapper mockValidationErrorMapper; | ||
|
||
@MockBean | ||
private SpotlightPublisherInterceptor mockSpotlightPublisherInterceptor; | ||
|
||
@Nested | ||
class getSpotlightSubmissionById { | ||
|
||
@Test | ||
void successfullyRetrieveSpotlightSubmission() throws Exception { | ||
final UUID spotlightSubmissionId = UUID.randomUUID(); | ||
final Instant now = Instant.now(); | ||
final SpotlightSubmission spotlightSubmission = SpotlightSubmission.builder().id(spotlightSubmissionId) | ||
.created(now).build(); | ||
|
||
when(mockSpotlightSubmissionService.getSpotlightSubmission(spotlightSubmissionId)) | ||
.thenReturn(spotlightSubmission); | ||
|
||
mockMvc.perform(get("/spotlight-submissions/{spotlightSubmissionId}", spotlightSubmissionId) | ||
.header(HttpHeaders.AUTHORIZATION, LAMBDA_AUTH_HEADER)).andExpect(status().isOk()) | ||
.andExpect(jsonPath("$.id").exists()); | ||
} | ||
|
||
@Test | ||
void notFoundAddSpotlightSubmissionToSpotlightBatch() throws Exception { | ||
|
||
final UUID spotlightSubmissionId = UUID.randomUUID(); | ||
|
||
when(mockSpotlightSubmissionService.getSpotlightSubmission(spotlightSubmissionId)) | ||
.thenThrow(NotFoundException.class); | ||
|
||
mockMvc.perform(get("/spotlight-submissions/{spotlightSubmissionId}", spotlightSubmissionId) | ||
.header(HttpHeaders.AUTHORIZATION, LAMBDA_AUTH_HEADER)).andExpect(status().isNotFound()); | ||
} | ||
|
||
} | ||
|
||
} |