Skip to content

Commit

Permalink
CDPS-1085: Handle 0 health results (#12)
Browse files Browse the repository at this point in the history
  • Loading branch information
danielburnley authored Jan 29, 2025
1 parent 76c1333 commit 7a32127
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,9 @@ class PrisonerHealthService(

// Fetch all non-empty health data for the given prisoner numbers
val healthData =
prisonerHealthRepository.findAllByPrisonerNumberInAndFoodAllergiesIsNotEmptyOrMedicalDietaryRequirementsIsNotEmpty(prisonerNumbers)
prisonerHealthRepository.findAllByPrisonerNumberInAndFoodAllergiesIsNotEmptyOrMedicalDietaryRequirementsIsNotEmpty(
prisonerNumbers,
)

// This maintains the order from the prisoner search API so that we're able to have sorting
val overlappingIds = prisonerNumbers.intersect(healthData.map { it.prisonerNumber }.toSet()).toList()
Expand Down Expand Up @@ -89,10 +91,14 @@ class PrisonerHealthService(
last = (lastIndex + 1) >= overlappingIds.size,
numberOfElements = idsForPage.size,
offset = startIndex,
pageNumber = Math.ceilDiv(startIndex, idsForPage.size) + 1,
pageNumber = if (idsForPage.isNotEmpty()) {
Math.ceilDiv(startIndex, idsForPage.size) + 1
} else {
1
},
size = request.size,
totalElements = overlappingIds.size,
totalPages = Math.ceilDiv(overlappingIds.size, request.size),
totalPages = Math.ceilDiv(overlappingIds.size, request.size).coerceAtLeast(1),
),
)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -434,6 +434,35 @@ class PrisonerHealthServiceTest {
)
}

@Test
fun `it doesnt crash with 0 prisoners`() {
whenever(
prisonerHealthRepository.findAllByPrisonerNumberInAndFoodAllergiesIsNotEmptyOrMedicalDietaryRequirementsIsNotEmpty(
mutableListOf(
PRISONER_NUMBER,
),
),
).thenReturn(emptyList())

assertThat(
underTest.getHealthForPrison(PRISON_ID, HealthAndMedicationForPrisonRequest(1, 10)),
).isEqualTo(
HealthAndMedicationForPrisonResponse(
content = emptyList(),
metadata = PageMeta(
first = true,
last = true,
numberOfElements = 0,
offset = 0,
pageNumber = 1,
size = 10,
totalElements = 0,
totalPages = 1,
),
),
)
}

@Test
fun `it fetches the health information for the returned prisoners`() {
assertThat(
Expand Down

0 comments on commit 7a32127

Please sign in to comment.