|
| 1 | +/* |
| 2 | + * Copyright 2024 the original author or authors. |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * https://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | + |
| 17 | +package org.springframework.data.jdbc.repository; |
| 18 | + |
| 19 | +import static org.assertj.core.api.Assertions.*; |
| 20 | +import static org.mockito.Mockito.*; |
| 21 | + |
| 22 | +import org.jetbrains.annotations.NotNull; |
| 23 | +import org.junit.jupiter.api.Test; |
| 24 | +import org.mockito.ArgumentCaptor; |
| 25 | +import org.springframework.context.ApplicationEventPublisher; |
| 26 | +import org.springframework.data.annotation.Id; |
| 27 | +import org.springframework.data.jdbc.core.convert.DataAccessStrategy; |
| 28 | +import org.springframework.data.jdbc.core.convert.DefaultJdbcTypeFactory; |
| 29 | +import org.springframework.data.jdbc.core.convert.DelegatingDataAccessStrategy; |
| 30 | +import org.springframework.data.jdbc.core.convert.JdbcConverter; |
| 31 | +import org.springframework.data.jdbc.core.convert.JdbcCustomConversions; |
| 32 | +import org.springframework.data.jdbc.core.convert.MappingJdbcConverter; |
| 33 | +import org.springframework.data.jdbc.core.mapping.JdbcMappingContext; |
| 34 | +import org.springframework.data.jdbc.repository.query.Query; |
| 35 | +import org.springframework.data.jdbc.repository.support.JdbcRepositoryFactory; |
| 36 | +import org.springframework.data.relational.core.dialect.Dialect; |
| 37 | +import org.springframework.data.relational.core.dialect.HsqlDbDialect; |
| 38 | +import org.springframework.data.relational.core.mapping.RelationalMappingContext; |
| 39 | +import org.springframework.data.relational.core.mapping.Table; |
| 40 | +import org.springframework.data.repository.CrudRepository; |
| 41 | +import org.springframework.jdbc.core.RowMapper; |
| 42 | +import org.springframework.jdbc.core.namedparam.NamedParameterJdbcOperations; |
| 43 | +import org.springframework.jdbc.core.namedparam.SqlParameterSource; |
| 44 | +import org.springframework.lang.Nullable; |
| 45 | + |
| 46 | +/** |
| 47 | + * Extracts the SQL statement that results from declared queries of a repository and perform assertions on it. |
| 48 | + * |
| 49 | + * @author Jens Schauder |
| 50 | + */ |
| 51 | +public class DeclaredQueryRepositoryUnitTests { |
| 52 | + |
| 53 | + private NamedParameterJdbcOperations operations = mock(NamedParameterJdbcOperations.class, RETURNS_DEEP_STUBS); |
| 54 | + |
| 55 | + @Test // GH-1856 |
| 56 | + void plainSql() { |
| 57 | + |
| 58 | + repository(DummyEntityRepository.class).plainQuery(); |
| 59 | + |
| 60 | + assertThat(query()).isEqualTo("select * from someTable"); |
| 61 | + } |
| 62 | + |
| 63 | + @Test // GH-1856 |
| 64 | + void tableNameQuery() { |
| 65 | + |
| 66 | + repository(DummyEntityRepository.class).tableNameQuery(); |
| 67 | + |
| 68 | + assertThat(query()).isEqualTo("select * from \"DUMMY_ENTITY\""); |
| 69 | + } |
| 70 | + |
| 71 | + @Test // GH-1856 |
| 72 | + void renamedTableNameQuery() { |
| 73 | + |
| 74 | + repository(RenamedEntityRepository.class).tableNameQuery(); |
| 75 | + |
| 76 | + assertThat(query()).isEqualTo("select * from \"ReNamed\""); |
| 77 | + } |
| 78 | + |
| 79 | + @Test // GH-1856 |
| 80 | + void fullyQualifiedTableNameQuery() { |
| 81 | + |
| 82 | + repository(RenamedEntityRepository.class).qualifiedTableNameQuery(); |
| 83 | + |
| 84 | + assertThat(query()).isEqualTo("select * from \"someSchema\".\"ReNamed\""); |
| 85 | + } |
| 86 | + |
| 87 | + private String query() { |
| 88 | + |
| 89 | + ArgumentCaptor<String> queryCaptor = ArgumentCaptor.forClass(String.class); |
| 90 | + verify(operations).queryForObject(queryCaptor.capture(), any(SqlParameterSource.class), any(RowMapper.class)); |
| 91 | + return queryCaptor.getValue(); |
| 92 | + } |
| 93 | + |
| 94 | + private @NotNull <T extends CrudRepository> T repository(Class<T> repositoryInterface) { |
| 95 | + |
| 96 | + Dialect dialect = HsqlDbDialect.INSTANCE; |
| 97 | + |
| 98 | + RelationalMappingContext context = new JdbcMappingContext(); |
| 99 | + |
| 100 | + DelegatingDataAccessStrategy delegatingDataAccessStrategy = new DelegatingDataAccessStrategy(); |
| 101 | + JdbcConverter converter = new MappingJdbcConverter(context, delegatingDataAccessStrategy, |
| 102 | + new JdbcCustomConversions(), new DefaultJdbcTypeFactory(operations.getJdbcOperations())); |
| 103 | + |
| 104 | + DataAccessStrategy dataAccessStrategy = mock(DataAccessStrategy.class); |
| 105 | + ApplicationEventPublisher publisher = mock(ApplicationEventPublisher.class); |
| 106 | + |
| 107 | + JdbcRepositoryFactory factory = new JdbcRepositoryFactory(dataAccessStrategy, context, converter, dialect, |
| 108 | + publisher, operations); |
| 109 | + |
| 110 | + return factory.getRepository(repositoryInterface); |
| 111 | + } |
| 112 | + |
| 113 | + @Table |
| 114 | + record DummyEntity(@Id Long id, String name) { |
| 115 | + } |
| 116 | + |
| 117 | + interface DummyEntityRepository extends CrudRepository<DummyEntity, Long> { |
| 118 | + |
| 119 | + @Nullable |
| 120 | + @Query("select * from someTable") |
| 121 | + DummyEntity plainQuery(); |
| 122 | + |
| 123 | + @Nullable |
| 124 | + @Query("select * from #{#tableName}") |
| 125 | + DummyEntity tableNameQuery(); |
| 126 | + } |
| 127 | + |
| 128 | + @Table(name = "ReNamed", schema = "someSchema") |
| 129 | + record RenamedEntity(@Id Long id, String name) { |
| 130 | + } |
| 131 | + |
| 132 | + interface RenamedEntityRepository extends CrudRepository<RenamedEntity, Long> { |
| 133 | + |
| 134 | + @Nullable |
| 135 | + @Query("select * from #{#tableName}") |
| 136 | + DummyEntity tableNameQuery(); |
| 137 | + |
| 138 | + @Nullable |
| 139 | + @Query("select * from #{#qualifiedTableName}") |
| 140 | + DummyEntity qualifiedTableNameQuery(); |
| 141 | + } |
| 142 | +} |
0 commit comments