Skip to content

Commit

Permalink
add dtos for SpotlightBatch and spotlightSubmission (#81)
Browse files Browse the repository at this point in the history
* add dto to get rid of hibernate issues due to lazy load of some entity relations

* java format
  • Loading branch information
a-lor-cab authored Nov 20, 2023
1 parent 0bcc1fb commit add8630
Show file tree
Hide file tree
Showing 11 changed files with 549 additions and 10 deletions.
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
package gov.cabinetoffice.gap.adminbackend.controllers;

import gov.cabinetoffice.gap.adminbackend.annotations.SpotlightPublisherHeaderValidator;
import gov.cabinetoffice.gap.adminbackend.dtos.spotlightBatch.SpotlightBatchDto;
import gov.cabinetoffice.gap.adminbackend.entities.SpotlightBatch;
import gov.cabinetoffice.gap.adminbackend.entities.SpotlightSubmission;
import gov.cabinetoffice.gap.adminbackend.enums.SpotlightBatchStatus;
import gov.cabinetoffice.gap.adminbackend.mappers.SpotlightBatchMapper;
import gov.cabinetoffice.gap.adminbackend.services.SpotlightBatchService;
import gov.cabinetoffice.gap.adminbackend.services.SpotlightSubmissionService;
import io.swagger.v3.oas.annotations.Operation;
Expand Down Expand Up @@ -36,6 +38,8 @@ public class SpotlightBatchController {

private final SpotlightSubmissionService spotlightSubmissionService;

private final SpotlightBatchMapper spotlightBatchMapper;

@GetMapping("/status/{status}/exists")
@Operation(summary = "Check if a spotlight batch with the given status exists")
@ApiResponses(value = {
Expand Down Expand Up @@ -72,12 +76,15 @@ public ResponseEntity<Boolean> spotlightBatchWithStatusExist(@PathVariable final
@ApiResponse(responseCode = "400", description = "Bad request",
content = @Content(mediaType = "application/json")) })
@SpotlightPublisherHeaderValidator
public ResponseEntity<SpotlightBatch> retrieveSpotlightBatchWithStatus(
public ResponseEntity<SpotlightBatchDto> 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)));

final SpotlightBatch spotlightBatch = spotlightBatchService.getSpotlightBatchWithStatus(status,
Integer.parseInt(batchSizeLimit));

return ResponseEntity.ok().body(spotlightBatchMapper.spotlightBatchToGetSpotlightBatchDto(spotlightBatch));
}

@PostMapping()
Expand All @@ -92,9 +99,12 @@ public ResponseEntity<SpotlightBatch> retrieveSpotlightBatchWithStatus(
@ApiResponse(responseCode = "400", description = "Bad request",
content = @Content(mediaType = "application/json")) })
@SpotlightPublisherHeaderValidator
public ResponseEntity<SpotlightBatch> createSpotlightBatch() {
public ResponseEntity<SpotlightBatchDto> createSpotlightBatch() {
log.info("Creating spotlight batch");
return ResponseEntity.ok().body(spotlightBatchService.createSpotlightBatch());

final SpotlightBatch spotlightBatch = spotlightBatchService.createSpotlightBatch();

return ResponseEntity.ok().body(spotlightBatchMapper.spotlightBatchToGetSpotlightBatchDto(spotlightBatch));
}

@PatchMapping("/{spotlightBatchId}/add-spotlight-submission/{spotlightSubmissionId}")
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
package gov.cabinetoffice.gap.adminbackend.controllers;

import gov.cabinetoffice.gap.adminbackend.annotations.SpotlightPublisherHeaderValidator;
import gov.cabinetoffice.gap.adminbackend.dtos.spotlightSubmissions.SpotlightSubmissionDto;
import gov.cabinetoffice.gap.adminbackend.entities.SpotlightSubmission;
import gov.cabinetoffice.gap.adminbackend.mappers.SpotlightSubmissionMapper;
import gov.cabinetoffice.gap.adminbackend.services.SpotlightSubmissionService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.media.Content;
Expand All @@ -28,6 +30,8 @@ public class SpotlightSubmissionController {

private final SpotlightSubmissionService spotlightSubmissionService;

private final SpotlightSubmissionMapper spotlightSubmissionMapper;

@GetMapping("/{spotlightSubmissionId}")
@Operation(summary = "Get spotlight submission by id")
@ApiResponses(value = {
Expand All @@ -42,11 +46,14 @@ public class SpotlightSubmissionController {
@ApiResponse(responseCode = "400", description = "Bad request",
content = @Content(mediaType = "application/json")) })
@SpotlightPublisherHeaderValidator
public ResponseEntity<SpotlightSubmission> getSpotlightSubmissionById(
public ResponseEntity<SpotlightSubmissionDto> getSpotlightSubmissionById(
@PathVariable final UUID spotlightSubmissionId) {

log.info("Getting spotlight submission with id {}", spotlightSubmissionId);
return ResponseEntity.ok().body(spotlightSubmissionService.getSpotlightSubmission(spotlightSubmissionId));
final SpotlightSubmission spotlightSubmission = spotlightSubmissionService
.getSpotlightSubmission(spotlightSubmissionId);
return ResponseEntity.ok()
.body(spotlightSubmissionMapper.spotlightSubmissionToSpotlightSubmissionDto(spotlightSubmission));
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
package gov.cabinetoffice.gap.adminbackend.dtos.spotlightBatch;

import gov.cabinetoffice.gap.adminbackend.dtos.spotlightSubmissions.SpotlightSubmissionDto;
import gov.cabinetoffice.gap.adminbackend.enums.SpotlightBatchStatus;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;

import java.time.Instant;
import java.util.List;
import java.util.UUID;

@Data
@Builder
@AllArgsConstructor
@NoArgsConstructor
public class SpotlightBatchDto {

private UUID id;

private SpotlightBatchStatus status;

private Instant lastSendAttempt;

private int version;

private Instant created = Instant.now();

private Instant lastUpdated;

private List<SpotlightSubmissionDto> spotlightSubmissions;

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
package gov.cabinetoffice.gap.adminbackend.dtos.spotlightSubmissions;

import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;

import java.math.BigDecimal;
import java.time.Instant;
import java.util.UUID;

@Data
@Builder
@AllArgsConstructor
@NoArgsConstructor
public class SpotlightMandatoryQuestionDto {

private UUID id;

private Integer schemeId;

private UUID submissionId;

private String name;

private String addressLine1;

private String addressLine2;

private String city;

private String county;

private String postcode;

private String orgType;

private String companiesHouseNumber;

private String charityCommissionNumber;

private BigDecimal fundingAmount;

private String[] fundingLocation;

private String status;

private Integer version;

private Instant created;

private long createdBy;

private String gapId;

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
package gov.cabinetoffice.gap.adminbackend.dtos.spotlightSubmissions;

import gov.cabinetoffice.gap.adminbackend.entities.SchemeEntity;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;

import java.time.Instant;
import java.util.UUID;

@Data
@Builder
@AllArgsConstructor
@NoArgsConstructor
public class SpotlightSubmissionDto {

private UUID id;

private SpotlightMandatoryQuestionDto mandatoryQuestions;

private SchemeEntity grantScheme;

private String status;

private Instant lastSendAttempt;

private int version;

private Instant created;

private Instant lastUpdated;

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
package gov.cabinetoffice.gap.adminbackend.mappers;

import gov.cabinetoffice.gap.adminbackend.dtos.spotlightBatch.SpotlightBatchDto;
import gov.cabinetoffice.gap.adminbackend.dtos.spotlightSubmissions.SpotlightMandatoryQuestionDto;
import gov.cabinetoffice.gap.adminbackend.dtos.spotlightSubmissions.SpotlightSubmissionDto;
import gov.cabinetoffice.gap.adminbackend.entities.GrantMandatoryQuestions;
import gov.cabinetoffice.gap.adminbackend.entities.SpotlightBatch;
import gov.cabinetoffice.gap.adminbackend.entities.SpotlightSubmission;
import org.mapstruct.Mapper;
import org.mapstruct.Mapping;
import org.mapstruct.Named;

import java.util.List;

@Mapper(componentModel = "spring")
public interface SpotlightBatchMapper {

@Mapping(source = "spotlightSubmissions", target = "spotlightSubmissions",
qualifiedByName = "mapSpotlightSubmissions")
SpotlightBatchDto spotlightBatchToGetSpotlightBatchDto(SpotlightBatch spotlightBatch);

@Named("mapSpotlightSubmissions")
default List<SpotlightSubmissionDto> mapSpotlightSubmission(List<SpotlightSubmission> spotlightSubmissions) {
return spotlightSubmissionListToSpotlightSubmissionDtoList(spotlightSubmissions);
}

@Mapping(source = "mandatoryQuestions", target = "mandatoryQuestions", qualifiedByName = "mapMandatoryQuestions")
SpotlightSubmissionDto spotlightSubmissionToSpotlightSubmissionDto(SpotlightSubmission spotlightSubmission);

@Named("mapMandatoryQuestions")
default SpotlightMandatoryQuestionDto mapMandatoryQuestions(GrantMandatoryQuestions mandatoryQuestions) {
return mandatoryQuestionsToSpotlightMandatoryQuestions(mandatoryQuestions);
}

@Mapping(source = "schemeEntity.id", target = "schemeId")
@Mapping(source = "submission.id", target = "submissionId")
@Mapping(source = "createdBy.id", target = "createdBy")
SpotlightMandatoryQuestionDto mandatoryQuestionsToSpotlightMandatoryQuestions(
GrantMandatoryQuestions mandatoryQuestions);

default List<SpotlightSubmissionDto> spotlightSubmissionListToSpotlightSubmissionDtoList(
List<SpotlightSubmission> submissions) {
return submissions.stream().map(this::spotlightSubmissionToSpotlightSubmissionDto).toList();
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package gov.cabinetoffice.gap.adminbackend.mappers;

import gov.cabinetoffice.gap.adminbackend.dtos.spotlightSubmissions.SpotlightMandatoryQuestionDto;
import gov.cabinetoffice.gap.adminbackend.dtos.spotlightSubmissions.SpotlightSubmissionDto;
import gov.cabinetoffice.gap.adminbackend.entities.GrantMandatoryQuestions;
import gov.cabinetoffice.gap.adminbackend.entities.SpotlightSubmission;
import org.mapstruct.Mapper;
import org.mapstruct.Mapping;
import org.mapstruct.Named;

@Mapper(componentModel = "spring")
public interface SpotlightSubmissionMapper {

@Mapping(source = "mandatoryQuestions", target = "mandatoryQuestions", qualifiedByName = "mapMandatoryQuestions")
SpotlightSubmissionDto spotlightSubmissionToSpotlightSubmissionDto(SpotlightSubmission spotlightSubmission);

@Named("mapMandatoryQuestions")
default SpotlightMandatoryQuestionDto mapMandatoryQuestions(GrantMandatoryQuestions mandatoryQuestions) {
return mandatoryQuestionsToSpotlightMandatoryQuestions(mandatoryQuestions);
}

@Mapping(source = "schemeEntity.id", target = "schemeId")
@Mapping(source = "submission.id", target = "submissionId")
@Mapping(source = "createdBy.id", target = "createdBy")
SpotlightMandatoryQuestionDto mandatoryQuestionsToSpotlightMandatoryQuestions(
GrantMandatoryQuestions mandatoryQuestions);

}
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
package gov.cabinetoffice.gap.adminbackend.controllers;

import gov.cabinetoffice.gap.adminbackend.config.SpotlightPublisherInterceptor;
import gov.cabinetoffice.gap.adminbackend.dtos.spotlightBatch.SpotlightBatchDto;
import gov.cabinetoffice.gap.adminbackend.entities.SpotlightBatch;
import gov.cabinetoffice.gap.adminbackend.entities.SpotlightSubmission;
import gov.cabinetoffice.gap.adminbackend.enums.SpotlightBatchStatus;
import gov.cabinetoffice.gap.adminbackend.exceptions.NotFoundException;
import gov.cabinetoffice.gap.adminbackend.mappers.SpotlightBatchMapper;
import gov.cabinetoffice.gap.adminbackend.mappers.ValidationErrorMapper;
import gov.cabinetoffice.gap.adminbackend.security.interceptors.AuthorizationHeaderInterceptor;
import gov.cabinetoffice.gap.adminbackend.services.SpotlightBatchService;
Expand Down Expand Up @@ -57,6 +59,9 @@ public class SpotlightBatchControllerTest {
@MockBean
private SpotlightPublisherInterceptor mockSpotlightPublisherInterceptor;

@MockBean
private SpotlightBatchMapper mockSpotlightBatchMapper;

@Nested
class spotlightBatchWithStatusExist {

Expand Down Expand Up @@ -88,9 +93,12 @@ class retrieveSpotlightBatchWithStatus {
void successfullyRetrieveSpotlightBatchWithStatus() throws Exception {
final SpotlightBatchStatus status = SpotlightBatchStatus.QUEUED;
final String batchSizeLimit = "150";
final SpotlightBatch expectedResult = SpotlightBatch.builder().id(uuid).build();
final SpotlightBatch spotlightBatch = SpotlightBatch.builder().id(uuid).build();
final SpotlightBatchDto expectedResult = SpotlightBatchDto.builder().id(uuid).build();

when(mockSpotlightBatchService.getSpotlightBatchWithStatus(status, Integer.parseInt(batchSizeLimit)))
.thenReturn(spotlightBatch);
when(mockSpotlightBatchMapper.spotlightBatchToGetSpotlightBatchDto(spotlightBatch))
.thenReturn(expectedResult);

mockMvc.perform(get("/spotlight-batch/status/{status}", status).param("batchSizeLimit", batchSizeLimit)
Expand Down Expand Up @@ -122,9 +130,12 @@ class createSpotlightBatch {

@Test
void successfullyCreateSpotlightBatch() throws Exception {
final SpotlightBatch expectedResult = SpotlightBatch.builder().id(uuid).build();
final SpotlightBatch spotlightBatch = SpotlightBatch.builder().id(uuid).build();
final SpotlightBatchDto expectedResult = SpotlightBatchDto.builder().id(uuid).build();

when(mockSpotlightBatchService.createSpotlightBatch()).thenReturn(expectedResult);
when(mockSpotlightBatchService.createSpotlightBatch()).thenReturn(spotlightBatch);
when(mockSpotlightBatchMapper.spotlightBatchToGetSpotlightBatchDto(spotlightBatch))
.thenReturn(expectedResult);

mockMvc.perform(post("/spotlight-batch").header(HttpHeaders.AUTHORIZATION, LAMBDA_AUTH_HEADER))
.andExpect(status().isOk()).andExpect(jsonPath("$.id").exists());
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
package gov.cabinetoffice.gap.adminbackend.controllers;

import gov.cabinetoffice.gap.adminbackend.config.SpotlightPublisherInterceptor;
import gov.cabinetoffice.gap.adminbackend.dtos.spotlightSubmissions.SpotlightSubmissionDto;
import gov.cabinetoffice.gap.adminbackend.entities.SpotlightSubmission;
import gov.cabinetoffice.gap.adminbackend.exceptions.NotFoundException;
import gov.cabinetoffice.gap.adminbackend.mappers.SpotlightSubmissionMapper;
import gov.cabinetoffice.gap.adminbackend.mappers.ValidationErrorMapper;
import gov.cabinetoffice.gap.adminbackend.security.interceptors.AuthorizationHeaderInterceptor;
import gov.cabinetoffice.gap.adminbackend.services.SpotlightSubmissionService;
Expand Down Expand Up @@ -47,6 +49,9 @@ class SpotlightSubmissionControllerTest {
@MockBean
private SpotlightPublisherInterceptor mockSpotlightPublisherInterceptor;

@MockBean
private SpotlightSubmissionMapper spotlightSubmissionMapper;

@Nested
class getSpotlightSubmissionById {

Expand All @@ -56,9 +61,13 @@ void successfullyRetrieveSpotlightSubmission() throws Exception {
final Instant now = Instant.now();
final SpotlightSubmission spotlightSubmission = SpotlightSubmission.builder().id(spotlightSubmissionId)
.created(now).build();
final SpotlightSubmissionDto expectedResult = SpotlightSubmissionDto.builder().id(spotlightSubmissionId)
.created(now).build();

when(mockSpotlightSubmissionService.getSpotlightSubmission(spotlightSubmissionId))
.thenReturn(spotlightSubmission);
when(spotlightSubmissionMapper.spotlightSubmissionToSpotlightSubmissionDto(spotlightSubmission))
.thenReturn(expectedResult);

mockMvc.perform(get("/spotlight-submissions/{spotlightSubmissionId}", spotlightSubmissionId)
.header(HttpHeaders.AUTHORIZATION, LAMBDA_AUTH_HEADER)).andExpect(status().isOk())
Expand Down
Loading

0 comments on commit add8630

Please sign in to comment.