From 8e740de58c36a46d69852a573833aaaa12341ea2 Mon Sep 17 00:00:00 2001 From: Chiara Date: Wed, 22 May 2024 10:55:56 +0100 Subject: [PATCH 1/6] Editing the type of ID we need to pull upstream from String to Int --- docker-compose.yml | 2 +- .../gateways/CreateAndVaryLicenceGateway.kt | 2 +- .../services/GetLicenceConditionService.kt | 9 +++++++-- .../GetLicenceConditionsTests.kt | 2 +- .../CreateAndVaryLicenceApiMockServer.kt | 2 +- .../services/GetLicenceConditionServiceTest.kt | 14 +++++++------- 6 files changed, 18 insertions(+), 13 deletions(-) diff --git a/docker-compose.yml b/docker-compose.yml index 9a288d7f6..612c3888d 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -112,7 +112,7 @@ services: dockerfile: Dockerfile.setup-create-and-vary-a-licence-api container_name: create-and-vary-licence-api healthcheck: - test: 'wget --header="Authorization: Bearer abc" http://0.0.0.0:4010/public/licences/id/abc -O /dev/null' + test: 'wget --header="Authorization: Bearer abc" http://0.0.0.0:4010/public/licences/id/123 -O /dev/null' ports: - '4070:4010' diff --git a/src/main/kotlin/uk/gov/justice/digital/hmpps/hmppsintegrationapi/gateways/CreateAndVaryLicenceGateway.kt b/src/main/kotlin/uk/gov/justice/digital/hmpps/hmppsintegrationapi/gateways/CreateAndVaryLicenceGateway.kt index 7404f4411..834425dbc 100644 --- a/src/main/kotlin/uk/gov/justice/digital/hmpps/hmppsintegrationapi/gateways/CreateAndVaryLicenceGateway.kt +++ b/src/main/kotlin/uk/gov/justice/digital/hmpps/hmppsintegrationapi/gateways/CreateAndVaryLicenceGateway.kt @@ -45,7 +45,7 @@ class CreateAndVaryLicenceGateway( } } - fun getLicenceConditions(id: String): Response> { + fun getLicenceConditions(id: Int): Response> { val result = webClient.request( HttpMethod.GET, diff --git a/src/main/kotlin/uk/gov/justice/digital/hmpps/hmppsintegrationapi/services/GetLicenceConditionService.kt b/src/main/kotlin/uk/gov/justice/digital/hmpps/hmppsintegrationapi/services/GetLicenceConditionService.kt index 536ce2a19..c2cf6e41b 100644 --- a/src/main/kotlin/uk/gov/justice/digital/hmpps/hmppsintegrationapi/services/GetLicenceConditionService.kt +++ b/src/main/kotlin/uk/gov/justice/digital/hmpps/hmppsintegrationapi/services/GetLicenceConditionService.kt @@ -21,8 +21,13 @@ class GetLicenceConditionService( if (crn != null) { licences = createAndVaryLicenceGateway.getLicenceSummaries(id = crn) licences.data.forEach { - val conditions = createAndVaryLicenceGateway.getLicenceConditions(it.id) - it.conditions = conditions.data + val licenceId = it.id.toIntOrNull() + if (licenceId != null) { + val conditions = createAndVaryLicenceGateway.getLicenceConditions(licenceId) + it.conditions = conditions.data + } else { + it.conditions = emptyList() + } } personLicences = PersonLicences(hmppsId, licences.data.firstOrNull()?.offenderNumber, licences.data) } diff --git a/src/test/kotlin/uk/gov/justice/digital/hmpps/hmppsintegrationapi/gateways/createAndVaryLicence/GetLicenceConditionsTests.kt b/src/test/kotlin/uk/gov/justice/digital/hmpps/hmppsintegrationapi/gateways/createAndVaryLicence/GetLicenceConditionsTests.kt index 8bf4dc04a..250cfc67b 100644 --- a/src/test/kotlin/uk/gov/justice/digital/hmpps/hmppsintegrationapi/gateways/createAndVaryLicence/GetLicenceConditionsTests.kt +++ b/src/test/kotlin/uk/gov/justice/digital/hmpps/hmppsintegrationapi/gateways/createAndVaryLicence/GetLicenceConditionsTests.kt @@ -29,7 +29,7 @@ class GetLicenceConditionsTests( ) : DescribeSpec( { val createAndVaryLicenceApiMockServer = CreateAndVaryLicenceApiMockServer() - val conditionId = "X777776" + val conditionId = 12345 beforeEach { createAndVaryLicenceApiMockServer.start() diff --git a/src/test/kotlin/uk/gov/justice/digital/hmpps/hmppsintegrationapi/mockservers/CreateAndVaryLicenceApiMockServer.kt b/src/test/kotlin/uk/gov/justice/digital/hmpps/hmppsintegrationapi/mockservers/CreateAndVaryLicenceApiMockServer.kt index 2e8ad4718..620e1ad31 100644 --- a/src/test/kotlin/uk/gov/justice/digital/hmpps/hmppsintegrationapi/mockservers/CreateAndVaryLicenceApiMockServer.kt +++ b/src/test/kotlin/uk/gov/justice/digital/hmpps/hmppsintegrationapi/mockservers/CreateAndVaryLicenceApiMockServer.kt @@ -29,7 +29,7 @@ class CreateAndVaryLicenceApiMockServer : WireMockServer(WIREMOCK_PORT) { } fun stubGetLicenceConditions( - id: String, + id: Int, body: String, status: HttpStatus = HttpStatus.OK, ) { diff --git a/src/test/kotlin/uk/gov/justice/digital/hmpps/hmppsintegrationapi/services/GetLicenceConditionServiceTest.kt b/src/test/kotlin/uk/gov/justice/digital/hmpps/hmppsintegrationapi/services/GetLicenceConditionServiceTest.kt index eaeb569e5..4e5d48831 100644 --- a/src/test/kotlin/uk/gov/justice/digital/hmpps/hmppsintegrationapi/services/GetLicenceConditionServiceTest.kt +++ b/src/test/kotlin/uk/gov/justice/digital/hmpps/hmppsintegrationapi/services/GetLicenceConditionServiceTest.kt @@ -25,13 +25,13 @@ import uk.gov.justice.digital.hmpps.hmppsintegrationapi.models.hmpps.UpstreamApi internal class GetLicenceConditionServiceTest( @MockBean val createAndVaryLicenceGateway: CreateAndVaryLicenceGateway, @MockBean val getPersonService: GetPersonService, - private val getLicenceCondtionService: GetLicenceConditionService, + private val getLicenceConditionService: GetLicenceConditionService, ) : DescribeSpec( { val hmppsId = "1234/56789B" val crn = "Z99999ZZ" val person = Person(firstName = "Qui-gon", lastName = "Jin", identifiers = Identifiers(deliusCrn = crn)) - val licences = listOf(Licence(id = "MockLicenceId")) + val licences = listOf(Licence(id = "12345")) val conditions = listOf(LicenceCondition(condition = "MockCondition", category = "AP")) beforeEach { @@ -40,11 +40,11 @@ internal class GetLicenceConditionServiceTest( whenever(getPersonService.execute(hmppsId = hmppsId)).thenReturn(Response(person)) whenever(createAndVaryLicenceGateway.getLicenceSummaries(id = crn)).thenReturn(Response(licences)) - whenever(createAndVaryLicenceGateway.getLicenceConditions(id = "MockLicenceId")).thenReturn(Response(conditions)) + whenever(createAndVaryLicenceGateway.getLicenceConditions(id = 12345)).thenReturn(Response(conditions)) } it("performs a search according to hmpps Id") { - getLicenceCondtionService.execute(hmppsId) + getLicenceConditionService.execute(hmppsId) verify(getPersonService, VerificationModeFactory.times(1)).execute(hmppsId = hmppsId) } @@ -61,7 +61,7 @@ internal class GetLicenceConditionServiceTest( ), ), ) - val result = getLicenceCondtionService.execute("notfound") + val result = getLicenceConditionService.execute("notfound") result.data.licences.shouldBe(emptyList()) result.errors.first().type.shouldBe(UpstreamApiError.Type.ENTITY_NOT_FOUND) } @@ -79,13 +79,13 @@ internal class GetLicenceConditionServiceTest( ), ), ) - val result = getLicenceCondtionService.execute(hmppsId = hmppsId) + val result = getLicenceConditionService.execute(hmppsId = hmppsId) result.data.licences.shouldBe(emptyList()) result.errors.first().type.shouldBe(UpstreamApiError.Type.ENTITY_NOT_FOUND) } it("should return licence condition from gateway") { - val result = getLicenceCondtionService.execute(hmppsId = hmppsId) + val result = getLicenceConditionService.execute(hmppsId = hmppsId) result.data.licences.first().conditions.first().condition.shouldBe("MockCondition") result.errors.count().shouldBe(0) } From 83d52a16fbe64792e6a8ad80fc968e9c9fcbf2ba Mon Sep 17 00:00:00 2001 From: Chiara Date: Wed, 22 May 2024 11:36:53 +0100 Subject: [PATCH 2/6] Refactor --- .../services/GetLicenceConditionService.kt | 14 +++++--------- 1 file changed, 5 insertions(+), 9 deletions(-) diff --git a/src/main/kotlin/uk/gov/justice/digital/hmpps/hmppsintegrationapi/services/GetLicenceConditionService.kt b/src/main/kotlin/uk/gov/justice/digital/hmpps/hmppsintegrationapi/services/GetLicenceConditionService.kt index c2cf6e41b..b6711d26c 100644 --- a/src/main/kotlin/uk/gov/justice/digital/hmpps/hmppsintegrationapi/services/GetLicenceConditionService.kt +++ b/src/main/kotlin/uk/gov/justice/digital/hmpps/hmppsintegrationapi/services/GetLicenceConditionService.kt @@ -13,21 +13,17 @@ class GetLicenceConditionService( @Autowired val getPersonService: GetPersonService, ) { fun execute(hmppsId: String): Response { - val personResponse = getPersonService.execute(hmppsId = hmppsId) + val personResponse = getPersonService.execute(hmppsId) val crn = personResponse.data?.identifiers?.deliusCrn var licences: Response> = Response(data = emptyList()) var personLicences = PersonLicences(hmppsId) if (crn != null) { licences = createAndVaryLicenceGateway.getLicenceSummaries(id = crn) - licences.data.forEach { - val licenceId = it.id.toIntOrNull() - if (licenceId != null) { - val conditions = createAndVaryLicenceGateway.getLicenceConditions(licenceId) - it.conditions = conditions.data - } else { - it.conditions = emptyList() - } + licences.data.forEach { summary -> + summary.conditions = summary.id.toIntOrNull()?.let { licenceId -> + createAndVaryLicenceGateway.getLicenceConditions(licenceId).data + } ?: emptyList() } personLicences = PersonLicences(hmppsId, licences.data.firstOrNull()?.offenderNumber, licences.data) } From ad1fbead250e2c00fad4370f8ed5da8d0289f600 Mon Sep 17 00:00:00 2001 From: Chiara Date: Thu, 23 May 2024 11:53:07 +0100 Subject: [PATCH 3/6] Testing licences WIP --- .../services/GetLicenceConditionService.kt | 21 ++-- .../GetLicenceConditionServiceTest.kt | 114 ++++++++---------- 2 files changed, 63 insertions(+), 72 deletions(-) diff --git a/src/main/kotlin/uk/gov/justice/digital/hmpps/hmppsintegrationapi/services/GetLicenceConditionService.kt b/src/main/kotlin/uk/gov/justice/digital/hmpps/hmppsintegrationapi/services/GetLicenceConditionService.kt index b6711d26c..9b75efda6 100644 --- a/src/main/kotlin/uk/gov/justice/digital/hmpps/hmppsintegrationapi/services/GetLicenceConditionService.kt +++ b/src/main/kotlin/uk/gov/justice/digital/hmpps/hmppsintegrationapi/services/GetLicenceConditionService.kt @@ -10,27 +10,26 @@ import uk.gov.justice.digital.hmpps.hmppsintegrationapi.models.hmpps.Response @Service class GetLicenceConditionService( @Autowired val createAndVaryLicenceGateway: CreateAndVaryLicenceGateway, - @Autowired val getPersonService: GetPersonService, +// @Autowired val getPersonService: GetPersonService, ) { fun execute(hmppsId: String): Response { - val personResponse = getPersonService.execute(hmppsId) - val crn = personResponse.data?.identifiers?.deliusCrn +// val personResponse = getPersonService.execute(hmppsId) +// val crn = personResponse.data?.identifiers?.deliusCrn var licences: Response> = Response(data = emptyList()) var personLicences = PersonLicences(hmppsId) - if (crn != null) { - licences = createAndVaryLicenceGateway.getLicenceSummaries(id = crn) - licences.data.forEach { summary -> - summary.conditions = summary.id.toIntOrNull()?.let { licenceId -> - createAndVaryLicenceGateway.getLicenceConditions(licenceId).data - } ?: emptyList() - } + + licences = createAndVaryLicenceGateway.getLicenceSummaries(id = hmppsId) + licences.data.forEach { summary -> + summary.conditions = summary.id.toIntOrNull()?.let { licenceId -> + createAndVaryLicenceGateway.getLicenceConditions(licenceId).data + } ?: emptyList() personLicences = PersonLicences(hmppsId, licences.data.firstOrNull()?.offenderNumber, licences.data) } return Response( data = personLicences, - errors = personResponse.errors + licences.errors, + errors = licences.errors, ) } } diff --git a/src/test/kotlin/uk/gov/justice/digital/hmpps/hmppsintegrationapi/services/GetLicenceConditionServiceTest.kt b/src/test/kotlin/uk/gov/justice/digital/hmpps/hmppsintegrationapi/services/GetLicenceConditionServiceTest.kt index 4e5d48831..17a5de237 100644 --- a/src/test/kotlin/uk/gov/justice/digital/hmpps/hmppsintegrationapi/services/GetLicenceConditionServiceTest.kt +++ b/src/test/kotlin/uk/gov/justice/digital/hmpps/hmppsintegrationapi/services/GetLicenceConditionServiceTest.kt @@ -1,11 +1,6 @@ package uk.gov.justice.digital.hmpps.hmppsintegrationapi.services import io.kotest.core.spec.style.DescribeSpec -import io.kotest.matchers.shouldBe -import org.mockito.Mockito -import org.mockito.internal.verification.VerificationModeFactory -import org.mockito.kotlin.verify -import org.mockito.kotlin.whenever import org.springframework.boot.test.context.ConfigDataApplicationContextInitializer import org.springframework.boot.test.mock.mockito.MockBean import org.springframework.test.context.ContextConfiguration @@ -14,9 +9,6 @@ import uk.gov.justice.digital.hmpps.hmppsintegrationapi.models.hmpps.Identifiers import uk.gov.justice.digital.hmpps.hmppsintegrationapi.models.hmpps.Licence import uk.gov.justice.digital.hmpps.hmppsintegrationapi.models.hmpps.LicenceCondition import uk.gov.justice.digital.hmpps.hmppsintegrationapi.models.hmpps.Person -import uk.gov.justice.digital.hmpps.hmppsintegrationapi.models.hmpps.Response -import uk.gov.justice.digital.hmpps.hmppsintegrationapi.models.hmpps.UpstreamApi -import uk.gov.justice.digital.hmpps.hmppsintegrationapi.models.hmpps.UpstreamApiError @ContextConfiguration( initializers = [ConfigDataApplicationContextInitializer::class], @@ -24,7 +16,7 @@ import uk.gov.justice.digital.hmpps.hmppsintegrationapi.models.hmpps.UpstreamApi ) internal class GetLicenceConditionServiceTest( @MockBean val createAndVaryLicenceGateway: CreateAndVaryLicenceGateway, - @MockBean val getPersonService: GetPersonService, +// @MockBean val getPersonService: GetPersonService, private val getLicenceConditionService: GetLicenceConditionService, ) : DescribeSpec( { @@ -34,60 +26,60 @@ internal class GetLicenceConditionServiceTest( val licences = listOf(Licence(id = "12345")) val conditions = listOf(LicenceCondition(condition = "MockCondition", category = "AP")) - beforeEach { - Mockito.reset(getPersonService) - Mockito.reset(createAndVaryLicenceGateway) +// beforeEach { +// // Mockito.reset(getPersonService) +// Mockito.reset(createAndVaryLicenceGateway) +// +// // whenever(getPersonService.execute(hmppsId = hmppsId)).thenReturn(Response(person)) +// whenever(createAndVaryLicenceGateway.getLicenceSummaries(id = crn)).thenReturn(Response(licences)) +// whenever(createAndVaryLicenceGateway.getLicenceConditions(id = 12345)).thenReturn(Response(conditions)) +// } - whenever(getPersonService.execute(hmppsId = hmppsId)).thenReturn(Response(person)) - whenever(createAndVaryLicenceGateway.getLicenceSummaries(id = crn)).thenReturn(Response(licences)) - whenever(createAndVaryLicenceGateway.getLicenceConditions(id = 12345)).thenReturn(Response(conditions)) - } +// it("performs a search according to hmpps Id") { +// getLicenceConditionService.execute(hmppsId) +// verify(getPersonService, VerificationModeFactory.times(1)).execute(hmppsId = hmppsId) +// } - it("performs a search according to hmpps Id") { - getLicenceConditionService.execute(hmppsId) - verify(getPersonService, VerificationModeFactory.times(1)).execute(hmppsId = hmppsId) - } +// it("should return a list of errors if person not found") { +// // whenever(getPersonService.execute(hmppsId = "notfound")).thenReturn( +// // Response( +// // data = null, +// // errors = +// // listOf( +// // UpstreamApiError( +// // causedBy = UpstreamApi.CVL, +// // type = UpstreamApiError.Type.ENTITY_NOT_FOUND, +// // ), +// // ), +// // ), +// // ) +// val result = getLicenceConditionService.execute("notfound") +// result.data.licences.shouldBe(emptyList()) +// result.errors.first().type.shouldBe(UpstreamApiError.Type.ENTITY_NOT_FOUND) +// } - it("should return a list of errors if person not found") { - whenever(getPersonService.execute(hmppsId = "notfound")).thenReturn( - Response( - data = null, - errors = - listOf( - UpstreamApiError( - causedBy = UpstreamApi.CVL, - type = UpstreamApiError.Type.ENTITY_NOT_FOUND, - ), - ), - ), - ) - val result = getLicenceConditionService.execute("notfound") - result.data.licences.shouldBe(emptyList()) - result.errors.first().type.shouldBe(UpstreamApiError.Type.ENTITY_NOT_FOUND) - } - - it("should return a list of errors if create and vary licence gateway service returns error") { - whenever(createAndVaryLicenceGateway.getLicenceSummaries(id = crn)).thenReturn( - Response( - data = emptyList(), - errors = - listOf( - UpstreamApiError( - causedBy = UpstreamApi.CVL, - type = UpstreamApiError.Type.ENTITY_NOT_FOUND, - ), - ), - ), - ) - val result = getLicenceConditionService.execute(hmppsId = hmppsId) - result.data.licences.shouldBe(emptyList()) - result.errors.first().type.shouldBe(UpstreamApiError.Type.ENTITY_NOT_FOUND) - } - - it("should return licence condition from gateway") { - val result = getLicenceConditionService.execute(hmppsId = hmppsId) - result.data.licences.first().conditions.first().condition.shouldBe("MockCondition") - result.errors.count().shouldBe(0) - } +// it("should return a list of errors if create and vary licence gateway service returns error") { +// whenever(createAndVaryLicenceGateway.getLicenceSummaries(id = crn)).thenReturn( +// Response( +// data = emptyList(), +// errors = +// listOf( +// UpstreamApiError( +// causedBy = UpstreamApi.CVL, +// type = UpstreamApiError.Type.ENTITY_NOT_FOUND, +// ), +// ), +// ), +// ) +// val result = getLicenceConditionService.execute(hmppsId = hmppsId) +// result.data.licences.shouldBe(emptyList()) +// result.errors.first().type.shouldBe(UpstreamApiError.Type.ENTITY_NOT_FOUND) +// } +// +// it("should return licence condition from gateway") { +// val result = getLicenceConditionService.execute(hmppsId = hmppsId) +// result.data.licences.first().conditions.first().condition.shouldBe("MockCondition") +// result.errors.count().shouldBe(0) +// } }, ) From 659148325d95dd1e2f92dff819100bb834be99b0 Mon Sep 17 00:00:00 2001 From: Chiara Date: Thu, 23 May 2024 14:06:35 +0100 Subject: [PATCH 4/6] Reverting --- .../services/GetLicenceConditionService.kt | 21 ++-- .../GetLicenceConditionServiceTest.kt | 114 ++++++++++-------- 2 files changed, 72 insertions(+), 63 deletions(-) diff --git a/src/main/kotlin/uk/gov/justice/digital/hmpps/hmppsintegrationapi/services/GetLicenceConditionService.kt b/src/main/kotlin/uk/gov/justice/digital/hmpps/hmppsintegrationapi/services/GetLicenceConditionService.kt index 9b75efda6..b6711d26c 100644 --- a/src/main/kotlin/uk/gov/justice/digital/hmpps/hmppsintegrationapi/services/GetLicenceConditionService.kt +++ b/src/main/kotlin/uk/gov/justice/digital/hmpps/hmppsintegrationapi/services/GetLicenceConditionService.kt @@ -10,26 +10,27 @@ import uk.gov.justice.digital.hmpps.hmppsintegrationapi.models.hmpps.Response @Service class GetLicenceConditionService( @Autowired val createAndVaryLicenceGateway: CreateAndVaryLicenceGateway, -// @Autowired val getPersonService: GetPersonService, + @Autowired val getPersonService: GetPersonService, ) { fun execute(hmppsId: String): Response { -// val personResponse = getPersonService.execute(hmppsId) -// val crn = personResponse.data?.identifiers?.deliusCrn + val personResponse = getPersonService.execute(hmppsId) + val crn = personResponse.data?.identifiers?.deliusCrn var licences: Response> = Response(data = emptyList()) var personLicences = PersonLicences(hmppsId) - - licences = createAndVaryLicenceGateway.getLicenceSummaries(id = hmppsId) - licences.data.forEach { summary -> - summary.conditions = summary.id.toIntOrNull()?.let { licenceId -> - createAndVaryLicenceGateway.getLicenceConditions(licenceId).data - } ?: emptyList() + if (crn != null) { + licences = createAndVaryLicenceGateway.getLicenceSummaries(id = crn) + licences.data.forEach { summary -> + summary.conditions = summary.id.toIntOrNull()?.let { licenceId -> + createAndVaryLicenceGateway.getLicenceConditions(licenceId).data + } ?: emptyList() + } personLicences = PersonLicences(hmppsId, licences.data.firstOrNull()?.offenderNumber, licences.data) } return Response( data = personLicences, - errors = licences.errors, + errors = personResponse.errors + licences.errors, ) } } diff --git a/src/test/kotlin/uk/gov/justice/digital/hmpps/hmppsintegrationapi/services/GetLicenceConditionServiceTest.kt b/src/test/kotlin/uk/gov/justice/digital/hmpps/hmppsintegrationapi/services/GetLicenceConditionServiceTest.kt index 17a5de237..4e5d48831 100644 --- a/src/test/kotlin/uk/gov/justice/digital/hmpps/hmppsintegrationapi/services/GetLicenceConditionServiceTest.kt +++ b/src/test/kotlin/uk/gov/justice/digital/hmpps/hmppsintegrationapi/services/GetLicenceConditionServiceTest.kt @@ -1,6 +1,11 @@ package uk.gov.justice.digital.hmpps.hmppsintegrationapi.services import io.kotest.core.spec.style.DescribeSpec +import io.kotest.matchers.shouldBe +import org.mockito.Mockito +import org.mockito.internal.verification.VerificationModeFactory +import org.mockito.kotlin.verify +import org.mockito.kotlin.whenever import org.springframework.boot.test.context.ConfigDataApplicationContextInitializer import org.springframework.boot.test.mock.mockito.MockBean import org.springframework.test.context.ContextConfiguration @@ -9,6 +14,9 @@ import uk.gov.justice.digital.hmpps.hmppsintegrationapi.models.hmpps.Identifiers import uk.gov.justice.digital.hmpps.hmppsintegrationapi.models.hmpps.Licence import uk.gov.justice.digital.hmpps.hmppsintegrationapi.models.hmpps.LicenceCondition import uk.gov.justice.digital.hmpps.hmppsintegrationapi.models.hmpps.Person +import uk.gov.justice.digital.hmpps.hmppsintegrationapi.models.hmpps.Response +import uk.gov.justice.digital.hmpps.hmppsintegrationapi.models.hmpps.UpstreamApi +import uk.gov.justice.digital.hmpps.hmppsintegrationapi.models.hmpps.UpstreamApiError @ContextConfiguration( initializers = [ConfigDataApplicationContextInitializer::class], @@ -16,7 +24,7 @@ import uk.gov.justice.digital.hmpps.hmppsintegrationapi.models.hmpps.Person ) internal class GetLicenceConditionServiceTest( @MockBean val createAndVaryLicenceGateway: CreateAndVaryLicenceGateway, -// @MockBean val getPersonService: GetPersonService, + @MockBean val getPersonService: GetPersonService, private val getLicenceConditionService: GetLicenceConditionService, ) : DescribeSpec( { @@ -26,60 +34,60 @@ internal class GetLicenceConditionServiceTest( val licences = listOf(Licence(id = "12345")) val conditions = listOf(LicenceCondition(condition = "MockCondition", category = "AP")) -// beforeEach { -// // Mockito.reset(getPersonService) -// Mockito.reset(createAndVaryLicenceGateway) -// -// // whenever(getPersonService.execute(hmppsId = hmppsId)).thenReturn(Response(person)) -// whenever(createAndVaryLicenceGateway.getLicenceSummaries(id = crn)).thenReturn(Response(licences)) -// whenever(createAndVaryLicenceGateway.getLicenceConditions(id = 12345)).thenReturn(Response(conditions)) -// } + beforeEach { + Mockito.reset(getPersonService) + Mockito.reset(createAndVaryLicenceGateway) -// it("performs a search according to hmpps Id") { -// getLicenceConditionService.execute(hmppsId) -// verify(getPersonService, VerificationModeFactory.times(1)).execute(hmppsId = hmppsId) -// } + whenever(getPersonService.execute(hmppsId = hmppsId)).thenReturn(Response(person)) + whenever(createAndVaryLicenceGateway.getLicenceSummaries(id = crn)).thenReturn(Response(licences)) + whenever(createAndVaryLicenceGateway.getLicenceConditions(id = 12345)).thenReturn(Response(conditions)) + } -// it("should return a list of errors if person not found") { -// // whenever(getPersonService.execute(hmppsId = "notfound")).thenReturn( -// // Response( -// // data = null, -// // errors = -// // listOf( -// // UpstreamApiError( -// // causedBy = UpstreamApi.CVL, -// // type = UpstreamApiError.Type.ENTITY_NOT_FOUND, -// // ), -// // ), -// // ), -// // ) -// val result = getLicenceConditionService.execute("notfound") -// result.data.licences.shouldBe(emptyList()) -// result.errors.first().type.shouldBe(UpstreamApiError.Type.ENTITY_NOT_FOUND) -// } + it("performs a search according to hmpps Id") { + getLicenceConditionService.execute(hmppsId) + verify(getPersonService, VerificationModeFactory.times(1)).execute(hmppsId = hmppsId) + } -// it("should return a list of errors if create and vary licence gateway service returns error") { -// whenever(createAndVaryLicenceGateway.getLicenceSummaries(id = crn)).thenReturn( -// Response( -// data = emptyList(), -// errors = -// listOf( -// UpstreamApiError( -// causedBy = UpstreamApi.CVL, -// type = UpstreamApiError.Type.ENTITY_NOT_FOUND, -// ), -// ), -// ), -// ) -// val result = getLicenceConditionService.execute(hmppsId = hmppsId) -// result.data.licences.shouldBe(emptyList()) -// result.errors.first().type.shouldBe(UpstreamApiError.Type.ENTITY_NOT_FOUND) -// } -// -// it("should return licence condition from gateway") { -// val result = getLicenceConditionService.execute(hmppsId = hmppsId) -// result.data.licences.first().conditions.first().condition.shouldBe("MockCondition") -// result.errors.count().shouldBe(0) -// } + it("should return a list of errors if person not found") { + whenever(getPersonService.execute(hmppsId = "notfound")).thenReturn( + Response( + data = null, + errors = + listOf( + UpstreamApiError( + causedBy = UpstreamApi.CVL, + type = UpstreamApiError.Type.ENTITY_NOT_FOUND, + ), + ), + ), + ) + val result = getLicenceConditionService.execute("notfound") + result.data.licences.shouldBe(emptyList()) + result.errors.first().type.shouldBe(UpstreamApiError.Type.ENTITY_NOT_FOUND) + } + + it("should return a list of errors if create and vary licence gateway service returns error") { + whenever(createAndVaryLicenceGateway.getLicenceSummaries(id = crn)).thenReturn( + Response( + data = emptyList(), + errors = + listOf( + UpstreamApiError( + causedBy = UpstreamApi.CVL, + type = UpstreamApiError.Type.ENTITY_NOT_FOUND, + ), + ), + ), + ) + val result = getLicenceConditionService.execute(hmppsId = hmppsId) + result.data.licences.shouldBe(emptyList()) + result.errors.first().type.shouldBe(UpstreamApiError.Type.ENTITY_NOT_FOUND) + } + + it("should return licence condition from gateway") { + val result = getLicenceConditionService.execute(hmppsId = hmppsId) + result.data.licences.first().conditions.first().condition.shouldBe("MockCondition") + result.errors.count().shouldBe(0) + } }, ) From 67b4a8fc97f805813bc5cc54ea2af3602c7e54dc Mon Sep 17 00:00:00 2001 From: Chiara Date: Thu, 23 May 2024 14:33:03 +0100 Subject: [PATCH 5/6] Format --- .../GetLicenceConditionServiceTest.kt | 25 ++++++++----------- 1 file changed, 11 insertions(+), 14 deletions(-) diff --git a/src/test/kotlin/uk/gov/justice/digital/hmpps/hmppsintegrationapi/services/GetLicenceConditionServiceTest.kt b/src/test/kotlin/uk/gov/justice/digital/hmpps/hmppsintegrationapi/services/GetLicenceConditionServiceTest.kt index 3820318ac..4e5d48831 100644 --- a/src/test/kotlin/uk/gov/justice/digital/hmpps/hmppsintegrationapi/services/GetLicenceConditionServiceTest.kt +++ b/src/test/kotlin/uk/gov/justice/digital/hmpps/hmppsintegrationapi/services/GetLicenceConditionServiceTest.kt @@ -1,6 +1,11 @@ package uk.gov.justice.digital.hmpps.hmppsintegrationapi.services import io.kotest.core.spec.style.DescribeSpec +import io.kotest.matchers.shouldBe +import org.mockito.Mockito +import org.mockito.internal.verification.VerificationModeFactory +import org.mockito.kotlin.verify +import org.mockito.kotlin.whenever import org.springframework.boot.test.context.ConfigDataApplicationContextInitializer import org.springframework.boot.test.mock.mockito.MockBean import org.springframework.test.context.ContextConfiguration @@ -9,6 +14,9 @@ import uk.gov.justice.digital.hmpps.hmppsintegrationapi.models.hmpps.Identifiers import uk.gov.justice.digital.hmpps.hmppsintegrationapi.models.hmpps.Licence import uk.gov.justice.digital.hmpps.hmppsintegrationapi.models.hmpps.LicenceCondition import uk.gov.justice.digital.hmpps.hmppsintegrationapi.models.hmpps.Person +import uk.gov.justice.digital.hmpps.hmppsintegrationapi.models.hmpps.Response +import uk.gov.justice.digital.hmpps.hmppsintegrationapi.models.hmpps.UpstreamApi +import uk.gov.justice.digital.hmpps.hmppsintegrationapi.models.hmpps.UpstreamApiError @ContextConfiguration( initializers = [ConfigDataApplicationContextInitializer::class], @@ -17,8 +25,6 @@ import uk.gov.justice.digital.hmpps.hmppsintegrationapi.models.hmpps.Person internal class GetLicenceConditionServiceTest( @MockBean val createAndVaryLicenceGateway: CreateAndVaryLicenceGateway, @MockBean val getPersonService: GetPersonService, - @MockBean val getPersonService: GetPersonService, - private val getLicenceConditionService: GetLicenceConditionService, ) : DescribeSpec( { @@ -28,18 +34,9 @@ internal class GetLicenceConditionServiceTest( val licences = listOf(Licence(id = "12345")) val conditions = listOf(LicenceCondition(condition = "MockCondition", category = "AP")) - beforeEach { - Mockito.reset(getPersonService) - Mockito.reset(createAndVaryLicenceGateway) - - whenever(getPersonService.execute(hmppsId = hmppsId)).thenReturn(Response(person)) - whenever(createAndVaryLicenceGateway.getLicenceSummaries(id = crn)).thenReturn(Response(licences)) - whenever(createAndVaryLicenceGateway.getLicenceConditions(id = 12345)).thenReturn(Response(conditions)) - } - it("performs a search according to hmpps Id") { - getLicenceConditionService.execute(hmppsId) - verify(getPersonService, VerificationModeFactory.times(1)).execute(hmppsId = hmppsId) - } + beforeEach { + Mockito.reset(getPersonService) + Mockito.reset(createAndVaryLicenceGateway) whenever(getPersonService.execute(hmppsId = hmppsId)).thenReturn(Response(person)) whenever(createAndVaryLicenceGateway.getLicenceSummaries(id = crn)).thenReturn(Response(licences)) From d25689e9f6a2856cea9f9675fbac53f9cc7c58aa Mon Sep 17 00:00:00 2001 From: Chiara Date: Thu, 23 May 2024 14:41:14 +0100 Subject: [PATCH 6/6] adding errors --- .../hmppsintegrationapi/services/GetLicenceConditionService.kt | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/main/kotlin/uk/gov/justice/digital/hmpps/hmppsintegrationapi/services/GetLicenceConditionService.kt b/src/main/kotlin/uk/gov/justice/digital/hmpps/hmppsintegrationapi/services/GetLicenceConditionService.kt index e18cea368..b6711d26c 100644 --- a/src/main/kotlin/uk/gov/justice/digital/hmpps/hmppsintegrationapi/services/GetLicenceConditionService.kt +++ b/src/main/kotlin/uk/gov/justice/digital/hmpps/hmppsintegrationapi/services/GetLicenceConditionService.kt @@ -30,7 +30,7 @@ class GetLicenceConditionService( return Response( data = personLicences, - errors = licences.errors, + errors = personResponse.errors + licences.errors, ) } }