Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
- Update handler tests
  • Loading branch information
joseph-bcl committed Oct 16, 2024
1 parent dbb8639 commit 9897dac
Show file tree
Hide file tree
Showing 4 changed files with 73 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,7 @@ object MessageGenerator {
val COMMON_PLATFORM_EVENT = ResourceLoader.message<CommonPlatformHearing>("common-platform-hearing")
val COMMON_PLATFORM_EVENT_VALIDATION_ERROR =
ResourceLoader.message<CommonPlatformHearing>("common-platform-hearing-validation-error")
val COMMON_PLATFORM_EVENT_NO_CASES =
ResourceLoader.message<CommonPlatformHearing>("common-platform-hearing-no-cases")

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
{
"Type": "Notification",
"MessageId": "aa2c2828-167f-529b-8e19-73735a2fb85c",
"TopicArn": "arn:aws:sns:eu-west-2:000000000000:example",
"Message": "{\"hearing\":{\"id\":\"00000000-0000-0000-0000-000000000000\",\"courtCentre\":{\"id\":\"00000000-0000-0000-0000-000000000000\",\"code\":\"A00AA00\",\"roomId\":\"00000000-0000-0000-0000-000000000000\",\"roomName\":\"Courtroom 01\"},\"type\":{\"id\":\"00000000-0000-0000-0000-000000000000\",\"description\":\"First hearing\",\"welshDescription\":null},\"jurisdictionType\":\"MAGISTRATES\",\"hearingDays\":[{\"sittingDay\":\"2024-01-01T12:00:00\",\"listedDurationMinutes\":30,\"listingSequence\":1}],\"prosecutionCases\":[]}}",
"Timestamp": "2022-07-27T14:22:08.509Z",
"SignatureVersion": "1",
"Signature": "EXAMPLE",
"SigningCertURL": "https://sns.eu-west-2.amazonaws.com/EXAMPLE.pem",
"UnsubscribeURL": "https://sns.eu-west-2.amazonaws.com/?Action=Unsubscribe&SubscriptionArn=EXAMPLE",
"MessageAttributes": {
"tracestate": {
"Type": "String",
"Value": "00-01a234b56c7de8f9g01h234i56789j0k-1l234m567n8o9p01-01"
},
"eventType": {
"Type": "String",
"Value": "COMMON_PLATFORM_HEARING"
},
"hearingEventType": {
"Type": "String",
"Value": "ConfirmedOrUpdated"
},
"traceparent": {
"Type": "String",
"Value": "00-01a234b56c7de8f9g01h234i56789j0k-1l234m567n8o9p01-01"
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,10 @@ class Handler(
override fun handle(notification: Notification<CommonPlatformHearing>) {
telemetryService.notificationReceived(notification)

val defendant = notification.message.hearing.prosecutionCases.firstOrNull()?.defendants?.firstOrNull()
?: throw IllegalArgumentException("No prosecution cases found")
val courtCode = notification.message.hearing.courtCentre.code.toDeliusCourtCode()

val dateOfBirth = notification.message.hearing.prosecutionCases
.firstOrNull()?.defendants?.firstOrNull()
?.personDefendant?.personDetails?.dateOfBirth
Expand All @@ -48,10 +52,6 @@ class Handler(
}
}

val defendant = notification.message.hearing.prosecutionCases.firstOrNull()?.defendants?.firstOrNull()
?: throw IllegalArgumentException("No prosecution cases found")
val courtCode = notification.message.hearing.courtCentre.code.toDeliusCourtCode()

val matchRequest = notification.message.toProbationMatchRequest()
val matchedPersonResponse = probationSearchClient.match(matchRequest)

Expand Down
Original file line number Diff line number Diff line change
@@ -1,14 +1,21 @@
package uk.gov.justice.digital.hmpps.messaging

import org.junit.jupiter.api.Test
import org.junit.jupiter.api.assertThrows
import org.junit.jupiter.api.extension.ExtendWith
import org.mockito.InjectMocks
import org.mockito.Mock
import org.mockito.junit.jupiter.MockitoExtension
import org.mockito.kotlin.any
import org.mockito.kotlin.verify
import org.mockito.kotlin.whenever
import uk.gov.justice.digital.hmpps.converter.NotificationConverter
import uk.gov.justice.digital.hmpps.data.generator.MessageGenerator
import uk.gov.justice.digital.hmpps.data.generator.ReferenceDataGenerator
import uk.gov.justice.digital.hmpps.integrations.client.*
import uk.gov.justice.digital.hmpps.integrations.delius.entity.repository.ReferenceDataRepository
import uk.gov.justice.digital.hmpps.message.Notification
import uk.gov.justice.digital.hmpps.service.PersonService
import uk.gov.justice.digital.hmpps.telemetry.TelemetryMessagingExtensions.notificationReceived
import uk.gov.justice.digital.hmpps.telemetry.TelemetryService

Expand All @@ -20,13 +27,43 @@ internal class HandlerTest {
@Mock
lateinit var converter: NotificationConverter<CommonPlatformHearing>

@Mock
lateinit var personService: PersonService

@Mock
lateinit var referenceDataRepository: ReferenceDataRepository

@Mock
lateinit var probationSearchClient: ProbationSearchClient

@InjectMocks
lateinit var handler: Handler

@Test
fun `message is logged to telemetry`() {
whenever(probationSearchClient.match(any())).thenReturn(ProbationMatchResponse(matches = emptyList(), matchedBy = "NONE"))
whenever(personService.generateCrn()).thenReturn("A000001")
whenever(referenceDataRepository.findByCode("M")).thenReturn(ReferenceDataGenerator.GENDER_MALE)
val notification = Notification(message = MessageGenerator.COMMON_PLATFORM_EVENT)
handler.handle(notification)
verify(telemetryService).notificationReceived(notification)
}

@Test
fun `exception thrown when age is under 10 years old`() {
val notification = Notification(message = MessageGenerator.COMMON_PLATFORM_EVENT_VALIDATION_ERROR)
val exception = assertThrows<IllegalArgumentException> {
handler.handle(notification)
}
assert(exception.message!!.contains("Date of birth would indicate person is under ten years old"))
}

@Test
fun `exception thrown prosecution cases is empty`() {
val notification = Notification(message = MessageGenerator.COMMON_PLATFORM_EVENT_NO_CASES)
val exception = assertThrows<IllegalArgumentException> {
handler.handle(notification)
}
assert(exception.message!!.contains("No prosecution cases found"))
}
}

0 comments on commit 9897dac

Please sign in to comment.