diff --git a/persistence-modules/spring-data-mongodb-2/src/main/java/com/baeldung/projection/model/Student.java b/persistence-modules/spring-data-mongodb-2/src/main/java/com/baeldung/projection/model/Student.java new file mode 100644 index 0000000000..5560d9c689 --- /dev/null +++ b/persistence-modules/spring-data-mongodb-2/src/main/java/com/baeldung/projection/model/Student.java @@ -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; + } +} diff --git a/persistence-modules/spring-data-mongodb-2/src/main/java/com/baeldung/projection/repository/StudentRepository.java b/persistence-modules/spring-data-mongodb-2/src/main/java/com/baeldung/projection/repository/StudentRepository.java new file mode 100644 index 0000000000..ec148400a9 --- /dev/null +++ b/persistence-modules/spring-data-mongodb-2/src/main/java/com/baeldung/projection/repository/StudentRepository.java @@ -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 { + @Aggregation(pipeline = { + "{ '$skip': ?0 }", + "{ '$limit': ?1 }" + }) + List findAll(Long skip, Long limit); + @Aggregation(pipeline = { + "{ '$match': { 'age': ?0 } }", + "{ $skip: ?1 }", + "{ $limit: ?2 }" + }) + List findAllByAgeCriteria(Long age, Long skip, Long limit); + @Aggregation(pipeline = { + "{ '$match': { 'id' : ?0 } }", + "{ '$sort' : { 'id' : 1 } }", + "{ '$skip' : ?1 }", + "{ '$limit' : ?2 }" + }) + List findByStudentId(final String studentId, Long skip, Long limit); + Page findAll(Pageable pageable); +} diff --git a/persistence-modules/spring-data-mongodb-2/src/test/java/com/baeldung/projection/StudentIntegrationTest.java b/persistence-modules/spring-data-mongodb-2/src/test/java/com/baeldung/projection/StudentIntegrationTest.java new file mode 100644 index 0000000000..ac68a2db32 --- /dev/null +++ b/persistence-modules/spring-data-mongodb-2/src/test/java/com/baeldung/projection/StudentIntegrationTest.java @@ -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 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 result = studentRepository.findAll(0L, 5L); + // THEN + assertEquals(5, result.size()); + } + + @Test + public void whenLimitingAndSkipping_thenReturnsLimitedStudents() { + // WHEN + List 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 result = studentRepository.findAllByAgeCriteria(30L, 0L, 10L); + // THEN + assertEquals(1, result.size()); + assertEquals("Usman", result.get(0).getName()); + } + + @Test + public void whenFindingById_thenReturnsMatchingStudent() { + // WHEN + List 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 resultPage = studentRepository.findAll(pageable); + + // THEN + assertEquals(5, resultPage.getTotalElements()); + assertEquals("Maria", resultPage.getContent().get(0).getName()); + } +} \ No newline at end of file