Skip to content

Commit

Permalink
add controller for spotlightSubmission (#75)
Browse files Browse the repository at this point in the history
* add controller for spotlightSubmission

* run javaformat apply
  • Loading branch information
a-lor-cab authored Nov 17, 2023
1 parent 43c7d40 commit a37c65e
Show file tree
Hide file tree
Showing 4 changed files with 147 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ public class SpotlightBatchController {
public ResponseEntity<Boolean> spotlightBatchWithStatusExist(@PathVariable final SpotlightBatchStatus status,
@RequestParam(name = "batchSizeLimit", required = false,
defaultValue = "200") final String batchSizeLimit) {
log.info("Checking if a spotlight batch with status {} exists", status);
return ResponseEntity.ok()
.body(spotlightBatchService.spotlightBatchWithStatusExists(status, Integer.parseInt(batchSizeLimit)));
}
Expand All @@ -74,6 +75,7 @@ public ResponseEntity<Boolean> spotlightBatchWithStatusExist(@PathVariable final
public ResponseEntity<SpotlightBatch> retrieveSpotlightBatchWithStatus(
@PathVariable final SpotlightBatchStatus status, @RequestParam(name = "batchSizeLimit", required = false,
defaultValue = "200") final String batchSizeLimit) {
log.info("Retrieving spotlight batch with status {}", status);
return ResponseEntity.ok()
.body(spotlightBatchService.getSpotlightBatchWithStatus(status, Integer.parseInt(batchSizeLimit)));
}
Expand All @@ -91,6 +93,7 @@ public ResponseEntity<SpotlightBatch> retrieveSpotlightBatchWithStatus(
content = @Content(mediaType = "application/json")) })
@SpotlightPublisherHeaderValidator
public ResponseEntity<SpotlightBatch> createSpotlightBatch() {
log.info("Creating spotlight batch");
return ResponseEntity.ok().body(spotlightBatchService.createSpotlightBatch());
}

Expand All @@ -108,12 +111,20 @@ public ResponseEntity<SpotlightBatch> createSpotlightBatch() {
@SpotlightPublisherHeaderValidator
public ResponseEntity<String> addSpotlightSubmissionToSpotlightBatch(@PathVariable final UUID spotlightBatchId,
@PathVariable final UUID spotlightSubmissionId) {
log.info("Adding spotlight submission with id {} to spotlight batch with id {}", spotlightSubmissionId,
spotlightBatchId);

final SpotlightSubmission spotlightSubmission = spotlightSubmissionService
.getSpotlightSubmission(spotlightSubmissionId);

final SpotlightBatch spotlightBatch = spotlightBatchService
.addSpotlightSubmissionToSpotlightBatch(spotlightSubmission, spotlightBatchId);
log.info("Spotlight submission with id {} added to spotlight batch with id {}", spotlightSubmissionId,
spotlightBatchId);

spotlightSubmissionService.addSpotlightBatchToSpotlightSubmission(spotlightSubmissionId, spotlightBatch);
log.info("Spotlight batch with id {} added to spotlight submission with id {}", spotlightBatchId,
spotlightSubmissionId);

return ResponseEntity.ok()
.body(String.format("Spotlight submission with id %s added to spotlight batch with id %s",
Expand Down
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));
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -164,8 +164,8 @@ void successfullyAddSpotlightSubmissionToSpotlightBatch() throws Exception {

@Test
void notFoundAddSpotlightSubmissionToSpotlightBatch() throws Exception {
UUID spotlightBatchId = UUID.randomUUID();
UUID spotlightSubmissionId = UUID.randomUUID();
final UUID spotlightBatchId = UUID.randomUUID();
final UUID spotlightSubmissionId = UUID.randomUUID();

when(mockSpotlightSubmissionService.getSpotlightSubmission(spotlightSubmissionId))
.thenThrow(NotFoundException.class);
Expand Down
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());
}

}

}

0 comments on commit a37c65e

Please sign in to comment.