Skip to content

Commit

Permalink
Reduce duplication in tests for creating Sentences
Browse files Browse the repository at this point in the history
Introduce helper function with defaults that allows tests to only
specify interesting fields to be asserted on.

Disable test that used to convert nDelius sentence length to terms, we
will take a different approach in a following PR.
  • Loading branch information
emileswarts committed Aug 30, 2023
1 parent c1ce935 commit 73977e4
Show file tree
Hide file tree
Showing 6 changed files with 111 additions and 264 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import org.springframework.http.HttpStatus
import org.springframework.test.web.servlet.MockMvc
import org.springframework.test.web.servlet.request.MockMvcRequestBuilders
import uk.gov.justice.digital.hmpps.hmppsintegrationapi.extensions.removeWhitespaceAndNewlines
import uk.gov.justice.digital.hmpps.hmppsintegrationapi.helpers.generateTestSentence
import uk.gov.justice.digital.hmpps.hmppsintegrationapi.models.Response
import uk.gov.justice.digital.hmpps.hmppsintegrationapi.models.UpstreamApi
import uk.gov.justice.digital.hmpps.hmppsintegrationapi.models.UpstreamApiError
Expand All @@ -23,7 +24,6 @@ import java.net.URLEncoder
import java.nio.charset.StandardCharsets
import java.time.LocalDate
import uk.gov.justice.digital.hmpps.hmppsintegrationapi.models.Sentence as IntegrationApiSentence
import uk.gov.justice.digital.hmpps.hmppsintegrationapi.models.Term as IntegrationApiTerm

@WebMvcTest(controllers = [SentencesController::class])
internal class SentencesControllerTest(
Expand All @@ -41,52 +41,8 @@ internal class SentencesControllerTest(
whenever(getSentencesForPersonService.execute(pncId)).thenReturn(
Response(
data = listOf(
IntegrationApiSentence(
dateOfSentencing = LocalDate.parse("1990-01-01"),
description = "CJA - Community Order",
isActive = true,
isCustodial = true,
terms = listOf(
IntegrationApiTerm(
hours = 2,
),
IntegrationApiTerm(
years = 25,
),
),
),
IntegrationApiSentence(
dateOfSentencing = LocalDate.parse("1991-01-01"),
description = "ORA CJA03 Standard Determinate Sentence",
isActive = false,
isCustodial = true,
terms = listOf(
IntegrationApiTerm(
years = 15,
months = 6,
weeks = 2,
),
IntegrationApiTerm(
months = 6,
weeks = 2,
days = 5,
),
),
),
IntegrationApiSentence(
dateOfSentencing = LocalDate.parse("1992-01-01"),
description = "CJA - Suspended Sentence Order",
isActive = null,
isCustodial = true,
terms = listOf(
IntegrationApiTerm(
years = 15,
),
IntegrationApiTerm(
weeks = 2,
),
),
),
generateTestSentence(description = "Some description 1"),
generateTestSentence(description = "Some description 2"),
),
),
)
Expand All @@ -109,74 +65,62 @@ internal class SentencesControllerTest(

result.response.contentAsString.shouldContain(
"""
[
{
"dateOfSentencing": "1990-01-01",
"description": "CJA - Community Order",
"isActive": true,
"isCustodial": true,
"terms": [
"data": [
{
"dateOfSentencing": null,
"description": "Some description 1",
"isActive": true,
"isCustodial": true,
"terms": [
{
"years": null,
"months": null,
"weeks": null,
"days": null,
"hours": 2
"years": null,
"months": null,
"weeks": null,
"days": null,
"hours": 2
},
{
"years": 25,
"months": null,
"weeks": null,
"days": null,
"hours": null
"years": 25,
"months": null,
"weeks": null,
"days": null,
"hours": null
}
]
},
{
"dateOfSentencing": "1991-01-01",
"description": "ORA CJA03 Standard Determinate Sentence",
"isActive": false,
"isCustodial": true,
"terms": [
{
"years": 15,
"months": 6,
"weeks": 2,
"days": null,
"hours": null
},
{
"years": null,
"months": 6,
"weeks": 2,
"days": 5,
"hours": null
}
]
},
{
"dateOfSentencing": "1992-01-01",
"description": "CJA - Suspended Sentence Order",
"isActive": null,
"isCustodial": true,
"terms": [
]
},
{
"dateOfSentencing": null,
"description": "Some description 2",
"isActive": true,
"isCustodial": true,
"terms": [
{
"years": 15,
"months": null,
"weeks": null,
"days": null,
"hours": null
"years": null,
"months": null,
"weeks": null,
"days": null,
"hours": 2
},
{
"years": null,
"months": null,
"weeks": 2,
"days": null,
"hours": null
"years": 25,
"months": null,
"weeks": null,
"days": null,
"hours": null
}
]
]
}
],
"pagination": {
"isLastPage": true,
"count": 2,
"page": 1,
"perPage": 10,
"totalCount": 2,
"totalPages": 1
}
}
]
""".removeWhitespaceAndNewlines(),
)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import org.springframework.test.context.ActiveProfiles
import org.springframework.test.context.ContextConfiguration
import uk.gov.justice.digital.hmpps.hmppsintegrationapi.gateways.HmppsAuthGateway
import uk.gov.justice.digital.hmpps.hmppsintegrationapi.gateways.NDeliusGateway
import uk.gov.justice.digital.hmpps.hmppsintegrationapi.helpers.generateTestSentence
import uk.gov.justice.digital.hmpps.hmppsintegrationapi.mockservers.HmppsAuthMockServer
import uk.gov.justice.digital.hmpps.hmppsintegrationapi.mockservers.NDeliusApiMockServer
import uk.gov.justice.digital.hmpps.hmppsintegrationapi.models.UpstreamApi
Expand Down Expand Up @@ -64,14 +65,14 @@ class GetSentencesForPersonTest(

response.data.shouldBe(
listOf(
IntegrationApiSentence(
generateTestSentence(
dateOfSentencing = LocalDate.parse("2009-07-07"),
description = "CJA - Community Order",
isActive = false,
isCustodial = false,
terms = listOf(IntegrationApiTerm(months = 12)),
),
IntegrationApiSentence(
generateTestSentence(
dateOfSentencing = LocalDate.parse("2009-09-01"),
description = "CJA - Suspended Sentence Order",
isActive = true,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import org.springframework.test.context.ContextConfiguration
import uk.gov.justice.digital.hmpps.hmppsintegrationapi.extensions.removeWhitespaceAndNewlines
import uk.gov.justice.digital.hmpps.hmppsintegrationapi.gateways.HmppsAuthGateway
import uk.gov.justice.digital.hmpps.hmppsintegrationapi.gateways.NomisGateway
import uk.gov.justice.digital.hmpps.hmppsintegrationapi.helpers.generateTestSentence
import uk.gov.justice.digital.hmpps.hmppsintegrationapi.mockservers.HmppsAuthMockServer
import uk.gov.justice.digital.hmpps.hmppsintegrationapi.mockservers.NomisApiMockServer
import uk.gov.justice.digital.hmpps.hmppsintegrationapi.models.UpstreamApi
Expand Down Expand Up @@ -93,7 +94,7 @@ class GetSentencesForPersonTest(

response.data.shouldBe(
listOf(
IntegrationApiSentence(
generateTestSentence(
dateOfSentencing = LocalDate.parse("2001-01-01"),
description = "ORA CJA03 Standard Determinate Sentence",
isActive = true,
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package uk.gov.justice.digital.hmpps.hmppsintegrationapi.helpers

import uk.gov.justice.digital.hmpps.hmppsintegrationapi.models.Sentence
import uk.gov.justice.digital.hmpps.hmppsintegrationapi.models.Term as IntegrationApiTerm
import java.time.LocalDate

fun generateTestSentence(
dateOfSentencing: LocalDate? = null,
description: String? = "Some description",
isActive: Boolean? = true,
isCustodial: Boolean = true,
terms: List<IntegrationApiTerm> = listOf(
IntegrationApiTerm(hours = 2),
IntegrationApiTerm(years = 25),
),
): Sentence = Sentence(
dateOfSentencing = dateOfSentencing,
description = description,
isActive = isActive,
isCustodial = isCustodial,
terms = terms,
)
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package uk.gov.justice.digital.hmpps.hmppsintegrationapi.models.ndelius
import io.kotest.core.spec.style.DescribeSpec
import io.kotest.matchers.nulls.shouldBeNull
import io.kotest.matchers.shouldBe
import uk.gov.justice.digital.hmpps.hmppsintegrationapi.helpers.generateTestSentence
import uk.gov.justice.digital.hmpps.hmppsintegrationapi.models.Offence
import uk.gov.justice.digital.hmpps.hmppsintegrationapi.models.Term
import java.time.LocalDate
Expand Down Expand Up @@ -117,54 +118,24 @@ class SupervisionsTest : DescribeSpec(

integrationApiSentences.shouldBe(
listOf(
IntegrationApiSentence(dateOfSentencing = LocalDate.parse("2009-07-07"), isActive = true, isCustodial = true, description = "CJA - Community Order"),
IntegrationApiSentence(dateOfSentencing = LocalDate.parse("2010-07-07"), isActive = false, isCustodial = true, description = "CJA - Suspended Sentence Order"),
generateTestSentence(dateOfSentencing = LocalDate.parse("2009-07-07"), description = "CJA - Community Order", terms = listOf(Term())),
generateTestSentence(dateOfSentencing = LocalDate.parse("2010-07-07"), isActive = false, description = "CJA - Suspended Sentence Order", terms = listOf(Term())),
),
)
}

it("maps nDelius sentence length to Integration API terms") {
xit("maps nDelius sentence length to Integration API terms") {
val supervisions = Supervisions(
listOf(
Supervision(custodial = true, sentence = NDeliusSentence(date = "2009-07-07", length = 11, lengthUnits = "Months")),
Supervision(custodial = false, sentence = NDeliusSentence(date = "2010-07-07", length = 2, lengthUnits = "Years")),
Supervision(active = true, custodial = true, sentence = NDeliusSentence(date = "2009-07-07", description = "CJA - Community Order")),
Supervision(active = false, custodial = true, sentence = NDeliusSentence(date = "2010-07-07", description = "CJA - Suspended Sentence Order")),
),
)

val integrationApiSentences = supervisions.supervisions.map { it.toSentence() }

integrationApiSentences.shouldBe(
listOf(
IntegrationApiSentence(
dateOfSentencing = LocalDate.parse("2009-07-07"),
isActive = null,
isCustodial = true,
terms = listOf(
Term(
years = null,
months = 11,
weeks = null,
days = null,
hours = null,
),
),
),
IntegrationApiSentence(
dateOfSentencing = LocalDate.parse("2010-07-07"),
isActive = null,
isCustodial = false,
terms = listOf(
Term(
years = 2,
months = null,
weeks = null,
days = null,
hours = null,
),
),
),
),
)
integrationApiSentences[0].terms.shouldBe(listOf(Term()))
integrationApiSentences[1].terms.shouldBe(listOf(Term()))
}

it("deals with NULL values") {
Expand Down
Loading

0 comments on commit 73977e4

Please sign in to comment.