1
1
package uk .gov .hmcts .ccd .data .user ;
2
2
3
+ import java .net .URI ;
3
4
import java .util .Arrays ;
4
5
import java .util .Collection ;
5
6
import java .util .Set ;
6
7
import java .util .stream .Collectors ;
7
8
8
9
import static java .util .Arrays .asList ;
9
10
import static java .util .Collections .emptyList ;
11
+ import static java .util .Collections .singletonList ;
10
12
import static org .hamcrest .CoreMatchers .is ;
11
13
import static org .hamcrest .MatcherAssert .assertThat ;
12
14
import static org .hamcrest .Matchers .hasItems ;
13
15
import static org .hamcrest .Matchers .hasSize ;
14
16
import static org .junit .jupiter .api .Assertions .assertAll ;
15
17
import static org .junit .jupiter .api .Assertions .assertThrows ;
16
- import static org .mockito .Matchers .any ;
17
- import static org .mockito .Matchers .anyListOf ;
18
- import static org .mockito .Matchers .anyString ;
19
- import static org .mockito .Matchers .eq ;
18
+ import static org .mockito .ArgumentMatchers .any ;
19
+ import static org .mockito .ArgumentMatchers .anyList ;
20
+ import static org .mockito .ArgumentMatchers .anyString ;
21
+ import static org .mockito .ArgumentMatchers .eq ;
22
+ import static org .mockito .ArgumentMatchers .same ;
20
23
import static org .mockito .Mockito .doReturn ;
21
24
import static org .mockito .Mockito .doThrow ;
22
25
import static org .mockito .Mockito .verify ;
31
34
import org .mockito .Mock ;
32
35
import org .mockito .MockitoAnnotations ;
33
36
import org .springframework .http .HttpEntity ;
37
+ import org .springframework .http .HttpHeaders ;
34
38
import org .springframework .http .HttpMethod ;
39
+ import org .springframework .http .HttpStatus ;
35
40
import org .springframework .http .ResponseEntity ;
36
41
import org .springframework .security .core .Authentication ;
37
42
import org .springframework .security .core .GrantedAuthority ;
38
43
import org .springframework .security .core .context .SecurityContext ;
39
44
import org .springframework .security .core .context .SecurityContextHolder ;
40
45
import org .springframework .web .client .RestClientException ;
46
+ import org .springframework .web .client .RestClientResponseException ;
41
47
import org .springframework .web .client .RestTemplate ;
42
48
import uk .gov .hmcts .ccd .ApplicationParams ;
43
49
import uk .gov .hmcts .ccd .data .SecurityUtils ;
44
50
import uk .gov .hmcts .ccd .data .casedetails .SecurityClassification ;
45
51
import uk .gov .hmcts .ccd .data .definition .CaseDefinitionRepository ;
46
52
import uk .gov .hmcts .ccd .domain .model .aggregated .IdamUser ;
53
+ import uk .gov .hmcts .ccd .domain .model .aggregated .UserDefault ;
54
+ import uk .gov .hmcts .ccd .domain .model .definition .Jurisdiction ;
47
55
import uk .gov .hmcts .ccd .domain .model .definition .UserRole ;
56
+ import uk .gov .hmcts .ccd .endpoint .exceptions .BadRequestException ;
48
57
import uk .gov .hmcts .ccd .endpoint .exceptions .ServiceException ;
49
58
import uk .gov .hmcts .reform .auth .checker .spring .serviceanduser .ServiceAndUserDetails ;
50
59
@@ -123,7 +132,8 @@ void shouldOnlyConsiderRolesForJurisdiction() {
123
132
userRepository .getUserClassifications (JURISDICTION_ID );
124
133
125
134
assertAll (
126
- () -> verify (caseDefinitionRepository ).getClassificationsForUserRoleList (asList (ROLE_CASEWORKER_CMC )),
135
+ () -> verify (caseDefinitionRepository ).getClassificationsForUserRoleList (
136
+ singletonList (ROLE_CASEWORKER_CMC )),
127
137
() -> verifyNoMoreInteractions (caseDefinitionRepository )
128
138
);
129
139
}
@@ -141,7 +151,7 @@ void shouldConsiderCitizen() {
141
151
userRepository .getUserClassifications (JURISDICTION_ID );
142
152
143
153
assertAll (
144
- () -> verify (caseDefinitionRepository ).getClassificationsForUserRoleList (asList (CITIZEN )),
154
+ () -> verify (caseDefinitionRepository ).getClassificationsForUserRoleList (singletonList (CITIZEN )),
145
155
() -> verifyNoMoreInteractions (caseDefinitionRepository )
146
156
);
147
157
}
@@ -154,7 +164,8 @@ void shouldConsiderLetterHolder() {
154
164
userRepository .getUserClassifications (JURISDICTION_ID );
155
165
156
166
assertAll (
157
- () -> verify (caseDefinitionRepository ).getClassificationsForUserRoleList (asList (LETTER_HOLDER )),
167
+ () -> verify (caseDefinitionRepository ).getClassificationsForUserRoleList (
168
+ singletonList (LETTER_HOLDER )),
158
169
() -> verifyNoMoreInteractions (caseDefinitionRepository )
159
170
);
160
171
}
@@ -169,6 +180,69 @@ void shouldExcludeOtherRoles() {
169
180
}
170
181
}
171
182
183
+ @ Nested
184
+ @ DisplayName ("getUserDefaultSettings()" )
185
+ class GetUserDefaultSettings {
186
+
187
+ @ Test
188
+ @ DisplayName ("should return the User Profile defaults for the user" )
189
+ void shouldReturnUserProfileDefaultsForUser () {
190
+ when (applicationParams .userDefaultSettingsURL ()).thenReturn ("http://test.hmcts.net/users?uid={uid}" );
191
+ final Jurisdiction jurisdiction = new Jurisdiction ();
192
+ jurisdiction .setId ("TEST" );
193
+ jurisdiction .setName ("Test" );
194
+ jurisdiction .setDescription ("Test Jurisdiction" );
195
+ final UserDefault userDefault = new UserDefault ();
196
+ userDefault .setJurisdictions (singletonList (jurisdiction ));
197
+ final ResponseEntity <UserDefault > responseEntity = new ResponseEntity <>(userDefault , HttpStatus .OK );
198
+ when (restTemplate
199
+ .exchange (any (URI .class ), eq (HttpMethod .GET ), any (HttpEntity .class ), eq (UserDefault .class )))
200
+ .thenReturn (responseEntity );
201
+
202
+ final UserDefault result =
userRepository .
getUserDefaultSettings (
"[email protected] " );
203
+ assertThat (result , is (userDefault ));
204
+ verify (restTemplate ).exchange (
205
+ any (URI .class ), same (HttpMethod .GET ), any (HttpEntity .class ), (Class <?>)any (Class .class ));
206
+ }
207
+
208
+ @ Test
209
+ @ DisplayName ("should throw a BadRequestException if the User Profile defaults cannot be retrieved" )
210
+ void shouldThrowExceptionIfUserProfileCannotBeRetrieved () {
211
+ when (applicationParams .userDefaultSettingsURL ()).thenReturn ("http://test.hmcts.net/users?uid={uid}" );
212
+ final HttpHeaders headers = new HttpHeaders ();
213
+ headers .add ("Message" , "User Profile data could not be retrieved" );
214
+ final RestClientResponseException exception =
215
+ new RestClientResponseException ("Error on GET" , 400 , "Bad Request" , headers , null , null );
216
+ when (restTemplate
217
+ .exchange (any (URI .class ), eq (HttpMethod .GET ), any (HttpEntity .class ), eq (UserDefault .class )))
218
+ .thenThrow (exception );
219
+
220
+ final BadRequestException badRequestException =
221
+ assertThrows (BadRequestException .class ,
222
+ () ->
userRepository .
getUserDefaultSettings (
"[email protected] " ),
223
+ "Expected getUserDefaultSettings() to throw, but it didn't" );
224
+ assertThat (badRequestException .getMessage (), is (headers .getFirst ("Message" )));
225
+ }
226
+
227
+ @ Test
228
+ @ DisplayName ("should throw a ServiceException if an error occurs retrieving the User Profile defaults" )
229
+ void shouldThrowExceptionIfErrorOnRetrievingUserProfile () {
230
+ when (applicationParams .userDefaultSettingsURL ()).thenReturn ("http://test.hmcts.net/users?uid={uid}" );
231
+ final RestClientResponseException exception =
232
+ new RestClientResponseException (null , 500 , "Internal Server Error" , null , null , null );
233
+ when (restTemplate
234
+ .exchange (any (URI .class ), eq (HttpMethod .GET ), any (HttpEntity .class ), eq (UserDefault .class )))
235
+ .thenThrow (exception );
236
+
237
+ final String userId =
"[email protected] " ;
238
+ final ServiceException serviceException =
239
+ assertThrows (ServiceException .class ,
240
+ () -> userRepository .getUserDefaultSettings (userId ),
241
+ "Expected getUserDefaultSettings() to throw, but it didn't" );
242
+ assertThat (serviceException .getMessage (), is ("Problem getting user default settings for " + userId ));
243
+ }
244
+ }
245
+
172
246
@ Nested
173
247
@ DisplayName ("getHighestUserClassification()" )
174
248
class GetHighestUserClassification {
@@ -184,7 +258,8 @@ void shouldReturnHighestClassification() {
184
258
userRole2 .setSecurityClassification (SecurityClassification .PUBLIC .name ());
185
259
UserRole userRole3 = new UserRole ();
186
260
userRole3 .setSecurityClassification (SecurityClassification .RESTRICTED .name ());
187
- when (caseDefinitionRepository .getClassificationsForUserRoleList (anyListOf (String .class ))).thenReturn (asList (userRole1 , userRole2 , userRole3 ));
261
+ when (caseDefinitionRepository .getClassificationsForUserRoleList (anyList ()))
262
+ .thenReturn (asList (userRole1 , userRole2 , userRole3 ));
188
263
189
264
SecurityClassification result = userRepository .getHighestUserClassification (JURISDICTION_ID );
190
265
@@ -195,7 +270,7 @@ void shouldReturnHighestClassification() {
195
270
@ DisplayName ("should throw exception when no user roles returned" )
196
271
void shouldThrowExceptionWhenNoUserRolesReturned () {
197
272
asCaseworker ();
198
- when (caseDefinitionRepository .getClassificationsForUserRoleList (anyListOf ( String . class ))).thenReturn (emptyList ());
273
+ when (caseDefinitionRepository .getClassificationsForUserRoleList (anyList ( ))).thenReturn (emptyList ());
199
274
200
275
assertThrows (ServiceException .class , () -> userRepository .getHighestUserClassification (JURISDICTION_ID ));
201
276
}
0 commit comments