Skip to content

Commit

Permalink
MAN-109 - update data saved to db and add service test
Browse files Browse the repository at this point in the history
  • Loading branch information
achimber-moj committed Oct 29, 2024
1 parent c9207ad commit e913cbe
Show file tree
Hide file tree
Showing 2 changed files with 87 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,13 @@ import org.springframework.web.server.ResponseStatusException
import uk.gov.justice.digital.hmpps.api.model.appointment.CreateAppointment
import uk.gov.justice.digital.hmpps.audit.service.AuditableService
import uk.gov.justice.digital.hmpps.audit.service.AuditedInteractionService
import uk.gov.justice.digital.hmpps.datetime.EuropeLondon
import uk.gov.justice.digital.hmpps.exception.ConflictException
import uk.gov.justice.digital.hmpps.exception.NotFoundException
import uk.gov.justice.digital.hmpps.integrations.delius.audit.BusinessInteractionCode
import uk.gov.justice.digital.hmpps.integrations.delius.overview.entity.RequirementRepository
import uk.gov.justice.digital.hmpps.integrations.delius.sentence.entity.*
import java.time.LocalDate
import java.time.ZonedDateTime

@Service
Expand Down Expand Up @@ -39,6 +41,14 @@ class AppointmentService(
createAppointment: CreateAppointment
) {

if (createAppointment.requirementId != null && createAppointment.licenceConditionId != null) {
throw ResponseStatusException(HttpStatus.BAD_REQUEST, "Either licence id or requirement id can be provided, not both")
}

if (createAppointment.end.isBefore(createAppointment.start)) {
throw ResponseStatusException(HttpStatus.BAD_REQUEST, "Appointment end time cannot be before start time.")
}

if (!eventSentenceRepository.existsById(createAppointment.eventId)) {
throw NotFoundException("Event", "eventId", createAppointment.eventId)
}
Expand Down Expand Up @@ -75,11 +85,11 @@ class AppointmentService(
om.person,
appointmentTypeRepository.getByCode(type.code),
start.toLocalDate(),
start,
ZonedDateTime.of(LocalDate.EPOCH, start.toLocalTime(), EuropeLondon),
om.team,
om.staff,
0,
end,
ZonedDateTime.of(LocalDate.EPOCH, end.toLocalTime(), EuropeLondon),
om.provider.id,
urn,
eventId = eventId,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,11 @@ import org.junit.jupiter.api.assertThrows
import org.junit.jupiter.api.extension.ExtendWith
import org.mockito.InjectMocks
import org.mockito.Mock
import org.mockito.Mockito.verifyNoMoreInteractions
import org.mockito.junit.jupiter.MockitoExtension
import org.mockito.kotlin.verifyNoInteractions
import org.mockito.kotlin.whenever
import org.springframework.web.server.ResponseStatusException
import uk.gov.justice.digital.hmpps.api.model.appointment.CreateAppointment
import uk.gov.justice.digital.hmpps.audit.service.AuditedInteractionService
import uk.gov.justice.digital.hmpps.data.generator.OffenderManagerGenerator
Expand Down Expand Up @@ -45,6 +48,55 @@ class AppointmentServiceTest {
@InjectMocks
lateinit var service: AppointmentService

@Test
fun `licence and requirement id provided`() {
val appointment = CreateAppointment(
CreateAppointment.Type.InitialAppointmentInOfficeNS,
ZonedDateTime.now().plusDays(1),
ZonedDateTime.now().plusDays(2),
1,
PersonGenerator.EVENT_1.id,
requirementId = 2,
licenceConditionId = 3)

whenever(offenderManagerRepository.findByPersonCrnAndSoftDeletedIsFalseAndActiveIsTrue(PersonGenerator.PERSON_1.crn)).thenReturn(OffenderManagerGenerator.OFFENDER_MANAGER_ACTIVE)
val exception = assertThrows<ResponseStatusException> {
service.createAppointment(PersonGenerator.PERSON_1.crn, appointment)
}

assertThat(exception.message, equalTo("400 BAD_REQUEST \"Either licence id or requirement id can be provided, not both\""))

verifyNoMoreInteractions(offenderManagerRepository)
verifyNoInteractions(eventSentenceRepository)
verifyNoInteractions(licenceConditionRepository)
verifyNoInteractions(requirementRepository)
verifyNoInteractions(appointmentRepository)
verifyNoInteractions(appointmentTypeRepository)
}

@Test
fun `start date before end date`() {
val appointment = CreateAppointment(
CreateAppointment.Type.InitialAppointmentInOfficeNS,
ZonedDateTime.now().plusDays(2),
ZonedDateTime.now().plusDays(1),
1,
PersonGenerator.EVENT_1.id)

whenever(offenderManagerRepository.findByPersonCrnAndSoftDeletedIsFalseAndActiveIsTrue(PersonGenerator.PERSON_1.crn)).thenReturn(OffenderManagerGenerator.OFFENDER_MANAGER_ACTIVE)
val exception = assertThrows<ResponseStatusException> {
service.createAppointment(PersonGenerator.PERSON_1.crn, appointment)
}

assertThat(exception.message, equalTo("400 BAD_REQUEST \"Appointment end time cannot be before start time.\""))

verifyNoMoreInteractions(offenderManagerRepository)
verifyNoInteractions(eventSentenceRepository)
verifyNoInteractions(licenceConditionRepository)
verifyNoInteractions(requirementRepository)
verifyNoInteractions(appointmentRepository)
verifyNoInteractions(appointmentTypeRepository)
}

@Test
fun `event not found`() {
Expand All @@ -56,11 +108,19 @@ class AppointmentServiceTest {
1)

whenever(offenderManagerRepository.findByPersonCrnAndSoftDeletedIsFalseAndActiveIsTrue(PersonGenerator.PERSON_1.crn)).thenReturn(OffenderManagerGenerator.OFFENDER_MANAGER_ACTIVE)
whenever(eventSentenceRepository.existsById(appointment.eventId)).thenReturn(false)
val exception = assertThrows<NotFoundException> {
service.createAppointment(PersonGenerator.PERSON_1.crn, appointment)
}

assertThat(exception.message, equalTo("Event with eventId of 1 not found"))

verifyNoMoreInteractions(offenderManagerRepository)
verifyNoMoreInteractions(eventSentenceRepository)
verifyNoInteractions(licenceConditionRepository)
verifyNoInteractions(requirementRepository)
verifyNoInteractions(appointmentRepository)
verifyNoInteractions(appointmentTypeRepository)
}

@Test
Expand All @@ -75,11 +135,19 @@ class AppointmentServiceTest {

whenever(offenderManagerRepository.findByPersonCrnAndSoftDeletedIsFalseAndActiveIsTrue(PersonGenerator.PERSON_1.crn)).thenReturn(OffenderManagerGenerator.OFFENDER_MANAGER_ACTIVE)
whenever(eventSentenceRepository.existsById(appointment.eventId)).thenReturn(true)
whenever(requirementRepository.existsById(appointment.requirementId!!)).thenReturn(false)
val exception = assertThrows<NotFoundException> {
service.createAppointment(PersonGenerator.PERSON_1.crn, appointment)
}

assertThat(exception.message, equalTo("Requirement with requirementId of 2 not found"))

verifyNoMoreInteractions(offenderManagerRepository)
verifyNoMoreInteractions(eventSentenceRepository)
verifyNoMoreInteractions(requirementRepository)
verifyNoInteractions(licenceConditionRepository)
verifyNoInteractions(appointmentRepository)
verifyNoInteractions(appointmentTypeRepository)
}

@Test
Expand All @@ -100,5 +168,12 @@ class AppointmentServiceTest {
}

assertThat(exception.message, equalTo("LicenceCondition with licenceConditionId of 3 not found"))

verifyNoMoreInteractions(offenderManagerRepository)
verifyNoMoreInteractions(eventSentenceRepository)
verifyNoMoreInteractions(licenceConditionRepository)
verifyNoInteractions(requirementRepository)
verifyNoInteractions(appointmentRepository)
verifyNoInteractions(appointmentTypeRepository)
}
}

0 comments on commit e913cbe

Please sign in to comment.