Skip to content

Commit ce7ac37

Browse files
committed
Get transactions service layer unit tests
1 parent f45f44d commit ce7ac37

File tree

1 file changed

+238
-0
lines changed

1 file changed

+238
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,238 @@
1+
package uk.gov.justice.digital.hmpps.hmppsintegrationapi.services
2+
3+
import io.kotest.core.spec.style.DescribeSpec
4+
import io.kotest.matchers.shouldBe
5+
import org.mockito.Mockito
6+
import org.mockito.internal.verification.VerificationModeFactory
7+
import org.mockito.kotlin.verify
8+
import org.mockito.kotlin.whenever
9+
import org.springframework.boot.test.context.ConfigDataApplicationContextInitializer
10+
import org.springframework.test.context.ContextConfiguration
11+
import org.springframework.test.context.bean.override.mockito.MockitoBean
12+
import uk.gov.justice.digital.hmpps.hmppsintegrationapi.gateways.NomisGateway
13+
import uk.gov.justice.digital.hmpps.hmppsintegrationapi.models.hmpps.NomisNumber
14+
import uk.gov.justice.digital.hmpps.hmppsintegrationapi.models.hmpps.Response
15+
import uk.gov.justice.digital.hmpps.hmppsintegrationapi.models.hmpps.Transaction
16+
import uk.gov.justice.digital.hmpps.hmppsintegrationapi.models.hmpps.Transactions
17+
import uk.gov.justice.digital.hmpps.hmppsintegrationapi.models.hmpps.Type
18+
import uk.gov.justice.digital.hmpps.hmppsintegrationapi.models.hmpps.UpstreamApi
19+
import uk.gov.justice.digital.hmpps.hmppsintegrationapi.models.hmpps.UpstreamApiError
20+
import uk.gov.justice.digital.hmpps.hmppsintegrationapi.models.roleconfig.ConsumerFilters
21+
22+
@ContextConfiguration(
23+
initializers = [ConfigDataApplicationContextInitializer::class],
24+
classes = [GetTransactionsForPersonService::class],
25+
)
26+
internal class GetTransactionsForPersonServiceTest(
27+
@MockitoBean val nomisGateway: NomisGateway,
28+
@MockitoBean val getPersonService: GetPersonService,
29+
private val getTransactionsForPersonService: GetTransactionsForPersonService,
30+
) : DescribeSpec({
31+
val hmppsId = "1234/56789B"
32+
val nomisNumber = "Z99999ZZ"
33+
val prisonId = "ABC"
34+
val accountCode = "spends"
35+
val startDate = "2019-04-01"
36+
val endDate = "2019-04-05"
37+
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")))
39+
40+
beforeEach {
41+
Mockito.reset(getPersonService)
42+
Mockito.reset(nomisGateway)
43+
44+
require(hmppsId.matches(Regex("^[0-9]+/[0-9A-Za-z]+$"))) {
45+
"Invalid Hmpps Id format: $hmppsId"
46+
}
47+
48+
whenever(getPersonService.getNomisNumber(hmppsId = hmppsId)).thenReturn(
49+
Response(data = NomisNumber(nomisNumber = nomisNumber)),
50+
)
51+
52+
whenever(
53+
nomisGateway.getTransactionsForPerson(
54+
prisonId,
55+
nomisNumber,
56+
accountCode,
57+
startDate,
58+
endDate,
59+
),
60+
).thenReturn(
61+
Response(
62+
data = exampleTransactions,
63+
),
64+
)
65+
}
66+
67+
it("gets a person using a Hmpps ID") {
68+
getTransactionsForPersonService.execute(
69+
prisonId,
70+
hmppsId,
71+
accountCode,
72+
startDate,
73+
endDate,
74+
filters,
75+
)
76+
77+
verify(getPersonService, VerificationModeFactory.times(1)).getNomisNumber(hmppsId = hmppsId)
78+
}
79+
80+
it("gets transactions from NOMIS using a prisoner number") {
81+
getTransactionsForPersonService.execute(
82+
prisonId,
83+
hmppsId,
84+
accountCode,
85+
startDate,
86+
endDate,
87+
filters,
88+
)
89+
90+
verify(nomisGateway, VerificationModeFactory.times(1)).getTransactionsForPerson(
91+
prisonId,
92+
nomisNumber,
93+
accountCode,
94+
startDate,
95+
endDate,
96+
)
97+
}
98+
99+
it("returns a person's transactions given a Hmpps ID") {
100+
val result =
101+
getTransactionsForPersonService.execute(
102+
prisonId,
103+
hmppsId,
104+
accountCode,
105+
startDate,
106+
endDate,
107+
filters,
108+
)
109+
110+
result.data.shouldBe(exampleTransactions)
111+
}
112+
113+
it("records upstream API errors") {
114+
whenever(getPersonService.getNomisNumber(hmppsId = hmppsId)).thenReturn(
115+
Response(
116+
data = null,
117+
errors =
118+
listOf(
119+
UpstreamApiError(
120+
causedBy = UpstreamApi.PROBATION_OFFENDER_SEARCH,
121+
type = UpstreamApiError.Type.ENTITY_NOT_FOUND,
122+
),
123+
),
124+
),
125+
)
126+
val response =
127+
getTransactionsForPersonService.execute(
128+
prisonId,
129+
hmppsId,
130+
accountCode,
131+
startDate,
132+
endDate,
133+
filters,
134+
)
135+
response
136+
.hasErrorCausedBy(
137+
causedBy = UpstreamApi.PROBATION_OFFENDER_SEARCH,
138+
type = UpstreamApiError.Type.ENTITY_NOT_FOUND,
139+
).shouldBe(true)
140+
}
141+
142+
it("records upstream API errors when hmppsID is invalid") {
143+
whenever(getPersonService.getNomisNumber(hmppsId = hmppsId)).thenReturn(
144+
Response(
145+
data = null,
146+
errors =
147+
listOf(
148+
UpstreamApiError(
149+
type = UpstreamApiError.Type.BAD_REQUEST,
150+
causedBy = UpstreamApi.NOMIS,
151+
),
152+
),
153+
),
154+
)
155+
val response =
156+
getTransactionsForPersonService.execute(
157+
prisonId,
158+
hmppsId,
159+
accountCode,
160+
startDate,
161+
endDate,
162+
filters,
163+
)
164+
response
165+
.hasErrorCausedBy(
166+
causedBy = UpstreamApi.NOMIS,
167+
type = UpstreamApiError.Type.BAD_REQUEST,
168+
).shouldBe(true)
169+
}
170+
171+
it("records upstream API errors when getTransactionsForPerson returns errors") {
172+
whenever(
173+
nomisGateway.getTransactionsForPerson(
174+
prisonId,
175+
nomisNumber,
176+
accountCode,
177+
startDate,
178+
endDate,
179+
),
180+
).thenReturn(
181+
Response(
182+
data = null,
183+
errors =
184+
listOf(
185+
UpstreamApiError(
186+
type = UpstreamApiError.Type.BAD_REQUEST,
187+
causedBy = UpstreamApi.NOMIS,
188+
),
189+
),
190+
),
191+
)
192+
val response =
193+
getTransactionsForPersonService.execute(
194+
prisonId,
195+
hmppsId,
196+
accountCode,
197+
startDate,
198+
endDate,
199+
filters,
200+
)
201+
response
202+
.hasErrorCausedBy(
203+
causedBy = UpstreamApi.NOMIS,
204+
type = UpstreamApiError.Type.BAD_REQUEST,
205+
).shouldBe(true)
206+
}
207+
208+
// break this
209+
it("returns null when transactions are requested from an unapproved prison") {
210+
val wrongPrisonId = "XYZ"
211+
val result =
212+
getTransactionsForPersonService.execute(
213+
wrongPrisonId,
214+
hmppsId,
215+
accountCode,
216+
startDate,
217+
endDate,
218+
ConsumerFilters(prisons = listOf("ABC")),
219+
)
220+
221+
result.data.shouldBe(null)
222+
result.errors.shouldBe(listOf(UpstreamApiError(UpstreamApi.NOMIS, UpstreamApiError.Type.ENTITY_NOT_FOUND, "Not found")))
223+
}
224+
225+
it("returns transactions when requested from an approved prison") {
226+
val result =
227+
getTransactionsForPersonService.execute(
228+
prisonId,
229+
hmppsId,
230+
accountCode,
231+
startDate,
232+
endDate,
233+
ConsumerFilters(prisons = listOf(prisonId)),
234+
)
235+
236+
result.data.shouldBe(exampleTransactions)
237+
}
238+
})

0 commit comments

Comments
 (0)