1
1
package uk.gov.justice.digital.hmpps.hmppsintegrationapi.services
2
2
3
3
import io.kotest.core.spec.style.DescribeSpec
4
+ import io.kotest.matchers.collections.shouldHaveSize
4
5
import io.kotest.matchers.shouldBe
5
6
import org.mockito.Mockito
6
7
import org.mockito.internal.verification.VerificationModeFactory
@@ -14,6 +15,8 @@ import uk.gov.justice.digital.hmpps.hmppsintegrationapi.models.hmpps.AccountBala
14
15
import uk.gov.justice.digital.hmpps.hmppsintegrationapi.models.hmpps.Balances
15
16
import uk.gov.justice.digital.hmpps.hmppsintegrationapi.models.hmpps.NomisNumber
16
17
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
17
20
import uk.gov.justice.digital.hmpps.hmppsintegrationapi.models.nomis.NomisAccounts
18
21
19
22
@ContextConfiguration(
@@ -24,132 +27,87 @@ internal class GetBalancesForPersonServiceTest(
24
27
@MockitoBean val nomisGateway : NomisGateway ,
25
28
@MockitoBean val getPersonService : GetPersonService ,
26
29
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({
35
31
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
39
38
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)
43
42
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
+ }
49
46
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),
54
64
),
55
- )
56
- }
65
+ )
57
66
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)
67
69
68
- it("gets a person using a Hmpps ID ") {
69
- getBalancesForPersonService.execute(prisonId, hmppsId)
70
+ verify(getPersonService, VerificationModeFactory .times(1)).getNomisNumber(hmppsId = hmppsId)
71
+ }
70
72
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
+ }
73
78
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)
76
81
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
+ )
78
99
}
79
100
80
- it("Returns a persons account balances given a hmppsId") {
81
- val result = getBalancesForPersonService.execute(prisonId, hmppsId)
101
+ it("records upstream API errors") {
82
102
83
- result.data.shouldBe(balance)
103
+ val response = getBalancesForPersonService.execute(prisonId, hmppsId)
104
+ response.errors.shouldHaveSize(1)
84
105
}
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)
150
109
//
151
- // val response = getOffencesForPersonService.execute(hmppsId)
152
- // response.errors.shouldHaveSize(2)
110
+ // verify(nomisGateway, VerificationModeFactory.times(0)).getAccountsForPerson(prisonId, nomisNumber)
153
111
// }
154
- // },
155
- // )
112
+ }
113
+ } )
0 commit comments