diff --git a/projects/court-case-and-delius/src/integrationTest/kotlin/uk/gov/justice/digital/hmpps/ConvictionByCrnAndEventIdIntegrationTest.kt b/projects/court-case-and-delius/src/integrationTest/kotlin/uk/gov/justice/digital/hmpps/ConvictionByCrnAndEventIdIntegrationTest.kt index 1fade86124..6b9cff0a7d 100644 --- a/projects/court-case-and-delius/src/integrationTest/kotlin/uk/gov/justice/digital/hmpps/ConvictionByCrnAndEventIdIntegrationTest.kt +++ b/projects/court-case-and-delius/src/integrationTest/kotlin/uk/gov/justice/digital/hmpps/ConvictionByCrnAndEventIdIntegrationTest.kt @@ -10,7 +10,6 @@ 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.request.MockMvcRequestBuilders.get -import org.springframework.test.web.servlet.result.MockMvcResultHandlers.print import org.springframework.test.web.servlet.result.MockMvcResultMatchers.jsonPath import org.springframework.test.web.servlet.result.MockMvcResultMatchers.status import uk.gov.justice.digital.hmpps.api.model.Attendances @@ -256,7 +255,6 @@ internal class ConvictionByCrnAndEventIdIntegrationTest { val response = mockMvc .perform(get("/probation-case/$crn/convictions/${event.id}").withToken()) .andExpect(status().is2xxSuccessful) - .andDo(print()) .andReturn().response.contentAsJson() assertEquals(expectedResponse.convictionId, response.convictionId) diff --git a/projects/court-case-and-delius/src/integrationTest/kotlin/uk/gov/justice/digital/hmpps/RequirementsByEventIdIntegrationTest.kt b/projects/court-case-and-delius/src/integrationTest/kotlin/uk/gov/justice/digital/hmpps/RequirementsByEventIdIntegrationTest.kt index 8e35c2d223..4613303342 100644 --- a/projects/court-case-and-delius/src/integrationTest/kotlin/uk/gov/justice/digital/hmpps/RequirementsByEventIdIntegrationTest.kt +++ b/projects/court-case-and-delius/src/integrationTest/kotlin/uk/gov/justice/digital/hmpps/RequirementsByEventIdIntegrationTest.kt @@ -10,7 +10,6 @@ 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.request.MockMvcRequestBuilders.get -import org.springframework.test.web.servlet.result.MockMvcResultHandlers.print import org.springframework.test.web.servlet.result.MockMvcResultMatchers.jsonPath import org.springframework.test.web.servlet.result.MockMvcResultMatchers.status import uk.gov.justice.digital.hmpps.api.model.KeyValue @@ -89,7 +88,6 @@ internal class RequirementsByEventIdIntegrationTest { val response = mockMvc .perform(get("/probation-case/$crn/convictions/${event.id}/requirements").withToken()) .andExpect(status().isOk) - .andDo(print()) .andReturn().response.contentAsJson() assertEquals(expectedResponse, response) @@ -119,7 +117,6 @@ internal class RequirementsByEventIdIntegrationTest { mockMvc .perform(get("/probation-case/$crn/convictions/${event.id}/requirements?$requestParams").withToken()) .andExpect(status().isOk) - .andDo(print()) .andExpect(jsonPath("$.requirements.length()").value(1)) .andExpect(jsonPath("$.requirements[0].active").value(active)) .andExpect(jsonPath("$.requirements[0].softDeleted").value(deleted)) diff --git a/projects/manage-supervision-and-delius/src/integrationTest/kotlin/uk/gov/justice/digital/hmpps/CreateAppointmentIntegrationTests.kt b/projects/manage-supervision-and-delius/src/integrationTest/kotlin/uk/gov/justice/digital/hmpps/CreateAppointmentIntegrationTests.kt index d6943c367f..4a4e2f2020 100644 --- a/projects/manage-supervision-and-delius/src/integrationTest/kotlin/uk/gov/justice/digital/hmpps/CreateAppointmentIntegrationTests.kt +++ b/projects/manage-supervision-and-delius/src/integrationTest/kotlin/uk/gov/justice/digital/hmpps/CreateAppointmentIntegrationTests.kt @@ -11,7 +11,6 @@ import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMock import org.springframework.boot.test.context.SpringBootTest import org.springframework.test.web.servlet.MockMvc import org.springframework.test.web.servlet.request.MockMvcRequestBuilders.post -import org.springframework.test.web.servlet.result.MockMvcResultHandlers.print import org.springframework.test.web.servlet.result.MockMvcResultMatchers import org.springframework.test.web.servlet.result.MockMvcResultMatchers.jsonPath import uk.gov.justice.digital.hmpps.api.model.appointment.AppointmentDetail @@ -131,7 +130,6 @@ class CreateAppointmentIntegrationTests { .withToken() .withJson(createAppointment) ) - .andDo(print()) .andExpect(MockMvcResultMatchers.status().isCreated) .andReturn().response.contentAsJson() diff --git a/projects/subject-access-requests-and-delius/src/dev/kotlin/uk/gov/justice/digital/hmpps/data/DataLoader.kt b/projects/subject-access-requests-and-delius/src/dev/kotlin/uk/gov/justice/digital/hmpps/data/DataLoader.kt index 6480ea934f..5d38ef190a 100644 --- a/projects/subject-access-requests-and-delius/src/dev/kotlin/uk/gov/justice/digital/hmpps/data/DataLoader.kt +++ b/projects/subject-access-requests-and-delius/src/dev/kotlin/uk/gov/justice/digital/hmpps/data/DataLoader.kt @@ -9,26 +9,27 @@ import org.springframework.stereotype.Component import org.springframework.transaction.annotation.Transactional import uk.gov.justice.digital.hmpps.data.generator.PersonGenerator import uk.gov.justice.digital.hmpps.data.generator.UserGenerator -import uk.gov.justice.digital.hmpps.user.AuditUserRepository +import uk.gov.justice.digital.hmpps.entity.UserRepository @Component @ConditionalOnProperty("seed.database") class DataLoader( - private val auditUserRepository: AuditUserRepository, - private val em: EntityManager + private val userRepository: UserRepository, + private val entityManager: EntityManager ) : ApplicationListener { @PostConstruct fun saveAuditUser() { - auditUserRepository.save(UserGenerator.AUDIT_USER) + userRepository.save(UserGenerator.AUDIT_USER) } @Transactional override fun onApplicationEvent(are: ApplicationReadyEvent) { - em.persistAll(PersonGenerator.PERSON1) - } - - private fun EntityManager.persistAll(vararg entities: Any) { - entities.forEach { persist(it) } + listOf( + PersonGenerator.PERSON1, + UserGenerator.USER1, + UserGenerator.USER2, + UserGenerator.INACTIVE_USER, + ).forEach { entityManager.persist(it) } } } diff --git a/projects/subject-access-requests-and-delius/src/dev/kotlin/uk/gov/justice/digital/hmpps/data/generator/PersonGenerator.kt b/projects/subject-access-requests-and-delius/src/dev/kotlin/uk/gov/justice/digital/hmpps/data/generator/PersonGenerator.kt index 7f83c4fbe0..8a047f1a59 100644 --- a/projects/subject-access-requests-and-delius/src/dev/kotlin/uk/gov/justice/digital/hmpps/data/generator/PersonGenerator.kt +++ b/projects/subject-access-requests-and-delius/src/dev/kotlin/uk/gov/justice/digital/hmpps/data/generator/PersonGenerator.kt @@ -1,6 +1,6 @@ package uk.gov.justice.digital.hmpps.data.generator -import uk.gov.justice.digital.hmpps.integration.delius.person.entity.Person +import uk.gov.justice.digital.hmpps.entity.Person object PersonGenerator { diff --git a/projects/subject-access-requests-and-delius/src/dev/kotlin/uk/gov/justice/digital/hmpps/data/generator/UserGenerator.kt b/projects/subject-access-requests-and-delius/src/dev/kotlin/uk/gov/justice/digital/hmpps/data/generator/UserGenerator.kt index 3722486f4f..d128875464 100644 --- a/projects/subject-access-requests-and-delius/src/dev/kotlin/uk/gov/justice/digital/hmpps/data/generator/UserGenerator.kt +++ b/projects/subject-access-requests-and-delius/src/dev/kotlin/uk/gov/justice/digital/hmpps/data/generator/UserGenerator.kt @@ -1,7 +1,12 @@ package uk.gov.justice.digital.hmpps.data.generator -import uk.gov.justice.digital.hmpps.user.AuditUser +import uk.gov.justice.digital.hmpps.entity.User +import java.time.LocalDate object UserGenerator { - val AUDIT_USER = AuditUser(IdGenerator.getAndIncrement(), "SubjectAccessRequestsAndDelius") + val AUDIT_USER = User(IdGenerator.getAndIncrement(), "SubjectAccessRequestsAndDelius", "Service") + + val USER1 = User(IdGenerator.getAndIncrement(), "username1", "surname1") + val USER2 = User(IdGenerator.getAndIncrement(), "username2", "surname2") + val INACTIVE_USER = User(IdGenerator.getAndIncrement(), "inactive", "inactive", LocalDate.now().minusDays(1)) } diff --git a/projects/subject-access-requests-and-delius/src/integrationTest/kotlin/uk/gov/justice/digital/hmpps/GetPersonByCRNIntegrationTest.kt b/projects/subject-access-requests-and-delius/src/integrationTest/kotlin/uk/gov/justice/digital/hmpps/GetPersonByCRNIntegrationTest.kt index 464c4fa154..fca93c1815 100644 --- a/projects/subject-access-requests-and-delius/src/integrationTest/kotlin/uk/gov/justice/digital/hmpps/GetPersonByCRNIntegrationTest.kt +++ b/projects/subject-access-requests-and-delius/src/integrationTest/kotlin/uk/gov/justice/digital/hmpps/GetPersonByCRNIntegrationTest.kt @@ -9,7 +9,6 @@ import org.springframework.boot.test.context.SpringBootTest.WebEnvironment.RANDO import org.springframework.boot.test.mock.mockito.MockBean import org.springframework.test.web.servlet.MockMvc import org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get -import org.springframework.test.web.servlet.result.MockMvcResultHandlers.print import org.springframework.test.web.servlet.result.MockMvcResultMatchers.jsonPath import org.springframework.test.web.servlet.result.MockMvcResultMatchers.status import uk.gov.justice.digital.hmpps.api.model.Name @@ -52,7 +51,6 @@ internal class GetPersonByCRNIntegrationTest { val response = mockMvc .perform(get("/probation-case/$crn").withToken()) .andExpect(status().isOk) - .andDo(print()) .andReturn().response.contentAsJson() assertEquals(expectedResponse, response) diff --git a/projects/subject-access-requests-and-delius/src/integrationTest/kotlin/uk/gov/justice/digital/hmpps/UserIntegrationTest.kt b/projects/subject-access-requests-and-delius/src/integrationTest/kotlin/uk/gov/justice/digital/hmpps/UserIntegrationTest.kt new file mode 100644 index 0000000000..b796306e8f --- /dev/null +++ b/projects/subject-access-requests-and-delius/src/integrationTest/kotlin/uk/gov/justice/digital/hmpps/UserIntegrationTest.kt @@ -0,0 +1,34 @@ +package uk.gov.justice.digital.hmpps + +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.boot.test.context.SpringBootTest.WebEnvironment.RANDOM_PORT +import org.springframework.test.web.servlet.MockMvc +import org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get +import org.springframework.test.web.servlet.result.MockMvcResultMatchers.status +import uk.gov.justice.digital.hmpps.api.model.User +import uk.gov.justice.digital.hmpps.test.MockMvcExtensions.andExpectJson +import uk.gov.justice.digital.hmpps.test.MockMvcExtensions.withToken + +@AutoConfigureMockMvc +@SpringBootTest(webEnvironment = RANDOM_PORT) +internal class UserIntegrationTest { + @Autowired + lateinit var mockMvc: MockMvc + + @Test + fun `returns data`() { + mockMvc + .perform(get("/user").withToken()) + .andExpect(status().isOk) + .andExpectJson( + listOf( + User("SubjectAccessRequestsAndDelius", "Service"), + User("username1", "surname1"), + User("username2", "surname2") + ), strict = true + ) + } +} diff --git a/projects/subject-access-requests-and-delius/src/main/kotlin/uk/gov/justice/digital/hmpps/api/controller/ProbationCaseController.kt b/projects/subject-access-requests-and-delius/src/main/kotlin/uk/gov/justice/digital/hmpps/api/controller/ProbationCaseController.kt new file mode 100644 index 0000000000..ea6e25bac9 --- /dev/null +++ b/projects/subject-access-requests-and-delius/src/main/kotlin/uk/gov/justice/digital/hmpps/api/controller/ProbationCaseController.kt @@ -0,0 +1,20 @@ +package uk.gov.justice.digital.hmpps.api.controller + +import org.springframework.security.access.prepost.PreAuthorize +import org.springframework.web.bind.annotation.GetMapping +import org.springframework.web.bind.annotation.PathVariable +import org.springframework.web.bind.annotation.RestController +import uk.gov.justice.digital.hmpps.api.model.Name +import uk.gov.justice.digital.hmpps.api.model.Person +import uk.gov.justice.digital.hmpps.entity.PersonRepository +import uk.gov.justice.digital.hmpps.entity.getPerson + +@RestController +@PreAuthorize("hasRole('PROBATION_API__SUBJECT_ACCESS_REQUEST__DETAIL')") +class ProbationCaseController(private val personRepository: PersonRepository) { + @GetMapping("/probation-case/{crn}") + fun getPersonalDetails(@PathVariable crn: String) = with(personRepository.getPerson(crn)) { + val middleName = listOfNotNull(secondName, thirdName).takeIf { it.isNotEmpty() }?.joinToString(" ") + Person(Name(forename, middleName, surname)) + } +} diff --git a/projects/subject-access-requests-and-delius/src/main/kotlin/uk/gov/justice/digital/hmpps/api/controller/SubjectAccessRequestsController.kt b/projects/subject-access-requests-and-delius/src/main/kotlin/uk/gov/justice/digital/hmpps/api/controller/SubjectAccessRequestsController.kt deleted file mode 100644 index 60b2ebb4c6..0000000000 --- a/projects/subject-access-requests-and-delius/src/main/kotlin/uk/gov/justice/digital/hmpps/api/controller/SubjectAccessRequestsController.kt +++ /dev/null @@ -1,17 +0,0 @@ -package uk.gov.justice.digital.hmpps.api.controller - -import org.springframework.security.access.prepost.PreAuthorize -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.service.SubjectAccessRequestsService - -@RestController -@RequestMapping("probation-case/{crn}") -@PreAuthorize("hasRole('PROBATION_API__SUBJECT_ACCESS_REQUEST__DETAIL')") -class SubjectAccessRequestsController(private val subjectAccessRequestsService: SubjectAccessRequestsService) { - - @GetMapping - fun getPersonalDetails(@PathVariable crn: String) = subjectAccessRequestsService.getPersonDetailsByCrn(crn) -} diff --git a/projects/subject-access-requests-and-delius/src/main/kotlin/uk/gov/justice/digital/hmpps/api/controller/UserController.kt b/projects/subject-access-requests-and-delius/src/main/kotlin/uk/gov/justice/digital/hmpps/api/controller/UserController.kt new file mode 100644 index 0000000000..8a308f6971 --- /dev/null +++ b/projects/subject-access-requests-and-delius/src/main/kotlin/uk/gov/justice/digital/hmpps/api/controller/UserController.kt @@ -0,0 +1,14 @@ +package uk.gov.justice.digital.hmpps.api.controller + +import org.springframework.security.access.prepost.PreAuthorize +import org.springframework.web.bind.annotation.GetMapping +import org.springframework.web.bind.annotation.RestController +import uk.gov.justice.digital.hmpps.api.model.User +import uk.gov.justice.digital.hmpps.entity.UserRepository + +@RestController +@PreAuthorize("hasRole('PROBATION_API__SUBJECT_ACCESS_REQUEST__DETAIL')") +class UserController(private val userRepository: UserRepository) { + @GetMapping("/user") + fun getUsers() = userRepository.findAll().map { User(it.username, it.surname) } +} diff --git a/projects/subject-access-requests-and-delius/src/main/kotlin/uk/gov/justice/digital/hmpps/api/model/User.kt b/projects/subject-access-requests-and-delius/src/main/kotlin/uk/gov/justice/digital/hmpps/api/model/User.kt new file mode 100644 index 0000000000..d9dc7a71fa --- /dev/null +++ b/projects/subject-access-requests-and-delius/src/main/kotlin/uk/gov/justice/digital/hmpps/api/model/User.kt @@ -0,0 +1,6 @@ +package uk.gov.justice.digital.hmpps.api.model + +data class User( + val username: String, + val lastName: String +) \ No newline at end of file diff --git a/projects/subject-access-requests-and-delius/src/main/kotlin/uk/gov/justice/digital/hmpps/integration/delius/person/entity/Person.kt b/projects/subject-access-requests-and-delius/src/main/kotlin/uk/gov/justice/digital/hmpps/entity/Person.kt similarity index 94% rename from projects/subject-access-requests-and-delius/src/main/kotlin/uk/gov/justice/digital/hmpps/integration/delius/person/entity/Person.kt rename to projects/subject-access-requests-and-delius/src/main/kotlin/uk/gov/justice/digital/hmpps/entity/Person.kt index af42c1a859..b61669bec3 100644 --- a/projects/subject-access-requests-and-delius/src/main/kotlin/uk/gov/justice/digital/hmpps/integration/delius/person/entity/Person.kt +++ b/projects/subject-access-requests-and-delius/src/main/kotlin/uk/gov/justice/digital/hmpps/entity/Person.kt @@ -1,4 +1,4 @@ -package uk.gov.justice.digital.hmpps.integration.delius.person.entity +package uk.gov.justice.digital.hmpps.entity import jakarta.persistence.Column import jakarta.persistence.Entity diff --git a/projects/subject-access-requests-and-delius/src/main/kotlin/uk/gov/justice/digital/hmpps/entity/User.kt b/projects/subject-access-requests-and-delius/src/main/kotlin/uk/gov/justice/digital/hmpps/entity/User.kt new file mode 100644 index 0000000000..a11d9bb967 --- /dev/null +++ b/projects/subject-access-requests-and-delius/src/main/kotlin/uk/gov/justice/digital/hmpps/entity/User.kt @@ -0,0 +1,31 @@ +package uk.gov.justice.digital.hmpps.entity + +import jakarta.persistence.Column +import jakarta.persistence.Entity +import jakarta.persistence.Id +import jakarta.persistence.Table +import org.hibernate.annotations.Immutable +import org.hibernate.annotations.SQLRestriction +import org.springframework.data.jpa.repository.JpaRepository +import java.time.LocalDate + +@Entity +@Immutable +@Table(name = "user_") +@SQLRestriction("end_date is null or end_date > current_date") +class User( + @Id + @Column(name = "user_id") + val id: Long, + + @Column(name = "distinguished_name") + val username: String, + + @Column(name = "surname") + val surname: String, + + @Column + val endDate: LocalDate? = null +) + +interface UserRepository : JpaRepository diff --git a/projects/subject-access-requests-and-delius/src/main/kotlin/uk/gov/justice/digital/hmpps/service/SubjectAccessRequestsService.kt b/projects/subject-access-requests-and-delius/src/main/kotlin/uk/gov/justice/digital/hmpps/service/SubjectAccessRequestsService.kt deleted file mode 100644 index 5771f2094e..0000000000 --- a/projects/subject-access-requests-and-delius/src/main/kotlin/uk/gov/justice/digital/hmpps/service/SubjectAccessRequestsService.kt +++ /dev/null @@ -1,27 +0,0 @@ -package uk.gov.justice.digital.hmpps.service - -import org.springframework.stereotype.Service -import uk.gov.justice.digital.hmpps.api.model.Name -import uk.gov.justice.digital.hmpps.integration.delius.person.entity.PersonRepository -import uk.gov.justice.digital.hmpps.integration.delius.person.entity.getPerson -import uk.gov.justice.digital.hmpps.integration.delius.person.entity.Person as PersonEntity -import uk.gov.justice.digital.hmpps.api.model.Person - -@Service -class SubjectAccessRequestsService(private val personRepository: PersonRepository) { - - fun getPersonDetailsByCrn(crn: String): Person { - return personRepository.getPerson(crn).toPerson() - } -} - -fun PersonEntity.toPerson(): Person { - val middleName = listOfNotNull(secondName, thirdName).let { - when (it.size) { - 0 -> null - else -> it.joinToString(" ") - } - } - - return Person(Name(forename, middleName, surname)) -} diff --git a/projects/subject-access-requests-and-delius/src/test/kotlin/uk/gov/justice/digital/hmpps/service/SubjectAccessRequestsServiceTest.kt b/projects/subject-access-requests-and-delius/src/test/kotlin/uk/gov/justice/digital/hmpps/api/controller/ProbationCaseControllerTest.kt similarity index 78% rename from projects/subject-access-requests-and-delius/src/test/kotlin/uk/gov/justice/digital/hmpps/service/SubjectAccessRequestsServiceTest.kt rename to projects/subject-access-requests-and-delius/src/test/kotlin/uk/gov/justice/digital/hmpps/api/controller/ProbationCaseControllerTest.kt index 622b9c63d8..f588b6a395 100644 --- a/projects/subject-access-requests-and-delius/src/test/kotlin/uk/gov/justice/digital/hmpps/service/SubjectAccessRequestsServiceTest.kt +++ b/projects/subject-access-requests-and-delius/src/test/kotlin/uk/gov/justice/digital/hmpps/api/controller/ProbationCaseControllerTest.kt @@ -1,4 +1,4 @@ -package uk.gov.justice.digital.hmpps.service +package uk.gov.justice.digital.hmpps.api.controller import org.junit.jupiter.api.Assertions.assertEquals import org.junit.jupiter.api.extension.ExtendWith @@ -11,19 +11,17 @@ import org.mockito.kotlin.whenever import uk.gov.justice.digital.hmpps.api.model.Name import uk.gov.justice.digital.hmpps.api.model.Person import uk.gov.justice.digital.hmpps.data.generator.PersonGenerator -import uk.gov.justice.digital.hmpps.integration.delius.person.entity.Person as PersonEntity -import uk.gov.justice.digital.hmpps.integration.delius.person.entity.PersonRepository +import uk.gov.justice.digital.hmpps.entity.PersonRepository +import uk.gov.justice.digital.hmpps.entity.Person as PersonEntity @ExtendWith(MockitoExtension::class) -class SubjectAccessRequestsServiceTest { +class ProbationCaseControllerTest { @Mock lateinit var personRepository: PersonRepository @InjectMocks - lateinit var subjectAccessRequestsService: SubjectAccessRequestsService - - val person1 = PersonGenerator.PERSON1 + lateinit var probationCaseController: ProbationCaseController @ParameterizedTest @CsvSource( @@ -36,7 +34,7 @@ class SubjectAccessRequestsServiceTest { whenever(personRepository.findByCrn(crn)).thenReturn(getPerson(person)) - val response = subjectAccessRequestsService.getPersonDetailsByCrn(crn) + val response = probationCaseController.getPersonalDetails(crn) val expectedResponse = Person(getName(fullName))