Skip to content

Commit

Permalink
fix jpql query, add json ignore properties to avoid infinite nesting …
Browse files Browse the repository at this point in the history
…response (#80)

* fix jpql query, add json ignore properties to avoid infinite nesting in the responses

* address comments

* java format
  • Loading branch information
a-lor-cab authored Nov 20, 2023
1 parent 1ebd4ab commit 0bcc1fb
Show file tree
Hide file tree
Showing 7 changed files with 29 additions and 24 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -53,9 +53,8 @@ public ResponseEntity<Boolean> spotlightBatchWithStatusExist(@PathVariable final
@RequestParam(name = "batchSizeLimit", required = false,
defaultValue = "200") final String batchSizeLimit) {
log.info("Checking if a spotlight batch with status {} exists", status);
log.info("Batch size limit: {}", batchSizeLimit);
return ResponseEntity.ok()
.body(spotlightBatchService.spotlightBatchWithStatusExists(status, Integer.parseInt(batchSizeLimit)));
.body(spotlightBatchService.existsByStatusAndMaxBatchSize(status, Integer.parseInt(batchSizeLimit)));
}

@GetMapping("/status/{status}")
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package gov.cabinetoffice.gap.adminbackend.entities;

import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
import com.vladmihalcea.hibernate.type.array.EnumArrayType;
import com.vladmihalcea.hibernate.type.array.internal.AbstractArrayType;
import gov.cabinetoffice.gap.adminbackend.dtos.submission.GrantApplicant;
Expand Down Expand Up @@ -42,6 +43,7 @@ public class GrantMandatoryQuestions extends BaseEntity {

@OneToOne
@JoinColumn(name = "submission_id", referencedColumnName = "id")
@JsonIgnoreProperties({ "application", "scheme", "applicant", "createdBy", "lastUpdatedBy", "definition" })
private Submission submission;

@Column(name = "name")
Expand Down Expand Up @@ -84,17 +86,8 @@ public class GrantMandatoryQuestions extends BaseEntity {
@Enumerated(EnumType.STRING)
@ColumnTransformer(write = "?::grant_mandatory_question_status")
@Builder.Default
private GrantMandatoryQuestionStatus status = GrantMandatoryQuestionStatus.NOT_STARTED; // TODO
// what
// if
// the
// status
// is
// still
// in
// progress

@Column(name = "version", nullable = false)
private GrantMandatoryQuestionStatus status = GrantMandatoryQuestionStatus.NOT_STARTED;

@Builder.Default
private Integer version = 1;

Expand All @@ -104,13 +97,15 @@ public class GrantMandatoryQuestions extends BaseEntity {

@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "created_by", referencedColumnName = "id")
@JsonIgnoreProperties({ "submissions", "organisationProfile" })
private GrantApplicant createdBy;

@Column(name = "last_updated")
private Instant lastUpdated;

@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "last_updated_by", referencedColumnName = "id")
@JsonIgnoreProperties({ "submissions", "organisationProfile" })
private GrantApplicant lastUpdatedBy;

@Column
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package gov.cabinetoffice.gap.adminbackend.entities;

import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
import gov.cabinetoffice.gap.adminbackend.enums.SpotlightBatchStatus;
import lombok.AllArgsConstructor;
import lombok.Builder;
Expand All @@ -10,6 +11,8 @@

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.EnumType;
import javax.persistence.Enumerated;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
Expand All @@ -36,6 +39,7 @@ public class SpotlightBatch {

@Column(name = "status", nullable = false)
@Builder.Default
@Enumerated(EnumType.STRING)
private SpotlightBatchStatus status = SpotlightBatchStatus.QUEUED;

@Column
Expand All @@ -54,6 +58,7 @@ public class SpotlightBatch {
@ManyToMany
@JoinTable(name = "spotlight_batch_submission", joinColumns = @JoinColumn(name = "spotlight_batch_id"),
inverseJoinColumns = @JoinColumn(name = "spotlight_submission_id"))
@JsonIgnoreProperties("batches")
private List<SpotlightSubmission> spotlightSubmissions;

}
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import gov.cabinetoffice.gap.adminbackend.enums.SpotlightBatchStatus;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.query.Param;
import org.springframework.stereotype.Repository;

import java.util.Optional;
Expand All @@ -12,10 +13,13 @@
@Repository
public interface SpotlightBatchRepository extends JpaRepository<SpotlightBatch, UUID> {

@Query("select (count(s) > 0) from SpotlightBatch s where s.status = ?1 AND SIZE(s.spotlightSubmissions) < CAST(?2 AS int)")
Boolean existsByStatus(SpotlightBatchStatus status, int maxSize);
@Query("SELECT (COUNT(s) > 0 ) FROM SpotlightBatch s WHERE s.status = :status AND SIZE(s.spotlightSubmissions) < :maxSize")

@Query("SELECT sb FROM SpotlightBatch sb WHERE sb.status = ?1 AND SIZE(sb.spotlightSubmissions) < CAST(?2 AS int)")
Optional<SpotlightBatch> findByStatusAndSpotlightSubmissionsSizeLessThan(SpotlightBatchStatus status, int maxSize);
boolean existsByStatusAndSpotlightSubmissionsSizeLessThan(@Param("status") SpotlightBatchStatus status,
@Param("maxSize") int maxSize);

@Query("SELECT s FROM SpotlightBatch s WHERE s.status = :status AND SIZE(s.spotlightSubmissions) < :maxSize")
Optional<SpotlightBatch> findByStatusAndSpotlightSubmissionsSizeLessThan(
@Param("status") SpotlightBatchStatus status, @Param("maxSize") int maxSize);

}
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,8 @@ public class SpotlightBatchService {

private final SpotlightBatchRepository spotlightBatchRepository;

public Boolean spotlightBatchWithStatusExists(SpotlightBatchStatus status, int maxSize) {
return spotlightBatchRepository.existsByStatus(status, maxSize);
public boolean existsByStatusAndMaxBatchSize(SpotlightBatchStatus status, int maxSize) {
return spotlightBatchRepository.existsByStatusAndSpotlightSubmissionsSizeLessThan(status, maxSize);
}

public SpotlightBatch getSpotlightBatchWithStatus(SpotlightBatchStatus status, int maxSize) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ void successfullySpotlightBatchWithStatusExist() throws Exception {
final String batchSizeLimit = "150";
final Boolean expectedResult = true;

when(mockSpotlightBatchService.spotlightBatchWithStatusExists(status, Integer.parseInt(batchSizeLimit)))
when(mockSpotlightBatchService.existsByStatusAndMaxBatchSize(status, Integer.parseInt(batchSizeLimit)))
.thenReturn(expectedResult);

mockMvc.perform(get("/spotlight-batch/status/{status}/exists", status)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,19 +42,21 @@ class SpotlightBatchWithStatusExistsTests {

@Test
void spotlightBatchWithStatusExists() {
when(spotlightBatchRepository.existsByStatus(any(), anyInt())).thenReturn(true);
when(spotlightBatchRepository.existsByStatusAndSpotlightSubmissionsSizeLessThan(any(), anyInt()))
.thenReturn(true);

final boolean result = spotlightBatchService.spotlightBatchWithStatusExists(SpotlightBatchStatus.QUEUED,
final boolean result = spotlightBatchService.existsByStatusAndMaxBatchSize(SpotlightBatchStatus.QUEUED,
200);

assertTrue(result);
}

@Test
void spotlightBatchWithStatusDoesNotExist() {
when(spotlightBatchRepository.existsByStatus(any(), anyInt())).thenReturn(false);
when(spotlightBatchRepository.existsByStatusAndSpotlightSubmissionsSizeLessThan(any(), anyInt()))
.thenReturn(false);

final boolean result = spotlightBatchService.spotlightBatchWithStatusExists(SpotlightBatchStatus.QUEUED,
final boolean result = spotlightBatchService.existsByStatusAndMaxBatchSize(SpotlightBatchStatus.QUEUED,
200);

assertFalse(result);
Expand Down

0 comments on commit 0bcc1fb

Please sign in to comment.