generated from ministryofjustice/hmpps-template-kotlin
-
Notifications
You must be signed in to change notification settings - Fork 2
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
PES - add hmpps id endpoint for a given nomis number #452
Merged
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
37 changes: 37 additions & 0 deletions
37
...tlin/uk/gov/justice/digital/hmpps/hmppsintegrationapi/controllers/v1/HmppsIdController.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
package uk.gov.justice.digital.hmpps.hmppsintegrationapi.controllers.v1 | ||
|
||
import org.springframework.beans.factory.annotation.Autowired | ||
import org.springframework.web.bind.annotation.GetMapping | ||
import org.springframework.web.bind.annotation.PathVariable | ||
import org.springframework.web.bind.annotation.RequestMapping | ||
import org.springframework.web.bind.annotation.RestController | ||
import uk.gov.justice.digital.hmpps.hmppsintegrationapi.exception.EntityNotFoundException | ||
import uk.gov.justice.digital.hmpps.hmppsintegrationapi.extensions.decodeUrlCharacters | ||
import uk.gov.justice.digital.hmpps.hmppsintegrationapi.models.hmpps.HmppsId | ||
import uk.gov.justice.digital.hmpps.hmppsintegrationapi.models.hmpps.UpstreamApiError | ||
import uk.gov.justice.digital.hmpps.hmppsintegrationapi.services.GetHmppsIdService | ||
import uk.gov.justice.digital.hmpps.hmppsintegrationapi.services.internal.AuditService | ||
|
||
@RestController | ||
@RequestMapping("/v1/hmpps-id") | ||
class HmppsIdController( | ||
@Autowired val getHmppsIdService: GetHmppsIdService, | ||
@Autowired val auditService: AuditService, | ||
) { | ||
@GetMapping("nomis-number/{encodedNomisNumber}") | ||
fun getHmppsIdByNomisNumber( | ||
@PathVariable encodedNomisNumber: String, | ||
): Map<String, HmppsId?> { | ||
val nomisNumber = encodedNomisNumber.decodeUrlCharacters() | ||
|
||
val response = getHmppsIdService.execute(nomisNumber) | ||
|
||
if (response.hasError(UpstreamApiError.Type.ENTITY_NOT_FOUND)) { | ||
throw EntityNotFoundException("Could not find HMPPS ID for nomis number: $nomisNumber") | ||
} | ||
|
||
auditService.createEvent("GET_HMPPS_ID_BY_NOMIS_NUMBER", mapOf("nomisNumber" to nomisNumber)) | ||
|
||
return mapOf("data" to response.data) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
5 changes: 5 additions & 0 deletions
5
src/main/kotlin/uk/gov/justice/digital/hmpps/hmppsintegrationapi/models/hmpps/HmppsId.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
package uk.gov.justice.digital.hmpps.hmppsintegrationapi.models.hmpps | ||
|
||
data class HmppsId( | ||
val hmppsId: String? = null, | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
20 changes: 20 additions & 0 deletions
20
...ain/kotlin/uk/gov/justice/digital/hmpps/hmppsintegrationapi/services/GetHmppsIdService.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
package uk.gov.justice.digital.hmpps.hmppsintegrationapi.services | ||
|
||
import org.springframework.beans.factory.annotation.Autowired | ||
import org.springframework.stereotype.Service | ||
import uk.gov.justice.digital.hmpps.hmppsintegrationapi.models.hmpps.HmppsId | ||
import uk.gov.justice.digital.hmpps.hmppsintegrationapi.models.hmpps.Response | ||
|
||
@Service | ||
class GetHmppsIdService( | ||
@Autowired val getPersonService: GetPersonService, | ||
) { | ||
fun execute(hmppsId: String): Response<HmppsId?> { | ||
val personResponse = getPersonService.execute(hmppsId = hmppsId) | ||
|
||
return Response( | ||
data = HmppsId(hmppsId = personResponse.data?.hmppsId), | ||
errors = personResponse.errors, | ||
) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
83 changes: 83 additions & 0 deletions
83
.../uk/gov/justice/digital/hmpps/hmppsintegrationapi/controllers/v1/HmppsIdControllerTest.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,83 @@ | ||
package uk.gov.justice.digital.hmpps.hmppsintegrationapi.controllers.v1 | ||
|
||
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.doThrow | ||
import org.mockito.kotlin.verify | ||
import org.mockito.kotlin.whenever | ||
import org.springframework.beans.factory.annotation.Autowired | ||
import org.springframework.boot.test.autoconfigure.web.servlet.WebMvcTest | ||
import org.springframework.boot.test.mock.mockito.MockBean | ||
import org.springframework.http.HttpStatus | ||
import org.springframework.test.context.ActiveProfiles | ||
import org.springframework.test.web.servlet.MockMvc | ||
import org.springframework.web.reactive.function.client.WebClientResponseException | ||
import uk.gov.justice.digital.hmpps.hmppsintegrationapi.helpers.IntegrationAPIMockMvc | ||
import uk.gov.justice.digital.hmpps.hmppsintegrationapi.models.hmpps.HmppsId | ||
import uk.gov.justice.digital.hmpps.hmppsintegrationapi.models.hmpps.Response | ||
import uk.gov.justice.digital.hmpps.hmppsintegrationapi.services.GetHmppsIdService | ||
import uk.gov.justice.digital.hmpps.hmppsintegrationapi.services.internal.AuditService | ||
|
||
@WebMvcTest(controllers = [HmppsIdController::class]) | ||
@ActiveProfiles("test") | ||
internal class HmppsIdControllerTest( | ||
@Autowired var springMockMvc: MockMvc, | ||
@MockBean val getHmppsIdService: GetHmppsIdService, | ||
@MockBean val auditService: AuditService, | ||
) : DescribeSpec({ | ||
val nomisNumber = "A1234AA" | ||
val path = "/v1/hmpps-id/nomis-number/$nomisNumber" | ||
val mockMvc = IntegrationAPIMockMvc(springMockMvc) | ||
|
||
describe("GET $path") { | ||
beforeTest { | ||
Mockito.reset(getHmppsIdService) | ||
whenever(getHmppsIdService.execute(nomisNumber)).thenReturn( | ||
Response( | ||
data = HmppsId(hmppsId = nomisNumber), | ||
), | ||
) | ||
Mockito.reset(auditService) | ||
} | ||
|
||
it("returns a 200 OK status code") { | ||
val result = mockMvc.performAuthorised(path) | ||
|
||
result.response.status.shouldBe(HttpStatus.OK.value()) | ||
} | ||
|
||
it("gets the person detail for a person with the matching ID") { | ||
mockMvc.performAuthorised(path) | ||
|
||
verify(getHmppsIdService, VerificationModeFactory.times(1)).execute(nomisNumber) | ||
} | ||
|
||
it("logs audit") { | ||
mockMvc.performAuthorised(path) | ||
verify( | ||
auditService, | ||
VerificationModeFactory.times(1), | ||
).createEvent( | ||
"GET_HMPPS_ID_BY_NOMIS_NUMBER", | ||
mapOf("nomisNumber" to nomisNumber), | ||
) | ||
} | ||
|
||
it("returns a 500 INTERNAL SERVER ERROR status code when upstream api return expected error") { | ||
|
||
whenever(getHmppsIdService.execute(nomisNumber)).doThrow( | ||
WebClientResponseException(500, "MockError", null, null, null, null), | ||
) | ||
|
||
val result = mockMvc.performAuthorised(path) | ||
assert(result.response.status == 500) | ||
assert( | ||
result.response.contentAsString.equals( | ||
"{\"status\":500,\"errorCode\":null,\"userMessage\":\"500 MockError\",\"developerMessage\":\"Unable to complete request as an upstream service is not responding\",\"moreInfo\":null}", | ||
), | ||
) | ||
} | ||
} | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
43 changes: 43 additions & 0 deletions
43
...kotlin/uk/gov/justice/digital/hmpps/hmppsintegrationapi/services/GetHmppsIdServiceTest.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
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.kotlin.whenever | ||
import org.springframework.boot.test.context.ConfigDataApplicationContextInitializer | ||
import org.springframework.boot.test.mock.mockito.MockBean | ||
import org.springframework.test.context.ContextConfiguration | ||
import uk.gov.justice.digital.hmpps.hmppsintegrationapi.models.hmpps.HmppsId | ||
import uk.gov.justice.digital.hmpps.hmppsintegrationapi.models.hmpps.Identifiers | ||
import uk.gov.justice.digital.hmpps.hmppsintegrationapi.models.hmpps.Person | ||
import uk.gov.justice.digital.hmpps.hmppsintegrationapi.models.hmpps.Response | ||
|
||
@ContextConfiguration( | ||
initializers = [ConfigDataApplicationContextInitializer::class], | ||
classes = [GetHmppsIdService::class], | ||
) | ||
internal class GetHmppsIdServiceTest( | ||
@MockBean val getPersonService: GetPersonService, | ||
private val getHmppsIdService: GetHmppsIdService, | ||
) : DescribeSpec( | ||
{ | ||
val id = "A7777ZZ" | ||
val hmppsId = HmppsId(hmppsId = id) | ||
|
||
beforeEach { | ||
Mockito.reset(getPersonService) | ||
|
||
whenever(getPersonService.execute(id)).thenReturn( | ||
Response( | ||
data = Person(firstName = "Qui-gon", lastName = "Jin", hmppsId = id, identifiers = Identifiers(nomisNumber = id)), | ||
), | ||
) | ||
} | ||
|
||
it("Returns a hmpps id for the given id") { | ||
val result = getHmppsIdService.execute(id) | ||
|
||
result.shouldBe(Response(data = hmppsId)) | ||
} | ||
}, | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Would we want to return nonsNumber as hmppsId? Also would probation search return person without CRN? Perharps you are looking to update POSPrisoner(person search from Prisoner serach) to return hmppsId?