1717
1818import com .amazonaws .services .dynamodbv2 .AmazonDynamoDB ;
1919import com .amazonaws .services .dynamodbv2 .document .Expected ;
20+
21+ import org .junit .Before ;
2022import org .junit .Rule ;
2123
2224import org .junit .Test ;
2931import org .springframework .beans .factory .annotation .Autowired ;
3032import org .springframework .context .annotation .Configuration ;
3133import org .springframework .dao .EmptyResultDataAccessException ;
34+ import org .springframework .data .domain .Page ;
35+ import org .springframework .data .domain .PageRequest ;
36+ import org .springframework .data .domain .Pageable ;
3237import org .springframework .test .context .ContextConfiguration ;
3338import org .springframework .test .context .TestExecutionListeners ;
3439import org .springframework .test .context .junit4 .SpringJUnit4ClassRunner ;
3944import java .util .List ;
4045import java .util .Optional ;
4146import java .util .concurrent .ThreadLocalRandom ;
47+ import java .util .function .Supplier ;
4248
4349import static org .hamcrest .CoreMatchers .hasItems ;
4450import static org .junit .Assert .assertEquals ;
@@ -64,6 +70,14 @@ public static class TestAppConfig {
6470
6571 @ Autowired
6672 private UserRepository userRepository ;
73+ @ Autowired
74+ private UserPaginationRepository userPaginationRepository ;
75+
76+ @ Before
77+ public void setUp () {
78+ userRepository .deleteAll ();
79+ userPaginationRepository .deleteAll ();
80+ }
6781
6882 @ Test
6983 public void testProjection () {
@@ -172,6 +186,42 @@ public void testDeleteNonExistent() {
172186 userRepository .deleteById ("non-existent" );
173187 }
174188
189+ @ Test
190+ public void testFilterAndPagination () {
191+
192+ Supplier <User > userSupplier = () -> {
193+ User u = new User ();
194+ u .setName ("test" );
195+ return u ;
196+ };
197+
198+ for (int i = 0 ; i < 22 ; i ++) {
199+ User u = userSupplier .get ();
200+ userPaginationRepository .save (u );
201+ }
202+ User u = userSupplier .get ();
203+ u .setName ("not-test" );
204+ userPaginationRepository .save (u );
205+
206+ List <User > allUsers = userPaginationRepository .findAll ();
207+ assertEquals (23 , allUsers .size ());
208+
209+ List <User > allTestUsers = userPaginationRepository .findAllByName ("test" );
210+ assertEquals (22 , allTestUsers .size ());
211+
212+ Pageable firstPage = PageRequest .of (0 , 10 );
213+ Page <User > firstResults = userPaginationRepository .findAllByName ("test" , firstPage );
214+ assertEquals (10 , firstResults .getNumberOfElements ());
215+
216+ Pageable secondPage = PageRequest .of (1 , 10 );
217+ Page <User > secondResults = userPaginationRepository .findAllByName ("test" , secondPage );
218+ assertEquals (10 , secondResults .getNumberOfElements ());
219+
220+ Pageable thirdPage = PageRequest .of (2 , 10 );
221+ Page <User > thirdResults = userPaginationRepository .findAllByName ("test" , thirdPage );
222+ assertEquals (2 , thirdResults .getNumberOfElements ());
223+ }
224+
175225 @ Test
176226 public void testDeleteNonExistentIdWithCondition () {
177227 // Delete conditional
0 commit comments