Skip to content

Commit

Permalink
PI-1555 added document endpoints
Browse files Browse the repository at this point in the history
  • Loading branch information
stevomcallister committed Oct 13, 2023
1 parent 895afb9 commit 79fd947
Show file tree
Hide file tree
Showing 8 changed files with 101 additions and 14 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import uk.gov.justice.digital.hmpps.data.generator.CaseloadGenerator
import uk.gov.justice.digital.hmpps.data.generator.ContactOutcomeGenerator
import uk.gov.justice.digital.hmpps.data.generator.ContactTypeGenerator
import uk.gov.justice.digital.hmpps.data.generator.DatasetGenerator
import uk.gov.justice.digital.hmpps.data.generator.DocumentGenerator
import uk.gov.justice.digital.hmpps.data.generator.NsiStatusGenerator
import uk.gov.justice.digital.hmpps.data.generator.NsiTypeGenerator
import uk.gov.justice.digital.hmpps.data.generator.OfficeLocationGenerator
Expand All @@ -35,6 +36,7 @@ import uk.gov.justice.digital.hmpps.integrations.delius.caseload.CaseloadReposit
import uk.gov.justice.digital.hmpps.integrations.delius.contact.outcome.ContactOutcomeRepository
import uk.gov.justice.digital.hmpps.integrations.delius.contact.type.ContactTypeCode
import uk.gov.justice.digital.hmpps.integrations.delius.contact.type.ContactTypeRepository
import uk.gov.justice.digital.hmpps.integrations.delius.document.DocumentRepository
import uk.gov.justice.digital.hmpps.integrations.delius.location.OfficeLocationRepository
import uk.gov.justice.digital.hmpps.integrations.delius.nonstatutoryintervention.entity.NsiStatusCode
import uk.gov.justice.digital.hmpps.integrations.delius.nonstatutoryintervention.entity.NsiStatusRepository
Expand Down Expand Up @@ -84,7 +86,8 @@ class DataLoader(
private val referralRepository: ReferralRepository,
private val probationCaseDataLoader: ProbationCaseDataLoader,
private val lduRepository: LduRepository,
private val staffUserRepository: StaffUserRepository
private val staffUserRepository: StaffUserRepository,
private val documentRepository: DocumentRepository
) : ApplicationListener<ApplicationReadyEvent> {

@PostConstruct
Expand Down Expand Up @@ -192,6 +195,8 @@ class DataLoader(
eventRepository.save(ANOTHER_EVENT)
referralRepository.save(ReferralGenerator.EXISTING_REFERRAL)

documentRepository.save(DocumentGenerator.EVENT_DOC)

probationCaseDataLoader.loadData()
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package uk.gov.justice.digital.hmpps.data.generator

import uk.gov.justice.digital.hmpps.integrations.delius.approvedpremises.referral.entity.Event
import uk.gov.justice.digital.hmpps.integrations.delius.document.entity.DocEvent
import uk.gov.justice.digital.hmpps.integrations.delius.document.entity.DocumentType
import uk.gov.justice.digital.hmpps.integrations.delius.document.entity.EventDocument
import uk.gov.justice.digital.hmpps.integrations.delius.person.Person
import java.time.ZonedDateTime

object DocumentGenerator {
val EVENT_DOC = generateEventDoc()

fun generateEventDoc(
event: Event = PersonGenerator.ANOTHER_EVENT,
id: Long = IdGenerator.getAndIncrement()
): EventDocument {
val doc = EventDocument(event.toDocEvent())
doc.id = id
doc.person = PersonGenerator.DEFAULT
doc.name = "test.doc"
doc.primaryKeyId = doc.event?.id!!
doc.alfrescoId = "uuid1"
doc.lastSaved = ZonedDateTime.now().minusDays(7)
doc.dateProduced = null
doc.type = DocumentType.DOCUMENT
return doc
}

private fun Event.toDocEvent() =
DocEvent(id, Person(personId, "", false ), true, number, null, null)
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
package uk.gov.justice.digital.hmpps

import com.github.tomakehurst.wiremock.WireMockServer
import org.hamcrest.Matchers
import org.junit.jupiter.api.Test
import org.springframework.beans.factory.annotation.Autowired
import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc
import org.springframework.boot.test.context.SpringBootTest
import org.springframework.test.web.servlet.MockMvc
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.content
import org.springframework.test.web.servlet.result.MockMvcResultMatchers.header
import org.springframework.test.web.servlet.result.MockMvcResultMatchers.status
import org.springframework.util.ResourceUtils
import uk.gov.justice.digital.hmpps.data.generator.PersonGenerator
import uk.gov.justice.digital.hmpps.security.withOAuth2Token

@AutoConfigureMockMvc
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
internal class DocIntegrationTest {
@Autowired
lateinit var mockMvc: MockMvc

@Autowired
lateinit var wireMockserver: WireMockServer

@Test
fun `document is downloaded`() {
mockMvc.perform(
get("/document/A000001/uuid1").accept("application/octet-stream").withOAuth2Token(wireMockserver)
)
.andExpect(status().is2xxSuccessful)
.andExpect(header().string("Content-Type", "application/octet-stream"))
.andExpect(
header().string(
"Content-Disposition",
"attachment; filename=\"=?UTF-8?Q?test.doc?=\"; filename*=UTF-8''test.doc"
)
)
.andExpect(header().doesNotExist("Custom-Alfresco-Header"))
.andExpect(content().bytes(ResourceUtils.getFile("classpath:simulations/__files/document.pdf").readBytes()))
}

@Test
fun `list documents`() {
mockMvc.perform(get("/document/${PersonGenerator.DEFAULT.crn}/all").withOAuth2Token(wireMockserver))
.andExpect(status().isOk)
.andExpect(MockMvcResultMatchers.jsonPath("$[0].id",Matchers.equalTo("uuid1")))
.andExpect(MockMvcResultMatchers.jsonPath("$[0].level",Matchers.equalTo("Conviction")))
.andExpect(MockMvcResultMatchers.jsonPath("$[0].eventNumber",Matchers.equalTo("8")))
.andExpect(MockMvcResultMatchers.jsonPath("$[0].filename",Matchers.equalTo("test.doc")))
.andExpect(MockMvcResultMatchers.jsonPath("$[0].typeCode",Matchers.equalTo("EVENT")))
.andExpect(MockMvcResultMatchers.jsonPath("$[0].typeDescription",Matchers.equalTo("Event")))


}
}
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ class DocumentController(private val documentService: DocumentService) {
to a probation practitioner
"""
)
@GetMapping
@GetMapping(value = ["/all"])
fun findDocuments(@PathVariable crn: String): List<APDocument> =
documentService.getDocumentsByCrn(crn)
}
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ abstract class Document : Relatable {

@ManyToOne
@JoinColumn(name = "offender_id")
open val person: Person? = null
open var person: Person? = null

@Column(name = "primary_key_id")
open var primaryKeyId: Long = 0
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,10 @@ package uk.gov.justice.digital.hmpps.integrations.delius.document.entity

import java.time.ZonedDateTime

data class PersonDocument(
val id: String?,
val name: String,
val relatedTo: RelatedTo,
val dateSaved: ZonedDateTime?,
val dateCreated: ZonedDateTime?,
val sensitive: Boolean
)

data class APDocument(
val id: String?,
val level: String,
val eventNumber: String?,
val filename: String,
val typeCode: String,
val typeDescription: String,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,8 +51,9 @@ class DocumentService(
APDocument(
it.alfrescoId,
if (it.findRelatedTo().event == null) "Offender" else "Conviction",
it.findRelatedTo().event?.eventNumber,
it.name,
it.findRelatedTo().name,
it.findRelatedTo().type.name,
it.findRelatedTo().description,
it.createdDate,
it.lastSaved,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ internal class DocumentIntegrationTest {

@Test
fun `document is downloaded`() {
mockMvc.perform(get("/document/X000004/uuid1").accept("application/octet-stream").withOAuth2Token(wireMockserver))
mockMvc.perform(get("/document/A000001/uuid1").accept("application/octet-stream").withOAuth2Token(wireMockserver))
.andExpect(status().is2xxSuccessful)
.andExpect(header().string("Content-Type", "application/octet-stream"))
.andExpect(header().string("Content-Disposition", "attachment; filename=\"=?UTF-8?Q?doc1?=\"; filename*=UTF-8''doc1"))
Expand Down

0 comments on commit 79fd947

Please sign in to comment.