Skip to content

Commit 6591483

Browse files
Reverting
1 parent ad1fbea commit 6591483

File tree

2 files changed

+72
-63
lines changed

2 files changed

+72
-63
lines changed

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

+11-10
Original file line numberDiff line numberDiff line change
@@ -10,26 +10,27 @@ import uk.gov.justice.digital.hmpps.hmppsintegrationapi.models.hmpps.Response
1010
@Service
1111
class GetLicenceConditionService(
1212
@Autowired val createAndVaryLicenceGateway: CreateAndVaryLicenceGateway,
13-
// @Autowired val getPersonService: GetPersonService,
13+
@Autowired val getPersonService: GetPersonService,
1414
) {
1515
fun execute(hmppsId: String): Response<PersonLicences> {
16-
// val personResponse = getPersonService.execute(hmppsId)
17-
// val crn = personResponse.data?.identifiers?.deliusCrn
16+
val personResponse = getPersonService.execute(hmppsId)
17+
val crn = personResponse.data?.identifiers?.deliusCrn
1818

1919
var licences: Response<List<Licence>> = Response(data = emptyList())
2020
var personLicences = PersonLicences(hmppsId)
21-
22-
licences = createAndVaryLicenceGateway.getLicenceSummaries(id = hmppsId)
23-
licences.data.forEach { summary ->
24-
summary.conditions = summary.id.toIntOrNull()?.let { licenceId ->
25-
createAndVaryLicenceGateway.getLicenceConditions(licenceId).data
26-
} ?: emptyList()
21+
if (crn != null) {
22+
licences = createAndVaryLicenceGateway.getLicenceSummaries(id = crn)
23+
licences.data.forEach { summary ->
24+
summary.conditions = summary.id.toIntOrNull()?.let { licenceId ->
25+
createAndVaryLicenceGateway.getLicenceConditions(licenceId).data
26+
} ?: emptyList()
27+
}
2728
personLicences = PersonLicences(hmppsId, licences.data.firstOrNull()?.offenderNumber, licences.data)
2829
}
2930

3031
return Response(
3132
data = personLicences,
32-
errors = licences.errors,
33+
errors = personResponse.errors + licences.errors,
3334
)
3435
}
3536
}
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,11 @@
11
package uk.gov.justice.digital.hmpps.hmppsintegrationapi.services
22

33
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
49
import org.springframework.boot.test.context.ConfigDataApplicationContextInitializer
510
import org.springframework.boot.test.mock.mockito.MockBean
611
import org.springframework.test.context.ContextConfiguration
@@ -9,14 +14,17 @@ import uk.gov.justice.digital.hmpps.hmppsintegrationapi.models.hmpps.Identifiers
914
import uk.gov.justice.digital.hmpps.hmppsintegrationapi.models.hmpps.Licence
1015
import uk.gov.justice.digital.hmpps.hmppsintegrationapi.models.hmpps.LicenceCondition
1116
import uk.gov.justice.digital.hmpps.hmppsintegrationapi.models.hmpps.Person
17+
import uk.gov.justice.digital.hmpps.hmppsintegrationapi.models.hmpps.Response
18+
import uk.gov.justice.digital.hmpps.hmppsintegrationapi.models.hmpps.UpstreamApi
19+
import uk.gov.justice.digital.hmpps.hmppsintegrationapi.models.hmpps.UpstreamApiError
1220

1321
@ContextConfiguration(
1422
initializers = [ConfigDataApplicationContextInitializer::class],
1523
classes = [GetLicenceConditionService::class],
1624
)
1725
internal class GetLicenceConditionServiceTest(
1826
@MockBean val createAndVaryLicenceGateway: CreateAndVaryLicenceGateway,
19-
// @MockBean val getPersonService: GetPersonService,
27+
@MockBean val getPersonService: GetPersonService,
2028
private val getLicenceConditionService: GetLicenceConditionService,
2129
) : DescribeSpec(
2230
{
@@ -26,60 +34,60 @@ internal class GetLicenceConditionServiceTest(
2634
val licences = listOf(Licence(id = "12345"))
2735
val conditions = listOf(LicenceCondition(condition = "MockCondition", category = "AP"))
2836

29-
// beforeEach {
30-
// // Mockito.reset(getPersonService)
31-
// Mockito.reset(createAndVaryLicenceGateway)
32-
//
33-
// // whenever(getPersonService.execute(hmppsId = hmppsId)).thenReturn(Response(person))
34-
// whenever(createAndVaryLicenceGateway.getLicenceSummaries(id = crn)).thenReturn(Response(licences))
35-
// whenever(createAndVaryLicenceGateway.getLicenceConditions(id = 12345)).thenReturn(Response(conditions))
36-
// }
37+
beforeEach {
38+
Mockito.reset(getPersonService)
39+
Mockito.reset(createAndVaryLicenceGateway)
3740

38-
// it("performs a search according to hmpps Id") {
39-
// getLicenceConditionService.execute(hmppsId)
40-
// verify(getPersonService, VerificationModeFactory.times(1)).execute(hmppsId = hmppsId)
41-
// }
41+
whenever(getPersonService.execute(hmppsId = hmppsId)).thenReturn(Response(person))
42+
whenever(createAndVaryLicenceGateway.getLicenceSummaries(id = crn)).thenReturn(Response(licences))
43+
whenever(createAndVaryLicenceGateway.getLicenceConditions(id = 12345)).thenReturn(Response(conditions))
44+
}
4245

43-
// it("should return a list of errors if person not found") {
44-
// // whenever(getPersonService.execute(hmppsId = "notfound")).thenReturn(
45-
// // Response(
46-
// // data = null,
47-
// // errors =
48-
// // listOf(
49-
// // UpstreamApiError(
50-
// // causedBy = UpstreamApi.CVL,
51-
// // type = UpstreamApiError.Type.ENTITY_NOT_FOUND,
52-
// // ),
53-
// // ),
54-
// // ),
55-
// // )
56-
// val result = getLicenceConditionService.execute("notfound")
57-
// result.data.licences.shouldBe(emptyList())
58-
// result.errors.first().type.shouldBe(UpstreamApiError.Type.ENTITY_NOT_FOUND)
59-
// }
46+
it("performs a search according to hmpps Id") {
47+
getLicenceConditionService.execute(hmppsId)
48+
verify(getPersonService, VerificationModeFactory.times(1)).execute(hmppsId = hmppsId)
49+
}
6050

61-
// it("should return a list of errors if create and vary licence gateway service returns error") {
62-
// whenever(createAndVaryLicenceGateway.getLicenceSummaries(id = crn)).thenReturn(
63-
// Response(
64-
// data = emptyList(),
65-
// errors =
66-
// listOf(
67-
// UpstreamApiError(
68-
// causedBy = UpstreamApi.CVL,
69-
// type = UpstreamApiError.Type.ENTITY_NOT_FOUND,
70-
// ),
71-
// ),
72-
// ),
73-
// )
74-
// val result = getLicenceConditionService.execute(hmppsId = hmppsId)
75-
// result.data.licences.shouldBe(emptyList())
76-
// result.errors.first().type.shouldBe(UpstreamApiError.Type.ENTITY_NOT_FOUND)
77-
// }
78-
//
79-
// it("should return licence condition from gateway") {
80-
// val result = getLicenceConditionService.execute(hmppsId = hmppsId)
81-
// result.data.licences.first().conditions.first().condition.shouldBe("MockCondition")
82-
// result.errors.count().shouldBe(0)
83-
// }
51+
it("should return a list of errors if person not found") {
52+
whenever(getPersonService.execute(hmppsId = "notfound")).thenReturn(
53+
Response(
54+
data = null,
55+
errors =
56+
listOf(
57+
UpstreamApiError(
58+
causedBy = UpstreamApi.CVL,
59+
type = UpstreamApiError.Type.ENTITY_NOT_FOUND,
60+
),
61+
),
62+
),
63+
)
64+
val result = getLicenceConditionService.execute("notfound")
65+
result.data.licences.shouldBe(emptyList())
66+
result.errors.first().type.shouldBe(UpstreamApiError.Type.ENTITY_NOT_FOUND)
67+
}
68+
69+
it("should return a list of errors if create and vary licence gateway service returns error") {
70+
whenever(createAndVaryLicenceGateway.getLicenceSummaries(id = crn)).thenReturn(
71+
Response(
72+
data = emptyList(),
73+
errors =
74+
listOf(
75+
UpstreamApiError(
76+
causedBy = UpstreamApi.CVL,
77+
type = UpstreamApiError.Type.ENTITY_NOT_FOUND,
78+
),
79+
),
80+
),
81+
)
82+
val result = getLicenceConditionService.execute(hmppsId = hmppsId)
83+
result.data.licences.shouldBe(emptyList())
84+
result.errors.first().type.shouldBe(UpstreamApiError.Type.ENTITY_NOT_FOUND)
85+
}
86+
87+
it("should return licence condition from gateway") {
88+
val result = getLicenceConditionService.execute(hmppsId = hmppsId)
89+
result.data.licences.first().conditions.first().condition.shouldBe("MockCondition")
90+
result.errors.count().shouldBe(0)
91+
}
8492
},
8593
)

0 commit comments

Comments
 (0)