1717import static org .hamcrest .Matchers .is ;
1818import static org .junit .jupiter .api .Assertions .assertThrows ;
1919
20+ import fr .insee .sugoi .core .configuration .GlobalKeysConfig ;
21+ import fr .insee .sugoi .core .event .configuration .EventKeysConfig ;
2022import fr .insee .sugoi .core .event .publisher .SugoiEventPublisher ;
2123import fr .insee .sugoi .core .model .ProviderRequest ;
2224import fr .insee .sugoi .core .model .ProviderResponse ;
3335import fr .insee .sugoi .model .exceptions .RealmNotFoundException ;
3436import fr .insee .sugoi .model .exceptions .UserAlreadyExistException ;
3537import fr .insee .sugoi .model .exceptions .UserNotFoundException ;
38+ import fr .insee .sugoi .model .paging .PageResult ;
3639import fr .insee .sugoi .model .paging .PageableResult ;
3740import fr .insee .sugoi .model .paging .SearchType ;
3841import java .io .ByteArrayInputStream ;
4245import java .util .Base64 ;
4346import java .util .List ;
4447import java .util .Optional ;
48+ import java .util .stream .Collectors ;
49+ import java .util .stream .Stream ;
4550import org .junit .jupiter .api .BeforeEach ;
4651import org .junit .jupiter .api .DisplayName ;
4752import org .junit .jupiter .api .Test ;
4853import org .junit .jupiter .api .extension .ExtendWith ;
4954import org .mockito .Mock ;
5055import org .mockito .Mockito ;
56+ import org .mockito .invocation .InvocationOnMock ;
5157import org .springframework .beans .factory .annotation .Autowired ;
5258import org .springframework .boot .context .properties .EnableConfigurationProperties ;
5359import org .springframework .boot .test .mock .mockito .MockBean ;
@@ -109,6 +115,7 @@ public void setup() {
109115
110116 realm = new Realm ();
111117 realm .setName ("realm" );
118+ realm .addProperty (GlobalKeysConfig .USERS_MAX_OUTPUT_SIZE , "100000" );
112119 UserStorage us1 = new UserStorage ();
113120 us1 .setName ("us1" );
114121 UserStorage us2 = new UserStorage ();
@@ -125,6 +132,11 @@ public void setup() {
125132 Mockito .when (readerStore2 .getUser ("UserWithCertificate" ))
126133 .thenReturn (Optional .of (userWithCertificate ));
127134
135+ Mockito .when (readerStore1 .searchUsers (Mockito .any (), Mockito .any (), Mockito .eq ("AND" )))
136+ .thenAnswer (invocation -> mockPageResultFromNUsersUs (invocation , 20000 ));
137+ Mockito .when (readerStore2 .searchUsers (Mockito .any (), Mockito .any (), Mockito .eq ("AND" )))
138+ .thenAnswer (invocation -> mockPageResultFromNUsersUs (invocation , 20000 ));
139+
128140 Mockito .when (storeProvider .getReaderStore (Mockito .eq ("idonotexist" ), Mockito .anyString ()))
129141 .thenThrow (RealmNotFoundException .class );
130142 Mockito .when (storeProvider .getWriterStore (Mockito .eq ("idonotexist" ), Mockito .anyString ()))
@@ -256,4 +268,38 @@ public void defaultConfigTestVerifyUniqueMail() {
256268 UserAlreadyExistException .class ,
257269 () -> userService .create ("realm" , "us2" , user1 , new ProviderRequest ()));
258270 }
271+
272+ @ Test
273+ @ DisplayName (
274+ "Given we ask for 30000 users on a realm with two storages each containing 20000 users, "
275+ + "with a limitation of the maximum requested users on the realm of 100000, "
276+ + "then we should get 30000 users from both userstorages" )
277+ public void getUsersWithPageableSizeShouldReturnEnoughUsers () {
278+ PageableResult pageable = new PageableResult (30000 , 0 , null );
279+ List <User > results =
280+ userService
281+ .findByProperties (realm .getName (), null , new User (), pageable , SearchType .AND )
282+ .getResults ();
283+ assertThat ("30000 users are retrieved" , results .size (), is (30000 ));
284+ assertThat (
285+ "Should contain users from userstorage1" ,
286+ results .stream ()
287+ .anyMatch (u -> u .getMetadatas ().get (EventKeysConfig .USERSTORAGE ).equals ("us1" )));
288+ assertThat (
289+ "Should contain users from userstorage2" ,
290+ results .stream ()
291+ .anyMatch (u -> u .getMetadatas ().get (EventKeysConfig .USERSTORAGE ).equals ("us2" )));
292+ }
293+
294+ private PageResult <User > mockPageResultFromNUsersUs (
295+ InvocationOnMock invocation , int nbUsersInUs ) {
296+ PageResult <User > pageResult = new PageResult <>();
297+ int minOfRequestedOrAllUsers =
298+ Math .min (((PageableResult ) invocation .getArgument (1 )).getSize (), nbUsersInUs );
299+ pageResult .setResults (
300+ Stream .generate (User ::new ).limit (minOfRequestedOrAllUsers ).collect (Collectors .toList ()));
301+ pageResult .setPageSize (nbUsersInUs );
302+ pageResult .setTotalElements (minOfRequestedOrAllUsers );
303+ return pageResult ;
304+ }
259305}
0 commit comments