@@ -4,10 +4,8 @@ import io.kotest.core.spec.style.DescribeSpec
4
4
import io.kotest.matchers.shouldBe
5
5
import io.kotest.matchers.string.shouldContain
6
6
import org.mockito.Mockito
7
- import org.mockito.internal.verification.VerificationModeFactory
8
- import org.mockito.kotlin.any
9
- import org.mockito.kotlin.argThat
10
7
import org.mockito.kotlin.doThrow
8
+ import org.mockito.kotlin.times
11
9
import org.mockito.kotlin.verify
12
10
import org.mockito.kotlin.whenever
13
11
import org.springframework.beans.factory.annotation.Autowired
@@ -24,10 +22,10 @@ import uk.gov.justice.digital.hmpps.hmppsintegrationapi.models.hmpps.CaseNote
24
22
import uk.gov.justice.digital.hmpps.hmppsintegrationapi.models.hmpps.Response
25
23
import uk.gov.justice.digital.hmpps.hmppsintegrationapi.models.hmpps.UpstreamApi
26
24
import uk.gov.justice.digital.hmpps.hmppsintegrationapi.models.hmpps.UpstreamApiError
25
+ import uk.gov.justice.digital.hmpps.hmppsintegrationapi.models.roleconfig.ConsumerFilters
27
26
import uk.gov.justice.digital.hmpps.hmppsintegrationapi.services.GetCaseNotesForPersonService
28
27
import uk.gov.justice.digital.hmpps.hmppsintegrationapi.services.internal.AuditService
29
- import java.net.URLEncoder
30
- import java.nio.charset.StandardCharsets
28
+ import java.time.LocalDateTime
31
29
32
30
@WebMvcTest(controllers = [CaseNotesController ::class ])
33
31
@ActiveProfiles(" test" )
@@ -37,43 +35,108 @@ class CaseNotesControllerTest(
37
35
@MockitoBean val auditService : AuditService ,
38
36
) : DescribeSpec(
39
37
{
40
- val hmppsId = " 9999/11111A"
41
- val encodedHmppsId = URLEncoder .encode(hmppsId, StandardCharsets .UTF_8 )
42
- val path = " /v1/persons/$encodedHmppsId /case-notes"
38
+ val hmppsId = " G2996UX"
39
+ val locationId = " MDI"
40
+ val startDate : LocalDateTime = LocalDateTime .now()
41
+ val endDate : LocalDateTime = LocalDateTime .now()
42
+ val path = " /v1/persons/$hmppsId /case-notes?startDate=$startDate &endDate=$endDate &locationId=$locationId "
43
+ val caseNoteFilter = CaseNoteFilter (hmppsId, startDate, endDate, locationId)
43
44
val mockMvc = IntegrationAPIMockMvc (springMockMvc)
44
45
val pageCaseNote =
45
46
listOf(
46
47
CaseNote (caseNoteId = "abcd1234"),
47
48
)
49
+ val filters = null
50
+
48
51
describe("GET $path") {
49
52
beforeTest {
50
53
Mockito .reset(getCaseNotesForPersonService)
51
54
Mockito .reset(auditService)
52
- whenever(getCaseNotesForPersonService.execute(any())).thenReturn(
55
+
56
+ whenever(getCaseNotesForPersonService.execute(caseNoteFilter, filters)).thenReturn(
53
57
Response (
54
58
data = pageCaseNote,
55
59
errors = emptyList(),
56
60
),
57
61
)
58
62
}
59
63
60
- it("returns a 200 OK status code ") {
61
- val result = mockMvc.performAuthorised(path)
64
+ it("logs audit ") {
65
+ mockMvc.performAuthorised(path)
62
66
63
- result.response.status.shouldBe(HttpStatus .OK .value())
67
+ verify(
68
+ auditService,
69
+ times(1),
70
+ ).createEvent("GET_CASE_NOTES ", mapOf("hmppsId" to hmppsId))
64
71
}
65
72
66
- it("gets the case notes for a person with the matching ID ") {
67
- mockMvc.performAuthorised (path)
73
+ it("passes filters into service ") {
74
+ mockMvc.performAuthorisedWithCN (path, "limited-prisons" )
68
75
69
76
verify(
70
77
getCaseNotesForPersonService,
71
- VerificationModeFactory .times(1),
72
- ).execute(argThat<CaseNoteFilter > { it -> it.hmppsId == hmppsId })
78
+ times(1),
79
+ ).execute(
80
+ caseNoteFilter,
81
+ ConsumerFilters (prisons = listOf("XYZ ")),
82
+ )
83
+ }
84
+
85
+ it("returns the case notes for a person with the matching ID with a 200 status code") {
86
+ val result = mockMvc.performAuthorised(path)
87
+ result.response.status.shouldBe(HttpStatus .OK .value())
88
+ result.response.contentAsString.shouldContain(
89
+ """
90
+ {
91
+ "data": [
92
+ {
93
+ "caseNoteId": "abcd1234",
94
+ "offenderIdentifier": null,
95
+ "type": null,
96
+ "typeDescription": null,
97
+ "subType": null,
98
+ "subTypeDescription": null,
99
+ "creationDateTime": null,
100
+ "occurrenceDateTime": null,
101
+ "text": null,
102
+ "locationId": null,
103
+ "sensitive": false,
104
+ "amendments": []
105
+ }
106
+ ],
107
+ "pagination": {
108
+ "isLastPage": true,
109
+ "count": 1,
110
+ "page": 1,
111
+ "perPage": 10,
112
+ "totalCount": 1,
113
+ "totalPages": 1
114
+ }
115
+ }
116
+ """.removeWhitespaceAndNewlines(),
117
+ )
118
+ }
119
+
120
+ it("returns a 400 when the upstream service returns bad request") {
121
+ whenever(getCaseNotesForPersonService.execute(caseNoteFilter, filters)).thenReturn(
122
+ Response (
123
+ data = emptyList(),
124
+ errors =
125
+ listOf(
126
+ UpstreamApiError (
127
+ type = UpstreamApiError .Type .BAD_REQUEST ,
128
+ causedBy = UpstreamApi .NOMIS ,
129
+ ),
130
+ ),
131
+ ),
132
+ )
133
+
134
+ val result = mockMvc.performAuthorised(path)
135
+ result.response.status.shouldBe(HttpStatus .BAD_REQUEST .value())
73
136
}
74
137
75
138
it("returns a 403 when the upstream service provides a 403") {
76
- whenever(getCaseNotesForPersonService.execute(any() )).thenReturn(
139
+ whenever(getCaseNotesForPersonService.execute(caseNoteFilter, filters )).thenReturn(
77
140
Response (
78
141
data = emptyList(),
79
142
errors =
@@ -85,61 +148,35 @@ class CaseNotesControllerTest(
85
148
),
86
149
),
87
150
)
151
+
88
152
val result = mockMvc.performAuthorised(path)
89
153
result.response.status.shouldBe(HttpStatus .FORBIDDEN .value())
90
154
}
91
155
92
- it("returns the case notes for a person with the matching ID ") {
93
- val result = mockMvc.performAuthorised(path)
94
-
95
- result.response.contentAsString.shouldContain(
96
- """
97
- {
98
- "data": [
99
- {
100
- "caseNoteId": "abcd1234",
101
- "offenderIdentifier": null,
102
- "type": null,
103
- "typeDescription": null,
104
- "subType": null,
105
- "subTypeDescription": null,
106
- "creationDateTime": null,
107
- "occurrenceDateTime": null,
108
- "text": null,
109
- "locationId": null,
110
- "sensitive": false,
111
- "amendments": []
112
- }
113
- ],
114
- "pagination": {
115
- "isLastPage": true,
116
- "count": 1,
117
- "page": 1,
118
- "perPage": 10,
119
- "totalCount": 1,
120
- "totalPages": 1
121
- }
122
- }
123
- """.removeWhitespaceAndNewlines(),
156
+ it("returns a 400 when the upstream service returns entity not found") {
157
+ whenever(getCaseNotesForPersonService.execute(caseNoteFilter, filters)).thenReturn(
158
+ Response (
159
+ data = emptyList(),
160
+ errors =
161
+ listOf(
162
+ UpstreamApiError (
163
+ type = UpstreamApiError .Type .ENTITY_NOT_FOUND ,
164
+ causedBy = UpstreamApi .NOMIS ,
165
+ ),
166
+ ),
167
+ ),
124
168
)
125
- }
126
-
127
- it("logs audit") {
128
- mockMvc.performAuthorised(path)
129
169
130
- verify(
131
- auditService,
132
- VerificationModeFactory .times(1),
133
- ).createEvent("GET_CASE_NOTES ", mapOf("hmppsId" to hmppsId))
170
+ val result = mockMvc.performAuthorised(path)
171
+ result.response.status.shouldBe(HttpStatus .NOT_FOUND .value())
134
172
}
135
173
136
174
it("fails with the appropriate error when an upstream service is down") {
137
- whenever(getCaseNotesForPersonService.execute(any() )).doThrow(
175
+ whenever(getCaseNotesForPersonService.execute(caseNoteFilter, filters )).doThrow(
138
176
WebClientResponseException (500, "MockError ", null, null, null, null),
139
177
)
140
178
141
179
val response = mockMvc.performAuthorised(path)
142
-
143
180
assert (response.response.status == 500)
144
181
assert (
145
182
response.response.contentAsString.equals(
0 commit comments