Skip to content

Commit 6fce67e

Browse files
committed
Created thin slice Licence model
1 parent 40c1e0c commit 6fce67e

File tree

14 files changed

+180
-4
lines changed

14 files changed

+180
-4
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
package uk.gov.justice.digital.hmpps.hmppsintegrationapi.gateways
2+
3+
import org.springframework.beans.factory.annotation.Autowired
4+
import org.springframework.beans.factory.annotation.Value
5+
import org.springframework.http.HttpMethod
6+
import org.springframework.stereotype.Component
7+
import uk.gov.justice.digital.hmpps.hmppsintegrationapi.extensions.WebClientWrapper
8+
import uk.gov.justice.digital.hmpps.hmppsintegrationapi.extensions.WebClientWrapper.WebClientWrapperResponse
9+
import uk.gov.justice.digital.hmpps.hmppsintegrationapi.models.createAndVaryLicence.CvlLicence
10+
import uk.gov.justice.digital.hmpps.hmppsintegrationapi.models.hmpps.Licence
11+
import uk.gov.justice.digital.hmpps.hmppsintegrationapi.models.hmpps.Response
12+
import uk.gov.justice.digital.hmpps.hmppsintegrationapi.models.hmpps.UpstreamApi
13+
14+
@Component
15+
class CreateAndVaryLicenceGateway(@Value("\${services.create-and-vary-licence.base-url}") baseUrl: String) {
16+
private val webClient = WebClientWrapper(baseUrl)
17+
18+
@Autowired
19+
lateinit var hmppsAuthGateway: HmppsAuthGateway
20+
21+
fun getLicenceSummaries(id: String): Response<Licence?> {
22+
val result = webClient.request<CvlLicence>(
23+
HttpMethod.GET,
24+
"/public/licence-summaries/crn/$id",
25+
authenticationHeader(),
26+
UpstreamApi.CVL,
27+
)
28+
29+
return when (result) {
30+
is WebClientWrapperResponse.Success -> {
31+
Response(data = result.data.toLicence())
32+
}
33+
34+
is WebClientWrapperResponse.Error -> {
35+
Response(
36+
data = null,
37+
errors = result.errors,
38+
)
39+
}
40+
}
41+
}
42+
43+
private fun authenticationHeader(): Map<String, String> {
44+
val token = hmppsAuthGateway.getClientToken("CVL")
45+
return mapOf(
46+
"Authorization" to "Bearer $token",
47+
)
48+
}
49+
}

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

+1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
package uk.gov.justice.digital.hmpps.hmppsintegrationapi.models.createAndVaryLicence
2+
23
import uk.gov.justice.digital.hmpps.hmppsintegrationapi.models.hmpps.Licence
34
data class CvlLicence(
45
val prisonNumber: String? = null,
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
package uk.gov.justice.digital.hmpps.hmppsintegrationapi.models.hmpps
22

33
data class Licence(
4-
val offenderNumber: String? = null
4+
val offenderNumber: String? = null,
55
)
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
package uk.gov.justice.digital.hmpps.hmppsintegrationapi.models.hmpps
22

33
enum class UpstreamApi {
4-
NOMIS, PRISONER_OFFENDER_SEARCH, PROBATION_OFFENDER_SEARCH, NDELIUS, ASSESS_RISKS_AND_NEEDS, ADJUDICATIONS, TEST
4+
NOMIS, PRISONER_OFFENDER_SEARCH, PROBATION_OFFENDER_SEARCH, NDELIUS, ASSESS_RISKS_AND_NEEDS, ADJUDICATIONS, CVL, TEST
55
}

src/main/resources/application-dev.yml

+2
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@ services:
1313
base-url: https://effective-proposal-framework-and-delius-dev.hmpps.service.justice.gov.uk
1414
adjudications:
1515
base-url: https://manage-adjudications-api-dev.hmpps.service.justice.gov.uk
16+
create-and-vary-licence:
17+
base-url: https://create-and-vary-a-licence-api-dev.hmpps.service.justice.gov.uk
1618
hmpps-auth:
1719
base-url: https://sign-in-dev.hmpps.service.justice.gov.uk
1820
username: ${CLIENT_ID}

src/main/resources/application-local-docker.yml

+2
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@ services:
1313
base-url: http://probation-integration-epf-api:4010
1414
adjudications:
1515
base-url: http://adjudications-api:4010
16+
create-and-vary-licence:
17+
base-url: http://create-and-vary-licence-api:4010
1618
hmpps-auth:
1719
base-url: http://hmpps-auth:8080
1820

src/main/resources/application-local.yml

+3-1
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@ services:
1515
base-url: http://localhost:9090
1616
adjudications:
1717
base-url: http://localhost:4045
18+
create-and-vary-licence:
19+
base-url: http://localhost:4070
1820

1921
hmpps.sqs:
2022
provider: localstack
@@ -43,4 +45,4 @@ authorisation:
4345
- "/health/ping"
4446
- "/health/readiness"
4547
- "/health/liveness"
46-
- "/info"
48+
- "/info"

src/main/resources/application-preprod.yml

+2
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@ services:
1313
base-url: https://effective-proposal-framework-and-delius-preprod.hmpps.service.justice.gov.uk
1414
adjudications:
1515
base-url: https://manage-adjudications-api-preprod.hmpps.service.justice.gov.uk
16+
create-and-vary-licence:
17+
base-url: https://create-and-vary-a-licence-api-preprod.hmpps.service.justice.gov.uk
1618
hmpps-auth:
1719
base-url: https://sign-in-preprod.hmpps.service.justice.gov.uk
1820
username: ${CLIENT_ID}

src/main/resources/application-prod.yml

+2
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@ services:
1313
base-url: https://effective-proposal-framework-and-delius.hmpps.service.justice.gov.uk
1414
adjudications:
1515
base-url: https://manage-adjudications-api-prod.hmpps.service.justice.gov.uk
16+
create-and-vary-licence:
17+
base-url: https://create-and-vary-a-licence-api-prod.hmpps.service.justice.gov.uk
1618
hmpps-auth:
1719
base-url: https://sign-in.hmpps.service.justice.gov.uk
1820
username: ${CLIENT_ID}

src/main/resources/application-test.yml

+2
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,8 @@ services:
2424
base-url: http://localhost:4005
2525
adjudications:
2626
base-url: http://localhost:4006
27+
create-and-vary-licence:
28+
base-url: http://localhost:4007
2729

2830
hmpps.sqs:
2931
provider: localstack

src/main/resources/application.yml

+2
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,8 @@ services:
6969
base-url: http://localhost:4060
7070
adjudications:
7171
base-url: http://localhost:4045
72+
create-and-vary-licence:
73+
base-url: http://localhost:4070
7274

7375
sentry:
7476
traces-sample-rate: "0.05"
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
package uk.gov.justice.digital.hmpps.hmppsintegrationapi.gateways.createAndVaryLicence
2+
3+
import io.kotest.core.spec.style.DescribeSpec
4+
import io.kotest.matchers.booleans.shouldBeTrue
5+
import io.kotest.matchers.shouldBe
6+
import org.mockito.Mockito
7+
import org.mockito.internal.verification.VerificationModeFactory
8+
import org.mockito.kotlin.verify
9+
import org.mockito.kotlin.whenever
10+
import org.springframework.boot.test.context.ConfigDataApplicationContextInitializer
11+
import org.springframework.boot.test.mock.mockito.MockBean
12+
import org.springframework.http.HttpStatus
13+
import org.springframework.test.context.ActiveProfiles
14+
import org.springframework.test.context.ContextConfiguration
15+
import uk.gov.justice.digital.hmpps.hmppsintegrationapi.gateways.CreateAndVaryLicenceGateway
16+
import uk.gov.justice.digital.hmpps.hmppsintegrationapi.gateways.HmppsAuthGateway
17+
import uk.gov.justice.digital.hmpps.hmppsintegrationapi.mockservers.CreateAndVaryLicenceApiMockServer
18+
import uk.gov.justice.digital.hmpps.hmppsintegrationapi.mockservers.HmppsAuthMockServer
19+
import uk.gov.justice.digital.hmpps.hmppsintegrationapi.models.hmpps.Licence
20+
import uk.gov.justice.digital.hmpps.hmppsintegrationapi.models.hmpps.UpstreamApiError
21+
22+
@ActiveProfiles("test")
23+
@ContextConfiguration(
24+
initializers = [ConfigDataApplicationContextInitializer::class],
25+
classes = [CreateAndVaryLicenceGateway::class],
26+
)
27+
class GetLicenceSummariesTest(
28+
@MockBean val hmppsAuthGateway: HmppsAuthGateway,
29+
private val createAndVaryLicenceGateway: CreateAndVaryLicenceGateway,
30+
) : DescribeSpec(
31+
{
32+
val createAndVaryLicenceApiMockServer = CreateAndVaryLicenceApiMockServer()
33+
val deliusCrn = "X777776"
34+
35+
beforeEach {
36+
createAndVaryLicenceApiMockServer.start()
37+
createAndVaryLicenceApiMockServer.stubGetLicenceSummaries(
38+
deliusCrn,
39+
"""
40+
{
41+
"prisonNumber": "1140484"
42+
}
43+
""",
44+
)
45+
46+
Mockito.reset(hmppsAuthGateway)
47+
whenever(hmppsAuthGateway.getClientToken("CVL")).thenReturn(HmppsAuthMockServer.TOKEN)
48+
}
49+
50+
afterTest {
51+
createAndVaryLicenceApiMockServer.stop()
52+
}
53+
54+
it("authenticates using HMPPS Auth with credentials") {
55+
createAndVaryLicenceGateway.getLicenceSummaries(deliusCrn)
56+
57+
verify(hmppsAuthGateway, VerificationModeFactory.times(1)).getClientToken("CVL")
58+
}
59+
60+
it("returns licences for a person with the matching ID") {
61+
val response = createAndVaryLicenceGateway.getLicenceSummaries(deliusCrn)
62+
63+
response.data.shouldBe(
64+
Licence(
65+
offenderNumber = "1140484",
66+
),
67+
)
68+
}
69+
70+
it("returns an error when 404 NOT FOUND is returned") {
71+
createAndVaryLicenceApiMockServer.stubGetLicenceSummaries(
72+
deliusCrn,
73+
"""
74+
{
75+
"developerMessage": "cannot find person"
76+
}
77+
""",
78+
HttpStatus.NOT_FOUND,
79+
)
80+
81+
val response = createAndVaryLicenceGateway.getLicenceSummaries(deliusCrn)
82+
83+
response.hasError(UpstreamApiError.Type.ENTITY_NOT_FOUND).shouldBeTrue()
84+
}
85+
},
86+
)
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
package uk.gov.justice.digital.hmpps.hmppsintegrationapi.mockservers
2+
3+
import com.github.tomakehurst.wiremock.WireMockServer
4+
import com.github.tomakehurst.wiremock.client.WireMock
5+
import org.springframework.http.HttpStatus
6+
7+
class CreateAndVaryLicenceApiMockServer : WireMockServer(WIREMOCK_PORT) {
8+
companion object {
9+
private const val WIREMOCK_PORT = 4007
10+
}
11+
12+
fun stubGetLicenceSummaries(id: String, body: String, status: HttpStatus = HttpStatus.OK) {
13+
stubFor(
14+
WireMock.get("/public/licence-summaries/crn/$id")
15+
.withHeader(
16+
"Authorization",
17+
WireMock.matching("Bearer ${HmppsAuthMockServer.TOKEN}"),
18+
).willReturn(
19+
WireMock.aResponse()
20+
.withHeader("Content-Type", "application/json")
21+
.withStatus(status.value())
22+
.withBody(body.trimIndent()),
23+
),
24+
)
25+
}
26+
}

src/test/kotlin/uk/gov/justice/digital/hmpps/hmppsintegrationapi/models/createAndVaryLicence/LicenceTest.kt

+1-1
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ class LicenceTest : DescribeSpec(
88
it("maps one-to-one attributes to integration API attributes") {
99
val cvlLcense = CvlLicence(
1010
prisonNumber = "1140484",
11-
)
11+
)
1212

1313
val integrationApiLicense = cvlLcense.toLicence()
1414

0 commit comments

Comments
 (0)