From 73977e43a78cd5290557b8a1bdc75a891eb57ebd Mon Sep 17 00:00:00 2001 From: Emile Date: Wed, 30 Aug 2023 12:12:40 +0100 Subject: [PATCH] Reduce duplication in tests for creating Sentences 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. --- .../controllers/v1/SentencesControllerTest.kt | 154 ++++++------------ .../ndelius/GetSentencesForPersonTest.kt | 5 +- .../nomis/GetSentencesForPersonTest.kt | 3 +- .../helpers/SentenceHelper.kt | 22 +++ .../models/ndelius/SupervisionsTest.kt | 45 +---- .../GetSentencesForPersonServiceTest.kt | 146 +++-------------- 6 files changed, 111 insertions(+), 264 deletions(-) create mode 100644 src/test/kotlin/uk/gov/justice/digital/hmpps/hmppsintegrationapi/helpers/SentenceHelper.kt diff --git a/src/test/kotlin/uk/gov/justice/digital/hmpps/hmppsintegrationapi/controllers/v1/SentencesControllerTest.kt b/src/test/kotlin/uk/gov/justice/digital/hmpps/hmppsintegrationapi/controllers/v1/SentencesControllerTest.kt index 9f3bf26a..a5061887 100644 --- a/src/test/kotlin/uk/gov/justice/digital/hmpps/hmppsintegrationapi/controllers/v1/SentencesControllerTest.kt +++ b/src/test/kotlin/uk/gov/justice/digital/hmpps/hmppsintegrationapi/controllers/v1/SentencesControllerTest.kt @@ -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 @@ -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( @@ -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"), ), ), ) @@ -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(), ) } diff --git a/src/test/kotlin/uk/gov/justice/digital/hmpps/hmppsintegrationapi/gateways/ndelius/GetSentencesForPersonTest.kt b/src/test/kotlin/uk/gov/justice/digital/hmpps/hmppsintegrationapi/gateways/ndelius/GetSentencesForPersonTest.kt index 49c1d891..dd0e9446 100644 --- a/src/test/kotlin/uk/gov/justice/digital/hmpps/hmppsintegrationapi/gateways/ndelius/GetSentencesForPersonTest.kt +++ b/src/test/kotlin/uk/gov/justice/digital/hmpps/hmppsintegrationapi/gateways/ndelius/GetSentencesForPersonTest.kt @@ -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 @@ -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, diff --git a/src/test/kotlin/uk/gov/justice/digital/hmpps/hmppsintegrationapi/gateways/nomis/GetSentencesForPersonTest.kt b/src/test/kotlin/uk/gov/justice/digital/hmpps/hmppsintegrationapi/gateways/nomis/GetSentencesForPersonTest.kt index 354fcb8e..3daf0100 100644 --- a/src/test/kotlin/uk/gov/justice/digital/hmpps/hmppsintegrationapi/gateways/nomis/GetSentencesForPersonTest.kt +++ b/src/test/kotlin/uk/gov/justice/digital/hmpps/hmppsintegrationapi/gateways/nomis/GetSentencesForPersonTest.kt @@ -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 @@ -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, diff --git a/src/test/kotlin/uk/gov/justice/digital/hmpps/hmppsintegrationapi/helpers/SentenceHelper.kt b/src/test/kotlin/uk/gov/justice/digital/hmpps/hmppsintegrationapi/helpers/SentenceHelper.kt new file mode 100644 index 00000000..21c8ba18 --- /dev/null +++ b/src/test/kotlin/uk/gov/justice/digital/hmpps/hmppsintegrationapi/helpers/SentenceHelper.kt @@ -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 = listOf( + IntegrationApiTerm(hours = 2), + IntegrationApiTerm(years = 25), + ), +): Sentence = Sentence( + dateOfSentencing = dateOfSentencing, + description = description, + isActive = isActive, + isCustodial = isCustodial, + terms = terms, +) diff --git a/src/test/kotlin/uk/gov/justice/digital/hmpps/hmppsintegrationapi/models/ndelius/SupervisionsTest.kt b/src/test/kotlin/uk/gov/justice/digital/hmpps/hmppsintegrationapi/models/ndelius/SupervisionsTest.kt index c8485937..d7631248 100644 --- a/src/test/kotlin/uk/gov/justice/digital/hmpps/hmppsintegrationapi/models/ndelius/SupervisionsTest.kt +++ b/src/test/kotlin/uk/gov/justice/digital/hmpps/hmppsintegrationapi/models/ndelius/SupervisionsTest.kt @@ -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 @@ -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") { diff --git a/src/test/kotlin/uk/gov/justice/digital/hmpps/hmppsintegrationapi/services/GetSentencesForPersonServiceTest.kt b/src/test/kotlin/uk/gov/justice/digital/hmpps/hmppsintegrationapi/services/GetSentencesForPersonServiceTest.kt index c5606bc0..8e6580d0 100644 --- a/src/test/kotlin/uk/gov/justice/digital/hmpps/hmppsintegrationapi/services/GetSentencesForPersonServiceTest.kt +++ b/src/test/kotlin/uk/gov/justice/digital/hmpps/hmppsintegrationapi/services/GetSentencesForPersonServiceTest.kt @@ -14,6 +14,7 @@ import uk.gov.justice.digital.hmpps.hmppsintegrationapi.gateways.NDeliusGateway import uk.gov.justice.digital.hmpps.hmppsintegrationapi.gateways.NomisGateway import uk.gov.justice.digital.hmpps.hmppsintegrationapi.gateways.PrisonerOffenderSearchGateway import uk.gov.justice.digital.hmpps.hmppsintegrationapi.gateways.ProbationOffenderSearchGateway +import uk.gov.justice.digital.hmpps.hmppsintegrationapi.helpers.generateTestSentence import uk.gov.justice.digital.hmpps.hmppsintegrationapi.models.Identifiers import uk.gov.justice.digital.hmpps.hmppsintegrationapi.models.Person import uk.gov.justice.digital.hmpps.hmppsintegrationapi.models.Response @@ -21,7 +22,6 @@ import uk.gov.justice.digital.hmpps.hmppsintegrationapi.models.UpstreamApi import uk.gov.justice.digital.hmpps.hmppsintegrationapi.models.UpstreamApiError import uk.gov.justice.digital.hmpps.hmppsintegrationapi.models.nomis.Booking 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 @ContextConfiguration( @@ -59,12 +59,8 @@ internal class GetSentencesForPersonServiceTest( whenever(nomisGateway.getBookingIdsForPerson(prisonerNumber)).thenReturn( Response( data = listOf( - Booking( - bookingId = firstBookingId, - ), - Booking( - bookingId = secondBookingId, - ), + Booking(bookingId = firstBookingId), + Booking(bookingId = secondBookingId), ), ), ) @@ -72,23 +68,12 @@ internal class GetSentencesForPersonServiceTest( whenever(nomisGateway.getSentencesForBooking(firstBookingId)).thenReturn( Response( data = listOf( - IntegrationApiSentence( + generateTestSentence( dateOfSentencing = LocalDate.parse("2001-01-01"), description = "ORA CJA03 Standard Determinate Sentence", - isActive = true, - isCustodial = true, terms = listOf( - IntegrationApiTerm( - years = 15, - months = 6, - weeks = 2, - ), - IntegrationApiTerm( - months = 6, - weeks = 2, - days = 5, - hours = null, - ), + IntegrationApiTerm(years = 15, months = 6, weeks = 2), + IntegrationApiTerm(months = 6, weeks = 2, days = 5), ), ), ), @@ -98,24 +83,13 @@ internal class GetSentencesForPersonServiceTest( whenever(nomisGateway.getSentencesForBooking(secondBookingId)).thenReturn( Response( data = listOf( - IntegrationApiSentence( + generateTestSentence( dateOfSentencing = LocalDate.parse("2002-01-01"), description = "ORA CJA04 Stealing hamburgers from the local restaurant", isActive = null, - isCustodial = true, terms = listOf( - IntegrationApiTerm( - years = 10, - months = null, - weeks = 5, - days = 5, - ), - IntegrationApiTerm( - years = 25, - months = null, - weeks = 5, - days = 4, - ), + IntegrationApiTerm(years = 10, weeks = 5, days = 5), + IntegrationApiTerm(years = 25, weeks = 5, days = 4), ), ), ), @@ -125,32 +99,21 @@ internal class GetSentencesForPersonServiceTest( whenever(nDeliusGateway.getSentencesForPerson(nDeliusCRN)).thenReturn( Response( data = listOf( - IntegrationApiSentence( + generateTestSentence( dateOfSentencing = LocalDate.parse("2003-01-01"), description = "CJA - Community Order", - isActive = true, - isCustodial = true, terms = listOf( - IntegrationApiTerm( - years = 4, - ), - IntegrationApiTerm( - weeks = 12, - ), + IntegrationApiTerm(years = 4), + IntegrationApiTerm(weeks = 12), ), ), - IntegrationApiSentence( + generateTestSentence( dateOfSentencing = LocalDate.parse("2004-01-01"), description = "CJA - Suspended Sentence Order", isActive = false, - isCustodial = true, terms = listOf( - IntegrationApiTerm( - weeks = 18, - ), - IntegrationApiTerm( - hours = 24, - ), + IntegrationApiTerm(weeks = 18), + IntegrationApiTerm(hours = 24), ), ), ), @@ -290,93 +253,38 @@ internal class GetSentencesForPersonServiceTest( response.data.shouldBe( listOf( - IntegrationApiSentence( + generateTestSentence( dateOfSentencing = LocalDate.parse("2001-01-01"), description = "ORA CJA03 Standard Determinate Sentence", - isActive = true, - isCustodial = true, terms = listOf( - IntegrationApiTerm( - years = 15, - months = 6, - weeks = 2, - days = null, - hours = null, - ), - IntegrationApiTerm( - years = null, - months = 6, - weeks = 2, - days = 5, - hours = null, - ), + IntegrationApiTerm(years = 15, months = 6, weeks = 2), + IntegrationApiTerm(months = 6, weeks = 2, days = 5), ), ), - IntegrationApiSentence( + generateTestSentence( dateOfSentencing = LocalDate.parse("2002-01-01"), description = "ORA CJA04 Stealing hamburgers from the local restaurant", isActive = null, - isCustodial = true, terms = listOf( - IntegrationApiTerm( - years = 10, - months = null, - weeks = 5, - days = 5, - hours = null, - ), - IntegrationApiTerm( - years = 25, - months = null, - weeks = 5, - days = 4, - hours = null, - ), + IntegrationApiTerm(years = 10, weeks = 5, days = 5), + IntegrationApiTerm(years = 25, weeks = 5, days = 4), ), ), - - IntegrationApiSentence( + generateTestSentence( dateOfSentencing = LocalDate.parse("2003-01-01"), description = "CJA - Community Order", - isActive = true, - isCustodial = true, terms = listOf( - IntegrationApiTerm( - years = 4, - months = null, - weeks = null, - days = null, - hours = null, - ), - IntegrationApiTerm( - years = null, - months = null, - weeks = 12, - days = null, - hours = null, - ), + IntegrationApiTerm(years = 4), + IntegrationApiTerm(weeks = 12), ), ), - IntegrationApiSentence( + generateTestSentence( dateOfSentencing = LocalDate.parse("2004-01-01"), description = "CJA - Suspended Sentence Order", isActive = false, - isCustodial = true, terms = listOf( - IntegrationApiTerm( - years = null, - months = null, - weeks = 18, - days = null, - hours = null, - ), - IntegrationApiTerm( - years = null, - months = null, - weeks = null, - days = null, - hours = 24, - ), + IntegrationApiTerm(weeks = 18), + IntegrationApiTerm(hours = 24), ), ), ),