Skip to content

Commit 4a0cc22

Browse files
authored
Merge pull request #181 from derjust/Issue_180
Issue #179: Test for filter + pagination
2 parents 2d6cb4d + 7112452 commit 4a0cc22

File tree

4 files changed

+96
-0
lines changed

4 files changed

+96
-0
lines changed

src/test/java/org/socialsignin/spring/data/dynamodb/domain/sample/CRUDOperationsIT.java

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@
1717

1818
import com.amazonaws.services.dynamodbv2.AmazonDynamoDB;
1919
import com.amazonaws.services.dynamodbv2.document.Expected;
20+
21+
import org.junit.Before;
2022
import org.junit.Rule;
2123

2224
import org.junit.Test;
@@ -29,6 +31,9 @@
2931
import org.springframework.beans.factory.annotation.Autowired;
3032
import org.springframework.context.annotation.Configuration;
3133
import 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;
3237
import org.springframework.test.context.ContextConfiguration;
3338
import org.springframework.test.context.TestExecutionListeners;
3439
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
@@ -39,6 +44,7 @@
3944
import java.util.List;
4045
import java.util.Optional;
4146
import java.util.concurrent.ThreadLocalRandom;
47+
import java.util.function.Supplier;
4248

4349
import static org.hamcrest.CoreMatchers.hasItems;
4450
import 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

src/test/java/org/socialsignin/spring/data/dynamodb/domain/sample/User.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@
1515
*/
1616
package org.socialsignin.spring.data.dynamodb.domain.sample;
1717

18+
import com.amazonaws.services.dynamodbv2.datamodeling.DynamoDBAutoGeneratedDefault;
19+
import com.amazonaws.services.dynamodbv2.datamodeling.DynamoDBAutoGeneratedKey;
1820
import com.amazonaws.services.dynamodbv2.datamodeling.DynamoDBHashKey;
1921
import com.amazonaws.services.dynamodbv2.datamodeling.DynamoDBIndexHashKey;
2022
import com.amazonaws.services.dynamodbv2.datamodeling.DynamoDBIndexRangeKey;
@@ -91,6 +93,7 @@ public void setPostCode(String postCode) {
9193
}
9294

9395
@DynamoDBHashKey(attributeName = "Id")
96+
@DynamoDBAutoGeneratedKey
9497
public String getId() {
9598
return id;
9699
}
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
/**
2+
* Copyright © 2018 spring-data-dynamodb (https://github.com/derjust/spring-data-dynamodb)
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package org.socialsignin.spring.data.dynamodb.domain.sample;
17+
18+
import java.util.List;
19+
20+
import org.socialsignin.spring.data.dynamodb.repository.EnableScan;
21+
import org.socialsignin.spring.data.dynamodb.repository.EnableScanCount;
22+
import org.springframework.data.domain.Page;
23+
import org.springframework.data.domain.Pageable;
24+
import org.springframework.data.repository.PagingAndSortingRepository;
25+
26+
public interface UserPaginationRepository extends PagingAndSortingRepository<User, String> {
27+
28+
@EnableScan
29+
@EnableScanCount
30+
Page<User> findAllByName(String name, Pageable pageable);
31+
32+
@EnableScan
33+
List<User> findAllByName(String name);
34+
35+
@EnableScan
36+
List<User> findAll();
37+
38+
@EnableScan
39+
void deleteAll();
40+
}

src/test/java/org/socialsignin/spring/data/dynamodb/domain/sample/UserRepository.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,4 +55,7 @@ public interface UserRepository extends CrudRepository<User, String> {
5555
User findByNameAndLeaveDate(String name, Instant leaveDate);
5656

5757
void deleteByPostCodeAndNumberOfPlaylists(String postCode, Integer numberOfPlaylists);
58+
59+
@EnableScan
60+
void deleteAll();
5861
}

0 commit comments

Comments
 (0)