Skip to content

Commit 20c2d5e

Browse files
committed
WIP - Failing tests on GetBalanceForPersonService
1 parent 6a0ebbb commit 20c2d5e

File tree

2 files changed

+80
-115
lines changed

2 files changed

+80
-115
lines changed

src/main/kotlin/uk/gov/justice/digital/hmpps/hmppsintegrationapi/services/GetBalancesForPersonService.kt

+9-2
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import uk.gov.justice.digital.hmpps.hmppsintegrationapi.gateways.NomisGateway
66
import uk.gov.justice.digital.hmpps.hmppsintegrationapi.models.hmpps.AccountBalance
77
import uk.gov.justice.digital.hmpps.hmppsintegrationapi.models.hmpps.Balances
88
import uk.gov.justice.digital.hmpps.hmppsintegrationapi.models.hmpps.Response
9+
import uk.gov.justice.digital.hmpps.hmppsintegrationapi.models.nomis.NomisAccounts
910

1011
@Service
1112
class GetBalancesForPersonService(
@@ -16,9 +17,14 @@ class GetBalancesForPersonService(
1617
prisonId: String,
1718
hmppsId: String,
1819
): Response<Balances> {
19-
val nomisNumber = getPersonService.getNomisNumber(hmppsId = hmppsId).data?.nomisNumber
20+
val personResponse = getPersonService.getNomisNumber(hmppsId = hmppsId)
21+
val nomisNumber = personResponse.data?.nomisNumber
22+
var nomisAccounts: Response<NomisAccounts?> = Response(data = null)
23+
24+
if (nomisNumber != null) {
25+
nomisAccounts = nomisGateway.getAccountsForPerson(prisonId, nomisNumber)
26+
}
2027

21-
val nomisAccounts = nomisGateway.getAccountsForPerson(prisonId, nomisNumber)
2228
val nomisSpends = nomisAccounts.data?.spends
2329
val nomisSavings = nomisAccounts.data?.savings
2430
val nomisCash = nomisAccounts.data?.cash
@@ -35,6 +41,7 @@ class GetBalancesForPersonService(
3541

3642
return Response(
3743
data = balance,
44+
errors = personResponse.errors,
3845
)
3946
}
4047
}
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package uk.gov.justice.digital.hmpps.hmppsintegrationapi.services
22

33
import io.kotest.core.spec.style.DescribeSpec
4+
import io.kotest.matchers.collections.shouldHaveSize
45
import io.kotest.matchers.shouldBe
56
import org.mockito.Mockito
67
import org.mockito.internal.verification.VerificationModeFactory
@@ -14,6 +15,8 @@ import uk.gov.justice.digital.hmpps.hmppsintegrationapi.models.hmpps.AccountBala
1415
import uk.gov.justice.digital.hmpps.hmppsintegrationapi.models.hmpps.Balances
1516
import uk.gov.justice.digital.hmpps.hmppsintegrationapi.models.hmpps.NomisNumber
1617
import uk.gov.justice.digital.hmpps.hmppsintegrationapi.models.hmpps.Response
18+
import uk.gov.justice.digital.hmpps.hmppsintegrationapi.models.hmpps.UpstreamApi
19+
import uk.gov.justice.digital.hmpps.hmppsintegrationapi.models.hmpps.UpstreamApiError
1720
import uk.gov.justice.digital.hmpps.hmppsintegrationapi.models.nomis.NomisAccounts
1821

1922
@ContextConfiguration(
@@ -24,132 +27,87 @@ internal class GetBalancesForPersonServiceTest(
2427
@MockitoBean val nomisGateway: NomisGateway,
2528
@MockitoBean val getPersonService: GetPersonService,
2629
private val getBalancesForPersonService: GetBalancesForPersonService,
27-
) : DescribeSpec(
28-
{
29-
val hmppsId = "1234/56789B"
30-
val prisonerId = "Z99999ZZ"
31-
val prisonId = "ABC"
32-
val nomisSpends = 101
33-
val nomisSavings = 102
34-
val nomisCash = 103
30+
) : DescribeSpec({
3531

36-
beforeEach {
37-
Mockito.reset(getPersonService)
38-
Mockito.reset(nomisGateway)
32+
val hmppsId = "1234/56789B"
33+
val nomisNumber = "Z99999ZZ"
34+
val prisonId = "ABC"
35+
val nomisSpends = 101
36+
val nomisSavings = 102
37+
val nomisCash = 103
3938

40-
require(hmppsId.matches(Regex("^[0-9]+/[0-9A-Za-z]+$"))) {
41-
"Invalid Hmpps Id format: $hmppsId"
42-
}
39+
beforeEach {
40+
Mockito.reset(getPersonService)
41+
Mockito.reset(nomisGateway)
4342

44-
whenever(getPersonService.getNomisNumber(hmppsId = hmppsId)).thenReturn(
45-
Response(
46-
data = NomisNumber(nomisNumber = prisonerId),
47-
),
48-
)
43+
require(hmppsId.matches(Regex("^[0-9]+/[0-9A-Za-z]+$"))) {
44+
"Invalid Hmpps Id format: $hmppsId"
45+
}
4946

50-
whenever(nomisGateway.getAccountsForPerson(prisonId, prisonerId)).thenReturn(
51-
Response(
52-
data =
53-
NomisAccounts(spends = nomisSpends, savings = nomisSavings, cash = nomisCash),
47+
whenever(getPersonService.getNomisNumber(hmppsId = hmppsId)).thenReturn(
48+
Response(data = NomisNumber(nomisNumber = nomisNumber)),
49+
)
50+
51+
whenever(nomisGateway.getAccountsForPerson(prisonId, nomisNumber)).thenReturn(
52+
Response(
53+
data = NomisAccounts(spends = nomisSpends, savings = nomisSavings, cash = nomisCash),
54+
),
55+
)
56+
}
57+
val balance =
58+
Balances(
59+
accountBalances =
60+
listOf(
61+
AccountBalance(accountCode = "spends", amount = nomisSpends),
62+
AccountBalance(accountCode = "saving", amount = nomisSavings),
63+
AccountBalance(accountCode = "cash", amount = nomisCash),
5464
),
55-
)
56-
}
65+
)
5766

58-
val balance =
59-
Balances(
60-
accountBalances =
61-
listOf(
62-
AccountBalance(accountCode = "spends", amount = nomisSpends),
63-
AccountBalance(accountCode = "saving", amount = nomisSavings),
64-
AccountBalance(accountCode = "cash", amount = nomisCash),
65-
),
66-
)
67+
it("gets a person using a Hmpps ID") {
68+
getBalancesForPersonService.execute(prisonId, hmppsId)
6769

68-
it("gets a person using a Hmpps ID") {
69-
getBalancesForPersonService.execute(prisonId, hmppsId)
70+
verify(getPersonService, VerificationModeFactory.times(1)).getNomisNumber(hmppsId = hmppsId)
71+
}
7072

71-
verify(getPersonService, VerificationModeFactory.times(1)).execute(hmppsId = hmppsId)
72-
}
73+
it("gets accounts from NOMIS using a prisoner number") {
74+
getBalancesForPersonService.execute(prisonId, hmppsId)
75+
76+
verify(nomisGateway, VerificationModeFactory.times(1)).getAccountsForPerson(prisonId, nomisNumber)
77+
}
7378

74-
it("gets accounts from NOMIS using a prisoner number") {
75-
getBalancesForPersonService.execute(prisonId, hmppsId)
79+
it("returns a person's account balances given a Hmpps ID") {
80+
val result = getBalancesForPersonService.execute(prisonId, hmppsId)
7681

77-
verify(nomisGateway, VerificationModeFactory.times(1)).getAccountsForPerson(prisonId = prisonId, nomisNumber = prisonerId)
82+
result.data.shouldBe(balance)
83+
}
84+
85+
describe("when an upstream API returns an error when looking up a balance from a Hmpps ID") {
86+
beforeEach {
87+
whenever(getPersonService.getNomisNumber(hmppsId = hmppsId)).thenReturn(
88+
Response(
89+
data = null,
90+
errors =
91+
listOf(
92+
UpstreamApiError(
93+
causedBy = UpstreamApi.PRISONER_OFFENDER_SEARCH,
94+
type = UpstreamApiError.Type.ENTITY_NOT_FOUND,
95+
),
96+
),
97+
),
98+
)
7899
}
79100

80-
it("Returns a persons account balances given a hmppsId") {
81-
val result = getBalancesForPersonService.execute(prisonId, hmppsId)
101+
it("records upstream API errors") {
82102

83-
result.data.shouldBe(balance)
103+
val response = getBalancesForPersonService.execute(prisonId, hmppsId)
104+
response.errors.shouldHaveSize(1)
84105
}
85-
},
86-
)
87-
88-
// describe("when an upstream API returns an error when looking up a person from a Hmpps ID") {
89-
// beforeEach {
90-
// whenever(getPersonService.execute(hmppsId = hmppsId)).thenReturn(
91-
// Response(
92-
// data = null,
93-
// errors =
94-
// listOf(
95-
// UpstreamApiError(
96-
// causedBy = UpstreamApi.PRISONER_OFFENDER_SEARCH,
97-
// type = UpstreamApiError.Type.ENTITY_NOT_FOUND,
98-
// ),
99-
// UpstreamApiError(
100-
// causedBy = UpstreamApi.PROBATION_OFFENDER_SEARCH,
101-
// type = UpstreamApiError.Type.ENTITY_NOT_FOUND,
102-
// ),
103-
// ),
104-
// ),
105-
// )
106-
// }
107-
//
108-
// it("records upstream API errors") {
109-
// val response = getOffencesForPersonService.execute(hmppsId)
110-
// response.errors.shouldHaveSize(2)
111-
// }
112-
//
113-
// it("does not get offences from Nomis") {
114-
// getOffencesForPersonService.execute(hmppsId)
115-
// verify(nomisGateway, VerificationModeFactory.times(0)).getOffencesForPerson(id = prisonerNumber)
116-
// }
117-
//
118-
// it("does not get offences from nDelius") {
119-
// getOffencesForPersonService.execute(hmppsId)
120-
// verify(nDeliusGateway, VerificationModeFactory.times(0)).getOffencesForPerson(id = nDeliusCRN)
121-
// }
122-
// }
123-
//
124-
// it("records errors when it cannot find offences for a person") {
125-
// whenever(nDeliusGateway.getOffencesForPerson(id = nDeliusCRN)).thenReturn(
126-
// Response(
127-
// data = emptyList(),
128-
// errors =
129-
// listOf(
130-
// UpstreamApiError(
131-
// causedBy = UpstreamApi.NDELIUS,
132-
// type = UpstreamApiError.Type.ENTITY_NOT_FOUND,
133-
// ),
134-
// ),
135-
// ),
136-
// )
137-
//
138-
// whenever(nomisGateway.getOffencesForPerson(id = prisonerNumber)).thenReturn(
139-
// Response(
140-
// data = emptyList(),
141-
// errors =
142-
// listOf(
143-
// UpstreamApiError(
144-
// causedBy = UpstreamApi.NOMIS,
145-
// type = UpstreamApiError.Type.ENTITY_NOT_FOUND,
146-
// ),
147-
// ),
148-
// ),
149-
// )
106+
107+
// it("does not call NOMIS if the person is not found") {
108+
// getBalancesForPersonService.execute(prisonId, hmppsId)
150109
//
151-
// val response = getOffencesForPersonService.execute(hmppsId)
152-
// response.errors.shouldHaveSize(2)
110+
// verify(nomisGateway, VerificationModeFactory.times(0)).getAccountsForPerson(prisonId, nomisNumber)
153111
// }
154-
// },
155-
// )
112+
}
113+
})

0 commit comments

Comments
 (0)