Skip to content

Commit 017816f

Browse files
committed
Expanded transaction controller tests
1 parent ce7ac37 commit 017816f

File tree

3 files changed

+131
-18
lines changed

3 files changed

+131
-18
lines changed

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

+44-18
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
package uk.gov.justice.digital.hmpps.hmppsintegrationapi.controllers.v1
22

3+
import io.swagger.v3.oas.annotations.Operation
34
import io.swagger.v3.oas.annotations.Parameter
5+
import io.swagger.v3.oas.annotations.media.Content
6+
import io.swagger.v3.oas.annotations.responses.ApiResponse
47
import jakarta.validation.ValidationException
58
import org.springframework.beans.factory.annotation.Autowired
69
import org.springframework.web.bind.annotation.GetMapping
@@ -24,36 +27,59 @@ class TransactionsController(
2427
@Autowired val auditService: AuditService,
2528
@Autowired val getTransactionsForPersonService: GetTransactionsForPersonService,
2629
) {
27-
// @Operation(
28-
// summary = "Returns all accounts for a prisoner that they have at a prison.",
29-
// description = "<b>Applicable filters</b>: <ul><li>prisons</li></ul>",
30-
// responses = [
31-
// ApiResponse(responseCode = "200", useReturnTypeSchema = true, description = "Successfully found a prisoner's accounts."),
32-
// ApiResponse(
33-
// responseCode = "400",
34-
// description = "The HMPPS ID provided has an invalid format or the prisoner does hot have accounts at the specified prison.",
35-
// content = [Content(schema = Schema(ref = "#/components/schemas/BadRequest"))],
36-
// ),
37-
// ApiResponse(responseCode = "404", content = [Content(schema = Schema(ref = "#/components/schemas/PersonNotFound"))]),
38-
// ApiResponse(responseCode = "500", content = [Content(schema = Schema(ref = "#/components/schemas/InternalServerError"))]),
39-
// ],
40-
// )
30+
@Operation(
31+
summary = "Returns all transactions for a prisoner that they have at a prison.",
32+
description = "<b>Applicable filters</b>: <ul><li>prisons</li></ul>",
33+
responses = [
34+
ApiResponse(responseCode = "200", useReturnTypeSchema = true, description = "Successfully found a prisoner's transactions."),
35+
ApiResponse(
36+
responseCode = "400",
37+
description = "The HMPPS ID provided has an invalid format or the prisoner does hot have transactions at the specified prison.",
38+
content = [
39+
Content(
40+
schema =
41+
io.swagger.v3.oas.annotations.media
42+
.Schema(ref = "#/components/schemas/BadRequest"),
43+
),
44+
],
45+
),
46+
ApiResponse(
47+
responseCode = "404",
48+
content = [
49+
Content(
50+
schema =
51+
io.swagger.v3.oas.annotations.media
52+
.Schema(ref = "#/components/schemas/PersonNotFound"),
53+
),
54+
],
55+
),
56+
ApiResponse(
57+
responseCode = "500",
58+
content = [
59+
Content(
60+
schema =
61+
io.swagger.v3.oas.annotations.media
62+
.Schema(ref = "#/components/schemas/InternalServerError"),
63+
),
64+
],
65+
),
66+
],
67+
)
4168
@GetMapping()
42-
fun getAccountCodeForPerson(
69+
fun getTransactionsByAccountCode(
4370
@PathVariable hmppsId: String,
4471
@PathVariable prisonId: String,
4572
@PathVariable accountCode: String,
4673
@RequestAttribute filters: ConsumerFilters?,
47-
@Parameter(description = "Start date for transactions (defaults to today if not supplied)") @RequestParam(required = false, name = "fromDate") fromDate: String?,
48-
@Parameter(description = "To date for transactions (defaults to today if not supplied)") @RequestParam(required = false, name = "toDate") toDate: String?,
74+
@Parameter(description = "Start date for transactions (defaults to today if not supplied)") @RequestParam(required = false, name = "from_date") fromDate: String?,
75+
@Parameter(description = "To date for transactions (defaults to today if not supplied)") @RequestParam(required = false, name = "to_date") toDate: String?,
4976
): DataResponse<Transactions?> {
5077
var startDate = LocalDate.now().toString()
5178
var endDate = LocalDate.now().toString()
5279

5380
if (fromDate == null && toDate != null || toDate == null && fromDate != null) {
5481
throw ValidationException("Both fromDate and toDate must be supplied if one is populated")
5582
}
56-
// respect filters
5783
// catch parse failures
5884
if (fromDate != null && toDate != null) {
5985
startDate = fromDate

src/main/resources/application-test.yml

+1
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,7 @@ authorisation:
8787
- "/v1/prison/prisoners/[^/]*$"
8888
- "/v1/prison/prisoners"
8989
- "/v1/prison/.*/prisoners/.*/balances$"
90+
- "/v1/prison/.*/prisoners/.*/transactions/[^/]*$"
9091
config-test:
9192
include:
9293
- "/v1/config/authorisation"
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
package uk.gov.justice.digital.hmpps.hmppsintegrationapi.controllers.v1
2+
3+
import io.kotest.core.spec.style.DescribeSpec
4+
import io.kotest.matchers.string.shouldContain
5+
import org.mockito.internal.verification.VerificationModeFactory
6+
import org.mockito.kotlin.verify
7+
import org.mockito.kotlin.whenever
8+
import org.springframework.beans.factory.annotation.Autowired
9+
import org.springframework.boot.test.autoconfigure.web.servlet.WebMvcTest
10+
import org.springframework.test.context.ActiveProfiles
11+
import org.springframework.test.context.bean.override.mockito.MockitoBean
12+
import org.springframework.test.web.servlet.MockMvc
13+
import uk.gov.justice.digital.hmpps.hmppsintegrationapi.extensions.removeWhitespaceAndNewlines
14+
import uk.gov.justice.digital.hmpps.hmppsintegrationapi.helpers.IntegrationAPIMockMvc
15+
import uk.gov.justice.digital.hmpps.hmppsintegrationapi.models.hmpps.Response
16+
import uk.gov.justice.digital.hmpps.hmppsintegrationapi.models.hmpps.Transaction
17+
import uk.gov.justice.digital.hmpps.hmppsintegrationapi.models.hmpps.Transactions
18+
import uk.gov.justice.digital.hmpps.hmppsintegrationapi.models.hmpps.Type
19+
import uk.gov.justice.digital.hmpps.hmppsintegrationapi.services.GetTransactionsForPersonService
20+
import uk.gov.justice.digital.hmpps.hmppsintegrationapi.services.internal.AuditService
21+
import java.time.LocalDate
22+
23+
@WebMvcTest(controllers = [TransactionsController::class])
24+
@ActiveProfiles("test")
25+
class TransactionsControllerTest(
26+
@Autowired var springMockMvc: MockMvc,
27+
@MockitoBean val auditService: AuditService,
28+
@MockitoBean val getTransactionsForPersonService: GetTransactionsForPersonService,
29+
) : DescribeSpec(
30+
{
31+
val hmppsId = "200313116M"
32+
val prisonId = "ABC"
33+
val accountCode = "spends"
34+
val basePath = "/v1/prison/$prisonId/prisoners/$hmppsId/transactions/$accountCode"
35+
val mockMvc = IntegrationAPIMockMvc(springMockMvc)
36+
37+
val transactions =
38+
Transactions(
39+
transactions =
40+
listOf(
41+
Transaction(
42+
id = "123",
43+
type = Type(code = "spends", desc = "Spends"),
44+
amount = 100,
45+
date = LocalDate.parse("2025-01-01").toString(),
46+
description = "Spends desc",
47+
),
48+
),
49+
)
50+
51+
it("calls the service with expected parameters when supplied a date range") {
52+
val dateParams = "?from_date=2025-01-01&to_date=2025-01-01"
53+
mockMvc.performAuthorised(basePath + dateParams)
54+
55+
verify(getTransactionsForPersonService, VerificationModeFactory.times(1)).execute(hmppsId, prisonId, accountCode, "2025-01-01", "2025-01-01", null)
56+
}
57+
58+
it("returns a prisoners transactions according to supplied code") {
59+
whenever(getTransactionsForPersonService.execute(hmppsId, prisonId, accountCode, "2025-01-01", "2025-01-01", null)).thenReturn(Response(transactions))
60+
61+
val dateParams = "?from_date=2025-01-01&to_date=2025-01-01"
62+
val result = mockMvc.performAuthorised(basePath + dateParams)
63+
64+
result.response.contentAsString.shouldContain(
65+
"""
66+
{
67+
"data": {
68+
"transactions": [
69+
{
70+
"id": "123",
71+
"type": {
72+
"code": "spends",
73+
"desc": "Spends"
74+
},
75+
"description": "Spends desc",
76+
"amount": 100,
77+
"date": "2025-01-01"
78+
}
79+
]
80+
}
81+
}
82+
""".removeWhitespaceAndNewlines(),
83+
)
84+
}
85+
},
86+
)

0 commit comments

Comments
 (0)