Skip to content
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

PI-2641 Add users endpoint for SAR service #4441

Merged
merged 1 commit into from
Nov 19, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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<Conviction>()

assertEquals(expectedResponse.convictionId, response.convictionId)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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<ConvictionRequirements>()

assertEquals(expectedResponse, response)
Expand Down Expand Up @@ -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))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -131,7 +130,6 @@ class CreateAppointmentIntegrationTests {
.withToken()
.withJson(createAppointment)
)
.andDo(print())
.andExpect(MockMvcResultMatchers.status().isCreated)
.andReturn().response.contentAsJson<AppointmentDetail>()

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<ApplicationReadyEvent> {

@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) }
}
}
Original file line number Diff line number Diff line change
@@ -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 {

Expand Down
Original file line number Diff line number Diff line change
@@ -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))
}
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -52,7 +51,6 @@ internal class GetPersonByCRNIntegrationTest {
val response = mockMvc
.perform(get("/probation-case/$crn").withToken())
.andExpect(status().isOk)
.andDo(print())
.andReturn().response.contentAsJson<Person>()

assertEquals(expectedResponse, response)
Expand Down
Original file line number Diff line number Diff line change
@@ -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
)
}
}
Original file line number Diff line number Diff line change
@@ -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))
}
}

This file was deleted.

Original file line number Diff line number Diff line change
@@ -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) }
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
package uk.gov.justice.digital.hmpps.api.model

data class User(
val username: String,
val lastName: String
)
Original file line number Diff line number Diff line change
@@ -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
Expand Down
Original file line number Diff line number Diff line change
@@ -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<User, Long>

This file was deleted.

Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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(
Expand All @@ -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))

Expand Down
Loading