1
1
package uk.gov.justice.digital.hmpps.hmppsintegrationapi.controllers.v1
2
2
3
- import com.fasterxml.jackson.databind.ObjectMapper
4
3
import io.kotest.core.spec.style.DescribeSpec
5
4
import io.kotest.matchers.shouldBe
6
5
import io.kotest.matchers.string.shouldContain
@@ -36,10 +35,11 @@ class BalancesControllerTest(
36
35
) : DescribeSpec({
37
36
val hmppsId = " 200313116M"
38
37
val prisonId = " ABC"
38
+ val accountCode = " spends"
39
39
40
- val basePath = " /v1/prison/$prisonId /prisoners/$hmppsId /balances"
40
+ val balancesPath = " /v1/prison/$prisonId /prisoners/$hmppsId /balances"
41
+ val accountCodePath = " /v1/prison/$prisonId /prisoners/$hmppsId /balances/$accountCode "
41
42
val mockMvc = IntegrationAPIMockMvc (springMockMvc)
42
- val objectMapper = ObjectMapper ()
43
43
val balance =
44
44
Balances (
45
45
balances =
@@ -49,20 +49,27 @@ class BalancesControllerTest(
49
49
AccountBalance (accountCode = "cash", amount = 103),
50
50
),
51
51
)
52
+ val singleBalance =
53
+ Balances (
54
+ balances =
55
+ listOf(
56
+ AccountBalance (accountCode = "spends", amount = 201),
57
+ ),
58
+ )
52
59
53
60
it("gets the balances for a person with the matching ID ") {
54
- mockMvc.performAuthorised(basePath )
61
+ mockMvc.performAuthorised(balancesPath )
55
62
56
- verify(getBalancesForPersonService, VerificationModeFactory .times(1)).execute(prisonId, hmppsId, null)
63
+ verify(getBalancesForPersonService, VerificationModeFactory .times(1)).execute(prisonId, hmppsId, filters = null)
57
64
}
58
65
59
66
it("returns the correct balances data") {
60
- whenever(getBalancesForPersonService.execute(prisonId, hmppsId, null)).thenReturn(
67
+ whenever(getBalancesForPersonService.execute(prisonId, hmppsId, filters = null)).thenReturn(
61
68
Response (
62
69
data = balance,
63
70
),
64
71
)
65
- val result = mockMvc.performAuthorised(basePath )
72
+ val result = mockMvc.performAuthorised(balancesPath )
66
73
result.response.contentAsString.shouldContain(
67
74
"""
68
75
"data": {
@@ -86,8 +93,8 @@ class BalancesControllerTest(
86
93
}
87
94
88
95
it("calls the API with the correct filters") {
89
- mockMvc.performAuthorisedWithCN(basePath , "limited-prisons")
90
- verify(getBalancesForPersonService, times(1)).execute(prisonId, hmppsId, ConsumerFilters (prisons = listOf("XYZ ")))
96
+ mockMvc.performAuthorisedWithCN(balancesPath , "limited-prisons")
97
+ verify(getBalancesForPersonService, times(1)).execute(prisonId, hmppsId, filters = ConsumerFilters (prisons = listOf("XYZ ")))
91
98
}
92
99
93
100
it("returns a 404 NOT FOUND status code when person isn't found in probation offender search") {
@@ -104,13 +111,13 @@ class BalancesControllerTest(
104
111
),
105
112
)
106
113
107
- val result = mockMvc.performAuthorised(basePath )
114
+ val result = mockMvc.performAuthorised(balancesPath )
108
115
109
116
result.response.status.shouldBe(HttpStatus .NOT_FOUND .value())
110
117
}
111
118
112
119
it("returns a 404 NOT FOUND status code when person isn't found in Nomis ") {
113
- whenever(getBalancesForPersonService.execute(prisonId, hmppsId, null)).thenReturn(
120
+ whenever(getBalancesForPersonService.execute(prisonId, hmppsId, filters = null)).thenReturn(
114
121
Response (
115
122
data = null,
116
123
errors =
@@ -123,13 +130,112 @@ class BalancesControllerTest(
123
130
),
124
131
)
125
132
126
- val result = mockMvc.performAuthorised(basePath )
133
+ val result = mockMvc.performAuthorised(balancesPath )
127
134
128
135
result.response.status.shouldBe(HttpStatus .NOT_FOUND .value())
129
136
}
130
137
131
138
it("returns a 400 BAD REQUEST status code when account isn't found in the upstream API ") {
132
- whenever(getBalancesForPersonService.execute(prisonId, hmppsId, null)).thenReturn(
139
+ whenever(getBalancesForPersonService.execute(prisonId, hmppsId, filters = null)).thenReturn(
140
+ Response (
141
+ data = null,
142
+ errors =
143
+ listOf(
144
+ UpstreamApiError (
145
+ type = UpstreamApiError .Type .BAD_REQUEST ,
146
+ causedBy = UpstreamApi .NOMIS ,
147
+ ),
148
+ ),
149
+ ),
150
+ )
151
+
152
+ val result = mockMvc.performAuthorised(balancesPath)
153
+
154
+ result.response.status.shouldBe(HttpStatus .BAD_REQUEST .value())
155
+ }
156
+
157
+ it("returns a 500 INTERNAL SERVER ERROR status code when balance isn't found in the upstream API ") {
158
+ whenever(getBalancesForPersonService.execute(prisonId, hmppsId, filters = null)).thenThrow(
159
+ IllegalStateException ("Error occurred while trying to get accounts for person with id: $hmppsId"),
160
+ )
161
+
162
+ val result = mockMvc.performAuthorised(balancesPath)
163
+
164
+ result.response.status.shouldBe(HttpStatus .INTERNAL_SERVER_ERROR .value())
165
+ }
166
+
167
+ it("gets the balance for the relevant account code for a person with the matching ID ") {
168
+ mockMvc.performAuthorised(accountCodePath)
169
+
170
+ verify(getBalancesForPersonService, VerificationModeFactory .times(1)).getBalance(prisonId, hmppsId, accountCode, null)
171
+ }
172
+
173
+ it("returns the correct balance data when given an account code") {
174
+ whenever(getBalancesForPersonService.getBalance(prisonId, hmppsId, accountCode, filters = null)).thenReturn(
175
+ Response (
176
+ data = singleBalance,
177
+ ),
178
+ )
179
+ val result = mockMvc.performAuthorised(accountCodePath)
180
+ result.response.contentAsString.shouldContain(
181
+ """
182
+ "data": {
183
+ "balances": [
184
+ {
185
+ "accountCode": "spends",
186
+ "amount": 201
187
+ }
188
+ ]
189
+ }
190
+ """.removeWhitespaceAndNewlines(),
191
+ )
192
+ }
193
+
194
+ it("calls the API with the correct filters") {
195
+ mockMvc.performAuthorisedWithCN(accountCodePath, "limited-prisons")
196
+ verify(getBalancesForPersonService, times(1)).getBalance(prisonId, hmppsId, accountCode, filters = ConsumerFilters (prisons = listOf("XYZ ")))
197
+ }
198
+
199
+ it("returns a 404 NOT FOUND status code when person isn't found in probation offender search") {
200
+ whenever(getBalancesForPersonService.getBalance(prisonId, hmppsId, accountCode)).thenReturn(
201
+ Response (
202
+ data = null,
203
+ errors =
204
+ listOf(
205
+ UpstreamApiError (
206
+ causedBy = UpstreamApi .PROBATION_OFFENDER_SEARCH ,
207
+ type = UpstreamApiError .Type .ENTITY_NOT_FOUND ,
208
+ ),
209
+ ),
210
+ ),
211
+ )
212
+
213
+ val result = mockMvc.performAuthorised(accountCodePath)
214
+
215
+ result.response.status.shouldBe(HttpStatus .NOT_FOUND .value())
216
+ }
217
+
218
+ it("returns a 404 NOT FOUND status code when person isn't found in Nomis ") {
219
+ whenever(getBalancesForPersonService.getBalance(prisonId, hmppsId, accountCode, filters = null)).thenReturn(
220
+ Response (
221
+ data = null,
222
+ errors =
223
+ listOf(
224
+ UpstreamApiError (
225
+ causedBy = UpstreamApi .NOMIS ,
226
+ type = UpstreamApiError .Type .ENTITY_NOT_FOUND ,
227
+ ),
228
+ ),
229
+ ),
230
+ )
231
+
232
+ val result = mockMvc.performAuthorised(accountCodePath)
233
+
234
+ result.response.status.shouldBe(HttpStatus .NOT_FOUND .value())
235
+ }
236
+
237
+ it("returns a 400 BAD REQUEST status code when account isn't found in the upstream API ") {
238
+ whenever(getBalancesForPersonService.getBalance(prisonId, hmppsId, accountCode, filters = null)).thenReturn(
133
239
Response (
134
240
data = null,
135
241
errors =
@@ -142,17 +248,17 @@ class BalancesControllerTest(
142
248
),
143
249
)
144
250
145
- val result = mockMvc.performAuthorised(basePath )
251
+ val result = mockMvc.performAuthorised(accountCodePath )
146
252
147
253
result.response.status.shouldBe(HttpStatus .BAD_REQUEST .value())
148
254
}
149
255
150
256
it("returns a 500 INTERNAL SERVER ERROR status code when balance isn't found in the upstream API ") {
151
- whenever(getBalancesForPersonService.execute (prisonId, hmppsId, null)).thenThrow(
257
+ whenever(getBalancesForPersonService.getBalance (prisonId, hmppsId, accountCode, filters = null)).thenThrow(
152
258
IllegalStateException ("Error occurred while trying to get accounts for person with id: $hmppsId"),
153
259
)
154
260
155
- val result = mockMvc.performAuthorised(basePath )
261
+ val result = mockMvc.performAuthorised(accountCodePath )
156
262
157
263
result.response.status.shouldBe(HttpStatus .INTERNAL_SERVER_ERROR .value())
158
264
}
0 commit comments