Skip to content

Commit 32a8ef0

Browse files
committed
Integration test for transaction controller
1 parent 5f1e50d commit 32a8ef0

File tree

8 files changed

+86
-17
lines changed

8 files changed

+86
-17
lines changed

src/main/kotlin/uk/gov/justice/digital/hmpps/hmppsintegrationapi/controllers/v1/TransactionsController.kt

+3-3
Original file line numberDiff line numberDiff line change
@@ -28,13 +28,13 @@ class TransactionsController(
2828
@Autowired val getTransactionsForPersonService: GetTransactionsForPersonService,
2929
) {
3030
@Operation(
31-
summary = "Returns all transactions for a prisoner that they have at a prison.",
31+
summary = "Returns all transactions for a prisoner associated with an account code that they have at a prison.",
3232
description = "<b>Applicable filters</b>: <ul><li>prisons</li></ul>",
3333
responses = [
3434
ApiResponse(responseCode = "200", useReturnTypeSchema = true, description = "Successfully found a prisoner's transactions."),
3535
ApiResponse(
3636
responseCode = "400",
37-
description = "The HMPPS ID provided has an invalid format or the prisoner does hot have transactions at the specified prison.",
37+
description = "The request data has an invalid format or the prisoner does hot have transactions at the specified prison.",
3838
content = [
3939
Content(
4040
schema =
@@ -80,7 +80,7 @@ class TransactionsController(
8080
if (fromDate == null && toDate != null || toDate == null && fromDate != null) {
8181
throw ValidationException("Both fromDate and toDate must be supplied if one is populated")
8282
}
83-
// catch parse failures
83+
8484
if (fromDate != null && toDate != null) {
8585
startDate = fromDate
8686
endDate = toDate

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

+9-1
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,8 @@ class GetTransactionsForPersonService(
1717
@Autowired val getPersonService: GetPersonService,
1818
) {
1919
fun execute(
20-
prisonId: String,
2120
hmppsId: String,
21+
prisonId: String,
2222
accountCode: String,
2323
startDate: String,
2424
endDate: String,
@@ -34,6 +34,14 @@ class GetTransactionsForPersonService(
3434
}
3535

3636
val personResponse = getPersonService.getNomisNumber(hmppsId = hmppsId)
37+
38+
if (personResponse == null) {
39+
return Response(
40+
data = null,
41+
errors = listOf(UpstreamApiError(UpstreamApi.NOMIS, UpstreamApiError.Type.ENTITY_NOT_FOUND, "Nomis number not found")),
42+
)
43+
}
44+
3745
val nomisNumber = personResponse.data?.nomisNumber
3846

3947
if (nomisNumber == null) {

src/main/resources/application-integration-test.yml

+2-1
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,7 @@ authorisation:
8585
- "/v1/prison/prisoners/[^/]*$"
8686
- "/v1/prison/prisoners"
8787
- "/v1/prison/.*/prisoners/.*/balances$"
88-
filters:
88+
- "/v1/prison/.*/prisoners/.*/transactions/.*"
8989
config-test:
9090
include:
9191
- "/v1/config/authorisation"
@@ -97,6 +97,7 @@ authorisation:
9797
limited-prisons:
9898
include:
9999
- "/v1/prison/prisoners/[^/]*$"
100+
- "/v1/prison/.*/prisoners/.*/transactions/.*"
100101
filters:
101102
prisons:
102103
- ABC

src/main/resources/application-test.yml

+1-1
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,7 @@ authorisation:
8787
- "/v1/prison/prisoners/[^/]*$"
8888
- "/v1/prison/prisoners"
8989
- "/v1/prison/.*/prisoners/.*/balances$"
90-
- "/v1/prison/.*/prisoners/.*/transactions/[^/]*$"
90+
- "/v1/prison/.*/prisoners/.*/transactions/.*"
9191
config-test:
9292
include:
9393
- "/v1/config/authorisation"

src/test/kotlin/uk/gov/justice/digital/hmpps/hmppsintegrationapi/integration/IntegrationTestBase.kt

+2-3
Original file line numberDiff line numberDiff line change
@@ -55,9 +55,8 @@ abstract class IntegrationTestBase {
5555

5656
fun getExpectedResponse(filename: String): String = File("./src/test/resources/expected-responses/$filename").readText(Charsets.UTF_8).removeWhitespaceAndNewlines()
5757

58-
fun callApi(path: String): ResultActions {
59-
return mockMvc.perform(
58+
fun callApi(path: String): ResultActions =
59+
mockMvc.perform(
6060
get(path).headers(getAuthHeader()),
6161
)
62-
}
6362
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
package uk.gov.justice.digital.hmpps.hmppsintegrationapi.integration
2+
3+
import org.junit.jupiter.api.Test
4+
import org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get
5+
import org.springframework.test.web.servlet.result.MockMvcResultMatchers.content
6+
import org.springframework.test.web.servlet.result.MockMvcResultMatchers.status
7+
8+
class TransactionsIntegrationTest : IntegrationTestBase() {
9+
val prisonId = "MDI"
10+
val hmppsId = "G2996UX"
11+
val accountCode = "spends"
12+
final val fromDate = "2024-01-01"
13+
final val toDate = "2024-01-14"
14+
val transactionsPath = "/v1/prison"
15+
var dateQueryParams = "?from_date=$fromDate&to_date=$toDate"
16+
17+
@Test
18+
fun `return a list of transactions for a prisoner`() {
19+
callApi("/v1/prison/$prisonId/prisoners/$hmppsId/transactions/$accountCode")
20+
.andExpect(status().isOk)
21+
.andExpect(content().json(getExpectedResponse("transactions-response")))
22+
}
23+
24+
@Test
25+
fun `return a list of transactions when the dates are supplied in the request`() {
26+
val headers = org.springframework.http.HttpHeaders()
27+
headers.set("subject-distinguished-name", "C=GB,ST=London,L=London,O=Home Office,CN=automated-test-client")
28+
mockMvc
29+
.perform(
30+
get("$transactionsPath/$prisonId/prisoners/$hmppsId/transactions/$accountCode$dateQueryParams").headers(headers),
31+
).andExpect(status().isOk)
32+
.andExpect(content().json(getExpectedResponse("transactions-response")))
33+
}
34+
35+
@Test
36+
fun `returns no results`() {
37+
var wrongPrisonId = "XYZ"
38+
val headers = org.springframework.http.HttpHeaders()
39+
headers.set("subject-distinguished-name", "C=GB,ST=London,L=London,O=Home Office,CN=limited-prisons")
40+
mockMvc
41+
.perform(
42+
get("$transactionsPath/$wrongPrisonId/prisoners/$hmppsId/transactions/$accountCode").headers(headers),
43+
).andExpect(status().isNotFound)
44+
}
45+
}

src/test/kotlin/uk/gov/justice/digital/hmpps/hmppsintegrationapi/services/GetTransactionsForPersonServiceTest.kt

+8-8
Original file line numberDiff line numberDiff line change
@@ -66,8 +66,8 @@ internal class GetTransactionsForPersonServiceTest(
6666

6767
it("gets a person using a Hmpps ID") {
6868
getTransactionsForPersonService.execute(
69-
prisonId,
7069
hmppsId,
70+
prisonId,
7171
accountCode,
7272
startDate,
7373
endDate,
@@ -79,8 +79,8 @@ internal class GetTransactionsForPersonServiceTest(
7979

8080
it("gets transactions from NOMIS using a prisoner number") {
8181
getTransactionsForPersonService.execute(
82-
prisonId,
8382
hmppsId,
83+
prisonId,
8484
accountCode,
8585
startDate,
8686
endDate,
@@ -99,8 +99,8 @@ internal class GetTransactionsForPersonServiceTest(
9999
it("returns a person's transactions given a Hmpps ID") {
100100
val result =
101101
getTransactionsForPersonService.execute(
102-
prisonId,
103102
hmppsId,
103+
prisonId,
104104
accountCode,
105105
startDate,
106106
endDate,
@@ -125,8 +125,8 @@ internal class GetTransactionsForPersonServiceTest(
125125
)
126126
val response =
127127
getTransactionsForPersonService.execute(
128-
prisonId,
129128
hmppsId,
129+
prisonId,
130130
accountCode,
131131
startDate,
132132
endDate,
@@ -154,8 +154,8 @@ internal class GetTransactionsForPersonServiceTest(
154154
)
155155
val response =
156156
getTransactionsForPersonService.execute(
157-
prisonId,
158157
hmppsId,
158+
prisonId,
159159
accountCode,
160160
startDate,
161161
endDate,
@@ -191,8 +191,8 @@ internal class GetTransactionsForPersonServiceTest(
191191
)
192192
val response =
193193
getTransactionsForPersonService.execute(
194-
prisonId,
195194
hmppsId,
195+
prisonId,
196196
accountCode,
197197
startDate,
198198
endDate,
@@ -210,8 +210,8 @@ internal class GetTransactionsForPersonServiceTest(
210210
val wrongPrisonId = "XYZ"
211211
val result =
212212
getTransactionsForPersonService.execute(
213-
wrongPrisonId,
214213
hmppsId,
214+
wrongPrisonId,
215215
accountCode,
216216
startDate,
217217
endDate,
@@ -225,8 +225,8 @@ internal class GetTransactionsForPersonServiceTest(
225225
it("returns transactions when requested from an approved prison") {
226226
val result =
227227
getTransactionsForPersonService.execute(
228-
prisonId,
229228
hmppsId,
229+
prisonId,
230230
accountCode,
231231
startDate,
232232
endDate,
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
{
2+
"data": {
3+
"transactions": [
4+
{
5+
"id": "204564839-3",
6+
"type": {
7+
"code": "string",
8+
"desc": "string"
9+
},
10+
"description": "Transfer In Regular from caseload PVR",
11+
"amount": 12345,
12+
"date": "2016-10-21"
13+
}
14+
]
15+
}
16+
}

0 commit comments

Comments
 (0)