Skip to content

Commit 8f6fb22

Browse files
committed
Updated transaction controller integration tests with transaction post tests, updated prism mock schema, tidying
1 parent 005fde1 commit 8f6fb22

File tree

8 files changed

+75
-21
lines changed

8 files changed

+75
-21
lines changed

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

+10-2
Original file line numberDiff line numberDiff line change
@@ -218,10 +218,18 @@ class TransactionsController(
218218
val response = postTransactionsForPersonService.execute(prisonId, hmppsId, transactionRequest, filters)
219219

220220
if (response.hasError(UpstreamApiError.Type.BAD_REQUEST)) {
221-
throw ValidationException("Either invalid HMPPS ID: $hmppsId or incorrect prison: $prisonId or invalid request body: ${transactionRequest.toMap()}")
221+
throw ValidationException("Either invalid HMPPS ID: $hmppsId or incorrect prison: $prisonId or invalid request body: ${transactionRequest.toApiConformingMap()}")
222222
}
223223

224-
auditService.createEvent("CREATE_TRANSACTION", mapOf("hmppsId" to hmppsId, "prisonId" to prisonId)) // add req obj
224+
if (response.hasError(UpstreamApiError.Type.FORBIDDEN)) {
225+
throw ValidationException("The prisonId: $prisonId is not valid for your consumer profile. ${response.errors[0].description}")
226+
}
227+
228+
if (response.hasError(UpstreamApiError.Type.ENTITY_NOT_FOUND)) {
229+
throw EntityNotFoundException(" ${response.errors[0].description}")
230+
}
231+
232+
auditService.createEvent("CREATE_TRANSACTION", mapOf("hmppsId" to hmppsId, "prisonId" to prisonId, "transactionRequest" to transactionRequest.toApiConformingMap().toString())) // add req properly
225233
return DataResponse(response.data)
226234
}
227235
}

src/main/kotlin/uk/gov/justice/digital/hmpps/hmppsintegrationapi/gateways/NomisGateway.kt

+1-1
Original file line numberDiff line numberDiff line change
@@ -404,7 +404,7 @@ class NomisGateway(
404404
"/api/v1/prison/$prisonId/offenders/$nomisNumber/transactions",
405405
authenticationHeader(),
406406
UpstreamApi.NOMIS,
407-
requestBody = transactionRequest.toMap(),
407+
requestBody = transactionRequest.toApiConformingMap(),
408408
)
409409
return when (result) {
410410
is WebClientWrapperResponse.Success -> {

src/main/kotlin/uk/gov/justice/digital/hmpps/hmppsintegrationapi/models/hmpps/TransactionRequest.kt

+3-3
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,12 @@ data class TransactionRequest(
77
val clientTransactionId: String,
88
val clientUniqueRef: String,
99
) {
10-
fun toMap(): Map<String, Any?> =
10+
fun toApiConformingMap(): Map<String, Any?> =
1111
mapOf(
1212
"type" to type,
1313
"description" to description,
1414
"amount" to amount,
15-
"clientTransactionId" to clientTransactionId,
16-
"clientUniqueRef" to clientUniqueRef,
15+
"client_transaction_id" to clientTransactionId,
16+
"client_unique_ref" to clientUniqueRef,
1717
).filterValues { it != null }
1818
}

src/main/kotlin/uk/gov/justice/digital/hmpps/hmppsintegrationapi/models/nomis/NomisTransactionResponse.kt

+1-7
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,4 @@ package uk.gov.justice.digital.hmpps.hmppsintegrationapi.models.nomis
33
data class NomisTransactionResponse(
44
val id: String?,
55
val description: String?,
6-
) {
7-
fun toMap(): Map<String, String?> =
8-
mapOf(
9-
"id" to id,
10-
"description" to description,
11-
)
12-
}
6+
)

src/main/kotlin/uk/gov/justice/digital/hmpps/hmppsintegrationapi/prismMocks/prison-api.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -24173,7 +24173,7 @@
2417324173
"type": "string",
2417424174
"description": "Valid transaction type for the prison_id",
2417524175
"example": "CANT",
24176-
"enum": ["CANT,REFND,PHONE,MRPR,MTDS,DTDS,CASHD,RELA,RELS"]
24176+
"enum": ["CANT","REFND","PHONE","MRPR","MTDS","DTDS","CASHD","RELA","RELS"]
2417724177
},
2417824178
"description": {
2417924179
"maxLength": 240,

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

+13-3
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,9 @@ class PostTransactionForPersonService(
1515
@Autowired val nomisGateway: NomisGateway,
1616
@Autowired val getPersonService: GetPersonService,
1717
) {
18+
@Suppress("ktlint:standard:property-naming")
19+
final val VALID_TRANSACTION_TYPE_CODES = listOf("CANT", "REFND", "PHONE", "MRPR", "MTDS", "DTDS", "CASHD", "RELA", "RELS")
20+
1821
fun execute(
1922
prisonId: String,
2023
hmppsId: String,
@@ -26,7 +29,14 @@ class PostTransactionForPersonService(
2629
) {
2730
return Response(
2831
data = null,
29-
errors = listOf(UpstreamApiError(UpstreamApi.NOMIS, UpstreamApiError.Type.ENTITY_NOT_FOUND, "Not found")),
32+
errors = listOf(UpstreamApiError(UpstreamApi.NOMIS, UpstreamApiError.Type.FORBIDDEN, "Not found")),
33+
)
34+
}
35+
36+
if (transactionRequest.type !in VALID_TRANSACTION_TYPE_CODES) {
37+
return Response(
38+
data = null,
39+
errors = listOf(UpstreamApiError(UpstreamApi.NOMIS, UpstreamApiError.Type.BAD_REQUEST, "Invalid transaction type")),
3040
)
3141
}
3242

@@ -48,7 +58,7 @@ class PostTransactionForPersonService(
4858
)
4959
}
5060

51-
var response =
61+
val response =
5262
nomisGateway.postTransactionForPerson(
5363
prisonId,
5464
nomisNumber,
@@ -62,7 +72,7 @@ class PostTransactionForPersonService(
6272
)
6373
}
6474

65-
var transactionCreateResponse = TransactionCreateResponse()
75+
val transactionCreateResponse = TransactionCreateResponse()
6676
if (!response.data.id.isNullOrBlank()) {
6777
transactionCreateResponse.transactionId = response.data.id
6878
}

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

+40-4
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package uk.gov.justice.digital.hmpps.hmppsintegrationapi.integration
22

33
import org.junit.jupiter.api.Test
44
import org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get
5+
import org.springframework.test.web.servlet.request.MockMvcRequestBuilders.post
56
import org.springframework.test.web.servlet.result.MockMvcResultMatchers.content
67
import org.springframework.test.web.servlet.result.MockMvcResultMatchers.status
78

@@ -64,9 +65,44 @@ class TransactionsIntegrationTest : IntegrationTestBase() {
6465

6566
// POST transaction
6667
@Test
67-
fun `return a response with a transaction ID`() {
68-
callApi("/v1/prison/$prisonId/prisoners/$hmppsId/transactions")
69-
.andExpect(status().isOk)
70-
.andExpect(content().json(getExpectedResponse("transactions-response")))
68+
fun `return an expected response for a successful transaction post`() {
69+
val requestBody =
70+
"""
71+
{
72+
"type": "CANT",
73+
"description": "Canteen Purchase of £16.34",
74+
"amount": 1634,
75+
"clientTransactionId": "CL123212",
76+
"clientUniqueRef": "CLIENT121131-0_11"
77+
}
78+
""".trimIndent()
79+
80+
val headers = org.springframework.http.HttpHeaders()
81+
headers.set("subject-distinguished-name", "C=GB,ST=London,L=London,O=Home Office,CN=automated-test-client")
82+
mockMvc
83+
.perform(
84+
post("/v1/prison/$prisonId/prisoners/$hmppsId/transactions").headers(headers).content(requestBody).contentType(org.springframework.http.MediaType.APPLICATION_JSON),
85+
).andExpect(status().isOk)
86+
.andExpect(content().json(getExpectedResponse("transaction-create-response")))
87+
}
88+
89+
@Test
90+
fun `return a 400 when request object contains invalid type`() {
91+
val requestBody =
92+
"""
93+
{
94+
"type": "IS_WRONG",
95+
"amount": 1634,
96+
"clientTransactionId": "CL123212",
97+
"clientUniqueRef": "CLIENT121131-0_11"
98+
}
99+
""".trimIndent()
100+
101+
val headers = org.springframework.http.HttpHeaders()
102+
headers.set("subject-distinguished-name", "C=GB,ST=London,L=London,O=Home Office,CN=automated-test-client")
103+
mockMvc
104+
.perform(
105+
post("/v1/prison/$prisonId/prisoners/$hmppsId/transactions").headers(headers).content(requestBody).contentType(org.springframework.http.MediaType.APPLICATION_JSON),
106+
).andExpect(status().isBadRequest)
71107
}
72108
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
{
2+
"data":
3+
{
4+
"transactionId": "6179604-1"
5+
}
6+
}

0 commit comments

Comments
 (0)