Skip to content

Commit

Permalink
Remove JurorPoolUtils.getSingleActiveJurorPool() and replace with jur…
Browse files Browse the repository at this point in the history
…orPoolSerive.getActiveRecordFromUser() (#744)

* Updated the way we retrieve juror pools to prevent non-unique entry errors

* Applied review comments

* Fixed styles / merged stabilisation13
  • Loading branch information
Ben-Edwards-cgi authored Aug 23, 2024
1 parent bd5852d commit a2db75d
Show file tree
Hide file tree
Showing 29 changed files with 548 additions and 859 deletions.
2 changes: 1 addition & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -287,7 +287,7 @@ pmdTest {
maxFailures = 292
}
pmdMain {
maxFailures = 771
maxFailures = 763
}
pmd {
maxFailures = 0
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
import uk.gov.hmcts.juror.api.moj.repository.ContactLogRepository;
import uk.gov.hmcts.juror.api.moj.repository.JurorHistoryRepository;
import uk.gov.hmcts.juror.api.moj.repository.JurorPoolRepository;
import uk.gov.hmcts.juror.api.moj.repository.JurorRepository;
import uk.gov.hmcts.juror.api.moj.repository.jurorresponse.JurorPaperResponseRepositoryMod;
import uk.gov.hmcts.juror.api.moj.utils.DataUtils;
import uk.gov.hmcts.juror.api.moj.utils.JurorPoolUtils;
Expand All @@ -42,6 +43,7 @@
*/
@RunWith(SpringRunner.class)
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
@SuppressWarnings("PMD.ExcessiveImports")
public class DeceasedResponseControllerITest extends AbstractIntegrationTest {

private HttpHeaders httpHeaders;
Expand All @@ -58,6 +60,8 @@ public class DeceasedResponseControllerITest extends AbstractIntegrationTest {
private ContactLogRepository contactLogRepository;
@Autowired
private JurorPaperResponseRepositoryMod jurorPaperResponseRepository;
@Autowired
private JurorRepository jurorRepository;

@Before
public void setUp() throws Exception {
Expand Down Expand Up @@ -155,7 +159,7 @@ public void markJurorAsDeceased_BureauUser_paperResponseDoesNotExists() {
executeInTransaction(() -> {
// verify the status of the juror record has been updated

Juror juror = JurorPoolUtils.getActiveJurorRecord(jurorPoolRepository, jurorNumber);
Juror juror = jurorRepository.findByJurorNumber(jurorNumber);
CourtLocation courtLocation = new CourtLocation();
courtLocation.setLocCode("415");
JurorPool jurorPool = JurorPoolUtils.getActiveJurorPool(jurorPoolRepository, jurorNumber, courtLocation);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,6 @@
import uk.gov.hmcts.juror.api.moj.repository.JurorRepository;
import uk.gov.hmcts.juror.api.moj.repository.PoolRequestRepository;
import uk.gov.hmcts.juror.api.moj.utils.DateUtils;
import uk.gov.hmcts.juror.api.moj.utils.JurorPoolUtils;

import java.net.URI;
import java.time.LocalDate;
Expand Down Expand Up @@ -853,7 +852,7 @@ void bureauProcessJurorActivePoolPaper() {
verifyActivePoolOldRecord(jurorPools.get(0));

// grab new record to verify it has been created and the properties have been updated correctly
Juror newJurorRecord = JurorPoolUtils.getActiveJurorRecord(jurorPoolRepository, JUROR_555555561);
Juror newJurorRecord = jurorRepository.findByJurorNumber(JUROR_555555561);

verifyActiveJurorNewRecord(newJurorRecord,
deferralReasonRequestDto.getPoolNumber(), deferralReasonRequestDto.getDeferralDate());
Expand Down Expand Up @@ -900,7 +899,7 @@ void courtProcessJurorActivePoolDigital() {
verifyActivePoolOldRecord(jurorPools.get(0));

// grab new record to verify it has been created and the properties have been updated correctly
Juror newJurorRecord = JurorPoolUtils.getActiveJurorRecord(jurorPoolRepository, JUROR_555555558);
Juror newJurorRecord = jurorRepository.findByJurorNumber(JUROR_555555558);
verifyActiveJurorNewRecord(newJurorRecord,
deferralReasonRequestDto.getPoolNumber(), deferralReasonRequestDto.getDeferralDate());

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,6 @@
import uk.gov.hmcts.juror.api.moj.repository.UserRepository;
import uk.gov.hmcts.juror.api.moj.repository.jurorresponse.JurorDigitalResponseRepositoryMod;
import uk.gov.hmcts.juror.api.moj.repository.jurorresponse.JurorPaperResponseRepositoryMod;
import uk.gov.hmcts.juror.api.moj.utils.JurorPoolUtils;

import java.net.URI;
import java.time.LocalDate;
Expand Down Expand Up @@ -110,7 +109,8 @@ public void refuseExcusalRequest_paperResponse_bureauUser_bureauOwner() throws E
PaperResponse jurorPaperResponse = jurorPaperResponseRepository.findByJurorNumber(jurorNumber);
validatePaperResponseExcusal(jurorPaperResponse, login);

JurorPool jurorPool = JurorPoolUtils.getSingleActiveJurorPool(jurorPoolRepository, jurorNumber);
JurorPool jurorPool = jurorPoolRepository
.findByPoolCourtLocationLocCodeAndJurorJurorNumberAndIsActiveTrue("415", jurorNumber);
validateRefuseExcusal(jurorPool, excusalDecisionDto, login);
validateExcusalDeniedLetter();
});
Expand All @@ -137,7 +137,8 @@ public void refuseExcusalRequest_paperResponse_courtUser_courtOwner() throws Exc
jurorPaperResponseRepository.findByJurorNumber(jurorNumber);
validatePaperResponseExcusal(jurorPaperResponse, login);

JurorPool jurorPool = JurorPoolUtils.getSingleActiveJurorPool(jurorPoolRepository, jurorNumber);
JurorPool jurorPool = jurorPoolRepository
.findByPoolCourtLocationLocCodeAndJurorJurorNumberAndIsActiveTrue("415", jurorNumber);
validateRefuseExcusal(jurorPool, excusalDecisionDto, login);

Iterable<BulkPrintData> bulkPrintDataIterable = bulkPrintDataRepository.findAll();
Expand Down Expand Up @@ -178,7 +179,8 @@ public void grantExcusalRequest_paperResponse_bureauUser_bureauOwner() throws Ex
jurorPaperResponseRepository.findByJurorNumber(jurorNumber);
validatePaperResponseExcusal(jurorPaperResponse, login);

JurorPool jurorPool = JurorPoolUtils.getSingleActiveJurorPool(jurorPoolRepository, jurorNumber);
JurorPool jurorPool = jurorPoolRepository
.findByPoolCourtLocationLocCodeAndJurorJurorNumberAndIsActiveTrue("415", jurorNumber);
validateExcusal(jurorPool, excusalDecisionDto, login);

validateExcusalLetter(excusalDecisionDto.getExcusalReasonCode());
Expand Down Expand Up @@ -207,7 +209,8 @@ public void grantExcusalRequest_paperResponse_courtUser_courtOwner() throws Exce
jurorPaperResponseRepository.findByJurorNumber(jurorNumber);
validatePaperResponseExcusal(jurorPaperResponse, login);

JurorPool jurorPool = JurorPoolUtils.getSingleActiveJurorPool(jurorPoolRepository, jurorNumber);
JurorPool jurorPool = jurorPoolRepository
.findByPoolCourtLocationLocCodeAndJurorJurorNumberAndIsActiveTrue("415", jurorNumber);
validateExcusal(jurorPool, excusalDecisionDto, login);
});
}
Expand All @@ -233,7 +236,8 @@ public void refuseExcusalRequest_digitalResponse_bureauUser_bureauOwner() throws
DigitalResponse jurorResponse = jurorResponseRepository.findByJurorNumber(jurorNumber);
validateDigitalResponseExcusal(jurorResponse, login);

JurorPool jurorPool = JurorPoolUtils.getSingleActiveJurorPool(jurorPoolRepository, jurorNumber);
JurorPool jurorPool = jurorPoolRepository
.findByPoolCourtLocationLocCodeAndJurorJurorNumberAndIsActiveTrue("415", jurorNumber);
validateRefuseExcusal(jurorPool, excusalDecisionDto, login);
validateExcusalDeniedLetter();
});
Expand All @@ -260,7 +264,8 @@ public void refuseExcusalRequest_digitalResponse_courtUser_courtOwner() throws E
DigitalResponse jurorResponse = jurorResponseRepository.findByJurorNumber(jurorNumber);
validateDigitalResponseExcusal(jurorResponse, login);

JurorPool jurorPool = JurorPoolUtils.getSingleActiveJurorPool(jurorPoolRepository, jurorNumber);
JurorPool jurorPool = jurorPoolRepository
.findByPoolCourtLocationLocCodeAndJurorJurorNumberAndIsActiveTrue("415", jurorNumber);
validateRefuseExcusal(jurorPool, excusalDecisionDto, login);
Iterable<BulkPrintData> bulkPrintDataIterable = bulkPrintDataRepository.findAll();
List<BulkPrintData> bulkPrintData = new ArrayList<>();
Expand Down Expand Up @@ -300,7 +305,8 @@ public void grantExcusalRequest_digitalResponse_bureauUser_bureauOwner() throws
DigitalResponse jurorResponse = jurorResponseRepository.findByJurorNumber(jurorNumber);
validateDigitalResponseExcusal(jurorResponse, login);

JurorPool jurorPool = JurorPoolUtils.getSingleActiveJurorPool(jurorPoolRepository, jurorNumber);
JurorPool jurorPool = jurorPoolRepository
.findByPoolCourtLocationLocCodeAndJurorJurorNumberAndIsActiveTrue("415", jurorNumber);
validateExcusal(jurorPool, excusalDecisionDto, login);
validateExcusalLetter(excusalDecisionDto.getExcusalReasonCode());
});
Expand Down Expand Up @@ -328,7 +334,8 @@ public void grantExcusalRequest_digitalResponse_courtUser_courtOwner() throws Ex
DigitalResponse jurorResponse = jurorResponseRepository.findByJurorNumber(jurorNumber);
validateDigitalResponseExcusal(jurorResponse, login);

JurorPool jurorPool = JurorPoolUtils.getSingleActiveJurorPool(jurorPoolRepository, jurorNumber);
JurorPool jurorPool = jurorPoolRepository
.findByPoolCourtLocationLocCodeAndJurorJurorNumberAndIsActiveTrue("415", jurorNumber);
validateExcusal(jurorPool, excusalDecisionDto, login);
});
}
Expand All @@ -355,7 +362,8 @@ public void grantExcusalRequest_excusalCodeDeceased() throws Exception {
PaperResponse jurorPaperResponse = jurorPaperResponseRepository.findByJurorNumber(jurorNumber);
validatePaperResponseExcusal(jurorPaperResponse, login);

JurorPool jurorPool = JurorPoolUtils.getSingleActiveJurorPool(jurorPoolRepository, jurorNumber);
JurorPool jurorPool = jurorPoolRepository
.findByPoolCourtLocationLocCodeAndJurorJurorNumberAndIsActiveTrue("415", jurorNumber);
validateExcusal(jurorPool, excusalDecisionDto, login);
validateExcusalLetter(excusalDecisionDto.getExcusalReasonCode());
});
Expand Down Expand Up @@ -420,7 +428,8 @@ public void excusalRequest_paperResponse_alreadyProcessed() throws Exception {
PaperResponse jurorPaperResponse = jurorPaperResponseRepository.findByJurorNumber(jurorNumber);
validatePaperResponseExcusal(jurorPaperResponse, login);

JurorPool jurorPool = JurorPoolUtils.getSingleActiveJurorPool(jurorPoolRepository, jurorNumber);
JurorPool jurorPool = jurorPoolRepository
.findByPoolCourtLocationLocCodeAndJurorJurorNumberAndIsActiveTrue("415", jurorNumber);
validateExcusal(jurorPool, excusalDecisionDto, login);
validateExcusalLetter(excusalDecisionDto.getExcusalReasonCode());
});
Expand Down Expand Up @@ -483,7 +492,8 @@ public void excusalRequest_paperResponse_doesNotExist() throws Exception {
ResponseEntity<String> response = template.exchange(requestEntity, String.class);
assertThat(response.getStatusCode()).isEqualTo(HttpStatus.OK);
executeInTransaction(() -> {
JurorPool jurorPool = JurorPoolUtils.getSingleActiveJurorPool(jurorPoolRepository, jurorNumber);
JurorPool jurorPool = jurorPoolRepository
.findByPoolCourtLocationLocCodeAndJurorJurorNumberAndIsActiveTrue("415", jurorNumber);
validateExcusal(jurorPool, excusalDecisionDto, login);
validateExcusalLetter(excusalDecisionDto.getExcusalReasonCode());
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@
import org.springframework.test.context.jdbc.Sql;
import org.springframework.test.context.junit4.SpringRunner;
import uk.gov.hmcts.juror.api.AbstractIntegrationTest;
import uk.gov.hmcts.juror.api.config.bureau.BureauJwtPayload;
import uk.gov.hmcts.juror.api.juror.domain.ProcessingStatus;
import uk.gov.hmcts.juror.api.juror.domain.WelshCourtLocationRepository;
import uk.gov.hmcts.juror.api.moj.controller.request.CjsEmploymentDetailsDto;
Expand Down Expand Up @@ -53,7 +52,6 @@
import java.time.LocalDateTime;
import java.time.temporal.ChronoUnit;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import java.util.Optional;
Expand Down Expand Up @@ -170,7 +168,7 @@ public void respondToSummons_courtUser_noAccess() throws Exception {
HttpMethod.POST, uri);
ResponseEntity<SaveJurorPaperReplyResponseDto> response = template.exchange(requestEntity,
SaveJurorPaperReplyResponseDto.class);
assertThat(response.getStatusCode()).isEqualTo(HttpStatus.FORBIDDEN);
assertThat(response.getStatusCode()).isEqualTo(HttpStatus.NOT_FOUND);
}

@Test
Expand Down Expand Up @@ -221,7 +219,7 @@ public void respondToSummons_courtUser_bureauOwnedJurorRecord() throws Exception
HttpMethod.POST, uri);
ResponseEntity<SaveJurorPaperReplyResponseDto> response = template.exchange(requestEntity,
SaveJurorPaperReplyResponseDto.class);
assertThat(response.getStatusCode()).isEqualTo(HttpStatus.FORBIDDEN);
assertThat(response.getStatusCode()).isEqualTo(HttpStatus.NOT_FOUND);
}

@Test
Expand Down Expand Up @@ -1764,22 +1762,10 @@ public void updateJurorPaperResponseStatus_bureauUser_noJurorRecord() throws Exc
assertThat(response.getStatusCode()).isEqualTo(HttpStatus.NOT_FOUND);
}

@Test
@Sql({"/db/mod/truncate.sql", "/db/JurorPaperResponse_updatePaperResponseStatus.sql"})
public void updateJurorPaperResponseStatus_bureauUser_multipleJurorRecords() throws Exception {
final String bureauJwt = createJwtBureau("BUREAU_USER");
final URI uri = URI.create("/api/v1/moj/juror-paper-response/update-status/444444444/CLOSED");

httpHeaders.set(HttpHeaders.AUTHORIZATION, bureauJwt);
RequestEntity<Void> requestEntity = new RequestEntity<>(httpHeaders, HttpMethod.PUT, uri);
ResponseEntity<Void> response = template.exchange(requestEntity, Void.class);
assertThat(response.getStatusCode()).isEqualTo(HttpStatus.INTERNAL_SERVER_ERROR);
}

@Test
@Sql({"/db/mod/truncate.sql", "/db/JurorPaperResponse_updatePaperResponseStatus.sql"})
public void updateJurorPaperResponseStatus_courtUser_thirdParty() throws Exception {
final String bureauJwt = initCourtsJwt("411", Arrays.asList("411", "774"));
final String bureauJwt = getSatelliteCourtJwt("411","411", "774");
final URI uri = URI.create("/api/v1/moj/juror-paper-response/update-status/555555555/CLOSED");

httpHeaders.set(HttpHeaders.AUTHORIZATION, bureauJwt);
Expand All @@ -1798,7 +1784,7 @@ public void updateJurorPaperResponseStatus_courtUser_thirdParty() throws Excepti
"555555555");

validateMergedJurorRecord(jurorPool, summonsReplyData, IJurorStatus.RESPONDED);
assertThat(jurorPool.getUserEdtq()).isEqualToIgnoringCase("COURT_USER");
assertThat(jurorPool.getUserEdtq()).isEqualToIgnoringCase("test_court_standard");

// juror contact details are omitted from merge when completed by a third party
assertThat(juror.getPhoneNumber()).isNull();
Expand Down Expand Up @@ -2085,16 +2071,6 @@ private void setThirdPartyDetails(JurorPaperResponseDto jurorPaperResponseDto) {
jurorPaperResponseDto.setThirdParty(thirdParty);
}

private String initCourtsJwt(String owner, List<String> courts) throws Exception {

return mintBureauJwt(BureauJwtPayload.builder()
.userLevel("99")
.login("COURT_USER")
.owner(owner)
.staff(BureauJwtPayload.Staff.builder().courts(courts).build())
.build());
}

private void validateMergedJurorRecord(JurorPool jurorPool, PaperResponse summonsReplyData,
long statusCode) {
Juror juror = jurorPool.getJuror();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,6 @@
import uk.gov.hmcts.juror.api.moj.repository.jurorresponse.JurorDigitalResponseRepositoryMod;
import uk.gov.hmcts.juror.api.moj.repository.jurorresponse.JurorPaperResponseRepositoryMod;
import uk.gov.hmcts.juror.api.moj.repository.jurorresponse.JurorReasonableAdjustmentRepository;
import uk.gov.hmcts.juror.api.moj.utils.JurorPoolUtils;
import uk.gov.hmcts.juror.api.moj.utils.RepositoryUtils;

import java.math.BigDecimal;
Expand Down Expand Up @@ -386,7 +385,8 @@ void approvePendingJurorRecordHappyPath() throws Exception {

//Check the Juror record has been created as well
RepositoryUtils.retrieveFromDatabase("041600001", jurorRepository);
JurorPool jurorPool = JurorPoolUtils.getSingleActiveJurorPool(jurorPoolRepository, "041600001");
JurorPool jurorPool = jurorPoolRepository.findByPoolCourtLocationLocCodeAndJurorJurorNumberAndIsActiveTrue(
"416", "041600001");
assertThat(jurorPool.getPool().getPoolNumber()).as("Expect the Juror to be created and in pool 416220503")
.isEqualTo("416220503");
assertThat(jurorPool.getStatus().getStatus()).as("Expect the Juror to be active")
Expand Down Expand Up @@ -470,8 +470,7 @@ void editJurorDetailsHappyPathAllFieldsRequired() {
.as("Expect the HTTP status to be NO CONTENT")
.isEqualTo(HttpStatus.NO_CONTENT);
executeInTransaction(() -> {
JurorPool jurorPool = JurorPoolUtils.getSingleActiveJurorPool(jurorPoolRepository, jurorNumber);
Juror juror = jurorPool.getJuror();
Juror juror = jurorRepository.findByJurorNumber(jurorNumber);

//Check data has been changed and now matches what was in the dto.
assertThat(juror.getTitle()).isEqualTo(requestDto.getTitle());
Expand Down Expand Up @@ -511,8 +510,7 @@ void editJurorDetailsHappyPathMandatoryFieldsOnly() {
.as("Expect the HTTP status to be NO CONTENT")
.isEqualTo(HttpStatus.NO_CONTENT);
executeInTransaction(() -> {
JurorPool jurorPool = JurorPoolUtils.getSingleActiveJurorPool(jurorPoolRepository, jurorNumber);
Juror juror = jurorPool.getJuror();
Juror juror = jurorRepository.findByJurorNumber(jurorNumber);

//Check data has been changed and now matches what was in the dto.
assertThat(juror.getTitle()).isEqualTo(requestDto.getTitle());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@
import uk.gov.hmcts.juror.api.moj.domain.JurorPool;
import uk.gov.hmcts.juror.api.moj.repository.JurorHistoryRepository;
import uk.gov.hmcts.juror.api.moj.repository.JurorPoolRepository;
import uk.gov.hmcts.juror.api.moj.utils.JurorPoolUtils;

import java.net.URI;
import java.time.LocalDate;
Expand Down Expand Up @@ -95,8 +94,8 @@ public void markJurorAsUndeliverable_multiple_BureauUser() {

private void assertUpdated(String jurorNumber) {
executeInTransaction(() -> {
JurorPool jurorPool =
JurorPoolUtils.getSingleActiveJurorPool(jurorPoolRepository, jurorNumber);
JurorPool jurorPool = jurorPoolRepository
.findByPoolCourtLocationLocCodeAndJurorJurorNumberAndIsActiveTrue("415", jurorNumber);
Juror juror = jurorPool.getJuror();

assertThat(juror.isResponded()).isFalse();
Expand Down Expand Up @@ -132,8 +131,8 @@ public void markJurorAsUndeliverable_CourtUser_bureauOwnedRecord() throws Except
restTemplate.exchange(createRequest("123456789"), String.class);

assertThat(response.getStatusCode())
.as("Expect the HTTP PUT request to be FORBIDDEN")
.isEqualTo(HttpStatus.FORBIDDEN);
.as("Expect the HTTP PUT request to be NOT_FOUND")
.isEqualTo(HttpStatus.NOT_FOUND);
}

@Test
Expand Down
Loading

0 comments on commit a2db75d

Please sign in to comment.