Skip to content

Commit c234af5

Browse files
committed
WIP: Functionality of GetBalancesforPersonService passing all tests. Potential refactor needed to get balances into its own data class.
1 parent aff8af6 commit c234af5

File tree

3 files changed

+88
-96
lines changed

3 files changed

+88
-96
lines changed

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

+3-2
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,9 @@ package uk.gov.justice.digital.hmpps.hmppsintegrationapi.models.hmpps
22

33
data class AccountBalance(
44
val accountCode: String? = null,
5-
val amount: Int? = null)
5+
val amount: Int? = null,
6+
)
67

78
data class Balances(
8-
val accountBalances: Array<AccountBalance> = emptyArray()
9+
val accountBalances: Array<AccountBalance> = emptyArray(),
910
)

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

+23-20
Original file line numberDiff line numberDiff line change
@@ -3,39 +3,42 @@ package uk.gov.justice.digital.hmpps.hmppsintegrationapi.services
33
import org.springframework.beans.factory.annotation.Autowired
44
import org.springframework.stereotype.Service
55
import uk.gov.justice.digital.hmpps.hmppsintegrationapi.gateways.NomisGateway
6-
import uk.gov.justice.digital.hmpps.hmppsintegrationapi.models.hmpps.AccountBalance
7-
import uk.gov.justice.digital.hmpps.hmppsintegrationapi.models.hmpps.Balances
8-
import uk.gov.justice.digital.hmpps.hmppsintegrationapi.models.hmpps.Offence
96
import uk.gov.justice.digital.hmpps.hmppsintegrationapi.models.hmpps.Response
107

118
@Service
129
class GetBalancesForPersonService(
1310
@Autowired val nomisGateway: NomisGateway,
14-
@Autowired val getPersonService: GetPersonService
11+
@Autowired val getPersonService: GetPersonService,
1512
) {
16-
fun execute(prisonId: String, hmppsId: String): Response<Map<String, Any>> {
13+
fun execute(
14+
prisonId: String,
15+
hmppsId: String,
16+
): Response<Map<String, Any>> {
1717
val personResponse = getPersonService.execute(hmppsId = hmppsId)
1818
val nomisNumber = personResponse.data?.identifiers?.nomisNumber
1919

2020
val nomisAccounts = nomisGateway.getAccountsForPerson(prisonId, nomisNumber)
21+
val nomisSpends = nomisAccounts.data?.spends ?: 0
22+
val nomisSavings = nomisAccounts.data?.savings ?: 0
23+
val nomisCash = nomisAccounts.data?.cash ?: 0
2124

22-
val balances = Balances(
23-
accountBalances = arrayOf(
24-
AccountBalance(accountCode = "spends", amount = nomisAccounts.data?.spends ?: 0),
25-
AccountBalance(accountCode = "saving", amount = nomisAccounts.data?.savings ?: 0),
26-
AccountBalance(accountCode = "cash", amount = nomisAccounts.data?.cash ?: 0)
27-
)
28-
)
25+
val balances = arrayOf(mapOf("accountCode:" to "spends", "amount:" to nomisSpends), mapOf("accountCode:" to "savings", "amount:" to nomisSavings), mapOf("accountCode:" to "cash", "amount:" to nomisCash))
26+
27+
// Balances(
28+
// accountBalances = arrayOf(
29+
// AccountBalance(accountCode = "spends", amount = nomisAccounts.data?.spends ?: 0),
30+
// AccountBalance(accountCode = "saving", amount = nomisAccounts.data?.savings ?: 0),
31+
// AccountBalance(accountCode = "cash", amount = nomisAccounts.data?.cash ?: 0)
32+
// )
33+
// )
2934

3035
return Response(
31-
data = mapOf(
32-
"prisonId" to prisonId,
33-
"prisonerId" to (nomisNumber ?: ""),
34-
"balances" to balances
35-
)
36+
data =
37+
mapOf(
38+
"prisonId" to prisonId,
39+
"prisonerId" to (nomisNumber ?: ""),
40+
"balances" to balances,
41+
),
3642
)
3743
}
3844
}
39-
40-
41-
Original file line numberDiff line numberDiff line change
@@ -1,107 +1,96 @@
11
package uk.gov.justice.digital.hmpps.hmppsintegrationapi.services
22

3-
import uk.gov.justice.digital.hmpps.hmppsintegrationapi.models.hmpps.AccountBalance
4-
import uk.gov.justice.digital.hmpps.hmppsintegrationapi.models.hmpps.Balances
5-
import uk.gov.justice.digital.hmpps.hmppsintegrationapi.models.nomis.NomisAccounts
6-
73
import io.kotest.core.spec.style.DescribeSpec
84
import io.kotest.matchers.shouldBe
95
import org.mockito.Mockito
106
import org.mockito.internal.verification.VerificationModeFactory
117
import org.mockito.kotlin.verify
128
import org.mockito.kotlin.whenever
139
import org.springframework.boot.test.context.ConfigDataApplicationContextInitializer
14-
import org.springframework.boot.test.context.SpringBootTest
1510
import org.springframework.test.context.ContextConfiguration
1611
import org.springframework.test.context.bean.override.mockito.MockitoBean
1712
import uk.gov.justice.digital.hmpps.hmppsintegrationapi.gateways.NomisGateway
1813
import uk.gov.justice.digital.hmpps.hmppsintegrationapi.models.hmpps.Identifiers
1914
import uk.gov.justice.digital.hmpps.hmppsintegrationapi.models.hmpps.Person
2015
import uk.gov.justice.digital.hmpps.hmppsintegrationapi.models.hmpps.Response
16+
import uk.gov.justice.digital.hmpps.hmppsintegrationapi.models.nomis.NomisAccounts
2117

2218
@ContextConfiguration(
2319
initializers = [ConfigDataApplicationContextInitializer::class],
2420
classes = [GetBalancesForPersonService::class],
2521
)
26-
2722
internal class GetBalancesForPersonServiceTest(
2823
@MockitoBean val nomisGateway: NomisGateway,
2924
@MockitoBean val getPersonService: GetPersonService,
3025
private val getBalancesForPersonService: GetBalancesForPersonService,
3126
) : DescribeSpec(
32-
{
33-
val hmppsId = "1234/56789B"
34-
val prisonerId = "Z99999ZZ"
35-
val prisonId = "ABC"
36-
37-
val personFromPrisonOffenderSearch =
38-
Person(
39-
firstName = "Chandler",
40-
lastName = "ProbationBing",
41-
identifiers = Identifiers(nomisNumber = prisonerId),
42-
)
43-
44-
beforeEach {
45-
Mockito.reset(getPersonService)
46-
Mockito.reset(nomisGateway)
47-
48-
require(hmppsId.matches(Regex("^[0-9]+/[0-9A-Za-z]+$"))) {
49-
"Invalid Hmpps Id format: $hmppsId"
27+
{
28+
val hmppsId = "1234/56789B"
29+
val prisonerId = "Z99999ZZ"
30+
val prisonId = "ABC"
31+
val nomisSpends = 100
32+
val nomisSavings = 100
33+
val nomisCash = 100
34+
35+
val personFromPrisonOffenderSearch =
36+
Person(
37+
firstName = "Chandler",
38+
lastName = "ProbationBing",
39+
identifiers = Identifiers(nomisNumber = prisonerId),
40+
)
41+
42+
beforeEach {
43+
Mockito.reset(getPersonService)
44+
Mockito.reset(nomisGateway)
45+
46+
require(hmppsId.matches(Regex("^[0-9]+/[0-9A-Za-z]+$"))) {
47+
"Invalid Hmpps Id format: $hmppsId"
48+
}
49+
50+
whenever(getPersonService.execute(hmppsId = hmppsId)).thenReturn(
51+
Response(
52+
data = personFromPrisonOffenderSearch,
53+
),
54+
)
55+
56+
whenever(nomisGateway.getAccountsForPerson(prisonId, prisonerId)).thenReturn(
57+
Response(
58+
data =
59+
NomisAccounts(spends = nomisSpends, savings = nomisSavings, cash = nomisCash),
60+
),
61+
)
5062
}
63+
// val expectedNomisAccounts = nomisGateway.getAccountsForPerson(prisonId, prisonerId).data
64+
// val balance = Balances(
65+
// accountBalances = arrayOf(
66+
// AccountBalance(accountCode = "spends", amount = nomisSpends),
67+
// AccountBalance(accountCode = "saving", amount = nomisSavings),
68+
// AccountBalance(accountCode = "cash", amount = nomisCash),
69+
// )
70+
// )
71+
val balance = arrayOf(mapOf("accountCode:" to "spends", "amount:" to nomisSpends), mapOf("accountCode:" to "savings", "amount:" to nomisSavings), mapOf("accountCode:" to "cash", "amount:" to nomisCash))
5172

52-
whenever(getPersonService.execute(hmppsId = hmppsId)).thenReturn(
53-
Response(
54-
data = personFromPrisonOffenderSearch,
55-
),
56-
)
57-
58-
whenever(nomisGateway.getAccountsForPerson(prisonId, prisonerId)).thenReturn(
59-
Response(
60-
data =
61-
NomisAccounts(spends = 100, savings = 100, cash = 100)
62-
),
63-
)
64-
65-
}
66-
val expectedNomisAccounts = nomisGateway.getAccountsForPerson(prisonId, prisonerId).data
67-
val balance = Balances(
68-
accountBalances = arrayOf(
69-
AccountBalance(accountCode = "spends", amount = expectedNomisAccounts?.spends),
70-
AccountBalance(accountCode = "saving", amount = expectedNomisAccounts?.savings),
71-
AccountBalance(accountCode = "cash", amount = expectedNomisAccounts?.cash)
72-
)
73-
)
74-
75-
it("gets a person using a Hmpps ID") {
76-
getBalancesForPersonService.execute(prisonId, hmppsId)
77-
78-
verify(getPersonService, VerificationModeFactory.times(1)).execute(hmppsId = hmppsId)
79-
}
80-
81-
82-
it("gets accounts from NOMIS using a prisoner number") {
83-
getBalancesForPersonService.execute(prisonId, hmppsId)
84-
85-
verify(nomisGateway, VerificationModeFactory.times(1)).getAccountsForPerson(prisonId = prisonId, nomisNumber = prisonerId)
86-
}
73+
it("gets a person using a Hmpps ID") {
74+
getBalancesForPersonService.execute(prisonId, hmppsId)
8775

88-
it("Returns a persons account balances given a hmppsId") {
89-
whenever(getPersonService.execute(hmppsId = hmppsId)).thenReturn(
90-
Response(
91-
data = personFromPrisonOffenderSearch,
92-
),
93-
)
76+
verify(getPersonService, VerificationModeFactory.times(1)).execute(hmppsId = hmppsId)
77+
}
9478

95-
val result = getBalancesForPersonService.execute(prisonId, hmppsId)
79+
it("gets accounts from NOMIS using a prisoner number") {
80+
getBalancesForPersonService.execute(prisonId, hmppsId)
9681

97-
result.shouldBe(
98-
Response(data = mapOf("prisonId" to prisonId, "prisonerId" to prisonerId, "balances" to balance)),
99-
)
100-
}
82+
verify(nomisGateway, VerificationModeFactory.times(1)).getAccountsForPerson(prisonId = prisonId, nomisNumber = prisonerId)
83+
}
10184

85+
it("Returns a persons account balances given a hmppsId") {
86+
val result = getBalancesForPersonService.execute(prisonId, hmppsId)
10287

103-
}
104-
)
88+
result.data.shouldBe(
89+
mapOf("prisonId" to prisonId, "prisonerId" to prisonerId, "balances" to balance),
90+
)
91+
}
92+
},
93+
)
10594

10695
// describe("when an upstream API returns an error when looking up a person from a Hmpps ID") {
10796
// beforeEach {
@@ -170,5 +159,4 @@ internal class GetBalancesForPersonServiceTest(
170159
// response.errors.shouldHaveSize(2)
171160
// }
172161
// },
173-
//)
174-
162+
// )

0 commit comments

Comments
 (0)