Feature/spring data mongodb skip and limit (#16285)

* mongo repo with @Aggregation in skip & limit

* add tests for skip & limit in MongoRepository

* cleanup

* fix spaces

* use BDD naming conventions
This commit is contained in:
Usman Mohyuddin 2024-04-14 01:58:24 +05:00 committed by GitHub
parent eaf222dfa0
commit 4d6b46699c
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
3 changed files with 158 additions and 0 deletions

View File

@ -0,0 +1,36 @@
package com.baeldung.projection.model;
import org.springframework.data.annotation.Id;
import org.springframework.data.mongodb.core.mapping.Document;
@Document(collection = "student")
public class Student {
@Id
private String id;
private String name;
private Long age;
public Student(String id, String name, Long age) {
this.id = id;
this.name = name;
this.age = age;
}
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Long getAge() {
return age;
}
public void setAge(Long age) {
this.age = age;
}
}

View File

@ -0,0 +1,32 @@
package com.baeldung.projection.repository;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.mongodb.repository.Aggregation;
import org.springframework.data.mongodb.repository.MongoRepository;
import com.baeldung.projection.model.Student;
import java.util.List;
public interface StudentRepository extends MongoRepository<Student, String> {
@Aggregation(pipeline = {
"{ '$skip': ?0 }",
"{ '$limit': ?1 }"
})
List<Student> findAll(Long skip, Long limit);
@Aggregation(pipeline = {
"{ '$match': { 'age': ?0 } }",
"{ $skip: ?1 }",
"{ $limit: ?2 }"
})
List<Student> findAllByAgeCriteria(Long age, Long skip, Long limit);
@Aggregation(pipeline = {
"{ '$match': { 'id' : ?0 } }",
"{ '$sort' : { 'id' : 1 } }",
"{ '$skip' : ?1 }",
"{ '$limit' : ?2 }"
})
List<Student> findByStudentId(final String studentId, Long skip, Long limit);
Page<Student> findAll(Pageable pageable);
}

View File

@ -0,0 +1,90 @@
package com.baeldung.projection;
import com.baeldung.projection.config.ProjectionConfig;
import com.baeldung.projection.model.Student;
import com.baeldung.projection.repository.StudentRepository;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import java.util.Arrays;
import java.util.List;
import static org.junit.Assert.assertEquals;
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(classes = ProjectionConfig.class)
public class StudentIntegrationTest {
@Autowired
private StudentRepository studentRepository;
private List<Student> studentList;
@Before
public void setUp() {
Student student1 = new Student("A", "Abraham", 15L);
Student student2 = new Student("B", "Usman", 30L);
Student student3 = new Student("C", "David", 20L);
Student student4 = new Student("D", "Tina", 45L);
Student student5 = new Student("E", "Maria", 33L);
studentList = Arrays.asList(student1, student2, student3, student4, student5);
studentRepository.saveAll(studentList);
}
@Test
public void whenRetrievingAllStudents_thenReturnsCorrectNumberOfRecords() {
// WHEN
List<Student> result = studentRepository.findAll(0L, 5L);
// THEN
assertEquals(5, result.size());
}
@Test
public void whenLimitingAndSkipping_thenReturnsLimitedStudents() {
// WHEN
List<Student> result = studentRepository.findAll(3L, 2L);
// THEN
assertEquals(2, result.size());
assertEquals("Tina", result.get(0).getName());
assertEquals("Maria", result.get(1).getName());
}
@Test
public void whenFilteringByAge_thenReturnsStudentsMatchingCriteria() {
// WHEN
List<Student> result = studentRepository.findAllByAgeCriteria(30L, 0L, 10L);
// THEN
assertEquals(1, result.size());
assertEquals("Usman", result.get(0).getName());
}
@Test
public void whenFindingById_thenReturnsMatchingStudent() {
// WHEN
List<Student> result = studentRepository.findByStudentId("A", 0L, 5L);
// THEN
assertEquals(1, result.size());
assertEquals("Abraham", result.get(0).getName());
}
@Test
public void whenFindByStudentIdUsingPageable_thenReturnsPageOfStudents() {
// GIVEN
Sort sort = Sort.by(Sort.Direction.DESC, "id");
Pageable pageable = PageRequest.of(0, 5, sort);
// WHEN
Page<Student> resultPage = studentRepository.findAll(pageable);
// THEN
assertEquals(5, resultPage.getTotalElements());
assertEquals("Maria", resultPage.getContent().get(0).getName());
}
}