Skip to content

Commit

Permalink
Use Lazy loading instead of Eager to reduce database calls (#740)
Browse files Browse the repository at this point in the history
* Updated Many classes to use Lazy Loading

* Removed comment

* Applied review comments
  • Loading branch information
Ben-Edwards-cgi committed Aug 23, 2024
1 parent 60896ee commit f7eca9a
Show file tree
Hide file tree
Showing 54 changed files with 2,383 additions and 2,112 deletions.
4 changes: 2 additions & 2 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -284,10 +284,10 @@ checkstyle {
}

pmdTest {
maxFailures = 294
maxFailures = 292
}
pmdMain {
maxFailures = 776
maxFailures = 771
}
pmd {
maxFailures = 0
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.security.authorization.AuthorizationDeniedException;
import org.springframework.test.context.ActiveProfiles;
import org.springframework.transaction.PlatformTransactionManager;
import org.springframework.transaction.support.TransactionTemplate;
import uk.gov.hmcts.juror.api.config.bureau.BureauJwtPayload;
import uk.gov.hmcts.juror.api.moj.domain.Role;
import uk.gov.hmcts.juror.api.moj.domain.UserType;
Expand All @@ -31,6 +33,7 @@
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.function.Supplier;

import static org.assertj.core.api.Assertions.assertThat;

Expand All @@ -49,13 +52,31 @@ public abstract class AbstractIntegrationTest extends ContainerTest {
@SuppressWarnings("SpringJavaAutowiredMembersInspection")
@Autowired
protected JdbcTemplate jdbcTemplate;
@Autowired
private PlatformTransactionManager transactionManager;
private TransactionTemplate transactionTemplate;

@Value("${jwt.secret.bureau}")
protected String bureauSecret;

@Value("${jwt.secret.hmac}")
protected String hmacSecret;


protected void executeInTransaction(Runnable supplier) {
executeInTransaction(() -> {
supplier.run();
return null;
});
}

protected <R> R executeInTransaction(Supplier<R> supplier) {
if (transactionTemplate == null) {
transactionTemplate = new TransactionTemplate(transactionManager);
}
return transactionTemplate.execute(status -> supplier.get());
}

protected HttpHeaders initialiseHeaders(String loginUser, UserType userType, Set<Role> roles, String owner) {
BureauJwtPayload payload = createBureauJwtPayload(loginUser, userType, roles, owner);
return buildHttpHeaders(payload);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,15 +40,19 @@ public void setUp() throws Exception {
@Test
@Sql("/db/truncate.sql")
@Sql("/db/BureauJurorSpecialNeedsRepository_findByJurorNumber.sql")
//FALSE positive
@SuppressWarnings("PMD.JUnitTestsShouldIncludeAssert")
public void findByJurorNumberWithValidJurorNumberReturnsJurorSpecialNeeds() {
List<JurorReasonableAdjustment> actualReasonableAdjustment = jurorReasonableAdjustmentRepository
.findByJurorNumber("209092530");
assertThat(actualReasonableAdjustment).hasSize(1);
assertThat(actualReasonableAdjustment).extracting("jurorNumber", "reasonableAdjustment.code",
"reasonableAdjustment.description", "reasonableAdjustmentDetail")
.contains(tuple(reasonableAdjustment.getJurorNumber(),
reasonableAdjustment.getReasonableAdjustment().getCode(),
reasonableAdjustment.getReasonableAdjustment().getDescription(),
reasonableAdjustment.getReasonableAdjustmentDetail()));
executeInTransaction(() -> {
List<JurorReasonableAdjustment> actualReasonableAdjustment = jurorReasonableAdjustmentRepository
.findByJurorNumber("209092530");
assertThat(actualReasonableAdjustment).hasSize(1);
assertThat(actualReasonableAdjustment).extracting("jurorNumber", "reasonableAdjustment.code",
"reasonableAdjustment.description", "reasonableAdjustmentDetail")
.contains(tuple(reasonableAdjustment.getJurorNumber(),
reasonableAdjustment.getReasonableAdjustment().getCode(),
reasonableAdjustment.getReasonableAdjustment().getDescription(),
reasonableAdjustment.getReasonableAdjustmentDetail()));
});
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -70,30 +70,32 @@ public void setUp() throws Exception {

private void validateJurorWasCompleted(LocalDate completionTime, String jurorNumber, String poolNumber,
boolean isDismissal) {
JurorPool jurorPool = jurorPoolRepository.findByJurorJurorNumberAndPoolPoolNumber(jurorNumber, poolNumber);
assertEquals(true, jurorPool.getIsActive(),
"Juror pool should be active");
assertEquals(false, jurorPool.getOnCall(),
"Juror pool should not be on call");
assertEquals(IJurorStatus.COMPLETED, jurorPool.getStatus().getStatus(),
"Juror pool status should be completed");
Juror juror = jurorPool.getJuror();

assertEquals(completionTime, juror.getCompletionDate(),
"Juror completion date should match");

if (isDismissal) {
assertThat(jurorPool.getNextDate()).isNull();
assertThat(jurorPool.getOnCall()).isFalse();
}
List<JurorHistory> jurorHistories = jurorHistoryRepository.findByJurorNumberOrderById(jurorNumber);
assertEquals(1, jurorHistories.size(), "Should only be one history entry");
JurorHistory jurorHistory = jurorHistories.get(0);
assertEquals(poolNumber, jurorHistory.getPoolNumber(), "Pool number should match");
assertEquals(jurorNumber, jurorHistory.getJurorNumber(), "Juror number should match");
assertEquals("COURT_USER", jurorHistory.getCreatedBy(), "User id should match");
assertEquals(HistoryCodeMod.COMPLETE_SERVICE, jurorHistory.getHistoryCode(), "History code should match");
assertEquals(completionTime, jurorHistory.getOtherInformationDate(), "Date should match");
executeInTransaction(() -> {
JurorPool jurorPool = jurorPoolRepository.findByJurorJurorNumberAndPoolPoolNumber(jurorNumber, poolNumber);
assertEquals(true, jurorPool.getIsActive(),
"Juror pool should be active");
assertEquals(false, jurorPool.getOnCall(),
"Juror pool should not be on call");
assertEquals(IJurorStatus.COMPLETED, jurorPool.getStatus().getStatus(),
"Juror pool status should be completed");
Juror juror = jurorPool.getJuror();

assertEquals(completionTime, juror.getCompletionDate(),
"Juror completion date should match");

if (isDismissal) {
assertThat(jurorPool.getNextDate()).isNull();
assertThat(jurorPool.getOnCall()).isFalse();
}
List<JurorHistory> jurorHistories = jurorHistoryRepository.findByJurorNumberOrderById(jurorNumber);
assertEquals(1, jurorHistories.size(), "Should only be one history entry");
JurorHistory jurorHistory = jurorHistories.get(0);
assertEquals(poolNumber, jurorHistory.getPoolNumber(), "Pool number should match");
assertEquals(jurorNumber, jurorHistory.getJurorNumber(), "Juror number should match");
assertEquals("COURT_USER", jurorHistory.getCreatedBy(), "User id should match");
assertEquals(HistoryCodeMod.COMPLETE_SERVICE, jurorHistory.getHistoryCode(), "History code should match");
assertEquals(completionTime, jurorHistory.getOtherInformationDate(), "Date should match");
});
}

@Nested
Expand Down Expand Up @@ -176,13 +178,15 @@ void negativeOneJurorNotFound() throws Exception {
+ "\"message\":\"Juror number 941500004 not found in pool 415220901\","
+ "\"path\":\"/api/v1/moj/complete-service/415220901/complete\"}",
response.getBody(), false);

JurorPool jurorPool = jurorPoolRepository.findByJurorJurorNumberAndPoolPoolNumber("641500005", "415220901");
assertEquals(IJurorStatus.RESPONDED, jurorPool.getStatus().getStatus(),
"Juror pool status should not be updated");
Juror juror = jurorPool.getJuror();
assertNull(juror.getCompletionDate(),
"Completion date should not be update to provided completion date");
executeInTransaction(() -> {
JurorPool jurorPool =
jurorPoolRepository.findByJurorJurorNumberAndPoolPoolNumber("641500005", "415220901");
assertEquals(IJurorStatus.RESPONDED, jurorPool.getStatus().getStatus(),
"Juror pool status should not be updated");
Juror juror = jurorPool.getJuror();
assertNull(juror.getCompletionDate(),
"Completion date should not be update to provided completion date");
});
}

@Test
Expand Down Expand Up @@ -210,24 +214,25 @@ void negativeOneJurorNotResponded() throws Exception {
+ "\"message\":\"Unable to complete the service for the following juror number(s) due to invalid "
+ "state: 641500003\","
+ "\"code\":\"COMPLETE_SERVICE_JUROR_IN_INVALID_STATE\"}", response.getBody(), false);

JurorPool jurorPool1 = jurorPoolRepository.findByJurorJurorNumberAndPoolPoolNumber("641500005",
"415220901");
assertEquals(IJurorStatus.COMPLETED, jurorPool1.getStatus().getStatus(),
"Juror pool status should not change as transaction should rollback");
Juror juror1 = jurorPool1.getJuror();
assertNotNull(juror1.getCompletionDate(),
"Completion date should not be null.");
assertEquals(completionTime, juror1.getCompletionDate(),
"Completion date should be equal to 2023-11-23");

JurorPool jurorPool2 = jurorPoolRepository.findByJurorJurorNumberAndPoolPoolNumber("641500003",
"415220901");
assertEquals(IJurorStatus.FAILED_TO_ATTEND, jurorPool2.getStatus().getStatus(),
"Juror pool status should not change as juror is in invalid state for completion");
Juror juror2 = jurorPool2.getJuror();
assertNull(juror2.getCompletionDate(),
"Completion date should not change as transaction should rollback");
executeInTransaction(() -> {
JurorPool jurorPool1 = jurorPoolRepository.findByJurorJurorNumberAndPoolPoolNumber("641500005",
"415220901");
assertEquals(IJurorStatus.COMPLETED, jurorPool1.getStatus().getStatus(),
"Juror pool status should not change as transaction should rollback");
Juror juror1 = jurorPool1.getJuror();
assertNotNull(juror1.getCompletionDate(),
"Completion date should not be null.");
assertEquals(completionTime, juror1.getCompletionDate(),
"Completion date should be equal to 2023-11-23");

JurorPool jurorPool2 = jurorPoolRepository.findByJurorJurorNumberAndPoolPoolNumber("641500003",
"415220901");
assertEquals(IJurorStatus.FAILED_TO_ATTEND, jurorPool2.getStatus().getStatus(),
"Juror pool status should not change as juror is in invalid state for completion");
Juror juror2 = jurorPool2.getJuror();
assertNull(juror2.getCompletionDate(),
"Completion date should not change as transaction should rollback");
});
}

@Test
Expand Down Expand Up @@ -521,14 +526,16 @@ void negativeOneJurorNotFound() throws Exception {
+ "\"message\":\"Juror number 941700009 not found in database\","
+ "\"path\":\"/api/v1/moj/complete-service/dismissal\"}",
response.getBody(), false);

JurorPool jurorPool = jurorPoolRepository.findByJurorJurorNumberAndPoolPoolNumber("641700003", "417230101");
assertEquals(IJurorStatus.RESPONDED, jurorPool.getStatus().getStatus(),
"Juror pool status should not change as transaction should rollback");

Juror juror = jurorPool.getJuror();
assertNull(juror.getCompletionDate(),
"Completion date should not change as transaction should rollback");
executeInTransaction(() -> {
JurorPool jurorPool =
jurorPoolRepository.findByJurorJurorNumberAndPoolPoolNumber("641700003", "417230101");
assertEquals(IJurorStatus.RESPONDED, jurorPool.getStatus().getStatus(),
"Juror pool status should not change as transaction should rollback");

Juror juror = jurorPool.getJuror();
assertNull(juror.getCompletionDate(),
"Completion date should not change as transaction should rollback");
});
}

@Test
Expand Down Expand Up @@ -601,22 +608,26 @@ class UncompleteService {
public static final String URL = BASE_URL + "/uncomplete";

private void validateJurorWasUncompleted(String jurorNumber, String poolNumber) {
JurorPool jurorPool = jurorPoolRepository.findByJurorJurorNumberAndPoolPoolNumber(jurorNumber, poolNumber);
assertEquals(IJurorStatus.RESPONDED, jurorPool.getStatus().getStatus(),
"Juror pool status should be responded");
Juror juror = jurorPool.getJuror();

assertNull(juror.getCompletionDate(),
"Juror completion date should be null");

List<JurorHistory> jurorHistories = jurorHistoryRepository.findByJurorNumberOrderById(jurorNumber);
assertEquals(1, jurorHistories.size(), "Should only be one history entry");
JurorHistory jurorHistory = jurorHistories.get(0);
assertEquals(poolNumber, jurorHistory.getPoolNumber(), "Pool number should match");
assertEquals(jurorNumber, jurorHistory.getJurorNumber(), "Juror number should match");
assertEquals("COURT_USER", jurorHistory.getCreatedBy(), "User id should match");
assertEquals(HistoryCodeMod.COMPLETE_SERVICE, jurorHistory.getHistoryCode(), "History code should match");
assertEquals("Completion date removed", jurorHistory.getOtherInformation(), "Info should match");
executeInTransaction(() -> {
JurorPool jurorPool =
jurorPoolRepository.findByJurorJurorNumberAndPoolPoolNumber(jurorNumber, poolNumber);
assertEquals(IJurorStatus.RESPONDED, jurorPool.getStatus().getStatus(),
"Juror pool status should be responded");
Juror juror = jurorPool.getJuror();

assertNull(juror.getCompletionDate(),
"Juror completion date should be null");

List<JurorHistory> jurorHistories = jurorHistoryRepository.findByJurorNumberOrderById(jurorNumber);
assertEquals(1, jurorHistories.size(), "Should only be one history entry");
JurorHistory jurorHistory = jurorHistories.get(0);
assertEquals(poolNumber, jurorHistory.getPoolNumber(), "Pool number should match");
assertEquals(jurorNumber, jurorHistory.getJurorNumber(), "Juror number should match");
assertEquals("COURT_USER", jurorHistory.getCreatedBy(), "User id should match");
assertEquals(HistoryCodeMod.COMPLETE_SERVICE, jurorHistory.getHistoryCode(),
"History code should match");
assertEquals("Completion date removed", jurorHistory.getOtherInformation(), "Info should match");
});
}

@Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -272,7 +272,7 @@ public void createPool_withDisqualifiedOnSelection() throws Exception {
RequestEntity<PoolMemberFilterRequestQuery> requestEntity2 = new RequestEntity<>(
body, httpHeaders, HttpMethod.POST, uri2);
ResponseEntity<PaginatedList<FilterPoolMember>> response2 = template.exchange(requestEntity2,
new ParameterizedTypeReference<PaginatedList<FilterPoolMember>>() {
new ParameterizedTypeReference<>() {
});
assertThat(response2.getStatusCode()).isEqualTo(HttpStatus.OK);

Expand Down Expand Up @@ -532,7 +532,7 @@ private PoolCreateRequestDto setUpPoolCreateRequestDto() {
PoolCreateRequestDto poolCreateRequestDto = new PoolCreateRequestDto();
poolCreateRequestDto.setPoolNumber("415221201");
poolCreateRequestDto.setStartDate(LocalDate.of(2022, 12, 4));
poolCreateRequestDto.setAttendTime(LocalDateTime.of(2022, 12, 04, 9, 0, 0));
poolCreateRequestDto.setAttendTime(LocalDateTime.of(2022, 12, 4, 9, 0, 0));
poolCreateRequestDto.setNoRequested(5);
poolCreateRequestDto.setBureauDeferrals(0);
poolCreateRequestDto.setNumberRequired(4);
Expand Down
Loading

0 comments on commit f7eca9a

Please sign in to comment.