diff --git a/projects/resettlement-passport-and-delius/src/dev/kotlin/uk/gov/justice/digital/hmpps/data/DataLoader.kt b/projects/resettlement-passport-and-delius/src/dev/kotlin/uk/gov/justice/digital/hmpps/data/DataLoader.kt index eb9452d89e..c836b74549 100644 --- a/projects/resettlement-passport-and-delius/src/dev/kotlin/uk/gov/justice/digital/hmpps/data/DataLoader.kt +++ b/projects/resettlement-passport-and-delius/src/dev/kotlin/uk/gov/justice/digital/hmpps/data/DataLoader.kt @@ -16,8 +16,11 @@ import uk.gov.justice.digital.hmpps.data.generator.NSITypeGenerator import uk.gov.justice.digital.hmpps.data.generator.PersonGenerator import uk.gov.justice.digital.hmpps.data.generator.ProviderGenerator import uk.gov.justice.digital.hmpps.data.generator.ReferenceDataGenerator +import uk.gov.justice.digital.hmpps.data.generator.RegistrationGenerator import uk.gov.justice.digital.hmpps.data.generator.UserGenerator import uk.gov.justice.digital.hmpps.datetime.EuropeLondon +import uk.gov.justice.digital.hmpps.entity.Category +import uk.gov.justice.digital.hmpps.entity.Level import uk.gov.justice.digital.hmpps.entity.Person import uk.gov.justice.digital.hmpps.user.AuditUserRepository import java.time.LocalDate @@ -48,6 +51,7 @@ class DataLoader( ProviderGenerator.DEFAULT_STAFF, ProviderGenerator.DEFAULT_STAFF_USER, PersonGenerator.DEFAULT, + PersonGenerator.DEFAULT_MANAGER, NSIGenerator.DEFAULT, NSIManagerGenerator.DEFAULT, AddressGenerator.DEFAULT, @@ -55,7 +59,19 @@ class DataLoader( AppointmentGenerator.NON_ATTENDANCE_TYPE, AppointmentGenerator.ATTENDED_OUTCOME, AppointmentGenerator.NON_ATTENDED_OUTCOME, - AppointmentGenerator.DEFAULT_LOCATION + AppointmentGenerator.DEFAULT_LOCATION, + RegistrationGenerator.MAPPA_TYPE + ) + + RegistrationGenerator.CATEGORIES.values.forEach { em.persist(it) } + RegistrationGenerator.LEVELS.values.forEach { em.persist(it) } + em.persist( + RegistrationGenerator.generate( + date = LocalDate.now().minusDays(30), + category = RegistrationGenerator.CATEGORIES[Category.M1.name], + level = RegistrationGenerator.LEVELS[Level.M2.name], + reviewDate = LocalDate.now().plusDays(60) + ) ) createAppointments(PersonGenerator.DEFAULT) diff --git a/projects/resettlement-passport-and-delius/src/dev/kotlin/uk/gov/justice/digital/hmpps/data/generator/PersonGenerator.kt b/projects/resettlement-passport-and-delius/src/dev/kotlin/uk/gov/justice/digital/hmpps/data/generator/PersonGenerator.kt index 1e6b91c0d9..3018e6e5b3 100644 --- a/projects/resettlement-passport-and-delius/src/dev/kotlin/uk/gov/justice/digital/hmpps/data/generator/PersonGenerator.kt +++ b/projects/resettlement-passport-and-delius/src/dev/kotlin/uk/gov/justice/digital/hmpps/data/generator/PersonGenerator.kt @@ -1,15 +1,31 @@ package uk.gov.justice.digital.hmpps.data.generator import uk.gov.justice.digital.hmpps.entity.Person +import uk.gov.justice.digital.hmpps.entity.PersonManager +import uk.gov.justice.digital.hmpps.entity.Staff object PersonGenerator { - val DEFAULT = generate("X123123") + val DEFAULT = generate("X123123", "A1234YZ") + val DEFAULT_MANAGER = generateManager(staff = ProviderGenerator.DEFAULT_STAFF) - fun generate(crn: String, noms: String? = null, softDeleted: Boolean = false, id: Long = IdGenerator.getAndIncrement()) = + fun generate( + crn: String, + noms: String? = null, + softDeleted: Boolean = false, + id: Long = IdGenerator.getAndIncrement() + ) = Person( id, crn, noms, softDeleted ) + + fun generateManager( + person: Person = PersonGenerator.DEFAULT, + staff: Staff, + active: Boolean = true, + softDeleted: Boolean = false, + id: Long = IdGenerator.getAndIncrement() + ) = PersonManager(person, staff, softDeleted, active, id) } diff --git a/projects/resettlement-passport-and-delius/src/dev/kotlin/uk/gov/justice/digital/hmpps/data/generator/RegistrationGenerator.kt b/projects/resettlement-passport-and-delius/src/dev/kotlin/uk/gov/justice/digital/hmpps/data/generator/RegistrationGenerator.kt new file mode 100644 index 0000000000..b0ae925584 --- /dev/null +++ b/projects/resettlement-passport-and-delius/src/dev/kotlin/uk/gov/justice/digital/hmpps/data/generator/RegistrationGenerator.kt @@ -0,0 +1,29 @@ +package uk.gov.justice.digital.hmpps.data.generator + +import uk.gov.justice.digital.hmpps.entity.Category +import uk.gov.justice.digital.hmpps.entity.Level +import uk.gov.justice.digital.hmpps.entity.Person +import uk.gov.justice.digital.hmpps.entity.ReferenceData +import uk.gov.justice.digital.hmpps.entity.RegisterType +import uk.gov.justice.digital.hmpps.entity.Registration +import java.time.LocalDate + +object RegistrationGenerator { + val MAPPA_TYPE = generateType("MAPP") + val CATEGORIES = Category.entries.map { ReferenceDataGenerator.generate(it.name) }.associateBy { it.code } + val LEVELS = Level.entries.map { ReferenceDataGenerator.generate(it.name) }.associateBy { it.code } + + fun generateType(code: String, id: Long = IdGenerator.getAndIncrement()) = RegisterType(id, code) + + fun generate( + type: RegisterType = MAPPA_TYPE, + category: ReferenceData? = null, + level: ReferenceData? = null, + date: LocalDate = LocalDate.now(), + reviewDate: LocalDate? = null, + person: Person = PersonGenerator.DEFAULT, + deRegistered: Boolean = false, + softDeleted: Boolean = false, + id: Long = IdGenerator.getAndIncrement() + ) = Registration(person, type, category, level, date, reviewDate, deRegistered, softDeleted, id) +} diff --git a/projects/resettlement-passport-and-delius/src/integrationTest/kotlin/uk/gov/justice/digital/hmpps/CaseDetailIntegrationTests.kt b/projects/resettlement-passport-and-delius/src/integrationTest/kotlin/uk/gov/justice/digital/hmpps/CaseDetailIntegrationTests.kt new file mode 100644 index 0000000000..2935c78a51 --- /dev/null +++ b/projects/resettlement-passport-and-delius/src/integrationTest/kotlin/uk/gov/justice/digital/hmpps/CaseDetailIntegrationTests.kt @@ -0,0 +1,84 @@ +package uk.gov.justice.digital.hmpps + +import com.fasterxml.jackson.databind.ObjectMapper +import com.fasterxml.jackson.module.kotlin.readValue +import com.github.tomakehurst.wiremock.WireMockServer +import org.hamcrest.MatcherAssert.assertThat +import org.hamcrest.Matchers.equalTo +import org.junit.jupiter.api.Assertions.assertFalse +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 +import org.springframework.test.web.servlet.result.MockMvcResultMatchers +import uk.gov.justice.digital.hmpps.api.model.CaseIdentifiers +import uk.gov.justice.digital.hmpps.api.model.Manager +import uk.gov.justice.digital.hmpps.api.model.MappaDetail +import uk.gov.justice.digital.hmpps.api.model.Name +import uk.gov.justice.digital.hmpps.data.generator.PersonGenerator +import uk.gov.justice.digital.hmpps.data.generator.ProviderGenerator +import uk.gov.justice.digital.hmpps.security.withOAuth2Token +import java.time.LocalDate + +@AutoConfigureMockMvc +@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT) +class CaseDetailIntegrationTests { + @Autowired + lateinit var mockMvc: MockMvc + + @Autowired + lateinit var wireMockServer: WireMockServer + + @Autowired + lateinit var objectMapper: ObjectMapper + + @Test + fun `crn is correctly returned when noms id present in delius`() { + val result = mockMvc.perform( + MockMvcRequestBuilders.get("/probation-cases/${PersonGenerator.DEFAULT.noms}/crn") + .withOAuth2Token(wireMockServer) + ).andExpect(MockMvcResultMatchers.status().is2xxSuccessful).andReturn().response + + val identifiers = objectMapper.readValue(result.contentAsString) + assertThat(identifiers.crn, equalTo(PersonGenerator.DEFAULT.crn)) + } + + @Test + fun `not found returned when noms id not present in delius`() { + mockMvc.perform( + MockMvcRequestBuilders.get("/probation-cases/N4567FD/crn") + .withOAuth2Token(wireMockServer) + ).andExpect(MockMvcResultMatchers.status().isNotFound) + } + + @Test + fun `mappa detail is returned when available`() { + val result = mockMvc.perform( + MockMvcRequestBuilders.get("/probation-cases/${PersonGenerator.DEFAULT.crn}/mappa") + .withOAuth2Token(wireMockServer) + ).andExpect(MockMvcResultMatchers.status().is2xxSuccessful).andReturn().response + + val mappa = objectMapper.readValue(result.contentAsString) + assertThat(mappa.category, equalTo(1)) + assertThat(mappa.level, equalTo(2)) + assertThat(mappa.startDate, equalTo(LocalDate.now().minusDays(30))) + assertThat(mappa.reviewDate, equalTo(LocalDate.now().plusDays(60))) + } + + @Test + fun `community manager is returned`() { + val result = mockMvc.perform( + MockMvcRequestBuilders.get("/probation-cases/${PersonGenerator.DEFAULT.crn}/community-manager") + .withOAuth2Token(wireMockServer) + ).andExpect(MockMvcResultMatchers.status().is2xxSuccessful).andReturn().response + + val manager = objectMapper.readValue(result.contentAsString) + assertThat( + manager.name, + equalTo(Name(ProviderGenerator.DEFAULT_STAFF.forename, ProviderGenerator.DEFAULT_STAFF.surname)) + ) + assertFalse(manager.unallocated) + } +} diff --git a/projects/resettlement-passport-and-delius/src/main/kotlin/uk/gov/justice/digital/hmpps/api/controller/CaseDetailController.kt b/projects/resettlement-passport-and-delius/src/main/kotlin/uk/gov/justice/digital/hmpps/api/controller/CaseDetailController.kt new file mode 100644 index 0000000000..665a920aa3 --- /dev/null +++ b/projects/resettlement-passport-and-delius/src/main/kotlin/uk/gov/justice/digital/hmpps/api/controller/CaseDetailController.kt @@ -0,0 +1,27 @@ +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.api.model.CaseIdentifiers +import uk.gov.justice.digital.hmpps.api.model.MappaDetail +import uk.gov.justice.digital.hmpps.service.CaseDetailService + +@RestController +@RequestMapping("/probation-cases") +class CaseDetailController(private val caseDetailService: CaseDetailService) { + + @PreAuthorize("hasRole('RESETTLEMENT_PASSPORT_SUPERVISION_ACCESS')") + @GetMapping("/{nomsId}/crn") + fun findCrn(@PathVariable nomsId: String): CaseIdentifiers = caseDetailService.findCrnByNomsId(nomsId) + + @PreAuthorize("hasRole('RESETTLEMENT_PASSPORT_SUPERVISION_ACCESS')") + @GetMapping("/{crn}/mappa") + fun findMappaInfo(@PathVariable crn: String): MappaDetail = caseDetailService.findMappaDetail(crn) + + @PreAuthorize("hasRole('RESETTLEMENT_PASSPORT_SUPERVISION_ACCESS')") + @GetMapping("/{crn}/community-manager") + fun getCommunityManager(@PathVariable crn: String) = caseDetailService.findCommunityManager(crn) +} diff --git a/projects/resettlement-passport-and-delius/src/main/kotlin/uk/gov/justice/digital/hmpps/api/model/CaseDetail.kt b/projects/resettlement-passport-and-delius/src/main/kotlin/uk/gov/justice/digital/hmpps/api/model/CaseDetail.kt new file mode 100644 index 0000000000..4441c61e2a --- /dev/null +++ b/projects/resettlement-passport-and-delius/src/main/kotlin/uk/gov/justice/digital/hmpps/api/model/CaseDetail.kt @@ -0,0 +1,21 @@ +package uk.gov.justice.digital.hmpps.api.model + +import java.time.LocalDate + +data class CaseIdentifiers( + val crn: String +) + +data class MappaDetail( + val level: Int?, + val levelDescription: String?, + val category: Int?, + val categoryDescription: String?, + val startDate: LocalDate, + val reviewDate: LocalDate? +) + +data class Manager( + val name: Name?, + val unallocated: Boolean +) diff --git a/projects/resettlement-passport-and-delius/src/main/kotlin/uk/gov/justice/digital/hmpps/entity/PersonAddress.kt b/projects/resettlement-passport-and-delius/src/main/kotlin/uk/gov/justice/digital/hmpps/entity/PersonAddress.kt index e15ccbced5..4ff593a646 100644 --- a/projects/resettlement-passport-and-delius/src/main/kotlin/uk/gov/justice/digital/hmpps/entity/PersonAddress.kt +++ b/projects/resettlement-passport-and-delius/src/main/kotlin/uk/gov/justice/digital/hmpps/entity/PersonAddress.kt @@ -1,4 +1,5 @@ package uk.gov.justice.digital.hmpps.entity + import jakarta.persistence.Column import jakarta.persistence.Convert import jakarta.persistence.Entity @@ -72,3 +73,8 @@ interface PersonAddressRepository : JpaRepository { ) fun getMainAddressByPersonId(personId: Long): PersonAddress? } + +interface PersonRepository : JpaRepository { + @Query("select p.crn from Person p where p.noms = :nomsId") + fun findCrnByNomsId(nomsId: String): String? +} diff --git a/projects/resettlement-passport-and-delius/src/main/kotlin/uk/gov/justice/digital/hmpps/entity/PersonManager.kt b/projects/resettlement-passport-and-delius/src/main/kotlin/uk/gov/justice/digital/hmpps/entity/PersonManager.kt new file mode 100644 index 0000000000..5769080888 --- /dev/null +++ b/projects/resettlement-passport-and-delius/src/main/kotlin/uk/gov/justice/digital/hmpps/entity/PersonManager.kt @@ -0,0 +1,40 @@ +package uk.gov.justice.digital.hmpps.entity + +import jakarta.persistence.Column +import jakarta.persistence.Entity +import jakarta.persistence.Id +import jakarta.persistence.JoinColumn +import jakarta.persistence.ManyToOne +import jakarta.persistence.Table +import org.hibernate.annotations.Immutable +import org.hibernate.annotations.Where +import org.springframework.data.jpa.repository.JpaRepository + +@Immutable +@Table(name = "offender_manager") +@Entity +@Where(clause = "soft_deleted = 0 and active_flag = 1") +class PersonManager( + + @ManyToOne + @JoinColumn(name = "offender_id") + val person: Person, + + @ManyToOne + @JoinColumn(name = "allocation_staff_id") + val staff: Staff, + + @Column(name = "soft_deleted", columnDefinition = "number") + val softDeleted: Boolean, + + @Column(name = "active_flag", columnDefinition = "number") + val active: Boolean, + + @Id + @Column(name = "offender_manager_id") + val id: Long +) + +interface PersonManagerRepository : JpaRepository { + fun findByPersonCrn(crn: String): PersonManager? +} diff --git a/projects/resettlement-passport-and-delius/src/main/kotlin/uk/gov/justice/digital/hmpps/entity/ReferenceData.kt b/projects/resettlement-passport-and-delius/src/main/kotlin/uk/gov/justice/digital/hmpps/entity/ReferenceData.kt index 5b20208f54..595fe894bd 100644 --- a/projects/resettlement-passport-and-delius/src/main/kotlin/uk/gov/justice/digital/hmpps/entity/ReferenceData.kt +++ b/projects/resettlement-passport-and-delius/src/main/kotlin/uk/gov/justice/digital/hmpps/entity/ReferenceData.kt @@ -21,3 +21,6 @@ class ReferenceData( @Column(name = "standard_reference_list_id") val id: Long ) + +enum class Category(val number: Int) { X9(0), M1(1), M2(2), M3(3), M4(4) } +enum class Level(val number: Int) { M0(0), M1(1), M2(2), M3(3) } diff --git a/projects/resettlement-passport-and-delius/src/main/kotlin/uk/gov/justice/digital/hmpps/entity/Registration.kt b/projects/resettlement-passport-and-delius/src/main/kotlin/uk/gov/justice/digital/hmpps/entity/Registration.kt new file mode 100644 index 0000000000..90a6505e86 --- /dev/null +++ b/projects/resettlement-passport-and-delius/src/main/kotlin/uk/gov/justice/digital/hmpps/entity/Registration.kt @@ -0,0 +1,77 @@ +package uk.gov.justice.digital.hmpps.entity + +import jakarta.persistence.Column +import jakarta.persistence.Entity +import jakarta.persistence.Id +import jakarta.persistence.JoinColumn +import jakarta.persistence.ManyToOne +import jakarta.persistence.Table +import org.hibernate.annotations.Immutable +import org.hibernate.annotations.Where +import org.springframework.data.jpa.repository.EntityGraph +import org.springframework.data.jpa.repository.JpaRepository +import java.time.LocalDate + +@Immutable +@Entity +@Where(clause = "soft_deleted = 0 and deregistered = 0") +@Table(name = "registration") +class Registration( + + @ManyToOne + @JoinColumn(name = "offender_id") + val person: Person, + + @ManyToOne + @JoinColumn(name = "register_type_id") + val type: RegisterType, + + @ManyToOne + @JoinColumn(name = "register_category_id") + val category: ReferenceData?, + + @ManyToOne + @JoinColumn(name = "register_level_id") + val level: ReferenceData?, + + @Column(name = "registration_date") + val date: LocalDate, + + @Column(name = "next_review_date") + val reviewDate: LocalDate?, + + @Column(name = "deregistered", columnDefinition = "number") + val deRegistered: Boolean, + + @Column(name = "soft_deleted", columnDefinition = "number") + val softDeleted: Boolean, + + @Id + @Column(name = "registration_id") + val id: Long +) + +@Entity +@Immutable +@Table(name = "r_register_type") +class RegisterType( + + @Id + @Column(name = "register_type_id") + val id: Long, + + val code: String +) { + companion object { + const val MAPPA_CODE = "MAPP" + } +} + +interface RegistrationRepository : JpaRepository { + + @EntityGraph(attributePaths = ["type", "category", "level"]) + fun findFirstByPersonCrnAndTypeCodeOrderByDateDesc(crn: String, typeCode: String): Registration? +} + +fun RegistrationRepository.findMappa(crn: String) = + findFirstByPersonCrnAndTypeCodeOrderByDateDesc(crn, RegisterType.MAPPA_CODE) diff --git a/projects/resettlement-passport-and-delius/src/main/kotlin/uk/gov/justice/digital/hmpps/service/CaseDetailService.kt b/projects/resettlement-passport-and-delius/src/main/kotlin/uk/gov/justice/digital/hmpps/service/CaseDetailService.kt new file mode 100644 index 0000000000..2492021e47 --- /dev/null +++ b/projects/resettlement-passport-and-delius/src/main/kotlin/uk/gov/justice/digital/hmpps/service/CaseDetailService.kt @@ -0,0 +1,52 @@ +package uk.gov.justice.digital.hmpps.service + +import org.springframework.stereotype.Service +import uk.gov.justice.digital.hmpps.api.model.CaseIdentifiers +import uk.gov.justice.digital.hmpps.api.model.Manager +import uk.gov.justice.digital.hmpps.api.model.MappaDetail +import uk.gov.justice.digital.hmpps.api.model.Name +import uk.gov.justice.digital.hmpps.entity.Category +import uk.gov.justice.digital.hmpps.entity.Level +import uk.gov.justice.digital.hmpps.entity.PersonManagerRepository +import uk.gov.justice.digital.hmpps.entity.PersonRepository +import uk.gov.justice.digital.hmpps.entity.RegistrationRepository +import uk.gov.justice.digital.hmpps.entity.Staff +import uk.gov.justice.digital.hmpps.entity.findMappa +import uk.gov.justice.digital.hmpps.exception.NotFoundException + +@Service +class CaseDetailService( + private val personRepository: PersonRepository, + private val registrationRepository: RegistrationRepository, + private val personManagerRepository: PersonManagerRepository +) { + fun findCrnByNomsId(nomsId: String): CaseIdentifiers = + personRepository.findCrnByNomsId(nomsId)?.let { CaseIdentifiers(it) } + ?: throw NotFoundException("Person", "nomsId", nomsId) + + fun findMappaDetail(crn: String): MappaDetail = registrationRepository.findMappa(crn)?.let { + MappaDetail( + it.level?.code?.toMappaLevel(), + it.level?.description, + it.category?.code?.toMappaCategory(), + it.category?.description, + it.date, + it.reviewDate + ) + } ?: throw NotFoundException("No MAPPA details found for $crn") + + fun findCommunityManager(crn: String) = personManagerRepository.findByPersonCrn(crn)?.staff?.asManager() + ?: throw NotFoundException("Person", "crn", crn) +} + +private fun String.toMappaLevel() = Level.entries.find { it.name == this }?.number + ?: throw IllegalStateException("Unexpected MAPPA level: $this") + +private fun String.toMappaCategory() = Category.entries.find { it.name == this }?.number + ?: throw IllegalStateException("Unexpected MAPPA category: $this") + +private fun Staff.asManager() = if (code.endsWith("U")) { + Manager(null, true) +} else { + Manager(Name(forename, surname), false) +} diff --git a/projects/resettlement-passport-and-delius/src/test/kotlin/uk/gov/justice/digital/hmpps/service/CaseDetailServiceTest.kt b/projects/resettlement-passport-and-delius/src/test/kotlin/uk/gov/justice/digital/hmpps/service/CaseDetailServiceTest.kt new file mode 100644 index 0000000000..a9413105f0 --- /dev/null +++ b/projects/resettlement-passport-and-delius/src/test/kotlin/uk/gov/justice/digital/hmpps/service/CaseDetailServiceTest.kt @@ -0,0 +1,115 @@ +package uk.gov.justice.digital.hmpps.service + +import org.hamcrest.MatcherAssert.assertThat +import org.hamcrest.Matchers.equalTo +import org.junit.jupiter.api.Test +import org.junit.jupiter.api.assertThrows +import org.junit.jupiter.api.extension.ExtendWith +import org.junit.jupiter.params.ParameterizedTest +import org.junit.jupiter.params.provider.Arguments +import org.junit.jupiter.params.provider.MethodSource +import org.mockito.InjectMocks +import org.mockito.Mock +import org.mockito.junit.jupiter.MockitoExtension +import org.mockito.kotlin.whenever +import uk.gov.justice.digital.hmpps.api.model.MappaDetail +import uk.gov.justice.digital.hmpps.data.generator.RegistrationGenerator.CATEGORIES +import uk.gov.justice.digital.hmpps.data.generator.RegistrationGenerator.LEVELS +import uk.gov.justice.digital.hmpps.data.generator.RegistrationGenerator.generate +import uk.gov.justice.digital.hmpps.entity.Category +import uk.gov.justice.digital.hmpps.entity.Level +import uk.gov.justice.digital.hmpps.entity.PersonManagerRepository +import uk.gov.justice.digital.hmpps.entity.PersonRepository +import uk.gov.justice.digital.hmpps.entity.Registration +import uk.gov.justice.digital.hmpps.entity.RegistrationRepository +import uk.gov.justice.digital.hmpps.entity.findMappa +import uk.gov.justice.digital.hmpps.exception.NotFoundException +import java.time.LocalDate + +@ExtendWith(MockitoExtension::class) +class CaseDetailServiceTest { + @Mock + internal lateinit var personRepository: PersonRepository + + @Mock + internal lateinit var registrationRepository: RegistrationRepository + + @Mock + internal lateinit var personManagerRepository: PersonManagerRepository + + @InjectMocks + internal lateinit var service: CaseDetailService + + @ParameterizedTest + @MethodSource("mappaDetails") + fun `mappa details are correctly mapped`(registration: Registration, mappa: MappaDetail) { + val crn = "Z123456" + whenever(registrationRepository.findMappa(crn)).thenReturn(registration) + + val res = service.findMappaDetail(crn) + assertThat(res, equalTo(mappa)) + } + + @Test + fun `service throws not found exception if no mappa details found`() { + val nfe = assertThrows { service.findMappaDetail("N123456") } + assertThat(nfe.message, equalTo("No MAPPA details found for N123456")) + } + + companion object { + + private val detail = MappaDetail( + null, + null, + null, + null, + LocalDate.now(), + null + ) + + @JvmStatic + fun mappaDetails() = listOf( + Arguments.of(generate(), detail), + Arguments.of( + generate(category = CATEGORIES[Category.X9.name]), + detail.copy(category = 0, categoryDescription = "Description of X9") + ), + Arguments.of( + generate(category = CATEGORIES[Category.M1.name], level = LEVELS[Level.M0.name]), + detail.copy( + category = 1, + categoryDescription = "Description of M1", + level = 0, + levelDescription = "Description of M0" + ) + ), + Arguments.of( + generate(category = CATEGORIES[Category.M2.name], level = LEVELS[Level.M1.name]), + detail.copy( + category = 2, + categoryDescription = "Description of M2", + level = 1, + levelDescription = "Description of M1" + ) + ), + Arguments.of( + generate(category = CATEGORIES[Category.M3.name], level = LEVELS[Level.M2.name]), + detail.copy( + category = 3, + categoryDescription = "Description of M3", + level = 2, + levelDescription = "Description of M2" + ) + ), + Arguments.of( + generate(category = CATEGORIES[Category.M4.name], level = LEVELS[Level.M3.name]), + detail.copy( + category = 4, + categoryDescription = "Description of M4", + level = 3, + levelDescription = "Description of M3" + ) + ) + ) + } +}