@@ -10,12 +10,18 @@ import org.mockito.Mockito.mock
10
10
import org.mockito.Mockito.`when`
11
11
import org.mockito.junit.jupiter.MockitoExtension
12
12
import org.mockito.kotlin.any
13
+ import org.mockito.kotlin.times
14
+ import org.mockito.kotlin.verify
13
15
import org.springframework.http.HttpStatus
16
+ import org.springframework.security.core.GrantedAuthority
17
+ import uk.gov.justice.digital.hmpps.learnerrecordsapi.config.Roles.ROLE_LEARNERS_RO
18
+ import uk.gov.justice.digital.hmpps.learnerrecordsapi.config.Roles.ROLE_LEARNERS_UI
14
19
import uk.gov.justice.digital.hmpps.learnerrecordsapi.models.lrsapi.response.exceptions.MatchNotFoundException
15
20
import uk.gov.justice.digital.hmpps.learnerrecordsapi.models.response.CheckMatchResponse
16
21
import uk.gov.justice.digital.hmpps.learnerrecordsapi.models.response.CheckMatchStatus
17
22
import uk.gov.justice.digital.hmpps.learnerrecordsapi.service.LearnerEventsService
18
23
import uk.gov.justice.digital.hmpps.learnerrecordsapi.service.MatchService
24
+ import uk.gov.justice.hmpps.kotlin.auth.HmppsAuthenticationHolder
19
25
import uk.gov.justice.hmpps.sqs.audit.HmppsAuditService
20
26
import java.time.LocalDate
21
27
@@ -33,50 +39,60 @@ class MatchResourceTest {
33
39
private lateinit var learnerEventsResource: LearnerEventsResource
34
40
private lateinit var mockAuditService: HmppsAuditService
35
41
private lateinit var mockLearnerEventsService: LearnerEventsService
42
+ private lateinit var mockAuthHolder: HmppsAuthenticationHolder
36
43
37
44
@BeforeEach
38
45
fun setup () {
39
46
mockMatchService = mock(MatchService ::class .java)
40
47
mockLearnerEventsService = mock(LearnerEventsService ::class .java)
41
48
mockAuditService = mock(HmppsAuditService ::class .java)
42
- matchResource = MatchResource (mockMatchService, mockLearnerEventsService, mockAuditService)
49
+ mockAuthHolder = mock(HmppsAuthenticationHolder ::class .java)
50
+ matchResource = MatchResource (mockMatchService, mockLearnerEventsService, mockAuditService, mockAuthHolder)
43
51
learnerEventsResource = LearnerEventsResource (mockLearnerEventsService)
44
52
}
45
53
54
+ private fun setRoleAndUln (role : String , uln : String? ) {
55
+ `when `(mockAuthHolder.roles).thenReturn(
56
+ listOf (GrantedAuthority { role }),
57
+ )
58
+ `when `(mockMatchService.findMatch(any())).thenReturn(
59
+ uln?.let {
60
+ CheckMatchResponse (
61
+ matchedUln = it,
62
+ )
63
+ },
64
+ )
65
+ }
66
+
46
67
@Test
47
- fun `should return NOT_FOUND if no record found` () {
48
- ` when `(mockMatchService.findMatch(any())).thenReturn( null )
68
+ fun `should return NOT_FOUND if no record found` () = runTest {
69
+ setRoleAndUln( ROLE_LEARNERS_UI , null )
49
70
50
71
val actual = matchResource.findMatch(nomisId, " " )
51
72
assertThat(actual.statusCode).isEqualTo(HttpStatus .NOT_FOUND )
52
73
assertThat(actual.body?.status ? : " " ).isEqualTo(CheckMatchStatus .NotFound )
74
+ verify(mockAuditService, times(0 )).publishEvent(any())
53
75
}
54
76
55
77
@Test
56
- fun `should return entity if record found` () {
57
- `when `(mockMatchService.findMatch(any())).thenReturn(
58
- CheckMatchResponse (
59
- matchedUln = matchedUln,
60
- ),
61
- )
78
+ fun `should return entity if record found` () = runTest {
79
+ setRoleAndUln(ROLE_LEARNERS_RO , matchedUln)
62
80
63
81
val actual = matchResource.findMatch(nomisId, " " )
64
82
assertThat(actual.statusCode).isEqualTo(HttpStatus .OK )
65
83
assertThat(actual.body?.matchedUln ? : " " ).isEqualTo(matchedUln)
66
84
assertThat(actual.body?.status ? : " " ).isEqualTo(CheckMatchStatus .Found )
85
+ verify(mockAuditService, times(1 )).publishEvent(any())
67
86
}
68
87
69
88
@Test
70
- fun `should return NO_MATCH if id cannot be matched` () {
71
- `when `(mockMatchService.findMatch(any())).thenReturn(
72
- CheckMatchResponse (
73
- matchedUln = " " ,
74
- ),
75
- )
89
+ fun `should return NO_MATCH if id cannot be matched` () = runTest {
90
+ setRoleAndUln(ROLE_LEARNERS_UI , " " )
76
91
77
92
val actual = matchResource.findMatch(nomisId, " " )
78
93
assertThat(actual.statusCode).isEqualTo(HttpStatus .OK )
79
94
assertThat(actual.body?.status ? : " " ).isEqualTo(CheckMatchStatus .NoMatch )
95
+ verify(mockAuditService, times(0 )).publishEvent(any())
80
96
}
81
97
82
98
@Test
@@ -86,6 +102,7 @@ class MatchResourceTest {
86
102
matchResource.findLearnerEventsByNomisId(nomisId, " " )
87
103
}
88
104
assertThat(exception.message).isEqualTo(nomisId)
105
+ verify(mockAuditService, times(1 )).publishEvent(any())
89
106
}
90
107
91
108
@Test
0 commit comments