Skip to content

Commit

Permalink
PI-2108: Add get document by id
Browse files Browse the repository at this point in the history
  • Loading branch information
pmcphee77 committed Aug 9, 2024
1 parent f42a1fd commit 2acc477
Show file tree
Hide file tree
Showing 10 changed files with 97 additions and 2 deletions.
1 change: 1 addition & 0 deletions projects/court-case-and-delius/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ dependencies {
implementation(project(":libs:messaging"))
implementation(project(":libs:oauth-client"))
implementation(project(":libs:oauth-server"))
implementation(project(":libs:document-management"))

implementation("org.springframework.boot:spring-boot-starter-actuator")
implementation("org.springframework.boot:spring-boot-starter-data-jpa")
Expand Down
Binary file not shown.
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
{
"request": {
"method": "GET",
"urlPath": "/alfresco/fetch/alfrescoId"
},
"response": {
"headers": {
"content-type": "application/msword;charset=UTF-8",
"content-length": "14812",
"content-disposition": "attachment; filename=\"doc1\"; filename*=UTF-8''doc1",
"accept-ranges": "bytes",
"content-range": "bytes 0-14811/14812",
"cache-control": "max-age=0, must-revalidate",
"etag": "1699630109776",
"last-modified": "Fri, 10 Nov 2023 15:28:29 GMT",
"Custom-Alfresco-Header": "should be ignored"
},
"status": 200,
"bodyFileName": "document.pdf"
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,11 @@ import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMock
import org.springframework.boot.test.context.SpringBootTest
import org.springframework.boot.test.context.SpringBootTest.WebEnvironment.RANDOM_PORT
import org.springframework.test.web.servlet.MockMvc
import org.springframework.test.web.servlet.MvcResult
import org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get
import org.springframework.test.web.servlet.result.MockMvcResultMatchers
import org.springframework.test.web.servlet.result.MockMvcResultMatchers.status
import org.springframework.util.ResourceUtils
import uk.gov.justice.digital.hmpps.api.model.OffenderDocuments
import uk.gov.justice.digital.hmpps.api.resource.advice.ErrorResponse
import uk.gov.justice.digital.hmpps.data.generator.PersonGenerator
Expand Down Expand Up @@ -46,4 +49,38 @@ internal class DocumentsIntegrationTest {

assertThat(response.developerMessage, equalTo("type of INVALID was not valid"))
}

@Test
fun `call download document by id`() {
val crn = PersonGenerator.CURRENTLY_MANAGED.crn

mockMvc.perform(get("/probation-case/$crn/documents/alfrescoId").accept("application/octet-stream").withToken())
.andExpect(MockMvcResultMatchers.request().asyncStarted())
.andDo(MvcResult::getAsyncResult)
.andExpect(status().is2xxSuccessful)
.andExpect(MockMvcResultMatchers.header().string("Content-Type", "application/octet-stream"))
.andExpect(
MockMvcResultMatchers.header().string(
"Content-Disposition",
"attachment; filename=\"=?UTF-8?Q?filename.txt?=\"; filename*=UTF-8''filename.txt"
)
)
.andExpect(MockMvcResultMatchers.header().doesNotExist("Custom-Alfresco-Header"))
.andExpect(
MockMvcResultMatchers.content()
.bytes(ResourceUtils.getFile("classpath:simulations/__files/document.pdf").readBytes())
)
}

@Test
fun `call download document by id returns not found`() {
val crn = PersonGenerator.CURRENTLY_MANAGED.crn

val response = mockMvc
.perform(get("/probation-case/$crn/documents/wrong").withToken())
.andExpect(status().isNotFound)
.andReturn().response.contentAsJson<ErrorResponse>()

assertThat(response.developerMessage, equalTo("Document with id of wrong not found for CRN C123456"))
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -314,6 +314,19 @@ class CommunityApiController(
return proxy(request)
}

@GetMapping("/offenders/crn/{crn}/documents/{documentId}")
fun downloadDocument(
request: HttpServletRequest,
@PathVariable crn: String,
@PathVariable documentId: String
): Any {

if (featureFlags.enabled("ccd-download-document")) {
return documentResource.getOffenderDocumentById(crn, documentId)
}
return proxy(request)
}

@GetMapping("/**")
fun proxy(request: HttpServletRequest): Any {
val headers = request.headerNames.asSequence().associateWith { request.getHeader(it) }.toMutableMap()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,4 +16,10 @@ class DocumentResource(private val documentService: DocumentService) {
@RequestParam(required = false) type: String?,
@RequestParam(required = false) subType: String?,
) = documentService.getDocumentsGroupedFor(crn, DocumentFilter(type, subType))

@GetMapping("/{documentId}")
fun getOffenderDocumentById(
@PathVariable crn: String,
@PathVariable documentId: String
) = documentService.downloadDocument(crn, documentId)
}
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,9 @@ fun Document.typeCode(): String = when (tableName) {

interface DocumentRepository : JpaRepository<DocumentEntity, Long> {

@Query("select d.name from DocumentEntity d where d.personId = :personId and d.alfrescoId = :alfrescoId")
fun findNameByPersonIdAndAlfrescoId(personId: Long, alfrescoId: String): String?

@Query(
"""
select d from DocumentEntity d
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,13 @@
package uk.gov.justice.digital.hmpps.integrations.delius.service

import org.springframework.http.ResponseEntity
import org.springframework.stereotype.Service
import org.springframework.web.servlet.mvc.method.annotation.StreamingResponseBody
import uk.gov.justice.digital.hmpps.alfresco.AlfrescoClient
import uk.gov.justice.digital.hmpps.api.model.*
import uk.gov.justice.digital.hmpps.datetime.EuropeLondon
import uk.gov.justice.digital.hmpps.exception.InvalidRequestException
import uk.gov.justice.digital.hmpps.exception.NotFoundException
import uk.gov.justice.digital.hmpps.integrations.delius.entity.*
import uk.gov.justice.digital.hmpps.integrations.delius.person.entity.PersonRepository
import uk.gov.justice.digital.hmpps.integrations.delius.person.entity.getPerson
Expand All @@ -12,7 +16,8 @@ import java.time.LocalDateTime
@Service
class DocumentService(
private val personRepository: PersonRepository,
private val documentRepository: DocumentRepository
private val documentRepository: DocumentRepository,
private val alfrescoClient: AlfrescoClient
) {

fun getDocumentsGroupedFor(crn: String, filterData: DocumentFilter): OffenderDocuments {
Expand Down Expand Up @@ -44,6 +49,13 @@ class DocumentService(
return OffenderDocuments(documents, convictions)
}

fun downloadDocument(crn: String, id: String): ResponseEntity<StreamingResponseBody> {
val person = personRepository.getPerson(crn)
val filename = documentRepository.findNameByPersonIdAndAlfrescoId(person.id, id)
?: throw NotFoundException("Document with id of $id not found for CRN $crn")
return alfrescoClient.streamDocument(id, filename)
}

private fun filter(record: OffenderDocumentDetail, filter: DocumentFilter): Boolean {
var include = true
if (filter.type != null) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,8 @@ springdoc.default-produces-media-type: application/json

delius.db.username: CourtCaseAndDelius # Should match value in [deploy/database/access.yml].

integrations.alfresco.url: http://localhost:${wiremock.port}/alfresco

management:
endpoints.web:
base-path: /
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"request": {
"method": "GET",
"urlPath": "/alfresco/fetch/A001"
"urlPath": "/alfresco/fetch/alfrescoId"
},
"response": {
"headers": {
Expand Down

0 comments on commit 2acc477

Please sign in to comment.