Skip to content

Commit 843c035

Browse files
add service and gateway tests for new transaction clientUniqueRef endpoint
1 parent f772d32 commit 843c035

File tree

5 files changed

+163
-67
lines changed

5 files changed

+163
-67
lines changed

src/test/kotlin/uk/gov/justice/digital/hmpps/hmppsintegrationapi/controllers/v1/TransactionsControllerTest.kt

+7-4
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,10 @@ class TransactionsControllerTest(
3535
val hmppsId = "200313116M"
3636
val prisonId = "ABC"
3737
val accountCode = "spends"
38-
val basePath = "/v1/prison/$prisonId/prisoners/$hmppsId/accounts/$accountCode/transactions"
38+
val clientUniqueRef = "client_unique_ref"
39+
val basePath = "/v1/prison/$prisonId/prisoners/$hmppsId"
40+
val transactionsPath = "$basePath/accounts/$accountCode/transactions"
41+
val transactionPath = "$basePath/accounts/$clientUniqueRef/transactions"
3942
val mockMvc = IntegrationAPIMockMvc(springMockMvc)
4043

4144
val transactions =
@@ -54,7 +57,7 @@ class TransactionsControllerTest(
5457

5558
it("calls the service with expected parameters when supplied a date range") {
5659
val dateParams = "?from_date=2025-01-01&to_date=2025-01-01"
57-
mockMvc.performAuthorised(basePath + dateParams)
60+
mockMvc.performAuthorised(transactionsPath + dateParams)
5861

5962
verify(getTransactionsForPersonService, VerificationModeFactory.times(1)).execute(hmppsId, prisonId, accountCode, "2025-01-01", "2025-01-01", null)
6063
}
@@ -63,7 +66,7 @@ class TransactionsControllerTest(
6366
whenever(getTransactionsForPersonService.execute(hmppsId, prisonId, accountCode, "2025-01-01", "2025-01-01", null)).thenReturn(Response(transactions))
6467

6568
val dateParams = "?from_date=2025-01-01&to_date=2025-01-01"
66-
val result = mockMvc.performAuthorised(basePath + dateParams)
69+
val result = mockMvc.performAuthorised(transactionsPath + dateParams)
6770

6871
result.response.contentAsString.shouldContain(
6972
"""
@@ -101,7 +104,7 @@ class TransactionsControllerTest(
101104
),
102105
)
103106
val dateParams = "?from_date=2025-01-01&to_date=2025-01-01"
104-
val result = mockMvc.performAuthorised(basePath + dateParams)
107+
val result = mockMvc.performAuthorised(transactionsPath + dateParams)
105108

106109
result.response.status.shouldBe(HttpStatus.NOT_FOUND.value())
107110
}

src/test/kotlin/uk/gov/justice/digital/hmpps/hmppsintegrationapi/gateways/nomis/GetTransactionForPersonServiceTest.kt

-3
This file was deleted.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,123 @@
1+
package uk.gov.justice.digital.hmpps.hmppsintegrationapi.gateways.nomis
2+
3+
import io.kotest.core.spec.style.DescribeSpec
4+
import io.kotest.matchers.collections.shouldBeEmpty
5+
import io.kotest.matchers.collections.shouldHaveSize
6+
import io.kotest.matchers.shouldBe
7+
import org.mockito.Mockito
8+
import org.mockito.internal.verification.VerificationModeFactory
9+
import org.mockito.kotlin.verify
10+
import org.mockito.kotlin.whenever
11+
import org.springframework.boot.test.context.ConfigDataApplicationContextInitializer
12+
import org.springframework.http.HttpStatus
13+
import org.springframework.test.context.ActiveProfiles
14+
import org.springframework.test.context.ContextConfiguration
15+
import org.springframework.test.context.bean.override.mockito.MockitoBean
16+
import uk.gov.justice.digital.hmpps.hmppsintegrationapi.extensions.removeWhitespaceAndNewlines
17+
import uk.gov.justice.digital.hmpps.hmppsintegrationapi.gateways.HmppsAuthGateway
18+
import uk.gov.justice.digital.hmpps.hmppsintegrationapi.gateways.NomisGateway
19+
import uk.gov.justice.digital.hmpps.hmppsintegrationapi.mockservers.HmppsAuthMockServer
20+
import uk.gov.justice.digital.hmpps.hmppsintegrationapi.mockservers.NomisApiMockServer
21+
import uk.gov.justice.digital.hmpps.hmppsintegrationapi.models.hmpps.UpstreamApi
22+
import uk.gov.justice.digital.hmpps.hmppsintegrationapi.models.hmpps.UpstreamApiError
23+
24+
@ActiveProfiles("test")
25+
@ContextConfiguration(
26+
initializers = [ConfigDataApplicationContextInitializer::class],
27+
classes = [NomisGateway::class],
28+
)
29+
class GetTransactionForPersonTest(
30+
@MockitoBean val hmppsAuthGateway: HmppsAuthGateway,
31+
val nomisGateway: NomisGateway,
32+
) : DescribeSpec(
33+
{
34+
val nomisApiMockServer = NomisApiMockServer()
35+
val nomisNumber = "AA1234Z"
36+
val prisonId = "XYZ"
37+
val clientUniqueRef = "client_unique_ref"
38+
val transactionPath = "/api/v1/prison/$prisonId/offenders/$nomisNumber/transactions/$clientUniqueRef"
39+
40+
beforeEach {
41+
nomisApiMockServer.start()
42+
nomisApiMockServer.stubNomisApiResponse(
43+
transactionPath,
44+
"""
45+
{
46+
"id": "204564839-3",
47+
"type": {
48+
"code": "spends",
49+
"desc": "Spends account code"
50+
},
51+
"description": "Transfer In Regular from caseload PVR",
52+
"amount": 12345,
53+
"date": "2016-10-21"
54+
}
55+
""".removeWhitespaceAndNewlines(),
56+
)
57+
58+
Mockito.reset(hmppsAuthGateway)
59+
whenever(hmppsAuthGateway.getClientToken("NOMIS")).thenReturn(HmppsAuthMockServer.TOKEN)
60+
}
61+
62+
afterTest {
63+
nomisApiMockServer.stop()
64+
}
65+
66+
it("authenticates using HMPPS Auth with credentials") {
67+
nomisGateway.getTransactionForPerson(
68+
prisonId,
69+
nomisNumber,
70+
clientUniqueRef,
71+
)
72+
73+
verify(hmppsAuthGateway, VerificationModeFactory.times(1)).getClientToken("NOMIS")
74+
}
75+
76+
it("returns a transaction for the matching person ID") {
77+
val response =
78+
nomisGateway.getTransactionForPerson(
79+
prisonId,
80+
nomisNumber,
81+
clientUniqueRef,
82+
)
83+
84+
response.errors.shouldBeEmpty()
85+
response.data
86+
?.id
87+
.shouldBe("204564839-3")
88+
response.data
89+
?.type
90+
?.code
91+
.shouldBe("spends")
92+
response.data
93+
?.type
94+
?.desc
95+
.shouldBe("Spends account code")
96+
response.data
97+
?.description
98+
.shouldBe("Transfer In Regular from caseload PVR")
99+
response.data
100+
?.amount
101+
.shouldBe(12345)
102+
response.data
103+
?.date
104+
.shouldBe("2016-10-21")
105+
}
106+
107+
it("returns an error when 404 Not Found is returned because no person is found") {
108+
nomisApiMockServer.stubNomisApiResponse(transactionPath, "", HttpStatus.NOT_FOUND)
109+
110+
val response = nomisGateway.getAccountsForPerson(prisonId, nomisNumber)
111+
112+
response.errors.shouldHaveSize(1)
113+
response.errors
114+
.first()
115+
.causedBy
116+
.shouldBe(UpstreamApi.NOMIS)
117+
response.errors
118+
.first()
119+
.type
120+
.shouldBe(UpstreamApiError.Type.ENTITY_NOT_FOUND)
121+
}
122+
},
123+
)

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

+33-59
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@ import uk.gov.justice.digital.hmpps.hmppsintegrationapi.gateways.NomisGateway
1313
import uk.gov.justice.digital.hmpps.hmppsintegrationapi.models.hmpps.NomisNumber
1414
import uk.gov.justice.digital.hmpps.hmppsintegrationapi.models.hmpps.Response
1515
import uk.gov.justice.digital.hmpps.hmppsintegrationapi.models.hmpps.Transaction
16-
import uk.gov.justice.digital.hmpps.hmppsintegrationapi.models.hmpps.Transactions
1716
import uk.gov.justice.digital.hmpps.hmppsintegrationapi.models.hmpps.Type
1817
import uk.gov.justice.digital.hmpps.hmppsintegrationapi.models.hmpps.UpstreamApi
1918
import uk.gov.justice.digital.hmpps.hmppsintegrationapi.models.hmpps.UpstreamApiError
@@ -26,16 +25,14 @@ import uk.gov.justice.digital.hmpps.hmppsintegrationapi.models.roleconfig.Consum
2625
internal class GetTransactionForPersonServiceTest(
2726
@MockitoBean val nomisGateway: NomisGateway,
2827
@MockitoBean val getPersonService: GetPersonService,
29-
private val getTransactionsForPersonService: GetTransactionsForPersonService,
28+
private val getTransactionForPersonService: GetTransactionForPersonService,
3029
) : DescribeSpec({
3130
val hmppsId = "1234/56789B"
3231
val nomisNumber = "Z99999ZZ"
3332
val prisonId = "ABC"
34-
val accountCode = "spends"
35-
val startDate = "2019-04-01"
36-
val endDate = "2019-04-05"
33+
val clientUniqueRef = "client_unique_ref"
3734
val filters = ConsumerFilters(null)
38-
val exampleTransactions = Transactions(listOf(Transaction("204564839-3", Type(code = "spends", desc = "Spends desc"), "Spends account code", 12345, "2016-10-21")))
35+
val exampleTransaction = Transaction("204564839-3", Type(code = "spends", desc = "Spends desc"), "Spends account code", 12345, "2016-10-21")
3936

4037
beforeEach {
4138
Mockito.reset(getPersonService)
@@ -50,64 +47,54 @@ internal class GetTransactionForPersonServiceTest(
5047
)
5148

5249
whenever(
53-
nomisGateway.getTransactionsForPerson(
50+
nomisGateway.getTransactionForPerson(
5451
prisonId,
5552
nomisNumber,
56-
accountCode,
57-
startDate,
58-
endDate,
53+
clientUniqueRef,
5954
),
6055
).thenReturn(
6156
Response(
62-
data = exampleTransactions,
57+
data = exampleTransaction,
6358
),
6459
)
6560
}
6661

6762
it("gets a person using a Hmpps ID") {
68-
getTransactionsForPersonService.execute(
63+
getTransactionForPersonService.execute(
6964
hmppsId,
7065
prisonId,
71-
accountCode,
72-
startDate,
73-
endDate,
66+
clientUniqueRef,
7467
filters,
7568
)
7669

7770
verify(getPersonService, VerificationModeFactory.times(1)).getNomisNumber(hmppsId = hmppsId)
7871
}
7972

80-
it("gets transactions from NOMIS using a prisoner number") {
81-
getTransactionsForPersonService.execute(
73+
it("gets a transaction from NOMIS using a unique client reference number") {
74+
getTransactionForPersonService.execute(
8275
hmppsId,
8376
prisonId,
84-
accountCode,
85-
startDate,
86-
endDate,
77+
clientUniqueRef,
8778
filters,
8879
)
8980

90-
verify(nomisGateway, VerificationModeFactory.times(1)).getTransactionsForPerson(
81+
verify(nomisGateway, VerificationModeFactory.times(1)).getTransactionForPerson(
9182
prisonId,
9283
nomisNumber,
93-
accountCode,
94-
startDate,
95-
endDate,
84+
clientUniqueRef,
9685
)
9786
}
9887

99-
it("returns a person's transactions given a Hmpps ID") {
88+
it("returns a transaction given a Hmpps ID") {
10089
val result =
101-
getTransactionsForPersonService.execute(
90+
getTransactionForPersonService.execute(
10291
hmppsId,
10392
prisonId,
104-
accountCode,
105-
startDate,
106-
endDate,
93+
clientUniqueRef,
10794
filters,
10895
)
10996

110-
result.data.shouldBe(exampleTransactions)
97+
result.data.shouldBe(exampleTransaction)
11198
}
11299

113100
it("records upstream API errors") {
@@ -124,12 +111,10 @@ internal class GetTransactionForPersonServiceTest(
124111
),
125112
)
126113
val response =
127-
getTransactionsForPersonService.execute(
114+
getTransactionForPersonService.execute(
128115
hmppsId,
129116
prisonId,
130-
accountCode,
131-
startDate,
132-
endDate,
117+
clientUniqueRef,
133118
filters,
134119
)
135120
response
@@ -153,12 +138,10 @@ internal class GetTransactionForPersonServiceTest(
153138
),
154139
)
155140
val response =
156-
getTransactionsForPersonService.execute(
141+
getTransactionForPersonService.execute(
157142
hmppsId,
158143
prisonId,
159-
accountCode,
160-
startDate,
161-
endDate,
144+
clientUniqueRef,
162145
filters,
163146
)
164147
response
@@ -168,14 +151,12 @@ internal class GetTransactionForPersonServiceTest(
168151
).shouldBe(true)
169152
}
170153

171-
it("records upstream API errors when getTransactionsForPerson returns errors") {
154+
it("records upstream API errors when getTransactionForPerson returns errors") {
172155
whenever(
173-
nomisGateway.getTransactionsForPerson(
156+
nomisGateway.getTransactionForPerson(
174157
prisonId,
175158
nomisNumber,
176-
accountCode,
177-
startDate,
178-
endDate,
159+
clientUniqueRef,
179160
),
180161
).thenReturn(
181162
Response(
@@ -190,12 +171,10 @@ internal class GetTransactionForPersonServiceTest(
190171
),
191172
)
192173
val response =
193-
getTransactionsForPersonService.execute(
174+
getTransactionForPersonService.execute(
194175
hmppsId,
195176
prisonId,
196-
accountCode,
197-
startDate,
198-
endDate,
177+
clientUniqueRef,
199178
filters,
200179
)
201180
response
@@ -205,34 +184,29 @@ internal class GetTransactionForPersonServiceTest(
205184
).shouldBe(true)
206185
}
207186

208-
// break this
209-
it("returns null when transactions are requested from an unapproved prison") {
187+
it("returns null when a transaction is requested from an unapproved prison") {
210188
val wrongPrisonId = "XYZ"
211189
val result =
212-
getTransactionsForPersonService.execute(
190+
getTransactionForPersonService.execute(
213191
hmppsId,
214192
wrongPrisonId,
215-
accountCode,
216-
startDate,
217-
endDate,
193+
clientUniqueRef,
218194
ConsumerFilters(prisons = listOf("ABC")),
219195
)
220196

221197
result.data.shouldBe(null)
222198
result.errors.shouldBe(listOf(UpstreamApiError(UpstreamApi.NOMIS, UpstreamApiError.Type.ENTITY_NOT_FOUND, "Not found")))
223199
}
224200

225-
it("returns transactions when requested from an approved prison") {
201+
it("returns a transaction when requested from an approved prison") {
226202
val result =
227-
getTransactionsForPersonService.execute(
203+
getTransactionForPersonService.execute(
228204
hmppsId,
229205
prisonId,
230-
accountCode,
231-
startDate,
232-
endDate,
206+
clientUniqueRef,
233207
ConsumerFilters(prisons = listOf(prisonId)),
234208
)
235209

236-
result.data.shouldBe(exampleTransactions)
210+
result.data.shouldBe(exampleTransaction)
237211
}
238212
})

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

-1
Original file line numberDiff line numberDiff line change
@@ -205,7 +205,6 @@ internal class GetTransactionsForPersonServiceTest(
205205
).shouldBe(true)
206206
}
207207

208-
// break this
209208
it("returns null when transactions are requested from an unapproved prison") {
210209
val wrongPrisonId = "XYZ"
211210
val result =

0 commit comments

Comments
 (0)