|
| 1 | +package com.cosium.spring.data.jpa.entity.graph.repository; |
| 2 | + |
| 3 | +import static org.assertj.core.api.Assertions.assertThat; |
| 4 | + |
| 5 | +import com.cosium.spring.data.jpa.entity.graph.BaseTest; |
| 6 | +import com.cosium.spring.data.jpa.entity.graph.domain2.EntityGraph; |
| 7 | +import com.cosium.spring.data.jpa.entity.graph.domain2.NamedEntityGraph; |
| 8 | +import com.cosium.spring.data.jpa.entity.graph.sample.Product; |
| 9 | +import com.cosium.spring.data.jpa.entity.graph.sample.ProductEntityGraph; |
| 10 | +import com.cosium.spring.data.jpa.entity.graph.sample.Product_; |
| 11 | +import com.github.springtestdbunit.annotation.DatabaseSetup; |
| 12 | +import java.util.Optional; |
| 13 | +import org.hibernate.Hibernate; |
| 14 | +import org.junit.jupiter.api.DisplayName; |
| 15 | +import org.junit.jupiter.api.Test; |
| 16 | +import org.springframework.beans.factory.annotation.Autowired; |
| 17 | +import org.springframework.data.domain.Page; |
| 18 | +import org.springframework.data.domain.Pageable; |
| 19 | +import org.springframework.data.domain.Slice; |
| 20 | +import org.springframework.data.jpa.domain.Specification; |
| 21 | +import org.springframework.data.repository.query.FluentQuery; |
| 22 | +import org.springframework.transaction.annotation.Transactional; |
| 23 | + |
| 24 | +/** |
| 25 | + * @author Réda Housni Alaoui |
| 26 | + */ |
| 27 | +@DatabaseSetup(BaseTest.DATASET) |
| 28 | +class FluentQueryTest extends BaseTest { |
| 29 | + |
| 30 | + @Autowired private ProductRepository productRepository; |
| 31 | + |
| 32 | + @Transactional |
| 33 | + @Test |
| 34 | + @DisplayName("It supports named entity graphs") |
| 35 | + void test1() { |
| 36 | + NamedEntityGraph entityGraph = NamedEntityGraph.loading(Product.BRAND_EG); |
| 37 | + |
| 38 | + Product product = |
| 39 | + productRepository.findBy( |
| 40 | + idEquals(1L), entityGraph, FluentQuery.FetchableFluentQuery::oneValue); |
| 41 | + |
| 42 | + assertThat(Hibernate.isInitialized(product.getBrand())).isTrue(); |
| 43 | + } |
| 44 | + |
| 45 | + @Transactional |
| 46 | + @Test |
| 47 | + @DisplayName("It supports dynamic entity graphs") |
| 48 | + void test2() { |
| 49 | + Product product = |
| 50 | + productRepository.findBy( |
| 51 | + idEquals(1L), |
| 52 | + ProductEntityGraph.____().brand().____.maker().country().____.____(), |
| 53 | + FluentQuery.FetchableFluentQuery::oneValue); |
| 54 | + |
| 55 | + assertThat(Hibernate.isInitialized(product.getBrand())).isTrue(); |
| 56 | + assertThat(Hibernate.isInitialized(product.getMaker())).isTrue(); |
| 57 | + assertThat(Hibernate.isInitialized(product.getMaker().getCountry())).isTrue(); |
| 58 | + |
| 59 | + assertThat(Hibernate.isInitialized(product.getCategory())).isFalse(); |
| 60 | + } |
| 61 | + |
| 62 | + @Transactional |
| 63 | + @Test |
| 64 | + @DisplayName("It uses default entity graphs when null entity graph is provided") |
| 65 | + void test3() { |
| 66 | + Product product = |
| 67 | + productRepository.findBy(idEquals(1L), null, FluentQuery.FetchableFluentQuery::oneValue); |
| 68 | + |
| 69 | + assertThat(Hibernate.isInitialized(product.getCategory())).isTrue(); |
| 70 | + |
| 71 | + assertThat(Hibernate.isInitialized(product.getBrand())).isFalse(); |
| 72 | + assertThat(Hibernate.isInitialized(product.getMaker())).isFalse(); |
| 73 | + assertThat(Hibernate.isInitialized(product.getMaker().getCountry())).isFalse(); |
| 74 | + } |
| 75 | + |
| 76 | + @Transactional |
| 77 | + @Test |
| 78 | + @DisplayName("It uses default entity graphs when noop entity graph is provided") |
| 79 | + void test4() { |
| 80 | + Product product = |
| 81 | + productRepository.findBy( |
| 82 | + idEquals(1L), EntityGraph.NOOP, FluentQuery.FetchableFluentQuery::oneValue); |
| 83 | + |
| 84 | + assertThat(Hibernate.isInitialized(product.getCategory())).isTrue(); |
| 85 | + |
| 86 | + assertThat(Hibernate.isInitialized(product.getBrand())).isFalse(); |
| 87 | + assertThat(Hibernate.isInitialized(product.getMaker())).isFalse(); |
| 88 | + assertThat(Hibernate.isInitialized(product.getMaker().getCountry())).isFalse(); |
| 89 | + } |
| 90 | + |
| 91 | + @Transactional |
| 92 | + @Test |
| 93 | + @DisplayName("It supports pagination") |
| 94 | + void test5() { |
| 95 | + Page<Product> products = |
| 96 | + productRepository.findBy( |
| 97 | + idEquals(1L), |
| 98 | + ProductEntityGraph.____().brand().____.maker().country().____.____(), |
| 99 | + query -> query.page(Pageable.ofSize(1))); |
| 100 | + |
| 101 | + assertThat(products).hasSize(1); |
| 102 | + |
| 103 | + Product product = products.stream().findFirst().orElseThrow(); |
| 104 | + assertThat(Hibernate.isInitialized(product.getBrand())).isTrue(); |
| 105 | + assertThat(Hibernate.isInitialized(product.getMaker())).isTrue(); |
| 106 | + assertThat(Hibernate.isInitialized(product.getMaker().getCountry())).isTrue(); |
| 107 | + |
| 108 | + assertThat(Hibernate.isInitialized(product.getCategory())).isFalse(); |
| 109 | + } |
| 110 | + |
| 111 | + @Transactional |
| 112 | + @Test |
| 113 | + @DisplayName("It supports slicing") |
| 114 | + void test6() { |
| 115 | + Slice<Product> products = |
| 116 | + productRepository.findBy( |
| 117 | + idEquals(1L), |
| 118 | + ProductEntityGraph.____().brand().____.maker().country().____.____(), |
| 119 | + query -> query.slice(Pageable.ofSize(1))); |
| 120 | + |
| 121 | + assertThat(products).hasSize(1); |
| 122 | + |
| 123 | + Product product = products.stream().findFirst().orElseThrow(); |
| 124 | + assertThat(Hibernate.isInitialized(product.getBrand())).isTrue(); |
| 125 | + assertThat(Hibernate.isInitialized(product.getMaker())).isTrue(); |
| 126 | + assertThat(Hibernate.isInitialized(product.getMaker().getCountry())).isTrue(); |
| 127 | + |
| 128 | + assertThat(Hibernate.isInitialized(product.getCategory())).isFalse(); |
| 129 | + } |
| 130 | + |
| 131 | + @Transactional |
| 132 | + @Test |
| 133 | + @DisplayName("It supports counting") |
| 134 | + void test7() { |
| 135 | + long count = |
| 136 | + productRepository.findBy( |
| 137 | + idEquals(1L), |
| 138 | + ProductEntityGraph.____().brand().____.maker().country().____.____(), |
| 139 | + FluentQuery.FetchableFluentQuery::count); |
| 140 | + |
| 141 | + assertThat(count).isEqualTo(1); |
| 142 | + } |
| 143 | + |
| 144 | + @Transactional |
| 145 | + @Test |
| 146 | + @DisplayName("It uses default entity graphs when the entity graph less method is used") |
| 147 | + void test8() { |
| 148 | + Product product = |
| 149 | + productRepository.findBy(idEquals(1L), FluentQuery.FetchableFluentQuery::oneValue); |
| 150 | + |
| 151 | + assertThat(Hibernate.isInitialized(product.getCategory())).isTrue(); |
| 152 | + |
| 153 | + assertThat(Hibernate.isInitialized(product.getBrand())).isFalse(); |
| 154 | + assertThat(Hibernate.isInitialized(product.getMaker())).isFalse(); |
| 155 | + assertThat(Hibernate.isInitialized(product.getMaker().getCountry())).isFalse(); |
| 156 | + } |
| 157 | + |
| 158 | + private static Specification<Product> idEquals(long id) { |
| 159 | + return (root, query, cb) -> cb.equal(root.get(Product_.id), id); |
| 160 | + } |
| 161 | + |
| 162 | + public interface ProductRepository |
| 163 | + extends EntityGraphJpaRepository<Product, Long>, |
| 164 | + EntityGraphJpaSpecificationExecutor<Product> { |
| 165 | + |
| 166 | + @Override |
| 167 | + default Optional<EntityGraph> defaultEntityGraph() { |
| 168 | + return Optional.of(ProductEntityGraph.____().category().____.____()); |
| 169 | + } |
| 170 | + } |
| 171 | +} |
0 commit comments