|
| 1 | +package com.baeldung.dao.repositories.product; |
| 2 | + |
| 3 | +import static org.hamcrest.Matchers.equalTo; |
| 4 | +import static org.hamcrest.Matchers.hasSize; |
| 5 | +import static org.junit.Assert.assertThat; |
| 6 | +import static org.junit.Assert.assertTrue; |
| 7 | + |
| 8 | +import java.util.Arrays; |
| 9 | +import java.util.List; |
| 10 | +import java.util.stream.Collectors; |
| 11 | + |
| 12 | +import org.junit.Before; |
| 13 | +import org.junit.Test; |
| 14 | +import org.junit.runner.RunWith; |
| 15 | +import org.springframework.beans.factory.annotation.Autowired; |
| 16 | +import org.springframework.data.domain.Page; |
| 17 | +import org.springframework.data.domain.PageRequest; |
| 18 | +import org.springframework.data.domain.Pageable; |
| 19 | +import org.springframework.data.domain.Sort; |
| 20 | +import org.springframework.test.context.ContextConfiguration; |
| 21 | +import org.springframework.test.context.junit4.SpringRunner; |
| 22 | +import org.springframework.transaction.annotation.EnableTransactionManagement; |
| 23 | +import org.springframework.transaction.annotation.Transactional; |
| 24 | + |
| 25 | +import com.baeldung.config.PersistenceProductConfiguration; |
| 26 | +import com.baeldung.domain.product.Product; |
| 27 | + |
| 28 | +@RunWith(SpringRunner.class) |
| 29 | +@ContextConfiguration(classes = { PersistenceProductConfiguration.class }) |
| 30 | +@EnableTransactionManagement |
| 31 | +public class ProductRepositoryIntegrationTest { |
| 32 | + |
| 33 | + @Autowired |
| 34 | + private ProductRepository productRepository; |
| 35 | + |
| 36 | + @Before |
| 37 | + @Transactional("productTransactionManager") |
| 38 | + public void setUp() { |
| 39 | + productRepository.save(Product.from(1001, "Book", 21)); |
| 40 | + productRepository.save(Product.from(1002, "Coffee", 10)); |
| 41 | + productRepository.save(Product.from(1003, "Jeans", 30)); |
| 42 | + productRepository.save(Product.from(1004, "Shirt", 32)); |
| 43 | + productRepository.save(Product.from(1005, "Bacon", 10)); |
| 44 | + } |
| 45 | + |
| 46 | + @Test |
| 47 | + public void whenRequestingFirstPageOfSizeTwo_ThenReturnFirstPage() { |
| 48 | + Pageable pageRequest = PageRequest.of(0, 2); |
| 49 | + |
| 50 | + Page<Product> result = productRepository.findAll(pageRequest); |
| 51 | + |
| 52 | + assertThat(result.getContent(), hasSize(2)); |
| 53 | + assertTrue(result.stream() |
| 54 | + .map(Product::getId) |
| 55 | + .allMatch(id -> Arrays.asList(1001, 1002) |
| 56 | + .contains(id))); |
| 57 | + } |
| 58 | + |
| 59 | + @Test |
| 60 | + public void whenRequestingSecondPageOfSizeTwo_ThenReturnSecondPage() { |
| 61 | + Pageable pageRequest = PageRequest.of(1, 2); |
| 62 | + |
| 63 | + Page<Product> result = productRepository.findAll(pageRequest); |
| 64 | + |
| 65 | + assertThat(result.getContent(), hasSize(2)); |
| 66 | + assertTrue(result.stream() |
| 67 | + .map(Product::getId) |
| 68 | + .allMatch(id -> Arrays.asList(1003, 1004) |
| 69 | + .contains(id))); |
| 70 | + } |
| 71 | + |
| 72 | + @Test |
| 73 | + public void whenRequestingLastPage_ThenReturnLastPageWithRemData() { |
| 74 | + Pageable pageRequest = PageRequest.of(2, 2); |
| 75 | + |
| 76 | + Page<Product> result = productRepository.findAll(pageRequest); |
| 77 | + |
| 78 | + assertThat(result.getContent(), hasSize(1)); |
| 79 | + assertTrue(result.stream() |
| 80 | + .map(Product::getId) |
| 81 | + .allMatch(id -> Arrays.asList(1005) |
| 82 | + .contains(id))); |
| 83 | + } |
| 84 | + |
| 85 | + @Test |
| 86 | + public void whenSortingByNameAscAndPaging_ThenReturnSortedPagedResult() { |
| 87 | + Pageable pageRequest = PageRequest.of(0, 3, Sort.by("name")); |
| 88 | + |
| 89 | + Page<Product> result = productRepository.findAll(pageRequest); |
| 90 | + |
| 91 | + assertThat(result.getContent(), hasSize(3)); |
| 92 | + assertThat(result.getContent() |
| 93 | + .stream() |
| 94 | + .map(Product::getId) |
| 95 | + .collect(Collectors.toList()), equalTo(Arrays.asList(1005, 1001, 1002))); |
| 96 | + |
| 97 | + } |
| 98 | + |
| 99 | + @Test |
| 100 | + public void whenSortingByPriceDescAndPaging_ThenReturnSortedPagedResult() { |
| 101 | + Pageable pageRequest = PageRequest.of(0, 3, Sort.by("price") |
| 102 | + .descending()); |
| 103 | + |
| 104 | + Page<Product> result = productRepository.findAll(pageRequest); |
| 105 | + |
| 106 | + assertThat(result.getContent(), hasSize(3)); |
| 107 | + assertThat(result.getContent() |
| 108 | + .stream() |
| 109 | + .map(Product::getId) |
| 110 | + .collect(Collectors.toList()), equalTo(Arrays.asList(1004, 1003, 1001))); |
| 111 | + |
| 112 | + } |
| 113 | + |
| 114 | + @Test |
| 115 | + public void whenSortingByPriceDescAndNameAscAndPaging_ThenReturnSortedPagedResult() { |
| 116 | + Pageable pageRequest = PageRequest.of(0, 5, Sort.by("price") |
| 117 | + .descending() |
| 118 | + .and(Sort.by("name"))); |
| 119 | + |
| 120 | + Page<Product> result = productRepository.findAll(pageRequest); |
| 121 | + |
| 122 | + assertThat(result.getContent(), hasSize(5)); |
| 123 | + assertThat(result.getContent() |
| 124 | + .stream() |
| 125 | + .map(Product::getId) |
| 126 | + .collect(Collectors.toList()), equalTo(Arrays.asList(1004, 1003, 1001, 1005, 1002))); |
| 127 | + |
| 128 | + } |
| 129 | + |
| 130 | + @Test |
| 131 | + public void whenRequestingFirstPageOfSizeTwoUsingCustomMethod_ThenReturnFirstPage() { |
| 132 | + Pageable pageRequest = PageRequest.of(0, 2); |
| 133 | + |
| 134 | + List<Product> result = productRepository.findAllByPrice(10, pageRequest); |
| 135 | + |
| 136 | + assertThat(result, hasSize(2)); |
| 137 | + assertTrue(result.stream() |
| 138 | + .map(Product::getId) |
| 139 | + .allMatch(id -> Arrays.asList(1002, 1005) |
| 140 | + .contains(id))); |
| 141 | + } |
| 142 | +} |
0 commit comments