|
| 1 | +package tech.ydb.data.repository.config; |
| 2 | + |
| 3 | +import java.lang.reflect.Field; |
| 4 | +import java.util.Optional; |
| 5 | + |
| 6 | +import org.assertj.core.api.Assertions; |
| 7 | +import org.junit.jupiter.api.Test; |
| 8 | +import org.springframework.beans.factory.annotation.Autowired; |
| 9 | +import org.springframework.data.annotation.Id; |
| 10 | +import org.springframework.data.relational.core.mapping.Table; |
| 11 | +import org.springframework.data.repository.CrudRepository; |
| 12 | +import org.springframework.data.repository.core.support.RepositoryFactoryBeanSupport; |
| 13 | +import org.springframework.util.ReflectionUtils; |
| 14 | + |
| 15 | +import tech.ydb.data.YdbBaseTest; |
| 16 | +import tech.ydb.data.repository.ViewIndex; |
| 17 | + |
| 18 | +/** |
| 19 | + * Tests for {@link JdbcRepositoryBeanPostProcessor}. |
| 20 | + * |
| 21 | + * @author Mikhail Polivakha |
| 22 | + */ |
| 23 | +class JdbcRepositoryBeanPostProcessorTest extends YdbBaseTest { |
| 24 | + |
| 25 | + @Autowired |
| 26 | + private RepositoryFactoryBeanSupport<UserRepository, User, Long> userFactoryBeanSupport; |
| 27 | + |
| 28 | + @Autowired |
| 29 | + private RepositoryFactoryBeanSupport<AddressRepository, Address, Long> addressFactoryBeanSupport; |
| 30 | + |
| 31 | + @Test |
| 32 | + void shouldExposeMetadataOnlyForRepositoriesWithViewIndexMethods() { |
| 33 | + |
| 34 | + // given. |
| 35 | + Field exposeMetadataField = ReflectionUtils.findField(RepositoryFactoryBeanSupport.class, "exposeMetadata"); |
| 36 | + exposeMetadataField.setAccessible(true); |
| 37 | + |
| 38 | + // when. |
| 39 | + Object userFactoryExposedMetadata = ReflectionUtils.getField(exposeMetadataField, userFactoryBeanSupport); |
| 40 | + Object addressFactoryExposedMetadata = ReflectionUtils.getField(exposeMetadataField, addressFactoryBeanSupport); |
| 41 | + |
| 42 | + // then. |
| 43 | + Assertions.assertThat(userFactoryExposedMetadata).isEqualTo(true); |
| 44 | + Assertions.assertThat(addressFactoryExposedMetadata).isEqualTo(false); |
| 45 | + } |
| 46 | + |
| 47 | + @Table |
| 48 | + static class User { |
| 49 | + |
| 50 | + @Id |
| 51 | + private Long id; |
| 52 | + |
| 53 | + private String name; |
| 54 | + } |
| 55 | + |
| 56 | + @Table |
| 57 | + static class Address { |
| 58 | + |
| 59 | + @Id |
| 60 | + private Long id; |
| 61 | + } |
| 62 | + |
| 63 | + interface UserRepository extends CrudRepository<User, Long> { |
| 64 | + |
| 65 | + @ViewIndex(indexName = "name_authors_index", tableName = "authors") |
| 66 | + Optional<User> findUserByName(String name); |
| 67 | + } |
| 68 | + |
| 69 | + interface AddressRepository extends CrudRepository<Address, Long> { |
| 70 | + |
| 71 | + } |
| 72 | +} |
0 commit comments