1
1
package uk.gov.justice.digital.hmpps.hmppsintegrationapi.services
2
2
3
3
import com.fasterxml.jackson.databind.ObjectMapper
4
+ import com.fasterxml.jackson.module.kotlin.readValue
4
5
import io.kotest.assertions.throwables.shouldThrow
5
6
import io.kotest.core.spec.style.DescribeSpec
6
7
import io.kotest.matchers.shouldBe
@@ -14,12 +15,14 @@ import org.mockito.kotlin.whenever
14
15
import software.amazon.awssdk.services.sqs.SqsAsyncClient
15
16
import software.amazon.awssdk.services.sqs.model.SendMessageRequest
16
17
import uk.gov.justice.digital.hmpps.hmppsintegrationapi.exception.MessageFailedException
17
- import uk.gov.justice.digital.hmpps.hmppsintegrationapi.models.expressionOfInterest.ExpressionInterest
18
+ import uk.gov.justice.digital.hmpps.hmppsintegrationapi.extensions.MockMvcExtensions.objectMapper
19
+ import uk.gov.justice.digital.hmpps.hmppsintegrationapi.models.hmpps.ExpressionOfInterest
18
20
import uk.gov.justice.digital.hmpps.hmppsintegrationapi.models.hmpps.ExpressionOfInterestMessage
21
+ import uk.gov.justice.digital.hmpps.hmppsintegrationapi.models.hmpps.MessageType
19
22
import uk.gov.justice.hmpps.sqs.HmppsQueue
20
23
import uk.gov.justice.hmpps.sqs.HmppsQueueService
21
24
22
- class ExpressionInterestServiceTest :
25
+ class PutExpressionInterestServiceTest :
23
26
DescribeSpec ({
24
27
val mockQueueService = mock<HmppsQueueService >()
25
28
val mockSqsClient = mock<SqsAsyncClient >()
@@ -31,7 +34,7 @@ class ExpressionInterestServiceTest :
31
34
on { queueUrl } doReturn " https://test-queue-url"
32
35
}
33
36
34
- val service = ExpressionInterestService (mockQueueService, mockObjectMapper)
37
+ val service = PutExpressionInterestService (mockQueueService, mockObjectMapper)
35
38
36
39
beforeTest {
37
40
reset(mockQueueService, mockSqsClient, mockObjectMapper)
@@ -40,13 +43,19 @@ class ExpressionInterestServiceTest :
40
43
41
44
describe(" sendExpressionOfInterest" ) {
42
45
it(" should send a valid message successfully to SQS" ) {
43
- val expressionInterest = ExpressionInterest (jobId = " 12345" , hmppsId = " H1234" )
44
- val messageBody = """ {"jobId":"12345","verifiedHmppsId":"H1234"}"""
46
+ val expressionOfInterest = ExpressionOfInterest (jobId = " 12345" , prisonNumber = " H1234" )
47
+ val expectedMessage =
48
+ ExpressionOfInterestMessage (
49
+ jobId = " 12345" ,
50
+ prisonNumber = " H1234" ,
51
+ eventType = MessageType .EXPRESSION_OF_INTEREST_CREATED ,
52
+ )
53
+ val messageBody = objectMapper.writeValueAsString(expectedMessage)
45
54
46
55
whenever(mockObjectMapper.writeValueAsString(any<ExpressionOfInterestMessage >()))
47
56
.thenReturn(messageBody)
48
57
49
- service.sendExpressionOfInterest(expressionInterest )
58
+ service.sendExpressionOfInterest(expressionOfInterest )
50
59
51
60
verify(mockSqsClient).sendMessage(
52
61
argThat<SendMessageRequest > { request: SendMessageRequest ? ->
@@ -59,10 +68,7 @@ class ExpressionInterestServiceTest :
59
68
}
60
69
61
70
it(" should throw MessageFailedException when SQS fails" ) {
62
- val expressionInterest = ExpressionInterest (jobId = " 12345" , hmppsId = " H1234" )
63
-
64
- whenever(mockObjectMapper.writeValueAsString(any<ExpressionOfInterestMessage >()))
65
- .thenReturn(""" {"jobId":"12345","verifiedHmppsId":"H1234"}""" )
71
+ val expressionInterest = ExpressionOfInterest (jobId = " 12345" , prisonNumber = " H1234" )
66
72
67
73
whenever(mockSqsClient.sendMessage(any<SendMessageRequest >()))
68
74
.thenThrow(RuntimeException (" Failed to send message to SQS" ))
@@ -74,5 +80,23 @@ class ExpressionInterestServiceTest :
74
80
75
81
exception.message shouldBe " Failed to send message to SQS"
76
82
}
83
+
84
+ it(" should serialize ExpressionOfInterestMessage with correct keys" ) {
85
+ val expectedMessage =
86
+ ExpressionOfInterestMessage (
87
+ messageId = " 1" ,
88
+ jobId = " 12345" ,
89
+ prisonNumber = " H1234" ,
90
+ eventType = MessageType .EXPRESSION_OF_INTEREST_CREATED ,
91
+ )
92
+
93
+ val serializedJson = objectMapper.writeValueAsString(expectedMessage)
94
+ val deserializedMap: Map <String , Any ?> = objectMapper.readValue(serializedJson)
95
+
96
+ assert (deserializedMap.containsKey(" messageId" ))
97
+ assert (deserializedMap.containsKey(" jobId" ))
98
+ assert (deserializedMap.containsKey(" prisonNumber" ))
99
+ assert (deserializedMap.containsKey(" eventType" ))
100
+ }
77
101
}
78
102
})
0 commit comments